aewl

view client.c @ 737:f05da68014bd

making the bar appear at bottom
author Anselm R. Garbe <arg@suckless.org>
date Thu, 08 Feb 2007 11:08:40 +0100
parents 1950833a5614
children 743061dbe7f2
line source
1 /* (C)opyright MMVI-MMVII Anselm R. Garbe <garbeam at gmail dot com>
2 * See LICENSE file for license details.
3 */
4 #include "dwm.h"
5 #include <stdlib.h>
6 #include <string.h>
7 #include <X11/Xatom.h>
8 #include <X11/Xutil.h>
10 /* static */
12 static void
13 detachstack(Client *c) {
14 Client **tc;
15 for(tc=&stack; *tc && *tc != c; tc=&(*tc)->snext);
16 *tc = c->snext;
17 }
19 static void
20 grabbuttons(Client *c, Bool focused) {
21 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
23 if(focused) {
24 XGrabButton(dpy, Button1, MODKEY, c->win, False, BUTTONMASK,
25 GrabModeAsync, GrabModeSync, None, None);
26 XGrabButton(dpy, Button1, MODKEY | LockMask, c->win, False, BUTTONMASK,
27 GrabModeAsync, GrabModeSync, None, None);
28 XGrabButton(dpy, Button1, MODKEY | numlockmask, c->win, False, BUTTONMASK,
29 GrabModeAsync, GrabModeSync, None, None);
30 XGrabButton(dpy, Button1, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
31 GrabModeAsync, GrabModeSync, None, None);
33 XGrabButton(dpy, Button2, MODKEY, c->win, False, BUTTONMASK,
34 GrabModeAsync, GrabModeSync, None, None);
35 XGrabButton(dpy, Button2, MODKEY | LockMask, c->win, False, BUTTONMASK,
36 GrabModeAsync, GrabModeSync, None, None);
37 XGrabButton(dpy, Button2, MODKEY | numlockmask, c->win, False, BUTTONMASK,
38 GrabModeAsync, GrabModeSync, None, None);
39 XGrabButton(dpy, Button2, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
40 GrabModeAsync, GrabModeSync, None, None);
42 XGrabButton(dpy, Button3, MODKEY, c->win, False, BUTTONMASK,
43 GrabModeAsync, GrabModeSync, None, None);
44 XGrabButton(dpy, Button3, MODKEY | LockMask, c->win, False, BUTTONMASK,
45 GrabModeAsync, GrabModeSync, None, None);
46 XGrabButton(dpy, Button3, MODKEY | numlockmask, c->win, False, BUTTONMASK,
47 GrabModeAsync, GrabModeSync, None, None);
48 XGrabButton(dpy, Button3, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
49 GrabModeAsync, GrabModeSync, None, None);
50 }
51 else
52 XGrabButton(dpy, AnyButton, AnyModifier, c->win, False, BUTTONMASK,
53 GrabModeAsync, GrabModeSync, None, None);
54 }
56 static void
57 setclientstate(Client *c, long state) {
58 long data[] = {state, None};
59 XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
60 PropModeReplace, (unsigned char *)data, 2);
61 }
63 static int
64 xerrordummy(Display *dsply, XErrorEvent *ee) {
65 return 0;
66 }
68 /* extern */
70 void
71 configure(Client *c) {
72 XEvent synev;
74 synev.type = ConfigureNotify;
75 synev.xconfigure.display = dpy;
76 synev.xconfigure.event = c->win;
77 synev.xconfigure.window = c->win;
78 synev.xconfigure.x = c->x;
79 synev.xconfigure.y = c->y;
80 synev.xconfigure.width = c->w;
81 synev.xconfigure.height = c->h;
82 synev.xconfigure.border_width = c->border;
83 synev.xconfigure.above = None;
84 XSendEvent(dpy, c->win, True, NoEventMask, &synev);
85 }
87 void
88 focus(Client *c) {
89 if(c && !isvisible(c))
90 return;
91 if(sel && sel != c) {
92 grabbuttons(sel, False);
93 XSetWindowBorder(dpy, sel->win, dc.norm[ColBorder]);
94 }
95 if(c) {
96 detachstack(c);
97 c->snext = stack;
98 stack = c;
99 grabbuttons(c, True);
100 }
101 sel = c;
102 drawstatus();
103 if(!selscreen)
104 return;
105 if(c) {
106 XSetWindowBorder(dpy, c->win, dc.sel[ColBorder]);
107 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
108 }
109 else
110 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
111 }
113 Client *
114 getclient(Window w) {
115 Client *c;
117 for(c = clients; c; c = c->next)
118 if(c->win == w)
119 return c;
120 return NULL;
121 }
123 Bool
124 isprotodel(Client *c) {
125 int i, n;
126 Atom *protocols;
127 Bool ret = False;
129 if(XGetWMProtocols(dpy, c->win, &protocols, &n)) {
130 for(i = 0; !ret && i < n; i++)
131 if(protocols[i] == wmatom[WMDelete])
132 ret = True;
133 XFree(protocols);
134 }
135 return ret;
136 }
138 void
139 killclient(Arg *arg) {
140 if(!sel)
141 return;
142 if(isprotodel(sel))
143 sendevent(sel->win, wmatom[WMProtocols], wmatom[WMDelete]);
144 else
145 XKillClient(dpy, sel->win);
146 }
148 void
149 manage(Window w, XWindowAttributes *wa) {
150 Client *c;
151 Window trans;
153 c = emallocz(sizeof(Client));
154 c->tags = emallocz(ntags * sizeof(Bool));
155 c->win = w;
156 c->x = wa->x;
157 c->y = wa->y;
158 c->w = wa->width;
159 c->h = wa->height;
160 if(c->w == sw && c->h == sh) {
161 c->border = 0;
162 c->x = sx;
163 c->y = sy;
164 }
165 else {
166 c->border = BORDERPX;
167 if(c->x + c->w + 2 * c->border > wax + waw)
168 c->x = wax + waw - c->w - 2 * c->border;
169 if(c->y + c->h + 2 * c->border > way + wah)
170 c->y = way + wah - c->h - 2 * c->border;
171 if(c->x < wax)
172 c->x = wax;
173 if(c->y < way)
174 c->y = way;
175 }
176 updatesizehints(c);
177 XSelectInput(dpy, c->win,
178 StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
179 XGetTransientForHint(dpy, c->win, &trans);
180 grabbuttons(c, False);
181 XSetWindowBorder(dpy, c->win, dc.norm[ColBorder]);
182 updatetitle(c);
183 settags(c, getclient(trans));
184 if(!c->isfloat)
185 c->isfloat = trans || c->isfixed;
186 if(clients)
187 clients->prev = c;
188 c->next = clients;
189 c->snext = stack;
190 stack = clients = c;
191 XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
192 XMapWindow(dpy, c->win);
193 setclientstate(c, NormalState);
194 if(isvisible(c))
195 focus(c);
196 arrange();
197 }
199 void
200 resize(Client *c, Bool sizehints) {
201 float actual, dx, dy, max, min;
202 XWindowChanges wc;
204 if(c->w <= 0 || c->h <= 0)
205 return;
206 if(sizehints) {
207 if(c->minw && c->w < c->minw)
208 c->w = c->minw;
209 if(c->minh && c->h < c->minh)
210 c->h = c->minh;
211 if(c->maxw && c->w > c->maxw)
212 c->w = c->maxw;
213 if(c->maxh && c->h > c->maxh)
214 c->h = c->maxh;
215 /* inspired by algorithm from fluxbox */
216 if(c->minay > 0 && c->maxay && (c->h - c->baseh) > 0) {
217 dx = (float)(c->w - c->basew);
218 dy = (float)(c->h - c->baseh);
219 min = (float)(c->minax) / (float)(c->minay);
220 max = (float)(c->maxax) / (float)(c->maxay);
221 actual = dx / dy;
222 if(max > 0 && min > 0 && actual > 0) {
223 if(actual < min) {
224 dy = (dx * min + dy) / (min * min + 1);
225 dx = dy * min;
226 c->w = (int)dx + c->basew;
227 c->h = (int)dy + c->baseh;
228 }
229 else if(actual > max) {
230 dy = (dx * min + dy) / (max * max + 1);
231 dx = dy * min;
232 c->w = (int)dx + c->basew;
233 c->h = (int)dy + c->baseh;
234 }
235 }
236 }
237 if(c->incw)
238 c->w -= (c->w - c->basew) % c->incw;
239 if(c->inch)
240 c->h -= (c->h - c->baseh) % c->inch;
241 }
242 if(c->w == sw && c->h == sh)
243 c->border = 0;
244 else
245 c->border = BORDERPX;
246 /* offscreen appearance fixes */
247 if(c->x > sw)
248 c->x = sw - c->w - 2 * c->border;
249 if(c->y > sh)
250 c->y = sh - c->h - 2 * c->border;
251 if(c->x + c->w + 2 * c->border < sx)
252 c->x = sx;
253 if(c->y + c->h + 2 * c->border < sy)
254 c->y = sy;
255 wc.x = c->x;
256 wc.y = c->y;
257 wc.width = c->w;
258 wc.height = c->h;
259 wc.border_width = c->border;
260 XConfigureWindow(dpy, c->win, CWX | CWY | CWWidth | CWHeight | CWBorderWidth, &wc);
261 configure(c);
262 XSync(dpy, False);
263 }
265 void
266 updatesizehints(Client *c) {
267 long msize;
268 XSizeHints size;
270 if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
271 size.flags = PSize;
272 c->flags = size.flags;
273 if(c->flags & PBaseSize) {
274 c->basew = size.base_width;
275 c->baseh = size.base_height;
276 }
277 else
278 c->basew = c->baseh = 0;
279 if(c->flags & PResizeInc) {
280 c->incw = size.width_inc;
281 c->inch = size.height_inc;
282 }
283 else
284 c->incw = c->inch = 0;
285 if(c->flags & PMaxSize) {
286 c->maxw = size.max_width;
287 c->maxh = size.max_height;
288 }
289 else
290 c->maxw = c->maxh = 0;
291 if(c->flags & PMinSize) {
292 c->minw = size.min_width;
293 c->minh = size.min_height;
294 }
295 else
296 c->minw = c->minh = 0;
297 if(c->flags & PAspect) {
298 c->minax = size.min_aspect.x;
299 c->minay = size.min_aspect.y;
300 c->maxax = size.max_aspect.x;
301 c->maxay = size.max_aspect.y;
302 }
303 else
304 c->minax = c->minay = c->maxax = c->maxay = 0;
305 c->isfixed = (c->maxw && c->minw && c->maxh && c->minh &&
306 c->maxw == c->minw && c->maxh == c->minh);
307 }
309 void
310 updatetitle(Client *c) {
311 char **list = NULL;
312 int n;
313 XTextProperty name;
315 name.nitems = 0;
316 c->name[0] = 0;
317 XGetTextProperty(dpy, c->win, &name, netatom[NetWMName]);
318 if(!name.nitems)
319 XGetWMName(dpy, c->win, &name);
320 if(!name.nitems)
321 return;
322 if(name.encoding == XA_STRING)
323 strncpy(c->name, (char *)name.value, sizeof c->name);
324 else {
325 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
326 && n > 0 && *list)
327 {
328 strncpy(c->name, *list, sizeof c->name);
329 XFreeStringList(list);
330 }
331 }
332 XFree(name.value);
333 }
335 void
336 unmanage(Client *c) {
337 Client *nc;
339 /* The server grab construct avoids race conditions. */
340 XGrabServer(dpy);
341 XSetErrorHandler(xerrordummy);
342 detach(c);
343 detachstack(c);
344 if(sel == c) {
345 for(nc = stack; nc && !isvisible(nc); nc = nc->snext);
346 focus(nc);
347 }
348 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
349 setclientstate(c, WithdrawnState);
350 free(c->tags);
351 free(c);
352 XSync(dpy, False);
353 XSetErrorHandler(xerror);
354 XUngrabServer(dpy);
355 arrange();
356 }