Krolyxon
Commit e4fb084

Branches

Refs →

nsxivwindow.c

526 lines · 13.7 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 win_res(Display *dpy, const char *rsc, const char **dst)
104{
105 char *type;
106 XrmValue ret;
107 XrmDatabase db;
108 char fullname[256];
109 char *res_man;
110
111 XrmInitialize();
112
113 if ((res_man = XResourceManagerString(dpy)) == NULL)
114 return;
115
116 if ((db = XrmGetStringDatabase(res_man)) == NULL)
117 return;
118
119 snprintf(fullname, sizeof(fullname), ".%s", rsc);
120 fullname[sizeof(fullname) - 1] = '\0';
121
122 XrmGetResource(db, fullname, "String", &type, &ret);
123
124 if (ret.addr != NULL && STREQ(type, "String"))
125 *dst = ret.addr;
126}
127
128#define INIT_ATOM_(atom) \
129 atoms[ATOM_##atom] = XInternAtom(e->dpy, #atom, False);
130
131void win_init(win_t *win)
132{
133 win_env_t *e;
134
135 memset(win, 0, sizeof(win_t));
136
137 e = &win->env;
138 if ((e->dpy = XOpenDisplay(NULL)) == NULL)
139 error(EXIT_FAILURE, 0, "Error opening X display");
140
141 e->scr = DefaultScreen(e->dpy);
142 e->scrw = DisplayWidth(e->dpy, e->scr);
143 e->scrh = DisplayHeight(e->dpy, e->scr);
144 e->vis = DefaultVisual(e->dpy, e->scr);
145 e->cmap = DefaultColormap(e->dpy, e->scr);
146 e->depth = DefaultDepth(e->dpy, e->scr);
147
148 if (setlocale(LC_CTYPE, "") == NULL || XSupportsLocale() == 0)
149 error(0, 0, "No locale support");
150
151 win_res(e->dpy, "background", &WIN_BG_COLOR);
152 win_res(e->dpy, "background", &BAR_FG_COLOR);
153 win_res(e->dpy, "foreground", &BAR_BG_COLOR);
154 win_res(e->dpy, "foreground", &SEL_COLOR);
155
156 win_init_font(e, BAR_FONT);
157
158 win_alloc_color(e, WIN_BG_COLOR, &win->bgcol);
159 win_alloc_color(e, WIN_FS_COLOR, &win->fscol);
160 win_alloc_color(e, SEL_COLOR, &win->selcol);
161 win_alloc_color(e, BAR_BG_COLOR, &win->bar.bgcol);
162 win_alloc_color(e, BAR_FG_COLOR, &win->bar.fgcol);
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 = "Sxiv";
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->fscol.pixel : win->bgcol.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->fscol.pixel : win->bgcol.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, 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, NULL);
420 FcCharSetDestroy(fccharset);
421 }
422 XftTextExtentsUtf8(win->env.dpy, f, (XftChar8*)t, next - t, &ext);
423 tw += ext.xOff;
424 if (tw <= w) {
425 XftDrawStringUtf8(d, color, f, x, y, (XftChar8*)t, next - t);
426 x += ext.xOff;
427 }
428 if (f != font)
429 XftFontClose(win->env.dpy, f);
430 }
431 return tw;
432}
433
434void win_draw_bar(win_t *win)
435{
436 int len, x, y, w, tw;
437 win_env_t *e;
438 win_bar_t *l, *r;
439 XftDraw *d;
440
441 if ((l = &win->bar.l)->buf == NULL || (r = &win->bar.r)->buf == NULL)
442 return;
443
444 e = &win->env;
445 y = win->h + font->ascent + V_TEXT_PAD;
446 w = win->w - 2*H_TEXT_PAD;
447 d = XftDrawCreate(e->dpy, win->buf.pm, DefaultVisual(e->dpy, e->scr),
448 DefaultColormap(e->dpy, e->scr));
449
450 XSetForeground(e->dpy, gc, win->bar.bgcol.pixel);
451 XFillRectangle(e->dpy, win->buf.pm, gc, 0, win->h, win->w, win->bar.h);
452
453 XSetForeground(e->dpy, gc, win->bar.fgcol.pixel);
454 XSetBackground(e->dpy, gc, win->bar.bgcol.pixel);
455
456 if ((len = strlen(r->buf)) > 0) {
457 if ((tw = TEXTWIDTH(win, r->buf, len)) > w)
458 return;
459 x = win->w - tw - H_TEXT_PAD;
460 w -= tw;
461 win_draw_text(win, d, &win->bar.fgcol, x, y, r->buf, len, tw);
462 }
463 if ((len = strlen(l->buf)) > 0) {
464 x = H_TEXT_PAD;
465 w -= 2 * H_TEXT_PAD; /* gap between left and right parts */
466 win_draw_text(win, d, &win->bar.fgcol, x, y, l->buf, len, w);
467 }
468 XftDrawDestroy(d);
469}
470
471void win_draw(win_t *win)
472{
473 if (win->bar.h > 0)
474 win_draw_bar(win);
475
476 XSetWindowBackgroundPixmap(win->env.dpy, win->xwin, win->buf.pm);
477 XClearWindow(win->env.dpy, win->xwin);
478 XFlush(win->env.dpy);
479}
480
481void win_draw_rect(win_t *win, int x, int y, int w, int h, bool fill, int lw,
482 unsigned long col)
483{
484 XGCValues gcval;
485
486 gcval.line_width = lw;
487 gcval.foreground = col;
488 XChangeGC(win->env.dpy, gc, GCForeground | GCLineWidth, &gcval);
489
490 if (fill)
491 XFillRectangle(win->env.dpy, win->buf.pm, gc, x, y, w, h);
492 else
493 XDrawRectangle(win->env.dpy, win->buf.pm, gc, x, y, w, h);
494}
495
496void win_set_title(win_t *win, const char *title)
497{
498 XStoreName(win->env.dpy, win->xwin, title);
499 XSetIconName(win->env.dpy, win->xwin, title);
500
501 XChangeProperty(win->env.dpy, win->xwin, atoms[ATOM__NET_WM_NAME],
502 XInternAtom(win->env.dpy, "UTF8_STRING", False), 8,
503 PropModeReplace, (unsigned char *) title, strlen(title));
504 XChangeProperty(win->env.dpy, win->xwin, atoms[ATOM__NET_WM_ICON_NAME],
505 XInternAtom(win->env.dpy, "UTF8_STRING", False), 8,
506 PropModeReplace, (unsigned char *) title, strlen(title));
507}
508
509void win_set_cursor(win_t *win, cursor_t cursor)
510{
511 if (cursor >= 0 && cursor < ARRLEN(cursors)) {
512 XDefineCursor(win->env.dpy, win->xwin, cursors[cursor].icon);
513 XFlush(win->env.dpy);
514 }
515}
516
517void win_cursor_pos(win_t *win, int *x, int *y)
518{
519 int i;
520 unsigned int ui;
521 Window w;
522
523 if (!XQueryPointer(win->env.dpy, win->xwin, &w, &w, &i, &i, x, y, &ui))
524 *x = *y = 0;
525}
526