Krolyxon
Commit a754115

Branches

Refs →

nsxivwindow.c

533 lines · 13.8 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 double fontsize;
52static int barheight;
53
54Atom atoms[ATOM_COUNT];
55
56static Bool fs_support;
57static Bool fs_warned;
58
59void win_init_font(const win_env_t *e, const char *fontstr)
60{
61 if ((font = XftFontOpenName(e->dpy, e->scr, fontstr)) == NULL)
62 error(EXIT_FAILURE, 0, "Error loading font '%s'", fontstr);
63 fontheight = font->ascent + font->descent;
64 FcPatternGetDouble(font->pattern, FC_SIZE, 0, &fontsize);
65 barheight = fontheight + 2 * V_TEXT_PAD;
66}
67
68void win_alloc_color(const win_env_t *e, const char *name, XftColor *col)
69{
70 if (!XftColorAllocName(e->dpy, DefaultVisual(e->dpy, e->scr),
71 DefaultColormap(e->dpy, e->scr), name, col))
72 {
73 error(EXIT_FAILURE, 0, "Error allocating color '%s'", name);
74 }
75}
76
77void win_check_wm_support(Display *dpy, Window root)
78{
79 int format;
80 long offset = 0, length = 16;
81 Atom *data, type;
82 unsigned long i, nitems, bytes_left;
83 Bool found = False;
84
85 while (!found && length > 0) {
86 if (XGetWindowProperty(dpy, root, atoms[ATOM__NET_SUPPORTED],
87 offset, length, False, XA_ATOM, &type, &format,
88 &nitems, &bytes_left, (unsigned char**) &data))
89 {
90 break;
91 }
92 if (type == XA_ATOM && format == 32) {
93 for (i = 0; i < nitems; i++) {
94 if (data[i] == atoms[ATOM__NET_WM_STATE_FULLSCREEN]) {
95 found = True;
96 fs_support = True;
97 break;
98 }
99 }
100 }
101 XFree(data);
102 offset += nitems;
103 length = MIN(length, bytes_left / 4);
104 }
105}
106
107const char* win_res(Display *dpy, const char *name, const char *def)
108{
109 char *type;
110 XrmValue ret;
111 XrmDatabase db;
112 char *res_man;
113
114 XrmInitialize();
115
116 if ((res_man = XResourceManagerString(dpy)) != NULL &&
117 (db = XrmGetStringDatabase(res_man)) != NULL &&
118 XrmGetResource(db, name, name, &type, &ret) && STREQ(type, "String"))
119 {
120 return ret.addr;
121 } else {
122 return def;
123 }
124}
125
126unsigned int win_luminance(const XftColor *col)
127{
128 return (col->color.red + col->color.green + col->color.blue) / 3;
129}
130
131#define INIT_ATOM_(atom) \
132 atoms[ATOM_##atom] = XInternAtom(e->dpy, #atom, False);
133
134void win_init(win_t *win)
135{
136 win_env_t *e;
137 const char *bg, *fg;
138
139 memset(win, 0, sizeof(win_t));
140
141 e = &win->env;
142 if ((e->dpy = XOpenDisplay(NULL)) == NULL)
143 error(EXIT_FAILURE, 0, "Error opening X display");
144
145 e->scr = DefaultScreen(e->dpy);
146 e->scrw = DisplayWidth(e->dpy, e->scr);
147 e->scrh = DisplayHeight(e->dpy, e->scr);
148 e->vis = DefaultVisual(e->dpy, e->scr);
149 e->cmap = DefaultColormap(e->dpy, e->scr);
150 e->depth = DefaultDepth(e->dpy, e->scr);
151
152 if (setlocale(LC_CTYPE, "") == NULL || XSupportsLocale() == 0)
153 error(0, 0, "No locale support");
154
155 win_init_font(e, BAR_FONT);
156
157 bg = win_res(e->dpy, RES_CLASS ".background", BG_COLOR);
158 fg = win_res(e->dpy, RES_CLASS ".foreground", FG_COLOR);
159 win_alloc_color(e, bg, &win->bg);
160 win_alloc_color(e, fg, &win->fg);
161 win_alloc_color(e, "black", &win->black);
162 win->light = win_luminance(&win->bg) > win_luminance(&win->fg);
163
164 win->bar.l.size = BAR_L_LEN;
165 win->bar.r.size = BAR_R_LEN;
166 /* 3 padding bytes needed by utf8_decode */
167 win->bar.l.buf = emalloc(win->bar.l.size + 3);
168 win->bar.l.buf[0] = '\0';
169 win->bar.r.buf = emalloc(win->bar.r.size + 3);
170 win->bar.r.buf[0] = '\0';
171 win->bar.h = options->hide_bar ? 0 : barheight;
172
173 INIT_ATOM_(WM_DELETE_WINDOW);
174 INIT_ATOM_(_NET_WM_NAME);
175 INIT_ATOM_(_NET_WM_ICON_NAME);
176 INIT_ATOM_(_NET_WM_ICON);
177 INIT_ATOM_(_NET_WM_STATE);
178 INIT_ATOM_(_NET_WM_STATE_FULLSCREEN);
179 INIT_ATOM_(_NET_SUPPORTED);
180
181 win_check_wm_support(e->dpy, RootWindow(e->dpy, e->scr));
182}
183
184void win_open(win_t *win)
185{
186 int c, i, j, n;
187 long parent;
188 win_env_t *e;
189 XClassHint classhint;
190 unsigned long *icon_data;
191 XColor col;
192 Cursor *cnone = &cursors[CURSOR_NONE].icon;
193 char none_data[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
194 Pixmap none;
195 int gmask;
196 XSizeHints sizehints;
197 Bool fullscreen = options->fullscreen && fs_support;
198
199 e = &win->env;
200 parent = options->embed != 0 ? options->embed : RootWindow(e->dpy, e->scr);
201
202 sizehints.flags = PWinGravity;
203 sizehints.win_gravity = NorthWestGravity;
204
205 /* determine window offsets, width & height */
206 if (options->geometry == NULL)
207 gmask = 0;
208 else
209 gmask = XParseGeometry(options->geometry, &win->x, &win->y,
210 &win->w, &win->h);
211 if ((gmask & WidthValue) != 0)
212 sizehints.flags |= USSize;
213 else
214 win->w = WIN_WIDTH;
215 if ((gmask & HeightValue) != 0)
216 sizehints.flags |= USSize;
217 else
218 win->h = WIN_HEIGHT;
219 if ((gmask & XValue) != 0) {
220 if ((gmask & XNegative) != 0) {
221 win->x += e->scrw - win->w;
222 sizehints.win_gravity = NorthEastGravity;
223 }
224 sizehints.flags |= USPosition;
225 } else {
226 win->x = 0;
227 }
228 if ((gmask & YValue) != 0) {
229 if ((gmask & YNegative) != 0) {
230 win->y += e->scrh - win->h;
231 sizehints.win_gravity = sizehints.win_gravity == NorthEastGravity
232 ? SouthEastGravity : SouthWestGravity;
233 }
234 sizehints.flags |= USPosition;
235 } else {
236 win->y = 0;
237 }
238
239 win->xwin = XCreateWindow(e->dpy, parent,
240 win->x, win->y, win->w, win->h, 0,
241 e->depth, InputOutput, e->vis, 0, NULL);
242 if (win->xwin == None)
243 error(EXIT_FAILURE, 0, "Error creating X window");
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, DefaultColormap(e->dpy, e->scr), "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 win_set_title(win, "sxiv");
283
284 classhint.res_class = RES_CLASS;
285 classhint.res_name = options->res_name != NULL ? options->res_name : "sxiv";
286 XSetClassHint(e->dpy, win->xwin, &classhint);
287
288 XSetWMProtocols(e->dpy, win->xwin, &atoms[ATOM_WM_DELETE_WINDOW], 1);
289
290 sizehints.width = win->w;
291 sizehints.height = win->h;
292 sizehints.x = win->x;
293 sizehints.y = win->y;
294 XSetWMNormalHints(win->env.dpy, win->xwin, &sizehints);
295
296 win->h -= win->bar.h;
297
298 win->buf.w = e->scrw;
299 win->buf.h = e->scrh;
300 win->buf.pm = XCreatePixmap(e->dpy, win->xwin,
301 win->buf.w, win->buf.h, e->depth);
302 XSetForeground(e->dpy, gc, fullscreen ? win->black.pixel : win->bg.pixel);
303 XFillRectangle(e->dpy, win->buf.pm, gc, 0, 0, win->buf.w, win->buf.h);
304 XSetWindowBackgroundPixmap(e->dpy, win->xwin, win->buf.pm);
305
306 XMapWindow(e->dpy, win->xwin);
307 XFlush(e->dpy);
308
309 if (fullscreen)
310 win_toggle_fullscreen(win);
311}
312
313CLEANUP void win_close(win_t *win)
314{
315 int i;
316
317 for (i = 0; i < ARRLEN(cursors); i++)
318 XFreeCursor(win->env.dpy, cursors[i].icon);
319
320 XFreeGC(win->env.dpy, gc);
321
322 XDestroyWindow(win->env.dpy, win->xwin);
323 XCloseDisplay(win->env.dpy);
324}
325
326bool win_configure(win_t *win, XConfigureEvent *c)
327{
328 bool changed;
329
330 changed = win->w != c->width || win->h + win->bar.h != c->height;
331
332 win->x = c->x;
333 win->y = c->y;
334 win->w = c->width;
335 win->h = c->height - win->bar.h;
336 win->bw = c->border_width;
337
338 return changed;
339}
340
341void win_toggle_fullscreen(win_t *win)
342{
343 XEvent ev;
344 XClientMessageEvent *cm;
345
346 if (!fs_support) {
347 if (!fs_warned) {
348 error(0, 0, "No fullscreen support");
349 fs_warned = True;
350 }
351 return;
352 }
353 win->fullscreen = !win->fullscreen;
354
355 memset(&ev, 0, sizeof(ev));
356 ev.type = ClientMessage;
357
358 cm = &ev.xclient;
359 cm->window = win->xwin;
360 cm->message_type = atoms[ATOM__NET_WM_STATE];
361 cm->format = 32;
362 cm->data.l[0] = win->fullscreen;
363 cm->data.l[1] = atoms[ATOM__NET_WM_STATE_FULLSCREEN];
364 cm->data.l[2] = cm->data.l[3] = 0;
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->fullscreen ? win->black.pixel : win->bg.pixel);
395 XFillRectangle(e->dpy, win->buf.pm, gc, 0, 0, win->buf.w, win->buf.h);
396}
397
398#define TEXTWIDTH(win, text, len) \
399 win_draw_text(win, NULL, NULL, 0, 0, text, len, 0)
400
401int win_draw_text(win_t *win, XftDraw *d, const XftColor *color, int x, int y,
402 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
435void 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 const XftColor *bg, *fg;
442
443 if ((l = &win->bar.l)->buf == NULL || (r = &win->bar.r)->buf == NULL)
444 return;
445
446 e = &win->env;
447 y = win->h + font->ascent + V_TEXT_PAD;
448 w = win->w - 2*H_TEXT_PAD;
449 d = XftDrawCreate(e->dpy, win->buf.pm, DefaultVisual(e->dpy, e->scr),
450 DefaultColormap(e->dpy, e->scr));
451
452 if (win->fullscreen && !win->light)
453 bg = &win->bg, fg = &win->fg;
454 else
455 bg = &win->fg, fg = &win->bg;
456
457 XSetForeground(e->dpy, gc, bg->pixel);
458 XFillRectangle(e->dpy, win->buf.pm, gc, 0, win->h, win->w, win->bar.h);
459
460 XSetForeground(e->dpy, gc, fg->pixel);
461 XSetBackground(e->dpy, gc, bg->pixel);
462
463 if ((len = strlen(r->buf)) > 0) {
464 if ((tw = TEXTWIDTH(win, r->buf, len)) > w)
465 return;
466 x = win->w - tw - H_TEXT_PAD;
467 w -= tw;
468 win_draw_text(win, d, fg, x, y, r->buf, len, tw);
469 }
470 if ((len = strlen(l->buf)) > 0) {
471 x = H_TEXT_PAD;
472 w -= 2 * H_TEXT_PAD; /* gap between left and right parts */
473 win_draw_text(win, d, fg, x, y, l->buf, len, w);
474 }
475 XftDrawDestroy(d);
476}
477
478void win_draw(win_t *win)
479{
480 if (win->bar.h > 0)
481 win_draw_bar(win);
482
483 XSetWindowBackgroundPixmap(win->env.dpy, win->xwin, win->buf.pm);
484 XClearWindow(win->env.dpy, win->xwin);
485 XFlush(win->env.dpy);
486}
487
488void win_draw_rect(win_t *win, int x, int y, int w, int h, bool fill, int lw,
489 unsigned long col)
490{
491 XGCValues gcval;
492
493 gcval.line_width = lw;
494 gcval.foreground = col;
495 XChangeGC(win->env.dpy, gc, GCForeground | GCLineWidth, &gcval);
496
497 if (fill)
498 XFillRectangle(win->env.dpy, win->buf.pm, gc, x, y, w, h);
499 else
500 XDrawRectangle(win->env.dpy, win->buf.pm, gc, x, y, w, h);
501}
502
503void win_set_title(win_t *win, const char *title)
504{
505 XStoreName(win->env.dpy, win->xwin, title);
506 XSetIconName(win->env.dpy, win->xwin, title);
507
508 XChangeProperty(win->env.dpy, win->xwin, atoms[ATOM__NET_WM_NAME],
509 XInternAtom(win->env.dpy, "UTF8_STRING", False), 8,
510 PropModeReplace, (unsigned char *) title, strlen(title));
511 XChangeProperty(win->env.dpy, win->xwin, atoms[ATOM__NET_WM_ICON_NAME],
512 XInternAtom(win->env.dpy, "UTF8_STRING", False), 8,
513 PropModeReplace, (unsigned char *) title, strlen(title));
514}
515
516void win_set_cursor(win_t *win, cursor_t cursor)
517{
518 if (cursor >= 0 && cursor < ARRLEN(cursors)) {
519 XDefineCursor(win->env.dpy, win->xwin, cursors[cursor].icon);
520 XFlush(win->env.dpy);
521 }
522}
523
524void win_cursor_pos(win_t *win, int *x, int *y)
525{
526 int i;
527 unsigned int ui;
528 Window w;
529
530 if (!XQueryPointer(win->env.dpy, win->xwin, &w, &w, &i, &i, x, y, &ui))
531 *x = *y = 0;
532}
533