Krolyxon
Commit 6d7acac

Branches

Refs →

nsxivwindow.c

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