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