Krolyxon
Commit 502b303

Branches

Refs →

nsxivwindow.c

545 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 },
60 { XC_dotbox },
61 { XC_fleur },
62 { XC_watch },
63 { XC_sb_left_arrow },
64 { XC_sb_right_arrow }
65};
66
67#if HAVE_LIBFONTS
68static XftFont *font;
69static double fontsize;
70#endif
71
72#if HAVE_LIBFONTS
73static void win_init_font(const win_env_t *e, const char *fontstr)
74{
75 int fontheight = 0;
76 if ((font = XftFontOpenName(e->dpy, e->scr, fontstr)) == NULL)
77 error(EXIT_FAILURE, 0, "Error loading font '%s'", fontstr);
78 fontheight = font->ascent + font->descent;
79 FcPatternGetDouble(font->pattern, FC_SIZE, 0, &fontsize);
80 barheight = fontheight + 2 * V_TEXT_PAD;
81}
82
83static void xft_alloc_color(const win_env_t *e, const char *name, XftColor *col)
84{
85 if (!XftColorAllocName(e->dpy, e->vis, e->cmap, name, col))
86 error(EXIT_FAILURE, 0, "Error allocating color '%s'", name);
87}
88#endif /* HAVE_LIBFONTS */
89
90static void win_alloc_color(const win_env_t *e, const char *name, XColor *col)
91{
92 XColor screen;
93 if (!XAllocNamedColor(e->dpy, e->cmap, name, &screen, col))
94 error(EXIT_FAILURE, 0, "Error allocating color '%s'", name);
95}
96
97static const char *win_res(XrmDatabase db, const char *name, const char *def)
98{
99 char *type;
100 XrmValue ret;
101
102 if (db != NULL &&
103 XrmGetResource(db, name, name, &type, &ret) &&
104 STREQ(type, "String") && *ret.addr != '\0')
105 {
106 return ret.addr;
107 } else {
108 return def;
109 }
110}
111
112void win_init(win_t *win)
113{
114 win_env_t *e;
115 const char *win_bg, *win_fg, *mrk_fg;
116 char *res_man;
117 XrmDatabase db;
118#if HAVE_LIBFONTS
119 const char *bar_fg, *bar_bg, *f;
120
121 static char lbuf[512 + UTF8_PADDING], rbuf[64 + UTF8_PADDING];
122#endif
123
124 memset(win, 0, sizeof(*win));
125
126 e = &win->env;
127 if ((e->dpy = XOpenDisplay(NULL)) == NULL)
128 error(EXIT_FAILURE, 0, "Error opening X display");
129
130 e->scr = DefaultScreen(e->dpy);
131 e->scrw = DisplayWidth(e->dpy, e->scr);
132 e->scrh = DisplayHeight(e->dpy, e->scr);
133 e->depth = DefaultDepth(e->dpy, e->scr);
134 e->vis = DefaultVisual(e->dpy, e->scr);
135 e->cmap = DefaultColormap(e->dpy, e->scr);
136
137 if (setlocale(LC_CTYPE, "") == NULL || XSupportsLocale() == 0)
138 error(0, 0, "No locale support");
139
140 XrmInitialize();
141 res_man = XResourceManagerString(e->dpy);
142 db = res_man == NULL ? NULL : XrmGetStringDatabase(res_man);
143
144 win_bg = win_res(db, RES_CLASS ".window.background", DEFAULT_WIN_BG);
145 win_fg = win_res(db, RES_CLASS ".window.foreground", DEFAULT_WIN_FG);
146 mrk_fg = win_res(db, RES_CLASS ".mark.foreground", DEFAULT_MARK_COLOR ? DEFAULT_MARK_COLOR : win_fg);
147 win_alloc_color(e, win_bg, &win->win_bg);
148 win_alloc_color(e, win_fg, &win->win_fg);
149 win_alloc_color(e, mrk_fg, &win->mrk_fg);
150
151#if HAVE_LIBFONTS
152 bar_bg = win_res(db, RES_CLASS ".bar.background", DEFAULT_BAR_BG ? DEFAULT_BAR_BG : win_bg);
153 bar_fg = win_res(db, RES_CLASS ".bar.foreground", DEFAULT_BAR_FG ? DEFAULT_BAR_FG : win_fg);
154 xft_alloc_color(e, bar_bg, &win->bar_bg);
155 xft_alloc_color(e, bar_fg, &win->bar_fg);
156
157 f = win_res(db, RES_CLASS ".bar.font", DEFAULT_FONT);
158 win_init_font(e, f);
159
160 win->bar.l.buf = lbuf;
161 win->bar.r.buf = rbuf;
162 win->bar.l.size = sizeof(lbuf) - UTF8_PADDING;
163 win->bar.r.size = sizeof(rbuf) - UTF8_PADDING;
164 win->bar.h = options->hide_bar ? 0 : barheight;
165 win->bar.top = TOP_STATUSBAR;
166#endif /* HAVE_LIBFONTS */
167
168 XrmDestroyDatabase(db);
169 INIT_ATOM_(WM_DELETE_WINDOW);
170 INIT_ATOM_(_NET_WM_NAME);
171 INIT_ATOM_(_NET_WM_ICON_NAME);
172 INIT_ATOM_(_NET_WM_ICON);
173 INIT_ATOM_(_NET_WM_STATE);
174 INIT_ATOM_(_NET_WM_PID);
175 INIT_ATOM_(_NET_WM_STATE_FULLSCREEN);
176 INIT_ATOM_(UTF8_STRING);
177 INIT_ATOM_(WM_NAME);
178 INIT_ATOM_(WM_ICON_NAME);
179}
180
181void win_open(win_t *win)
182{
183 int c, i, j, n;
184 Window parent;
185 win_env_t *e;
186 XClassHint classhint;
187 unsigned long *icon_data;
188 XColor col;
189 Cursor *cnone = &cursors[CURSOR_NONE].icon;
190 char none_data[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
191 Pixmap none;
192 int gmask;
193 XSizeHints sizehints;
194 XWMHints hints;
195 long pid;
196 char hostname[256];
197 XSetWindowAttributes attrs;
198 char res_class[] = RES_CLASS;
199 char res_name[] = "nsxiv";
200
201 e = &win->env;
202 parent = options->embed ? options->embed : RootWindow(e->dpy, e->scr);
203
204 sizehints.flags = PWinGravity;
205 sizehints.win_gravity = NorthWestGravity;
206
207 /* determine window offsets, width & height */
208 if (options->geometry == NULL)
209 gmask = 0;
210 else
211 gmask = XParseGeometry(options->geometry, &win->x, &win->y,
212 &win->w, &win->h);
213 if (gmask & WidthValue)
214 sizehints.flags |= USSize;
215 else
216 win->w = WIN_WIDTH;
217 if (gmask & HeightValue)
218 sizehints.flags |= USSize;
219 else
220 win->h = WIN_HEIGHT;
221 if (gmask & XValue) {
222 if (gmask & XNegative) {
223 win->x += e->scrw - win->w;
224 sizehints.win_gravity = NorthEastGravity;
225 }
226 sizehints.flags |= USPosition;
227 } else {
228 win->x = 0;
229 }
230 if (gmask & YValue) {
231 if (gmask & YNegative) {
232 win->y += e->scrh - win->h;
233 sizehints.win_gravity = sizehints.win_gravity == NorthEastGravity ?
234 SouthEastGravity : SouthWestGravity;
235 }
236 sizehints.flags |= USPosition;
237 } else {
238 win->y = 0;
239 }
240
241 attrs.colormap = e->cmap;
242 attrs.border_pixel = 0;
243
244 win->xwin = XCreateWindow(e->dpy, parent, win->x, win->y, win->w, win->h, 0,
245 e->depth, InputOutput, e->vis,
246 CWColormap | CWBorderPixel, &attrs);
247 if (win->xwin == None)
248 error(EXIT_FAILURE, 0, "Error creating X window");
249
250 /* set the _NET_WM_PID */
251 pid = getpid();
252 XChangeProperty(e->dpy, win->xwin, atoms[ATOM__NET_WM_PID], XA_CARDINAL,
253 32, PropModeReplace, (unsigned char *)&pid, 1);
254 if (gethostname(hostname, ARRLEN(hostname)) == 0) {
255 XTextProperty tp;
256 tp.value = (unsigned char *)hostname;
257 tp.nitems = strnlen(hostname, ARRLEN(hostname));
258 tp.encoding = XA_STRING;
259 tp.format = 8;
260 XSetWMClientMachine(e->dpy, win->xwin, &tp);
261 }
262
263 XSelectInput(e->dpy, win->xwin,
264 ButtonReleaseMask | ButtonPressMask | KeyPressMask |
265 PointerMotionMask | StructureNotifyMask);
266
267 for (i = 0; i < (int)ARRLEN(cursors); i++) {
268 if (i != CURSOR_NONE)
269 cursors[i].icon = XCreateFontCursor(e->dpy, cursors[i].name);
270 }
271 if (XAllocNamedColor(e->dpy, e->cmap, "black", &col, &col) == 0)
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 < (int)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 < (int)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, atoms[ATOM__NET_WM_ICON], XA_CARDINAL, 32,
292 i == 0 ? PropModeReplace : PropModeAppend,
293 (unsigned char *)icon_data, n);
294 }
295 free(icon_data);
296
297 win_set_title(win, res_name, strlen(res_name));
298 classhint.res_class = res_class;
299 classhint.res_name = options->res_name != NULL ? options->res_name : res_name;
300 XSetClassHint(e->dpy, win->xwin, &classhint);
301
302 XSetWMProtocols(e->dpy, win->xwin, &atoms[ATOM_WM_DELETE_WINDOW], 1);
303
304 sizehints.width = win->w;
305 sizehints.height = win->h;
306 sizehints.x = win->x;
307 sizehints.y = win->y;
308 XSetWMNormalHints(win->env.dpy, win->xwin, &sizehints);
309
310 hints.flags = InputHint | StateHint;
311 hints.input = 1;
312 hints.initial_state = NormalState;
313 XSetWMHints(win->env.dpy, win->xwin, &hints);
314
315 if (options->fullscreen) {
316 XChangeProperty(e->dpy, win->xwin, atoms[ATOM__NET_WM_STATE],
317 XA_ATOM, 32, PropModeReplace,
318 (unsigned char *)&atoms[ATOM__NET_WM_STATE_FULLSCREEN], 1);
319 }
320
321 win->h -= win->bar.h;
322
323 win->buf.w = e->scrw;
324 win->buf.h = e->scrh;
325 win->buf.pm = XCreatePixmap(e->dpy, win->xwin, win->buf.w, win->buf.h, e->depth);
326
327 XSetForeground(e->dpy, gc, win->win_bg.pixel);
328 XFillRectangle(e->dpy, win->buf.pm, gc, 0, 0, win->buf.w, win->buf.h);
329 XSetWindowBackgroundPixmap(e->dpy, win->xwin, win->buf.pm);
330 XMapWindow(e->dpy, win->xwin);
331 XFlush(e->dpy);
332}
333
334CLEANUP void win_close(win_t *win)
335{
336 unsigned int i;
337
338 for (i = 0; i < ARRLEN(cursors); i++)
339 XFreeCursor(win->env.dpy, cursors[i].icon);
340
341 XFreeGC(win->env.dpy, gc);
342#if HAVE_LIBFONTS
343 XftFontClose(win->env.dpy, font);
344#endif
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 != (unsigned int)c->width || win->h + win->bar.h != (unsigned int)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 = &win->env;
397
398 if (win->w > win->buf.w || win->h + win->bar.h > win->buf.h) {
399 XFreePixmap(e->dpy, win->buf.pm);
400 win->buf.w = MAX(win->buf.w, win->w);
401 win->buf.h = MAX(win->buf.h, win->h + win->bar.h);
402 win->buf.pm = XCreatePixmap(e->dpy, win->xwin,
403 win->buf.w, win->buf.h, e->depth);
404 }
405 XSetForeground(e->dpy, gc, win->win_bg.pixel);
406 XFillRectangle(e->dpy, win->buf.pm, gc, 0, 0, win->buf.w, win->buf.h);
407}
408
409#if HAVE_LIBFONTS
410static int win_draw_text(win_t *win, XftDraw *d, const XftColor *color,
411 int x, int y, char *text, int len, int w)
412{
413 int err, tw = 0, warned = 0;
414 char *t, *next;
415 uint32_t rune;
416 XftFont *f;
417 FcCharSet *fccharset;
418 XGlyphInfo ext;
419
420 for (t = text; t - text < len; t = next) {
421 err = 0;
422 next = utf8_decode(t, &rune, &err);
423 if (err) {
424 if (!warned)
425 error(0, 0, "error decoding utf8 status-bar text");
426 warned = 1;
427 continue;
428 }
429 if (XftCharExists(win->env.dpy, font, rune)) {
430 f = font;
431 } else { /* fallback font */
432 fccharset = FcCharSetCreate();
433 FcCharSetAddChar(fccharset, rune);
434 f = XftFontOpen(win->env.dpy, win->env.scr, FC_CHARSET, FcTypeCharSet,
435 fccharset, FC_SCALABLE, FcTypeBool, FcTrue,
436 FC_SIZE, FcTypeDouble, fontsize, NULL);
437 FcCharSetDestroy(fccharset);
438 }
439 XftTextExtentsUtf8(win->env.dpy, f, (XftChar8 *)t, next - t, &ext);
440 tw += ext.xOff;
441 if (tw <= w) {
442 XftDrawStringUtf8(d, color, f, x, y, (XftChar8 *)t, next - t);
443 x += ext.xOff;
444 }
445 if (f != font)
446 XftFontClose(win->env.dpy, f);
447 }
448 return tw;
449}
450
451static void win_draw_bar(win_t *win)
452{
453 int len, x, y, w, tw;
454 win_env_t *e;
455 win_bar_t *l, *r;
456 XftDraw *d;
457
458 e = &win->env;
459 l = &win->bar.l;
460 r = &win->bar.r;
461 assert(l->buf != NULL && r->buf != NULL);
462 y = (win->bar.top ? 0 : win->h) + font->ascent + V_TEXT_PAD;
463 w = win->w - 2 * H_TEXT_PAD;
464 d = XftDrawCreate(e->dpy, win->buf.pm, e->vis, e->cmap);
465
466 XSetForeground(e->dpy, gc, win->bar_bg.pixel);
467 XFillRectangle(e->dpy, win->buf.pm, gc, 0, win->bar.top ? 0 : win->h, win->w, win->bar.h);
468
469 XSetForeground(e->dpy, gc, win->win_bg.pixel);
470 XSetBackground(e->dpy, gc, win->bar_bg.pixel);
471
472 if ((len = strlen(r->buf)) > 0) {
473 if ((tw = TEXTWIDTH(win, r->buf, len)) > w)
474 return;
475 x = win->w - tw - H_TEXT_PAD;
476 w -= tw;
477 win_draw_text(win, d, &win->bar_fg, x, y, r->buf, len, tw);
478 }
479 if ((len = strlen(l->buf)) > 0) {
480 x = H_TEXT_PAD;
481 w -= 2 * H_TEXT_PAD; /* gap between left and right parts */
482 win_draw_text(win, d, &win->bar_fg, x, y, l->buf, len, w);
483 }
484 XftDrawDestroy(d);
485}
486#else
487static void win_draw_bar(win_t *win)
488{
489 (void)win;
490}
491#endif /* HAVE_LIBFONTS */
492
493void win_draw(win_t *win)
494{
495 if (win->bar.h > 0)
496 win_draw_bar(win);
497
498 XSetWindowBackgroundPixmap(win->env.dpy, win->xwin, win->buf.pm);
499 XClearWindow(win->env.dpy, win->xwin);
500 XFlush(win->env.dpy);
501}
502
503void win_draw_rect(win_t *win, int x, int y, int w, int h, bool fill, int lw,
504 unsigned long col)
505{
506 XGCValues gcval;
507
508 gcval.line_width = lw;
509 gcval.foreground = col;
510 XChangeGC(win->env.dpy, gc, GCForeground | GCLineWidth, &gcval);
511
512 if (fill)
513 XFillRectangle(win->env.dpy, win->buf.pm, gc, x, y, w, h);
514 else
515 XDrawRectangle(win->env.dpy, win->buf.pm, gc, x, y, w, h);
516}
517
518void win_set_title(win_t *win, const char *title, size_t len)
519{
520 int i, targets[] = { ATOM_WM_NAME, ATOM_WM_ICON_NAME, ATOM__NET_WM_NAME, ATOM__NET_WM_ICON_NAME };
521
522 for (i = 0; i < (int)ARRLEN(targets); ++i) {
523 XChangeProperty(win->env.dpy, win->xwin, atoms[targets[i]],
524 atoms[ATOM_UTF8_STRING], 8, PropModeReplace,
525 (unsigned char *)title, len);
526 }
527}
528
529void win_set_cursor(win_t *win, cursor_t cursor)
530{
531 if (cursor >= 0 && cursor < ARRLEN(cursors)) {
532 XDefineCursor(win->env.dpy, win->xwin, cursors[cursor].icon);
533 XFlush(win->env.dpy);
534 }
535}
536
537void win_cursor_pos(win_t *win, int *x, int *y)
538{
539 int i;
540 unsigned int ui;
541 Window w;
542
543 if (!XQueryPointer(win->env.dpy, win->xwin, &w, &w, &i, &i, x, y, &ui))
544 *x = *y = 0;
545}