Krolyxon
Commit cef8d51

Branches

Refs →

nsxivwindow.c

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