Krolyxon
Commit b845827

Branches

Refs →

nsxivwindow.c

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