JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
new resize stuff (using XConfigureWindow instead of XSendEvent)
[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)))
77                 c = getnext(clients);
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 & PROTODELWIN)
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, BUTTONMASK,
248                         GrabModeAsync, GrabModeSync, None, None);
249         XGrabButton(dpy, Button2, MODKEY, c->win, False, BUTTONMASK,
250                         GrabModeAsync, GrabModeSync, None, None);
251         XGrabButton(dpy, Button3, MODKEY, c->win, False, BUTTONMASK,
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         XMapRaised(dpy, c->win);
264         XMapRaised(dpy, c->title);
265         if(c->tags[tsel])
266                 focus(c);
267 }
268
269 void
270 resize(Client *c, Bool sizehints, Corner sticky)
271 {
272         int bottom = c->y + c->h;
273         int right = c->x + c->w;
274         XWindowChanges wc;
275
276         if(sizehints) {
277                 if(c->incw)
278                         c->w -= (c->w - c->basew) % c->incw;
279                 if(c->inch)
280                         c->h -= (c->h - c->baseh) % c->inch;
281                 if(c->minw && c->w < c->minw)
282                         c->w = c->minw;
283                 if(c->minh && c->h < c->minh)
284                         c->h = c->minh;
285                 if(c->maxw && c->w > c->maxw)
286                         c->w = c->maxw;
287                 if(c->maxh && c->h > c->maxh)
288                         c->h = c->maxh;
289         }
290         if(sticky == TopRight || sticky == BotRight)
291                 c->x = right - c->w;
292         if(sticky == BotLeft || sticky == BotRight)
293                 c->y = bottom - c->h;
294
295         resizetitle(c);
296
297         if(c->tags[tsel])
298                 wc.x = c->x;
299         else
300                 wc.x = c->x + 2 * sw;
301         wc.y = c->y;
302         wc.width = c->w;
303         wc.height = c->h;
304         wc.border_width = 1;
305         XConfigureWindow(dpy, c->win, CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
306         XSync(dpy, False);
307 }
308
309 void
310 setsize(Client *c)
311 {
312         long msize;
313         XSizeHints size;
314
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         char **list = NULL;
352         int n;
353         XTextProperty name;
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 togglemax(Arg *arg)
378 {
379         int ox, oy, ow, oh;
380         XEvent ev;
381
382         if(!sel)
383                 return;
384
385         if((sel->ismax = !sel->ismax)) {
386                 ox = sel->x;
387                 oy = sel->y;
388                 ow = sel->w;
389                 oh = sel->h;
390                 sel->x = sx;
391                 sel->y = sy + bh;
392                 sel->w = sw - 2 * sel->border;
393                 sel->h = sh - 2 * sel->border - bh;
394
395                 higher(sel);
396                 resize(sel, False, TopLeft);
397
398                 sel->x = ox;
399                 sel->y = oy;
400                 sel->w = ow;
401                 sel->h = oh;
402         }
403         else
404                 resize(sel, False, TopLeft);
405         while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
406 }
407
408 void
409 unmanage(Client *c)
410 {
411         XGrabServer(dpy);
412         XSetErrorHandler(xerrordummy);
413
414         XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
415         XDestroyWindow(dpy, c->title);
416
417         if(c->prev)
418                 c->prev->next = c->next;
419         if(c->next)
420                 c->next->prev = c->prev;
421         if(c == clients)
422                 clients = c->next;
423         if(sel == c) {
424                 sel = getnext(c->next);
425                 if(!sel)
426                         sel = getprev(c->prev);
427                 if(!sel)
428                         sel = clients;
429         }
430         free(c);
431
432         XSync(dpy, False);
433         XSetErrorHandler(xerror);
434         XUngrabServer(dpy);
435         arrange(NULL);
436         if(sel)
437                 focus(sel);
438 }
439
440 void
441 zoom(Arg *arg)
442 {
443         Client *c;
444
445         if(!sel)
446                 return;
447
448         if(sel == getnext(clients) && sel->next)  {
449                 if((c = getnext(sel->next)))
450                         sel = c;
451         }
452
453         /* pop */
454         if(sel->prev)
455                 sel->prev->next = sel->next;
456         if(sel->next)
457                 sel->next->prev = sel->prev;
458         sel->prev = NULL;
459         if(clients)
460                 clients->prev = sel;
461         sel->next = clients;
462         clients = sel;
463         arrange(NULL);
464         focus(sel);
465 }