Krolyxon
Commit c7ca547

Branches

Refs →

nsxivwindow.c

495 lines · 12.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
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, e->vis,
68 e->cmap, name, col))
69 {
70 error(EXIT_FAILURE, 0, "Error allocating color '%s'", name);
71 }
72}
73
74const char* win_res(XrmDatabase db, const char *name, const char *def)
75{
76 char *type;
77 XrmValue ret;
78
79 if (db != None &&
80 XrmGetResource(db, name, name, &type, &ret) &&
81 STREQ(type, "String"))
82 {
83 return ret.addr;
84 } else {
85 return def;
86 }
87}
88
89#define INIT_ATOM_(atom) \
90 atoms[ATOM_##atom] = XInternAtom(e->dpy, #atom, False);
91
92void win_init(win_t *win)
93{
94 win_env_t *e;
95 const char *bg, *fg, *f;
96 char *res_man;
97 XrmDatabase db;
98 XVisualInfo vis;
99 XWindowAttributes attr;
100 Window parent;
101
102 memset(win, 0, sizeof(win_t));
103
104 e = &win->env;
105 if ((e->dpy = XOpenDisplay(NULL)) == NULL)
106 error(EXIT_FAILURE, 0, "Error opening X display");
107
108 e->scr = DefaultScreen(e->dpy);
109 e->scrw = DisplayWidth(e->dpy, e->scr);
110 e->scrh = DisplayHeight(e->dpy, e->scr);
111
112 parent = options->embed != 0 ? options->embed : RootWindow(e->dpy, e->scr);
113
114 if (options->embed == 0) {
115 e->depth = DefaultDepth(e->dpy, e->scr);
116 } else {
117 XGetWindowAttributes(e->dpy, parent, &attr);
118 e->depth = attr.depth;
119 }
120
121 XMatchVisualInfo(e->dpy, e->scr, e->depth, TrueColor, &vis);
122 e->vis = vis.visual;
123 e->cmap = XCreateColormap(e->dpy, parent, e->vis, None);
124
125 if (setlocale(LC_CTYPE, "") == NULL || XSupportsLocale() == 0)
126 error(0, 0, "No locale support");
127
128 XrmInitialize();
129 res_man = XResourceManagerString(e->dpy);
130 db = res_man != NULL ? XrmGetStringDatabase(res_man) : None;
131
132 f = win_res(db, RES_CLASS ".font", "monospace-8");
133 win_init_font(e, f);
134
135 bg = win_res(db, RES_CLASS ".background", "white");
136 fg = win_res(db, RES_CLASS ".foreground", "black");
137 win_alloc_color(e, bg, &win->bg);
138 win_alloc_color(e, fg, &win->fg);
139
140 win->bar.l.size = BAR_L_LEN;
141 win->bar.r.size = BAR_R_LEN;
142 /* 3 padding bytes needed by utf8_decode */
143 win->bar.l.buf = emalloc(win->bar.l.size + 3);
144 win->bar.l.buf[0] = '\0';
145 win->bar.r.buf = emalloc(win->bar.r.size + 3);
146 win->bar.r.buf[0] = '\0';
147 win->bar.h = options->hide_bar ? 0 : barheight;
148
149 INIT_ATOM_(WM_DELETE_WINDOW);
150 INIT_ATOM_(_NET_WM_NAME);
151 INIT_ATOM_(_NET_WM_ICON_NAME);
152 INIT_ATOM_(_NET_WM_ICON);
153 INIT_ATOM_(_NET_WM_STATE);
154 INIT_ATOM_(_NET_WM_STATE_FULLSCREEN);
155}
156
157void win_open(win_t *win)
158{
159 int c, i, j, n;
160 long parent;
161 win_env_t *e;
162 XClassHint classhint;
163 unsigned long *icon_data;
164 XColor col;
165 Cursor *cnone = &cursors[CURSOR_NONE].icon;
166 char none_data[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
167 Pixmap none;
168 int gmask;
169 XSizeHints sizehints;
170 XWMHints hints;
171
172 e = &win->env;
173 parent = options->embed != 0 ? options->embed : RootWindow(e->dpy, e->scr);
174
175 sizehints.flags = PWinGravity;
176 sizehints.win_gravity = NorthWestGravity;
177
178 /* determine window offsets, width & height */
179 if (options->geometry == NULL)
180 gmask = 0;
181 else
182 gmask = XParseGeometry(options->geometry, &win->x, &win->y,
183 &win->w, &win->h);
184 if ((gmask & WidthValue) != 0)
185 sizehints.flags |= USSize;
186 else
187 win->w = WIN_WIDTH;
188 if ((gmask & HeightValue) != 0)
189 sizehints.flags |= USSize;
190 else
191 win->h = WIN_HEIGHT;
192 if ((gmask & XValue) != 0) {
193 if ((gmask & XNegative) != 0) {
194 win->x += e->scrw - win->w;
195 sizehints.win_gravity = NorthEastGravity;
196 }
197 sizehints.flags |= USPosition;
198 } else {
199 win->x = 0;
200 }
201 if ((gmask & YValue) != 0) {
202 if ((gmask & YNegative) != 0) {
203 win->y += e->scrh - win->h;
204 sizehints.win_gravity = sizehints.win_gravity == NorthEastGravity
205 ? SouthEastGravity : SouthWestGravity;
206 }
207 sizehints.flags |= USPosition;
208 } else {
209 win->y = 0;
210 }
211
212 win->xwin = XCreateWindow(e->dpy, parent,
213 win->x, win->y, win->w, win->h, 0,
214 e->depth, InputOutput, e->vis, 0, NULL);
215 if (win->xwin == None)
216 error(EXIT_FAILURE, 0, "Error creating X window");
217
218 XSelectInput(e->dpy, win->xwin,
219 ButtonReleaseMask | ButtonPressMask | KeyPressMask |
220 PointerMotionMask | StructureNotifyMask);
221
222 for (i = 0; i < ARRLEN(cursors); i++) {
223 if (i != CURSOR_NONE)
224 cursors[i].icon = XCreateFontCursor(e->dpy, cursors[i].name);
225 }
226 if (XAllocNamedColor(e->dpy, e->cmap, "black",
227 &col, &col) == 0)
228 {
229 error(EXIT_FAILURE, 0, "Error allocating color 'black'");
230 }
231 none = XCreateBitmapFromData(e->dpy, win->xwin, none_data, 8, 8);
232 *cnone = XCreatePixmapCursor(e->dpy, none, none, &col, &col, 0, 0);
233
234 gc = XCreateGC(e->dpy, win->xwin, 0, None);
235
236 n = icons[ARRLEN(icons)-1].size;
237 icon_data = emalloc((n * n + 2) * sizeof(*icon_data));
238
239 for (i = 0; i < ARRLEN(icons); i++) {
240 n = 0;
241 icon_data[n++] = icons[i].size;
242 icon_data[n++] = icons[i].size;
243
244 for (j = 0; j < icons[i].cnt; j++) {
245 for (c = icons[i].data[j] >> 4; c >= 0; c--)
246 icon_data[n++] = icon_colors[icons[i].data[j] & 0x0F];
247 }
248 XChangeProperty(e->dpy, win->xwin,
249 atoms[ATOM__NET_WM_ICON], XA_CARDINAL, 32,
250 i == 0 ? PropModeReplace : PropModeAppend,
251 (unsigned char *) icon_data, n);
252 }
253 free(icon_data);
254
255 win_set_title(win, "sxiv");
256
257 classhint.res_class = RES_CLASS;
258 classhint.res_name = options->res_name != NULL ? options->res_name : "sxiv";
259 XSetClassHint(e->dpy, win->xwin, &classhint);
260
261 XSetWMProtocols(e->dpy, win->xwin, &atoms[ATOM_WM_DELETE_WINDOW], 1);
262
263 sizehints.width = win->w;
264 sizehints.height = win->h;
265 sizehints.x = win->x;
266 sizehints.y = win->y;
267 XSetWMNormalHints(win->env.dpy, win->xwin, &sizehints);
268
269 hints.flags = InputHint | StateHint;
270 hints.input = 1;
271 hints.initial_state = NormalState;
272 XSetWMHints(win->env.dpy, win->xwin, &hints);
273
274 win->h -= win->bar.h;
275
276 win->buf.w = e->scrw;
277 win->buf.h = e->scrh;
278 win->buf.pm = XCreatePixmap(e->dpy, win->xwin,
279 win->buf.w, win->buf.h, e->depth);
280 XSetForeground(e->dpy, gc, win->bg.pixel);
281 XFillRectangle(e->dpy, win->buf.pm, gc, 0, 0, win->buf.w, win->buf.h);
282 XSetWindowBackgroundPixmap(e->dpy, win->xwin, win->buf.pm);
283
284 XMapWindow(e->dpy, win->xwin);
285 XFlush(e->dpy);
286
287 if (options->fullscreen)
288 win_toggle_fullscreen(win);
289}
290
291CLEANUP void win_close(win_t *win)
292{
293 int i;
294
295 for (i = 0; i < ARRLEN(cursors); i++)
296 XFreeCursor(win->env.dpy, cursors[i].icon);
297
298 XFreeGC(win->env.dpy, gc);
299
300 XDestroyWindow(win->env.dpy, win->xwin);
301 XCloseDisplay(win->env.dpy);
302}
303
304bool win_configure(win_t *win, XConfigureEvent *c)
305{
306 bool changed;
307
308 changed = win->w != c->width || win->h + win->bar.h != c->height;
309
310 win->x = c->x;
311 win->y = c->y;
312 win->w = c->width;
313 win->h = c->height - win->bar.h;
314 win->bw = c->border_width;
315
316 return changed;
317}
318
319void win_toggle_fullscreen(win_t *win)
320{
321 XEvent ev;
322 XClientMessageEvent *cm;
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] = 2; // toggle
332 cm->data.l[1] = atoms[ATOM__NET_WM_STATE_FULLSCREEN];
333
334 XSendEvent(win->env.dpy, DefaultRootWindow(win->env.dpy), False,
335 SubstructureNotifyMask | SubstructureRedirectMask, &ev);
336}
337
338void win_toggle_bar(win_t *win)
339{
340 if (win->bar.h != 0) {
341 win->h += win->bar.h;
342 win->bar.h = 0;
343 } else {
344 win->bar.h = barheight;
345 win->h -= win->bar.h;
346 }
347}
348
349void win_clear(win_t *win)
350{
351 win_env_t *e;
352
353 e = &win->env;
354
355 if (win->w > win->buf.w || win->h + win->bar.h > win->buf.h) {
356 XFreePixmap(e->dpy, win->buf.pm);
357 win->buf.w = MAX(win->buf.w, win->w);
358 win->buf.h = MAX(win->buf.h, win->h + win->bar.h);
359 win->buf.pm = XCreatePixmap(e->dpy, win->xwin,
360 win->buf.w, win->buf.h, e->depth);
361 }
362 XSetForeground(e->dpy, gc, win->bg.pixel);
363 XFillRectangle(e->dpy, win->buf.pm, gc, 0, 0, win->buf.w, win->buf.h);
364}
365
366#define TEXTWIDTH(win, text, len) \
367 win_draw_text(win, NULL, NULL, 0, 0, text, len, 0)
368
369int win_draw_text(win_t *win, XftDraw *d, const XftColor *color, int x, int y,
370 char *text, int len, int w)
371{
372 int err, tw = 0;
373 char *t, *next;
374 uint32_t rune;
375 XftFont *f;
376 FcCharSet *fccharset;
377 XGlyphInfo ext;
378
379 for (t = text; t - text < len; t = next) {
380 next = utf8_decode(t, &rune, &err);
381 if (XftCharExists(win->env.dpy, font, rune)) {
382 f = font;
383 } else { /* fallback font */
384 fccharset = FcCharSetCreate();
385 FcCharSetAddChar(fccharset, rune);
386 f = XftFontOpen(win->env.dpy, win->env.scr, FC_CHARSET, FcTypeCharSet,
387 fccharset, FC_SCALABLE, FcTypeBool, FcTrue,
388 FC_SIZE, FcTypeDouble, fontsize, 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, e->vis,
417 e->cmap);
418
419 XSetForeground(e->dpy, gc, win->fg.pixel);
420 XFillRectangle(e->dpy, win->buf.pm, gc, 0, win->h, win->w, win->bar.h);
421
422 XSetForeground(e->dpy, gc, win->bg.pixel);
423 XSetBackground(e->dpy, gc, win->fg.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->bg, 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->bg, 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