Krolyxon
Commit cd710f5

Branches

Refs →

nsxivwindow.c

534 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_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 != None &&
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 ? XrmGetStringDatabase(res_man) : None;
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 INIT_ATOM_(WM_DELETE_WINDOW);
162 INIT_ATOM_(_NET_WM_NAME);
163 INIT_ATOM_(_NET_WM_ICON_NAME);
164 INIT_ATOM_(_NET_WM_ICON);
165 INIT_ATOM_(_NET_WM_STATE);
166 INIT_ATOM_(_NET_WM_PID);
167 INIT_ATOM_(_NET_WM_STATE_FULLSCREEN);
168}
169
170void win_open(win_t *win)
171{
172 int c, i, j, n;
173 long parent;
174 win_env_t *e;
175 XClassHint classhint;
176 unsigned long *icon_data;
177 XColor col;
178 Cursor *cnone = &cursors[CURSOR_NONE].icon;
179 char none_data[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
180 Pixmap none;
181 int gmask;
182 XSizeHints sizehints;
183 XWMHints hints;
184 pid_t pid;
185 char hostname[256];
186 XSetWindowAttributes attrs;
187
188 e = &win->env;
189 parent = options->embed != 0 ? options->embed : RootWindow(e->dpy, e->scr);
190
191 sizehints.flags = PWinGravity;
192 sizehints.win_gravity = NorthWestGravity;
193
194 /* determine window offsets, width & height */
195 if (options->geometry == NULL)
196 gmask = 0;
197 else
198 gmask = XParseGeometry(options->geometry, &win->x, &win->y,
199 &win->w, &win->h);
200 if ((gmask & WidthValue) != 0)
201 sizehints.flags |= USSize;
202 else
203 win->w = WIN_WIDTH;
204 if ((gmask & HeightValue) != 0)
205 sizehints.flags |= USSize;
206 else
207 win->h = WIN_HEIGHT;
208 if ((gmask & XValue) != 0) {
209 if ((gmask & XNegative) != 0) {
210 win->x += e->scrw - win->w;
211 sizehints.win_gravity = NorthEastGravity;
212 }
213 sizehints.flags |= USPosition;
214 } else {
215 win->x = 0;
216 }
217 if ((gmask & YValue) != 0) {
218 if ((gmask & YNegative) != 0) {
219 win->y += e->scrh - win->h;
220 sizehints.win_gravity = sizehints.win_gravity == NorthEastGravity
221 ? SouthEastGravity : SouthWestGravity;
222 }
223 sizehints.flags |= USPosition;
224 } else {
225 win->y = 0;
226 }
227
228 attrs.colormap = e->cmap;
229 attrs.border_pixel = 0;
230
231 win->xwin = XCreateWindow(e->dpy, parent, win->x, win->y, win->w, win->h, 0,
232 e->depth, InputOutput, e->vis,
233 CWColormap | CWBorderPixel, &attrs);
234 if (win->xwin == None)
235 error(EXIT_FAILURE, 0, "Error creating X window");
236
237 /* set the _NET_WM_PID */
238 pid = getpid();
239 XChangeProperty(e->dpy, win->xwin, atoms[ATOM__NET_WM_PID], XA_CARDINAL,
240 sizeof(pid_t) * 8, PropModeReplace, (unsigned char *) &pid, 1);
241 if (gethostname(hostname, ARRLEN(hostname)) == 0) {
242 XTextProperty tp;
243 tp.value = (unsigned char *)hostname;
244 tp.nitems = strnlen(hostname, ARRLEN(hostname));
245 tp.encoding = XA_STRING;
246 tp.format = 8;
247 XSetWMClientMachine(e->dpy, win->xwin, &tp);
248 }
249
250 XSelectInput(e->dpy, win->xwin,
251 ButtonReleaseMask | ButtonPressMask | KeyPressMask |
252 PointerMotionMask | StructureNotifyMask);
253
254 for (i = 0; i < ARRLEN(cursors); i++) {
255 if (i != CURSOR_NONE)
256 cursors[i].icon = XCreateFontCursor(e->dpy, cursors[i].name);
257 }
258 if (XAllocNamedColor(e->dpy, e->cmap, "black", &col, &col) == 0)
259 error(EXIT_FAILURE, 0, "Error allocating color 'black'");
260
261 none = XCreateBitmapFromData(e->dpy, win->xwin, none_data, 8, 8);
262 *cnone = XCreatePixmapCursor(e->dpy, none, none, &col, &col, 0, 0);
263
264 gc = XCreateGC(e->dpy, win->xwin, 0, None);
265
266 n = icons[ARRLEN(icons)-1].size;
267 icon_data = emalloc((n * n + 2) * sizeof(*icon_data));
268
269 for (i = 0; i < ARRLEN(icons); i++) {
270 n = 0;
271 icon_data[n++] = icons[i].size;
272 icon_data[n++] = icons[i].size;
273
274 for (j = 0; j < icons[i].cnt; j++) {
275 for (c = icons[i].data[j] >> 4; c >= 0; c--)
276 icon_data[n++] = icon_colors[icons[i].data[j] & 0x0F];
277 }
278 XChangeProperty(e->dpy, win->xwin, atoms[ATOM__NET_WM_ICON], XA_CARDINAL, 32,
279 i == 0 ? PropModeReplace : PropModeAppend,
280 (unsigned char *) icon_data, n);
281 }
282 free(icon_data);
283
284 /* These two atoms won't change and thus only need to be set once. */
285 XStoreName(win->env.dpy, win->xwin, "nsxiv");
286 XSetIconName(win->env.dpy, win->xwin, "nsxiv");
287
288 classhint.res_class = RES_CLASS;
289 classhint.res_name = options->res_name != NULL ? options->res_name : "nsxiv";
290 XSetClassHint(e->dpy, win->xwin, &classhint);
291
292 XSetWMProtocols(e->dpy, win->xwin, &atoms[ATOM_WM_DELETE_WINDOW], 1);
293
294 sizehints.width = win->w;
295 sizehints.height = win->h;
296 sizehints.x = win->x;
297 sizehints.y = win->y;
298 XSetWMNormalHints(win->env.dpy, win->xwin, &sizehints);
299
300 hints.flags = InputHint | StateHint;
301 hints.input = 1;
302 hints.initial_state = NormalState;
303 XSetWMHints(win->env.dpy, win->xwin, &hints);
304
305 win->h -= win->bar.h;
306
307 win->buf.w = e->scrw;
308 win->buf.h = e->scrh;
309 win->buf.pm = XCreatePixmap(e->dpy, win->xwin, win->buf.w, win->buf.h, e->depth);
310
311 XSetForeground(e->dpy, gc, win->win_bg.pixel);
312 XFillRectangle(e->dpy, win->buf.pm, gc, 0, 0, win->buf.w, win->buf.h);
313 XSetWindowBackgroundPixmap(e->dpy, win->xwin, win->buf.pm);
314 XMapWindow(e->dpy, win->xwin);
315 XFlush(e->dpy);
316
317 if (options->fullscreen)
318 win_toggle_fullscreen(win);
319}
320
321CLEANUP void win_close(win_t *win)
322{
323 int i;
324
325 for (i = 0; i < ARRLEN(cursors); i++)
326 XFreeCursor(win->env.dpy, cursors[i].icon);
327
328 XFreeGC(win->env.dpy, gc);
329
330 XDestroyWindow(win->env.dpy, win->xwin);
331 XCloseDisplay(win->env.dpy);
332}
333
334bool win_configure(win_t *win, XConfigureEvent *c)
335{
336 bool changed;
337
338 changed = win->w != c->width || win->h + win->bar.h != c->height;
339
340 win->x = c->x;
341 win->y = c->y;
342 win->w = c->width;
343 win->h = c->height - win->bar.h;
344 win->bw = c->border_width;
345
346 return changed;
347}
348
349void win_toggle_fullscreen(win_t *win)
350{
351 XEvent ev;
352 XClientMessageEvent *cm;
353
354 memset(&ev, 0, sizeof(ev));
355 ev.type = ClientMessage;
356
357 cm = &ev.xclient;
358 cm->window = win->xwin;
359 cm->message_type = atoms[ATOM__NET_WM_STATE];
360 cm->format = 32;
361 cm->data.l[0] = 2; // toggle
362 cm->data.l[1] = atoms[ATOM__NET_WM_STATE_FULLSCREEN];
363
364 XSendEvent(win->env.dpy, DefaultRootWindow(win->env.dpy), False,
365 SubstructureNotifyMask | SubstructureRedirectMask, &ev);
366}
367
368void win_toggle_bar(win_t *win)
369{
370 if (win->bar.h != 0) {
371 win->h += win->bar.h;
372 win->bar.h = 0;
373 } else {
374 win->bar.h = barheight;
375 win->h -= win->bar.h;
376 }
377}
378
379void win_clear(win_t *win)
380{
381 win_env_t *e;
382
383 e = &win->env;
384
385 if (win->w > win->buf.w || win->h + win->bar.h > win->buf.h) {
386 XFreePixmap(e->dpy, win->buf.pm);
387 win->buf.w = MAX(win->buf.w, win->w);
388 win->buf.h = MAX(win->buf.h, win->h + win->bar.h);
389 win->buf.pm = XCreatePixmap(e->dpy, win->xwin,
390 win->buf.w, win->buf.h, e->depth);
391 }
392 XSetForeground(e->dpy, gc, win->win_bg.pixel);
393 XFillRectangle(e->dpy, win->buf.pm, gc, 0, 0, win->buf.w, win->buf.h);
394}
395
396#if HAVE_LIBFONTS
397int win_draw_text(win_t *win, XftDraw *d, const XftColor *color, int x, int y,
398 char *text, int len, int w)
399{
400 int err, tw = 0;
401 char *t, *next;
402 uint32_t rune;
403 XftFont *f;
404 FcCharSet *fccharset;
405 XGlyphInfo ext;
406
407 for (t = text; t - text < len; t = next) {
408 next = utf8_decode(t, &rune, &err);
409 if (XftCharExists(win->env.dpy, font, rune)) {
410 f = font;
411 } else { /* fallback font */
412 fccharset = FcCharSetCreate();
413 FcCharSetAddChar(fccharset, rune);
414 f = XftFontOpen(win->env.dpy, win->env.scr, FC_CHARSET, FcTypeCharSet,
415 fccharset, FC_SCALABLE, FcTypeBool, FcTrue,
416 FC_SIZE, FcTypeDouble, fontsize, NULL);
417 FcCharSetDestroy(fccharset);
418 }
419 XftTextExtentsUtf8(win->env.dpy, f, (XftChar8*)t, next - t, &ext);
420 tw += ext.xOff;
421 if (tw <= w) {
422 XftDrawStringUtf8(d, color, f, x, y, (XftChar8*)t, next - t);
423 x += ext.xOff;
424 }
425 if (f != font)
426 XftFontClose(win->env.dpy, f);
427 }
428 return tw;
429}
430
431void win_draw_bar(win_t *win)
432{
433 int len, x, y, w, tw;
434 win_env_t *e;
435 win_bar_t *l, *r;
436 XftDraw *d;
437
438 if ((l = &win->bar.l)->buf == NULL || (r = &win->bar.r)->buf == NULL)
439 return;
440
441 e = &win->env;
442 y = win->h + font->ascent + V_TEXT_PAD;
443 w = win->w - 2*H_TEXT_PAD;
444 d = XftDrawCreate(e->dpy, win->buf.pm, e->vis, e->cmap);
445
446 XSetForeground(e->dpy, gc, win->bar_bg.pixel);
447 XFillRectangle(e->dpy, win->buf.pm, gc, 0, win->h, win->w, win->bar.h);
448
449 XSetForeground(e->dpy, gc, win->win_bg.pixel);
450 XSetBackground(e->dpy, gc, win->bar_bg.pixel);
451
452 if ((len = strlen(r->buf)) > 0) {
453 if ((tw = TEXTWIDTH(win, r->buf, len)) > w)
454 return;
455 x = win->w - tw - H_TEXT_PAD;
456 w -= tw;
457 win_draw_text(win, d, &win->bar_fg, x, y, r->buf, len, tw);
458 }
459 if ((len = strlen(l->buf)) > 0) {
460 x = H_TEXT_PAD;
461 w -= 2 * H_TEXT_PAD; /* gap between left and right parts */
462 win_draw_text(win, d, &win->bar_fg, x, y, l->buf, len, w);
463 }
464 XftDrawDestroy(d);
465}
466#else
467void win_draw_bar(win_t *win){}
468#endif /* HAVE_LIBFONTS */
469
470void win_draw(win_t *win)
471{
472 if (win->bar.h > 0)
473 win_draw_bar(win);
474
475 XSetWindowBackgroundPixmap(win->env.dpy, win->xwin, win->buf.pm);
476 XClearWindow(win->env.dpy, win->xwin);
477 XFlush(win->env.dpy);
478}
479
480void win_draw_rect(win_t *win, int x, int y, int w, int h, bool fill, int lw,
481 unsigned long col)
482{
483 XGCValues gcval;
484
485 gcval.line_width = lw;
486 gcval.foreground = col;
487 XChangeGC(win->env.dpy, gc, GCForeground | GCLineWidth, &gcval);
488
489 if (fill)
490 XFillRectangle(win->env.dpy, win->buf.pm, gc, x, y, w, h);
491 else
492 XDrawRectangle(win->env.dpy, win->buf.pm, gc, x, y, w, h);
493}
494
495void win_set_title(win_t *win, const char *path)
496{
497 enum { title_max = 512 };
498 char title[title_max];
499 const char *basename = strrchr(path, '/') + 1;
500
501 /* Return if window is not ready yet */
502 if (win->xwin == None)
503 return;
504
505 snprintf(title, title_max, "%s%s", options->title_prefix,
506 options->title_suffixmode == SUFFIX_BASENAME ? basename : path);
507 if (options->title_suffixmode == SUFFIX_EMPTY)
508 *(title+strlen(options->title_prefix)) = '\0';
509
510 XChangeProperty(win->env.dpy, win->xwin, atoms[ATOM__NET_WM_NAME],
511 XInternAtom(win->env.dpy, "UTF8_STRING", False), 8,
512 PropModeReplace, (unsigned char *) title, strlen(title));
513 XChangeProperty(win->env.dpy, win->xwin, atoms[ATOM__NET_WM_ICON_NAME],
514 XInternAtom(win->env.dpy, "UTF8_STRING", False), 8,
515 PropModeReplace, (unsigned char *) title, strlen(title));
516}
517
518void win_set_cursor(win_t *win, cursor_t cursor)
519{
520 if (cursor >= 0 && cursor < ARRLEN(cursors)) {
521 XDefineCursor(win->env.dpy, win->xwin, cursors[cursor].icon);
522 XFlush(win->env.dpy);
523 }
524}
525
526void win_cursor_pos(win_t *win, int *x, int *y)
527{
528 int i;
529 unsigned int ui;
530 Window w;
531
532 if (!XQueryPointer(win->env.dpy, win->xwin, &w, &w, &i, &i, x, y, &ui))
533 *x = *y = 0;
534}