Krolyxon
Commit d69f39e

Branches

Refs →

nsxivwindow.c

479 lines · 12.4 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 win_env_t *e;
152 XClassHint classhint;
153 unsigned long *icon_data;
154 XColor col;
155 char none_data[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
156 Pixmap none;
157 int gmask;
158 XSizeHints sizehints;
159 Bool fullscreen = options->fullscreen && fs_support;
160
161 e = &win->env;
162
163 sizehints.flags = PWinGravity;
164 sizehints.win_gravity = NorthWestGravity;
165
166 /* determine window offsets, width & height */
167 if (options->geometry == NULL)
168 gmask = 0;
169 else
170 gmask = XParseGeometry(options->geometry, &win->x, &win->y,
171 &win->w, &win->h);
172 if ((gmask & WidthValue) != 0)
173 sizehints.flags |= USSize;
174 else
175 win->w = WIN_WIDTH;
176 if ((gmask & HeightValue) != 0)
177 sizehints.flags |= USSize;
178 else
179 win->h = WIN_HEIGHT;
180 if ((gmask & XValue) != 0) {
181 if ((gmask & XNegative) != 0) {
182 win->x += e->scrw - win->w;
183 sizehints.win_gravity = NorthEastGravity;
184 }
185 sizehints.flags |= USPosition;
186 } else {
187 win->x = 0;
188 }
189 if ((gmask & YValue) != 0) {
190 if ((gmask & YNegative) != 0) {
191 win->y += e->scrh - win->h;
192 sizehints.win_gravity = sizehints.win_gravity == NorthEastGravity
193 ? SouthEastGravity : SouthWestGravity;
194 }
195 sizehints.flags |= USPosition;
196 } else {
197 win->y = 0;
198 }
199
200 win->xwin = XCreateWindow(e->dpy, RootWindow(e->dpy, e->scr),
201 win->x, win->y, win->w, win->h, 0,
202 e->depth, InputOutput, e->vis, 0, NULL);
203 if (win->xwin == None)
204 error(EXIT_FAILURE, 0, "Error creating X window");
205
206 XSelectInput(e->dpy, win->xwin,
207 ButtonReleaseMask | ButtonPressMask | KeyPressMask |
208 PointerMotionMask | StructureNotifyMask);
209
210 carrow = XCreateFontCursor(e->dpy, XC_left_ptr);
211 chand = XCreateFontCursor(e->dpy, XC_fleur);
212 cwatch = XCreateFontCursor(e->dpy, XC_watch);
213
214 if (XAllocNamedColor(e->dpy, DefaultColormap(e->dpy, e->scr), "black",
215 &col, &col) == 0)
216 {
217 error(EXIT_FAILURE, 0, "Error allocating color 'black'");
218 }
219 none = XCreateBitmapFromData(e->dpy, win->xwin, none_data, 8, 8);
220 cnone = XCreatePixmapCursor(e->dpy, none, none, &col, &col, 0, 0);
221
222 gc = XCreateGC(e->dpy, win->xwin, 0, None);
223
224 n = icons[ARRLEN(icons)-1].size;
225 icon_data = emalloc((n * n + 2) * sizeof(*icon_data));
226
227 for (i = 0; i < ARRLEN(icons); i++) {
228 n = 0;
229 icon_data[n++] = icons[i].size;
230 icon_data[n++] = icons[i].size;
231
232 for (j = 0; j < icons[i].cnt; j++) {
233 for (c = icons[i].data[j] >> 4; c >= 0; c--)
234 icon_data[n++] = icon_colors[icons[i].data[j] & 0x0F];
235 }
236 XChangeProperty(e->dpy, win->xwin,
237 atoms[ATOM__NET_WM_ICON], XA_CARDINAL, 32,
238 i == 0 ? PropModeReplace : PropModeAppend,
239 (unsigned char *) icon_data, n);
240 }
241 free(icon_data);
242
243 win_set_title(win, "sxiv");
244
245 classhint.res_class = "Sxiv";
246 classhint.res_name = options->res_name != NULL ? options->res_name : "sxiv";
247 XSetClassHint(e->dpy, win->xwin, &classhint);
248
249 XSetWMProtocols(e->dpy, win->xwin, &atoms[ATOM_WM_DELETE_WINDOW], 1);
250
251 sizehints.width = win->w;
252 sizehints.height = win->h;
253 sizehints.x = win->x;
254 sizehints.y = win->y;
255 XSetWMNormalHints(win->env.dpy, win->xwin, &sizehints);
256
257 win->h -= win->bar.h;
258
259 win->buf.w = e->scrw;
260 win->buf.h = e->scrh;
261 win->buf.pm = XCreatePixmap(e->dpy, win->xwin,
262 win->buf.w, win->buf.h, e->depth);
263 XSetForeground(e->dpy, gc, fullscreen ? win->fscol.pixel : win->bgcol.pixel);
264 XFillRectangle(e->dpy, win->buf.pm, gc, 0, 0, win->buf.w, win->buf.h);
265 XSetWindowBackgroundPixmap(e->dpy, win->xwin, win->buf.pm);
266
267 XMapWindow(e->dpy, win->xwin);
268 XFlush(e->dpy);
269
270 if (fullscreen)
271 win_toggle_fullscreen(win);
272}
273
274CLEANUP void win_close(win_t *win)
275{
276 XFreeCursor(win->env.dpy, carrow);
277 XFreeCursor(win->env.dpy, cnone);
278 XFreeCursor(win->env.dpy, chand);
279 XFreeCursor(win->env.dpy, cwatch);
280
281 XFreeGC(win->env.dpy, gc);
282
283 XDestroyWindow(win->env.dpy, win->xwin);
284 XCloseDisplay(win->env.dpy);
285}
286
287bool win_configure(win_t *win, XConfigureEvent *c)
288{
289 bool changed;
290
291 changed = win->w != c->width || win->h + win->bar.h != c->height;
292
293 win->x = c->x;
294 win->y = c->y;
295 win->w = c->width;
296 win->h = c->height - win->bar.h;
297 win->bw = c->border_width;
298
299 return changed;
300}
301
302void win_toggle_fullscreen(win_t *win)
303{
304 XEvent ev;
305 XClientMessageEvent *cm;
306
307 if (!fs_support) {
308 if (!fs_warned) {
309 error(0, 0, "No fullscreen support");
310 fs_warned = True;
311 }
312 return;
313 }
314 win->fullscreen = !win->fullscreen;
315
316 memset(&ev, 0, sizeof(ev));
317 ev.type = ClientMessage;
318
319 cm = &ev.xclient;
320 cm->window = win->xwin;
321 cm->message_type = atoms[ATOM__NET_WM_STATE];
322 cm->format = 32;
323 cm->data.l[0] = win->fullscreen;
324 cm->data.l[1] = atoms[ATOM__NET_WM_STATE_FULLSCREEN];
325 cm->data.l[2] = cm->data.l[3] = 0;
326
327 XSendEvent(win->env.dpy, DefaultRootWindow(win->env.dpy), False,
328 SubstructureNotifyMask | SubstructureRedirectMask, &ev);
329}
330
331void win_toggle_bar(win_t *win)
332{
333 if (win->bar.h != 0) {
334 win->h += win->bar.h;
335 win->bar.h = 0;
336 } else {
337 win->bar.h = barheight;
338 win->h -= win->bar.h;
339 }
340}
341
342void win_clear(win_t *win)
343{
344 win_env_t *e;
345
346 e = &win->env;
347
348 if (win->w > win->buf.w || win->h + win->bar.h > win->buf.h) {
349 XFreePixmap(e->dpy, win->buf.pm);
350 win->buf.w = MAX(win->buf.w, win->w);
351 win->buf.h = MAX(win->buf.h, win->h + win->bar.h);
352 win->buf.pm = XCreatePixmap(e->dpy, win->xwin,
353 win->buf.w, win->buf.h, e->depth);
354 }
355 XSetForeground(e->dpy, gc, win->fullscreen ? win->fscol.pixel : win->bgcol.pixel);
356 XFillRectangle(e->dpy, win->buf.pm, gc, 0, 0, win->buf.w, win->buf.h);
357}
358
359void win_draw_bar(win_t *win)
360{
361 int len, olen, x, y, w, tw;
362 char rest[3];
363 const char *dots = "...";
364 win_env_t *e;
365 win_bar_t *l, *r;
366 XftDraw *d;
367
368 if ((l = &win->bar.l)->buf == NULL || (r = &win->bar.r)->buf == NULL)
369 return;
370
371 e = &win->env;
372 y = win->h + font->ascent + V_TEXT_PAD;
373 w = win->w;
374 d = XftDrawCreate(e->dpy, win->buf.pm, DefaultVisual(e->dpy, e->scr),
375 DefaultColormap(e->dpy, e->scr));
376
377 XSetForeground(e->dpy, gc, win->bar.bgcol.pixel);
378 XFillRectangle(e->dpy, win->buf.pm, gc, 0, win->h, win->w, win->bar.h);
379
380 XSetForeground(e->dpy, gc, win->bar.fgcol.pixel);
381 XSetBackground(e->dpy, gc, win->bar.bgcol.pixel);
382
383 if ((len = strlen(r->buf)) > 0) {
384 if ((tw = win_textwidth(e, r->buf, len, true)) > w)
385 return;
386 x = win->w - tw + H_TEXT_PAD;
387 w -= tw;
388 XftDrawStringUtf8(d, &win->bar.fgcol, font, x, y, (XftChar8*)r->buf, len);
389 }
390 if ((len = strlen(l->buf)) > 0) {
391 olen = len;
392 while (len > 0 && (tw = win_textwidth(e, l->buf, len, true)) > w)
393 len--;
394 if (len > 0) {
395 if (len != olen) {
396 w = strlen(dots);
397 if (len <= w)
398 return;
399 memcpy(rest, l->buf + len - w, w);
400 memcpy(l->buf + len - w, dots, w);
401 }
402 x = H_TEXT_PAD;
403 XftDrawStringUtf8(d, &win->bar.fgcol, font, x, y, (XftChar8*)l->buf, len);
404 if (len != olen)
405 memcpy(l->buf + len - w, rest, w);
406 }
407 }
408 XftDrawDestroy(d);
409}
410
411void win_draw(win_t *win)
412{
413 if (win->bar.h > 0)
414 win_draw_bar(win);
415
416 XSetWindowBackgroundPixmap(win->env.dpy, win->xwin, win->buf.pm);
417 XClearWindow(win->env.dpy, win->xwin);
418 XFlush(win->env.dpy);
419}
420
421void win_draw_rect(win_t *win, int x, int y, int w, int h, bool fill, int lw,
422 unsigned long col)
423{
424 XGCValues gcval;
425
426 gcval.line_width = lw;
427 gcval.foreground = col;
428 XChangeGC(win->env.dpy, gc, GCForeground | GCLineWidth, &gcval);
429
430 if (fill)
431 XFillRectangle(win->env.dpy, win->buf.pm, gc, x, y, w, h);
432 else
433 XDrawRectangle(win->env.dpy, win->buf.pm, gc, x, y, w, h);
434}
435
436int win_textwidth(const win_env_t *e, const char *text, unsigned int len, bool with_padding)
437{
438 XGlyphInfo ext;
439
440 XftTextExtentsUtf8(e->dpy, font, (XftChar8*)text, len, &ext);
441 return ext.xOff + (with_padding ? 2 * H_TEXT_PAD : 0);
442}
443
444void win_set_title(win_t *win, const char *title)
445{
446 if (title == NULL)
447 title = "sxiv";
448
449 XStoreName(win->env.dpy, win->xwin, title);
450 XSetIconName(win->env.dpy, win->xwin, title);
451
452 XChangeProperty(win->env.dpy, win->xwin, atoms[ATOM__NET_WM_NAME],
453 XInternAtom(win->env.dpy, "UTF8_STRING", False), 8,
454 PropModeReplace, (unsigned char *) title, strlen(title));
455 XChangeProperty(win->env.dpy, win->xwin, atoms[ATOM__NET_WM_ICON_NAME],
456 XInternAtom(win->env.dpy, "UTF8_STRING", False), 8,
457 PropModeReplace, (unsigned char *) title, strlen(title));
458}
459
460void win_set_cursor(win_t *win, cursor_t cursor)
461{
462 switch (cursor) {
463 case CURSOR_NONE:
464 XDefineCursor(win->env.dpy, win->xwin, cnone);
465 break;
466 case CURSOR_HAND:
467 XDefineCursor(win->env.dpy, win->xwin, chand);
468 break;
469 case CURSOR_WATCH:
470 XDefineCursor(win->env.dpy, win->xwin, cwatch);
471 break;
472 case CURSOR_ARROW:
473 default:
474 XDefineCursor(win->env.dpy, win->xwin, carrow);
475 break;
476 }
477
478 XFlush(win->env.dpy);
479}