Krolyxon
Commit 156a537

Branches

Refs →

nsxivwindow.c

531 lines · 14.1 KB · C

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