Krolyxon
Commit 603e664

Branches

Refs →

nsxivwindow.c

261 lines · 6.0 KB · C

1/* sxiv: window.c
2 * Copyright (c) 2011 Bert Muennich <muennich at informatik.hu-berlin.de>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19#include <string.h>
20
21#include <X11/Xutil.h>
22#include <X11/cursorfont.h>
23
24#include "config.h"
25#include "options.h"
26#include "util.h"
27#include "window.h"
28
29static Cursor arrow;
30static Cursor hand;
31
32static GC bgc;
33
34void win_set_sizehints(win_t *win) {
35 XSizeHints sizehints;
36
37 if (!win)
38 return;
39
40 sizehints.flags = PMinSize | PMaxSize;
41 sizehints.min_width = win->w;
42 sizehints.max_width = win->w;
43 sizehints.min_height = win->h;
44 sizehints.max_height = win->h;
45 XSetWMNormalHints(win->env.dpy, win->xwin, &sizehints);
46}
47
48void win_open(win_t *win) {
49 win_env_t *e;
50 XClassHint classhint;
51 XColor bgcol;
52 int gmask;
53
54 if (!win)
55 return;
56
57 e = &win->env;
58 if (!(e->dpy = XOpenDisplay(NULL)))
59 die("could not open display");
60
61 e->scr = DefaultScreen(e->dpy);
62 e->scrw = DisplayWidth(e->dpy, e->scr);
63 e->scrh = DisplayHeight(e->dpy, e->scr);
64 e->vis = DefaultVisual(e->dpy, e->scr);
65 e->cmap = DefaultColormap(e->dpy, e->scr);
66 e->depth = DefaultDepth(e->dpy, e->scr);
67
68 if (!XAllocNamedColor(e->dpy, DefaultColormap(e->dpy, e->scr), BG_COLOR,
69 &bgcol, &bgcol))
70 die("could not allocate color: %s", BG_COLOR);
71
72 win->bgcol = bgcol.pixel;
73 win->pm = 0;
74
75 win->fullscreen = 0;
76
77 /* determine window offsets, width & height */
78 if (!options->geometry)
79 gmask = 0;
80 else
81 gmask = XParseGeometry(options->geometry, &win->x, &win->y,
82 &win->w, &win->h);
83 if (!(gmask & WidthValue))
84 win->w = WIN_WIDTH;
85 if (win->w > e->scrw)
86 win->w = e->scrw;
87 if (!(gmask & HeightValue))
88 win->h = WIN_HEIGHT;
89 if (win->h > e->scrh)
90 win->h = e->scrh;
91 if (!(gmask & XValue))
92 win->x = (e->scrw - win->w) / 2;
93 else if (gmask & XNegative)
94 win->x += e->scrw - win->w;
95 if (!(gmask & YValue))
96 win->y = (e->scrh - win->h) / 2;
97 else if (gmask & YNegative)
98 win->y += e->scrh - win->h;
99
100 win->xwin = XCreateWindow(e->dpy, RootWindow(e->dpy, e->scr),
101 win->x, win->y, win->w, win->h, 0,
102 e->depth, InputOutput, e->vis, 0, None);
103 if (win->xwin == None)
104 die("could not create window");
105
106 XSelectInput(e->dpy, win->xwin, StructureNotifyMask | KeyPressMask |
107 ButtonPressMask | ButtonReleaseMask | Button2MotionMask);
108
109 arrow = XCreateFontCursor(e->dpy, XC_left_ptr);
110 hand = XCreateFontCursor(e->dpy, XC_fleur);
111
112 bgc = XCreateGC(e->dpy, win->xwin, 0, None);
113
114 win_set_title(win, "sxiv");
115
116 classhint.res_name = "sxiv";
117 classhint.res_class = "sxiv";
118 XSetClassHint(e->dpy, win->xwin, &classhint);
119
120 if (options->fixed)
121 win_set_sizehints(win);
122
123 XMapWindow(e->dpy, win->xwin);
124 XFlush(e->dpy);
125
126 if (options->fullscreen)
127 win_toggle_fullscreen(win);
128}
129
130void win_close(win_t *win) {
131 if (!win)
132 return;
133
134 XFreeCursor(win->env.dpy, arrow);
135 XFreeCursor(win->env.dpy, hand);
136
137 XFreeGC(win->env.dpy, bgc);
138
139 XDestroyWindow(win->env.dpy, win->xwin);
140 XCloseDisplay(win->env.dpy);
141}
142
143int win_configure(win_t *win, XConfigureEvent *c) {
144 int changed;
145
146 if (!win)
147 return 0;
148
149 changed = win->w != c->width || win->h != c->height;
150
151 win->x = c->x;
152 win->y = c->y;
153 win->w = c->width;
154 win->h = c->height;
155 win->bw = c->border_width;
156
157 return changed;
158}
159
160int win_moveresize(win_t *win, int x, int y, unsigned int w, unsigned int h) {
161 if (!win)
162 return 0;
163
164 x = MAX(0, x);
165 y = MAX(0, y);
166 w = MIN(w, win->env.scrw - 2 * win->bw);
167 h = MIN(h, win->env.scrh - 2 * win->bw);
168
169 if (win->x == x && win->y == y && win->w == w && win->h == h)
170 return 0;
171
172 win->x = x;
173 win->y = y;
174 win->w = w;
175 win->h = h;
176
177 if (options->fixed)
178 win_set_sizehints(win);
179
180 XMoveResizeWindow(win->env.dpy, win->xwin, win->x, win->y, win->w, win->h);
181
182 return 1;
183}
184
185void win_toggle_fullscreen(win_t *win) {
186 XEvent ev;
187 XClientMessageEvent *cm;
188
189 if (!win)
190 return;
191
192 win->fullscreen ^= 1;
193
194 memset(&ev, 0, sizeof(ev));
195 ev.type = ClientMessage;
196
197 cm = &ev.xclient;
198 cm->window = win->xwin;
199 cm->message_type = XInternAtom(win->env.dpy, "_NET_WM_STATE", False);
200 cm->format = 32;
201 cm->data.l[0] = win->fullscreen;
202 cm->data.l[1] = XInternAtom(win->env.dpy, "_NET_WM_STATE_FULLSCREEN", False);
203 cm->data.l[2] = XInternAtom(win->env.dpy, "_NET_WM_STATE_ABOVE", False);
204 cm->data.l[3] = 0;
205
206 XSendEvent(win->env.dpy, DefaultRootWindow(win->env.dpy), False,
207 SubstructureNotifyMask, &ev);
208}
209
210void win_clear(win_t *win) {
211 win_env_t *e;
212 XGCValues gcval;
213
214 if (!win)
215 return;
216
217 e = &win->env;
218
219 if (win->pm)
220 XFreePixmap(e->dpy, win->pm);
221 win->pm = XCreatePixmap(e->dpy, win->xwin, e->scrw, e->scrh, e->depth);
222
223 gcval.foreground = win->fullscreen ? BlackPixel(e->dpy, e->scr) : win->bgcol;
224 XChangeGC(e->dpy, bgc, GCForeground, &gcval);
225
226 XFillRectangle(e->dpy, win->pm, bgc, 0, 0, e->scrw, e->scrh);
227}
228
229void win_draw(win_t *win) {
230 if (!win)
231 return;
232
233 XSetWindowBackgroundPixmap(win->env.dpy, win->xwin, win->pm);
234 XClearWindow(win->env.dpy, win->xwin);
235}
236
237void win_set_title(win_t *win, const char *title) {
238 if (!win)
239 return;
240
241 if (!title)
242 title = "sxiv";
243
244 XStoreName(win->env.dpy, win->xwin, title);
245 XSetIconName(win->env.dpy, win->xwin, title);
246}
247
248void win_set_cursor(win_t *win, win_cur_t cursor) {
249 if (!win)
250 return;
251
252 switch (cursor) {
253 case CURSOR_HAND:
254 XDefineCursor(win->env.dpy, win->xwin, hand);
255 break;
256 case CURSOR_ARROW:
257 default:
258 XDefineCursor(win->env.dpy, win->xwin, arrow);
259 break;
260 }
261}