Krolyxon
Commit 98972e9

Branches

Refs →

nsxivwindow.c

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