Krolyxon
Commit 8081cbe

Branches

Refs →

nsxivwindow.c

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