Krolyxon
Commit 9d244da

Branches

Refs →

nsxivwindow.c

521 lines · 13.6 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
124#define INIT_ATOM_(atom) \
125 atoms[ATOM_##atom] = XInternAtom(e->dpy, #atom, False);
126
127void win_init(win_t *win)
128{
129 win_env_t *e;
130
131 memset(win, 0, sizeof(win_t));
132
133 e = &win->env;
134 if ((e->dpy = XOpenDisplay(NULL)) == NULL)
135 error(EXIT_FAILURE, 0, "Error opening X display");
136
137 e->scr = DefaultScreen(e->dpy);
138 e->scrw = DisplayWidth(e->dpy, e->scr);
139 e->scrh = DisplayHeight(e->dpy, e->scr);
140 e->vis = DefaultVisual(e->dpy, e->scr);
141 e->cmap = DefaultColormap(e->dpy, e->scr);
142 e->depth = DefaultDepth(e->dpy, e->scr);
143
144 if (setlocale(LC_CTYPE, "") == NULL || XSupportsLocale() == 0)
145 error(0, 0, "No locale support");
146
147 win_init_font(e, BAR_FONT);
148
149 win_alloc_color(e, win_res(e->dpy, RES_CLASS ".background", WIN_BG_COLOR),
150 &win->bgcol);
151 win_alloc_color(e, WIN_FS_COLOR, &win->fscol);
152 win_alloc_color(e, win_res(e->dpy, RES_CLASS ".foreground", SEL_COLOR),
153 &win->selcol);
154 win_alloc_color(e, win_res(e->dpy, RES_CLASS ".foreground", BAR_BG_COLOR),
155 &win->bar.bgcol);
156 win_alloc_color(e, win_res(e->dpy, RES_CLASS ".background", BAR_FG_COLOR),
157 &win->bar.fgcol);
158
159 win->bar.l.size = BAR_L_LEN;
160 win->bar.r.size = BAR_R_LEN;
161 /* 3 padding bytes needed by utf8_decode */
162 win->bar.l.buf = emalloc(win->bar.l.size + 3);
163 win->bar.l.buf[0] = '\0';
164 win->bar.r.buf = emalloc(win->bar.r.size + 3);
165 win->bar.r.buf[0] = '\0';
166 win->bar.h = options->hide_bar ? 0 : barheight;
167
168 INIT_ATOM_(WM_DELETE_WINDOW);
169 INIT_ATOM_(_NET_WM_NAME);
170 INIT_ATOM_(_NET_WM_ICON_NAME);
171 INIT_ATOM_(_NET_WM_ICON);
172 INIT_ATOM_(_NET_WM_STATE);
173 INIT_ATOM_(_NET_WM_STATE_FULLSCREEN);
174 INIT_ATOM_(_NET_SUPPORTED);
175
176 win_check_wm_support(e->dpy, RootWindow(e->dpy, e->scr));
177}
178
179void win_open(win_t *win)
180{
181 int c, i, j, n;
182 long parent;
183 win_env_t *e;
184 XClassHint classhint;
185 unsigned long *icon_data;
186 XColor col;
187 Cursor *cnone = &cursors[CURSOR_NONE].icon;
188 char none_data[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
189 Pixmap none;
190 int gmask;
191 XSizeHints sizehints;
192 Bool fullscreen = options->fullscreen && fs_support;
193
194 e = &win->env;
195 parent = options->embed != 0 ? options->embed : RootWindow(e->dpy, e->scr);
196
197 sizehints.flags = PWinGravity;
198 sizehints.win_gravity = NorthWestGravity;
199
200 /* determine window offsets, width & height */
201 if (options->geometry == NULL)
202 gmask = 0;
203 else
204 gmask = XParseGeometry(options->geometry, &win->x, &win->y,
205 &win->w, &win->h);
206 if ((gmask & WidthValue) != 0)
207 sizehints.flags |= USSize;
208 else
209 win->w = WIN_WIDTH;
210 if ((gmask & HeightValue) != 0)
211 sizehints.flags |= USSize;
212 else
213 win->h = WIN_HEIGHT;
214 if ((gmask & XValue) != 0) {
215 if ((gmask & XNegative) != 0) {
216 win->x += e->scrw - win->w;
217 sizehints.win_gravity = NorthEastGravity;
218 }
219 sizehints.flags |= USPosition;
220 } else {
221 win->x = 0;
222 }
223 if ((gmask & YValue) != 0) {
224 if ((gmask & YNegative) != 0) {
225 win->y += e->scrh - win->h;
226 sizehints.win_gravity = sizehints.win_gravity == NorthEastGravity
227 ? SouthEastGravity : SouthWestGravity;
228 }
229 sizehints.flags |= USPosition;
230 } else {
231 win->y = 0;
232 }
233
234 win->xwin = XCreateWindow(e->dpy, parent,
235 win->x, win->y, win->w, win->h, 0,
236 e->depth, InputOutput, e->vis, 0, NULL);
237 if (win->xwin == None)
238 error(EXIT_FAILURE, 0, "Error creating X window");
239
240 XSelectInput(e->dpy, win->xwin,
241 ButtonReleaseMask | ButtonPressMask | KeyPressMask |
242 PointerMotionMask | StructureNotifyMask);
243
244 for (i = 0; i < ARRLEN(cursors); i++) {
245 if (i != CURSOR_NONE)
246 cursors[i].icon = XCreateFontCursor(e->dpy, cursors[i].name);
247 }
248 if (XAllocNamedColor(e->dpy, DefaultColormap(e->dpy, e->scr), "black",
249 &col, &col) == 0)
250 {
251 error(EXIT_FAILURE, 0, "Error allocating color 'black'");
252 }
253 none = XCreateBitmapFromData(e->dpy, win->xwin, none_data, 8, 8);
254 *cnone = XCreatePixmapCursor(e->dpy, none, none, &col, &col, 0, 0);
255
256 gc = XCreateGC(e->dpy, win->xwin, 0, None);
257
258 n = icons[ARRLEN(icons)-1].size;
259 icon_data = emalloc((n * n + 2) * sizeof(*icon_data));
260
261 for (i = 0; i < ARRLEN(icons); i++) {
262 n = 0;
263 icon_data[n++] = icons[i].size;
264 icon_data[n++] = icons[i].size;
265
266 for (j = 0; j < icons[i].cnt; j++) {
267 for (c = icons[i].data[j] >> 4; c >= 0; c--)
268 icon_data[n++] = icon_colors[icons[i].data[j] & 0x0F];
269 }
270 XChangeProperty(e->dpy, win->xwin,
271 atoms[ATOM__NET_WM_ICON], XA_CARDINAL, 32,
272 i == 0 ? PropModeReplace : PropModeAppend,
273 (unsigned char *) icon_data, n);
274 }
275 free(icon_data);
276
277 win_set_title(win, "sxiv");
278
279 classhint.res_class = RES_CLASS;
280 classhint.res_name = options->res_name != NULL ? options->res_name : "sxiv";
281 XSetClassHint(e->dpy, win->xwin, &classhint);
282
283 XSetWMProtocols(e->dpy, win->xwin, &atoms[ATOM_WM_DELETE_WINDOW], 1);
284
285 sizehints.width = win->w;
286 sizehints.height = win->h;
287 sizehints.x = win->x;
288 sizehints.y = win->y;
289 XSetWMNormalHints(win->env.dpy, win->xwin, &sizehints);
290
291 win->h -= win->bar.h;
292
293 win->buf.w = e->scrw;
294 win->buf.h = e->scrh;
295 win->buf.pm = XCreatePixmap(e->dpy, win->xwin,
296 win->buf.w, win->buf.h, e->depth);
297 XSetForeground(e->dpy, gc, fullscreen ? win->fscol.pixel : win->bgcol.pixel);
298 XFillRectangle(e->dpy, win->buf.pm, gc, 0, 0, win->buf.w, win->buf.h);
299 XSetWindowBackgroundPixmap(e->dpy, win->xwin, win->buf.pm);
300
301 XMapWindow(e->dpy, win->xwin);
302 XFlush(e->dpy);
303
304 if (fullscreen)
305 win_toggle_fullscreen(win);
306}
307
308CLEANUP void win_close(win_t *win)
309{
310 int i;
311
312 for (i = 0; i < ARRLEN(cursors); i++)
313 XFreeCursor(win->env.dpy, cursors[i].icon);
314
315 XFreeGC(win->env.dpy, gc);
316
317 XDestroyWindow(win->env.dpy, win->xwin);
318 XCloseDisplay(win->env.dpy);
319}
320
321bool win_configure(win_t *win, XConfigureEvent *c)
322{
323 bool changed;
324
325 changed = win->w != c->width || win->h + win->bar.h != c->height;
326
327 win->x = c->x;
328 win->y = c->y;
329 win->w = c->width;
330 win->h = c->height - win->bar.h;
331 win->bw = c->border_width;
332
333 return changed;
334}
335
336void win_toggle_fullscreen(win_t *win)
337{
338 XEvent ev;
339 XClientMessageEvent *cm;
340
341 if (!fs_support) {
342 if (!fs_warned) {
343 error(0, 0, "No fullscreen support");
344 fs_warned = True;
345 }
346 return;
347 }
348 win->fullscreen = !win->fullscreen;
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] = win->fullscreen;
358 cm->data.l[1] = atoms[ATOM__NET_WM_STATE_FULLSCREEN];
359 cm->data.l[2] = cm->data.l[3] = 0;
360
361 XSendEvent(win->env.dpy, DefaultRootWindow(win->env.dpy), False,
362 SubstructureNotifyMask | SubstructureRedirectMask, &ev);
363}
364
365void win_toggle_bar(win_t *win)
366{
367 if (win->bar.h != 0) {
368 win->h += win->bar.h;
369 win->bar.h = 0;
370 } else {
371 win->bar.h = barheight;
372 win->h -= win->bar.h;
373 }
374}
375
376void win_clear(win_t *win)
377{
378 win_env_t *e;
379
380 e = &win->env;
381
382 if (win->w > win->buf.w || win->h + win->bar.h > win->buf.h) {
383 XFreePixmap(e->dpy, win->buf.pm);
384 win->buf.w = MAX(win->buf.w, win->w);
385 win->buf.h = MAX(win->buf.h, win->h + win->bar.h);
386 win->buf.pm = XCreatePixmap(e->dpy, win->xwin,
387 win->buf.w, win->buf.h, e->depth);
388 }
389 XSetForeground(e->dpy, gc, win->fullscreen ? win->fscol.pixel : win->bgcol.pixel);
390 XFillRectangle(e->dpy, win->buf.pm, gc, 0, 0, win->buf.w, win->buf.h);
391}
392
393#define TEXTWIDTH(win, text, len) \
394 win_draw_text(win, NULL, NULL, 0, 0, text, len, 0)
395
396int win_draw_text(win_t *win, XftDraw *d, XftColor *color, int x, int y,
397 char *text, int len, int w)
398{
399 int err, tw = 0;
400 char *t, *next;
401 uint32_t rune;
402 XftFont *f;
403 FcCharSet *fccharset;
404 XGlyphInfo ext;
405
406 for (t = text; t - text < len; t = next) {
407 next = utf8_decode(t, &rune, &err);
408 if (XftCharExists(win->env.dpy, font, rune)) {
409 f = font;
410 } else { /* fallback font */
411 fccharset = FcCharSetCreate();
412 FcCharSetAddChar(fccharset, rune);
413 f = XftFontOpen(win->env.dpy, win->env.scr, FC_CHARSET, FcTypeCharSet,
414 fccharset, FC_SCALABLE, FcTypeBool, FcTrue, 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->bar.bgcol.pixel);
446 XFillRectangle(e->dpy, win->buf.pm, gc, 0, win->h, win->w, win->bar.h);
447
448 XSetForeground(e->dpy, gc, win->bar.fgcol.pixel);
449 XSetBackground(e->dpy, gc, win->bar.bgcol.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.fgcol, 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.fgcol, 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