Krolyxon
Commit f37fa09

Branches

Refs →

nsxivwindow.c

534 lines · 14.2 KB · C

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