Krolyxon
Commit c8fcc3d

Branches

Refs →

nsxivwindow.c

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