Krolyxon
Commit f6510b0

Branches

Refs →

nsxivwindow.c

575 lines · 13.9 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#define _POSIX_C_SOURCE 200112L
20#define _WINDOW_CONFIG
21
22#include <stdlib.h>
23#include <string.h>
24#include <locale.h>
25#include <X11/cursorfont.h>
26#include <X11/Xatom.h>
27
28#include "options.h"
29#include "util.h"
30#include "window.h"
31#include "config.h"
32#include "icon/data.h"
33
34enum {
35 H_TEXT_PAD = 5,
36 V_TEXT_PAD = 1
37};
38
39static Cursor carrow;
40static Cursor cnone;
41static Cursor chand;
42static Cursor cwatch;
43static GC gc;
44
45Atom wm_delete_win;
46
47static struct {
48 int ascent;
49 int descent;
50 XFontStruct *xfont;
51 XFontSet set;
52} font;
53
54static int fontheight;
55static int barheight;
56
57void win_init_font(Display *dpy, const char *fontstr)
58{
59 int n;
60 char *def, **missing;
61
62 font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
63 if (missing)
64 XFreeStringList(missing);
65 if (font.set) {
66 XFontStruct **xfonts;
67 char **font_names;
68
69 font.ascent = font.descent = 0;
70 XExtentsOfFontSet(font.set);
71 n = XFontsOfFontSet(font.set, &xfonts, &font_names);
72 while (n--) {
73 font.ascent = MAX(font.ascent, (*xfonts)->ascent);
74 font.descent = MAX(font.descent,(*xfonts)->descent);
75 xfonts++;
76 }
77 } else {
78 if ((font.xfont = XLoadQueryFont(dpy, fontstr)) == NULL &&
79 (font.xfont = XLoadQueryFont(dpy, "fixed")) == NULL)
80 {
81 die("could not load font: %s", fontstr);
82 }
83 font.ascent = font.xfont->ascent;
84 font.descent = font.xfont->descent;
85 }
86 fontheight = font.ascent + font.descent;
87 barheight = fontheight + 2 * V_TEXT_PAD;
88}
89
90unsigned long win_alloc_color(win_t *win, const char *name)
91{
92 XColor col;
93
94 if (win == NULL)
95 return 0UL;
96 if (XAllocNamedColor(win->env.dpy,
97 DefaultColormap(win->env.dpy, win->env.scr),
98 name, &col, &col) == 0)
99 {
100 die("could not allocate color: %s", name);
101 }
102 return col.pixel;
103}
104
105void win_init(win_t *win)
106{
107 win_env_t *e;
108
109 if (win == NULL)
110 return;
111
112 memset(win, 0, sizeof(win_t));
113
114 e = &win->env;
115 if ((e->dpy = XOpenDisplay(NULL)) == NULL)
116 die("could not open display");
117
118 e->scr = DefaultScreen(e->dpy);
119 e->scrw = DisplayWidth(e->dpy, e->scr);
120 e->scrh = DisplayHeight(e->dpy, e->scr);
121 e->vis = DefaultVisual(e->dpy, e->scr);
122 e->cmap = DefaultColormap(e->dpy, e->scr);
123 e->depth = DefaultDepth(e->dpy, e->scr);
124
125 if (setlocale(LC_CTYPE, "") == NULL || XSupportsLocale() == 0)
126 warn("no locale support");
127
128 win_init_font(e->dpy, BAR_FONT);
129
130 win->white = WhitePixel(e->dpy, e->scr);
131 win->bgcol = win_alloc_color(win, WIN_BG_COLOR);
132 win->fscol = win_alloc_color(win, WIN_FS_COLOR);
133 win->selcol = win_alloc_color(win, SEL_COLOR);
134 win->bar.bgcol = win_alloc_color(win, BAR_BG_COLOR);
135 win->bar.fgcol = win_alloc_color(win, BAR_FG_COLOR);
136 win->bar.h = options->hide_bar ? 0 : barheight;
137
138 win->sizehints.flags = PWinGravity;
139 win->sizehints.win_gravity = NorthWestGravity;
140 if (options->fixed_win)
141 /* actual min/max values set in win_update_sizehints() */
142 win->sizehints.flags |= PMinSize | PMaxSize;
143
144 wm_delete_win = XInternAtom(e->dpy, "WM_DELETE_WINDOW", False);
145}
146
147void win_update_sizehints(win_t *win)
148{
149 if (win == NULL || win->xwin == None)
150 return;
151
152 if ((win->sizehints.flags & USSize) != 0) {
153 win->sizehints.width = win->w;
154 win->sizehints.height = win->h + win->bar.h;
155 }
156 if ((win->sizehints.flags & USPosition) != 0) {
157 win->sizehints.x = win->x;
158 win->sizehints.y = win->y;
159 }
160 if (options->fixed_win) {
161 win->sizehints.min_width = win->sizehints.max_width = win->w;
162 win->sizehints.min_height = win->sizehints.max_height = win->h + win->bar.h;
163 }
164 XSetWMNormalHints(win->env.dpy, win->xwin, &win->sizehints);
165}
166
167void win_open(win_t *win)
168{
169 int c, i, j, n;
170 win_env_t *e;
171 XClassHint classhint;
172 XSetWindowAttributes attr;
173 unsigned long attr_mask;
174 unsigned long *icon_data;
175 XColor col;
176 char none_data[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
177 Pixmap none;
178 int gmask;
179
180 if (win == NULL)
181 return;
182
183 e = &win->env;
184
185 /* determine window offsets, width & height */
186 if (options->geometry == NULL)
187 gmask = 0;
188 else
189 gmask = XParseGeometry(options->geometry, &win->x, &win->y,
190 &win->w, &win->h);
191 if ((gmask & WidthValue) != 0)
192 win->sizehints.flags |= USSize;
193 else
194 win->w = WIN_WIDTH;
195 if ((gmask & HeightValue) != 0)
196 win->sizehints.flags |= USSize;
197 else
198 win->h = WIN_HEIGHT;
199 if ((gmask & XValue) != 0) {
200 if ((gmask & XNegative) != 0) {
201 win->x += e->scrw - win->w;
202 win->sizehints.win_gravity = NorthEastGravity;
203 }
204 win->sizehints.flags |= USPosition;
205 } else {
206 win->x = (e->scrw - win->w) / 2;
207 }
208 if ((gmask & YValue) != 0) {
209 if ((gmask & YNegative) != 0) {
210 win->y += e->scrh - win->h;
211 if (win->sizehints.win_gravity == NorthEastGravity)
212 win->sizehints.win_gravity = SouthEastGravity;
213 else
214 win->sizehints.win_gravity = SouthWestGravity;
215 }
216 win->sizehints.flags |= USPosition;
217 } else {
218 win->y = (e->scrh - win->h) / 2;
219 }
220
221 attr.background_pixel = win->bgcol;
222 attr_mask = CWBackPixel;
223
224 win->xwin = XCreateWindow(e->dpy, RootWindow(e->dpy, e->scr),
225 win->x, win->y, win->w, win->h, 0,
226 e->depth, InputOutput, e->vis, attr_mask, &attr);
227 if (win->xwin == None)
228 die("could not create window");
229
230 XSelectInput(e->dpy, win->xwin,
231 ExposureMask | ButtonReleaseMask | ButtonPressMask |
232 KeyPressMask | PointerMotionMask | StructureNotifyMask);
233
234 carrow = XCreateFontCursor(e->dpy, XC_left_ptr);
235 chand = XCreateFontCursor(e->dpy, XC_fleur);
236 cwatch = XCreateFontCursor(e->dpy, XC_watch);
237
238 if (XAllocNamedColor(e->dpy, DefaultColormap(e->dpy, e->scr), "black",
239 &col, &col) == 0)
240 {
241 die("could not allocate color: black");
242 }
243 none = XCreateBitmapFromData(e->dpy, win->xwin, none_data, 8, 8);
244 cnone = XCreatePixmapCursor(e->dpy, none, none, &col, &col, 0, 0);
245
246 gc = XCreateGC(e->dpy, win->xwin, 0, None);
247
248 n = icons[ARRLEN(icons)-1].size;
249 icon_data = s_malloc((n * n + 2) * sizeof(*icon_data));
250
251 for (i = 0; i < ARRLEN(icons); i++) {
252 n = 0;
253 icon_data[n++] = icons[i].size;
254 icon_data[n++] = icons[i].size;
255
256 for (j = 0; j < icons[i].cnt; j++) {
257 for (c = icons[i].data[j] >> 4; c >= 0; c--)
258 icon_data[n++] = icon_colors[icons[i].data[j] & 0x0F];
259 }
260 XChangeProperty(e->dpy, win->xwin,
261 XInternAtom(e->dpy, "_NET_WM_ICON", False),
262 XA_CARDINAL, 32,
263 i == 0 ? PropModeReplace : PropModeAppend,
264 (unsigned char *) icon_data, n);
265 }
266 free(icon_data);
267
268 win_set_title(win, "sxiv");
269
270 classhint.res_class = "Sxiv";
271 classhint.res_name = options->res_name != NULL ? options->res_name : "sxiv";
272 XSetClassHint(e->dpy, win->xwin, &classhint);
273
274 XSetWMProtocols(e->dpy, win->xwin, &wm_delete_win, 1);
275
276 win->h -= win->bar.h;
277 win_update_sizehints(win);
278
279 XMapWindow(e->dpy, win->xwin);
280 XFlush(e->dpy);
281
282 if (options->fullscreen)
283 win_toggle_fullscreen(win);
284}
285
286void win_close(win_t *win)
287{
288 if (win == NULL || win->xwin == None)
289 return;
290
291 XFreeCursor(win->env.dpy, carrow);
292 XFreeCursor(win->env.dpy, cnone);
293 XFreeCursor(win->env.dpy, chand);
294 XFreeCursor(win->env.dpy, cwatch);
295
296 XFreeGC(win->env.dpy, gc);
297
298 XDestroyWindow(win->env.dpy, win->xwin);
299 XCloseDisplay(win->env.dpy);
300}
301
302bool win_configure(win_t *win, XConfigureEvent *c)
303{
304 bool changed;
305
306 if (win == NULL || c == NULL)
307 return false;
308
309 if ((changed = win->w != c->width || win->h + win->bar.h != c->height)) {
310 if (win->pm != None) {
311 XFreePixmap(win->env.dpy, win->pm);
312 win->pm = None;
313 }
314 }
315
316 win->x = c->x;
317 win->y = c->y;
318 win->w = c->width;
319 win->h = c->height - win->bar.h;
320 win->bw = c->border_width;
321
322 return changed;
323}
324
325void win_expose(win_t *win, XExposeEvent *e)
326{
327 if (win == NULL || win->xwin == None || win->pm == None || e == NULL)
328 return;
329
330 XCopyArea(win->env.dpy, win->pm, win->xwin, gc,
331 e->x, e->y, e->width, e->height, e->x, e->y);
332}
333
334bool win_moveresize(win_t *win, int x, int y, unsigned int w, unsigned int h)
335{
336 if (win == NULL || win->xwin == None)
337 return false;
338
339 /* caller knows nothing about the bar */
340 h += win->bar.h;
341
342 x = MAX(0, x);
343 y = MAX(0, y);
344 w = MIN(w, win->env.scrw - 2 * win->bw);
345 h = MIN(h, win->env.scrh - 2 * win->bw);
346
347 if (win->x == x && win->y == y && win->w == w && win->h + win->bar.h == h)
348 return false;
349
350 win->x = x;
351 win->y = y;
352 win->w = w;
353 win->h = h - win->bar.h;
354
355 win_update_sizehints(win);
356
357 XMoveResizeWindow(win->env.dpy, win->xwin, x, y, w, h);
358
359 if (win->pm != None) {
360 XFreePixmap(win->env.dpy, win->pm);
361 win->pm = None;
362 }
363
364 return true;
365}
366
367void win_toggle_fullscreen(win_t *win)
368{
369 XEvent ev;
370 XClientMessageEvent *cm;
371
372 if (win == NULL || win->xwin == None)
373 return;
374
375 win->fullscreen = !win->fullscreen;
376
377 memset(&ev, 0, sizeof(ev));
378 ev.type = ClientMessage;
379
380 cm = &ev.xclient;
381 cm->window = win->xwin;
382 cm->message_type = XInternAtom(win->env.dpy, "_NET_WM_STATE", False);
383 cm->format = 32;
384 cm->data.l[0] = win->fullscreen;
385 cm->data.l[1] = XInternAtom(win->env.dpy, "_NET_WM_STATE_FULLSCREEN", False);
386 cm->data.l[2] = cm->data.l[3] = 0;
387
388 XSendEvent(win->env.dpy, DefaultRootWindow(win->env.dpy), False,
389 SubstructureNotifyMask | SubstructureRedirectMask, &ev);
390}
391
392void win_toggle_bar(win_t *win)
393{
394 if (win == NULL || win->xwin == None)
395 return;
396
397 if (win->bar.h != 0) {
398 win->h += win->bar.h;
399 win->bar.h = 0;
400 } else {
401 win->bar.h = barheight;
402 win->h -= win->bar.h;
403 }
404}
405
406void win_clear(win_t *win)
407{
408 int h;
409 win_env_t *e;
410
411 if (win == NULL || win->xwin == None)
412 return;
413
414 h = win->h + win->bar.h;
415 e = &win->env;
416
417 if (win->pm == None)
418 win->pm = XCreatePixmap(e->dpy, win->xwin, win->w, h, e->depth);
419
420 XSetForeground(e->dpy, gc, win->fullscreen ? win->fscol : win->bgcol);
421 XFillRectangle(e->dpy, win->pm, gc, 0, 0, win->w, h);
422}
423
424void win_draw_bar(win_t *win)
425{
426 int len, olen, x, y, w, tw;
427 char rest[3];
428 const char *dots = "...";
429 win_env_t *e;
430
431 if (win == NULL || win->xwin == None || win->pm == None)
432 return;
433
434 e = &win->env;
435 y = win->h + font.ascent + V_TEXT_PAD;
436 w = win->w;
437
438 XSetForeground(e->dpy, gc, win->bar.bgcol);
439 XFillRectangle(e->dpy, win->pm, gc, 0, win->h, win->w, win->bar.h);
440
441 XSetForeground(e->dpy, gc, win->bar.fgcol);
442 XSetBackground(e->dpy, gc, win->bar.bgcol);
443
444 if ((len = strlen(win->bar.r)) > 0) {
445 if ((tw = win_textwidth(win->bar.r, len, true)) > w)
446 return;
447 x = win->w - tw + H_TEXT_PAD;
448 w -= tw;
449 if (font.set)
450 XmbDrawString(e->dpy, win->pm, font.set, gc, x, y, win->bar.r, len);
451 else
452 XDrawString(e->dpy, win->pm, gc, x, y, win->bar.r, len);
453 }
454 if ((len = strlen(win->bar.l)) > 0) {
455 olen = len;
456 while (len > 0 && (tw = win_textwidth(win->bar.l, len, true)) > w)
457 len--;
458 if (len > 0) {
459 if (len != olen) {
460 w = strlen(dots);
461 if (len <= w)
462 return;
463 memcpy(rest, win->bar.l + len - w, w);
464 memcpy(win->bar.l + len - w, dots, w);
465 }
466 x = H_TEXT_PAD;
467 if (font.set)
468 XmbDrawString(e->dpy, win->pm, font.set, gc, x, y, win->bar.l, len);
469 else
470 XDrawString(e->dpy, win->pm, gc, x, y, win->bar.l, len);
471 if (len != olen)
472 memcpy(win->bar.l + len - w, rest, w);
473 }
474 }
475}
476
477void win_draw(win_t *win)
478{
479 if (win == NULL || win->xwin == None || win->pm == None)
480 return;
481
482 if (win->bar.h > 0)
483 win_draw_bar(win);
484
485 XCopyArea(win->env.dpy, win->pm, win->xwin, gc,
486 0, 0, win->w, win->h + win->bar.h, 0, 0);
487}
488
489void win_draw_rect(win_t *win, Pixmap pm, int x, int y, int w, int h,
490 bool fill, int lw, unsigned long col)
491{
492 XGCValues gcval;
493
494 if (win == NULL || pm == None)
495 return;
496
497 gcval.line_width = lw;
498 gcval.foreground = col;
499 XChangeGC(win->env.dpy, gc, GCForeground | GCLineWidth, &gcval);
500
501 if (fill)
502 XFillRectangle(win->env.dpy, pm, gc, x, y, w, h);
503 else
504 XDrawRectangle(win->env.dpy, pm, gc, x, y, w, h);
505}
506
507void win_update_bar(win_t *win)
508{
509 if (win == NULL || win->xwin == None || win->pm == None)
510 return;
511
512 if (win->bar.h > 0) {
513 win_draw_bar(win);
514 XCopyArea(win->env.dpy, win->pm, win->xwin, gc,
515 0, win->h, win->w, win->bar.h, 0, win->h);
516 }
517}
518
519int win_textwidth(const char *text, unsigned int len, bool with_padding)
520{
521 XRectangle r;
522 int padding = with_padding ? 2 * H_TEXT_PAD : 0;
523
524 if (font.set) {
525 XmbTextExtents(font.set, text, len, NULL, &r);
526 return r.width + padding;
527 } else {
528 return XTextWidth(font.xfont, text, len) + padding;
529 }
530}
531
532void win_set_title(win_t *win, const char *title)
533{
534 if (win == NULL || win->xwin == None)
535 return;
536
537 if (title == NULL)
538 title = "sxiv";
539
540 XStoreName(win->env.dpy, win->xwin, title);
541 XSetIconName(win->env.dpy, win->xwin, title);
542
543 XChangeProperty(win->env.dpy, win->xwin,
544 XInternAtom(win->env.dpy, "_NET_WM_NAME", False),
545 XInternAtom(win->env.dpy, "UTF8_STRING", False), 8,
546 PropModeReplace, (unsigned char *) title, strlen(title));
547 XChangeProperty(win->env.dpy, win->xwin,
548 XInternAtom(win->env.dpy, "_NET_WM_ICON_NAME", False),
549 XInternAtom(win->env.dpy, "UTF8_STRING", False), 8,
550 PropModeReplace, (unsigned char *) title, strlen(title));
551}
552
553void win_set_cursor(win_t *win, cursor_t cursor)
554{
555 if (win == NULL || win->xwin == None)
556 return;
557
558 switch (cursor) {
559 case CURSOR_NONE:
560 XDefineCursor(win->env.dpy, win->xwin, cnone);
561 break;
562 case CURSOR_HAND:
563 XDefineCursor(win->env.dpy, win->xwin, chand);
564 break;
565 case CURSOR_WATCH:
566 XDefineCursor(win->env.dpy, win->xwin, cwatch);
567 break;
568 case CURSOR_ARROW:
569 default:
570 XDefineCursor(win->env.dpy, win->xwin, carrow);
571 break;
572 }
573
574 XFlush(win->env.dpy);
575}