Krolyxon
Commit c713186

Branches

Refs →

nsxivwindow.c

537 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
33#if HAVE_LIBFONTS
34#include "utf8.h"
35static XftFont *font;
36static double fontsize;
37#define TEXTWIDTH(win, text, len) \
38 win_draw_text(win, NULL, NULL, 0, 0, text, len, 0)
39#endif
40
41#define RES_CLASS "Nsxiv"
42
43#define INIT_ATOM_(atom) \
44 atoms[ATOM_##atom] = XInternAtom(e->dpy, #atom, False);
45
46enum {
47 H_TEXT_PAD = 5,
48 V_TEXT_PAD = 1
49};
50
51static struct {
52 int name;
53 Cursor icon;
54} cursors[CURSOR_COUNT] = {
55 { XC_left_ptr }, { XC_dotbox }, { XC_fleur }, { XC_watch },
56 { XC_sb_left_arrow }, { XC_sb_right_arrow }
57};
58
59static GC gc;
60
61static int barheight;
62
63Atom atoms[ATOM_COUNT];
64
65#if HAVE_LIBFONTS
66static void win_init_font(const win_env_t *e, const char *fontstr)
67{
68 int fontheight = 0;
69 if ((font = XftFontOpenName(e->dpy, e->scr, fontstr)) == NULL)
70 error(EXIT_FAILURE, 0, "Error loading font '%s'", fontstr);
71 fontheight = font->ascent + font->descent;
72 FcPatternGetDouble(font->pattern, FC_SIZE, 0, &fontsize);
73 barheight = fontheight + 2 * V_TEXT_PAD;
74}
75
76static void xft_alloc_color(const win_env_t *e, const char *name, XftColor *col)
77{
78 if (!XftColorAllocName(e->dpy, e->vis, e->cmap, name, col))
79 error(EXIT_FAILURE, 0, "Error allocating color '%s'", name);
80}
81#endif /* HAVE_LIBFONTS */
82
83static void win_alloc_color(const win_env_t *e, const char *name, XColor *col)
84{
85 XColor screen;
86 if (!XAllocNamedColor(e->dpy, e->cmap, name, &screen, col))
87 error(EXIT_FAILURE, 0, "Error allocating color '%s'", name);
88}
89
90static const char* win_res(XrmDatabase db, const char *name, const char *def)
91{
92 char *type;
93 XrmValue ret;
94
95 if (db != NULL &&
96 XrmGetResource(db, name, name, &type, &ret) &&
97 STREQ(type, "String"))
98 {
99 return ret.addr;
100 } else {
101 return def;
102 }
103}
104
105void win_init(win_t *win)
106{
107 win_env_t *e;
108 const char *win_bg, *win_fg, *mrk_fg;
109#if HAVE_LIBFONTS
110 const char *bar_fg, *bar_bg, *f;
111#endif
112 char *res_man;
113 XrmDatabase db;
114
115 memset(win, 0, sizeof(win_t));
116
117 e = &win->env;
118 if ((e->dpy = XOpenDisplay(NULL)) == NULL)
119 error(EXIT_FAILURE, 0, "Error opening X display");
120
121 e->scr = DefaultScreen(e->dpy);
122 e->scrw = DisplayWidth(e->dpy, e->scr);
123 e->scrh = DisplayHeight(e->dpy, e->scr);
124 e->depth = DefaultDepth(e->dpy, e->scr);
125 e->vis = DefaultVisual(e->dpy, e->scr);
126 e->cmap = DefaultColormap(e->dpy, e->scr);
127
128 if (setlocale(LC_CTYPE, "") == NULL || XSupportsLocale() == 0)
129 error(0, 0, "No locale support");
130
131 XrmInitialize();
132 res_man = XResourceManagerString(e->dpy);
133 db = res_man == NULL ? NULL : XrmGetStringDatabase(res_man);
134
135 win_bg = win_res(db, RES_CLASS ".window.background", DEFAULT_WIN_BG);
136 win_fg = win_res(db, RES_CLASS ".window.foreground", DEFAULT_WIN_FG);
137 mrk_fg = win_res(db, RES_CLASS ".mark.foreground", DEFAULT_MARK_COLOR ? DEFAULT_MARK_COLOR : win_fg);
138 win_alloc_color(e, win_bg, &win->win_bg);
139 win_alloc_color(e, win_fg, &win->win_fg);
140 win_alloc_color(e, mrk_fg, &win->mrk_fg);
141
142#if HAVE_LIBFONTS
143 bar_bg = win_res(db, RES_CLASS ".bar.background", DEFAULT_BAR_BG ? DEFAULT_BAR_BG : win_bg);
144 bar_fg = win_res(db, RES_CLASS ".bar.foreground", DEFAULT_BAR_FG ? DEFAULT_BAR_FG : win_fg);
145 xft_alloc_color(e, bar_bg, &win->bar_bg);
146 xft_alloc_color(e, bar_fg, &win->bar_fg);
147
148 f = win_res(db, RES_CLASS ".bar.font", DEFAULT_FONT);
149 win_init_font(e, f);
150
151 win->bar.l.size = BAR_L_LEN;
152 win->bar.r.size = BAR_R_LEN;
153 /* 3 padding bytes needed by utf8_decode */
154 win->bar.l.buf = emalloc(win->bar.l.size + 3);
155 win->bar.l.buf[0] = '\0';
156 win->bar.r.buf = emalloc(win->bar.r.size + 3);
157 win->bar.r.buf[0] = '\0';
158 win->bar.h = options->hide_bar ? 0 : barheight;
159 win->bar.top = TOP_STATUSBAR;
160#endif /* HAVE_LIBFONTS */
161
162 XrmDestroyDatabase(db);
163 INIT_ATOM_(WM_DELETE_WINDOW);
164 INIT_ATOM_(_NET_WM_NAME);
165 INIT_ATOM_(_NET_WM_ICON_NAME);
166 INIT_ATOM_(_NET_WM_ICON);
167 INIT_ATOM_(_NET_WM_STATE);
168 INIT_ATOM_(_NET_WM_PID);
169 INIT_ATOM_(_NET_WM_STATE_FULLSCREEN);
170 INIT_ATOM_(UTF8_STRING);
171 INIT_ATOM_(WM_NAME);
172 INIT_ATOM_(WM_ICON_NAME);
173}
174
175void win_open(win_t *win)
176{
177 int c, i, j, n;
178 Window parent;
179 win_env_t *e;
180 XClassHint classhint;
181 unsigned long *icon_data;
182 XColor col;
183 Cursor *cnone = &cursors[CURSOR_NONE].icon;
184 char none_data[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
185 Pixmap none;
186 int gmask;
187 XSizeHints sizehints;
188 XWMHints hints;
189 long pid;
190 char hostname[256];
191 XSetWindowAttributes attrs;
192 char res_class[] = RES_CLASS;
193 char res_name[] = "nsxiv";
194
195 e = &win->env;
196 parent = options->embed ? options->embed : RootWindow(e->dpy, e->scr);
197
198 sizehints.flags = PWinGravity;
199 sizehints.win_gravity = NorthWestGravity;
200
201 /* determine window offsets, width & height */
202 if (options->geometry == NULL)
203 gmask = 0;
204 else
205 gmask = XParseGeometry(options->geometry, &win->x, &win->y,
206 &win->w, &win->h);
207 if (gmask & WidthValue)
208 sizehints.flags |= USSize;
209 else
210 win->w = WIN_WIDTH;
211 if (gmask & HeightValue)
212 sizehints.flags |= USSize;
213 else
214 win->h = WIN_HEIGHT;
215 if (gmask & XValue) {
216 if (gmask & XNegative) {
217 win->x += e->scrw - win->w;
218 sizehints.win_gravity = NorthEastGravity;
219 }
220 sizehints.flags |= USPosition;
221 } else {
222 win->x = 0;
223 }
224 if (gmask & YValue) {
225 if (gmask & YNegative) {
226 win->y += e->scrh - win->h;
227 sizehints.win_gravity = sizehints.win_gravity == NorthEastGravity
228 ? SouthEastGravity : SouthWestGravity;
229 }
230 sizehints.flags |= USPosition;
231 } else {
232 win->y = 0;
233 }
234
235 attrs.colormap = e->cmap;
236 attrs.border_pixel = 0;
237
238 win->xwin = XCreateWindow(e->dpy, parent, win->x, win->y, win->w, win->h, 0,
239 e->depth, InputOutput, e->vis,
240 CWColormap | CWBorderPixel, &attrs);
241 if (win->xwin == None)
242 error(EXIT_FAILURE, 0, "Error creating X window");
243
244 /* set the _NET_WM_PID */
245 pid = getpid();
246 XChangeProperty(e->dpy, win->xwin, atoms[ATOM__NET_WM_PID], XA_CARDINAL,
247 32, PropModeReplace, (unsigned char *) &pid, 1);
248 if (gethostname(hostname, ARRLEN(hostname)) == 0) {
249 XTextProperty tp;
250 tp.value = (unsigned char *)hostname;
251 tp.nitems = strnlen(hostname, ARRLEN(hostname));
252 tp.encoding = XA_STRING;
253 tp.format = 8;
254 XSetWMClientMachine(e->dpy, win->xwin, &tp);
255 }
256
257 XSelectInput(e->dpy, win->xwin,
258 ButtonReleaseMask | ButtonPressMask | KeyPressMask |
259 PointerMotionMask | StructureNotifyMask);
260
261 for (i = 0; i < ARRLEN(cursors); i++) {
262 if (i != CURSOR_NONE)
263 cursors[i].icon = XCreateFontCursor(e->dpy, cursors[i].name);
264 }
265 if (XAllocNamedColor(e->dpy, e->cmap, "black", &col, &col) == 0)
266 error(EXIT_FAILURE, 0, "Error allocating color 'black'");
267
268 none = XCreateBitmapFromData(e->dpy, win->xwin, none_data, 8, 8);
269 *cnone = XCreatePixmapCursor(e->dpy, none, none, &col, &col, 0, 0);
270
271 gc = XCreateGC(e->dpy, win->xwin, 0, None);
272
273 n = icons[ARRLEN(icons)-1].size;
274 icon_data = emalloc((n * n + 2) * sizeof(*icon_data));
275
276 for (i = 0; i < ARRLEN(icons); i++) {
277 n = 0;
278 icon_data[n++] = icons[i].size;
279 icon_data[n++] = icons[i].size;
280
281 for (j = 0; j < icons[i].cnt; j++) {
282 for (c = icons[i].data[j] >> 4; c >= 0; c--)
283 icon_data[n++] = icon_colors[icons[i].data[j] & 0x0F];
284 }
285 XChangeProperty(e->dpy, win->xwin, atoms[ATOM__NET_WM_ICON], XA_CARDINAL, 32,
286 i == 0 ? PropModeReplace : PropModeAppend,
287 (unsigned char *) icon_data, n);
288 }
289 free(icon_data);
290
291 win_set_title(win, true);
292 classhint.res_class = res_class;
293 classhint.res_name = options->res_name != NULL ? options->res_name : res_name;
294 XSetClassHint(e->dpy, win->xwin, &classhint);
295
296 XSetWMProtocols(e->dpy, win->xwin, &atoms[ATOM_WM_DELETE_WINDOW], 1);
297
298 sizehints.width = win->w;
299 sizehints.height = win->h;
300 sizehints.x = win->x;
301 sizehints.y = win->y;
302 XSetWMNormalHints(win->env.dpy, win->xwin, &sizehints);
303
304 hints.flags = InputHint | StateHint;
305 hints.input = 1;
306 hints.initial_state = NormalState;
307 XSetWMHints(win->env.dpy, win->xwin, &hints);
308
309 if (options->fullscreen) {
310 XChangeProperty(e->dpy, win->xwin, atoms[ATOM__NET_WM_STATE],
311 XA_ATOM, 32, PropModeReplace,
312 (unsigned char *) &atoms[ATOM__NET_WM_STATE_FULLSCREEN], 1);
313 }
314
315 win->h -= win->bar.h;
316
317 win->buf.w = e->scrw;
318 win->buf.h = e->scrh;
319 win->buf.pm = XCreatePixmap(e->dpy, win->xwin, win->buf.w, win->buf.h, e->depth);
320
321 XSetForeground(e->dpy, gc, win->win_bg.pixel);
322 XFillRectangle(e->dpy, win->buf.pm, gc, 0, 0, win->buf.w, win->buf.h);
323 XSetWindowBackgroundPixmap(e->dpy, win->xwin, win->buf.pm);
324 XMapWindow(e->dpy, win->xwin);
325 XFlush(e->dpy);
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#if HAVE_LIBFONTS
337 XftFontClose(win->env.dpy, font);
338#endif
339 XDestroyWindow(win->env.dpy, win->xwin);
340 XCloseDisplay(win->env.dpy);
341}
342
343bool win_configure(win_t *win, XConfigureEvent *c)
344{
345 bool changed;
346
347 changed = win->w != c->width || win->h + win->bar.h != c->height;
348
349 win->x = c->x;
350 win->y = c->y;
351 win->w = c->width;
352 win->h = c->height - win->bar.h;
353 win->bw = c->border_width;
354
355 return changed;
356}
357
358void win_toggle_fullscreen(win_t *win)
359{
360 XEvent ev;
361 XClientMessageEvent *cm;
362
363 memset(&ev, 0, sizeof(ev));
364 ev.type = ClientMessage;
365
366 cm = &ev.xclient;
367 cm->window = win->xwin;
368 cm->message_type = atoms[ATOM__NET_WM_STATE];
369 cm->format = 32;
370 cm->data.l[0] = 2; /* toggle */
371 cm->data.l[1] = atoms[ATOM__NET_WM_STATE_FULLSCREEN];
372
373 XSendEvent(win->env.dpy, DefaultRootWindow(win->env.dpy), False,
374 SubstructureNotifyMask | SubstructureRedirectMask, &ev);
375}
376
377void win_toggle_bar(win_t *win)
378{
379 if (win->bar.h != 0) {
380 win->h += win->bar.h;
381 win->bar.h = 0;
382 } else {
383 win->bar.h = barheight;
384 win->h -= win->bar.h;
385 }
386}
387
388void win_clear(win_t *win)
389{
390 win_env_t *e;
391
392 e = &win->env;
393
394 if (win->w > win->buf.w || win->h + win->bar.h > win->buf.h) {
395 XFreePixmap(e->dpy, win->buf.pm);
396 win->buf.w = MAX(win->buf.w, win->w);
397 win->buf.h = MAX(win->buf.h, win->h + win->bar.h);
398 win->buf.pm = XCreatePixmap(e->dpy, win->xwin,
399 win->buf.w, win->buf.h, e->depth);
400 }
401 XSetForeground(e->dpy, gc, win->win_bg.pixel);
402 XFillRectangle(e->dpy, win->buf.pm, gc, 0, 0, win->buf.w, win->buf.h);
403}
404
405#if HAVE_LIBFONTS
406static int win_draw_text(win_t *win, XftDraw *d, const XftColor *color,
407 int x, int y, char *text, int len, int w)
408{
409 int err, tw = 0;
410 char *t, *next;
411 uint32_t rune;
412 XftFont *f;
413 FcCharSet *fccharset;
414 XGlyphInfo ext;
415
416 for (t = text; t - text < len; t = next) {
417 next = utf8_decode(t, &rune, &err);
418 if (XftCharExists(win->env.dpy, font, rune)) {
419 f = font;
420 } else { /* fallback font */
421 fccharset = FcCharSetCreate();
422 FcCharSetAddChar(fccharset, rune);
423 f = XftFontOpen(win->env.dpy, win->env.scr, FC_CHARSET, FcTypeCharSet,
424 fccharset, FC_SCALABLE, FcTypeBool, FcTrue,
425 FC_SIZE, FcTypeDouble, fontsize, NULL);
426 FcCharSetDestroy(fccharset);
427 }
428 XftTextExtentsUtf8(win->env.dpy, f, (XftChar8*)t, next - t, &ext);
429 tw += ext.xOff;
430 if (tw <= w) {
431 XftDrawStringUtf8(d, color, f, x, y, (XftChar8*)t, next - t);
432 x += ext.xOff;
433 }
434 if (f != font)
435 XftFontClose(win->env.dpy, f);
436 }
437 return tw;
438}
439
440static void win_draw_bar(win_t *win)
441{
442 int len, x, y, w, tw;
443 win_env_t *e;
444 win_bar_t *l, *r;
445 XftDraw *d;
446
447 if ((l = &win->bar.l)->buf == NULL || (r = &win->bar.r)->buf == NULL)
448 return;
449
450 e = &win->env;
451 y = (win->bar.top ? 0 : win->h) + font->ascent + V_TEXT_PAD;
452 w = win->w - 2*H_TEXT_PAD;
453 d = XftDrawCreate(e->dpy, win->buf.pm, e->vis, e->cmap);
454
455 XSetForeground(e->dpy, gc, win->bar_bg.pixel);
456 XFillRectangle(e->dpy, win->buf.pm, gc, 0, win->bar.top ? 0 : win->h, win->w, win->bar.h);
457
458 XSetForeground(e->dpy, gc, win->win_bg.pixel);
459 XSetBackground(e->dpy, gc, win->bar_bg.pixel);
460
461 if ((len = strlen(r->buf)) > 0) {
462 if ((tw = TEXTWIDTH(win, r->buf, len)) > w)
463 return;
464 x = win->w - tw - H_TEXT_PAD;
465 w -= tw;
466 win_draw_text(win, d, &win->bar_fg, x, y, r->buf, len, tw);
467 }
468 if ((len = strlen(l->buf)) > 0) {
469 x = H_TEXT_PAD;
470 w -= 2 * H_TEXT_PAD; /* gap between left and right parts */
471 win_draw_text(win, d, &win->bar_fg, x, y, l->buf, len, w);
472 }
473 XftDrawDestroy(d);
474}
475#else
476static void win_draw_bar(win_t *win)
477{
478 (void) win;
479}
480#endif /* HAVE_LIBFONTS */
481
482void win_draw(win_t *win)
483{
484 if (win->bar.h > 0)
485 win_draw_bar(win);
486
487 XSetWindowBackgroundPixmap(win->env.dpy, win->xwin, win->buf.pm);
488 XClearWindow(win->env.dpy, win->xwin);
489 XFlush(win->env.dpy);
490}
491
492void win_draw_rect(win_t *win, int x, int y, int w, int h, bool fill, int lw,
493 unsigned long col)
494{
495 XGCValues gcval;
496
497 gcval.line_width = lw;
498 gcval.foreground = col;
499 XChangeGC(win->env.dpy, gc, GCForeground | GCLineWidth, &gcval);
500
501 if (fill)
502 XFillRectangle(win->env.dpy, win->buf.pm, gc, x, y, w, h);
503 else
504 XDrawRectangle(win->env.dpy, win->buf.pm, gc, x, y, w, h);
505}
506
507void win_set_title(win_t *win, bool init)
508{
509 size_t len, i;
510 unsigned char title[512];
511 int targets[] = { ATOM_WM_NAME, ATOM_WM_ICON_NAME, ATOM__NET_WM_NAME, ATOM__NET_WM_ICON_NAME };
512
513 if ((len = get_win_title(title, ARRLEN(title), init)) > 0) {
514 for (i = 0; i < ARRLEN(targets); ++i) {
515 XChangeProperty(win->env.dpy, win->xwin, atoms[targets[i]],
516 atoms[ATOM_UTF8_STRING], 8, PropModeReplace, title, len);
517 }
518 }
519}
520
521void win_set_cursor(win_t *win, cursor_t cursor)
522{
523 if (cursor >= 0 && cursor < ARRLEN(cursors)) {
524 XDefineCursor(win->env.dpy, win->xwin, cursors[cursor].icon);
525 XFlush(win->env.dpy);
526 }
527}
528
529void win_cursor_pos(win_t *win, int *x, int *y)
530{
531 int i;
532 unsigned int ui;
533 Window w;
534
535 if (!XQueryPointer(win->env.dpy, win->xwin, &w, &w, &i, &i, x, y, &ui))
536 *x = *y = 0;
537}