Krolyxon
Commit 55777ba

Branches

Refs →

nsxivwindow.c

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