Krolyxon
Commit e0e9697

Branches

Refs →

nsxivwindow.c

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