Krolyxon
Commit 1e2ddc4

Branches

Refs →

nsxivwindow.c

544 lines · 12.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 <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 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 (win->bar.r != NULL) {
415 len = strlen(win->bar.r);
416 if (len > 0) {
417 if ((tw = win_textwidth(win->bar.r, len, true)) > w)
418 return;
419 x = win->w - tw + H_TEXT_PAD;
420 w -= tw;
421 if (font.set)
422 XmbDrawString(e->dpy, win->pm, font.set, gc, x, y, win->bar.r, len);
423 else
424 XDrawString(e->dpy, win->pm, gc, x, y, win->bar.r, len);
425 }
426 }
427 if (win->bar.l != NULL) {
428 olen = len = strlen(win->bar.l);
429 while (len > 0 && (tw = win_textwidth(win->bar.l, len, true)) > w)
430 len--;
431 if (len > 0) {
432 if (len != olen) {
433 w = strlen(dots);
434 if (len <= w)
435 return;
436 memcpy(rest, win->bar.l + len - w, w);
437 memcpy(win->bar.l + len - w, dots, w);
438 }
439 x = H_TEXT_PAD;
440 if (font.set)
441 XmbDrawString(e->dpy, win->pm, font.set, gc, x, y, win->bar.l, len);
442 else
443 XDrawString(e->dpy, win->pm, gc, x, y, win->bar.l, len);
444 if (len != olen)
445 memcpy(win->bar.l + len - w, rest, w);
446 }
447 }
448}
449
450void win_draw(win_t *win)
451{
452 if (win == NULL || win->xwin == None || win->pm == None)
453 return;
454
455 if (win->bar.h > 0)
456 win_draw_bar(win);
457
458 XCopyArea(win->env.dpy, win->pm, win->xwin, gc,
459 0, 0, win->w, win->h + win->bar.h, 0, 0);
460}
461
462void win_draw_rect(win_t *win, Pixmap pm, int x, int y, int w, int h,
463 bool fill, int lw, unsigned long col)
464{
465 XGCValues gcval;
466
467 if (win == NULL || pm == None)
468 return;
469
470 gcval.line_width = lw;
471 gcval.foreground = col;
472 XChangeGC(win->env.dpy, gc, GCForeground | GCLineWidth, &gcval);
473
474 if (fill)
475 XFillRectangle(win->env.dpy, pm, gc, x, y, w, h);
476 else
477 XDrawRectangle(win->env.dpy, pm, gc, x, y, w, h);
478}
479
480int win_textwidth(const char *text, unsigned int len, bool with_padding)
481{
482 XRectangle r;
483 int padding = with_padding ? 2 * H_TEXT_PAD : 0;
484
485 if (font.set) {
486 XmbTextExtents(font.set, text, len, NULL, &r);
487 return r.width + padding;
488 } else {
489 return XTextWidth(font.xfont, text, len) + padding;
490 }
491}
492
493void win_set_title(win_t *win, const char *title)
494{
495 if (win == NULL || win->xwin == None)
496 return;
497
498 if (title == NULL)
499 title = "sxiv";
500
501 XStoreName(win->env.dpy, win->xwin, title);
502 XSetIconName(win->env.dpy, win->xwin, title);
503
504 XChangeProperty(win->env.dpy, win->xwin,
505 XInternAtom(win->env.dpy, "_NET_WM_NAME", False),
506 XInternAtom(win->env.dpy, "UTF8_STRING", False), 8,
507 PropModeReplace, (unsigned char *) title, strlen(title));
508 XChangeProperty(win->env.dpy, win->xwin,
509 XInternAtom(win->env.dpy, "_NET_WM_ICON_NAME", False),
510 XInternAtom(win->env.dpy, "UTF8_STRING", False), 8,
511 PropModeReplace, (unsigned char *) title, strlen(title));
512}
513
514void win_set_bar_info(win_t *win, char *linfo, char *rinfo)
515{
516 if (win != NULL) {
517 win->bar.l = linfo;
518 win->bar.r = rinfo;
519 }
520}
521
522void win_set_cursor(win_t *win, cursor_t cursor)
523{
524 if (win == NULL || win->xwin == None)
525 return;
526
527 switch (cursor) {
528 case CURSOR_NONE:
529 XDefineCursor(win->env.dpy, win->xwin, cnone);
530 break;
531 case CURSOR_HAND:
532 XDefineCursor(win->env.dpy, win->xwin, chand);
533 break;
534 case CURSOR_WATCH:
535 XDefineCursor(win->env.dpy, win->xwin, cwatch);
536 break;
537 case CURSOR_ARROW:
538 default:
539 XDefineCursor(win->env.dpy, win->xwin, carrow);
540 break;
541 }
542
543 XFlush(win->env.dpy);
544}