JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
after switching to OpenBSD again, I switched back to a saner color scheme
[dwm.git] / client.c
1 /*
2  * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
3  * See LICENSE file for license details.
4  */
5 #include "dwm.h"
6 #include <stdlib.h>
7 #include <string.h>
8 #include <X11/Xatom.h>
9 #include <X11/Xutil.h>
10
11 /* static functions */
12
13 static void
14 grabbutton(Client *c, unsigned int button, unsigned int modifier)
15 {
16         XGrabButton(dpy, button, modifier, c->win, False, BUTTONMASK,
17                         GrabModeAsync, GrabModeSync, None, None);
18         XGrabButton(dpy, button, modifier | LockMask, c->win, False, BUTTONMASK,
19                         GrabModeAsync, GrabModeSync, None, None);
20         XGrabButton(dpy, button, modifier | numlockmask, c->win, False, BUTTONMASK,
21                         GrabModeAsync, GrabModeSync, None, None);
22         XGrabButton(dpy, button, modifier | numlockmask | LockMask, c->win, False, BUTTONMASK,
23                         GrabModeAsync, GrabModeSync, None, None);
24 }
25
26 static void
27 resizetitle(Client *c)
28 {
29         int i;
30
31         c->tw = 0;
32         for(i = 0; i < ntags; i++)
33                 if(c->tags[i])
34                         c->tw += textw(tags[i]);
35         c->tw += textw(c->name);
36         if(c->tw > c->w)
37                 c->tw = c->w + 2;
38         c->tx = c->x + c->w - c->tw + 2;
39         c->ty = c->y;
40         if(isvisible(c))
41                 XMoveResizeWindow(dpy, c->title, c->tx, c->ty, c->tw, c->th);
42         else
43                 XMoveResizeWindow(dpy, c->title, c->tx + 2 * sw, c->ty, c->tw, c->th);
44
45 }
46
47 static void
48 ungrabbutton(Client *c, unsigned int button, unsigned int modifier)
49 {
50         XUngrabButton(dpy, button, modifier, c->win);
51         XUngrabButton(dpy, button, modifier | LockMask, c->win);
52         XUngrabButton(dpy, button, modifier | numlockmask, c->win);
53         XUngrabButton(dpy, button, modifier | numlockmask | LockMask, c->win);
54 }
55
56 static int
57 xerrordummy(Display *dsply, XErrorEvent *ee)
58 {
59         return 0;
60 }
61
62 /* extern functions */
63
64 void
65 ban(Client *c)
66 {
67         XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
68         XMoveWindow(dpy, c->title, c->tx + 2 * sw, c->ty);
69 }
70
71 void
72 focus(Client *c)
73 {
74         Client *old = sel;
75
76         if(!issel)
77                 return;
78         if(!sel)
79                 sel = c;
80         else if(sel != c) {
81                 if(sel->ismax)
82                         togglemax(NULL);
83                 sel = c;
84                 grabbutton(old, AnyButton, 0);
85                 drawtitle(old);
86         }
87         ungrabbutton(c, AnyButton, 0);
88         drawtitle(c);
89         XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
90 }
91
92 void
93 focusnext(Arg *arg)
94 {
95         Client *c;
96    
97         if(!sel)
98                 return;
99
100         if(!(c = getnext(sel->next)))
101                 c = getnext(clients);
102         if(c) {
103                 focus(c);
104                 restack();
105         }
106 }
107
108 void
109 focusprev(Arg *arg)
110 {
111         Client *c;
112
113         if(!sel)
114                 return;
115
116         if(!(c = getprev(sel->prev))) {
117                 for(c = clients; c && c->next; c = c->next);
118                 c = getprev(c);
119         }
120         if(c) {
121                 focus(c);
122                 restack();
123         }
124 }
125
126 Client *
127 getclient(Window w)
128 {
129         Client *c;
130
131         for(c = clients; c; c = c->next)
132                 if(c->win == w)
133                         return c;
134         return NULL;
135 }
136
137 Client *
138 getctitle(Window w)
139 {
140         Client *c;
141
142         for(c = clients; c; c = c->next)
143                 if(c->title == w)
144                         return c;
145         return NULL;
146 }
147
148 void
149 gravitate(Client *c, Bool invert)
150 {
151         int dx = 0, dy = 0;
152
153         switch(c->grav) {
154         default:
155                 break;
156         case StaticGravity:
157         case NorthWestGravity:
158         case NorthGravity:
159         case NorthEastGravity:
160                 dy = c->border;
161                 break;
162         case EastGravity:
163         case CenterGravity:
164         case WestGravity:
165                 dy = -(c->h / 2) + c->border;
166                 break;
167         case SouthEastGravity:
168         case SouthGravity:
169         case SouthWestGravity:
170                 dy = -(c->h);
171                 break;
172         }
173
174         switch (c->grav) {
175         default:
176                 break;
177         case StaticGravity:
178         case NorthWestGravity:
179         case WestGravity:
180         case SouthWestGravity:
181                 dx = c->border;
182                 break;
183         case NorthGravity:
184         case CenterGravity:
185         case SouthGravity:
186                 dx = -(c->w / 2) + c->border;
187                 break;
188         case NorthEastGravity:
189         case EastGravity:
190         case SouthEastGravity:
191                 dx = -(c->w + c->border);
192                 break;
193         }
194
195         if(invert) {
196                 dx = -dx;
197                 dy = -dy;
198         }
199         c->x += dx;
200         c->y += dy;
201 }
202
203 void
204 killclient(Arg *arg)
205 {
206         if(!sel)
207                 return;
208         if(sel->proto & PROTODELWIN)
209                 sendevent(sel->win, wmatom[WMProtocols], wmatom[WMDelete]);
210         else
211                 XKillClient(dpy, sel->win);
212 }
213
214 void
215 manage(Window w, XWindowAttributes *wa)
216 {
217         Client *c, *tc;
218         Window trans;
219         XSetWindowAttributes twa;
220
221         c = emallocz(sizeof(Client));
222         c->tags = emallocz(ntags * sizeof(Bool));
223         c->win = w;
224         c->x = c->tx = wa->x;
225         c->y = c->ty = wa->y;
226         c->w = c->tw = wa->width;
227         c->h = wa->height;
228         c->th = bh;
229
230         c->border = 0;
231         setsize(c);
232
233         if(c->x + c->w + 2 > sw)
234                 c->x = sw - c->w - 2;
235         if(c->x < 0)
236                 c->x = 0;
237         if(c->y + c->h + 2 > sh)
238                 c->y = sh - c->h - 2;
239         if(c->h != sh && c->y < bh)
240                 c->y = bh;
241
242         c->proto = getproto(c->win);
243         XSelectInput(dpy, c->win,
244                 StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
245         XGetTransientForHint(dpy, c->win, &trans);
246         twa.override_redirect = 1;
247         twa.background_pixmap = ParentRelative;
248         twa.event_mask = ExposureMask | EnterWindowMask;
249
250         c->title = XCreateWindow(dpy, root, c->tx, c->ty, c->tw, c->th,
251                         0, DefaultDepth(dpy, screen), CopyFromParent,
252                         DefaultVisual(dpy, screen),
253                         CWOverrideRedirect | CWBackPixmap | CWEventMask, &twa);
254
255         if(clients)
256                 clients->prev = c;
257         c->next = clients;
258         clients = c;
259
260         grabbutton(c, Button1, MODKEY);
261         grabbutton(c, Button2, MODKEY);
262         grabbutton(c, Button3, MODKEY);
263
264         settags(c);
265         if(!c->isfloat)
266                 c->isfloat = trans
267                         || (c->maxw && c->minw &&
268                                 c->maxw == c->minw && c->maxh == c->minh);
269         settitle(c);
270         if(isvisible(c))
271                 sel = c;
272         arrange(NULL);
273         XMapWindow(dpy, c->win);
274         XMapWindow(dpy, c->title);
275         if(isvisible(c))
276                 focus(c);
277 }
278
279 void
280 resize(Client *c, Bool sizehints, Corner sticky)
281 {
282         int bottom = c->y + c->h;
283         int right = c->x + c->w;
284         XWindowChanges wc;
285
286         if(sizehints) {
287                 if(c->incw)
288                         c->w -= (c->w - c->basew) % c->incw;
289                 if(c->inch)
290                         c->h -= (c->h - c->baseh) % c->inch;
291                 if(c->minw && c->w < c->minw)
292                         c->w = c->minw;
293                 if(c->minh && c->h < c->minh)
294                         c->h = c->minh;
295                 if(c->maxw && c->w > c->maxw)
296                         c->w = c->maxw;
297                 if(c->maxh && c->h > c->maxh)
298                         c->h = c->maxh;
299         }
300         if(sticky == TopRight || sticky == BotRight)
301                 c->x = right - c->w;
302         if(sticky == BotLeft || sticky == BotRight)
303                 c->y = bottom - c->h;
304
305         resizetitle(c);
306         wc.x = c->x;
307         wc.y = c->y;
308         wc.width = c->w;
309         wc.height = c->h;
310         if(c->w == sw && c->h == sh)
311                 wc.border_width = 0;
312         else
313                 wc.border_width = 1;
314         XConfigureWindow(dpy, c->win, CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
315         XSync(dpy, False);
316 }
317
318 void
319 setsize(Client *c)
320 {
321         long msize;
322         XSizeHints size;
323
324         if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
325                 size.flags = PSize;
326         c->flags = size.flags;
327         if(c->flags & PBaseSize) {
328                 c->basew = size.base_width;
329                 c->baseh = size.base_height;
330         }
331         else
332                 c->basew = c->baseh = 0;
333         if(c->flags & PResizeInc) {
334                 c->incw = size.width_inc;
335                 c->inch = size.height_inc;
336         }
337         else
338                 c->incw = c->inch = 0;
339         if(c->flags & PMaxSize) {
340                 c->maxw = size.max_width;
341                 c->maxh = size.max_height;
342         }
343         else
344                 c->maxw = c->maxh = 0;
345         if(c->flags & PMinSize) {
346                 c->minw = size.min_width;
347                 c->minh = size.min_height;
348         }
349         else
350                 c->minw = c->minh = 0;
351         if(c->flags & PWinGravity)
352                 c->grav = size.win_gravity;
353         else
354                 c->grav = NorthWestGravity;
355 }
356
357 void
358 settitle(Client *c)
359 {
360         char **list = NULL;
361         int n;
362         XTextProperty name;
363
364         name.nitems = 0;
365         c->name[0] = 0;
366         XGetTextProperty(dpy, c->win, &name, netatom[NetWMName]);
367         if(!name.nitems)
368                 XGetWMName(dpy, c->win, &name);
369         if(!name.nitems)
370                 return;
371         if(name.encoding == XA_STRING)
372                 strncpy(c->name, (char *)name.value, sizeof(c->name));
373         else {
374                 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
375                                 && n > 0 && *list)
376                 {
377                         strncpy(c->name, *list, sizeof(c->name));
378                         XFreeStringList(list);
379                 }
380         }
381         XFree(name.value);
382         resizetitle(c);
383 }
384
385 void
386 togglemax(Arg *arg)
387 {
388         int ox, oy, ow, oh;
389         XEvent ev;
390
391         if(!sel)
392                 return;
393
394         if((sel->ismax = !sel->ismax)) {
395                 ox = sel->x;
396                 oy = sel->y;
397                 ow = sel->w;
398                 oh = sel->h;
399                 sel->x = sx;
400                 sel->y = sy + bh;
401                 sel->w = sw - 2;
402                 sel->h = sh - 2 - bh;
403
404                 restack();
405                 resize(sel, arrange == dofloat, TopLeft);
406
407                 sel->x = ox;
408                 sel->y = oy;
409                 sel->w = ow;
410                 sel->h = oh;
411         }
412         else
413                 resize(sel, False, TopLeft);
414         while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
415 }
416
417 void
418 unmanage(Client *c)
419 {
420         XGrabServer(dpy);
421         XSetErrorHandler(xerrordummy);
422
423         XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
424         XDestroyWindow(dpy, c->title);
425
426         if(c->prev)
427                 c->prev->next = c->next;
428         if(c->next)
429                 c->next->prev = c->prev;
430         if(c == clients)
431                 clients = c->next;
432         if(sel == c)
433                 sel = getnext(clients);
434         free(c->tags);
435         free(c);
436
437         XSync(dpy, False);
438         XSetErrorHandler(xerror);
439         XUngrabServer(dpy);
440         if(sel)
441                 focus(sel);
442         arrange(NULL);
443 }
444
445 void
446 zoom(Arg *arg)
447 {
448         Client *c;
449
450         if(!sel || (arrange != dotile) || sel->isfloat || sel->ismax)
451                 return;
452
453         if(sel == getnext(clients))  {
454                 if((c = getnext(sel->next)))
455                         sel = c;
456                 else
457                         return;
458         }
459
460         /* pop */
461         sel->prev->next = sel->next;
462         if(sel->next)
463                 sel->next->prev = sel->prev;
464         sel->prev = NULL;
465         clients->prev = sel;
466         sel->next = clients;
467         clients = sel;
468         focus(sel);
469         arrange(NULL);
470 }