Krolyxon
Commit eaf55e9

Branches

Refs →

nsxivwindow.c

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