JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
draw.c is useless (belongs to main.c now)
[dwm.git] / client.c
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>
9
10 /* static */
11
12 static void
13 attachstack(Client *c) {
14         c->snext = stack;
15         stack = c;
16 }
17
18 static void
19 detachstack(Client *c) {
20         Client **tc;
21         for(tc=&stack; *tc && *tc != c; tc=&(*tc)->snext);
22         *tc = c->snext;
23 }
24
25 static void
26 grabbuttons(Client *c, Bool focused) {
27         XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
28
29         if(focused) {
30                 XGrabButton(dpy, Button1, MODKEY, c->win, False, BUTTONMASK,
31                                 GrabModeAsync, GrabModeSync, None, None);
32                 XGrabButton(dpy, Button1, MODKEY | LockMask, c->win, False, BUTTONMASK,
33                                 GrabModeAsync, GrabModeSync, None, None);
34                 XGrabButton(dpy, Button1, MODKEY | numlockmask, c->win, False, BUTTONMASK,
35                                 GrabModeAsync, GrabModeSync, None, None);
36                 XGrabButton(dpy, Button1, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
37                                 GrabModeAsync, GrabModeSync, None, None);
38
39                 XGrabButton(dpy, Button2, MODKEY, c->win, False, BUTTONMASK,
40                                 GrabModeAsync, GrabModeSync, None, None);
41                 XGrabButton(dpy, Button2, MODKEY | LockMask, c->win, False, BUTTONMASK,
42                                 GrabModeAsync, GrabModeSync, None, None);
43                 XGrabButton(dpy, Button2, MODKEY | numlockmask, c->win, False, BUTTONMASK,
44                                 GrabModeAsync, GrabModeSync, None, None);
45                 XGrabButton(dpy, Button2, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
46                                 GrabModeAsync, GrabModeSync, None, None);
47
48                 XGrabButton(dpy, Button3, MODKEY, c->win, False, BUTTONMASK,
49                                 GrabModeAsync, GrabModeSync, None, None);
50                 XGrabButton(dpy, Button3, MODKEY | LockMask, c->win, False, BUTTONMASK,
51                                 GrabModeAsync, GrabModeSync, None, None);
52                 XGrabButton(dpy, Button3, MODKEY | numlockmask, c->win, False, BUTTONMASK,
53                                 GrabModeAsync, GrabModeSync, None, None);
54                 XGrabButton(dpy, Button3, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
55                                 GrabModeAsync, GrabModeSync, None, None);
56         }
57         else
58                 XGrabButton(dpy, AnyButton, AnyModifier, c->win, False, BUTTONMASK,
59                                 GrabModeAsync, GrabModeSync, None, None);
60 }
61
62 static Bool
63 isprotodel(Client *c) {
64         int i, n;
65         Atom *protocols;
66         Bool ret = False;
67
68         if(XGetWMProtocols(dpy, c->win, &protocols, &n)) {
69                 for(i = 0; !ret && i < n; i++)
70                         if(protocols[i] == wmatom[WMDelete])
71                                 ret = True;
72                 XFree(protocols);
73         }
74         return ret;
75 }
76
77 static void
78 setclientstate(Client *c, long state) {
79         long data[] = {state, None};
80         XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
81                         PropModeReplace, (unsigned char *)data, 2);
82 }
83
84 static void
85 togglemax(Client *c) {
86         XEvent ev;
87
88         if(c->isfixed)
89                 return;
90         if((c->ismax = !c->ismax)) {
91                 c->rx = c->x;
92                 c->ry = c->y;
93                 c->rw = c->w;
94                 c->rh = c->h;
95                 resize(c, wax, way, waw - 2 * BORDERPX, wah - 2 * BORDERPX, True);
96         }
97         else
98                 resize(c, c->rx, c->ry, c->rw, c->rh, True);
99         while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
100 }
101
102 static int
103 xerrordummy(Display *dsply, XErrorEvent *ee) {
104         return 0;
105 }
106
107 /* extern */
108
109 void
110 attach(Client *c) {
111         if(clients)
112                 clients->prev = c;
113         c->next = clients;
114         clients = c;
115 }
116
117 void
118 configure(Client *c) {
119         XConfigureEvent ce;
120
121         ce.type = ConfigureNotify;
122         ce.display = dpy;
123         ce.event = c->win;
124         ce.window = c->win;
125         ce.x = c->x;
126         ce.y = c->y;
127         ce.width = c->w;
128         ce.height = c->h;
129         ce.border_width = c->border;
130         ce.above = None;
131         ce.override_redirect = False;
132         XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&ce);
133 }
134
135 void
136 detach(Client *c) {
137         if(c->prev)
138                 c->prev->next = c->next;
139         if(c->next)
140                 c->next->prev = c->prev;
141         if(c == clients)
142                 clients = c->next;
143         c->next = c->prev = NULL;
144 }
145
146 void
147 focus(Client *c) {
148         if(c && !isvisible(c))
149                 return;
150         if(sel && sel != c) {
151                 grabbuttons(sel, False);
152                 XSetWindowBorder(dpy, sel->win, dc.norm[ColBorder]);
153         }
154         if(c) {
155                 detachstack(c);
156                 attachstack(c);
157                 grabbuttons(c, True);
158         }
159         sel = c;
160         drawstatus();
161         if(!selscreen)
162                 return;
163         if(c) {
164                 XSetWindowBorder(dpy, c->win, dc.sel[ColBorder]);
165                 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
166         }
167         else
168                 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
169 }
170
171 void
172 focusnext(Arg *arg) {
173         Client *c;
174    
175         if(!sel)
176                 return;
177         for(c = sel->next; c && !isvisible(c); c = c->next);
178         if(!c)
179                 for(c = clients; c && !isvisible(c); c = c->next);
180         if(c) {
181                 focus(c);
182                 restack();
183         }
184 }
185
186 void
187 focusprev(Arg *arg) {
188         Client *c;
189
190         if(!sel)
191                 return;
192         for(c = sel->prev; c && !isvisible(c); c = c->prev);
193         if(!c) {
194                 for(c = clients; c && c->next; c = c->next);
195                 for(; c && !isvisible(c); c = c->prev);
196         }
197         if(c) {
198                 focus(c);
199                 restack();
200         }
201 }
202
203 void
204 killclient(Arg *arg) {
205         if(!sel)
206                 return;
207         if(isprotodel(sel))
208                 sendevent(sel->win, wmatom[WMProtocols], wmatom[WMDelete]);
209         else
210                 XKillClient(dpy, sel->win);
211 }
212
213 void
214 manage(Window w, XWindowAttributes *wa) {
215         Client *c, *t;
216         Window trans;
217         XWindowChanges wc;
218
219         c = emallocz(sizeof(Client));
220         c->tags = emallocz(ntags * sizeof(Bool));
221         c->win = w;
222         c->x = wa->x;
223         c->y = wa->y;
224         c->w = wa->width;
225         c->h = wa->height;
226         if(c->w == sw && c->h == sh) {
227                 c->border = 0;
228                 c->x = sx;
229                 c->y = sy;
230         }
231         else {
232                 c->border = BORDERPX;
233                 if(c->x + c->w + 2 * c->border > wax + waw)
234                         c->x = wax + waw - c->w - 2 * c->border;
235                 if(c->y + c->h + 2 * c->border > way + wah)
236                         c->y = way + wah - c->h - 2 * c->border;
237                 if(c->x < wax)
238                         c->x = wax;
239                 if(c->y < way)
240                         c->y = way;
241         }
242         updatesizehints(c);
243         XSelectInput(dpy, c->win,
244                 StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
245         XGetTransientForHint(dpy, c->win, &trans);
246         grabbuttons(c, False);
247         wc.border_width = c->border;
248         XConfigureWindow(dpy, c->win, CWBorderWidth, &wc);
249         XSetWindowBorder(dpy, c->win, dc.norm[ColBorder]);
250         configure(c); /* propagates border_width, if size doesn't change */
251         updatetitle(c);
252         for(t = clients; t && t->win != c->win; t = t->next);
253         settags(c, t);
254         if(!c->isfloat)
255                 c->isfloat = (t != 0) || c->isfixed;
256         attach(c);
257         attachstack(c);
258         c->isbanned = True;
259         XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
260         XMapWindow(dpy, c->win);
261         setclientstate(c, NormalState);
262         if(isvisible(c))
263                 focus(c);
264         arrange();
265 }
266
267 Client *
268 nexttiled(Client *c) {
269         for(; c && (c->isfloat || !isvisible(c)); c = c->next);
270         return c;
271 }
272
273 void
274 resize(Client *c, int x, int y, int w, int h, Bool sizehints) {
275         float actual, dx, dy, max, min;
276         XWindowChanges wc;
277
278         if(w <= 0 || h <= 0)
279                 return;
280         if(sizehints) {
281                 if(c->minw && w < c->minw)
282                         w = c->minw;
283                 if(c->minh && h < c->minh)
284                         h = c->minh;
285                 if(c->maxw && w > c->maxw)
286                         w = c->maxw;
287                 if(c->maxh && h > c->maxh)
288                         h = c->maxh;
289                 /* inspired by algorithm from fluxbox */
290                 if(c->minay > 0 && c->maxay && (h - c->baseh) > 0) {
291                         dx = (float)(w - c->basew);
292                         dy = (float)(h - c->baseh);
293                         min = (float)(c->minax) / (float)(c->minay);
294                         max = (float)(c->maxax) / (float)(c->maxay);
295                         actual = dx / dy;
296                         if(max > 0 && min > 0 && actual > 0) {
297                                 if(actual < min) {
298                                         dy = (dx * min + dy) / (min * min + 1);
299                                         dx = dy * min;
300                                         w = (int)dx + c->basew;
301                                         h = (int)dy + c->baseh;
302                                 }
303                                 else if(actual > max) {
304                                         dy = (dx * min + dy) / (max * max + 1);
305                                         dx = dy * min;
306                                         w = (int)dx + c->basew;
307                                         h = (int)dy + c->baseh;
308                                 }
309                         }
310                 }
311                 if(c->incw)
312                         w -= (w - c->basew) % c->incw;
313                 if(c->inch)
314                         h -= (h - c->baseh) % c->inch;
315         }
316         if(w == sw && h == sh)
317                 c->border = 0;
318         else
319                 c->border = BORDERPX;
320         /* offscreen appearance fixes */
321         if(x > sw)
322                 x = sw - w - 2 * c->border;
323         if(y > sh)
324                 y = sh - h - 2 * c->border;
325         if(x + w + 2 * c->border < sx)
326                 x = sx;
327         if(y + h + 2 * c->border < sy)
328                 y = sy;
329         if(c->x != x || c->y != y || c->w != w || c->h != h) {
330                 c->x = wc.x = x;
331                 c->y = wc.y = y;
332                 c->w = wc.width = w;
333                 c->h = wc.height = h;
334                 wc.border_width = c->border;
335                 XConfigureWindow(dpy, c->win, CWX | CWY | CWWidth | CWHeight | CWBorderWidth, &wc);
336                 configure(c);
337                 XSync(dpy, False);
338         }
339 }
340
341 void
342 updatesizehints(Client *c) {
343         long msize;
344         XSizeHints size;
345
346         if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
347                 size.flags = PSize;
348         c->flags = size.flags;
349         if(c->flags & PBaseSize) {
350                 c->basew = size.base_width;
351                 c->baseh = size.base_height;
352         }
353         else
354                 c->basew = c->baseh = 0;
355         if(c->flags & PResizeInc) {
356                 c->incw = size.width_inc;
357                 c->inch = size.height_inc;
358         }
359         else
360                 c->incw = c->inch = 0;
361         if(c->flags & PMaxSize) {
362                 c->maxw = size.max_width;
363                 c->maxh = size.max_height;
364         }
365         else
366                 c->maxw = c->maxh = 0;
367         if(c->flags & PMinSize) {
368                 c->minw = size.min_width;
369                 c->minh = size.min_height;
370         }
371         else
372                 c->minw = c->minh = 0;
373         if(c->flags & PAspect) {
374                 c->minax = size.min_aspect.x;
375                 c->minay = size.min_aspect.y;
376                 c->maxax = size.max_aspect.x;
377                 c->maxay = size.max_aspect.y;
378         }
379         else
380                 c->minax = c->minay = c->maxax = c->maxay = 0;
381         c->isfixed = (c->maxw && c->minw && c->maxh && c->minh
382                         && c->maxw == c->minw && c->maxh == c->minh);
383 }
384
385 void
386 updatetitle(Client *c) {
387         char **list = NULL;
388         int n;
389         XTextProperty name;
390
391         name.nitems = 0;
392         c->name[0] = 0;
393         XGetTextProperty(dpy, c->win, &name, netatom[NetWMName]);
394         if(!name.nitems)
395                 XGetWMName(dpy, c->win, &name);
396         if(!name.nitems)
397                 return;
398         if(name.encoding == XA_STRING)
399                 strncpy(c->name, (char *)name.value, sizeof c->name);
400         else {
401                 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
402                 && n > 0 && *list)
403                 {
404                         strncpy(c->name, *list, sizeof c->name);
405                         XFreeStringList(list);
406                 }
407         }
408         XFree(name.value);
409 }
410
411 void
412 unmanage(Client *c) {
413         Client *nc;
414
415         /* The server grab construct avoids race conditions. */
416         XGrabServer(dpy);
417         XSetErrorHandler(xerrordummy);
418         detach(c);
419         detachstack(c);
420         if(sel == c) {
421                 for(nc = stack; nc && !isvisible(nc); nc = nc->snext);
422                 focus(nc);
423         }
424         XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
425         setclientstate(c, WithdrawnState);
426         free(c->tags);
427         free(c);
428         XSync(dpy, False);
429         XSetErrorHandler(xerror);
430         XUngrabServer(dpy);
431         arrange();
432 }
433
434 void
435 zoom(Arg *arg) {
436         unsigned int n;
437         Client *c;
438
439         if(!sel)
440                 return;
441         if(sel->isfloat || (arrange == dofloat)) {
442                 togglemax(sel);
443                 return;
444         }
445         for(n = 0, c = nexttiled(clients); c; c = nexttiled(c->next))
446                 n++;
447
448         if((c = sel) == nexttiled(clients))
449                 if(!(c = nexttiled(c->next)))
450                         return;
451         detach(c);
452         attach(c);
453         focus(c);
454         arrange();
455 }