Krolyxon
Commit 1ef5429

Branches

Refs →

nsxivwindow.c

530 lines · 13.7 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 <X11/cursorfont.h>
29#include <X11/Xatom.h>
30#include <X11/Xresource.h>
31
32#define RES_CLASS "Sxiv"
33
34enum {
35 H_TEXT_PAD = 5,
36 V_TEXT_PAD = 1
37};
38
39static struct {
40 int name;
41 Cursor icon;
42} cursors[CURSOR_COUNT] = {
43 { XC_left_ptr }, { XC_dotbox }, { XC_watch },
44 { XC_sb_left_arrow }, { XC_sb_right_arrow }
45};
46
47static GC gc;
48
49static XftFont *font;
50static int fontheight;
51static int barheight;
52
53Atom atoms[ATOM_COUNT];
54
55static Bool fs_support;
56static Bool fs_warned;
57
58void win_init_font(const win_env_t *e, const char *fontstr)
59{
60 if ((font = XftFontOpenName(e->dpy, e->scr, fontstr)) == NULL)
61 error(EXIT_FAILURE, 0, "Error loading font '%s'", fontstr);
62 fontheight = font->ascent + font->descent;
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, DefaultVisual(e->dpy, e->scr),
69 DefaultColormap(e->dpy, e->scr), name, col))
70 {
71 error(EXIT_FAILURE, 0, "Error allocating color '%s'", name);
72 }
73}
74
75void win_check_wm_support(Display *dpy, Window root)
76{
77 int format;
78 long offset = 0, length = 16;
79 Atom *data, type;
80 unsigned long i, nitems, bytes_left;
81 Bool found = False;
82
83 while (!found && length > 0) {
84 if (XGetWindowProperty(dpy, root, atoms[ATOM__NET_SUPPORTED],
85 offset, length, False, XA_ATOM, &type, &format,
86 &nitems, &bytes_left, (unsigned char**) &data))
87 {
88 break;
89 }
90 if (type == XA_ATOM && format == 32) {
91 for (i = 0; i < nitems; i++) {
92 if (data[i] == atoms[ATOM__NET_WM_STATE_FULLSCREEN]) {
93 found = True;
94 fs_support = True;
95 break;
96 }
97 }
98 }
99 XFree(data);
100 offset += nitems;
101 length = MIN(length, bytes_left / 4);
102 }
103}
104
105const char* win_res(Display *dpy, const char *name, const char *def)
106{
107 char *type;
108 XrmValue ret;
109 XrmDatabase db;
110 char *res_man;
111
112 XrmInitialize();
113
114 if ((res_man = XResourceManagerString(dpy)) != NULL &&
115 (db = XrmGetStringDatabase(res_man)) != NULL &&
116 XrmGetResource(db, name, name, &type, &ret) && STREQ(type, "String"))
117 {
118 return ret.addr;
119 } else {
120 return def;
121 }
122}
123
124unsigned int win_luminance(const XftColor *col)
125{
126 return (col->color.red + col->color.green + col->color.blue) / 3;
127}
128
129#define INIT_ATOM_(atom) \
130 atoms[ATOM_##atom] = XInternAtom(e->dpy, #atom, False);
131
132void win_init(win_t *win)
133{
134 win_env_t *e;
135 const char *bg, *fg;
136
137 memset(win, 0, sizeof(win_t));
138
139 e = &win->env;
140 if ((e->dpy = XOpenDisplay(NULL)) == NULL)
141 error(EXIT_FAILURE, 0, "Error opening X display");
142
143 e->scr = DefaultScreen(e->dpy);
144 e->scrw = DisplayWidth(e->dpy, e->scr);
145 e->scrh = DisplayHeight(e->dpy, e->scr);
146 e->vis = DefaultVisual(e->dpy, e->scr);
147 e->cmap = DefaultColormap(e->dpy, e->scr);
148 e->depth = DefaultDepth(e->dpy, e->scr);
149
150 if (setlocale(LC_CTYPE, "") == NULL || XSupportsLocale() == 0)
151 error(0, 0, "No locale support");
152
153 win_init_font(e, BAR_FONT);
154
155 bg = win_res(e->dpy, RES_CLASS ".background", BG_COLOR);
156 fg = win_res(e->dpy, RES_CLASS ".foreground", FG_COLOR);
157 win_alloc_color(e, bg, &win->bg);
158 win_alloc_color(e, fg, &win->fg);
159 win_alloc_color(e, "black", &win->black);
160 win->light = win_luminance(&win->bg) > win_luminance(&win->fg);
161
162 win->bar.l.size = BAR_L_LEN;
163 win->bar.r.size = BAR_R_LEN;
164 /* 3 padding bytes needed by utf8_decode */
165 win->bar.l.buf = emalloc(win->bar.l.size + 3);
166 win->bar.l.buf[0] = '\0';
167 win->bar.r.buf = emalloc(win->bar.r.size + 3);
168 win->bar.r.buf[0] = '\0';
169 win->bar.h = options->hide_bar ? 0 : barheight;
170
171 INIT_ATOM_(WM_DELETE_WINDOW);
172 INIT_ATOM_(_NET_WM_NAME);
173 INIT_ATOM_(_NET_WM_ICON_NAME);
174 INIT_ATOM_(_NET_WM_ICON);
175 INIT_ATOM_(_NET_WM_STATE);
176 INIT_ATOM_(_NET_WM_STATE_FULLSCREEN);
177 INIT_ATOM_(_NET_SUPPORTED);
178
179 win_check_wm_support(e->dpy, RootWindow(e->dpy, e->scr));
180}
181
182void win_open(win_t *win)
183{
184 int c, i, j, n;
185 long parent;
186 win_env_t *e;
187 XClassHint classhint;
188 unsigned long *icon_data;
189 XColor col;
190 Cursor *cnone = &cursors[CURSOR_NONE].icon;
191 char none_data[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
192 Pixmap none;
193 int gmask;
194 XSizeHints sizehints;
195 Bool fullscreen = options->fullscreen && fs_support;
196
197 e = &win->env;
198 parent = options->embed != 0 ? options->embed : RootWindow(e->dpy, e->scr);
199
200 sizehints.flags = PWinGravity;
201 sizehints.win_gravity = NorthWestGravity;
202
203 /* determine window offsets, width & height */
204 if (options->geometry == NULL)
205 gmask = 0;
206 else
207 gmask = XParseGeometry(options->geometry, &win->x, &win->y,
208 &win->w, &win->h);
209 if ((gmask & WidthValue) != 0)
210 sizehints.flags |= USSize;
211 else
212 win->w = WIN_WIDTH;
213 if ((gmask & HeightValue) != 0)
214 sizehints.flags |= USSize;
215 else
216 win->h = WIN_HEIGHT;
217 if ((gmask & XValue) != 0) {
218 if ((gmask & XNegative) != 0) {
219 win->x += e->scrw - win->w;
220 sizehints.win_gravity = NorthEastGravity;
221 }
222 sizehints.flags |= USPosition;
223 } else {
224 win->x = 0;
225 }
226 if ((gmask & YValue) != 0) {
227 if ((gmask & YNegative) != 0) {
228 win->y += e->scrh - win->h;
229 sizehints.win_gravity = sizehints.win_gravity == NorthEastGravity
230 ? SouthEastGravity : SouthWestGravity;
231 }
232 sizehints.flags |= USPosition;
233 } else {
234 win->y = 0;
235 }
236
237 win->xwin = XCreateWindow(e->dpy, parent,
238 win->x, win->y, win->w, win->h, 0,
239 e->depth, InputOutput, e->vis, 0, NULL);
240 if (win->xwin == None)
241 error(EXIT_FAILURE, 0, "Error creating X window");
242
243 XSelectInput(e->dpy, win->xwin,
244 ButtonReleaseMask | ButtonPressMask | KeyPressMask |
245 PointerMotionMask | StructureNotifyMask);
246
247 for (i = 0; i < ARRLEN(cursors); i++) {
248 if (i != CURSOR_NONE)
249 cursors[i].icon = XCreateFontCursor(e->dpy, cursors[i].name);
250 }
251 if (XAllocNamedColor(e->dpy, DefaultColormap(e->dpy, e->scr), "black",
252 &col, &col) == 0)
253 {
254 error(EXIT_FAILURE, 0, "Error allocating color 'black'");
255 }
256 none = XCreateBitmapFromData(e->dpy, win->xwin, none_data, 8, 8);
257 *cnone = XCreatePixmapCursor(e->dpy, none, none, &col, &col, 0, 0);
258
259 gc = XCreateGC(e->dpy, win->xwin, 0, None);
260
261 n = icons[ARRLEN(icons)-1].size;
262 icon_data = emalloc((n * n + 2) * sizeof(*icon_data));
263
264 for (i = 0; i < ARRLEN(icons); i++) {
265 n = 0;
266 icon_data[n++] = icons[i].size;
267 icon_data[n++] = icons[i].size;
268
269 for (j = 0; j < icons[i].cnt; j++) {
270 for (c = icons[i].data[j] >> 4; c >= 0; c--)
271 icon_data[n++] = icon_colors[icons[i].data[j] & 0x0F];
272 }
273 XChangeProperty(e->dpy, win->xwin,
274 atoms[ATOM__NET_WM_ICON], XA_CARDINAL, 32,
275 i == 0 ? PropModeReplace : PropModeAppend,
276 (unsigned char *) icon_data, n);
277 }
278 free(icon_data);
279
280 win_set_title(win, "sxiv");
281
282 classhint.res_class = RES_CLASS;
283 classhint.res_name = options->res_name != NULL ? options->res_name : "sxiv";
284 XSetClassHint(e->dpy, win->xwin, &classhint);
285
286 XSetWMProtocols(e->dpy, win->xwin, &atoms[ATOM_WM_DELETE_WINDOW], 1);
287
288 sizehints.width = win->w;
289 sizehints.height = win->h;
290 sizehints.x = win->x;
291 sizehints.y = win->y;
292 XSetWMNormalHints(win->env.dpy, win->xwin, &sizehints);
293
294 win->h -= win->bar.h;
295
296 win->buf.w = e->scrw;
297 win->buf.h = e->scrh;
298 win->buf.pm = XCreatePixmap(e->dpy, win->xwin,
299 win->buf.w, win->buf.h, e->depth);
300 XSetForeground(e->dpy, gc, fullscreen ? win->black.pixel : win->bg.pixel);
301 XFillRectangle(e->dpy, win->buf.pm, gc, 0, 0, win->buf.w, win->buf.h);
302 XSetWindowBackgroundPixmap(e->dpy, win->xwin, win->buf.pm);
303
304 XMapWindow(e->dpy, win->xwin);
305 XFlush(e->dpy);
306
307 if (fullscreen)
308 win_toggle_fullscreen(win);
309}
310
311CLEANUP void win_close(win_t *win)
312{
313 int i;
314
315 for (i = 0; i < ARRLEN(cursors); i++)
316 XFreeCursor(win->env.dpy, cursors[i].icon);
317
318 XFreeGC(win->env.dpy, gc);
319
320 XDestroyWindow(win->env.dpy, win->xwin);
321 XCloseDisplay(win->env.dpy);
322}
323
324bool win_configure(win_t *win, XConfigureEvent *c)
325{
326 bool changed;
327
328 changed = win->w != c->width || win->h + win->bar.h != c->height;
329
330 win->x = c->x;
331 win->y = c->y;
332 win->w = c->width;
333 win->h = c->height - win->bar.h;
334 win->bw = c->border_width;
335
336 return changed;
337}
338
339void win_toggle_fullscreen(win_t *win)
340{
341 XEvent ev;
342 XClientMessageEvent *cm;
343
344 if (!fs_support) {
345 if (!fs_warned) {
346 error(0, 0, "No fullscreen support");
347 fs_warned = True;
348 }
349 return;
350 }
351 win->fullscreen = !win->fullscreen;
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] = win->fullscreen;
361 cm->data.l[1] = atoms[ATOM__NET_WM_STATE_FULLSCREEN];
362 cm->data.l[2] = cm->data.l[3] = 0;
363
364 XSendEvent(win->env.dpy, DefaultRootWindow(win->env.dpy), False,
365 SubstructureNotifyMask | SubstructureRedirectMask, &ev);
366}
367
368void win_toggle_bar(win_t *win)
369{
370 if (win->bar.h != 0) {
371 win->h += win->bar.h;
372 win->bar.h = 0;
373 } else {
374 win->bar.h = barheight;
375 win->h -= win->bar.h;
376 }
377}
378
379void win_clear(win_t *win)
380{
381 win_env_t *e;
382
383 e = &win->env;
384
385 if (win->w > win->buf.w || win->h + win->bar.h > win->buf.h) {
386 XFreePixmap(e->dpy, win->buf.pm);
387 win->buf.w = MAX(win->buf.w, win->w);
388 win->buf.h = MAX(win->buf.h, win->h + win->bar.h);
389 win->buf.pm = XCreatePixmap(e->dpy, win->xwin,
390 win->buf.w, win->buf.h, e->depth);
391 }
392 XSetForeground(e->dpy, gc, win->fullscreen ? win->black.pixel : win->bg.pixel);
393 XFillRectangle(e->dpy, win->buf.pm, gc, 0, 0, win->buf.w, win->buf.h);
394}
395
396#define TEXTWIDTH(win, text, len) \
397 win_draw_text(win, NULL, NULL, 0, 0, text, len, 0)
398
399int win_draw_text(win_t *win, XftDraw *d, const XftColor *color, int x, int y,
400 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, 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 const XftColor *bg, *fg;
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, DefaultVisual(e->dpy, e->scr),
447 DefaultColormap(e->dpy, e->scr));
448
449 if (win->fullscreen && !win->light)
450 bg = &win->bg, fg = &win->fg;
451 else
452 bg = &win->fg, fg = &win->bg;
453
454 XSetForeground(e->dpy, gc, bg->pixel);
455 XFillRectangle(e->dpy, win->buf.pm, gc, 0, win->h, win->w, win->bar.h);
456
457 XSetForeground(e->dpy, gc, fg->pixel);
458 XSetBackground(e->dpy, gc, bg->pixel);
459
460 if ((len = strlen(r->buf)) > 0) {
461 if ((tw = TEXTWIDTH(win, r->buf, len)) > w)
462 return;
463 x = win->w - tw - H_TEXT_PAD;
464 w -= tw;
465 win_draw_text(win, d, fg, x, y, r->buf, len, tw);
466 }
467 if ((len = strlen(l->buf)) > 0) {
468 x = H_TEXT_PAD;
469 w -= 2 * H_TEXT_PAD; /* gap between left and right parts */
470 win_draw_text(win, d, fg, x, y, l->buf, len, w);
471 }
472 XftDrawDestroy(d);
473}
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 *title)
501{
502 XStoreName(win->env.dpy, win->xwin, title);
503 XSetIconName(win->env.dpy, win->xwin, title);
504
505 XChangeProperty(win->env.dpy, win->xwin, atoms[ATOM__NET_WM_NAME],
506 XInternAtom(win->env.dpy, "UTF8_STRING", False), 8,
507 PropModeReplace, (unsigned char *) title, strlen(title));
508 XChangeProperty(win->env.dpy, win->xwin, atoms[ATOM__NET_WM_ICON_NAME],
509 XInternAtom(win->env.dpy, "UTF8_STRING", False), 8,
510 PropModeReplace, (unsigned char *) title, strlen(title));
511}
512
513void win_set_cursor(win_t *win, cursor_t cursor)
514{
515 if (cursor >= 0 && cursor < ARRLEN(cursors)) {
516 XDefineCursor(win->env.dpy, win->xwin, cursors[cursor].icon);
517 XFlush(win->env.dpy);
518 }
519}
520
521void win_cursor_pos(win_t *win, int *x, int *y)
522{
523 int i;
524 unsigned int ui;
525 Window w;
526
527 if (!XQueryPointer(win->env.dpy, win->xwin, &w, &w, &i, &i, x, y, &ui))
528 *x = *y = 0;
529}
530