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