Krolyxon
Commit 12d635b

Branches

Refs →

nsxivwindow.c

541 lines · 14.5 KB · C

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