Krolyxon
Commit a46006f

Branches

Refs →

nsxivwindow.c

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