Krolyxon
Commit 16b966b

Branches

Refs →

nsxivwindow.c

540 lines · 14.5 KB · C

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