Krolyxon
Commit 524d9de

Branches

Refs →

nsxivwindow.c

590 lines · 14.3 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
377void win_toggle_fullscreen(win_t *win)
378{
379 XEvent ev;
380 XClientMessageEvent *cm;
381
382 if (win == NULL || win->xwin == None)
383 return;
384
385 if (!fs_support) {
386 if (!fs_warned) {
387 warn("window manager does not support fullscreen");
388 fs_warned = True;
389 }
390 return;
391 }
392 win->fullscreen = !win->fullscreen;
393
394 memset(&ev, 0, sizeof(ev));
395 ev.type = ClientMessage;
396
397 cm = &ev.xclient;
398 cm->window = win->xwin;
399 cm->message_type = atoms[ATOM__NET_WM_STATE];
400 cm->format = 32;
401 cm->data.l[0] = win->fullscreen;
402 cm->data.l[1] = atoms[ATOM__NET_WM_STATE_FULLSCREEN];
403 cm->data.l[2] = cm->data.l[3] = 0;
404
405 XSendEvent(win->env.dpy, DefaultRootWindow(win->env.dpy), False,
406 SubstructureNotifyMask | SubstructureRedirectMask, &ev);
407}
408
409void win_toggle_bar(win_t *win)
410{
411 if (win == NULL || win->xwin == None)
412 return;
413
414 if (win->bar.h != 0) {
415 win->h += win->bar.h;
416 win->bar.h = 0;
417 } else {
418 win->bar.h = barheight;
419 win->h -= win->bar.h;
420 }
421}
422
423void win_clear(win_t *win)
424{
425 int h;
426 win_env_t *e;
427
428 if (win == NULL || win->xwin == None)
429 return;
430
431 h = win->h + win->bar.h;
432 e = &win->env;
433
434 if (win->pm == None)
435 win->pm = XCreatePixmap(e->dpy, win->xwin, win->w, h, e->depth);
436
437 XSetForeground(e->dpy, gc, win->fullscreen ? win->fscol : win->bgcol);
438 XFillRectangle(e->dpy, win->pm, gc, 0, 0, win->w, h);
439}
440
441void win_draw_bar(win_t *win)
442{
443 int len, olen, x, y, w, tw;
444 char rest[3];
445 const char *dots = "...";
446 win_env_t *e;
447
448 if (win == NULL || win->xwin == None || win->pm == None)
449 return;
450
451 e = &win->env;
452 y = win->h + font.ascent + V_TEXT_PAD;
453 w = win->w;
454
455 XSetForeground(e->dpy, gc, win->bar.bgcol);
456 XFillRectangle(e->dpy, win->pm, gc, 0, win->h, win->w, win->bar.h);
457
458 XSetForeground(e->dpy, gc, win->bar.fgcol);
459 XSetBackground(e->dpy, gc, win->bar.bgcol);
460
461 if ((len = strlen(win->bar.r)) > 0) {
462 if ((tw = win_textwidth(win->bar.r, len, true)) > w)
463 return;
464 x = win->w - tw + H_TEXT_PAD;
465 w -= tw;
466 if (font.set)
467 XmbDrawString(e->dpy, win->pm, font.set, gc, x, y, win->bar.r, len);
468 else
469 XDrawString(e->dpy, win->pm, gc, x, y, win->bar.r, len);
470 }
471 if ((len = strlen(win->bar.l)) > 0) {
472 olen = len;
473 while (len > 0 && (tw = win_textwidth(win->bar.l, len, true)) > w)
474 len--;
475 if (len > 0) {
476 if (len != olen) {
477 w = strlen(dots);
478 if (len <= w)
479 return;
480 memcpy(rest, win->bar.l + len - w, w);
481 memcpy(win->bar.l + len - w, dots, w);
482 }
483 x = H_TEXT_PAD;
484 if (font.set)
485 XmbDrawString(e->dpy, win->pm, font.set, gc, x, y, win->bar.l, len);
486 else
487 XDrawString(e->dpy, win->pm, gc, x, y, win->bar.l, len);
488 if (len != olen)
489 memcpy(win->bar.l + len - w, rest, w);
490 }
491 }
492}
493
494void win_draw(win_t *win)
495{
496 if (win == NULL || win->xwin == None || win->pm == None)
497 return;
498
499 if (win->bar.h > 0)
500 win_draw_bar(win);
501
502 XCopyArea(win->env.dpy, win->pm, win->xwin, gc,
503 0, 0, win->w, win->h + win->bar.h, 0, 0);
504}
505
506void win_draw_rect(win_t *win, Pixmap pm, int x, int y, int w, int h,
507 bool fill, int lw, unsigned long col)
508{
509 XGCValues gcval;
510
511 if (win == NULL || pm == None)
512 return;
513
514 gcval.line_width = lw;
515 gcval.foreground = col;
516 XChangeGC(win->env.dpy, gc, GCForeground | GCLineWidth, &gcval);
517
518 if (fill)
519 XFillRectangle(win->env.dpy, pm, gc, x, y, w, h);
520 else
521 XDrawRectangle(win->env.dpy, pm, gc, x, y, w, h);
522}
523
524void win_update_bar(win_t *win)
525{
526 if (win == NULL || win->xwin == None || win->pm == None)
527 return;
528
529 if (win->bar.h > 0) {
530 win_draw_bar(win);
531 XCopyArea(win->env.dpy, win->pm, win->xwin, gc,
532 0, win->h, win->w, win->bar.h, 0, win->h);
533 }
534}
535
536int win_textwidth(const char *text, unsigned int len, bool with_padding)
537{
538 XRectangle r;
539 int padding = with_padding ? 2 * H_TEXT_PAD : 0;
540
541 if (font.set) {
542 XmbTextExtents(font.set, text, len, NULL, &r);
543 return r.width + padding;
544 } else {
545 return XTextWidth(font.xfont, text, len) + padding;
546 }
547}
548
549void win_set_title(win_t *win, const char *title)
550{
551 if (win == NULL || win->xwin == None)
552 return;
553
554 if (title == NULL)
555 title = "sxiv";
556
557 XStoreName(win->env.dpy, win->xwin, title);
558 XSetIconName(win->env.dpy, win->xwin, title);
559
560 XChangeProperty(win->env.dpy, win->xwin, atoms[ATOM__NET_WM_NAME],
561 XInternAtom(win->env.dpy, "UTF8_STRING", False), 8,
562 PropModeReplace, (unsigned char *) title, strlen(title));
563 XChangeProperty(win->env.dpy, win->xwin, atoms[ATOM__NET_WM_ICON_NAME],
564 XInternAtom(win->env.dpy, "UTF8_STRING", False), 8,
565 PropModeReplace, (unsigned char *) title, strlen(title));
566}
567
568void win_set_cursor(win_t *win, cursor_t cursor)
569{
570 if (win == NULL || win->xwin == None)
571 return;
572
573 switch (cursor) {
574 case CURSOR_NONE:
575 XDefineCursor(win->env.dpy, win->xwin, cnone);
576 break;
577 case CURSOR_HAND:
578 XDefineCursor(win->env.dpy, win->xwin, chand);
579 break;
580 case CURSOR_WATCH:
581 XDefineCursor(win->env.dpy, win->xwin, cwatch);
582 break;
583 case CURSOR_ARROW:
584 default:
585 XDefineCursor(win->env.dpy, win->xwin, carrow);
586 break;
587 }
588
589 XFlush(win->env.dpy);
590}