Krolyxon
Commit aac28cc

Branches

Refs →

nsxivwindow.c

545 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 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 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 win->h -= win->bar.h;
247 win_update_sizehints(win);
248
249 XMapWindow(e->dpy, win->xwin);
250 XFlush(e->dpy);
251
252 if (options->fullscreen)
253 win_toggle_fullscreen(win);
254}
255
256void win_close(win_t *win)
257{
258 if (win == NULL || win->xwin == None)
259 return;
260
261 XFreeCursor(win->env.dpy, carrow);
262 XFreeCursor(win->env.dpy, cnone);
263 XFreeCursor(win->env.dpy, chand);
264 XFreeCursor(win->env.dpy, cwatch);
265
266 XFreeGC(win->env.dpy, gc);
267
268 XDestroyWindow(win->env.dpy, win->xwin);
269 XCloseDisplay(win->env.dpy);
270}
271
272bool win_configure(win_t *win, XConfigureEvent *c)
273{
274 bool changed;
275
276 if (win == NULL || c == NULL)
277 return false;
278
279 if ((changed = win->w != c->width || win->h + win->bar.h != c->height)) {
280 if (win->pm != None) {
281 XFreePixmap(win->env.dpy, win->pm);
282 win->pm = None;
283 }
284 }
285
286 win->x = c->x;
287 win->y = c->y;
288 win->w = c->width;
289 win->h = c->height - win->bar.h;
290 win->bw = c->border_width;
291
292 return changed;
293}
294
295void win_expose(win_t *win, XExposeEvent *e)
296{
297 if (win == NULL || win->xwin == None || win->pm == None || e == NULL)
298 return;
299
300 XCopyArea(win->env.dpy, win->pm, win->xwin, gc,
301 e->x, e->y, e->width, e->height, e->x, e->y);
302}
303
304bool win_moveresize(win_t *win, int x, int y, unsigned int w, unsigned int h)
305{
306 if (win == NULL || win->xwin == None)
307 return false;
308
309 /* caller knows nothing about the bar */
310 h += win->bar.h;
311
312 x = MAX(0, x);
313 y = MAX(0, y);
314 w = MIN(w, win->env.scrw - 2 * win->bw);
315 h = MIN(h, win->env.scrh - 2 * win->bw);
316
317 if (win->x == x && win->y == y && win->w == w && win->h + win->bar.h == h)
318 return false;
319
320 win->x = x;
321 win->y = y;
322 win->w = w;
323 win->h = h - win->bar.h;
324
325 win_update_sizehints(win);
326
327 XMoveResizeWindow(win->env.dpy, win->xwin, x, y, w, h);
328
329 if (win->pm != None) {
330 XFreePixmap(win->env.dpy, win->pm);
331 win->pm = None;
332 }
333
334 return true;
335}
336
337void win_toggle_fullscreen(win_t *win)
338{
339 XEvent ev;
340 XClientMessageEvent *cm;
341
342 if (win == NULL || win->xwin == None)
343 return;
344
345 win->fullscreen = !win->fullscreen;
346
347 memset(&ev, 0, sizeof(ev));
348 ev.type = ClientMessage;
349
350 cm = &ev.xclient;
351 cm->window = win->xwin;
352 cm->message_type = XInternAtom(win->env.dpy, "_NET_WM_STATE", False);
353 cm->format = 32;
354 cm->data.l[0] = win->fullscreen;
355 cm->data.l[1] = XInternAtom(win->env.dpy, "_NET_WM_STATE_FULLSCREEN", False);
356 cm->data.l[2] = cm->data.l[3] = 0;
357
358 XSendEvent(win->env.dpy, DefaultRootWindow(win->env.dpy), False,
359 SubstructureNotifyMask | SubstructureRedirectMask, &ev);
360}
361
362void win_toggle_bar(win_t *win)
363{
364 if (win == NULL || win->xwin == None)
365 return;
366
367 if (win->bar.h != 0) {
368 win->h += win->bar.h;
369 win->bar.h = 0;
370 } else {
371 win->bar.h = barheight;
372 win->h -= win->bar.h;
373 }
374}
375
376void win_clear(win_t *win)
377{
378 int h;
379 win_env_t *e;
380
381 if (win == NULL || win->xwin == None)
382 return;
383
384 h = win->h + win->bar.h;
385 e = &win->env;
386
387 if (win->pm == None)
388 win->pm = XCreatePixmap(e->dpy, win->xwin, win->w, h, e->depth);
389
390 XSetForeground(e->dpy, gc, win->fullscreen ? win->fscol : win->bgcol);
391 XFillRectangle(e->dpy, win->pm, gc, 0, 0, win->w, h);
392}
393
394void win_draw_bar(win_t *win)
395{
396 int len, olen, x, y, w, tw;
397 char rest[3];
398 const char *dots = "...";
399 win_env_t *e;
400
401 if (win == NULL || win->xwin == None || win->pm == None)
402 return;
403
404 e = &win->env;
405 y = win->h + font.ascent + V_TEXT_PAD;
406 w = win->w;
407
408 XSetForeground(e->dpy, gc, win->bar.bgcol);
409 XFillRectangle(e->dpy, win->pm, gc, 0, win->h, win->w, win->bar.h);
410
411 XSetForeground(e->dpy, gc, win->bar.fgcol);
412 XSetBackground(e->dpy, gc, win->bar.bgcol);
413
414 if ((len = strlen(win->bar.r)) > 0) {
415 if ((tw = win_textwidth(win->bar.r, len, true)) > w)
416 return;
417 x = win->w - tw + H_TEXT_PAD;
418 w -= tw;
419 if (font.set)
420 XmbDrawString(e->dpy, win->pm, font.set, gc, x, y, win->bar.r, len);
421 else
422 XDrawString(e->dpy, win->pm, gc, x, y, win->bar.r, len);
423 }
424 if ((len = strlen(win->bar.l)) > 0) {
425 olen = len;
426 while (len > 0 && (tw = win_textwidth(win->bar.l, len, true)) > w)
427 len--;
428 if (len > 0) {
429 if (len != olen) {
430 w = strlen(dots);
431 if (len <= w)
432 return;
433 memcpy(rest, win->bar.l + len - w, w);
434 memcpy(win->bar.l + len - w, dots, w);
435 }
436 x = H_TEXT_PAD;
437 if (font.set)
438 XmbDrawString(e->dpy, win->pm, font.set, gc, x, y, win->bar.l, len);
439 else
440 XDrawString(e->dpy, win->pm, gc, x, y, win->bar.l, len);
441 if (len != olen)
442 memcpy(win->bar.l + len - w, rest, w);
443 }
444 }
445}
446
447void win_draw(win_t *win)
448{
449 if (win == NULL || win->xwin == None || win->pm == None)
450 return;
451
452 if (win->bar.h > 0)
453 win_draw_bar(win);
454
455 XCopyArea(win->env.dpy, win->pm, win->xwin, gc,
456 0, 0, win->w, win->h + win->bar.h, 0, 0);
457}
458
459void win_draw_rect(win_t *win, Pixmap pm, int x, int y, int w, int h,
460 bool fill, int lw, unsigned long col)
461{
462 XGCValues gcval;
463
464 if (win == NULL || pm == None)
465 return;
466
467 gcval.line_width = lw;
468 gcval.foreground = col;
469 XChangeGC(win->env.dpy, gc, GCForeground | GCLineWidth, &gcval);
470
471 if (fill)
472 XFillRectangle(win->env.dpy, pm, gc, x, y, w, h);
473 else
474 XDrawRectangle(win->env.dpy, pm, gc, x, y, w, h);
475}
476
477void win_update_bar(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 XCopyArea(win->env.dpy, win->pm, win->xwin, gc,
485 0, win->h, win->w, win->bar.h, 0, win->h);
486 }
487}
488
489int win_textwidth(const char *text, unsigned int len, bool with_padding)
490{
491 XRectangle r;
492 int padding = with_padding ? 2 * H_TEXT_PAD : 0;
493
494 if (font.set) {
495 XmbTextExtents(font.set, text, len, NULL, &r);
496 return r.width + padding;
497 } else {
498 return XTextWidth(font.xfont, text, len) + padding;
499 }
500}
501
502void win_set_title(win_t *win, const char *title)
503{
504 if (win == NULL || win->xwin == None)
505 return;
506
507 if (title == NULL)
508 title = "sxiv";
509
510 XStoreName(win->env.dpy, win->xwin, title);
511 XSetIconName(win->env.dpy, win->xwin, title);
512
513 XChangeProperty(win->env.dpy, win->xwin,
514 XInternAtom(win->env.dpy, "_NET_WM_NAME", False),
515 XInternAtom(win->env.dpy, "UTF8_STRING", False), 8,
516 PropModeReplace, (unsigned char *) title, strlen(title));
517 XChangeProperty(win->env.dpy, win->xwin,
518 XInternAtom(win->env.dpy, "_NET_WM_ICON_NAME", False),
519 XInternAtom(win->env.dpy, "UTF8_STRING", False), 8,
520 PropModeReplace, (unsigned char *) title, strlen(title));
521}
522
523void win_set_cursor(win_t *win, cursor_t cursor)
524{
525 if (win == NULL || win->xwin == None)
526 return;
527
528 switch (cursor) {
529 case CURSOR_NONE:
530 XDefineCursor(win->env.dpy, win->xwin, cnone);
531 break;
532 case CURSOR_HAND:
533 XDefineCursor(win->env.dpy, win->xwin, chand);
534 break;
535 case CURSOR_WATCH:
536 XDefineCursor(win->env.dpy, win->xwin, cwatch);
537 break;
538 case CURSOR_ARROW:
539 default:
540 XDefineCursor(win->env.dpy, win->xwin, carrow);
541 break;
542 }
543
544 XFlush(win->env.dpy);
545}