Krolyxon
Commit ee908ca

Branches

Refs →

nsxivwindow.c

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