Krolyxon
Commit e26c81f

Branches

Refs →

nsxivwindow.c

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