Krolyxon
Commit eb82519

Branches

Refs →

nsxivwindow.c

623 lines · 14.9 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->white = WhitePixel(e->dpy, e->scr);
167 win->bgcol = win_alloc_color(win, WIN_BG_COLOR);
168 win->fscol = win_alloc_color(win, WIN_FS_COLOR);
169 win->selcol = win_alloc_color(win, SEL_COLOR);
170 win->bar.bgcol = win_alloc_color(win, BAR_BG_COLOR);
171 win->bar.fgcol = win_alloc_color(win, BAR_FG_COLOR);
172 win->bar.h = options->hide_bar ? 0 : barheight;
173
174 win->sizehints.flags = PWinGravity;
175 win->sizehints.win_gravity = NorthWestGravity;
176 if (options->fixed_win)
177 /* actual min/max values set in win_update_sizehints() */
178 win->sizehints.flags |= PMinSize | PMaxSize;
179
180 INIT_ATOM_(WM_DELETE_WINDOW);
181 INIT_ATOM_(_NET_WM_NAME);
182 INIT_ATOM_(_NET_WM_ICON_NAME);
183 INIT_ATOM_(_NET_WM_ICON);
184 INIT_ATOM_(_NET_WM_STATE);
185 INIT_ATOM_(_NET_WM_STATE_FULLSCREEN);
186 INIT_ATOM_(_NET_SUPPORTED);
187
188 win_check_wm_support(e->dpy, RootWindow(e->dpy, e->scr));
189}
190
191void win_update_sizehints(win_t *win)
192{
193 if (win == NULL || win->xwin == None)
194 return;
195
196 if ((win->sizehints.flags & USSize) != 0) {
197 win->sizehints.width = win->w;
198 win->sizehints.height = win->h + win->bar.h;
199 }
200 if ((win->sizehints.flags & USPosition) != 0) {
201 win->sizehints.x = win->x;
202 win->sizehints.y = win->y;
203 }
204 if (options->fixed_win) {
205 win->sizehints.min_width = win->sizehints.max_width = win->w;
206 win->sizehints.min_height = win->sizehints.max_height = win->h + win->bar.h;
207 }
208 XSetWMNormalHints(win->env.dpy, win->xwin, &win->sizehints);
209}
210
211void win_open(win_t *win)
212{
213 int c, i, j, n;
214 win_env_t *e;
215 XClassHint classhint;
216 XSetWindowAttributes attr;
217 unsigned long attr_mask;
218 unsigned long *icon_data;
219 XColor col;
220 char none_data[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
221 Pixmap none;
222 int gmask;
223
224 if (win == NULL)
225 return;
226
227 e = &win->env;
228
229 /* determine window offsets, width & height */
230 if (options->geometry == NULL)
231 gmask = 0;
232 else
233 gmask = XParseGeometry(options->geometry, &win->x, &win->y,
234 &win->w, &win->h);
235 if ((gmask & WidthValue) != 0)
236 win->sizehints.flags |= USSize;
237 else
238 win->w = WIN_WIDTH;
239 if ((gmask & HeightValue) != 0)
240 win->sizehints.flags |= USSize;
241 else
242 win->h = WIN_HEIGHT;
243 if ((gmask & XValue) != 0) {
244 if ((gmask & XNegative) != 0) {
245 win->x += e->scrw - win->w;
246 win->sizehints.win_gravity = NorthEastGravity;
247 }
248 win->sizehints.flags |= USPosition;
249 } else {
250 win->x = (e->scrw - win->w) / 2;
251 }
252 if ((gmask & YValue) != 0) {
253 if ((gmask & YNegative) != 0) {
254 win->y += e->scrh - win->h;
255 if (win->sizehints.win_gravity == NorthEastGravity)
256 win->sizehints.win_gravity = SouthEastGravity;
257 else
258 win->sizehints.win_gravity = SouthWestGravity;
259 }
260 win->sizehints.flags |= USPosition;
261 } else {
262 win->y = (e->scrh - win->h) / 2;
263 }
264
265 attr.background_pixel = win->bgcol;
266 attr_mask = CWBackPixel;
267
268 win->xwin = XCreateWindow(e->dpy, RootWindow(e->dpy, e->scr),
269 win->x, win->y, win->w, win->h, 0,
270 e->depth, InputOutput, e->vis, attr_mask, &attr);
271 if (win->xwin == None)
272 die("could not create window");
273
274 XSelectInput(e->dpy, win->xwin,
275 ExposureMask | ButtonReleaseMask | ButtonPressMask |
276 KeyPressMask | PointerMotionMask | StructureNotifyMask);
277
278 carrow = XCreateFontCursor(e->dpy, XC_left_ptr);
279 chand = XCreateFontCursor(e->dpy, XC_fleur);
280 cwatch = XCreateFontCursor(e->dpy, XC_watch);
281
282 if (XAllocNamedColor(e->dpy, DefaultColormap(e->dpy, e->scr), "black",
283 &col, &col) == 0)
284 {
285 die("could not allocate color: black");
286 }
287 none = XCreateBitmapFromData(e->dpy, win->xwin, none_data, 8, 8);
288 cnone = XCreatePixmapCursor(e->dpy, none, none, &col, &col, 0, 0);
289
290 gc = XCreateGC(e->dpy, win->xwin, 0, None);
291
292 n = icons[ARRLEN(icons)-1].size;
293 icon_data = s_malloc((n * n + 2) * sizeof(*icon_data));
294
295 for (i = 0; i < ARRLEN(icons); i++) {
296 n = 0;
297 icon_data[n++] = icons[i].size;
298 icon_data[n++] = icons[i].size;
299
300 for (j = 0; j < icons[i].cnt; j++) {
301 for (c = icons[i].data[j] >> 4; c >= 0; c--)
302 icon_data[n++] = icon_colors[icons[i].data[j] & 0x0F];
303 }
304 XChangeProperty(e->dpy, win->xwin,
305 atoms[ATOM__NET_WM_ICON], XA_CARDINAL, 32,
306 i == 0 ? PropModeReplace : PropModeAppend,
307 (unsigned char *) icon_data, n);
308 }
309 free(icon_data);
310
311 win_set_title(win, "sxiv");
312
313 classhint.res_class = "Sxiv";
314 classhint.res_name = options->res_name != NULL ? options->res_name : "sxiv";
315 XSetClassHint(e->dpy, win->xwin, &classhint);
316
317 XSetWMProtocols(e->dpy, win->xwin, &atoms[ATOM_WM_DELETE_WINDOW], 1);
318
319 win->h -= win->bar.h;
320 win_update_sizehints(win);
321
322 XMapWindow(e->dpy, win->xwin);
323 XFlush(e->dpy);
324
325 if (options->fullscreen)
326 win_toggle_fullscreen(win);
327}
328
329void win_close(win_t *win)
330{
331 if (win == NULL || win->xwin == None)
332 return;
333
334 XFreeCursor(win->env.dpy, carrow);
335 XFreeCursor(win->env.dpy, cnone);
336 XFreeCursor(win->env.dpy, chand);
337 XFreeCursor(win->env.dpy, cwatch);
338
339 XFreeGC(win->env.dpy, gc);
340
341 XDestroyWindow(win->env.dpy, win->xwin);
342 XCloseDisplay(win->env.dpy);
343}
344
345bool win_configure(win_t *win, XConfigureEvent *c)
346{
347 bool changed;
348
349 if (win == NULL || c == NULL)
350 return false;
351
352 if ((changed = win->w != c->width || win->h + win->bar.h != c->height)) {
353 if (win->pm != None) {
354 XFreePixmap(win->env.dpy, win->pm);
355 win->pm = None;
356 }
357 }
358
359 win->x = c->x;
360 win->y = c->y;
361 win->w = c->width;
362 win->h = c->height - win->bar.h;
363 win->bw = c->border_width;
364
365 return changed;
366}
367
368void win_expose(win_t *win, XExposeEvent *e)
369{
370 if (win == NULL || win->xwin == None || win->pm == None || e == NULL)
371 return;
372
373 XCopyArea(win->env.dpy, win->pm, win->xwin, gc,
374 e->x, e->y, e->width, e->height, e->x, e->y);
375}
376
377bool win_moveresize(win_t *win, int x, int y, unsigned int w, unsigned int h)
378{
379 if (win == NULL || win->xwin == None)
380 return false;
381
382 /* caller knows nothing about the bar */
383 h += win->bar.h;
384
385 x = MAX(0, x);
386 y = MAX(0, y);
387 w = MIN(w, win->env.scrw - 2 * win->bw);
388 h = MIN(h, win->env.scrh - 2 * win->bw);
389
390 if (win->x == x && win->y == y && win->w == w && win->h + win->bar.h == h)
391 return false;
392
393 win->x = x;
394 win->y = y;
395 win->w = w;
396 win->h = h - win->bar.h;
397
398 win_update_sizehints(win);
399
400 XMoveResizeWindow(win->env.dpy, win->xwin, x, y, w, h);
401
402 if (win->pm != None) {
403 XFreePixmap(win->env.dpy, win->pm);
404 win->pm = None;
405 }
406
407 return true;
408}
409
410void win_toggle_fullscreen(win_t *win)
411{
412 XEvent ev;
413 XClientMessageEvent *cm;
414
415 if (win == NULL || win->xwin == None)
416 return;
417
418 if (!fs_support) {
419 if (!fs_warned) {
420 warn("window manager does not support fullscreen");
421 fs_warned = True;
422 }
423 return;
424 }
425 win->fullscreen = !win->fullscreen;
426
427 memset(&ev, 0, sizeof(ev));
428 ev.type = ClientMessage;
429
430 cm = &ev.xclient;
431 cm->window = win->xwin;
432 cm->message_type = atoms[ATOM__NET_WM_STATE];
433 cm->format = 32;
434 cm->data.l[0] = win->fullscreen;
435 cm->data.l[1] = atoms[ATOM__NET_WM_STATE_FULLSCREEN];
436 cm->data.l[2] = cm->data.l[3] = 0;
437
438 XSendEvent(win->env.dpy, DefaultRootWindow(win->env.dpy), False,
439 SubstructureNotifyMask | SubstructureRedirectMask, &ev);
440}
441
442void win_toggle_bar(win_t *win)
443{
444 if (win == NULL || win->xwin == None)
445 return;
446
447 if (win->bar.h != 0) {
448 win->h += win->bar.h;
449 win->bar.h = 0;
450 } else {
451 win->bar.h = barheight;
452 win->h -= win->bar.h;
453 }
454}
455
456void win_clear(win_t *win)
457{
458 int h;
459 win_env_t *e;
460
461 if (win == NULL || win->xwin == None)
462 return;
463
464 h = win->h + win->bar.h;
465 e = &win->env;
466
467 if (win->pm == None)
468 win->pm = XCreatePixmap(e->dpy, win->xwin, win->w, h, e->depth);
469
470 XSetForeground(e->dpy, gc, win->fullscreen ? win->fscol : win->bgcol);
471 XFillRectangle(e->dpy, win->pm, gc, 0, 0, win->w, h);
472}
473
474void win_draw_bar(win_t *win)
475{
476 int len, olen, x, y, w, tw;
477 char rest[3];
478 const char *dots = "...";
479 win_env_t *e;
480
481 if (win == NULL || win->xwin == None || win->pm == None)
482 return;
483
484 e = &win->env;
485 y = win->h + font.ascent + V_TEXT_PAD;
486 w = win->w;
487
488 XSetForeground(e->dpy, gc, win->bar.bgcol);
489 XFillRectangle(e->dpy, win->pm, gc, 0, win->h, win->w, win->bar.h);
490
491 XSetForeground(e->dpy, gc, win->bar.fgcol);
492 XSetBackground(e->dpy, gc, win->bar.bgcol);
493
494 if ((len = strlen(win->bar.r)) > 0) {
495 if ((tw = win_textwidth(win->bar.r, len, true)) > w)
496 return;
497 x = win->w - tw + H_TEXT_PAD;
498 w -= tw;
499 if (font.set)
500 XmbDrawString(e->dpy, win->pm, font.set, gc, x, y, win->bar.r, len);
501 else
502 XDrawString(e->dpy, win->pm, gc, x, y, win->bar.r, len);
503 }
504 if ((len = strlen(win->bar.l)) > 0) {
505 olen = len;
506 while (len > 0 && (tw = win_textwidth(win->bar.l, len, true)) > w)
507 len--;
508 if (len > 0) {
509 if (len != olen) {
510 w = strlen(dots);
511 if (len <= w)
512 return;
513 memcpy(rest, win->bar.l + len - w, w);
514 memcpy(win->bar.l + len - w, dots, w);
515 }
516 x = H_TEXT_PAD;
517 if (font.set)
518 XmbDrawString(e->dpy, win->pm, font.set, gc, x, y, win->bar.l, len);
519 else
520 XDrawString(e->dpy, win->pm, gc, x, y, win->bar.l, len);
521 if (len != olen)
522 memcpy(win->bar.l + len - w, rest, w);
523 }
524 }
525}
526
527void win_draw(win_t *win)
528{
529 if (win == NULL || win->xwin == None || win->pm == None)
530 return;
531
532 if (win->bar.h > 0)
533 win_draw_bar(win);
534
535 XCopyArea(win->env.dpy, win->pm, win->xwin, gc,
536 0, 0, win->w, win->h + win->bar.h, 0, 0);
537}
538
539void win_draw_rect(win_t *win, Pixmap pm, int x, int y, int w, int h,
540 bool fill, int lw, unsigned long col)
541{
542 XGCValues gcval;
543
544 if (win == NULL || pm == None)
545 return;
546
547 gcval.line_width = lw;
548 gcval.foreground = col;
549 XChangeGC(win->env.dpy, gc, GCForeground | GCLineWidth, &gcval);
550
551 if (fill)
552 XFillRectangle(win->env.dpy, pm, gc, x, y, w, h);
553 else
554 XDrawRectangle(win->env.dpy, pm, gc, x, y, w, h);
555}
556
557void win_update_bar(win_t *win)
558{
559 if (win == NULL || win->xwin == None || win->pm == None)
560 return;
561
562 if (win->bar.h > 0) {
563 win_draw_bar(win);
564 XCopyArea(win->env.dpy, win->pm, win->xwin, gc,
565 0, win->h, win->w, win->bar.h, 0, win->h);
566 }
567}
568
569int win_textwidth(const char *text, unsigned int len, bool with_padding)
570{
571 XRectangle r;
572 int padding = with_padding ? 2 * H_TEXT_PAD : 0;
573
574 if (font.set) {
575 XmbTextExtents(font.set, text, len, NULL, &r);
576 return r.width + padding;
577 } else {
578 return XTextWidth(font.xfont, text, len) + padding;
579 }
580}
581
582void win_set_title(win_t *win, const char *title)
583{
584 if (win == NULL || win->xwin == None)
585 return;
586
587 if (title == NULL)
588 title = "sxiv";
589
590 XStoreName(win->env.dpy, win->xwin, title);
591 XSetIconName(win->env.dpy, win->xwin, title);
592
593 XChangeProperty(win->env.dpy, win->xwin, atoms[ATOM__NET_WM_NAME],
594 XInternAtom(win->env.dpy, "UTF8_STRING", False), 8,
595 PropModeReplace, (unsigned char *) title, strlen(title));
596 XChangeProperty(win->env.dpy, win->xwin, atoms[ATOM__NET_WM_ICON_NAME],
597 XInternAtom(win->env.dpy, "UTF8_STRING", False), 8,
598 PropModeReplace, (unsigned char *) title, strlen(title));
599}
600
601void win_set_cursor(win_t *win, cursor_t cursor)
602{
603 if (win == NULL || win->xwin == None)
604 return;
605
606 switch (cursor) {
607 case CURSOR_NONE:
608 XDefineCursor(win->env.dpy, win->xwin, cnone);
609 break;
610 case CURSOR_HAND:
611 XDefineCursor(win->env.dpy, win->xwin, chand);
612 break;
613 case CURSOR_WATCH:
614 XDefineCursor(win->env.dpy, win->xwin, cwatch);
615 break;
616 case CURSOR_ARROW:
617 default:
618 XDefineCursor(win->env.dpy, win->xwin, carrow);
619 break;
620 }
621
622 XFlush(win->env.dpy);
623}