Krolyxon
Commit a527025

Branches

Refs →

nsxivwindow.c

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