Krolyxon
Commit 35a9ca3

Branches

Refs →

nsxivwindow.c

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