Krolyxon
Commit 4a5d5d2

Branches

Refs →

nsxivwindow.c

498 lines · 12.1 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
45struct {
46 int ascent;
47 int descent;
48 XFontStruct *xfont;
49 XFontSet set;
50} font;
51
52void win_init_font(Display *dpy, const char *fontstr) {
53 int n;
54 char *def, **missing;
55
56 font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
57 if (missing)
58 XFreeStringList(missing);
59 if (font.set) {
60 XFontStruct **xfonts;
61 char **font_names;
62
63 font.ascent = font.descent = 0;
64 XExtentsOfFontSet(font.set);
65 n = XFontsOfFontSet(font.set, &xfonts, &font_names);
66 while (n--) {
67 font.ascent = MAX(font.ascent, (*xfonts)->ascent);
68 font.descent = MAX(font.descent,(*xfonts)->descent);
69 xfonts++;
70 }
71 } else {
72 if ((font.xfont = XLoadQueryFont(dpy, fontstr)) == NULL &&
73 (font.xfont = XLoadQueryFont(dpy, "fixed")) == NULL)
74 {
75 die("could not load font: %s", fontstr);
76 }
77 font.ascent = font.xfont->ascent;
78 font.descent = font.xfont->descent;
79 }
80}
81
82unsigned long win_alloc_color(win_t *win, const char *name) {
83 XColor col;
84
85 if (win == NULL)
86 return 0UL;
87 if (XAllocNamedColor(win->env.dpy,
88 DefaultColormap(win->env.dpy, win->env.scr),
89 name, &col, &col) == 0)
90 {
91 die("could not allocate color: %s", name);
92 }
93 return col.pixel;
94}
95
96void win_init(win_t *win) {
97 win_env_t *e;
98
99 if (win == NULL)
100 return;
101
102 e = &win->env;
103 if ((e->dpy = XOpenDisplay(NULL)) == NULL)
104 die("could not open display");
105
106 e->scr = DefaultScreen(e->dpy);
107 e->scrw = DisplayWidth(e->dpy, e->scr);
108 e->scrh = DisplayHeight(e->dpy, e->scr);
109 e->vis = DefaultVisual(e->dpy, e->scr);
110 e->cmap = DefaultColormap(e->dpy, e->scr);
111 e->depth = DefaultDepth(e->dpy, e->scr);
112
113 win->white = WhitePixel(e->dpy, e->scr);
114 win->bgcol = win_alloc_color(win, WIN_BG_COLOR);
115 win->fscol = win_alloc_color(win, WIN_FS_COLOR);
116 win->selcol = win_alloc_color(win, SEL_COLOR);
117 win->barbgcol = win_alloc_color(win, BAR_BG_COLOR);
118 win->barfgcol = win_alloc_color(win, BAR_FG_COLOR);
119
120 win->xwin = 0;
121 win->pm = 0;
122 win->fullscreen = false;
123 win->barh = 0;
124 win->lbar = NULL;
125 win->rbar = NULL;
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->barh;
145 sizehints.max_height = win->h + win->barh;
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_name = "sxiv";
212 classhint.res_class = "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->barh = font.ascent + font.descent + 2 * V_TEXT_PAD;
219 win->h -= win->barh;
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->barh != 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->barh;
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 || e == NULL)
271 return;
272
273 if (win->pm != None)
274 XCopyArea(win->env.dpy, win->pm, win->xwin, gc,
275 e->x, e->y, e->width, e->height, e->x, e->y);
276}
277
278bool win_moveresize(win_t *win, int x, int y, unsigned int w, unsigned int h) {
279 if (win == NULL || win->xwin == None)
280 return false;
281
282 x = MAX(0, x);
283 y = MAX(0, y);
284 w = MIN(w, win->env.scrw - 2 * win->bw);
285 h = MIN(h, win->env.scrh - 2 * win->bw);
286
287 if (win->x == x && win->y == y && win->w == w && win->h + win->barh == h)
288 return false;
289
290 win->x = x;
291 win->y = y;
292 win->w = w;
293 win->h = h - win->barh;
294
295 if (options->fixed_win)
296 win_set_sizehints(win);
297
298 XMoveResizeWindow(win->env.dpy, win->xwin, x, y, w, h);
299
300 return true;
301}
302
303void win_toggle_fullscreen(win_t *win) {
304 XEvent ev;
305 XClientMessageEvent *cm;
306
307 if (win == NULL || win->xwin == None)
308 return;
309
310 win->fullscreen = !win->fullscreen;
311
312 memset(&ev, 0, sizeof(ev));
313 ev.type = ClientMessage;
314
315 cm = &ev.xclient;
316 cm->window = win->xwin;
317 cm->message_type = XInternAtom(win->env.dpy, "_NET_WM_STATE", False);
318 cm->format = 32;
319 cm->data.l[0] = win->fullscreen;
320 cm->data.l[1] = XInternAtom(win->env.dpy, "_NET_WM_STATE_FULLSCREEN", False);
321 cm->data.l[2] = cm->data.l[3] = 0;
322
323 XSendEvent(win->env.dpy, DefaultRootWindow(win->env.dpy), False,
324 SubstructureNotifyMask | SubstructureRedirectMask, &ev);
325}
326
327void win_toggle_bar(win_t *win) {
328 if (win == NULL || win->xwin == None)
329 return;
330
331 if (win->barh != 0) {
332 win->h += win->barh;
333 win->barh = 0;
334 } else {
335 win->barh = font.ascent + font.descent + 2 * V_TEXT_PAD;
336 win->h -= win->barh;
337 }
338}
339
340void win_clear(win_t *win) {
341 int h;
342 win_env_t *e;
343
344 if (win == NULL || win->xwin == None)
345 return;
346
347 h = win->h + win->barh;
348 e = &win->env;
349
350 if (win->pm == None)
351 win->pm = XCreatePixmap(e->dpy, win->xwin, win->w, h, e->depth);
352
353 XSetForeground(e->dpy, gc, win->fullscreen ? win->fscol : win->bgcol);
354 XFillRectangle(e->dpy, win->pm, gc, 0, 0, win->w, h);
355}
356
357void win_draw_bar(win_t *win) {
358 win_env_t *e;
359 int len, x, y, w, tw = 0, seplen;
360 const char *rt;
361
362 if (win == NULL || win->xwin == None)
363 return;
364
365 e = &win->env;
366 x = H_TEXT_PAD;
367 y = win->h + font.ascent + V_TEXT_PAD;
368 w = win->w - 2 * H_TEXT_PAD;
369
370 XSetForeground(e->dpy, gc, win->barbgcol);
371 XFillRectangle(e->dpy, win->pm, gc, 0, win->h, win->w, win->barh);
372
373 XSetForeground(e->dpy, gc, win->barfgcol);
374 XSetBackground(e->dpy, gc, win->barbgcol);
375
376 if (win->lbar != NULL) {
377 len = strlen(win->lbar);
378 while (len > 0 && (tw = win_textwidth(win->lbar, len, false)) > w)
379 len--;
380 w -= tw + 2 * H_TEXT_PAD;
381 if (font.set)
382 XmbDrawString(e->dpy, win->pm, font.set, gc, x, y, win->lbar, len);
383 else
384 XDrawString(e->dpy, win->pm, gc, x, y, win->lbar, len);
385 }
386 if (win->rbar != NULL) {
387 len = strlen(win->rbar);
388 seplen = strlen(BAR_SEPARATOR);
389 rt = win->rbar;
390 while (len > 0 && (tw = win_textwidth(rt, len, false)) > w) {
391 rt = strstr(rt, BAR_SEPARATOR);
392 if (rt != NULL) {
393 rt += seplen;
394 len = strlen(rt);
395 } else {
396 len = 0;
397 }
398 }
399 if (len > 0) {
400 x = win->w - tw - H_TEXT_PAD;
401 if (font.set)
402 XmbDrawString(e->dpy, win->pm, font.set, gc, x, y, rt, len);
403 else
404 XDrawString(e->dpy, win->pm, gc, x, y, rt, len);
405 }
406 }
407}
408
409void win_draw(win_t *win) {
410 if (win == NULL || win->xwin == None)
411 return;
412
413 if (win->barh > 0)
414 win_draw_bar(win);
415
416 XCopyArea(win->env.dpy, win->pm, win->xwin, gc,
417 0, 0, win->w, win->h + win->barh, 0, 0);
418}
419
420void win_draw_rect(win_t *win, Pixmap pm, int x, int y, int w, int h,
421 bool fill, int lw, unsigned long col)
422{
423 XGCValues gcval;
424
425 if (win == NULL || pm == None)
426 return;
427
428 gcval.line_width = lw;
429 gcval.foreground = col;
430 XChangeGC(win->env.dpy, gc, GCForeground | GCLineWidth, &gcval);
431
432 if (fill)
433 XFillRectangle(win->env.dpy, pm, gc, x, y, w, h);
434 else
435 XDrawRectangle(win->env.dpy, pm, gc, x, y, w, h);
436}
437
438int win_textwidth(const char *text, unsigned int len, bool with_padding) {
439 XRectangle r;
440 int padding = with_padding ? 2 * H_TEXT_PAD : 0;
441
442 if (font.set) {
443 XmbTextExtents(font.set, text, len, NULL, &r);
444 return r.width + padding;
445 } else {
446 return XTextWidth(font.xfont, text, len) + padding;
447 }
448}
449
450void win_set_title(win_t *win, const char *title) {
451 if (win == NULL || win->xwin == None)
452 return;
453
454 if (title == NULL)
455 title = "sxiv";
456
457 XStoreName(win->env.dpy, win->xwin, title);
458 XSetIconName(win->env.dpy, win->xwin, title);
459
460 XChangeProperty(win->env.dpy, win->xwin,
461 XInternAtom(win->env.dpy, "_NET_WM_NAME", False),
462 XInternAtom(win->env.dpy, "UTF8_STRING", False), 8,
463 PropModeReplace, (unsigned char *) title, strlen(title));
464 XChangeProperty(win->env.dpy, win->xwin,
465 XInternAtom(win->env.dpy, "_NET_WM_ICON_NAME", False),
466 XInternAtom(win->env.dpy, "UTF8_STRING", False), 8,
467 PropModeReplace, (unsigned char *) title, strlen(title));
468}
469
470void win_set_bar_info(win_t *win, const char *li, const char *ri) {
471 if (win != NULL) {
472 win->lbar = li;
473 win->rbar = ri;
474 }
475}
476
477void win_set_cursor(win_t *win, cursor_t cursor) {
478 if (win == NULL || win->xwin == None)
479 return;
480
481 switch (cursor) {
482 case CURSOR_NONE:
483 XDefineCursor(win->env.dpy, win->xwin, cnone);
484 break;
485 case CURSOR_HAND:
486 XDefineCursor(win->env.dpy, win->xwin, chand);
487 break;
488 case CURSOR_WATCH:
489 XDefineCursor(win->env.dpy, win->xwin, cwatch);
490 break;
491 case CURSOR_ARROW:
492 default:
493 XDefineCursor(win->env.dpy, win->xwin, carrow);
494 break;
495 }
496
497 XFlush(win->env.dpy);
498}