Krolyxon
Commit bf41012

Branches

Refs →

nsxivwindow.c

555 lines · 13.0 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 win->sizehints.flags = PWinGravity;
131 win->sizehints.win_gravity = NorthWestGravity;
132
133 if (setlocale(LC_CTYPE, "") == NULL || XSupportsLocale() == 0)
134 warn("no locale support");
135
136 win_init_font(e->dpy, BAR_FONT);
137
138 wm_delete_win = XInternAtom(e->dpy, "WM_DELETE_WINDOW", False);
139}
140
141void win_set_sizehints(win_t *win)
142{
143
144 if (win == NULL || win->xwin == None)
145 return;
146
147 if ((win->sizehints.flags & PMinSize) == 1) {
148 win->sizehints.min_width = win->w;
149 win->sizehints.min_height = win->h + win->bar.h;
150 }
151 if ((win->sizehints.flags & PMaxSize) == 1) {
152 win->sizehints.max_width = win->w;
153 win->sizehints.max_height = win->h + win->bar.h;
154 }
155 if ((win->sizehints.flags & USPosition) == 1) {
156 win->sizehints.x = win->x;
157 win->sizehints.y = win->y;
158 }
159
160 XSetWMNormalHints(win->env.dpy, win->xwin, &win->sizehints);
161}
162
163void win_open(win_t *win)
164{
165 win_env_t *e;
166 XClassHint classhint;
167 XColor col;
168 char none_data[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
169 Pixmap none;
170 int gmask;
171
172 if (win == NULL)
173 return;
174
175 e = &win->env;
176
177 /* determine window offsets, width & height */
178 if (options->geometry == NULL)
179 gmask = 0;
180 else
181 gmask = XParseGeometry(options->geometry, &win->x, &win->y,
182 &win->w, &win->h);
183 if ((gmask & WidthValue) == 0) {
184 win->w = WIN_WIDTH;
185 } else {
186 win->sizehints.flags |= USSize;
187 }
188 if (win->w > e->scrw)
189 win->w = e->scrw;
190 if ((gmask & HeightValue) == 0) {
191 win->h = WIN_HEIGHT;
192 } else {
193 win->sizehints.flags |= USSize;
194 }
195 if (win->h > e->scrh)
196 win->h = e->scrh;
197 if ((gmask & XValue) == 0) {
198 win->x = (e->scrw - win->w) / 2;
199 } else {
200 if ((gmask & XNegative) != 0) {
201 win->x += e->scrw - win->w;
202 win->sizehints.win_gravity = NorthEastGravity;
203 }
204 win->sizehints.flags |= USPosition;
205 }
206 if ((gmask & YValue) == 0) {
207 win->y = (e->scrh - win->h) / 2;
208 } else {
209 if ((gmask & YNegative) != 0) {
210 win->y += e->scrh - win->h;
211 if (win->sizehints.win_gravity == NorthEastGravity) {
212 win->sizehints.win_gravity = SouthEastGravity;
213 } else {
214 win->sizehints.win_gravity = SouthWestGravity;
215 }
216 }
217 win->sizehints.flags |= USPosition;
218 }
219
220 win->xwin = XCreateWindow(e->dpy, RootWindow(e->dpy, e->scr),
221 win->x, win->y, win->w, win->h, 0,
222 e->depth, InputOutput, e->vis, 0, None);
223 if (win->xwin == None)
224 die("could not create window");
225
226 XSelectInput(e->dpy, win->xwin,
227 ExposureMask | ButtonReleaseMask | ButtonPressMask |
228 KeyPressMask | PointerMotionMask | StructureNotifyMask);
229
230 carrow = XCreateFontCursor(e->dpy, XC_left_ptr);
231 chand = XCreateFontCursor(e->dpy, XC_fleur);
232 cwatch = XCreateFontCursor(e->dpy, XC_watch);
233
234 if (XAllocNamedColor(e->dpy, DefaultColormap(e->dpy, e->scr), "black",
235 &col, &col) == 0)
236 {
237 die("could not allocate color: black");
238 }
239 none = XCreateBitmapFromData(e->dpy, win->xwin, none_data, 8, 8);
240 cnone = XCreatePixmapCursor(e->dpy, none, none, &col, &col, 0, 0);
241
242 gc = XCreateGC(e->dpy, win->xwin, 0, None);
243
244 win_set_title(win, "sxiv");
245
246 classhint.res_class = "Sxiv";
247 classhint.res_name = options->res_name != NULL ? options->res_name : "sxiv";
248 XSetClassHint(e->dpy, win->xwin, &classhint);
249
250 XSetWMProtocols(e->dpy, win->xwin, &wm_delete_win, 1);
251
252 if (!options->hide_bar) {
253 win->bar.h = barheight;
254 win->h -= win->bar.h;
255 }
256
257 if (options->fixed_win)
258 win->sizehints.flags |= PMinSize | PMaxSize;
259
260 win_set_sizehints(win);
261
262 XMapWindow(e->dpy, win->xwin);
263 XFlush(e->dpy);
264
265 if (options->fullscreen)
266 win_toggle_fullscreen(win);
267}
268
269void win_close(win_t *win)
270{
271 if (win == NULL || win->xwin == None)
272 return;
273
274 XFreeCursor(win->env.dpy, carrow);
275 XFreeCursor(win->env.dpy, cnone);
276 XFreeCursor(win->env.dpy, chand);
277 XFreeCursor(win->env.dpy, cwatch);
278
279 XFreeGC(win->env.dpy, gc);
280
281 XDestroyWindow(win->env.dpy, win->xwin);
282 XCloseDisplay(win->env.dpy);
283}
284
285bool win_configure(win_t *win, XConfigureEvent *c)
286{
287 bool changed;
288
289 if (win == NULL || c == NULL)
290 return false;
291
292 if ((changed = win->w != c->width || win->h + win->bar.h != c->height)) {
293 if (win->pm != None) {
294 XFreePixmap(win->env.dpy, win->pm);
295 win->pm = None;
296 }
297 }
298
299 win->x = c->x;
300 win->y = c->y;
301 win->w = c->width;
302 win->h = c->height - win->bar.h;
303 win->bw = c->border_width;
304
305 return changed;
306}
307
308void win_expose(win_t *win, XExposeEvent *e)
309{
310 if (win == NULL || win->xwin == None || win->pm == None || e == NULL)
311 return;
312
313 XCopyArea(win->env.dpy, win->pm, win->xwin, gc,
314 e->x, e->y, e->width, e->height, e->x, e->y);
315}
316
317bool win_moveresize(win_t *win, int x, int y, unsigned int w, unsigned int h)
318{
319 if (win == NULL || win->xwin == None)
320 return false;
321
322 x = MAX(0, x);
323 y = MAX(0, y);
324 w = MIN(w, win->env.scrw - 2 * win->bw);
325 h = MIN(h, win->env.scrh - 2 * win->bw);
326
327 if (win->x == x && win->y == y && win->w == w && win->h + win->bar.h == h)
328 return false;
329
330 win->x = x;
331 win->y = y;
332 win->w = w;
333 win->h = h - win->bar.h;
334
335
336 win_set_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}