Krolyxon
Commit e9468d3

Branches

Refs →

nsxivwindow.c

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