Krolyxon
Commit f7557c5

Branches

Refs →

nsxivwindow.c

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