Krolyxon
Commit fb9686c

Branches

Refs →

nsxivwindow.c

542 lines · 14.4 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 INCLUDE_WINDOW_CONFIG
22#include "config.h"
23#include "icon/data.h"
24
25#include <locale.h>
26#include <stdlib.h>
27#include <string.h>
28#include <unistd.h>
29
30#include <X11/Xatom.h>
31#include <X11/Xresource.h>
32#include <X11/cursorfont.h>
33
34#if HAVE_LIBFONTS
35#include "utf8.h"
36static XftFont *font;
37static double fontsize;
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
44#define INIT_ATOM_(atom) \
45 atoms[ATOM_##atom] = XInternAtom(e->dpy, #atom, False);
46
47enum {
48 H_TEXT_PAD = 5,
49 V_TEXT_PAD = 1
50};
51
52static struct {
53 int name;
54 Cursor icon;
55} cursors[CURSOR_COUNT] = {
56 { XC_left_ptr }, { XC_dotbox }, { XC_fleur }, { XC_watch },
57 { XC_sb_left_arrow }, { XC_sb_right_arrow }
58};
59
60static GC gc;
61
62static int barheight;
63
64Atom atoms[ATOM_COUNT];
65
66#if HAVE_LIBFONTS
67static void win_init_font(const win_env_t *e, const char *fontstr)
68{
69 int fontheight = 0;
70 if ((font = XftFontOpenName(e->dpy, e->scr, fontstr)) == NULL)
71 error(EXIT_FAILURE, 0, "Error loading font '%s'", fontstr);
72 fontheight = font->ascent + font->descent;
73 FcPatternGetDouble(font->pattern, FC_SIZE, 0, &fontsize);
74 barheight = fontheight + 2 * V_TEXT_PAD;
75}
76
77static void xft_alloc_color(const win_env_t *e, const char *name, XftColor *col)
78{
79 if (!XftColorAllocName(e->dpy, e->vis, e->cmap, name, col))
80 error(EXIT_FAILURE, 0, "Error allocating color '%s'", name);
81}
82#endif /* HAVE_LIBFONTS */
83
84static void win_alloc_color(const win_env_t *e, const char *name, XColor *col)
85{
86 XColor screen;
87 if (!XAllocNamedColor(e->dpy, e->cmap, name, &screen, col))
88 error(EXIT_FAILURE, 0, "Error allocating color '%s'", name);
89}
90
91static const char* win_res(XrmDatabase db, const char *name, const char *def)
92{
93 char *type;
94 XrmValue ret;
95
96 if (db != NULL &&
97 XrmGetResource(db, name, name, &type, &ret) &&
98 STREQ(type, "String") && *ret.addr != '\0')
99 {
100 return ret.addr;
101 } else {
102 return def;
103 }
104}
105
106void win_init(win_t *win)
107{
108 win_env_t *e;
109 const char *win_bg, *win_fg, *mrk_fg;
110 char *res_man;
111 XrmDatabase db;
112#if HAVE_LIBFONTS
113 const char *bar_fg, *bar_bg, *f;
114
115 static char lbuf[512 + 3], rbuf[64 + 3];
116#endif
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.buf = lbuf;
155 win->bar.r.buf = rbuf;
156 /* 3 padding bytes needed by utf8_decode */
157 win->bar.l.size = sizeof(lbuf) - 3;
158 win->bar.r.size = sizeof(rbuf) - 3;
159 win->bar.h = options->hide_bar ? 0 : barheight;
160 win->bar.top = TOP_STATUSBAR;
161#endif /* HAVE_LIBFONTS */
162
163 XrmDestroyDatabase(db);
164 INIT_ATOM_(WM_DELETE_WINDOW);
165 INIT_ATOM_(_NET_WM_NAME);
166 INIT_ATOM_(_NET_WM_ICON_NAME);
167 INIT_ATOM_(_NET_WM_ICON);
168 INIT_ATOM_(_NET_WM_STATE);
169 INIT_ATOM_(_NET_WM_PID);
170 INIT_ATOM_(_NET_WM_STATE_FULLSCREEN);
171 INIT_ATOM_(UTF8_STRING);
172 INIT_ATOM_(WM_NAME);
173 INIT_ATOM_(WM_ICON_NAME);
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 long 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 32, 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 < (int)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 < (int)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 < (int)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 win_set_title(win, res_name, strlen(res_name));
293 classhint.res_class = res_class;
294 classhint.res_name = options->res_name != NULL ? options->res_name : res_name;
295 XSetClassHint(e->dpy, win->xwin, &classhint);
296
297 XSetWMProtocols(e->dpy, win->xwin, &atoms[ATOM_WM_DELETE_WINDOW], 1);
298
299 sizehints.width = win->w;
300 sizehints.height = win->h;
301 sizehints.x = win->x;
302 sizehints.y = win->y;
303 XSetWMNormalHints(win->env.dpy, win->xwin, &sizehints);
304
305 hints.flags = InputHint | StateHint;
306 hints.input = 1;
307 hints.initial_state = NormalState;
308 XSetWMHints(win->env.dpy, win->xwin, &hints);
309
310 if (options->fullscreen) {
311 XChangeProperty(e->dpy, win->xwin, atoms[ATOM__NET_WM_STATE],
312 XA_ATOM, 32, PropModeReplace,
313 (unsigned char *) &atoms[ATOM__NET_WM_STATE_FULLSCREEN], 1);
314 }
315
316 win->h -= win->bar.h;
317
318 win->buf.w = e->scrw;
319 win->buf.h = e->scrh;
320 win->buf.pm = XCreatePixmap(e->dpy, win->xwin, win->buf.w, win->buf.h, e->depth);
321
322 XSetForeground(e->dpy, gc, win->win_bg.pixel);
323 XFillRectangle(e->dpy, win->buf.pm, gc, 0, 0, win->buf.w, win->buf.h);
324 XSetWindowBackgroundPixmap(e->dpy, win->xwin, win->buf.pm);
325 XMapWindow(e->dpy, win->xwin);
326 XFlush(e->dpy);
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#if HAVE_LIBFONTS
338 XftFontClose(win->env.dpy, font);
339#endif
340 XDestroyWindow(win->env.dpy, win->xwin);
341 XCloseDisplay(win->env.dpy);
342}
343
344bool win_configure(win_t *win, XConfigureEvent *c)
345{
346 bool changed;
347
348 changed = win->w != (unsigned int)c->width || win->h + win->bar.h != (unsigned int)c->height;
349
350 win->x = c->x;
351 win->y = c->y;
352 win->w = c->width;
353 win->h = c->height - win->bar.h;
354 win->bw = c->border_width;
355
356 return changed;
357}
358
359void win_toggle_fullscreen(win_t *win)
360{
361 XEvent ev;
362 XClientMessageEvent *cm;
363
364 memset(&ev, 0, sizeof(ev));
365 ev.type = ClientMessage;
366
367 cm = &ev.xclient;
368 cm->window = win->xwin;
369 cm->message_type = atoms[ATOM__NET_WM_STATE];
370 cm->format = 32;
371 cm->data.l[0] = 2; /* toggle */
372 cm->data.l[1] = atoms[ATOM__NET_WM_STATE_FULLSCREEN];
373
374 XSendEvent(win->env.dpy, DefaultRootWindow(win->env.dpy), False,
375 SubstructureNotifyMask | SubstructureRedirectMask, &ev);
376}
377
378void win_toggle_bar(win_t *win)
379{
380 if (win->bar.h != 0) {
381 win->h += win->bar.h;
382 win->bar.h = 0;
383 } else {
384 win->bar.h = barheight;
385 win->h -= win->bar.h;
386 }
387}
388
389void win_clear(win_t *win)
390{
391 win_env_t *e;
392
393 e = &win->env;
394
395 if (win->w > win->buf.w || win->h + win->bar.h > win->buf.h) {
396 XFreePixmap(e->dpy, win->buf.pm);
397 win->buf.w = MAX(win->buf.w, win->w);
398 win->buf.h = MAX(win->buf.h, win->h + win->bar.h);
399 win->buf.pm = XCreatePixmap(e->dpy, win->xwin,
400 win->buf.w, win->buf.h, e->depth);
401 }
402 XSetForeground(e->dpy, gc, win->win_bg.pixel);
403 XFillRectangle(e->dpy, win->buf.pm, gc, 0, 0, win->buf.w, win->buf.h);
404}
405
406#if HAVE_LIBFONTS
407static int win_draw_text(win_t *win, XftDraw *d, const XftColor *color,
408 int x, int y, char *text, int len, int w)
409{
410 int err, tw = 0, warned = 0;
411 char *t, *next;
412 uint32_t rune;
413 XftFont *f;
414 FcCharSet *fccharset;
415 XGlyphInfo ext;
416
417 for (t = text; t - text < len; t = next) {
418 err = 0;
419 next = utf8_decode(t, &rune, &err);
420 if (err) {
421 if (!warned)
422 error(0, 0, "error decoding utf8 status-bar text");
423 warned = 1;
424 continue;
425 }
426 if (XftCharExists(win->env.dpy, font, rune)) {
427 f = font;
428 } else { /* fallback font */
429 fccharset = FcCharSetCreate();
430 FcCharSetAddChar(fccharset, rune);
431 f = XftFontOpen(win->env.dpy, win->env.scr, FC_CHARSET, FcTypeCharSet,
432 fccharset, FC_SCALABLE, FcTypeBool, FcTrue,
433 FC_SIZE, FcTypeDouble, fontsize, NULL);
434 FcCharSetDestroy(fccharset);
435 }
436 XftTextExtentsUtf8(win->env.dpy, f, (XftChar8*)t, next - t, &ext);
437 tw += ext.xOff;
438 if (tw <= w) {
439 XftDrawStringUtf8(d, color, f, x, y, (XftChar8*)t, next - t);
440 x += ext.xOff;
441 }
442 if (f != font)
443 XftFontClose(win->env.dpy, f);
444 }
445 return tw;
446}
447
448static void win_draw_bar(win_t *win)
449{
450 int len, x, y, w, tw;
451 win_env_t *e;
452 win_bar_t *l, *r;
453 XftDraw *d;
454
455 if ((l = &win->bar.l)->buf == NULL || (r = &win->bar.r)->buf == NULL)
456 return;
457
458 e = &win->env;
459 y = (win->bar.top ? 0 : win->h) + font->ascent + V_TEXT_PAD;
460 w = win->w - 2*H_TEXT_PAD;
461 d = XftDrawCreate(e->dpy, win->buf.pm, e->vis, e->cmap);
462
463 XSetForeground(e->dpy, gc, win->bar_bg.pixel);
464 XFillRectangle(e->dpy, win->buf.pm, gc, 0, win->bar.top ? 0 : win->h, win->w, win->bar.h);
465
466 XSetForeground(e->dpy, gc, win->win_bg.pixel);
467 XSetBackground(e->dpy, gc, win->bar_bg.pixel);
468
469 if ((len = strlen(r->buf)) > 0) {
470 if ((tw = TEXTWIDTH(win, r->buf, len)) > w)
471 return;
472 x = win->w - tw - H_TEXT_PAD;
473 w -= tw;
474 win_draw_text(win, d, &win->bar_fg, x, y, r->buf, len, tw);
475 }
476 if ((len = strlen(l->buf)) > 0) {
477 x = H_TEXT_PAD;
478 w -= 2 * H_TEXT_PAD; /* gap between left and right parts */
479 win_draw_text(win, d, &win->bar_fg, x, y, l->buf, len, w);
480 }
481 XftDrawDestroy(d);
482}
483#else
484static void win_draw_bar(win_t *win)
485{
486 (void) win;
487}
488#endif /* HAVE_LIBFONTS */
489
490void win_draw(win_t *win)
491{
492 if (win->bar.h > 0)
493 win_draw_bar(win);
494
495 XSetWindowBackgroundPixmap(win->env.dpy, win->xwin, win->buf.pm);
496 XClearWindow(win->env.dpy, win->xwin);
497 XFlush(win->env.dpy);
498}
499
500void win_draw_rect(win_t *win, int x, int y, int w, int h, bool fill, int lw,
501 unsigned long col)
502{
503 XGCValues gcval;
504
505 gcval.line_width = lw;
506 gcval.foreground = col;
507 XChangeGC(win->env.dpy, gc, GCForeground | GCLineWidth, &gcval);
508
509 if (fill)
510 XFillRectangle(win->env.dpy, win->buf.pm, gc, x, y, w, h);
511 else
512 XDrawRectangle(win->env.dpy, win->buf.pm, gc, x, y, w, h);
513}
514
515void win_set_title(win_t *win, const char *title, size_t len)
516{
517 int i, targets[] = { ATOM_WM_NAME, ATOM_WM_ICON_NAME, ATOM__NET_WM_NAME, ATOM__NET_WM_ICON_NAME };
518
519 for (i = 0; i < (int)ARRLEN(targets); ++i) {
520 XChangeProperty(win->env.dpy, win->xwin, atoms[targets[i]],
521 atoms[ATOM_UTF8_STRING], 8, PropModeReplace,
522 (unsigned char *)title, len);
523 }
524}
525
526void win_set_cursor(win_t *win, cursor_t cursor)
527{
528 if (cursor >= 0 && cursor < ARRLEN(cursors)) {
529 XDefineCursor(win->env.dpy, win->xwin, cursors[cursor].icon);
530 XFlush(win->env.dpy);
531 }
532}
533
534void win_cursor_pos(win_t *win, int *x, int *y)
535{
536 int i;
537 unsigned int ui;
538 Window w;
539
540 if (!XQueryPointer(win->env.dpy, win->xwin, &w, &w, &i, &i, x, y, &ui))
541 *x = *y = 0;
542}