Krolyxon
Commit 61da0a8

Branches

Refs →

nsxivwindow.c

539 lines · 14.5 KB · C

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