Krolyxon
Commit e810072

Branches

Refs →

nsxivwindow.c

518 lines · 12.2 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 x = MAX(0, x);
291 y = MAX(0, y);
292 w = MIN(w, win->env.scrw - 2 * win->bw);
293 h = MIN(h, win->env.scrh - 2 * win->bw);
294
295 if (win->x == x && win->y == y && win->w == w && win->h + win->bar.h == h)
296 return false;
297
298 win->x = x;
299 win->y = y;
300 win->w = w;
301 win->h = h - win->bar.h;
302
303 if (options->fixed_win)
304 win_set_sizehints(win);
305
306 XMoveResizeWindow(win->env.dpy, win->xwin, x, y, w, h);
307
308 return true;
309}
310
311void win_toggle_fullscreen(win_t *win)
312{
313 XEvent ev;
314 XClientMessageEvent *cm;
315
316 if (win == NULL || win->xwin == None)
317 return;
318
319 win->fullscreen = !win->fullscreen;
320
321 memset(&ev, 0, sizeof(ev));
322 ev.type = ClientMessage;
323
324 cm = &ev.xclient;
325 cm->window = win->xwin;
326 cm->message_type = XInternAtom(win->env.dpy, "_NET_WM_STATE", False);
327 cm->format = 32;
328 cm->data.l[0] = win->fullscreen;
329 cm->data.l[1] = XInternAtom(win->env.dpy, "_NET_WM_STATE_FULLSCREEN", False);
330 cm->data.l[2] = cm->data.l[3] = 0;
331
332 XSendEvent(win->env.dpy, DefaultRootWindow(win->env.dpy), False,
333 SubstructureNotifyMask | SubstructureRedirectMask, &ev);
334}
335
336void win_toggle_bar(win_t *win)
337{
338 if (win == NULL || win->xwin == None)
339 return;
340
341 if (win->bar.h != 0) {
342 win->h += win->bar.h;
343 win->bar.h = 0;
344 } else {
345 win->bar.h = barheight;
346 win->h -= win->bar.h;
347 }
348}
349
350void win_clear(win_t *win)
351{
352 int h;
353 win_env_t *e;
354
355 if (win == NULL || win->xwin == None)
356 return;
357
358 h = win->h + win->bar.h;
359 e = &win->env;
360
361 if (win->pm == None)
362 win->pm = XCreatePixmap(e->dpy, win->xwin, win->w, h, e->depth);
363
364 XSetForeground(e->dpy, gc, win->fullscreen ? win->fscol : win->bgcol);
365 XFillRectangle(e->dpy, win->pm, gc, 0, 0, win->w, h);
366}
367
368void win_draw_bar(win_t *win)
369{
370 int len, olen, x, y, w, tw;
371 char rest[3];
372 const char *dots = "...";
373 win_env_t *e;
374
375 if (win == NULL || win->xwin == None || win->pm == None)
376 return;
377
378 e = &win->env;
379 y = win->h + font.ascent + V_TEXT_PAD;
380 w = win->w;
381
382 XSetForeground(e->dpy, gc, win->bar.bgcol);
383 XFillRectangle(e->dpy, win->pm, gc, 0, win->h, win->w, win->bar.h);
384
385 XSetForeground(e->dpy, gc, win->bar.fgcol);
386 XSetBackground(e->dpy, gc, win->bar.bgcol);
387
388 if (win->bar.r != NULL) {
389 len = strlen(win->bar.r);
390 if (len > 0) {
391 if ((tw = win_textwidth(win->bar.r, len, true)) > w)
392 return;
393 x = win->w - tw + H_TEXT_PAD;
394 w -= tw;
395 if (font.set)
396 XmbDrawString(e->dpy, win->pm, font.set, gc, x, y, win->bar.r, len);
397 else
398 XDrawString(e->dpy, win->pm, gc, x, y, win->bar.r, len);
399 }
400 }
401 if (win->bar.l != NULL) {
402 olen = len = strlen(win->bar.l);
403 while (len > 0 && (tw = win_textwidth(win->bar.l, len, true)) > w)
404 len--;
405 if (len > 0) {
406 if (len != olen) {
407 w = strlen(dots);
408 if (len <= w)
409 return;
410 memcpy(rest, win->bar.l + len - w, w);
411 memcpy(win->bar.l + len - w, dots, w);
412 }
413 x = H_TEXT_PAD;
414 if (font.set)
415 XmbDrawString(e->dpy, win->pm, font.set, gc, x, y, win->bar.l, len);
416 else
417 XDrawString(e->dpy, win->pm, gc, x, y, win->bar.l, len);
418 if (len != olen)
419 memcpy(win->bar.l + len - w, rest, w);
420 }
421 }
422}
423
424void win_draw(win_t *win)
425{
426 if (win == NULL || win->xwin == None || win->pm == None)
427 return;
428
429 if (win->bar.h > 0)
430 win_draw_bar(win);
431
432 XCopyArea(win->env.dpy, win->pm, win->xwin, gc,
433 0, 0, win->w, win->h + win->bar.h, 0, 0);
434}
435
436void win_draw_rect(win_t *win, Pixmap pm, int x, int y, int w, int h,
437 bool fill, int lw, unsigned long col)
438{
439 XGCValues gcval;
440
441 if (win == NULL || pm == None)
442 return;
443
444 gcval.line_width = lw;
445 gcval.foreground = col;
446 XChangeGC(win->env.dpy, gc, GCForeground | GCLineWidth, &gcval);
447
448 if (fill)
449 XFillRectangle(win->env.dpy, pm, gc, x, y, w, h);
450 else
451 XDrawRectangle(win->env.dpy, pm, gc, x, y, w, h);
452}
453
454int win_textwidth(const char *text, unsigned int len, bool with_padding)
455{
456 XRectangle r;
457 int padding = with_padding ? 2 * H_TEXT_PAD : 0;
458
459 if (font.set) {
460 XmbTextExtents(font.set, text, len, NULL, &r);
461 return r.width + padding;
462 } else {
463 return XTextWidth(font.xfont, text, len) + padding;
464 }
465}
466
467void win_set_title(win_t *win, const char *title)
468{
469 if (win == NULL || win->xwin == None)
470 return;
471
472 if (title == NULL)
473 title = "sxiv";
474
475 XStoreName(win->env.dpy, win->xwin, title);
476 XSetIconName(win->env.dpy, win->xwin, title);
477
478 XChangeProperty(win->env.dpy, win->xwin,
479 XInternAtom(win->env.dpy, "_NET_WM_NAME", False),
480 XInternAtom(win->env.dpy, "UTF8_STRING", False), 8,
481 PropModeReplace, (unsigned char *) title, strlen(title));
482 XChangeProperty(win->env.dpy, win->xwin,
483 XInternAtom(win->env.dpy, "_NET_WM_ICON_NAME", False),
484 XInternAtom(win->env.dpy, "UTF8_STRING", False), 8,
485 PropModeReplace, (unsigned char *) title, strlen(title));
486}
487
488void win_set_bar_info(win_t *win, char *linfo, char *rinfo)
489{
490 if (win != NULL) {
491 win->bar.l = linfo;
492 win->bar.r = rinfo;
493 }
494}
495
496void win_set_cursor(win_t *win, cursor_t cursor)
497{
498 if (win == NULL || win->xwin == None)
499 return;
500
501 switch (cursor) {
502 case CURSOR_NONE:
503 XDefineCursor(win->env.dpy, win->xwin, cnone);
504 break;
505 case CURSOR_HAND:
506 XDefineCursor(win->env.dpy, win->xwin, chand);
507 break;
508 case CURSOR_WATCH:
509 XDefineCursor(win->env.dpy, win->xwin, cwatch);
510 break;
511 case CURSOR_ARROW:
512 default:
513 XDefineCursor(win->env.dpy, win->xwin, carrow);
514 break;
515 }
516
517 XFlush(win->env.dpy);
518}