Krolyxon
Commit 09b04d7

Branches

Refs →

nsxivwindow.c

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