Krolyxon
Commit bdd9521

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(unsigned 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 win->bar.top = TOP_STATUSBAR;
163#endif /* HAVE_LIBFONTS */
164
165 XrmDestroyDatabase(db);
166 INIT_ATOM_(WM_DELETE_WINDOW);
167 INIT_ATOM_(_NET_WM_NAME);
168 INIT_ATOM_(_NET_WM_ICON_NAME);
169 INIT_ATOM_(_NET_WM_ICON);
170 INIT_ATOM_(_NET_WM_STATE);
171 INIT_ATOM_(_NET_WM_PID);
172 INIT_ATOM_(_NET_WM_STATE_FULLSCREEN);
173 INIT_ATOM_(UTF8_STRING);
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 pid_t 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 sizeof(pid_t) * 8, 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 < 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 < 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 < 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 /* These two atoms won't change and thus only need to be set once. */
293 XStoreName(win->env.dpy, win->xwin, res_name);
294 XSetIconName(win->env.dpy, win->xwin, res_name);
295
296 classhint.res_class = res_class;
297 classhint.res_name = options->res_name != NULL ? options->res_name : res_name;
298 XSetClassHint(e->dpy, win->xwin, &classhint);
299
300 XSetWMProtocols(e->dpy, win->xwin, &atoms[ATOM_WM_DELETE_WINDOW], 1);
301
302 sizehints.width = win->w;
303 sizehints.height = win->h;
304 sizehints.x = win->x;
305 sizehints.y = win->y;
306 XSetWMNormalHints(win->env.dpy, win->xwin, &sizehints);
307
308 hints.flags = InputHint | StateHint;
309 hints.input = 1;
310 hints.initial_state = NormalState;
311 XSetWMHints(win->env.dpy, win->xwin, &hints);
312
313 win->h -= win->bar.h;
314
315 win->buf.w = e->scrw;
316 win->buf.h = e->scrh;
317 win->buf.pm = XCreatePixmap(e->dpy, win->xwin, win->buf.w, win->buf.h, e->depth);
318
319 XSetForeground(e->dpy, gc, win->win_bg.pixel);
320 XFillRectangle(e->dpy, win->buf.pm, gc, 0, 0, win->buf.w, win->buf.h);
321 XSetWindowBackgroundPixmap(e->dpy, win->xwin, win->buf.pm);
322 XMapWindow(e->dpy, win->xwin);
323 XFlush(e->dpy);
324
325 if (options->fullscreen)
326 win_toggle_fullscreen(win);
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
338 XDestroyWindow(win->env.dpy, win->xwin);
339 XCloseDisplay(win->env.dpy);
340}
341
342bool win_configure(win_t *win, XConfigureEvent *c)
343{
344 bool changed;
345
346 changed = win->w != c->width || win->h + win->bar.h != c->height;
347
348 win->x = c->x;
349 win->y = c->y;
350 win->w = c->width;
351 win->h = c->height - win->bar.h;
352 win->bw = c->border_width;
353
354 return changed;
355}
356
357void win_toggle_fullscreen(win_t *win)
358{
359 XEvent ev;
360 XClientMessageEvent *cm;
361
362 memset(&ev, 0, sizeof(ev));
363 ev.type = ClientMessage;
364
365 cm = &ev.xclient;
366 cm->window = win->xwin;
367 cm->message_type = atoms[ATOM__NET_WM_STATE];
368 cm->format = 32;
369 cm->data.l[0] = 2; /* toggle */
370 cm->data.l[1] = atoms[ATOM__NET_WM_STATE_FULLSCREEN];
371
372 XSendEvent(win->env.dpy, DefaultRootWindow(win->env.dpy), False,
373 SubstructureNotifyMask | SubstructureRedirectMask, &ev);
374}
375
376void win_toggle_bar(win_t *win)
377{
378 if (win->bar.h != 0) {
379 win->h += win->bar.h;
380 win->bar.h = 0;
381 } else {
382 win->bar.h = barheight;
383 win->h -= win->bar.h;
384 }
385}
386
387void win_clear(win_t *win)
388{
389 win_env_t *e;
390
391 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;
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 next = utf8_decode(t, &rune, &err);
417 if (XftCharExists(win->env.dpy, font, rune)) {
418 f = font;
419 } else { /* fallback font */
420 fccharset = FcCharSetCreate();
421 FcCharSetAddChar(fccharset, rune);
422 f = XftFontOpen(win->env.dpy, win->env.scr, FC_CHARSET, FcTypeCharSet,
423 fccharset, FC_SCALABLE, FcTypeBool, FcTrue,
424 FC_SIZE, FcTypeDouble, fontsize, NULL);
425 FcCharSetDestroy(fccharset);
426 }
427 XftTextExtentsUtf8(win->env.dpy, f, (XftChar8*)t, next - t, &ext);
428 tw += ext.xOff;
429 if (tw <= w) {
430 XftDrawStringUtf8(d, color, f, x, y, (XftChar8*)t, next - t);
431 x += ext.xOff;
432 }
433 if (f != font)
434 XftFontClose(win->env.dpy, f);
435 }
436 return tw;
437}
438
439static void win_draw_bar(win_t *win)
440{
441 int len, x, y, w, tw;
442 win_env_t *e;
443 win_bar_t *l, *r;
444 XftDraw *d;
445
446 if ((l = &win->bar.l)->buf == NULL || (r = &win->bar.r)->buf == NULL)
447 return;
448
449 e = &win->env;
450 y = (win->bar.top ? 0 : win->h) + font->ascent + V_TEXT_PAD;
451 w = win->w - 2*H_TEXT_PAD;
452 d = XftDrawCreate(e->dpy, win->buf.pm, e->vis, e->cmap);
453
454 XSetForeground(e->dpy, gc, win->bar_bg.pixel);
455 XFillRectangle(e->dpy, win->buf.pm, gc, 0, win->bar.top ? 0 : win->h, win->w, win->bar.h);
456
457 XSetForeground(e->dpy, gc, win->win_bg.pixel);
458 XSetBackground(e->dpy, gc, win->bar_bg.pixel);
459
460 if ((len = strlen(r->buf)) > 0) {
461 if ((tw = TEXTWIDTH(win, r->buf, len)) > w)
462 return;
463 x = win->w - tw - H_TEXT_PAD;
464 w -= tw;
465 win_draw_text(win, d, &win->bar_fg, x, y, r->buf, len, tw);
466 }
467 if ((len = strlen(l->buf)) > 0) {
468 x = H_TEXT_PAD;
469 w -= 2 * H_TEXT_PAD; /* gap between left and right parts */
470 win_draw_text(win, d, &win->bar_fg, x, y, l->buf, len, w);
471 }
472 XftDrawDestroy(d);
473}
474#else
475static void win_draw_bar(win_t *win)
476{
477 (void) win;
478}
479#endif /* HAVE_LIBFONTS */
480
481void win_draw(win_t *win)
482{
483 if (win->bar.h > 0)
484 win_draw_bar(win);
485
486 XSetWindowBackgroundPixmap(win->env.dpy, win->xwin, win->buf.pm);
487 XClearWindow(win->env.dpy, win->xwin);
488 XFlush(win->env.dpy);
489}
490
491void win_draw_rect(win_t *win, int x, int y, int w, int h, bool fill, int lw,
492 unsigned long col)
493{
494 XGCValues gcval;
495
496 gcval.line_width = lw;
497 gcval.foreground = col;
498 XChangeGC(win->env.dpy, gc, GCForeground | GCLineWidth, &gcval);
499
500 if (fill)
501 XFillRectangle(win->env.dpy, win->buf.pm, gc, x, y, w, h);
502 else
503 XDrawRectangle(win->env.dpy, win->buf.pm, gc, x, y, w, h);
504}
505
506void win_set_title(win_t *win)
507{
508 unsigned char title[512];
509 size_t len;
510
511 if ((len = get_win_title(title, ARRLEN(title))) <= 0)
512 return;
513
514 XChangeProperty(win->env.dpy, win->xwin, atoms[ATOM__NET_WM_NAME],
515 atoms[ATOM_UTF8_STRING], 8, PropModeReplace, title, len);
516 XChangeProperty(win->env.dpy, win->xwin, atoms[ATOM__NET_WM_ICON_NAME],
517 atoms[ATOM_UTF8_STRING], 8, PropModeReplace, 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}