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