Krolyxon
Commit ba0d87f

Branches

Refs →

nsxivwindow.c

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