Krolyxon
Commit 4853e17

Branches

Refs →

nsxivwindow.c

530 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
32enum {
33 H_TEXT_PAD = 5,
34 V_TEXT_PAD = 1
35};
36
37static struct {
38 int name;
39 Cursor icon;
40} cursors[CURSOR_COUNT] = {
41 { XC_left_ptr }, { XC_dotbox }, { XC_watch },
42 { XC_sb_left_arrow }, { XC_sb_right_arrow }
43};
44
45static GC gc;
46
47static XftFont *font;
48static int fontheight;
49static int barheight;
50
51Atom atoms[ATOM_COUNT];
52
53static Bool fs_support;
54static Bool fs_warned;
55
56void win_init_font(const win_env_t *e, const char *fontstr)
57{
58 if ((font = XftFontOpenName(e->dpy, e->scr, fontstr)) == NULL)
59 error(EXIT_FAILURE, 0, "Error loading font '%s'", fontstr);
60 fontheight = font->ascent + font->descent;
61 barheight = fontheight + 2 * V_TEXT_PAD;
62}
63
64void win_alloc_color(const win_env_t *e, const char *name, XftColor *col)
65{
66 if (!XftColorAllocName(e->dpy, DefaultVisual(e->dpy, e->scr),
67 DefaultColormap(e->dpy, e->scr), name, col))
68 {
69 error(EXIT_FAILURE, 0, "Error allocating color '%s'", name);
70 }
71}
72
73void win_check_wm_support(Display *dpy, Window root)
74{
75 int format;
76 long offset = 0, length = 16;
77 Atom *data, type;
78 unsigned long i, nitems, bytes_left;
79 Bool found = False;
80
81 while (!found && length > 0) {
82 if (XGetWindowProperty(dpy, root, atoms[ATOM__NET_SUPPORTED],
83 offset, length, False, XA_ATOM, &type, &format,
84 &nitems, &bytes_left, (unsigned char**) &data))
85 {
86 break;
87 }
88 if (type == XA_ATOM && format == 32) {
89 for (i = 0; i < nitems; i++) {
90 if (data[i] == atoms[ATOM__NET_WM_STATE_FULLSCREEN]) {
91 found = True;
92 fs_support = True;
93 break;
94 }
95 }
96 }
97 XFree(data);
98 offset += nitems;
99 length = MIN(length, bytes_left / 4);
100 }
101}
102
103void get_xresource(Display *dpy, const char* rsc, const void* dst)
104{
105 char *type;
106 XrmValue ret;
107 XrmDatabase db;
108 char fullname[256];
109 char *resource_manager;
110
111 XrmInitialize();
112 resource_manager = XResourceManagerString(dpy);
113
114 if (resource_manager == NULL)
115 return;
116
117 db = XrmGetStringDatabase(resource_manager);
118
119 if (db == NULL)
120 return;
121
122 snprintf(fullname, sizeof(fullname), ".%s", rsc);
123 fullname[sizeof(fullname) - 1] = '\0';
124
125 XrmGetResource(db, fullname, "String", &type, &ret);
126
127 if (ret.addr != NULL || !strncmp("String", type, 64)) {
128 *( (char **) dst ) = ret.addr;
129 }
130}
131
132#define INIT_ATOM_(atom) \
133 atoms[ATOM_##atom] = XInternAtom(e->dpy, #atom, False);
134
135void win_init(win_t *win)
136{
137 win_env_t *e;
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 get_xresource(e->dpy, "background", &WIN_BG_COLOR);
156 get_xresource(e->dpy, "background", &BAR_FG_COLOR);
157 get_xresource(e->dpy, "foreground", &BAR_BG_COLOR);
158 get_xresource(e->dpy, "foreground", &SEL_COLOR);
159
160 win_init_font(e, BAR_FONT);
161
162 win_alloc_color(e, WIN_BG_COLOR, &win->bgcol);
163 win_alloc_color(e, WIN_FS_COLOR, &win->fscol);
164 win_alloc_color(e, SEL_COLOR, &win->selcol);
165 win_alloc_color(e, BAR_BG_COLOR, &win->bar.bgcol);
166 win_alloc_color(e, BAR_FG_COLOR, &win->bar.fgcol);
167
168 win->bar.l.size = BAR_L_LEN;
169 win->bar.r.size = BAR_R_LEN;
170 /* 3 padding bytes needed by utf8_decode */
171 win->bar.l.buf = emalloc(win->bar.l.size + 3);
172 win->bar.l.buf[0] = '\0';
173 win->bar.r.buf = emalloc(win->bar.r.size + 3);
174 win->bar.r.buf[0] = '\0';
175 win->bar.h = options->hide_bar ? 0 : barheight;
176
177 INIT_ATOM_(WM_DELETE_WINDOW);
178 INIT_ATOM_(_NET_WM_NAME);
179 INIT_ATOM_(_NET_WM_ICON_NAME);
180 INIT_ATOM_(_NET_WM_ICON);
181 INIT_ATOM_(_NET_WM_STATE);
182 INIT_ATOM_(_NET_WM_STATE_FULLSCREEN);
183 INIT_ATOM_(_NET_SUPPORTED);
184
185 win_check_wm_support(e->dpy, RootWindow(e->dpy, e->scr));
186}
187
188void win_open(win_t *win)
189{
190 int c, i, j, n;
191 long parent;
192 win_env_t *e;
193 XClassHint classhint;
194 unsigned long *icon_data;
195 XColor col;
196 Cursor *cnone = &cursors[CURSOR_NONE].icon;
197 char none_data[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
198 Pixmap none;
199 int gmask;
200 XSizeHints sizehints;
201 Bool fullscreen = options->fullscreen && fs_support;
202
203 e = &win->env;
204 parent = options->embed != 0 ? options->embed : RootWindow(e->dpy, e->scr);
205
206 sizehints.flags = PWinGravity;
207 sizehints.win_gravity = NorthWestGravity;
208
209 /* determine window offsets, width & height */
210 if (options->geometry == NULL)
211 gmask = 0;
212 else
213 gmask = XParseGeometry(options->geometry, &win->x, &win->y,
214 &win->w, &win->h);
215 if ((gmask & WidthValue) != 0)
216 sizehints.flags |= USSize;
217 else
218 win->w = WIN_WIDTH;
219 if ((gmask & HeightValue) != 0)
220 sizehints.flags |= USSize;
221 else
222 win->h = WIN_HEIGHT;
223 if ((gmask & XValue) != 0) {
224 if ((gmask & XNegative) != 0) {
225 win->x += e->scrw - win->w;
226 sizehints.win_gravity = NorthEastGravity;
227 }
228 sizehints.flags |= USPosition;
229 } else {
230 win->x = 0;
231 }
232 if ((gmask & YValue) != 0) {
233 if ((gmask & YNegative) != 0) {
234 win->y += e->scrh - win->h;
235 sizehints.win_gravity = sizehints.win_gravity == NorthEastGravity
236 ? SouthEastGravity : SouthWestGravity;
237 }
238 sizehints.flags |= USPosition;
239 } else {
240 win->y = 0;
241 }
242
243 win->xwin = XCreateWindow(e->dpy, parent,
244 win->x, win->y, win->w, win->h, 0,
245 e->depth, InputOutput, e->vis, 0, NULL);
246 if (win->xwin == None)
247 error(EXIT_FAILURE, 0, "Error creating X window");
248
249 XSelectInput(e->dpy, win->xwin,
250 ButtonReleaseMask | ButtonPressMask | KeyPressMask |
251 PointerMotionMask | StructureNotifyMask);
252
253 for (i = 0; i < ARRLEN(cursors); i++) {
254 if (i != CURSOR_NONE)
255 cursors[i].icon = XCreateFontCursor(e->dpy, cursors[i].name);
256 }
257 if (XAllocNamedColor(e->dpy, DefaultColormap(e->dpy, e->scr), "black",
258 &col, &col) == 0)
259 {
260 error(EXIT_FAILURE, 0, "Error allocating color 'black'");
261 }
262 none = XCreateBitmapFromData(e->dpy, win->xwin, none_data, 8, 8);
263 *cnone = XCreatePixmapCursor(e->dpy, none, none, &col, &col, 0, 0);
264
265 gc = XCreateGC(e->dpy, win->xwin, 0, None);
266
267 n = icons[ARRLEN(icons)-1].size;
268 icon_data = emalloc((n * n + 2) * sizeof(*icon_data));
269
270 for (i = 0; i < ARRLEN(icons); i++) {
271 n = 0;
272 icon_data[n++] = icons[i].size;
273 icon_data[n++] = icons[i].size;
274
275 for (j = 0; j < icons[i].cnt; j++) {
276 for (c = icons[i].data[j] >> 4; c >= 0; c--)
277 icon_data[n++] = icon_colors[icons[i].data[j] & 0x0F];
278 }
279 XChangeProperty(e->dpy, win->xwin,
280 atoms[ATOM__NET_WM_ICON], XA_CARDINAL, 32,
281 i == 0 ? PropModeReplace : PropModeAppend,
282 (unsigned char *) icon_data, n);
283 }
284 free(icon_data);
285
286 win_set_title(win, "sxiv");
287
288 classhint.res_class = "Sxiv";
289 classhint.res_name = options->res_name != NULL ? options->res_name : "sxiv";
290 XSetClassHint(e->dpy, win->xwin, &classhint);
291
292 XSetWMProtocols(e->dpy, win->xwin, &atoms[ATOM_WM_DELETE_WINDOW], 1);
293
294 sizehints.width = win->w;
295 sizehints.height = win->h;
296 sizehints.x = win->x;
297 sizehints.y = win->y;
298 XSetWMNormalHints(win->env.dpy, win->xwin, &sizehints);
299
300 win->h -= win->bar.h;
301
302 win->buf.w = e->scrw;
303 win->buf.h = e->scrh;
304 win->buf.pm = XCreatePixmap(e->dpy, win->xwin,
305 win->buf.w, win->buf.h, e->depth);
306 XSetForeground(e->dpy, gc, fullscreen ? win->fscol.pixel : win->bgcol.pixel);
307 XFillRectangle(e->dpy, win->buf.pm, gc, 0, 0, win->buf.w, win->buf.h);
308 XSetWindowBackgroundPixmap(e->dpy, win->xwin, win->buf.pm);
309
310 XMapWindow(e->dpy, win->xwin);
311 XFlush(e->dpy);
312
313 if (fullscreen)
314 win_toggle_fullscreen(win);
315}
316
317CLEANUP void win_close(win_t *win)
318{
319 int i;
320
321 for (i = 0; i < ARRLEN(cursors); i++)
322 XFreeCursor(win->env.dpy, cursors[i].icon);
323
324 XFreeGC(win->env.dpy, gc);
325
326 XDestroyWindow(win->env.dpy, win->xwin);
327 XCloseDisplay(win->env.dpy);
328}
329
330bool win_configure(win_t *win, XConfigureEvent *c)
331{
332 bool changed;
333
334 changed = win->w != c->width || win->h + win->bar.h != c->height;
335
336 win->x = c->x;
337 win->y = c->y;
338 win->w = c->width;
339 win->h = c->height - win->bar.h;
340 win->bw = c->border_width;
341
342 return changed;
343}
344
345void win_toggle_fullscreen(win_t *win)
346{
347 XEvent ev;
348 XClientMessageEvent *cm;
349
350 if (!fs_support) {
351 if (!fs_warned) {
352 error(0, 0, "No fullscreen support");
353 fs_warned = True;
354 }
355 return;
356 }
357 win->fullscreen = !win->fullscreen;
358
359 memset(&ev, 0, sizeof(ev));
360 ev.type = ClientMessage;
361
362 cm = &ev.xclient;
363 cm->window = win->xwin;
364 cm->message_type = atoms[ATOM__NET_WM_STATE];
365 cm->format = 32;
366 cm->data.l[0] = win->fullscreen;
367 cm->data.l[1] = atoms[ATOM__NET_WM_STATE_FULLSCREEN];
368 cm->data.l[2] = cm->data.l[3] = 0;
369
370 XSendEvent(win->env.dpy, DefaultRootWindow(win->env.dpy), False,
371 SubstructureNotifyMask | SubstructureRedirectMask, &ev);
372}
373
374void win_toggle_bar(win_t *win)
375{
376 if (win->bar.h != 0) {
377 win->h += win->bar.h;
378 win->bar.h = 0;
379 } else {
380 win->bar.h = barheight;
381 win->h -= win->bar.h;
382 }
383}
384
385void win_clear(win_t *win)
386{
387 win_env_t *e;
388
389 e = &win->env;
390
391 if (win->w > win->buf.w || win->h + win->bar.h > win->buf.h) {
392 XFreePixmap(e->dpy, win->buf.pm);
393 win->buf.w = MAX(win->buf.w, win->w);
394 win->buf.h = MAX(win->buf.h, win->h + win->bar.h);
395 win->buf.pm = XCreatePixmap(e->dpy, win->xwin,
396 win->buf.w, win->buf.h, e->depth);
397 }
398 XSetForeground(e->dpy, gc, win->fullscreen ? win->fscol.pixel : win->bgcol.pixel);
399 XFillRectangle(e->dpy, win->buf.pm, gc, 0, 0, win->buf.w, win->buf.h);
400}
401
402#define TEXTWIDTH(win, text, len) \
403 win_draw_text(win, NULL, NULL, 0, 0, text, len, 0)
404
405int win_draw_text(win_t *win, XftDraw *d, XftColor *color, int x, int y,
406 char *text, int len, int w)
407{
408 int err, tw = 0;
409 char *t, *next;
410 uint32_t rune;
411 XftFont *f;
412 FcCharSet *fccharset;
413 XGlyphInfo ext;
414
415 for (t = text; t - text < len; t = next) {
416 next = utf8_decode(t, &rune, &err);
417 if (XftCharExists(win->env.dpy, font, rune)) {
418 f = font;
419 } else { /* fallback font */
420 fccharset = FcCharSetCreate();
421 FcCharSetAddChar(fccharset, rune);
422 f = XftFontOpen(win->env.dpy, win->env.scr, FC_CHARSET, FcTypeCharSet,
423 fccharset, FC_SCALABLE, FcTypeBool, FcTrue, NULL);
424 FcCharSetDestroy(fccharset);
425 }
426 XftTextExtentsUtf8(win->env.dpy, f, (XftChar8*)t, next - t, &ext);
427 tw += ext.xOff;
428 if (tw <= w) {
429 XftDrawStringUtf8(d, color, f, x, y, (XftChar8*)t, next - t);
430 x += ext.xOff;
431 }
432 if (f != font)
433 XftFontClose(win->env.dpy, f);
434 }
435 return tw;
436}
437
438void win_draw_bar(win_t *win)
439{
440 int len, x, y, w, tw;
441 win_env_t *e;
442 win_bar_t *l, *r;
443 XftDraw *d;
444
445 if ((l = &win->bar.l)->buf == NULL || (r = &win->bar.r)->buf == NULL)
446 return;
447
448 e = &win->env;
449 y = win->h + font->ascent + V_TEXT_PAD;
450 w = win->w - 2*H_TEXT_PAD;
451 d = XftDrawCreate(e->dpy, win->buf.pm, DefaultVisual(e->dpy, e->scr),
452 DefaultColormap(e->dpy, e->scr));
453
454 XSetForeground(e->dpy, gc, win->bar.bgcol.pixel);
455 XFillRectangle(e->dpy, win->buf.pm, gc, 0, win->h, win->w, win->bar.h);
456
457 XSetForeground(e->dpy, gc, win->bar.fgcol.pixel);
458 XSetBackground(e->dpy, gc, win->bar.bgcol.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, &win->bar.fgcol, 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, &win->bar.fgcol, 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