Krolyxon
Commit 92e3b57

Branches

Refs →

nsxivwindow.c

559 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->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
172 win->bar.l.size = BAR_L_LEN;
173 win->bar.r.size = BAR_R_LEN;
174 win->bar.l.buf = s_malloc(win->bar.l.size);
175 win->bar.r.buf = s_malloc(win->bar.r.size);
176 win->bar.h = options->hide_bar ? 0 : barheight;
177
178 INIT_ATOM_(WM_DELETE_WINDOW);
179 INIT_ATOM_(_NET_WM_NAME);
180 INIT_ATOM_(_NET_WM_ICON_NAME);
181 INIT_ATOM_(_NET_WM_ICON);
182 INIT_ATOM_(_NET_WM_STATE);
183 INIT_ATOM_(_NET_WM_STATE_FULLSCREEN);
184 INIT_ATOM_(_NET_SUPPORTED);
185
186 win_check_wm_support(e->dpy, RootWindow(e->dpy, e->scr));
187}
188
189void win_open(win_t *win)
190{
191 int c, i, j, n;
192 win_env_t *e;
193 XClassHint classhint;
194 unsigned long *icon_data;
195 XColor col;
196 char none_data[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
197 Pixmap none;
198 int gmask;
199 XSizeHints sizehints;
200 Bool fullscreen = options->fullscreen && fs_support;
201
202 if (win == NULL)
203 return;
204
205 e = &win->env;
206
207 sizehints.flags = PWinGravity;
208 sizehints.win_gravity = NorthWestGravity;
209
210 /* determine window offsets, width & height */
211 if (options->geometry == NULL)
212 gmask = 0;
213 else
214 gmask = XParseGeometry(options->geometry, &win->x, &win->y,
215 &win->w, &win->h);
216 if ((gmask & WidthValue) != 0)
217 sizehints.flags |= USSize;
218 else
219 win->w = WIN_WIDTH;
220 if ((gmask & HeightValue) != 0)
221 sizehints.flags |= USSize;
222 else
223 win->h = WIN_HEIGHT;
224 if ((gmask & XValue) != 0) {
225 if ((gmask & XNegative) != 0) {
226 win->x += e->scrw - win->w;
227 sizehints.win_gravity = NorthEastGravity;
228 }
229 sizehints.flags |= USPosition;
230 } else {
231 win->x = 0;
232 }
233 if ((gmask & YValue) != 0) {
234 if ((gmask & YNegative) != 0) {
235 win->y += e->scrh - win->h;
236 sizehints.win_gravity = sizehints.win_gravity == NorthEastGravity
237 ? SouthEastGravity : SouthWestGravity;
238 }
239 sizehints.flags |= USPosition;
240 } else {
241 win->y = 0;
242 }
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, 0, NULL);
247 if (win->xwin == None)
248 die("could not create window");
249
250 XSelectInput(e->dpy, win->xwin,
251 ButtonReleaseMask | ButtonPressMask | KeyPressMask |
252 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 win->buf.w = e->scrw;
304 win->buf.h = e->scrh;
305 win->buf.pm = XCreatePixmap(e->dpy, win->xwin,
306 win->buf.w, win->buf.h, e->depth);
307 XSetForeground(e->dpy, gc, fullscreen ? win->fscol : win->bgcol);
308 XFillRectangle(e->dpy, win->buf.pm, gc, 0, 0, win->buf.w, win->buf.h);
309 XSetWindowBackgroundPixmap(e->dpy, win->xwin, win->buf.pm);
310
311 XMapWindow(e->dpy, win->xwin);
312 XFlush(e->dpy);
313
314 if (fullscreen)
315 win_toggle_fullscreen(win);
316}
317
318void win_close(win_t *win)
319{
320 if (win == NULL || win->xwin == None)
321 return;
322
323 XFreeCursor(win->env.dpy, carrow);
324 XFreeCursor(win->env.dpy, cnone);
325 XFreeCursor(win->env.dpy, chand);
326 XFreeCursor(win->env.dpy, cwatch);
327
328 XFreeGC(win->env.dpy, gc);
329
330 XDestroyWindow(win->env.dpy, win->xwin);
331 XCloseDisplay(win->env.dpy);
332}
333
334bool win_configure(win_t *win, XConfigureEvent *c)
335{
336 bool changed;
337
338 if (win == NULL || c == NULL)
339 return false;
340
341 changed = win->w != c->width || win->h + win->bar.h != c->height;
342
343 win->x = c->x;
344 win->y = c->y;
345 win->w = c->width;
346 win->h = c->height - win->bar.h;
347 win->bw = c->border_width;
348
349 return changed;
350}
351
352void win_toggle_fullscreen(win_t *win)
353{
354 XEvent ev;
355 XClientMessageEvent *cm;
356
357 if (win == NULL || win->xwin == None)
358 return;
359
360 if (!fs_support) {
361 if (!fs_warned) {
362 warn("window manager does not support fullscreen");
363 fs_warned = True;
364 }
365 return;
366 }
367 win->fullscreen = !win->fullscreen;
368
369 memset(&ev, 0, sizeof(ev));
370 ev.type = ClientMessage;
371
372 cm = &ev.xclient;
373 cm->window = win->xwin;
374 cm->message_type = atoms[ATOM__NET_WM_STATE];
375 cm->format = 32;
376 cm->data.l[0] = win->fullscreen;
377 cm->data.l[1] = atoms[ATOM__NET_WM_STATE_FULLSCREEN];
378 cm->data.l[2] = cm->data.l[3] = 0;
379
380 XSendEvent(win->env.dpy, DefaultRootWindow(win->env.dpy), False,
381 SubstructureNotifyMask | SubstructureRedirectMask, &ev);
382}
383
384void win_toggle_bar(win_t *win)
385{
386 if (win == NULL || win->xwin == None)
387 return;
388
389 if (win->bar.h != 0) {
390 win->h += win->bar.h;
391 win->bar.h = 0;
392 } else {
393 win->bar.h = barheight;
394 win->h -= win->bar.h;
395 }
396}
397
398void win_clear(win_t *win)
399{
400 win_env_t *e;
401
402 if (win == NULL || win->xwin == None)
403 return;
404
405 e = &win->env;
406
407 if (win->w > win->buf.w || win->h + win->bar.h > win->buf.h) {
408 XFreePixmap(e->dpy, win->buf.pm);
409 win->buf.w = MAX(win->buf.w, win->w);
410 win->buf.h = MAX(win->buf.h, win->h + win->bar.h);
411 win->buf.pm = XCreatePixmap(e->dpy, win->xwin,
412 win->buf.w, win->buf.h, e->depth);
413 }
414 XSetForeground(e->dpy, gc, win->fullscreen ? win->fscol : win->bgcol);
415 XFillRectangle(e->dpy, win->buf.pm, gc, 0, 0, win->buf.w, win->buf.h);
416}
417
418void win_draw_bar(win_t *win)
419{
420 int len, olen, x, y, w, tw;
421 char rest[3];
422 const char *dots = "...";
423 win_env_t *e;
424 win_bar_t *l, *r;
425
426 if (win == NULL || win->xwin == None)
427 return;
428 if ((l = &win->bar.l)->buf == NULL || (r = &win->bar.r)->buf == NULL)
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->buf.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(r->buf)) > 0) {
442 if ((tw = win_textwidth(r->buf, 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->buf.pm, font.set, gc, x, y, r->buf, len);
448 else
449 XDrawString(e->dpy, win->buf.pm, gc, x, y, r->buf, len);
450 }
451 if ((len = strlen(l->buf)) > 0) {
452 olen = len;
453 while (len > 0 && (tw = win_textwidth(l->buf, 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, l->buf + len - w, w);
461 memcpy(l->buf + len - w, dots, w);
462 }
463 x = H_TEXT_PAD;
464 if (font.set)
465 XmbDrawString(e->dpy, win->buf.pm, font.set, gc, x, y, l->buf, len);
466 else
467 XDrawString(e->dpy, win->buf.pm, gc, x, y, l->buf, len);
468 if (len != olen)
469 memcpy(l->buf + len - w, rest, w);
470 }
471 }
472}
473
474void win_draw(win_t *win)
475{
476 if (win == NULL || win->xwin == None)
477 return;
478
479 if (win->bar.h > 0)
480 win_draw_bar(win);
481
482 XSetWindowBackgroundPixmap(win->env.dpy, win->xwin, win->buf.pm);
483 XClearWindow(win->env.dpy, win->xwin);
484 XFlush(win->env.dpy);
485}
486
487void win_draw_rect(win_t *win, int x, int y, int w, int h, bool fill, int lw,
488 unsigned long col)
489{
490 XGCValues gcval;
491
492 if (win == NULL || win->buf.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, win->buf.pm, gc, x, y, w, h);
501 else
502 XDrawRectangle(win->env.dpy, win->buf.pm, gc, x, y, w, h);
503}
504
505int win_textwidth(const char *text, unsigned int len, bool with_padding)
506{
507 XRectangle r;
508 int padding = with_padding ? 2 * H_TEXT_PAD : 0;
509
510 if (font.set) {
511 XmbTextExtents(font.set, text, len, NULL, &r);
512 return r.width + padding;
513 } else {
514 return XTextWidth(font.xfont, text, len) + padding;
515 }
516}
517
518void win_set_title(win_t *win, const char *title)
519{
520 if (win == NULL || win->xwin == None)
521 return;
522
523 if (title == NULL)
524 title = "sxiv";
525
526 XStoreName(win->env.dpy, win->xwin, title);
527 XSetIconName(win->env.dpy, win->xwin, title);
528
529 XChangeProperty(win->env.dpy, win->xwin, atoms[ATOM__NET_WM_NAME],
530 XInternAtom(win->env.dpy, "UTF8_STRING", False), 8,
531 PropModeReplace, (unsigned char *) title, strlen(title));
532 XChangeProperty(win->env.dpy, win->xwin, atoms[ATOM__NET_WM_ICON_NAME],
533 XInternAtom(win->env.dpy, "UTF8_STRING", False), 8,
534 PropModeReplace, (unsigned char *) title, strlen(title));
535}
536
537void win_set_cursor(win_t *win, cursor_t cursor)
538{
539 if (win == NULL || win->xwin == None)
540 return;
541
542 switch (cursor) {
543 case CURSOR_NONE:
544 XDefineCursor(win->env.dpy, win->xwin, cnone);
545 break;
546 case CURSOR_HAND:
547 XDefineCursor(win->env.dpy, win->xwin, chand);
548 break;
549 case CURSOR_WATCH:
550 XDefineCursor(win->env.dpy, win->xwin, cwatch);
551 break;
552 case CURSOR_ARROW:
553 default:
554 XDefineCursor(win->env.dpy, win->xwin, carrow);
555 break;
556 }
557
558 XFlush(win->env.dpy);
559}