Krolyxon
Commit 9c0a53b

Branches

Refs →

nsxivwindow.c

500 lines · 12.3 KB · C

1/* sxiv: window.c
2 * Copyright (c) 2012 Bert Muennich <be.muennich at googlemail.com>
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18
19#define _POSIX_C_SOURCE 200112L
20#define _WINDOW_CONFIG
21
22#include <string.h>
23#include <locale.h>
24#include <X11/Xutil.h>
25#include <X11/cursorfont.h>
26
27#include "options.h"
28#include "util.h"
29#include "window.h"
30#include "config.h"
31
32enum {
33 H_TEXT_PAD = 5,
34 V_TEXT_PAD = 1
35};
36
37static Cursor carrow;
38static Cursor cnone;
39static Cursor chand;
40static Cursor cwatch;
41static GC gc;
42
43Atom wm_delete_win;
44
45static struct {
46 int ascent;
47 int descent;
48 XFontStruct *xfont;
49 XFontSet set;
50} font;
51
52static int fontheight;
53static int barheight;
54
55void win_init_font(Display *dpy, const char *fontstr) {
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 XColor col;
89
90 if (win == NULL)
91 return 0UL;
92 if (XAllocNamedColor(win->env.dpy,
93 DefaultColormap(win->env.dpy, win->env.scr),
94 name, &col, &col) == 0)
95 {
96 die("could not allocate color: %s", name);
97 }
98 return col.pixel;
99}
100
101void win_init(win_t *win) {
102 win_env_t *e;
103
104 if (win == NULL)
105 return;
106
107 memset(win, 0, sizeof(win_t));
108
109 e = &win->env;
110 if ((e->dpy = XOpenDisplay(NULL)) == NULL)
111 die("could not open display");
112
113 e->scr = DefaultScreen(e->dpy);
114 e->scrw = DisplayWidth(e->dpy, e->scr);
115 e->scrh = DisplayHeight(e->dpy, e->scr);
116 e->vis = DefaultVisual(e->dpy, e->scr);
117 e->cmap = DefaultColormap(e->dpy, e->scr);
118 e->depth = DefaultDepth(e->dpy, e->scr);
119
120 win->white = WhitePixel(e->dpy, e->scr);
121 win->bgcol = win_alloc_color(win, WIN_BG_COLOR);
122 win->fscol = win_alloc_color(win, WIN_FS_COLOR);
123 win->selcol = win_alloc_color(win, SEL_COLOR);
124 win->bar.bgcol = win_alloc_color(win, BAR_BG_COLOR);
125 win->bar.fgcol = win_alloc_color(win, BAR_FG_COLOR);
126
127 if (setlocale(LC_CTYPE, "") == NULL || XSupportsLocale() == 0)
128 warn("no locale support");
129
130 win_init_font(e->dpy, BAR_FONT);
131
132 wm_delete_win = XInternAtom(e->dpy, "WM_DELETE_WINDOW", False);
133}
134
135void win_set_sizehints(win_t *win) {
136 XSizeHints sizehints;
137
138 if (win == NULL || win->xwin == None)
139 return;
140
141 sizehints.flags = PMinSize | PMaxSize;
142 sizehints.min_width = win->w;
143 sizehints.max_width = win->w;
144 sizehints.min_height = win->h + win->bar.h;
145 sizehints.max_height = win->h + win->bar.h;
146 XSetWMNormalHints(win->env.dpy, win->xwin, &sizehints);
147}
148
149void win_open(win_t *win) {
150 win_env_t *e;
151 XClassHint classhint;
152 XColor col;
153 char none_data[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
154 Pixmap none;
155 int gmask;
156
157 if (win == NULL)
158 return;
159
160 e = &win->env;
161
162 /* determine window offsets, width & height */
163 if (options->geometry == NULL)
164 gmask = 0;
165 else
166 gmask = XParseGeometry(options->geometry, &win->x, &win->y,
167 &win->w, &win->h);
168 if ((gmask & WidthValue) == 0)
169 win->w = WIN_WIDTH;
170 if (win->w > e->scrw)
171 win->w = e->scrw;
172 if ((gmask & HeightValue) == 0)
173 win->h = WIN_HEIGHT;
174 if (win->h > e->scrh)
175 win->h = e->scrh;
176 if ((gmask & XValue) == 0)
177 win->x = (e->scrw - win->w) / 2;
178 else if ((gmask & XNegative) != 0)
179 win->x += e->scrw - win->w;
180 if ((gmask & YValue) == 0)
181 win->y = (e->scrh - win->h) / 2;
182 else if ((gmask & YNegative) != 0)
183 win->y += e->scrh - win->h;
184
185 win->xwin = XCreateWindow(e->dpy, RootWindow(e->dpy, e->scr),
186 win->x, win->y, win->w, win->h, 0,
187 e->depth, InputOutput, e->vis, 0, None);
188 if (win->xwin == None)
189 die("could not create window");
190
191 XSelectInput(e->dpy, win->xwin,
192 ExposureMask | ButtonReleaseMask | ButtonPressMask |
193 KeyPressMask | PointerMotionMask | StructureNotifyMask);
194
195 carrow = XCreateFontCursor(e->dpy, XC_left_ptr);
196 chand = XCreateFontCursor(e->dpy, XC_fleur);
197 cwatch = XCreateFontCursor(e->dpy, XC_watch);
198
199 if (XAllocNamedColor(e->dpy, DefaultColormap(e->dpy, e->scr), "black",
200 &col, &col) == 0)
201 {
202 die("could not allocate color: black");
203 }
204 none = XCreateBitmapFromData(e->dpy, win->xwin, none_data, 8, 8);
205 cnone = XCreatePixmapCursor(e->dpy, none, none, &col, &col, 0, 0);
206
207 gc = XCreateGC(e->dpy, win->xwin, 0, None);
208
209 win_set_title(win, "sxiv");
210
211 classhint.res_class = "Sxiv";
212 classhint.res_name = options->res_name != NULL ? options->res_name : "sxiv";
213 XSetClassHint(e->dpy, win->xwin, &classhint);
214
215 XSetWMProtocols(e->dpy, win->xwin, &wm_delete_win, 1);
216
217 if (!options->hide_bar) {
218 win->bar.h = barheight;
219 win->h -= win->bar.h;
220 }
221
222 if (options->fixed_win)
223 win_set_sizehints(win);
224
225 XMapWindow(e->dpy, win->xwin);
226 XFlush(e->dpy);
227
228 if (options->fullscreen)
229 win_toggle_fullscreen(win);
230}
231
232void win_close(win_t *win) {
233 if (win == NULL || win->xwin == None)
234 return;
235
236 XFreeCursor(win->env.dpy, carrow);
237 XFreeCursor(win->env.dpy, cnone);
238 XFreeCursor(win->env.dpy, chand);
239 XFreeCursor(win->env.dpy, cwatch);
240
241 XFreeGC(win->env.dpy, gc);
242
243 XDestroyWindow(win->env.dpy, win->xwin);
244 XCloseDisplay(win->env.dpy);
245}
246
247bool win_configure(win_t *win, XConfigureEvent *c) {
248 bool changed;
249
250 if (win == NULL || c == NULL)
251 return false;
252
253 if ((changed = win->w != c->width || win->h + win->bar.h != c->height)) {
254 if (win->pm != None) {
255 XFreePixmap(win->env.dpy, win->pm);
256 win->pm = None;
257 }
258 }
259
260 win->x = c->x;
261 win->y = c->y;
262 win->w = c->width;
263 win->h = c->height - win->bar.h;
264 win->bw = c->border_width;
265
266 return changed;
267}
268
269void win_expose(win_t *win, XExposeEvent *e) {
270 if (win == NULL || win->xwin == None || win->pm == None || e == NULL)
271 return;
272
273 XCopyArea(win->env.dpy, win->pm, win->xwin, gc,
274 e->x, e->y, e->width, e->height, e->x, e->y);
275}
276
277bool win_moveresize(win_t *win, int x, int y, unsigned int w, unsigned int h) {
278 if (win == NULL || win->xwin == None)
279 return false;
280
281 x = MAX(0, x);
282 y = MAX(0, y);
283 w = MIN(w, win->env.scrw - 2 * win->bw);
284 h = MIN(h, win->env.scrh - 2 * win->bw);
285
286 if (win->x == x && win->y == y && win->w == w && win->h + win->bar.h == h)
287 return false;
288
289 win->x = x;
290 win->y = y;
291 win->w = w;
292 win->h = h - win->bar.h;
293
294 if (options->fixed_win)
295 win_set_sizehints(win);
296
297 XMoveResizeWindow(win->env.dpy, win->xwin, x, y, w, h);
298
299 return true;
300}
301
302void win_toggle_fullscreen(win_t *win) {
303 XEvent ev;
304 XClientMessageEvent *cm;
305
306 if (win == NULL || win->xwin == None)
307 return;
308
309 win->fullscreen = !win->fullscreen;
310
311 memset(&ev, 0, sizeof(ev));
312 ev.type = ClientMessage;
313
314 cm = &ev.xclient;
315 cm->window = win->xwin;
316 cm->message_type = XInternAtom(win->env.dpy, "_NET_WM_STATE", False);
317 cm->format = 32;
318 cm->data.l[0] = win->fullscreen;
319 cm->data.l[1] = XInternAtom(win->env.dpy, "_NET_WM_STATE_FULLSCREEN", False);
320 cm->data.l[2] = cm->data.l[3] = 0;
321
322 XSendEvent(win->env.dpy, DefaultRootWindow(win->env.dpy), False,
323 SubstructureNotifyMask | SubstructureRedirectMask, &ev);
324}
325
326void win_toggle_bar(win_t *win) {
327 if (win == NULL || win->xwin == None)
328 return;
329
330 if (win->bar.h != 0) {
331 win->h += win->bar.h;
332 win->bar.h = 0;
333 } else {
334 win->bar.h = barheight;
335 win->h -= win->bar.h;
336 }
337}
338
339void win_clear(win_t *win) {
340 int h;
341 win_env_t *e;
342
343 if (win == NULL || win->xwin == None)
344 return;
345
346 h = win->h + win->bar.h;
347 e = &win->env;
348
349 if (win->pm == None)
350 win->pm = XCreatePixmap(e->dpy, win->xwin, win->w, h, e->depth);
351
352 XSetForeground(e->dpy, gc, win->fullscreen ? win->fscol : win->bgcol);
353 XFillRectangle(e->dpy, win->pm, gc, 0, 0, win->w, h);
354}
355
356void win_draw_bar(win_t *win) {
357 int len, olen, x, y, w, tw;
358 char rest[3];
359 const char *dots = "...";
360 win_env_t *e;
361
362 if (win == NULL || win->xwin == None || win->pm == None)
363 return;
364
365 e = &win->env;
366 y = win->h + font.ascent + V_TEXT_PAD;
367 w = win->w;
368
369 XSetForeground(e->dpy, gc, win->bar.bgcol);
370 XFillRectangle(e->dpy, win->pm, gc, 0, win->h, win->w, win->bar.h);
371
372 XSetForeground(e->dpy, gc, win->bar.fgcol);
373 XSetBackground(e->dpy, gc, win->bar.bgcol);
374
375 if (win->bar.r != NULL) {
376 len = strlen(win->bar.r);
377 if (len > 0) {
378 if ((tw = win_textwidth(win->bar.r, len, true)) > w)
379 return;
380 x = win->w - tw + H_TEXT_PAD;
381 w -= tw;
382 if (font.set)
383 XmbDrawString(e->dpy, win->pm, font.set, gc, x, y, win->bar.r, len);
384 else
385 XDrawString(e->dpy, win->pm, gc, x, y, win->bar.r, len);
386 }
387 }
388 if (win->bar.l != NULL) {
389 olen = len = strlen(win->bar.l);
390 while (len > 0 && (tw = win_textwidth(win->bar.l, len, true)) > w)
391 len--;
392 if (len > 0) {
393 if (len != olen) {
394 w = strlen(dots);
395 if (len <= w)
396 return;
397 memcpy(rest, win->bar.l + len - w, w);
398 memcpy(win->bar.l + len - w, dots, w);
399 }
400 x = H_TEXT_PAD;
401 if (font.set)
402 XmbDrawString(e->dpy, win->pm, font.set, gc, x, y, win->bar.l, len);
403 else
404 XDrawString(e->dpy, win->pm, gc, x, y, win->bar.l, len);
405 if (len != olen)
406 memcpy(win->bar.l + len - w, rest, w);
407 }
408 }
409}
410
411void win_draw(win_t *win) {
412 if (win == NULL || win->xwin == None || win->pm == None)
413 return;
414
415 if (win->bar.h > 0)
416 win_draw_bar(win);
417
418 XCopyArea(win->env.dpy, win->pm, win->xwin, gc,
419 0, 0, win->w, win->h + win->bar.h, 0, 0);
420}
421
422void win_draw_rect(win_t *win, Pixmap pm, int x, int y, int w, int h,
423 bool fill, int lw, unsigned long col)
424{
425 XGCValues gcval;
426
427 if (win == NULL || pm == None)
428 return;
429
430 gcval.line_width = lw;
431 gcval.foreground = col;
432 XChangeGC(win->env.dpy, gc, GCForeground | GCLineWidth, &gcval);
433
434 if (fill)
435 XFillRectangle(win->env.dpy, pm, gc, x, y, w, h);
436 else
437 XDrawRectangle(win->env.dpy, pm, gc, x, y, w, h);
438}
439
440int win_textwidth(const char *text, unsigned int len, bool with_padding) {
441 XRectangle r;
442 int padding = with_padding ? 2 * H_TEXT_PAD : 0;
443
444 if (font.set) {
445 XmbTextExtents(font.set, text, len, NULL, &r);
446 return r.width + padding;
447 } else {
448 return XTextWidth(font.xfont, text, len) + padding;
449 }
450}
451
452void win_set_title(win_t *win, const char *title) {
453 if (win == NULL || win->xwin == None)
454 return;
455
456 if (title == NULL)
457 title = "sxiv";
458
459 XStoreName(win->env.dpy, win->xwin, title);
460 XSetIconName(win->env.dpy, win->xwin, title);
461
462 XChangeProperty(win->env.dpy, win->xwin,
463 XInternAtom(win->env.dpy, "_NET_WM_NAME", False),
464 XInternAtom(win->env.dpy, "UTF8_STRING", False), 8,
465 PropModeReplace, (unsigned char *) title, strlen(title));
466 XChangeProperty(win->env.dpy, win->xwin,
467 XInternAtom(win->env.dpy, "_NET_WM_ICON_NAME", False),
468 XInternAtom(win->env.dpy, "UTF8_STRING", False), 8,
469 PropModeReplace, (unsigned char *) title, strlen(title));
470}
471
472void win_set_bar_info(win_t *win, char *linfo, char *rinfo) {
473 if (win != NULL) {
474 win->bar.l = linfo;
475 win->bar.r = rinfo;
476 }
477}
478
479void win_set_cursor(win_t *win, cursor_t cursor) {
480 if (win == NULL || win->xwin == None)
481 return;
482
483 switch (cursor) {
484 case CURSOR_NONE:
485 XDefineCursor(win->env.dpy, win->xwin, cnone);
486 break;
487 case CURSOR_HAND:
488 XDefineCursor(win->env.dpy, win->xwin, chand);
489 break;
490 case CURSOR_WATCH:
491 XDefineCursor(win->env.dpy, win->xwin, cwatch);
492 break;
493 case CURSOR_ARROW:
494 default:
495 XDefineCursor(win->env.dpy, win->xwin, carrow);
496 break;
497 }
498
499 XFlush(win->env.dpy);
500}