Krolyxon
Commit 56142bd

Branches

Refs →

nsxivwindow.c

550 lines · 13.2 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 if (setlocale(LC_CTYPE, "") == NULL || XSupportsLocale() == 0)
123 warn("no locale support");
124
125 win_init_font(e->dpy, BAR_FONT);
126
127 win->white = WhitePixel(e->dpy, e->scr);
128 win->bgcol = win_alloc_color(win, WIN_BG_COLOR);
129 win->fscol = win_alloc_color(win, WIN_FS_COLOR);
130 win->selcol = win_alloc_color(win, SEL_COLOR);
131 win->bar.bgcol = win_alloc_color(win, BAR_BG_COLOR);
132 win->bar.fgcol = win_alloc_color(win, BAR_FG_COLOR);
133 win->bar.h = options->hide_bar ? 0 : barheight;
134
135 win->sizehints.flags = PWinGravity;
136 win->sizehints.win_gravity = NorthWestGravity;
137 if (options->fixed_win)
138 /* actual min/max values set in win_update_sizehints() */
139 win->sizehints.flags |= PMinSize | PMaxSize;
140
141 wm_delete_win = XInternAtom(e->dpy, "WM_DELETE_WINDOW", False);
142}
143
144void win_update_sizehints(win_t *win)
145{
146 if (win == NULL || win->xwin == None)
147 return;
148
149 if ((win->sizehints.flags & USSize) != 0) {
150 win->sizehints.width = win->w;
151 win->sizehints.height = win->h + win->bar.h;
152 }
153 if ((win->sizehints.flags & USPosition) != 0) {
154 win->sizehints.x = win->x;
155 win->sizehints.y = win->y;
156 }
157 if (options->fixed_win) {
158 win->sizehints.min_width = win->sizehints.max_width = win->w;
159 win->sizehints.min_height = win->sizehints.max_height = win->h + win->bar.h;
160 }
161 XSetWMNormalHints(win->env.dpy, win->xwin, &win->sizehints);
162}
163
164void win_open(win_t *win)
165{
166 win_env_t *e;
167 XClassHint classhint;
168 XSetWindowAttributes attr;
169 unsigned long attr_mask;
170 XColor col;
171 char none_data[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
172 Pixmap none;
173 int gmask;
174
175 if (win == NULL)
176 return;
177
178 e = &win->env;
179
180 /* determine window offsets, width & height */
181 if (options->geometry == NULL)
182 gmask = 0;
183 else
184 gmask = XParseGeometry(options->geometry, &win->x, &win->y,
185 &win->w, &win->h);
186 if ((gmask & WidthValue) != 0)
187 win->sizehints.flags |= USSize;
188 else
189 win->w = WIN_WIDTH;
190 if ((gmask & HeightValue) != 0)
191 win->sizehints.flags |= USSize;
192 else
193 win->h = WIN_HEIGHT;
194 if ((gmask & XValue) != 0) {
195 if ((gmask & XNegative) != 0) {
196 win->x += e->scrw - win->w;
197 win->sizehints.win_gravity = NorthEastGravity;
198 }
199 win->sizehints.flags |= USPosition;
200 } else {
201 win->x = (e->scrw - win->w) / 2;
202 }
203 if ((gmask & YValue) != 0) {
204 if ((gmask & YNegative) != 0) {
205 win->y += e->scrh - win->h;
206 if (win->sizehints.win_gravity == NorthEastGravity)
207 win->sizehints.win_gravity = SouthEastGravity;
208 else
209 win->sizehints.win_gravity = SouthWestGravity;
210 }
211 win->sizehints.flags |= USPosition;
212 } else {
213 win->y = (e->scrh - win->h) / 2;
214 }
215
216 attr.background_pixel = win->bgcol;
217 attr_mask = CWBackPixel;
218
219 win->xwin = XCreateWindow(e->dpy, RootWindow(e->dpy, e->scr),
220 win->x, win->y, win->w, win->h, 0,
221 e->depth, InputOutput, e->vis, attr_mask, &attr);
222 if (win->xwin == None)
223 die("could not create window");
224
225 XSelectInput(e->dpy, win->xwin,
226 ExposureMask | ButtonReleaseMask | ButtonPressMask |
227 KeyPressMask | PointerMotionMask | StructureNotifyMask);
228
229 carrow = XCreateFontCursor(e->dpy, XC_left_ptr);
230 chand = XCreateFontCursor(e->dpy, XC_fleur);
231 cwatch = XCreateFontCursor(e->dpy, XC_watch);
232
233 if (XAllocNamedColor(e->dpy, DefaultColormap(e->dpy, e->scr), "black",
234 &col, &col) == 0)
235 {
236 die("could not allocate color: black");
237 }
238 none = XCreateBitmapFromData(e->dpy, win->xwin, none_data, 8, 8);
239 cnone = XCreatePixmapCursor(e->dpy, none, none, &col, &col, 0, 0);
240
241 gc = XCreateGC(e->dpy, win->xwin, 0, None);
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, &wm_delete_win, 1);
250
251 win->h -= win->bar.h;
252 win_update_sizehints(win);
253
254 XMapWindow(e->dpy, win->xwin);
255 XFlush(e->dpy);
256
257 if (options->fullscreen)
258 win_toggle_fullscreen(win);
259}
260
261void win_close(win_t *win)
262{
263 if (win == NULL || win->xwin == None)
264 return;
265
266 XFreeCursor(win->env.dpy, carrow);
267 XFreeCursor(win->env.dpy, cnone);
268 XFreeCursor(win->env.dpy, chand);
269 XFreeCursor(win->env.dpy, cwatch);
270
271 XFreeGC(win->env.dpy, gc);
272
273 XDestroyWindow(win->env.dpy, win->xwin);
274 XCloseDisplay(win->env.dpy);
275}
276
277bool win_configure(win_t *win, XConfigureEvent *c)
278{
279 bool changed;
280
281 if (win == NULL || c == NULL)
282 return false;
283
284 if ((changed = win->w != c->width || win->h + win->bar.h != c->height)) {
285 if (win->pm != None) {
286 XFreePixmap(win->env.dpy, win->pm);
287 win->pm = None;
288 }
289 }
290
291 win->x = c->x;
292 win->y = c->y;
293 win->w = c->width;
294 win->h = c->height - win->bar.h;
295 win->bw = c->border_width;
296
297 return changed;
298}
299
300void win_expose(win_t *win, XExposeEvent *e)
301{
302 if (win == NULL || win->xwin == None || win->pm == None || e == NULL)
303 return;
304
305 XCopyArea(win->env.dpy, win->pm, win->xwin, gc,
306 e->x, e->y, e->width, e->height, e->x, e->y);
307}
308
309bool win_moveresize(win_t *win, int x, int y, unsigned int w, unsigned int h)
310{
311 if (win == NULL || win->xwin == None)
312 return false;
313
314 /* caller knows nothing about the bar */
315 h += win->bar.h;
316
317 x = MAX(0, x);
318 y = MAX(0, y);
319 w = MIN(w, win->env.scrw - 2 * win->bw);
320 h = MIN(h, win->env.scrh - 2 * win->bw);
321
322 if (win->x == x && win->y == y && win->w == w && win->h + win->bar.h == h)
323 return false;
324
325 win->x = x;
326 win->y = y;
327 win->w = w;
328 win->h = h - win->bar.h;
329
330 win_update_sizehints(win);
331
332 XMoveResizeWindow(win->env.dpy, win->xwin, x, y, w, h);
333
334 if (win->pm != None) {
335 XFreePixmap(win->env.dpy, win->pm);
336 win->pm = None;
337 }
338
339 return true;
340}
341
342void win_toggle_fullscreen(win_t *win)
343{
344 XEvent ev;
345 XClientMessageEvent *cm;
346
347 if (win == NULL || win->xwin == None)
348 return;
349
350 win->fullscreen = !win->fullscreen;
351
352 memset(&ev, 0, sizeof(ev));
353 ev.type = ClientMessage;
354
355 cm = &ev.xclient;
356 cm->window = win->xwin;
357 cm->message_type = XInternAtom(win->env.dpy, "_NET_WM_STATE", False);
358 cm->format = 32;
359 cm->data.l[0] = win->fullscreen;
360 cm->data.l[1] = XInternAtom(win->env.dpy, "_NET_WM_STATE_FULLSCREEN", False);
361 cm->data.l[2] = cm->data.l[3] = 0;
362
363 XSendEvent(win->env.dpy, DefaultRootWindow(win->env.dpy), False,
364 SubstructureNotifyMask | SubstructureRedirectMask, &ev);
365}
366
367void win_toggle_bar(win_t *win)
368{
369 if (win == NULL || win->xwin == None)
370 return;
371
372 if (win->bar.h != 0) {
373 win->h += win->bar.h;
374 win->bar.h = 0;
375 } else {
376 win->bar.h = barheight;
377 win->h -= win->bar.h;
378 }
379}
380
381void win_clear(win_t *win)
382{
383 int h;
384 win_env_t *e;
385
386 if (win == NULL || win->xwin == None)
387 return;
388
389 h = win->h + win->bar.h;
390 e = &win->env;
391
392 if (win->pm == None)
393 win->pm = XCreatePixmap(e->dpy, win->xwin, win->w, h, e->depth);
394
395 XSetForeground(e->dpy, gc, win->fullscreen ? win->fscol : win->bgcol);
396 XFillRectangle(e->dpy, win->pm, gc, 0, 0, win->w, h);
397}
398
399void win_draw_bar(win_t *win)
400{
401 int len, olen, x, y, w, tw;
402 char rest[3];
403 const char *dots = "...";
404 win_env_t *e;
405
406 if (win == NULL || win->xwin == None || win->pm == None)
407 return;
408
409 e = &win->env;
410 y = win->h + font.ascent + V_TEXT_PAD;
411 w = win->w;
412
413 XSetForeground(e->dpy, gc, win->bar.bgcol);
414 XFillRectangle(e->dpy, win->pm, gc, 0, win->h, win->w, win->bar.h);
415
416 XSetForeground(e->dpy, gc, win->bar.fgcol);
417 XSetBackground(e->dpy, gc, win->bar.bgcol);
418
419 if ((len = strlen(win->bar.r)) > 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 if ((len = strlen(win->bar.l)) > 0) {
430 olen = len;
431 while (len > 0 && (tw = win_textwidth(win->bar.l, len, true)) > w)
432 len--;
433 if (len > 0) {
434 if (len != olen) {
435 w = strlen(dots);
436 if (len <= w)
437 return;
438 memcpy(rest, win->bar.l + len - w, w);
439 memcpy(win->bar.l + len - w, dots, w);
440 }
441 x = H_TEXT_PAD;
442 if (font.set)
443 XmbDrawString(e->dpy, win->pm, font.set, gc, x, y, win->bar.l, len);
444 else
445 XDrawString(e->dpy, win->pm, gc, x, y, win->bar.l, len);
446 if (len != olen)
447 memcpy(win->bar.l + len - w, rest, w);
448 }
449 }
450}
451
452void win_draw(win_t *win)
453{
454 if (win == NULL || win->xwin == None || win->pm == None)
455 return;
456
457 if (win->bar.h > 0)
458 win_draw_bar(win);
459
460 XCopyArea(win->env.dpy, win->pm, win->xwin, gc,
461 0, 0, win->w, win->h + win->bar.h, 0, 0);
462}
463
464void win_draw_rect(win_t *win, Pixmap pm, int x, int y, int w, int h,
465 bool fill, int lw, unsigned long col)
466{
467 XGCValues gcval;
468
469 if (win == NULL || pm == None)
470 return;
471
472 gcval.line_width = lw;
473 gcval.foreground = col;
474 XChangeGC(win->env.dpy, gc, GCForeground | GCLineWidth, &gcval);
475
476 if (fill)
477 XFillRectangle(win->env.dpy, pm, gc, x, y, w, h);
478 else
479 XDrawRectangle(win->env.dpy, pm, gc, x, y, w, h);
480}
481
482void win_update_bar(win_t *win)
483{
484 if (win == NULL || win->xwin == None || win->pm == None)
485 return;
486
487 if (win->bar.h > 0) {
488 win_draw_bar(win);
489 XCopyArea(win->env.dpy, win->pm, win->xwin, gc,
490 0, win->h, win->w, win->bar.h, 0, win->h);
491 }
492}
493
494int win_textwidth(const char *text, unsigned int len, bool with_padding)
495{
496 XRectangle r;
497 int padding = with_padding ? 2 * H_TEXT_PAD : 0;
498
499 if (font.set) {
500 XmbTextExtents(font.set, text, len, NULL, &r);
501 return r.width + padding;
502 } else {
503 return XTextWidth(font.xfont, text, len) + padding;
504 }
505}
506
507void win_set_title(win_t *win, const char *title)
508{
509 if (win == NULL || win->xwin == None)
510 return;
511
512 if (title == NULL)
513 title = "sxiv";
514
515 XStoreName(win->env.dpy, win->xwin, title);
516 XSetIconName(win->env.dpy, win->xwin, title);
517
518 XChangeProperty(win->env.dpy, win->xwin,
519 XInternAtom(win->env.dpy, "_NET_WM_NAME", False),
520 XInternAtom(win->env.dpy, "UTF8_STRING", False), 8,
521 PropModeReplace, (unsigned char *) title, strlen(title));
522 XChangeProperty(win->env.dpy, win->xwin,
523 XInternAtom(win->env.dpy, "_NET_WM_ICON_NAME", False),
524 XInternAtom(win->env.dpy, "UTF8_STRING", False), 8,
525 PropModeReplace, (unsigned char *) title, strlen(title));
526}
527
528void win_set_cursor(win_t *win, cursor_t cursor)
529{
530 if (win == NULL || win->xwin == None)
531 return;
532
533 switch (cursor) {
534 case CURSOR_NONE:
535 XDefineCursor(win->env.dpy, win->xwin, cnone);
536 break;
537 case CURSOR_HAND:
538 XDefineCursor(win->env.dpy, win->xwin, chand);
539 break;
540 case CURSOR_WATCH:
541 XDefineCursor(win->env.dpy, win->xwin, cwatch);
542 break;
543 case CURSOR_ARROW:
544 default:
545 XDefineCursor(win->env.dpy, win->xwin, carrow);
546 break;
547 }
548
549 XFlush(win->env.dpy);
550}