Krolyxon
Commit 3f25b90

Branches

Refs →

nsxivwindow.c

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