Krolyxon
Commit cd02f2d

Branches

Refs →

nsxivwindow.c

570 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#define _POSIX_C_SOURCE 200112L
20#define _WINDOW_CONFIG
21
22#include <stdlib.h>
23#include <string.h>
24#include <locale.h>
25#include <X11/cursorfont.h>
26#include <X11/Xatom.h>
27
28#include "options.h"
29#include "util.h"
30#include "window.h"
31#include "config.h"
32#include "icon/data.h"
33
34enum {
35 H_TEXT_PAD = 5,
36 V_TEXT_PAD = 1
37};
38
39static Cursor carrow;
40static Cursor cnone;
41static Cursor chand;
42static Cursor cwatch;
43static GC gc;
44
45static struct {
46 int ascent;
47 int descent;
48 XFontStruct *xfont;
49 XFontSet set;
50} font;
51
52static int fontheight;
53static int barheight;
54
55Atom atoms[ATOM_COUNT];
56
57static Bool fs_support;
58static Bool fs_warned;
59
60void win_init_font(Display *dpy, const char *fontstr)
61{
62 int n;
63 char *def, **missing;
64
65 font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
66 if (missing)
67 XFreeStringList(missing);
68 if (font.set) {
69 XFontStruct **xfonts;
70 char **font_names;
71
72 font.ascent = font.descent = 0;
73 XExtentsOfFontSet(font.set);
74 n = XFontsOfFontSet(font.set, &xfonts, &font_names);
75 while (n--) {
76 font.ascent = MAX(font.ascent, (*xfonts)->ascent);
77 font.descent = MAX(font.descent,(*xfonts)->descent);
78 xfonts++;
79 }
80 } else {
81 if ((font.xfont = XLoadQueryFont(dpy, fontstr)) == NULL &&
82 (font.xfont = XLoadQueryFont(dpy, "fixed")) == NULL)
83 {
84 die("could not load font: %s", fontstr);
85 }
86 font.ascent = font.xfont->ascent;
87 font.descent = font.xfont->descent;
88 }
89 fontheight = font.ascent + font.descent;
90 barheight = fontheight + 2 * V_TEXT_PAD;
91}
92
93unsigned long win_alloc_color(win_t *win, const char *name)
94{
95 XColor col;
96
97 if (win == NULL)
98 return 0UL;
99 if (XAllocNamedColor(win->env.dpy,
100 DefaultColormap(win->env.dpy, win->env.scr),
101 name, &col, &col) == 0)
102 {
103 die("could not allocate color: %s", name);
104 }
105 return col.pixel;
106}
107
108void win_check_wm_support(Display *dpy, Window root)
109{
110 int format;
111 long offset = 0, length = 16;
112 Atom *data, type;
113 unsigned long i, nitems, bytes_left;
114 Bool found = False;
115
116 while (!found && length > 0) {
117 if (XGetWindowProperty(dpy, root, atoms[ATOM__NET_SUPPORTED],
118 offset, length, False, XA_ATOM, &type, &format,
119 &nitems, &bytes_left, (unsigned char**) &data))
120 {
121 break;
122 }
123 if (type == XA_ATOM && format == 32) {
124 for (i = 0; i < nitems; i++) {
125 if (data[i] == atoms[ATOM__NET_WM_STATE_FULLSCREEN]) {
126 found = True;
127 fs_support = True;
128 break;
129 }
130 }
131 }
132 XFree(data);
133 offset += nitems;
134 length = MIN(length, bytes_left / 4);
135 }
136}
137
138#define INIT_ATOM_(atom) \
139 atoms[ATOM_##atom] = XInternAtom(e->dpy, #atom, False);
140
141void win_init(win_t *win)
142{
143 win_env_t *e;
144
145 if (win == NULL)
146 return;
147
148 memset(win, 0, sizeof(win_t));
149
150 e = &win->env;
151 if ((e->dpy = XOpenDisplay(NULL)) == NULL)
152 die("could not open display");
153
154 e->scr = DefaultScreen(e->dpy);
155 e->scrw = DisplayWidth(e->dpy, e->scr);
156 e->scrh = DisplayHeight(e->dpy, e->scr);
157 e->vis = DefaultVisual(e->dpy, e->scr);
158 e->cmap = DefaultColormap(e->dpy, e->scr);
159 e->depth = DefaultDepth(e->dpy, e->scr);
160
161 if (setlocale(LC_CTYPE, "") == NULL || XSupportsLocale() == 0)
162 warn("no locale support");
163
164 win_init_font(e->dpy, BAR_FONT);
165
166 win->bgcol = win_alloc_color(win, WIN_BG_COLOR);
167 win->fscol = win_alloc_color(win, WIN_FS_COLOR);
168 win->selcol = win_alloc_color(win, SEL_COLOR);
169 win->bar.bgcol = win_alloc_color(win, BAR_BG_COLOR);
170 win->bar.fgcol = win_alloc_color(win, BAR_FG_COLOR);
171 win->bar.h = options->hide_bar ? 0 : barheight;
172
173 INIT_ATOM_(WM_DELETE_WINDOW);
174 INIT_ATOM_(_NET_WM_NAME);
175 INIT_ATOM_(_NET_WM_ICON_NAME);
176 INIT_ATOM_(_NET_WM_ICON);
177 INIT_ATOM_(_NET_WM_STATE);
178 INIT_ATOM_(_NET_WM_STATE_FULLSCREEN);
179 INIT_ATOM_(_NET_SUPPORTED);
180
181 win_check_wm_support(e->dpy, RootWindow(e->dpy, e->scr));
182}
183
184void win_open(win_t *win)
185{
186 int c, i, j, n;
187 win_env_t *e;
188 XClassHint classhint;
189 XSetWindowAttributes attr;
190 unsigned long attr_mask;
191 unsigned long *icon_data;
192 XColor col;
193 char none_data[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
194 Pixmap none;
195 int gmask;
196 XSizeHints sizehints;
197
198 if (win == NULL)
199 return;
200
201 e = &win->env;
202
203 sizehints.flags = PWinGravity;
204 sizehints.win_gravity = NorthWestGravity;
205
206 /* determine window offsets, width & height */
207 if (options->geometry == NULL)
208 gmask = 0;
209 else
210 gmask = XParseGeometry(options->geometry, &win->x, &win->y,
211 &win->w, &win->h);
212 if ((gmask & WidthValue) != 0)
213 sizehints.flags |= USSize;
214 else
215 win->w = WIN_WIDTH;
216 if ((gmask & HeightValue) != 0)
217 sizehints.flags |= USSize;
218 else
219 win->h = WIN_HEIGHT;
220 if ((gmask & XValue) != 0) {
221 if ((gmask & XNegative) != 0) {
222 win->x += e->scrw - win->w;
223 sizehints.win_gravity = NorthEastGravity;
224 }
225 sizehints.flags |= USPosition;
226 } else {
227 win->x = 0;
228 }
229 if ((gmask & YValue) != 0) {
230 if ((gmask & YNegative) != 0) {
231 win->y += e->scrh - win->h;
232 sizehints.win_gravity = sizehints.win_gravity == NorthEastGravity
233 ? SouthEastGravity : SouthWestGravity;
234 }
235 sizehints.flags |= USPosition;
236 } else {
237 win->y = 0;
238 }
239
240 attr.background_pixel = win->bgcol;
241 attr_mask = CWBackPixel;
242
243 win->xwin = XCreateWindow(e->dpy, RootWindow(e->dpy, e->scr),
244 win->x, win->y, win->w, win->h, 0,
245 e->depth, InputOutput, e->vis, attr_mask, &attr);
246 if (win->xwin == None)
247 die("could not create window");
248
249 XSelectInput(e->dpy, win->xwin,
250 ExposureMask | ButtonReleaseMask | ButtonPressMask |
251 KeyPressMask | PointerMotionMask | StructureNotifyMask);
252
253 carrow = XCreateFontCursor(e->dpy, XC_left_ptr);
254 chand = XCreateFontCursor(e->dpy, XC_fleur);
255 cwatch = XCreateFontCursor(e->dpy, XC_watch);
256
257 if (XAllocNamedColor(e->dpy, DefaultColormap(e->dpy, e->scr), "black",
258 &col, &col) == 0)
259 {
260 die("could not allocate color: black");
261 }
262 none = XCreateBitmapFromData(e->dpy, win->xwin, none_data, 8, 8);
263 cnone = XCreatePixmapCursor(e->dpy, none, none, &col, &col, 0, 0);
264
265 gc = XCreateGC(e->dpy, win->xwin, 0, None);
266
267 n = icons[ARRLEN(icons)-1].size;
268 icon_data = s_malloc((n * n + 2) * sizeof(*icon_data));
269
270 for (i = 0; i < ARRLEN(icons); i++) {
271 n = 0;
272 icon_data[n++] = icons[i].size;
273 icon_data[n++] = icons[i].size;
274
275 for (j = 0; j < icons[i].cnt; j++) {
276 for (c = icons[i].data[j] >> 4; c >= 0; c--)
277 icon_data[n++] = icon_colors[icons[i].data[j] & 0x0F];
278 }
279 XChangeProperty(e->dpy, win->xwin,
280 atoms[ATOM__NET_WM_ICON], XA_CARDINAL, 32,
281 i == 0 ? PropModeReplace : PropModeAppend,
282 (unsigned char *) icon_data, n);
283 }
284 free(icon_data);
285
286 win_set_title(win, "sxiv");
287
288 classhint.res_class = "Sxiv";
289 classhint.res_name = options->res_name != NULL ? options->res_name : "sxiv";
290 XSetClassHint(e->dpy, win->xwin, &classhint);
291
292 XSetWMProtocols(e->dpy, win->xwin, &atoms[ATOM_WM_DELETE_WINDOW], 1);
293
294 sizehints.width = win->w;
295 sizehints.height = win->h;
296 sizehints.x = win->x;
297 sizehints.y = win->y;
298 XSetWMNormalHints(win->env.dpy, win->xwin, &sizehints);
299
300 win->h -= win->bar.h;
301
302 XMapWindow(e->dpy, win->xwin);
303 XFlush(e->dpy);
304
305 if (options->fullscreen)
306 win_toggle_fullscreen(win);
307}
308
309void win_close(win_t *win)
310{
311 if (win == NULL || win->xwin == None)
312 return;
313
314 XFreeCursor(win->env.dpy, carrow);
315 XFreeCursor(win->env.dpy, cnone);
316 XFreeCursor(win->env.dpy, chand);
317 XFreeCursor(win->env.dpy, cwatch);
318
319 XFreeGC(win->env.dpy, gc);
320
321 XDestroyWindow(win->env.dpy, win->xwin);
322 XCloseDisplay(win->env.dpy);
323}
324
325bool win_configure(win_t *win, XConfigureEvent *c)
326{
327 bool changed;
328
329 if (win == NULL || c == NULL)
330 return false;
331
332 if ((changed = win->w != c->width || win->h + win->bar.h != c->height)) {
333 if (win->pm != None) {
334 XFreePixmap(win->env.dpy, win->pm);
335 win->pm = None;
336 }
337 }
338
339 win->x = c->x;
340 win->y = c->y;
341 win->w = c->width;
342 win->h = c->height - win->bar.h;
343 win->bw = c->border_width;
344
345 return changed;
346}
347
348void win_expose(win_t *win, XExposeEvent *e)
349{
350 if (win == NULL || win->xwin == None || win->pm == None || e == NULL)
351 return;
352
353 XCopyArea(win->env.dpy, win->pm, win->xwin, gc,
354 e->x, e->y, e->width, e->height, e->x, e->y);
355}
356
357void win_toggle_fullscreen(win_t *win)
358{
359 XEvent ev;
360 XClientMessageEvent *cm;
361
362 if (win == NULL || win->xwin == None)
363 return;
364
365 if (!fs_support) {
366 if (!fs_warned) {
367 warn("window manager does not support fullscreen");
368 fs_warned = True;
369 }
370 return;
371 }
372 win->fullscreen = !win->fullscreen;
373
374 memset(&ev, 0, sizeof(ev));
375 ev.type = ClientMessage;
376
377 cm = &ev.xclient;
378 cm->window = win->xwin;
379 cm->message_type = atoms[ATOM__NET_WM_STATE];
380 cm->format = 32;
381 cm->data.l[0] = win->fullscreen;
382 cm->data.l[1] = atoms[ATOM__NET_WM_STATE_FULLSCREEN];
383 cm->data.l[2] = cm->data.l[3] = 0;
384
385 XSendEvent(win->env.dpy, DefaultRootWindow(win->env.dpy), False,
386 SubstructureNotifyMask | SubstructureRedirectMask, &ev);
387}
388
389void win_toggle_bar(win_t *win)
390{
391 if (win == NULL || win->xwin == None)
392 return;
393
394 if (win->bar.h != 0) {
395 win->h += win->bar.h;
396 win->bar.h = 0;
397 } else {
398 win->bar.h = barheight;
399 win->h -= win->bar.h;
400 }
401}
402
403void win_clear(win_t *win)
404{
405 int h;
406 win_env_t *e;
407
408 if (win == NULL || win->xwin == None)
409 return;
410
411 h = win->h + win->bar.h;
412 e = &win->env;
413
414 if (win->pm == None)
415 win->pm = XCreatePixmap(e->dpy, win->xwin, win->w, h, e->depth);
416
417 XSetForeground(e->dpy, gc, win->fullscreen ? win->fscol : win->bgcol);
418 XFillRectangle(e->dpy, win->pm, gc, 0, 0, win->w, h);
419}
420
421void win_draw_bar(win_t *win)
422{
423 int len, olen, x, y, w, tw;
424 char rest[3];
425 const char *dots = "...";
426 win_env_t *e;
427
428 if (win == NULL || win->xwin == None || win->pm == None)
429 return;
430
431 e = &win->env;
432 y = win->h + font.ascent + V_TEXT_PAD;
433 w = win->w;
434
435 XSetForeground(e->dpy, gc, win->bar.bgcol);
436 XFillRectangle(e->dpy, win->pm, gc, 0, win->h, win->w, win->bar.h);
437
438 XSetForeground(e->dpy, gc, win->bar.fgcol);
439 XSetBackground(e->dpy, gc, win->bar.bgcol);
440
441 if ((len = strlen(win->bar.r)) > 0) {
442 if ((tw = win_textwidth(win->bar.r, len, true)) > w)
443 return;
444 x = win->w - tw + H_TEXT_PAD;
445 w -= tw;
446 if (font.set)
447 XmbDrawString(e->dpy, win->pm, font.set, gc, x, y, win->bar.r, len);
448 else
449 XDrawString(e->dpy, win->pm, gc, x, y, win->bar.r, len);
450 }
451 if ((len = strlen(win->bar.l)) > 0) {
452 olen = len;
453 while (len > 0 && (tw = win_textwidth(win->bar.l, len, true)) > w)
454 len--;
455 if (len > 0) {
456 if (len != olen) {
457 w = strlen(dots);
458 if (len <= w)
459 return;
460 memcpy(rest, win->bar.l + len - w, w);
461 memcpy(win->bar.l + len - w, dots, w);
462 }
463 x = H_TEXT_PAD;
464 if (font.set)
465 XmbDrawString(e->dpy, win->pm, font.set, gc, x, y, win->bar.l, len);
466 else
467 XDrawString(e->dpy, win->pm, gc, x, y, win->bar.l, len);
468 if (len != olen)
469 memcpy(win->bar.l + len - w, rest, w);
470 }
471 }
472}
473
474void win_draw(win_t *win)
475{
476 if (win == NULL || win->xwin == None || win->pm == None)
477 return;
478
479 if (win->bar.h > 0)
480 win_draw_bar(win);
481
482 XCopyArea(win->env.dpy, win->pm, win->xwin, gc,
483 0, 0, win->w, win->h + win->bar.h, 0, 0);
484}
485
486void win_draw_rect(win_t *win, Pixmap pm, int x, int y, int w, int h,
487 bool fill, int lw, unsigned long col)
488{
489 XGCValues gcval;
490
491 if (win == NULL || pm == None)
492 return;
493
494 gcval.line_width = lw;
495 gcval.foreground = col;
496 XChangeGC(win->env.dpy, gc, GCForeground | GCLineWidth, &gcval);
497
498 if (fill)
499 XFillRectangle(win->env.dpy, pm, gc, x, y, w, h);
500 else
501 XDrawRectangle(win->env.dpy, pm, gc, x, y, w, h);
502}
503
504void win_update_bar(win_t *win)
505{
506 if (win == NULL || win->xwin == None || win->pm == None)
507 return;
508
509 if (win->bar.h > 0) {
510 win_draw_bar(win);
511 XCopyArea(win->env.dpy, win->pm, win->xwin, gc,
512 0, win->h, win->w, win->bar.h, 0, win->h);
513 }
514}
515
516int win_textwidth(const char *text, unsigned int len, bool with_padding)
517{
518 XRectangle r;
519 int padding = with_padding ? 2 * H_TEXT_PAD : 0;
520
521 if (font.set) {
522 XmbTextExtents(font.set, text, len, NULL, &r);
523 return r.width + padding;
524 } else {
525 return XTextWidth(font.xfont, text, len) + padding;
526 }
527}
528
529void win_set_title(win_t *win, const char *title)
530{
531 if (win == NULL || win->xwin == None)
532 return;
533
534 if (title == NULL)
535 title = "sxiv";
536
537 XStoreName(win->env.dpy, win->xwin, title);
538 XSetIconName(win->env.dpy, win->xwin, title);
539
540 XChangeProperty(win->env.dpy, win->xwin, atoms[ATOM__NET_WM_NAME],
541 XInternAtom(win->env.dpy, "UTF8_STRING", False), 8,
542 PropModeReplace, (unsigned char *) title, strlen(title));
543 XChangeProperty(win->env.dpy, win->xwin, atoms[ATOM__NET_WM_ICON_NAME],
544 XInternAtom(win->env.dpy, "UTF8_STRING", False), 8,
545 PropModeReplace, (unsigned char *) title, strlen(title));
546}
547
548void win_set_cursor(win_t *win, cursor_t cursor)
549{
550 if (win == NULL || win->xwin == None)
551 return;
552
553 switch (cursor) {
554 case CURSOR_NONE:
555 XDefineCursor(win->env.dpy, win->xwin, cnone);
556 break;
557 case CURSOR_HAND:
558 XDefineCursor(win->env.dpy, win->xwin, chand);
559 break;
560 case CURSOR_WATCH:
561 XDefineCursor(win->env.dpy, win->xwin, cwatch);
562 break;
563 case CURSOR_ARROW:
564 default:
565 XDefineCursor(win->env.dpy, win->xwin, carrow);
566 break;
567 }
568
569 XFlush(win->env.dpy);
570}