Krolyxon
Commit 2886876

Branches

Refs →

nsxivwindow.c

521 lines · 13.4 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
126#define INIT_ATOM_(atom) \
127 atoms[ATOM_##atom] = XInternAtom(e->dpy, #atom, False);
128
129void win_init(win_t *win)
130{
131 win_env_t *e;
132 const char *bg, *fg, *f;
133
134 memset(win, 0, sizeof(win_t));
135
136 e = &win->env;
137 if ((e->dpy = XOpenDisplay(NULL)) == NULL)
138 error(EXIT_FAILURE, 0, "Error opening X display");
139
140 e->scr = DefaultScreen(e->dpy);
141 e->scrw = DisplayWidth(e->dpy, e->scr);
142 e->scrh = DisplayHeight(e->dpy, e->scr);
143 e->vis = DefaultVisual(e->dpy, e->scr);
144 e->cmap = DefaultColormap(e->dpy, e->scr);
145 e->depth = DefaultDepth(e->dpy, e->scr);
146
147 if (setlocale(LC_CTYPE, "") == NULL || XSupportsLocale() == 0)
148 error(0, 0, "No locale support");
149
150 f = win_res(e->dpy, RES_CLASS ".font", "monospace-8");
151 win_init_font(e, f);
152
153 bg = win_res(e->dpy, RES_CLASS ".background", "white");
154 fg = win_res(e->dpy, RES_CLASS ".foreground", "black");
155 win_alloc_color(e, bg, &win->bg);
156 win_alloc_color(e, fg, &win->fg);
157
158 win->bar.l.size = BAR_L_LEN;
159 win->bar.r.size = BAR_R_LEN;
160 /* 3 padding bytes needed by utf8_decode */
161 win->bar.l.buf = emalloc(win->bar.l.size + 3);
162 win->bar.l.buf[0] = '\0';
163 win->bar.r.buf = emalloc(win->bar.r.size + 3);
164 win->bar.r.buf[0] = '\0';
165 win->bar.h = options->hide_bar ? 0 : barheight;
166
167 INIT_ATOM_(WM_DELETE_WINDOW);
168 INIT_ATOM_(_NET_WM_NAME);
169 INIT_ATOM_(_NET_WM_ICON_NAME);
170 INIT_ATOM_(_NET_WM_ICON);
171 INIT_ATOM_(_NET_WM_STATE);
172 INIT_ATOM_(_NET_WM_STATE_FULLSCREEN);
173 INIT_ATOM_(_NET_SUPPORTED);
174
175 win_check_wm_support(e->dpy, RootWindow(e->dpy, e->scr));
176}
177
178void win_open(win_t *win)
179{
180 int c, i, j, n;
181 long parent;
182 win_env_t *e;
183 XClassHint classhint;
184 unsigned long *icon_data;
185 XColor col;
186 Cursor *cnone = &cursors[CURSOR_NONE].icon;
187 char none_data[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
188 Pixmap none;
189 int gmask;
190 XSizeHints sizehints;
191 Bool fullscreen = options->fullscreen && fs_support;
192
193 e = &win->env;
194 parent = options->embed != 0 ? options->embed : RootWindow(e->dpy, e->scr);
195
196 sizehints.flags = PWinGravity;
197 sizehints.win_gravity = NorthWestGravity;
198
199 /* determine window offsets, width & height */
200 if (options->geometry == NULL)
201 gmask = 0;
202 else
203 gmask = XParseGeometry(options->geometry, &win->x, &win->y,
204 &win->w, &win->h);
205 if ((gmask & WidthValue) != 0)
206 sizehints.flags |= USSize;
207 else
208 win->w = WIN_WIDTH;
209 if ((gmask & HeightValue) != 0)
210 sizehints.flags |= USSize;
211 else
212 win->h = WIN_HEIGHT;
213 if ((gmask & XValue) != 0) {
214 if ((gmask & XNegative) != 0) {
215 win->x += e->scrw - win->w;
216 sizehints.win_gravity = NorthEastGravity;
217 }
218 sizehints.flags |= USPosition;
219 } else {
220 win->x = 0;
221 }
222 if ((gmask & YValue) != 0) {
223 if ((gmask & YNegative) != 0) {
224 win->y += e->scrh - win->h;
225 sizehints.win_gravity = sizehints.win_gravity == NorthEastGravity
226 ? SouthEastGravity : SouthWestGravity;
227 }
228 sizehints.flags |= USPosition;
229 } else {
230 win->y = 0;
231 }
232
233 win->xwin = XCreateWindow(e->dpy, parent,
234 win->x, win->y, win->w, win->h, 0,
235 e->depth, InputOutput, e->vis, 0, NULL);
236 if (win->xwin == None)
237 error(EXIT_FAILURE, 0, "Error creating X window");
238
239 XSelectInput(e->dpy, win->xwin,
240 ButtonReleaseMask | ButtonPressMask | KeyPressMask |
241 PointerMotionMask | StructureNotifyMask);
242
243 for (i = 0; i < ARRLEN(cursors); i++) {
244 if (i != CURSOR_NONE)
245 cursors[i].icon = XCreateFontCursor(e->dpy, cursors[i].name);
246 }
247 if (XAllocNamedColor(e->dpy, DefaultColormap(e->dpy, e->scr), "black",
248 &col, &col) == 0)
249 {
250 error(EXIT_FAILURE, 0, "Error allocating color 'black'");
251 }
252 none = XCreateBitmapFromData(e->dpy, win->xwin, none_data, 8, 8);
253 *cnone = XCreatePixmapCursor(e->dpy, none, none, &col, &col, 0, 0);
254
255 gc = XCreateGC(e->dpy, win->xwin, 0, None);
256
257 n = icons[ARRLEN(icons)-1].size;
258 icon_data = emalloc((n * n + 2) * sizeof(*icon_data));
259
260 for (i = 0; i < ARRLEN(icons); i++) {
261 n = 0;
262 icon_data[n++] = icons[i].size;
263 icon_data[n++] = icons[i].size;
264
265 for (j = 0; j < icons[i].cnt; j++) {
266 for (c = icons[i].data[j] >> 4; c >= 0; c--)
267 icon_data[n++] = icon_colors[icons[i].data[j] & 0x0F];
268 }
269 XChangeProperty(e->dpy, win->xwin,
270 atoms[ATOM__NET_WM_ICON], XA_CARDINAL, 32,
271 i == 0 ? PropModeReplace : PropModeAppend,
272 (unsigned char *) icon_data, n);
273 }
274 free(icon_data);
275
276 win_set_title(win, "sxiv");
277
278 classhint.res_class = RES_CLASS;
279 classhint.res_name = options->res_name != NULL ? options->res_name : "sxiv";
280 XSetClassHint(e->dpy, win->xwin, &classhint);
281
282 XSetWMProtocols(e->dpy, win->xwin, &atoms[ATOM_WM_DELETE_WINDOW], 1);
283
284 sizehints.width = win->w;
285 sizehints.height = win->h;
286 sizehints.x = win->x;
287 sizehints.y = win->y;
288 XSetWMNormalHints(win->env.dpy, win->xwin, &sizehints);
289
290 win->h -= win->bar.h;
291
292 win->buf.w = e->scrw;
293 win->buf.h = e->scrh;
294 win->buf.pm = XCreatePixmap(e->dpy, win->xwin,
295 win->buf.w, win->buf.h, e->depth);
296 XSetForeground(e->dpy, gc, win->bg.pixel);
297 XFillRectangle(e->dpy, win->buf.pm, gc, 0, 0, win->buf.w, win->buf.h);
298 XSetWindowBackgroundPixmap(e->dpy, win->xwin, win->buf.pm);
299
300 XMapWindow(e->dpy, win->xwin);
301 XFlush(e->dpy);
302
303 if (fullscreen)
304 win_toggle_fullscreen(win);
305}
306
307CLEANUP void win_close(win_t *win)
308{
309 int i;
310
311 for (i = 0; i < ARRLEN(cursors); i++)
312 XFreeCursor(win->env.dpy, cursors[i].icon);
313
314 XFreeGC(win->env.dpy, gc);
315
316 XDestroyWindow(win->env.dpy, win->xwin);
317 XCloseDisplay(win->env.dpy);
318}
319
320bool win_configure(win_t *win, XConfigureEvent *c)
321{
322 bool changed;
323
324 changed = win->w != c->width || win->h + win->bar.h != c->height;
325
326 win->x = c->x;
327 win->y = c->y;
328 win->w = c->width;
329 win->h = c->height - win->bar.h;
330 win->bw = c->border_width;
331
332 return changed;
333}
334
335void win_toggle_fullscreen(win_t *win)
336{
337 XEvent ev;
338 XClientMessageEvent *cm;
339
340 if (!fs_support) {
341 if (!fs_warned) {
342 error(0, 0, "No fullscreen support");
343 fs_warned = True;
344 }
345 return;
346 }
347 win->fullscreen = !win->fullscreen;
348
349 memset(&ev, 0, sizeof(ev));
350 ev.type = ClientMessage;
351
352 cm = &ev.xclient;
353 cm->window = win->xwin;
354 cm->message_type = atoms[ATOM__NET_WM_STATE];
355 cm->format = 32;
356 cm->data.l[0] = win->fullscreen;
357 cm->data.l[1] = atoms[ATOM__NET_WM_STATE_FULLSCREEN];
358 cm->data.l[2] = cm->data.l[3] = 0;
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->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, DefaultVisual(e->dpy, e->scr),
443 DefaultColormap(e->dpy, e->scr));
444
445 XSetForeground(e->dpy, gc, win->fg.pixel);
446 XFillRectangle(e->dpy, win->buf.pm, gc, 0, win->h, win->w, win->bar.h);
447
448 XSetForeground(e->dpy, gc, win->bg.pixel);
449 XSetBackground(e->dpy, gc, win->fg.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->bg, 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->bg, 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 *title)
492{
493 XStoreName(win->env.dpy, win->xwin, title);
494 XSetIconName(win->env.dpy, win->xwin, title);
495
496 XChangeProperty(win->env.dpy, win->xwin, atoms[ATOM__NET_WM_NAME],
497 XInternAtom(win->env.dpy, "UTF8_STRING", False), 8,
498 PropModeReplace, (unsigned char *) title, strlen(title));
499 XChangeProperty(win->env.dpy, win->xwin, atoms[ATOM__NET_WM_ICON_NAME],
500 XInternAtom(win->env.dpy, "UTF8_STRING", False), 8,
501 PropModeReplace, (unsigned char *) title, strlen(title));
502}
503
504void win_set_cursor(win_t *win, cursor_t cursor)
505{
506 if (cursor >= 0 && cursor < ARRLEN(cursors)) {
507 XDefineCursor(win->env.dpy, win->xwin, cursors[cursor].icon);
508 XFlush(win->env.dpy);
509 }
510}
511
512void win_cursor_pos(win_t *win, int *x, int *y)
513{
514 int i;
515 unsigned int ui;
516 Window w;
517
518 if (!XQueryPointer(win->env.dpy, win->xwin, &w, &w, &i, &i, x, y, &ui))
519 *x = *y = 0;
520}
521