aewl

view client.c @ 104:3a708f113f55

implemented fallback for too many clients in stacked mode
author arg@10ksloc.org
date Wed, 19 Jul 2006 13:36:04 +0200
parents 130d49140341
children 3e74cc981e9b
line source
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"
7 #include <stdlib.h>
8 #include <string.h>
9 #include <X11/Xatom.h>
10 #include <X11/Xutil.h>
12 /* static functions */
14 static void
15 resizetitle(Client *c)
16 {
17 int i;
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 XMoveResizeWindow(dpy, c->title, c->bx, c->by, c->bw, c->bh);
29 }
31 static int
32 xerrordummy(Display *dsply, XErrorEvent *ee)
33 {
34 return 0;
35 }
37 /* extern functions */
39 void
40 ban(Client *c)
41 {
42 XMoveWindow(dpy, c->win, *c->x + 2 * sw, *c->y);
43 XMoveWindow(dpy, c->title, c->bx + 2 * sw, c->by);
44 }
46 void
47 focus(Client *c)
48 {
49 Client *old = sel;
50 XEvent ev;
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 }
61 void
62 focusnext(Arg *arg)
63 {
64 Client *c;
66 if(!sel)
67 return;
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 }
78 void
79 focusprev(Arg *arg)
80 {
81 Client *c;
83 if(!sel)
84 return;
86 if((c = sel->revert && sel->revert->tags[tsel] ? sel->revert : NULL)) {
87 higher(c);
88 focus(c);
89 }
90 }
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 }
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 }
112 void
113 gravitate(Client *c, Bool invert)
114 {
115 int dx = 0, dy = 0;
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 }
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 }
159 if(invert) {
160 dx = -dx;
161 dy = -dy;
162 }
163 *c->x += dx;
164 *c->y += dy;
165 }
167 void
168 higher(Client *c)
169 {
170 XRaiseWindow(dpy, c->win);
171 XRaiseWindow(dpy, c->title);
172 }
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 }
185 void
186 lower(Client *c)
187 {
188 XLowerWindow(dpy, c->title);
189 XLowerWindow(dpy, c->win);
190 }
192 void
193 manage(Window w, XWindowAttributes *wa)
194 {
195 int diff;
196 Client *c;
197 XSetWindowAttributes twa;
198 Window trans;
200 c = emallocz(sizeof(Client));
201 c->win = w;
202 c->bx = c->fx = c->tx = wa->x;
203 c->by = c->fy = c->ty = wa->y;
204 c->bw = c->fw = c->tw = wa->width;
205 c->fh = c->th = wa->height;
206 c->bh = bh;
208 diff = sw - c->fw;
209 c->fx = random() % (diff ? diff : 1);
210 diff = sh - c->fh - bh;
211 c->fy = random() % (diff ? diff : 1);
213 if(c->fy < bh)
214 c->by = c->fy = c->ty = bh;
216 c->border = 1;
217 c->proto = getproto(c->win);
218 setsize(c);
219 XSelectInput(dpy, c->win,
220 StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
221 XGetTransientForHint(dpy, c->win, &trans);
222 twa.override_redirect = 1;
223 twa.background_pixmap = ParentRelative;
224 twa.event_mask = ExposureMask;
226 c->title = XCreateWindow(dpy, root, c->bx, c->by, c->bw, c->bh,
227 0, DefaultDepth(dpy, screen), CopyFromParent,
228 DefaultVisual(dpy, screen),
229 CWOverrideRedirect | CWBackPixmap | CWEventMask, &twa);
231 settags(c);
233 c->next = clients;
234 clients = c;
236 XGrabButton(dpy, Button1, ControlMask, c->win, False, ButtonPressMask,
237 GrabModeAsync, GrabModeSync, None, None);
238 XGrabButton(dpy, Button1, Mod1Mask, c->win, False, ButtonPressMask,
239 GrabModeAsync, GrabModeSync, None, None);
240 XGrabButton(dpy, Button2, Mod1Mask, c->win, False, ButtonPressMask,
241 GrabModeAsync, GrabModeSync, None, None);
242 XGrabButton(dpy, Button3, Mod1Mask, c->win, False, ButtonPressMask,
243 GrabModeAsync, GrabModeSync, None, None);
245 if(!c->isfloat)
246 c->isfloat = trans
247 || ((c->maxw == c->minw) && (c->maxh == c->minh));
249 setgeom(c);
250 settitle(c);
252 arrange(NULL);
254 /* mapping the window now prevents flicker */
255 if(c->tags[tsel]) {
256 XMapRaised(dpy, c->win);
257 XMapRaised(dpy, c->title);
258 focus(c);
259 }
260 else {
261 ban(c);
262 XMapRaised(dpy, c->win);
263 XMapRaised(dpy, c->title);
264 XSync(dpy, False);
265 }
266 }
268 void
269 maximize(Arg *arg)
270 {
271 if(!sel)
272 return;
273 *sel->x = sx;
274 *sel->y = sy + bh;
275 *sel->w = sw - 2 * sel->border;
276 *sel->h = sh - 2 * sel->border - bh;
277 higher(sel);
278 resize(sel, False, TopLeft);
279 }
281 void
282 pop(Client *c)
283 {
284 Client **l;
285 for(l = &clients; *l && *l != c; l = &(*l)->next);
286 *l = c->next;
288 c->next = clients; /* pop */
289 clients = c;
290 arrange(NULL);
291 }
293 void
294 resize(Client *c, Bool inc, Corner sticky)
295 {
296 XConfigureEvent e;
297 int right = *c->x + *c->w;
298 int bottom = *c->y + *c->h;
300 if(inc) {
301 if(c->incw)
302 *c->w -= (*c->w - c->basew) % c->incw;
303 if(c->inch)
304 *c->h -= (*c->h - c->baseh) % c->inch;
305 }
306 if(*c->x > sw) /* might happen on restart */
307 *c->x = sw - *c->w;
308 if(*c->y > sh)
309 *c->y = sh - *c->h;
310 if(c->minw && *c->w < c->minw)
311 *c->w = c->minw;
312 if(c->minh && *c->h < c->minh)
313 *c->h = c->minh;
314 if(c->maxw && *c->w > c->maxw)
315 *c->w = c->maxw;
316 if(c->maxh && *c->h > c->maxh)
317 *c->h = c->maxh;
318 if(sticky == TopRight || sticky == BottomRight)
319 *c->x = right - *c->w;
320 if(sticky == BottomLeft || sticky == BottomRight)
321 *c->y = bottom - *c->h;
322 resizetitle(c);
323 XSetWindowBorderWidth(dpy, c->win, 1);
324 XMoveResizeWindow(dpy, c->win, *c->x, *c->y, *c->w, *c->h);
325 e.type = ConfigureNotify;
326 e.event = c->win;
327 e.window = c->win;
328 e.x = *c->x;
329 e.y = *c->y;
330 e.width = *c->w;
331 e.height = *c->h;
332 e.border_width = c->border;
333 e.above = None;
334 e.override_redirect = False;
335 XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&e);
336 XSync(dpy, False);
337 }
339 void
340 setgeom(Client *c)
341 {
342 if((arrange == dotile) && !c->isfloat) {
343 c->x = &c->tx;
344 c->y = &c->ty;
345 c->w = &c->tw;
346 c->h = &c->th;
347 }
348 else {
349 c->x = &c->fx;
350 c->y = &c->fy;
351 c->w = &c->fw;
352 c->h = &c->fh;
353 }
354 }
356 void
357 setsize(Client *c)
358 {
359 XSizeHints size;
360 long msize;
361 if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
362 size.flags = PSize;
363 c->flags = size.flags;
364 if(c->flags & PBaseSize) {
365 c->basew = size.base_width;
366 c->baseh = size.base_height;
367 }
368 else
369 c->basew = c->baseh = 0;
370 if(c->flags & PResizeInc) {
371 c->incw = size.width_inc;
372 c->inch = size.height_inc;
373 }
374 else
375 c->incw = c->inch = 0;
376 if(c->flags & PMaxSize) {
377 c->maxw = size.max_width;
378 c->maxh = size.max_height;
379 }
380 else
381 c->maxw = c->maxh = 0;
382 if(c->flags & PMinSize) {
383 c->minw = size.min_width;
384 c->minh = size.min_height;
385 }
386 else
387 c->minw = c->minh = 0;
388 if(c->flags & PWinGravity)
389 c->grav = size.win_gravity;
390 else
391 c->grav = NorthWestGravity;
392 }
394 void
395 settitle(Client *c)
396 {
397 XTextProperty name;
398 int n;
399 char **list = NULL;
401 name.nitems = 0;
402 c->name[0] = 0;
403 XGetTextProperty(dpy, c->win, &name, netatom[NetWMName]);
404 if(!name.nitems)
405 XGetWMName(dpy, c->win, &name);
406 if(!name.nitems)
407 return;
408 if(name.encoding == XA_STRING)
409 strncpy(c->name, (char *)name.value, sizeof(c->name));
410 else {
411 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
412 && n > 0 && *list)
413 {
414 strncpy(c->name, *list, sizeof(c->name));
415 XFreeStringList(list);
416 }
417 }
418 XFree(name.value);
419 resizetitle(c);
420 }
422 void
423 unmanage(Client *c)
424 {
425 Client **l;
427 XGrabServer(dpy);
428 XSetErrorHandler(xerrordummy);
430 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
431 XDestroyWindow(dpy, c->title);
433 for(l = &clients; *l && *l != c; l = &(*l)->next);
434 *l = c->next;
435 for(l = &clients; *l; l = &(*l)->next)
436 if((*l)->revert == c)
437 (*l)->revert = NULL;
438 if(sel == c)
439 sel = sel->revert ? sel->revert : clients;
441 free(c);
443 XSync(dpy, False);
444 XSetErrorHandler(xerror);
445 XUngrabServer(dpy);
446 arrange(NULL);
447 if(sel)
448 focus(sel);
449 }
451 void
452 zoom(Arg *arg)
453 {
454 Client *c;
456 if(!sel)
457 return;
459 if(sel == getnext(clients, tsel) && sel->next) {
460 if((c = getnext(sel->next, tsel)))
461 sel = c;
462 }
464 pop(sel);
465 focus(sel);
466 }