Krolyxon
Commit 591be8c

Branches

Refs →

nsxivwindow.c

539 lines · 14.3 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 _WINDOW_CONFIG
22#include "config.h"
23#include "icon/data.h"
24
25#include <stdlib.h>
26#include <string.h>
27#include <locale.h>
28#include <unistd.h>
29#include <X11/cursorfont.h>
30#include <X11/Xatom.h>
31#include <X11/Xresource.h>
32
33extern size_t get_win_title(unsigned char *, int, bool);
34
35#if HAVE_LIBFONTS
36#include "utf8.h"
37static XftFont *font;
38static double fontsize;
39#define TEXTWIDTH(win, text, len) \
40 win_draw_text(win, NULL, NULL, 0, 0, text, len, 0)
41#endif
42
43#define RES_CLASS "Nsxiv"
44
45#define INIT_ATOM_(atom) \
46 atoms[ATOM_##atom] = XInternAtom(e->dpy, #atom, False);
47
48enum {
49 H_TEXT_PAD = 5,
50 V_TEXT_PAD = 1
51};
52
53static struct {
54 int name;
55 Cursor icon;
56} cursors[CURSOR_COUNT] = {
57 { XC_left_ptr }, { XC_dotbox }, { XC_fleur }, { XC_watch },
58 { XC_sb_left_arrow }, { XC_sb_right_arrow }
59};
60
61static GC gc;
62
63static int barheight;
64
65Atom atoms[ATOM_COUNT];
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"))
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#if HAVE_LIBFONTS
112 const char *bar_fg, *bar_bg, *f;
113#endif
114 char *res_man;
115 XrmDatabase db;
116
117 memset(win, 0, sizeof(win_t));
118
119 e = &win->env;
120 if ((e->dpy = XOpenDisplay(NULL)) == NULL)
121 error(EXIT_FAILURE, 0, "Error opening X display");
122
123 e->scr = DefaultScreen(e->dpy);
124 e->scrw = DisplayWidth(e->dpy, e->scr);
125 e->scrh = DisplayHeight(e->dpy, e->scr);
126 e->depth = DefaultDepth(e->dpy, e->scr);
127 e->vis = DefaultVisual(e->dpy, e->scr);
128 e->cmap = DefaultColormap(e->dpy, e->scr);
129
130 if (setlocale(LC_CTYPE, "") == NULL || XSupportsLocale() == 0)
131 error(0, 0, "No locale support");
132
133 XrmInitialize();
134 res_man = XResourceManagerString(e->dpy);
135 db = res_man == NULL ? NULL : XrmGetStringDatabase(res_man);
136
137 win_bg = win_res(db, RES_CLASS ".window.background", DEFAULT_WIN_BG);
138 win_fg = win_res(db, RES_CLASS ".window.foreground", DEFAULT_WIN_FG);
139 mrk_fg = win_res(db, RES_CLASS ".mark.foreground", DEFAULT_MARK_COLOR ? DEFAULT_MARK_COLOR : win_fg);
140 win_alloc_color(e, win_bg, &win->win_bg);
141 win_alloc_color(e, win_fg, &win->win_fg);
142 win_alloc_color(e, mrk_fg, &win->mrk_fg);
143
144#if HAVE_LIBFONTS
145 bar_bg = win_res(db, RES_CLASS ".bar.background", DEFAULT_BAR_BG ? DEFAULT_BAR_BG : win_bg);
146 bar_fg = win_res(db, RES_CLASS ".bar.foreground", DEFAULT_BAR_FG ? DEFAULT_BAR_FG : win_fg);
147 xft_alloc_color(e, bar_bg, &win->bar_bg);
148 xft_alloc_color(e, bar_fg, &win->bar_fg);
149
150 f = win_res(db, RES_CLASS ".bar.font", DEFAULT_FONT);
151 win_init_font(e, f);
152
153 win->bar.l.size = BAR_L_LEN;
154 win->bar.r.size = BAR_R_LEN;
155 /* 3 padding bytes needed by utf8_decode */
156 win->bar.l.buf = emalloc(win->bar.l.size + 3);
157 win->bar.l.buf[0] = '\0';
158 win->bar.r.buf = emalloc(win->bar.r.size + 3);
159 win->bar.r.buf[0] = '\0';
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 < 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 < 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 < 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, true);
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 != c->width || win->h + win->bar.h != 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;
393
394 e = &win->env;
395
396 if (win->w > win->buf.w || win->h + win->bar.h > win->buf.h) {
397 XFreePixmap(e->dpy, win->buf.pm);
398 win->buf.w = MAX(win->buf.w, win->w);
399 win->buf.h = MAX(win->buf.h, win->h + win->bar.h);
400 win->buf.pm = XCreatePixmap(e->dpy, win->xwin,
401 win->buf.w, win->buf.h, e->depth);
402 }
403 XSetForeground(e->dpy, gc, win->win_bg.pixel);
404 XFillRectangle(e->dpy, win->buf.pm, gc, 0, 0, win->buf.w, win->buf.h);
405}
406
407#if HAVE_LIBFONTS
408static int win_draw_text(win_t *win, XftDraw *d, const XftColor *color,
409 int x, int y, char *text, int len, int w)
410{
411 int err, tw = 0;
412 char *t, *next;
413 uint32_t rune;
414 XftFont *f;
415 FcCharSet *fccharset;
416 XGlyphInfo ext;
417
418 for (t = text; t - text < len; t = next) {
419 next = utf8_decode(t, &rune, &err);
420 if (XftCharExists(win->env.dpy, font, rune)) {
421 f = font;
422 } else { /* fallback font */
423 fccharset = FcCharSetCreate();
424 FcCharSetAddChar(fccharset, rune);
425 f = XftFontOpen(win->env.dpy, win->env.scr, FC_CHARSET, FcTypeCharSet,
426 fccharset, FC_SCALABLE, FcTypeBool, FcTrue,
427 FC_SIZE, FcTypeDouble, fontsize, NULL);
428 FcCharSetDestroy(fccharset);
429 }
430 XftTextExtentsUtf8(win->env.dpy, f, (XftChar8*)t, next - t, &ext);
431 tw += ext.xOff;
432 if (tw <= w) {
433 XftDrawStringUtf8(d, color, f, x, y, (XftChar8*)t, next - t);
434 x += ext.xOff;
435 }
436 if (f != font)
437 XftFontClose(win->env.dpy, f);
438 }
439 return tw;
440}
441
442static void win_draw_bar(win_t *win)
443{
444 int len, x, y, w, tw;
445 win_env_t *e;
446 win_bar_t *l, *r;
447 XftDraw *d;
448
449 if ((l = &win->bar.l)->buf == NULL || (r = &win->bar.r)->buf == NULL)
450 return;
451
452 e = &win->env;
453 y = (win->bar.top ? 0 : win->h) + font->ascent + V_TEXT_PAD;
454 w = win->w - 2*H_TEXT_PAD;
455 d = XftDrawCreate(e->dpy, win->buf.pm, e->vis, e->cmap);
456
457 XSetForeground(e->dpy, gc, win->bar_bg.pixel);
458 XFillRectangle(e->dpy, win->buf.pm, gc, 0, win->bar.top ? 0 : win->h, win->w, win->bar.h);
459
460 XSetForeground(e->dpy, gc, win->win_bg.pixel);
461 XSetBackground(e->dpy, gc, win->bar_bg.pixel);
462
463 if ((len = strlen(r->buf)) > 0) {
464 if ((tw = TEXTWIDTH(win, r->buf, len)) > w)
465 return;
466 x = win->w - tw - H_TEXT_PAD;
467 w -= tw;
468 win_draw_text(win, d, &win->bar_fg, x, y, r->buf, len, tw);
469 }
470 if ((len = strlen(l->buf)) > 0) {
471 x = H_TEXT_PAD;
472 w -= 2 * H_TEXT_PAD; /* gap between left and right parts */
473 win_draw_text(win, d, &win->bar_fg, x, y, l->buf, len, w);
474 }
475 XftDrawDestroy(d);
476}
477#else
478static void win_draw_bar(win_t *win)
479{
480 (void) win;
481}
482#endif /* HAVE_LIBFONTS */
483
484void win_draw(win_t *win)
485{
486 if (win->bar.h > 0)
487 win_draw_bar(win);
488
489 XSetWindowBackgroundPixmap(win->env.dpy, win->xwin, win->buf.pm);
490 XClearWindow(win->env.dpy, win->xwin);
491 XFlush(win->env.dpy);
492}
493
494void win_draw_rect(win_t *win, int x, int y, int w, int h, bool fill, int lw,
495 unsigned long col)
496{
497 XGCValues gcval;
498
499 gcval.line_width = lw;
500 gcval.foreground = col;
501 XChangeGC(win->env.dpy, gc, GCForeground | GCLineWidth, &gcval);
502
503 if (fill)
504 XFillRectangle(win->env.dpy, win->buf.pm, gc, x, y, w, h);
505 else
506 XDrawRectangle(win->env.dpy, win->buf.pm, gc, x, y, w, h);
507}
508
509void win_set_title(win_t *win, bool init)
510{
511 size_t len, i;
512 unsigned char title[512];
513 int targets[] = { ATOM_WM_NAME, ATOM_WM_ICON_NAME, ATOM__NET_WM_NAME, ATOM__NET_WM_ICON_NAME };
514
515 if ((len = get_win_title(title, ARRLEN(title), init)) > 0) {
516 for (i = 0; i < ARRLEN(targets); ++i) {
517 XChangeProperty(win->env.dpy, win->xwin, atoms[targets[i]],
518 atoms[ATOM_UTF8_STRING], 8, PropModeReplace, title, len);
519 }
520 }
521}
522
523void win_set_cursor(win_t *win, cursor_t cursor)
524{
525 if (cursor >= 0 && cursor < ARRLEN(cursors)) {
526 XDefineCursor(win->env.dpy, win->xwin, cursors[cursor].icon);
527 XFlush(win->env.dpy);
528 }
529}
530
531void win_cursor_pos(win_t *win, int *x, int *y)
532{
533 int i;
534 unsigned int ui;
535 Window w;
536
537 if (!XQueryPointer(win->env.dpy, win->xwin, &w, &w, &i, &i, x, y, &ui))
538 *x = *y = 0;
539}