Krolyxon
Commit 7e7eaf0

Branches

Refs →

nsxivwindow.c

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