Krolyxon
Commit b752d5c

Branches

Refs →

nsxivwindow.c

462 lines · 11.3 KB · C

1/* sxiv: window.c
2 * Copyright (c) 2011 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->black = BlackPixel(e->dpy, e->scr);
114 win->white = WhitePixel(e->dpy, e->scr);
115
116 win->bgcol = win_alloc_color(win, WIN_BG_COLOR);
117 win->selcol = win_alloc_color(win, SEL_COLOR);
118 win->barbgcol = win_alloc_color(win, BAR_BG_COLOR);
119 win->barfgcol = win_alloc_color(win, BAR_FG_COLOR);
120
121 win->xwin = 0;
122 win->pm = 0;
123 win->fullscreen = false;
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
133void win_set_sizehints(win_t *win) {
134 XSizeHints sizehints;
135
136 if (win == NULL || win->xwin == None)
137 return;
138
139 sizehints.flags = PMinSize | PMaxSize;
140 sizehints.min_width = win->w;
141 sizehints.max_width = win->w;
142 sizehints.min_height = win->h + win->barh;
143 sizehints.max_height = win->h + win->barh;
144 XSetWMNormalHints(win->env.dpy, win->xwin, &sizehints);
145}
146
147void win_open(win_t *win) {
148 win_env_t *e;
149 XClassHint classhint;
150 XColor col;
151 char none_data[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
152 Pixmap none;
153 int gmask;
154
155 if (win == NULL)
156 return;
157
158 e = &win->env;
159
160 /* determine window offsets, width & height */
161 if (options->geometry == NULL)
162 gmask = 0;
163 else
164 gmask = XParseGeometry(options->geometry, &win->x, &win->y,
165 &win->w, &win->h);
166 if ((gmask & WidthValue) == 0)
167 win->w = WIN_WIDTH;
168 if (win->w > e->scrw)
169 win->w = e->scrw;
170 if ((gmask & HeightValue) == 0)
171 win->h = WIN_HEIGHT;
172 if (win->h > e->scrh)
173 win->h = e->scrh;
174 if ((gmask & XValue) == 0)
175 win->x = (e->scrw - win->w) / 2;
176 else if ((gmask & XNegative) != 0)
177 win->x += e->scrw - win->w;
178 if ((gmask & YValue) == 0)
179 win->y = (e->scrh - win->h) / 2;
180 else if ((gmask & YNegative) != 0)
181 win->y += e->scrh - win->h;
182
183 win->xwin = XCreateWindow(e->dpy, RootWindow(e->dpy, e->scr),
184 win->x, win->y, win->w, win->h, 0,
185 e->depth, InputOutput, e->vis, 0, None);
186 if (win->xwin == None)
187 die("could not create window");
188
189 XSelectInput(e->dpy, win->xwin, StructureNotifyMask | KeyPressMask |
190 ButtonPressMask | ButtonReleaseMask | PointerMotionMask);
191
192 carrow = XCreateFontCursor(e->dpy, XC_left_ptr);
193 chand = XCreateFontCursor(e->dpy, XC_fleur);
194 cwatch = XCreateFontCursor(e->dpy, XC_watch);
195
196 if (XAllocNamedColor(e->dpy, DefaultColormap(e->dpy, e->scr), "black",
197 &col, &col) == 0)
198 {
199 die("could not allocate color: black");
200 }
201 none = XCreateBitmapFromData(e->dpy, win->xwin, none_data, 8, 8);
202 cnone = XCreatePixmapCursor(e->dpy, none, none, &col, &col, 0, 0);
203
204 gc = XCreateGC(e->dpy, win->xwin, 0, None);
205
206 win_set_title(win, "sxiv");
207
208 classhint.res_name = "sxiv";
209 classhint.res_class = "sxiv";
210 XSetClassHint(e->dpy, win->xwin, &classhint);
211
212 win->barh = font.ascent + font.descent + 2 * V_TEXT_PAD;
213 win->h -= win->barh;
214
215 if (options->fixed_win)
216 win_set_sizehints(win);
217
218 XMapWindow(e->dpy, win->xwin);
219 XFlush(e->dpy);
220
221 wm_delete_win = XInternAtom(e->dpy, "WM_DELETE_WINDOW", False);
222 XSetWMProtocols(e->dpy, win->xwin, &wm_delete_win, 1);
223
224 if (options->fullscreen)
225 win_toggle_fullscreen(win);
226}
227
228void win_close(win_t *win) {
229 if (win == NULL || win->xwin == None)
230 return;
231
232 XFreeCursor(win->env.dpy, carrow);
233 XFreeCursor(win->env.dpy, cnone);
234 XFreeCursor(win->env.dpy, chand);
235 XFreeCursor(win->env.dpy, cwatch);
236
237 XFreeGC(win->env.dpy, gc);
238
239 XDestroyWindow(win->env.dpy, win->xwin);
240 XCloseDisplay(win->env.dpy);
241}
242
243bool win_configure(win_t *win, XConfigureEvent *c) {
244 bool changed;
245
246 if (win == NULL)
247 return false;
248
249 changed = win->w != c->width || win->h + win->barh != c->height;
250
251 win->x = c->x;
252 win->y = c->y;
253 win->w = c->width;
254 win->h = c->height - win->barh;
255 win->bw = c->border_width;
256
257 return changed;
258}
259
260bool win_moveresize(win_t *win, int x, int y, unsigned int w, unsigned int h) {
261 if (win == NULL || win->xwin == None)
262 return false;
263
264 x = MAX(0, x);
265 y = MAX(0, y);
266 w = MIN(w, win->env.scrw - 2 * win->bw);
267 h = MIN(h, win->env.scrh - 2 * win->bw);
268
269 if (win->x == x && win->y == y && win->w == w && win->h + win->barh == h)
270 return false;
271
272 win->x = x;
273 win->y = y;
274 win->w = w;
275 win->h = h - win->barh;
276
277 if (options->fixed_win)
278 win_set_sizehints(win);
279
280 XMoveResizeWindow(win->env.dpy, win->xwin, x, y, w, h);
281
282 return true;
283}
284
285void win_toggle_fullscreen(win_t *win) {
286 XEvent ev;
287 XClientMessageEvent *cm;
288
289 if (win == NULL || win->xwin == None)
290 return;
291
292 win->fullscreen = !win->fullscreen;
293
294 memset(&ev, 0, sizeof(ev));
295 ev.type = ClientMessage;
296
297 cm = &ev.xclient;
298 cm->window = win->xwin;
299 cm->message_type = XInternAtom(win->env.dpy, "_NET_WM_STATE", False);
300 cm->format = 32;
301 cm->data.l[0] = win->fullscreen;
302 cm->data.l[1] = XInternAtom(win->env.dpy, "_NET_WM_STATE_FULLSCREEN", False);
303 cm->data.l[2] = cm->data.l[3] = 0;
304
305 XSendEvent(win->env.dpy, DefaultRootWindow(win->env.dpy), False,
306 SubstructureNotifyMask | SubstructureRedirectMask, &ev);
307}
308
309void win_clear(win_t *win) {
310 win_env_t *e;
311
312 if (win == NULL || win->xwin == None)
313 return;
314
315 e = &win->env;
316
317 if (win->pm != None)
318 XFreePixmap(e->dpy, win->pm);
319 win->pm = XCreatePixmap(e->dpy, win->xwin, e->scrw, e->scrh, e->depth);
320
321 XSetForeground(e->dpy, gc, win->fullscreen ? win->black : win->bgcol);
322 XFillRectangle(e->dpy, win->pm, gc, 0, 0, e->scrw, e->scrh);
323}
324
325int win_textwidth(const char *text, unsigned int len) {
326 XRectangle r;
327
328 if (font.set) {
329 XmbTextExtents(font.set, text, len, NULL, &r);
330 return r.width;
331 } else {
332 return XTextWidth(font.xfont, text, len);
333 }
334}
335void win_draw_bar(win_t *win) {
336 win_env_t *e;
337 int len, x, y, w, tw = 0, seplen;
338 const char *rt;
339
340 if (win == NULL || win->xwin == None)
341 return;
342
343 e = &win->env;
344 x = H_TEXT_PAD;
345 y = win->h + font.ascent + V_TEXT_PAD;
346 w = win->w - 2 * H_TEXT_PAD;
347
348 XSetForeground(e->dpy, gc, win->barbgcol);
349 XFillRectangle(e->dpy, win->pm, gc, 0, win->h, win->w, win->barh);
350
351 XSetForeground(e->dpy, gc, win->barfgcol);
352 XSetBackground(e->dpy, gc, win->barbgcol);
353
354 if (win->lbar != NULL) {
355 len = strlen(win->lbar);
356 while (len > 0 && (tw = win_textwidth(win->lbar, len)) > w)
357 len--;
358 w -= tw + 2 * H_TEXT_PAD;
359 if (font.set)
360 XmbDrawString(e->dpy, win->pm, font.set, gc, x, y, win->lbar, len);
361 else
362 XDrawString(e->dpy, win->pm, gc, x, y, win->lbar, len);
363 }
364 if (win->rbar != NULL) {
365 len = strlen(win->rbar);
366 seplen = strlen(BAR_SEPARATOR);
367 rt = win->rbar;
368 while (len > 0 && (tw = win_textwidth(rt, len)) > w) {
369 rt = strstr(rt, BAR_SEPARATOR);
370 if (rt != NULL) {
371 rt += seplen;
372 len = strlen(rt);
373 } else {
374 len = 0;
375 }
376 }
377 if (len > 0) {
378 x = win->w - tw - H_TEXT_PAD;
379 if (font.set)
380 XmbDrawString(e->dpy, win->pm, font.set, gc, x, y, rt, len);
381 else
382 XDrawString(e->dpy, win->pm, gc, x, y, rt, len);
383 }
384 }
385}
386
387void win_draw(win_t *win) {
388 if (win == NULL || win->xwin == None)
389 return;
390
391 win_draw_bar(win);
392
393 XSetWindowBackgroundPixmap(win->env.dpy, win->xwin, win->pm);
394 XClearWindow(win->env.dpy, win->xwin);
395}
396
397void win_draw_rect(win_t *win, Pixmap pm, int x, int y, int w, int h,
398 bool fill, int lw, unsigned long col) {
399 XGCValues gcval;
400
401 if (win == NULL || pm == None)
402 return;
403
404 gcval.line_width = lw;
405 gcval.foreground = col;
406 XChangeGC(win->env.dpy, gc, GCForeground | GCLineWidth, &gcval);
407
408 if (fill)
409 XFillRectangle(win->env.dpy, pm, gc, x, y, w, h);
410 else
411 XDrawRectangle(win->env.dpy, pm, gc, x, y, w, h);
412}
413
414void win_set_title(win_t *win, const char *title) {
415 if (win == NULL || win->xwin == None)
416 return;
417
418 if (title == NULL)
419 title = "sxiv";
420
421 XStoreName(win->env.dpy, win->xwin, title);
422 XSetIconName(win->env.dpy, win->xwin, title);
423
424 XChangeProperty(win->env.dpy, win->xwin,
425 XInternAtom(win->env.dpy, "_NET_WM_NAME", False),
426 XInternAtom(win->env.dpy, "UTF8_STRING", False), 8,
427 PropModeReplace, (unsigned char *) title, strlen(title));
428 XChangeProperty(win->env.dpy, win->xwin,
429 XInternAtom(win->env.dpy, "_NET_WM_ICON_NAME", False),
430 XInternAtom(win->env.dpy, "UTF8_STRING", False), 8,
431 PropModeReplace, (unsigned char *) title, strlen(title));
432}
433
434void win_set_bar_info(win_t *win, const char *li, const char *ri) {
435 if (win != NULL) {
436 win->lbar = li;
437 win->rbar = ri;
438 }
439}
440
441void win_set_cursor(win_t *win, cursor_t cursor) {
442 if (win == NULL || win->xwin == None)
443 return;
444
445 switch (cursor) {
446 case CURSOR_NONE:
447 XDefineCursor(win->env.dpy, win->xwin, cnone);
448 break;
449 case CURSOR_HAND:
450 XDefineCursor(win->env.dpy, win->xwin, chand);
451 break;
452 case CURSOR_WATCH:
453 XDefineCursor(win->env.dpy, win->xwin, cwatch);
454 break;
455 case CURSOR_ARROW:
456 default:
457 XDefineCursor(win->env.dpy, win->xwin, carrow);
458 break;
459 }
460
461 XFlush(win->env.dpy);
462}