Krolyxon
Commit 30e2356

Branches

Refs →

nsxivwindow.c

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