Krolyxon
Commit 6a662ae

Branches

Refs →

nsxivwindow.c

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