Krolyxon
Commit 18d63db

Branches

Refs →

nsxivwindow.c

534 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, *f;
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 f = win_res(e->dpy, RES_CLASS ".font", "monospace-8");
156 win_init_font(e, f);
157
158 bg = win_res(e->dpy, RES_CLASS ".background", "white");
159 fg = win_res(e->dpy, RES_CLASS ".foreground", "black");
160 win_alloc_color(e, bg, &win->bg);
161 win_alloc_color(e, fg, &win->fg);
162 win_alloc_color(e, "black", &win->black);
163 win->light = win_luminance(&win->bg) > win_luminance(&win->fg);
164
165 win->bar.l.size = BAR_L_LEN;
166 win->bar.r.size = BAR_R_LEN;
167 /* 3 padding bytes needed by utf8_decode */
168 win->bar.l.buf = emalloc(win->bar.l.size + 3);
169 win->bar.l.buf[0] = '\0';
170 win->bar.r.buf = emalloc(win->bar.r.size + 3);
171 win->bar.r.buf[0] = '\0';
172 win->bar.h = options->hide_bar ? 0 : barheight;
173
174 INIT_ATOM_(WM_DELETE_WINDOW);
175 INIT_ATOM_(_NET_WM_NAME);
176 INIT_ATOM_(_NET_WM_ICON_NAME);
177 INIT_ATOM_(_NET_WM_ICON);
178 INIT_ATOM_(_NET_WM_STATE);
179 INIT_ATOM_(_NET_WM_STATE_FULLSCREEN);
180 INIT_ATOM_(_NET_SUPPORTED);
181
182 win_check_wm_support(e->dpy, RootWindow(e->dpy, e->scr));
183}
184
185void win_open(win_t *win)
186{
187 int c, i, j, n;
188 long parent;
189 win_env_t *e;
190 XClassHint classhint;
191 unsigned long *icon_data;
192 XColor col;
193 Cursor *cnone = &cursors[CURSOR_NONE].icon;
194 char none_data[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
195 Pixmap none;
196 int gmask;
197 XSizeHints sizehints;
198 Bool fullscreen = options->fullscreen && fs_support;
199
200 e = &win->env;
201 parent = options->embed != 0 ? options->embed : RootWindow(e->dpy, e->scr);
202
203 sizehints.flags = PWinGravity;
204 sizehints.win_gravity = NorthWestGravity;
205
206 /* determine window offsets, width & height */
207 if (options->geometry == NULL)
208 gmask = 0;
209 else
210 gmask = XParseGeometry(options->geometry, &win->x, &win->y,
211 &win->w, &win->h);
212 if ((gmask & WidthValue) != 0)
213 sizehints.flags |= USSize;
214 else
215 win->w = WIN_WIDTH;
216 if ((gmask & HeightValue) != 0)
217 sizehints.flags |= USSize;
218 else
219 win->h = WIN_HEIGHT;
220 if ((gmask & XValue) != 0) {
221 if ((gmask & XNegative) != 0) {
222 win->x += e->scrw - win->w;
223 sizehints.win_gravity = NorthEastGravity;
224 }
225 sizehints.flags |= USPosition;
226 } else {
227 win->x = 0;
228 }
229 if ((gmask & YValue) != 0) {
230 if ((gmask & YNegative) != 0) {
231 win->y += e->scrh - win->h;
232 sizehints.win_gravity = sizehints.win_gravity == NorthEastGravity
233 ? SouthEastGravity : SouthWestGravity;
234 }
235 sizehints.flags |= USPosition;
236 } else {
237 win->y = 0;
238 }
239
240 win->xwin = XCreateWindow(e->dpy, parent,
241 win->x, win->y, win->w, win->h, 0,
242 e->depth, InputOutput, e->vis, 0, NULL);
243 if (win->xwin == None)
244 error(EXIT_FAILURE, 0, "Error creating X window");
245
246 XSelectInput(e->dpy, win->xwin,
247 ButtonReleaseMask | ButtonPressMask | KeyPressMask |
248 PointerMotionMask | StructureNotifyMask);
249
250 for (i = 0; i < ARRLEN(cursors); i++) {
251 if (i != CURSOR_NONE)
252 cursors[i].icon = XCreateFontCursor(e->dpy, cursors[i].name);
253 }
254 if (XAllocNamedColor(e->dpy, DefaultColormap(e->dpy, e->scr), "black",
255 &col, &col) == 0)
256 {
257 error(EXIT_FAILURE, 0, "Error allocating color 'black'");
258 }
259 none = XCreateBitmapFromData(e->dpy, win->xwin, none_data, 8, 8);
260 *cnone = XCreatePixmapCursor(e->dpy, none, none, &col, &col, 0, 0);
261
262 gc = XCreateGC(e->dpy, win->xwin, 0, None);
263
264 n = icons[ARRLEN(icons)-1].size;
265 icon_data = emalloc((n * n + 2) * sizeof(*icon_data));
266
267 for (i = 0; i < ARRLEN(icons); i++) {
268 n = 0;
269 icon_data[n++] = icons[i].size;
270 icon_data[n++] = icons[i].size;
271
272 for (j = 0; j < icons[i].cnt; j++) {
273 for (c = icons[i].data[j] >> 4; c >= 0; c--)
274 icon_data[n++] = icon_colors[icons[i].data[j] & 0x0F];
275 }
276 XChangeProperty(e->dpy, win->xwin,
277 atoms[ATOM__NET_WM_ICON], XA_CARDINAL, 32,
278 i == 0 ? PropModeReplace : PropModeAppend,
279 (unsigned char *) icon_data, n);
280 }
281 free(icon_data);
282
283 win_set_title(win, "sxiv");
284
285 classhint.res_class = RES_CLASS;
286 classhint.res_name = options->res_name != NULL ? options->res_name : "sxiv";
287 XSetClassHint(e->dpy, win->xwin, &classhint);
288
289 XSetWMProtocols(e->dpy, win->xwin, &atoms[ATOM_WM_DELETE_WINDOW], 1);
290
291 sizehints.width = win->w;
292 sizehints.height = win->h;
293 sizehints.x = win->x;
294 sizehints.y = win->y;
295 XSetWMNormalHints(win->env.dpy, win->xwin, &sizehints);
296
297 win->h -= win->bar.h;
298
299 win->buf.w = e->scrw;
300 win->buf.h = e->scrh;
301 win->buf.pm = XCreatePixmap(e->dpy, win->xwin,
302 win->buf.w, win->buf.h, e->depth);
303 XSetForeground(e->dpy, gc, fullscreen ? win->black.pixel : win->bg.pixel);
304 XFillRectangle(e->dpy, win->buf.pm, gc, 0, 0, win->buf.w, win->buf.h);
305 XSetWindowBackgroundPixmap(e->dpy, win->xwin, win->buf.pm);
306
307 XMapWindow(e->dpy, win->xwin);
308 XFlush(e->dpy);
309
310 if (fullscreen)
311 win_toggle_fullscreen(win);
312}
313
314CLEANUP void win_close(win_t *win)
315{
316 int i;
317
318 for (i = 0; i < ARRLEN(cursors); i++)
319 XFreeCursor(win->env.dpy, cursors[i].icon);
320
321 XFreeGC(win->env.dpy, gc);
322
323 XDestroyWindow(win->env.dpy, win->xwin);
324 XCloseDisplay(win->env.dpy);
325}
326
327bool win_configure(win_t *win, XConfigureEvent *c)
328{
329 bool changed;
330
331 changed = win->w != c->width || win->h + win->bar.h != c->height;
332
333 win->x = c->x;
334 win->y = c->y;
335 win->w = c->width;
336 win->h = c->height - win->bar.h;
337 win->bw = c->border_width;
338
339 return changed;
340}
341
342void win_toggle_fullscreen(win_t *win)
343{
344 XEvent ev;
345 XClientMessageEvent *cm;
346
347 if (!fs_support) {
348 if (!fs_warned) {
349 error(0, 0, "No fullscreen support");
350 fs_warned = True;
351 }
352 return;
353 }
354 win->fullscreen = !win->fullscreen;
355
356 memset(&ev, 0, sizeof(ev));
357 ev.type = ClientMessage;
358
359 cm = &ev.xclient;
360 cm->window = win->xwin;
361 cm->message_type = atoms[ATOM__NET_WM_STATE];
362 cm->format = 32;
363 cm->data.l[0] = win->fullscreen;
364 cm->data.l[1] = atoms[ATOM__NET_WM_STATE_FULLSCREEN];
365 cm->data.l[2] = cm->data.l[3] = 0;
366
367 XSendEvent(win->env.dpy, DefaultRootWindow(win->env.dpy), False,
368 SubstructureNotifyMask | SubstructureRedirectMask, &ev);
369}
370
371void win_toggle_bar(win_t *win)
372{
373 if (win->bar.h != 0) {
374 win->h += win->bar.h;
375 win->bar.h = 0;
376 } else {
377 win->bar.h = barheight;
378 win->h -= win->bar.h;
379 }
380}
381
382void win_clear(win_t *win)
383{
384 win_env_t *e;
385
386 e = &win->env;
387
388 if (win->w > win->buf.w || win->h + win->bar.h > win->buf.h) {
389 XFreePixmap(e->dpy, win->buf.pm);
390 win->buf.w = MAX(win->buf.w, win->w);
391 win->buf.h = MAX(win->buf.h, win->h + win->bar.h);
392 win->buf.pm = XCreatePixmap(e->dpy, win->xwin,
393 win->buf.w, win->buf.h, e->depth);
394 }
395 XSetForeground(e->dpy, gc, win->fullscreen ? win->black.pixel : win->bg.pixel);
396 XFillRectangle(e->dpy, win->buf.pm, gc, 0, 0, win->buf.w, win->buf.h);
397}
398
399#define TEXTWIDTH(win, text, len) \
400 win_draw_text(win, NULL, NULL, 0, 0, text, len, 0)
401
402int win_draw_text(win_t *win, XftDraw *d, const XftColor *color, int x, int y,
403 char *text, int len, int w)
404{
405 int err, tw = 0;
406 char *t, *next;
407 uint32_t rune;
408 XftFont *f;
409 FcCharSet *fccharset;
410 XGlyphInfo ext;
411
412 for (t = text; t - text < len; t = next) {
413 next = utf8_decode(t, &rune, &err);
414 if (XftCharExists(win->env.dpy, font, rune)) {
415 f = font;
416 } else { /* fallback font */
417 fccharset = FcCharSetCreate();
418 FcCharSetAddChar(fccharset, rune);
419 f = XftFontOpen(win->env.dpy, win->env.scr, FC_CHARSET, FcTypeCharSet,
420 fccharset, FC_SCALABLE, FcTypeBool, FcTrue,
421 FC_SIZE, FcTypeDouble, fontsize, NULL);
422 FcCharSetDestroy(fccharset);
423 }
424 XftTextExtentsUtf8(win->env.dpy, f, (XftChar8*)t, next - t, &ext);
425 tw += ext.xOff;
426 if (tw <= w) {
427 XftDrawStringUtf8(d, color, f, x, y, (XftChar8*)t, next - t);
428 x += ext.xOff;
429 }
430 if (f != font)
431 XftFontClose(win->env.dpy, f);
432 }
433 return tw;
434}
435
436void win_draw_bar(win_t *win)
437{
438 int len, x, y, w, tw;
439 win_env_t *e;
440 win_bar_t *l, *r;
441 XftDraw *d;
442 const XftColor *bg, *fg;
443
444 if ((l = &win->bar.l)->buf == NULL || (r = &win->bar.r)->buf == NULL)
445 return;
446
447 e = &win->env;
448 y = win->h + font->ascent + V_TEXT_PAD;
449 w = win->w - 2*H_TEXT_PAD;
450 d = XftDrawCreate(e->dpy, win->buf.pm, DefaultVisual(e->dpy, e->scr),
451 DefaultColormap(e->dpy, e->scr));
452
453 if (win->fullscreen && !win->light)
454 bg = &win->bg, fg = &win->fg;
455 else
456 bg = &win->fg, fg = &win->bg;
457
458 XSetForeground(e->dpy, gc, bg->pixel);
459 XFillRectangle(e->dpy, win->buf.pm, gc, 0, win->h, win->w, win->bar.h);
460
461 XSetForeground(e->dpy, gc, fg->pixel);
462 XSetBackground(e->dpy, gc, bg->pixel);
463
464 if ((len = strlen(r->buf)) > 0) {
465 if ((tw = TEXTWIDTH(win, r->buf, len)) > w)
466 return;
467 x = win->w - tw - H_TEXT_PAD;
468 w -= tw;
469 win_draw_text(win, d, fg, x, y, r->buf, len, tw);
470 }
471 if ((len = strlen(l->buf)) > 0) {
472 x = H_TEXT_PAD;
473 w -= 2 * H_TEXT_PAD; /* gap between left and right parts */
474 win_draw_text(win, d, fg, x, y, l->buf, len, w);
475 }
476 XftDrawDestroy(d);
477}
478
479void win_draw(win_t *win)
480{
481 if (win->bar.h > 0)
482 win_draw_bar(win);
483
484 XSetWindowBackgroundPixmap(win->env.dpy, win->xwin, win->buf.pm);
485 XClearWindow(win->env.dpy, win->xwin);
486 XFlush(win->env.dpy);
487}
488
489void win_draw_rect(win_t *win, int x, int y, int w, int h, bool fill, int lw,
490 unsigned long col)
491{
492 XGCValues gcval;
493
494 gcval.line_width = lw;
495 gcval.foreground = col;
496 XChangeGC(win->env.dpy, gc, GCForeground | GCLineWidth, &gcval);
497
498 if (fill)
499 XFillRectangle(win->env.dpy, win->buf.pm, gc, x, y, w, h);
500 else
501 XDrawRectangle(win->env.dpy, win->buf.pm, gc, x, y, w, h);
502}
503
504void win_set_title(win_t *win, const char *title)
505{
506 XStoreName(win->env.dpy, win->xwin, title);
507 XSetIconName(win->env.dpy, win->xwin, title);
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