Krolyxon
Commit d8ec6f9

Branches

Refs →

nsxivwindow.c

515 lines · 13.3 KB · C

1/* Copyright 2011-2013 Bert Muennich
2 *
3 * This file is part of sxiv.
4 *
5 * sxiv is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published
7 * by the Free Software Foundation; either version 2 of the License,
8 * or (at your option) any later version.
9 *
10 * sxiv is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with sxiv. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#include "sxiv.h"
20#define _WINDOW_CONFIG
21#include "config.h"
22#include "icon/data.h"
23#include "utf8.h"
24
25#include <stdlib.h>
26#include <string.h>
27#include <locale.h>
28#include <unistd.h>
29#include <X11/cursorfont.h>
30#include <X11/Xatom.h>
31#include <X11/Xresource.h>
32
33#define RES_CLASS "Sxiv"
34
35enum {
36 H_TEXT_PAD = 5,
37 V_TEXT_PAD = 1
38};
39
40static struct {
41 int name;
42 Cursor icon;
43} cursors[CURSOR_COUNT] = {
44 { XC_left_ptr }, { XC_dotbox }, { XC_watch },
45 { XC_sb_left_arrow }, { XC_sb_right_arrow }
46};
47
48static GC gc;
49
50static XftFont *font;
51static int fontheight;
52static double fontsize;
53static int barheight;
54
55Atom atoms[ATOM_COUNT];
56
57void win_init_font(const win_env_t *e, const char *fontstr)
58{
59 if ((font = XftFontOpenName(e->dpy, e->scr, fontstr)) == NULL)
60 error(EXIT_FAILURE, 0, "Error loading font '%s'", fontstr);
61 fontheight = font->ascent + font->descent;
62 FcPatternGetDouble(font->pattern, FC_SIZE, 0, &fontsize);
63 barheight = fontheight + 2 * V_TEXT_PAD;
64}
65
66void win_alloc_color(const win_env_t *e, const char *name, XftColor *col)
67{
68 if (!XftColorAllocName(e->dpy, e->vis,
69 e->cmap, name, col))
70 {
71 error(EXIT_FAILURE, 0, "Error allocating color '%s'", name);
72 }
73}
74
75const char* win_res(XrmDatabase db, const char *name, const char *def)
76{
77 char *type;
78 XrmValue ret;
79
80 if (db != None &&
81 XrmGetResource(db, name, name, &type, &ret) &&
82 STREQ(type, "String"))
83 {
84 return ret.addr;
85 } else {
86 return def;
87 }
88}
89
90#define INIT_ATOM_(atom) \
91 atoms[ATOM_##atom] = XInternAtom(e->dpy, #atom, False);
92
93void win_init(win_t *win)
94{
95 win_env_t *e;
96 const char *bg, *fg, *f;
97 char *res_man;
98 XrmDatabase db;
99 XVisualInfo vis;
100 XWindowAttributes attr;
101 Window parent;
102
103 memset(win, 0, sizeof(win_t));
104
105 e = &win->env;
106 if ((e->dpy = XOpenDisplay(NULL)) == NULL)
107 error(EXIT_FAILURE, 0, "Error opening X display");
108
109 e->scr = DefaultScreen(e->dpy);
110 e->scrw = DisplayWidth(e->dpy, e->scr);
111 e->scrh = DisplayHeight(e->dpy, e->scr);
112
113 parent = options->embed != 0 ? options->embed : RootWindow(e->dpy, e->scr);
114
115 if (options->embed == 0) {
116 e->depth = DefaultDepth(e->dpy, e->scr);
117 } else {
118 XGetWindowAttributes(e->dpy, parent, &attr);
119 e->depth = attr.depth;
120 }
121
122 XMatchVisualInfo(e->dpy, e->scr, e->depth, TrueColor, &vis);
123 e->vis = vis.visual;
124 e->cmap = XCreateColormap(e->dpy, parent, e->vis, None);
125
126 if (setlocale(LC_CTYPE, "") == NULL || XSupportsLocale() == 0)
127 error(0, 0, "No locale support");
128
129 XrmInitialize();
130 res_man = XResourceManagerString(e->dpy);
131 db = res_man != NULL ? XrmGetStringDatabase(res_man) : None;
132
133 f = win_res(db, RES_CLASS ".font", "monospace-8");
134 win_init_font(e, f);
135
136 bg = win_res(db, RES_CLASS ".background", "white");
137 fg = win_res(db, RES_CLASS ".foreground", "black");
138 win_alloc_color(e, bg, &win->bg);
139 win_alloc_color(e, fg, &win->fg);
140
141 win->bar.l.size = BAR_L_LEN;
142 win->bar.r.size = BAR_R_LEN;
143 /* 3 padding bytes needed by utf8_decode */
144 win->bar.l.buf = emalloc(win->bar.l.size + 3);
145 win->bar.l.buf[0] = '\0';
146 win->bar.r.buf = emalloc(win->bar.r.size + 3);
147 win->bar.r.buf[0] = '\0';
148 win->bar.h = options->hide_bar ? 0 : barheight;
149
150 INIT_ATOM_(WM_DELETE_WINDOW);
151 INIT_ATOM_(_NET_WM_NAME);
152 INIT_ATOM_(_NET_WM_ICON_NAME);
153 INIT_ATOM_(_NET_WM_ICON);
154 INIT_ATOM_(_NET_WM_STATE);
155 INIT_ATOM_(_NET_WM_PID);
156 INIT_ATOM_(_NET_WM_STATE_FULLSCREEN);
157}
158
159void win_open(win_t *win)
160{
161 int c, i, j, n;
162 long parent;
163 win_env_t *e;
164 XClassHint classhint;
165 unsigned long *icon_data;
166 XColor col;
167 Cursor *cnone = &cursors[CURSOR_NONE].icon;
168 char none_data[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
169 Pixmap none;
170 int gmask;
171 XSizeHints sizehints;
172 XWMHints hints;
173 pid_t pid;
174 char hostname[255];
175
176 e = &win->env;
177 parent = options->embed != 0 ? options->embed : RootWindow(e->dpy, e->scr);
178
179 sizehints.flags = PWinGravity;
180 sizehints.win_gravity = NorthWestGravity;
181
182 /* determine window offsets, width & height */
183 if (options->geometry == NULL)
184 gmask = 0;
185 else
186 gmask = XParseGeometry(options->geometry, &win->x, &win->y,
187 &win->w, &win->h);
188 if ((gmask & WidthValue) != 0)
189 sizehints.flags |= USSize;
190 else
191 win->w = WIN_WIDTH;
192 if ((gmask & HeightValue) != 0)
193 sizehints.flags |= USSize;
194 else
195 win->h = WIN_HEIGHT;
196 if ((gmask & XValue) != 0) {
197 if ((gmask & XNegative) != 0) {
198 win->x += e->scrw - win->w;
199 sizehints.win_gravity = NorthEastGravity;
200 }
201 sizehints.flags |= USPosition;
202 } else {
203 win->x = 0;
204 }
205 if ((gmask & YValue) != 0) {
206 if ((gmask & YNegative) != 0) {
207 win->y += e->scrh - win->h;
208 sizehints.win_gravity = sizehints.win_gravity == NorthEastGravity
209 ? SouthEastGravity : SouthWestGravity;
210 }
211 sizehints.flags |= USPosition;
212 } else {
213 win->y = 0;
214 }
215
216 win->xwin = XCreateWindow(e->dpy, parent,
217 win->x, win->y, win->w, win->h, 0,
218 e->depth, InputOutput, e->vis, 0, NULL);
219 if (win->xwin == None)
220 error(EXIT_FAILURE, 0, "Error creating X window");
221
222 /* set the _NET_WM_PID */
223 pid = getpid();
224 XChangeProperty(e->dpy, win->xwin,
225 atoms[ATOM__NET_WM_PID], XA_CARDINAL, sizeof(pid_t) * 8,
226 PropModeReplace, (unsigned char *) &pid, 1);
227
228 /* set the _NET_WM_PID */
229 if (gethostname(hostname, sizeof(hostname)) == 0) {
230 XTextProperty tp;
231 tp.value = (unsigned char *)hostname;
232 tp.nitems = strnlen(hostname, sizeof(hostname));
233 tp.encoding = XA_STRING;
234 tp.format = 8;
235 XSetWMClientMachine(e->dpy, win->xwin, &tp);
236 }
237
238 XSelectInput(e->dpy, win->xwin,
239 ButtonReleaseMask | ButtonPressMask | KeyPressMask |
240 PointerMotionMask | StructureNotifyMask);
241
242 for (i = 0; i < ARRLEN(cursors); i++) {
243 if (i != CURSOR_NONE)
244 cursors[i].icon = XCreateFontCursor(e->dpy, cursors[i].name);
245 }
246 if (XAllocNamedColor(e->dpy, e->cmap, "black",
247 &col, &col) == 0)
248 {
249 error(EXIT_FAILURE, 0, "Error allocating color 'black'");
250 }
251 none = XCreateBitmapFromData(e->dpy, win->xwin, none_data, 8, 8);
252 *cnone = XCreatePixmapCursor(e->dpy, none, none, &col, &col, 0, 0);
253
254 gc = XCreateGC(e->dpy, win->xwin, 0, None);
255
256 n = icons[ARRLEN(icons)-1].size;
257 icon_data = emalloc((n * n + 2) * sizeof(*icon_data));
258
259 for (i = 0; i < ARRLEN(icons); i++) {
260 n = 0;
261 icon_data[n++] = icons[i].size;
262 icon_data[n++] = icons[i].size;
263
264 for (j = 0; j < icons[i].cnt; j++) {
265 for (c = icons[i].data[j] >> 4; c >= 0; c--)
266 icon_data[n++] = icon_colors[icons[i].data[j] & 0x0F];
267 }
268 XChangeProperty(e->dpy, win->xwin,
269 atoms[ATOM__NET_WM_ICON], XA_CARDINAL, 32,
270 i == 0 ? PropModeReplace : PropModeAppend,
271 (unsigned char *) icon_data, n);
272 }
273 free(icon_data);
274
275 win_set_title(win, "sxiv");
276
277 classhint.res_class = RES_CLASS;
278 classhint.res_name = options->res_name != NULL ? options->res_name : "sxiv";
279 XSetClassHint(e->dpy, win->xwin, &classhint);
280
281 XSetWMProtocols(e->dpy, win->xwin, &atoms[ATOM_WM_DELETE_WINDOW], 1);
282
283 sizehints.width = win->w;
284 sizehints.height = win->h;
285 sizehints.x = win->x;
286 sizehints.y = win->y;
287 XSetWMNormalHints(win->env.dpy, win->xwin, &sizehints);
288
289 hints.flags = InputHint | StateHint;
290 hints.input = 1;
291 hints.initial_state = NormalState;
292 XSetWMHints(win->env.dpy, win->xwin, &hints);
293
294 win->h -= win->bar.h;
295
296 win->buf.w = e->scrw;
297 win->buf.h = e->scrh;
298 win->buf.pm = XCreatePixmap(e->dpy, win->xwin,
299 win->buf.w, win->buf.h, e->depth);
300 XSetForeground(e->dpy, gc, win->bg.pixel);
301 XFillRectangle(e->dpy, win->buf.pm, gc, 0, 0, win->buf.w, win->buf.h);
302 XSetWindowBackgroundPixmap(e->dpy, win->xwin, win->buf.pm);
303
304 XMapWindow(e->dpy, win->xwin);
305 XFlush(e->dpy);
306
307 if (options->fullscreen)
308 win_toggle_fullscreen(win);
309}
310
311CLEANUP void win_close(win_t *win)
312{
313 int i;
314
315 for (i = 0; i < ARRLEN(cursors); i++)
316 XFreeCursor(win->env.dpy, cursors[i].icon);
317
318 XFreeGC(win->env.dpy, gc);
319
320 XDestroyWindow(win->env.dpy, win->xwin);
321 XCloseDisplay(win->env.dpy);
322}
323
324bool win_configure(win_t *win, XConfigureEvent *c)
325{
326 bool changed;
327
328 changed = win->w != c->width || win->h + win->bar.h != c->height;
329
330 win->x = c->x;
331 win->y = c->y;
332 win->w = c->width;
333 win->h = c->height - win->bar.h;
334 win->bw = c->border_width;
335
336 return changed;
337}
338
339void win_toggle_fullscreen(win_t *win)
340{
341 XEvent ev;
342 XClientMessageEvent *cm;
343
344 memset(&ev, 0, sizeof(ev));
345 ev.type = ClientMessage;
346
347 cm = &ev.xclient;
348 cm->window = win->xwin;
349 cm->message_type = atoms[ATOM__NET_WM_STATE];
350 cm->format = 32;
351 cm->data.l[0] = 2; // toggle
352 cm->data.l[1] = atoms[ATOM__NET_WM_STATE_FULLSCREEN];
353
354 XSendEvent(win->env.dpy, DefaultRootWindow(win->env.dpy), False,
355 SubstructureNotifyMask | SubstructureRedirectMask, &ev);
356}
357
358void win_toggle_bar(win_t *win)
359{
360 if (win->bar.h != 0) {
361 win->h += win->bar.h;
362 win->bar.h = 0;
363 } else {
364 win->bar.h = barheight;
365 win->h -= win->bar.h;
366 }
367}
368
369void win_clear(win_t *win)
370{
371 win_env_t *e;
372
373 e = &win->env;
374
375 if (win->w > win->buf.w || win->h + win->bar.h > win->buf.h) {
376 XFreePixmap(e->dpy, win->buf.pm);
377 win->buf.w = MAX(win->buf.w, win->w);
378 win->buf.h = MAX(win->buf.h, win->h + win->bar.h);
379 win->buf.pm = XCreatePixmap(e->dpy, win->xwin,
380 win->buf.w, win->buf.h, e->depth);
381 }
382 XSetForeground(e->dpy, gc, win->bg.pixel);
383 XFillRectangle(e->dpy, win->buf.pm, gc, 0, 0, win->buf.w, win->buf.h);
384}
385
386#define TEXTWIDTH(win, text, len) \
387 win_draw_text(win, NULL, NULL, 0, 0, text, len, 0)
388
389int win_draw_text(win_t *win, XftDraw *d, const XftColor *color, int x, int y,
390 char *text, int len, int w)
391{
392 int err, tw = 0;
393 char *t, *next;
394 uint32_t rune;
395 XftFont *f;
396 FcCharSet *fccharset;
397 XGlyphInfo ext;
398
399 for (t = text; t - text < len; t = next) {
400 next = utf8_decode(t, &rune, &err);
401 if (XftCharExists(win->env.dpy, font, rune)) {
402 f = font;
403 } else { /* fallback font */
404 fccharset = FcCharSetCreate();
405 FcCharSetAddChar(fccharset, rune);
406 f = XftFontOpen(win->env.dpy, win->env.scr, FC_CHARSET, FcTypeCharSet,
407 fccharset, FC_SCALABLE, FcTypeBool, FcTrue,
408 FC_SIZE, FcTypeDouble, fontsize, NULL);
409 FcCharSetDestroy(fccharset);
410 }
411 XftTextExtentsUtf8(win->env.dpy, f, (XftChar8*)t, next - t, &ext);
412 tw += ext.xOff;
413 if (tw <= w) {
414 XftDrawStringUtf8(d, color, f, x, y, (XftChar8*)t, next - t);
415 x += ext.xOff;
416 }
417 if (f != font)
418 XftFontClose(win->env.dpy, f);
419 }
420 return tw;
421}
422
423void win_draw_bar(win_t *win)
424{
425 int len, x, y, w, tw;
426 win_env_t *e;
427 win_bar_t *l, *r;
428 XftDraw *d;
429
430 if ((l = &win->bar.l)->buf == NULL || (r = &win->bar.r)->buf == NULL)
431 return;
432
433 e = &win->env;
434 y = win->h + font->ascent + V_TEXT_PAD;
435 w = win->w - 2*H_TEXT_PAD;
436 d = XftDrawCreate(e->dpy, win->buf.pm, e->vis,
437 e->cmap);
438
439 XSetForeground(e->dpy, gc, win->fg.pixel);
440 XFillRectangle(e->dpy, win->buf.pm, gc, 0, win->h, win->w, win->bar.h);
441
442 XSetForeground(e->dpy, gc, win->bg.pixel);
443 XSetBackground(e->dpy, gc, win->fg.pixel);
444
445 if ((len = strlen(r->buf)) > 0) {
446 if ((tw = TEXTWIDTH(win, r->buf, len)) > w)
447 return;
448 x = win->w - tw - H_TEXT_PAD;
449 w -= tw;
450 win_draw_text(win, d, &win->bg, x, y, r->buf, len, tw);
451 }
452 if ((len = strlen(l->buf)) > 0) {
453 x = H_TEXT_PAD;
454 w -= 2 * H_TEXT_PAD; /* gap between left and right parts */
455 win_draw_text(win, d, &win->bg, x, y, l->buf, len, w);
456 }
457 XftDrawDestroy(d);
458}
459
460void win_draw(win_t *win)
461{
462 if (win->bar.h > 0)
463 win_draw_bar(win);
464
465 XSetWindowBackgroundPixmap(win->env.dpy, win->xwin, win->buf.pm);
466 XClearWindow(win->env.dpy, win->xwin);
467 XFlush(win->env.dpy);
468}
469
470void win_draw_rect(win_t *win, int x, int y, int w, int h, bool fill, int lw,
471 unsigned long col)
472{
473 XGCValues gcval;
474
475 gcval.line_width = lw;
476 gcval.foreground = col;
477 XChangeGC(win->env.dpy, gc, GCForeground | GCLineWidth, &gcval);
478
479 if (fill)
480 XFillRectangle(win->env.dpy, win->buf.pm, gc, x, y, w, h);
481 else
482 XDrawRectangle(win->env.dpy, win->buf.pm, gc, x, y, w, h);
483}
484
485void win_set_title(win_t *win, const char *title)
486{
487 XStoreName(win->env.dpy, win->xwin, title);
488 XSetIconName(win->env.dpy, win->xwin, title);
489
490 XChangeProperty(win->env.dpy, win->xwin, atoms[ATOM__NET_WM_NAME],
491 XInternAtom(win->env.dpy, "UTF8_STRING", False), 8,
492 PropModeReplace, (unsigned char *) title, strlen(title));
493 XChangeProperty(win->env.dpy, win->xwin, atoms[ATOM__NET_WM_ICON_NAME],
494 XInternAtom(win->env.dpy, "UTF8_STRING", False), 8,
495 PropModeReplace, (unsigned char *) title, strlen(title));
496}
497
498void win_set_cursor(win_t *win, cursor_t cursor)
499{
500 if (cursor >= 0 && cursor < ARRLEN(cursors)) {
501 XDefineCursor(win->env.dpy, win->xwin, cursors[cursor].icon);
502 XFlush(win->env.dpy);
503 }
504}
505
506void win_cursor_pos(win_t *win, int *x, int *y)
507{
508 int i;
509 unsigned int ui;
510 Window w;
511
512 if (!XQueryPointer(win->env.dpy, win->xwin, &w, &w, &i, &i, x, y, &ui))
513 *x = *y = 0;
514}
515