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