Krolyxon
Commit 675db4b

Branches

Refs →

nsxivwindow.c

554 lines · 14.6 KB · C

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