Krolyxon
Commit e574a6d

Branches

Refs →

nsxivwindow.c

521 lines · 13.1 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#include <stdlib.h>
20#include <string.h>
21#include <locale.h>
22#include <X11/cursorfont.h>
23#include <X11/Xatom.h>
24
25#include "options.h"
26#include "util.h"
27#include "window.h"
28#include "icon/data.h"
29
30#define _WINDOW_CONFIG
31#include "config.h"
32
33enum {
34 H_TEXT_PAD = 5,
35 V_TEXT_PAD = 1
36};
37
38static Cursor carrow;
39static Cursor cnone;
40static Cursor chand;
41static Cursor cwatch;
42static GC gc;
43
44static struct {
45 int ascent;
46 int descent;
47 XFontStruct *xfont;
48 XFontSet set;
49} font;
50
51static int fontheight;
52static int barheight;
53
54Atom atoms[ATOM_COUNT];
55
56static Bool fs_support;
57static Bool fs_warned;
58
59void win_init_font(Display *dpy, const char *fontstr)
60{
61 int n;
62 char *def, **missing;
63
64 font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
65 if (missing)
66 XFreeStringList(missing);
67 if (font.set) {
68 XFontStruct **xfonts;
69 char **font_names;
70
71 font.ascent = font.descent = 0;
72 XExtentsOfFontSet(font.set);
73 n = XFontsOfFontSet(font.set, &xfonts, &font_names);
74 while (n--) {
75 font.ascent = MAX(font.ascent, (*xfonts)->ascent);
76 font.descent = MAX(font.descent,(*xfonts)->descent);
77 xfonts++;
78 }
79 } else {
80 if ((font.xfont = XLoadQueryFont(dpy, fontstr)) == NULL &&
81 (font.xfont = XLoadQueryFont(dpy, "fixed")) == NULL)
82 {
83 die("could not load font: %s", fontstr);
84 }
85 font.ascent = font.xfont->ascent;
86 font.descent = font.xfont->descent;
87 }
88 fontheight = font.ascent + font.descent;
89 barheight = fontheight + 2 * V_TEXT_PAD;
90}
91
92unsigned long win_alloc_color(win_t *win, const char *name)
93{
94 XColor col;
95
96 if (XAllocNamedColor(win->env.dpy,
97 DefaultColormap(win->env.dpy, win->env.scr),
98 name, &col, &col) == 0)
99 {
100 die("could not allocate color: %s", name);
101 }
102 return col.pixel;
103}
104
105void win_check_wm_support(Display *dpy, Window root)
106{
107 int format;
108 long offset = 0, length = 16;
109 Atom *data, type;
110 unsigned long i, nitems, bytes_left;
111 Bool found = False;
112
113 while (!found && length > 0) {
114 if (XGetWindowProperty(dpy, root, atoms[ATOM__NET_SUPPORTED],
115 offset, length, False, XA_ATOM, &type, &format,
116 &nitems, &bytes_left, (unsigned char**) &data))
117 {
118 break;
119 }
120 if (type == XA_ATOM && format == 32) {
121 for (i = 0; i < nitems; i++) {
122 if (data[i] == atoms[ATOM__NET_WM_STATE_FULLSCREEN]) {
123 found = True;
124 fs_support = True;
125 break;
126 }
127 }
128 }
129 XFree(data);
130 offset += nitems;
131 length = MIN(length, bytes_left / 4);
132 }
133}
134
135#define INIT_ATOM_(atom) \
136 atoms[ATOM_##atom] = XInternAtom(e->dpy, #atom, False);
137
138void win_init(win_t *win)
139{
140 win_env_t *e;
141
142 memset(win, 0, sizeof(win_t));
143
144 e = &win->env;
145 if ((e->dpy = XOpenDisplay(NULL)) == NULL)
146 die("could not open display");
147
148 e->scr = DefaultScreen(e->dpy);
149 e->scrw = DisplayWidth(e->dpy, e->scr);
150 e->scrh = DisplayHeight(e->dpy, e->scr);
151 e->vis = DefaultVisual(e->dpy, e->scr);
152 e->cmap = DefaultColormap(e->dpy, e->scr);
153 e->depth = DefaultDepth(e->dpy, e->scr);
154
155 if (setlocale(LC_CTYPE, "") == NULL || XSupportsLocale() == 0)
156 warn("no locale support");
157
158 win_init_font(e->dpy, BAR_FONT);
159
160 win->bgcol = win_alloc_color(win, WIN_BG_COLOR);
161 win->fscol = win_alloc_color(win, WIN_FS_COLOR);
162 win->selcol = win_alloc_color(win, SEL_COLOR);
163 win->bar.bgcol = win_alloc_color(win, BAR_BG_COLOR);
164 win->bar.fgcol = win_alloc_color(win, BAR_FG_COLOR);
165
166 win->bar.l.size = BAR_L_LEN;
167 win->bar.r.size = BAR_R_LEN;
168 win->bar.l.buf = s_malloc(win->bar.l.size);
169 win->bar.r.buf = s_malloc(win->bar.r.size);
170 win->bar.h = options->hide_bar ? 0 : barheight;
171
172 INIT_ATOM_(WM_DELETE_WINDOW);
173 INIT_ATOM_(_NET_WM_NAME);
174 INIT_ATOM_(_NET_WM_ICON_NAME);
175 INIT_ATOM_(_NET_WM_ICON);
176 INIT_ATOM_(_NET_WM_STATE);
177 INIT_ATOM_(_NET_WM_STATE_FULLSCREEN);
178 INIT_ATOM_(_NET_SUPPORTED);
179
180 win_check_wm_support(e->dpy, RootWindow(e->dpy, e->scr));
181}
182
183void win_open(win_t *win)
184{
185 int c, i, j, n;
186 win_env_t *e;
187 XClassHint classhint;
188 unsigned long *icon_data;
189 XColor col;
190 char none_data[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
191 Pixmap none;
192 int gmask;
193 XSizeHints sizehints;
194 Bool fullscreen = options->fullscreen && fs_support;
195
196 e = &win->env;
197
198 sizehints.flags = PWinGravity;
199 sizehints.win_gravity = NorthWestGravity;
200
201 /* determine window offsets, width & height */
202 if (options->geometry == NULL)
203 gmask = 0;
204 else
205 gmask = XParseGeometry(options->geometry, &win->x, &win->y,
206 &win->w, &win->h);
207 if ((gmask & WidthValue) != 0)
208 sizehints.flags |= USSize;
209 else
210 win->w = WIN_WIDTH;
211 if ((gmask & HeightValue) != 0)
212 sizehints.flags |= USSize;
213 else
214 win->h = WIN_HEIGHT;
215 if ((gmask & XValue) != 0) {
216 if ((gmask & XNegative) != 0) {
217 win->x += e->scrw - win->w;
218 sizehints.win_gravity = NorthEastGravity;
219 }
220 sizehints.flags |= USPosition;
221 } else {
222 win->x = 0;
223 }
224 if ((gmask & YValue) != 0) {
225 if ((gmask & YNegative) != 0) {
226 win->y += e->scrh - win->h;
227 sizehints.win_gravity = sizehints.win_gravity == NorthEastGravity
228 ? SouthEastGravity : SouthWestGravity;
229 }
230 sizehints.flags |= USPosition;
231 } else {
232 win->y = 0;
233 }
234
235 win->xwin = XCreateWindow(e->dpy, RootWindow(e->dpy, e->scr),
236 win->x, win->y, win->w, win->h, 0,
237 e->depth, InputOutput, e->vis, 0, NULL);
238 if (win->xwin == None)
239 die("could not create window");
240
241 XSelectInput(e->dpy, win->xwin,
242 ButtonReleaseMask | ButtonPressMask | KeyPressMask |
243 PointerMotionMask | StructureNotifyMask);
244
245 carrow = XCreateFontCursor(e->dpy, XC_left_ptr);
246 chand = XCreateFontCursor(e->dpy, XC_fleur);
247 cwatch = XCreateFontCursor(e->dpy, XC_watch);
248
249 if (XAllocNamedColor(e->dpy, DefaultColormap(e->dpy, e->scr), "black",
250 &col, &col) == 0)
251 {
252 die("could not allocate color: black");
253 }
254 none = XCreateBitmapFromData(e->dpy, win->xwin, none_data, 8, 8);
255 cnone = XCreatePixmapCursor(e->dpy, none, none, &col, &col, 0, 0);
256
257 gc = XCreateGC(e->dpy, win->xwin, 0, None);
258
259 n = icons[ARRLEN(icons)-1].size;
260 icon_data = s_malloc((n * n + 2) * sizeof(*icon_data));
261
262 for (i = 0; i < ARRLEN(icons); i++) {
263 n = 0;
264 icon_data[n++] = icons[i].size;
265 icon_data[n++] = icons[i].size;
266
267 for (j = 0; j < icons[i].cnt; j++) {
268 for (c = icons[i].data[j] >> 4; c >= 0; c--)
269 icon_data[n++] = icon_colors[icons[i].data[j] & 0x0F];
270 }
271 XChangeProperty(e->dpy, win->xwin,
272 atoms[ATOM__NET_WM_ICON], XA_CARDINAL, 32,
273 i == 0 ? PropModeReplace : PropModeAppend,
274 (unsigned char *) icon_data, n);
275 }
276 free(icon_data);
277
278 win_set_title(win, "sxiv");
279
280 classhint.res_class = "Sxiv";
281 classhint.res_name = options->res_name != NULL ? options->res_name : "sxiv";
282 XSetClassHint(e->dpy, win->xwin, &classhint);
283
284 XSetWMProtocols(e->dpy, win->xwin, &atoms[ATOM_WM_DELETE_WINDOW], 1);
285
286 sizehints.width = win->w;
287 sizehints.height = win->h;
288 sizehints.x = win->x;
289 sizehints.y = win->y;
290 XSetWMNormalHints(win->env.dpy, win->xwin, &sizehints);
291
292 win->h -= win->bar.h;
293
294 win->buf.w = e->scrw;
295 win->buf.h = e->scrh;
296 win->buf.pm = XCreatePixmap(e->dpy, win->xwin,
297 win->buf.w, win->buf.h, e->depth);
298 XSetForeground(e->dpy, gc, fullscreen ? win->fscol : win->bgcol);
299 XFillRectangle(e->dpy, win->buf.pm, gc, 0, 0, win->buf.w, win->buf.h);
300 XSetWindowBackgroundPixmap(e->dpy, win->xwin, win->buf.pm);
301
302 XMapWindow(e->dpy, win->xwin);
303 XFlush(e->dpy);
304
305 if (fullscreen)
306 win_toggle_fullscreen(win);
307}
308
309void win_close(win_t *win)
310{
311 XFreeCursor(win->env.dpy, carrow);
312 XFreeCursor(win->env.dpy, cnone);
313 XFreeCursor(win->env.dpy, chand);
314 XFreeCursor(win->env.dpy, cwatch);
315
316 XFreeGC(win->env.dpy, gc);
317
318 XDestroyWindow(win->env.dpy, win->xwin);
319 XCloseDisplay(win->env.dpy);
320}
321
322bool win_configure(win_t *win, XConfigureEvent *c)
323{
324 bool changed;
325
326 changed = win->w != c->width || win->h + win->bar.h != c->height;
327
328 win->x = c->x;
329 win->y = c->y;
330 win->w = c->width;
331 win->h = c->height - win->bar.h;
332 win->bw = c->border_width;
333
334 return changed;
335}
336
337void win_toggle_fullscreen(win_t *win)
338{
339 XEvent ev;
340 XClientMessageEvent *cm;
341
342 if (!fs_support) {
343 if (!fs_warned) {
344 warn("window manager does not support fullscreen");
345 fs_warned = True;
346 }
347 return;
348 }
349 win->fullscreen = !win->fullscreen;
350
351 memset(&ev, 0, sizeof(ev));
352 ev.type = ClientMessage;
353
354 cm = &ev.xclient;
355 cm->window = win->xwin;
356 cm->message_type = atoms[ATOM__NET_WM_STATE];
357 cm->format = 32;
358 cm->data.l[0] = win->fullscreen;
359 cm->data.l[1] = atoms[ATOM__NET_WM_STATE_FULLSCREEN];
360 cm->data.l[2] = cm->data.l[3] = 0;
361
362 XSendEvent(win->env.dpy, DefaultRootWindow(win->env.dpy), False,
363 SubstructureNotifyMask | SubstructureRedirectMask, &ev);
364}
365
366void win_toggle_bar(win_t *win)
367{
368 if (win->bar.h != 0) {
369 win->h += win->bar.h;
370 win->bar.h = 0;
371 } else {
372 win->bar.h = barheight;
373 win->h -= win->bar.h;
374 }
375}
376
377void win_clear(win_t *win)
378{
379 win_env_t *e;
380
381 e = &win->env;
382
383 if (win->w > win->buf.w || win->h + win->bar.h > win->buf.h) {
384 XFreePixmap(e->dpy, win->buf.pm);
385 win->buf.w = MAX(win->buf.w, win->w);
386 win->buf.h = MAX(win->buf.h, win->h + win->bar.h);
387 win->buf.pm = XCreatePixmap(e->dpy, win->xwin,
388 win->buf.w, win->buf.h, e->depth);
389 }
390 XSetForeground(e->dpy, gc, win->fullscreen ? win->fscol : win->bgcol);
391 XFillRectangle(e->dpy, win->buf.pm, gc, 0, 0, win->buf.w, win->buf.h);
392}
393
394void win_draw_bar(win_t *win)
395{
396 int len, olen, x, y, w, tw;
397 char rest[3];
398 const char *dots = "...";
399 win_env_t *e;
400 win_bar_t *l, *r;
401
402 if ((l = &win->bar.l)->buf == NULL || (r = &win->bar.r)->buf == NULL)
403 return;
404
405 e = &win->env;
406 y = win->h + font.ascent + V_TEXT_PAD;
407 w = win->w;
408
409 XSetForeground(e->dpy, gc, win->bar.bgcol);
410 XFillRectangle(e->dpy, win->buf.pm, gc, 0, win->h, win->w, win->bar.h);
411
412 XSetForeground(e->dpy, gc, win->bar.fgcol);
413 XSetBackground(e->dpy, gc, win->bar.bgcol);
414
415 if ((len = strlen(r->buf)) > 0) {
416 if ((tw = win_textwidth(r->buf, len, true)) > w)
417 return;
418 x = win->w - tw + H_TEXT_PAD;
419 w -= tw;
420 if (font.set)
421 XmbDrawString(e->dpy, win->buf.pm, font.set, gc, x, y, r->buf, len);
422 else
423 XDrawString(e->dpy, win->buf.pm, gc, x, y, r->buf, len);
424 }
425 if ((len = strlen(l->buf)) > 0) {
426 olen = len;
427 while (len > 0 && (tw = win_textwidth(l->buf, len, true)) > w)
428 len--;
429 if (len > 0) {
430 if (len != olen) {
431 w = strlen(dots);
432 if (len <= w)
433 return;
434 memcpy(rest, l->buf + len - w, w);
435 memcpy(l->buf + len - w, dots, w);
436 }
437 x = H_TEXT_PAD;
438 if (font.set)
439 XmbDrawString(e->dpy, win->buf.pm, font.set, gc, x, y, l->buf, len);
440 else
441 XDrawString(e->dpy, win->buf.pm, gc, x, y, l->buf, len);
442 if (len != olen)
443 memcpy(l->buf + len - w, rest, w);
444 }
445 }
446}
447
448void win_draw(win_t *win)
449{
450 if (win->bar.h > 0)
451 win_draw_bar(win);
452
453 XSetWindowBackgroundPixmap(win->env.dpy, win->xwin, win->buf.pm);
454 XClearWindow(win->env.dpy, win->xwin);
455 XFlush(win->env.dpy);
456}
457
458void win_draw_rect(win_t *win, int x, int y, int w, int h, bool fill, int lw,
459 unsigned long col)
460{
461 XGCValues gcval;
462
463 gcval.line_width = lw;
464 gcval.foreground = col;
465 XChangeGC(win->env.dpy, gc, GCForeground | GCLineWidth, &gcval);
466
467 if (fill)
468 XFillRectangle(win->env.dpy, win->buf.pm, gc, x, y, w, h);
469 else
470 XDrawRectangle(win->env.dpy, win->buf.pm, gc, x, y, w, h);
471}
472
473int win_textwidth(const char *text, unsigned int len, bool with_padding)
474{
475 XRectangle r;
476 int padding = with_padding ? 2 * H_TEXT_PAD : 0;
477
478 if (font.set) {
479 XmbTextExtents(font.set, text, len, NULL, &r);
480 return r.width + padding;
481 } else {
482 return XTextWidth(font.xfont, text, len) + padding;
483 }
484}
485
486void win_set_title(win_t *win, const char *title)
487{
488 if (title == NULL)
489 title = "sxiv";
490
491 XStoreName(win->env.dpy, win->xwin, title);
492 XSetIconName(win->env.dpy, win->xwin, title);
493
494 XChangeProperty(win->env.dpy, win->xwin, atoms[ATOM__NET_WM_NAME],
495 XInternAtom(win->env.dpy, "UTF8_STRING", False), 8,
496 PropModeReplace, (unsigned char *) title, strlen(title));
497 XChangeProperty(win->env.dpy, win->xwin, atoms[ATOM__NET_WM_ICON_NAME],
498 XInternAtom(win->env.dpy, "UTF8_STRING", False), 8,
499 PropModeReplace, (unsigned char *) title, strlen(title));
500}
501
502void win_set_cursor(win_t *win, cursor_t cursor)
503{
504 switch (cursor) {
505 case CURSOR_NONE:
506 XDefineCursor(win->env.dpy, win->xwin, cnone);
507 break;
508 case CURSOR_HAND:
509 XDefineCursor(win->env.dpy, win->xwin, chand);
510 break;
511 case CURSOR_WATCH:
512 XDefineCursor(win->env.dpy, win->xwin, cwatch);
513 break;
514 case CURSOR_ARROW:
515 default:
516 XDefineCursor(win->env.dpy, win->xwin, carrow);
517 break;
518 }
519
520 XFlush(win->env.dpy);
521}