aewl

view client.c @ 325:58c09c533d3f

removed badmalloc (thx for the pointer to Uriel)
author Anselm R. Garbe <arg@10kloc.org>
date Tue, 22 Aug 2006 16:06:11 +0200
parents 94d2d7658673
children 96d09fd98e89
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"
6 #include <stdlib.h>
7 #include <string.h>
8 #include <X11/Xatom.h>
9 #include <X11/Xutil.h>
11 /* static functions */
13 static void
14 grabbutton(Client *c, unsigned int button, unsigned int modifier)
15 {
16 XGrabButton(dpy, button, modifier, c->win, False, BUTTONMASK,
17 GrabModeAsync, GrabModeSync, None, None);
18 XGrabButton(dpy, button, modifier | LockMask, c->win, False, BUTTONMASK,
19 GrabModeAsync, GrabModeSync, None, None);
20 XGrabButton(dpy, button, modifier | numlockmask, c->win, False, BUTTONMASK,
21 GrabModeAsync, GrabModeSync, None, None);
22 XGrabButton(dpy, button, modifier | numlockmask | LockMask, c->win, False, BUTTONMASK,
23 GrabModeAsync, GrabModeSync, None, None);
24 }
26 static void
27 resizetitle(Client *c)
28 {
29 int i;
31 c->tw = 0;
32 for(i = 0; i < ntags; i++)
33 if(c->tags[i])
34 c->tw += textw(tags[i]);
35 c->tw += textw(c->name);
36 if(c->tw > c->w)
37 c->tw = c->w + 2;
38 c->tx = c->x + c->w - c->tw + 2;
39 c->ty = c->y;
40 if(isvisible(c))
41 XMoveResizeWindow(dpy, c->title, c->tx, c->ty, c->tw, c->th);
42 else
43 XMoveResizeWindow(dpy, c->title, c->tx + 2 * sw, c->ty, c->tw, c->th);
45 }
47 static void
48 ungrabbutton(Client *c, unsigned int button, unsigned int modifier)
49 {
50 XUngrabButton(dpy, button, modifier, c->win);
51 XUngrabButton(dpy, button, modifier | LockMask, c->win);
52 XUngrabButton(dpy, button, modifier | numlockmask, c->win);
53 XUngrabButton(dpy, button, modifier | numlockmask | LockMask, c->win);
54 }
56 static int
57 xerrordummy(Display *dsply, XErrorEvent *ee)
58 {
59 return 0;
60 }
62 /* extern functions */
64 void
65 ban(Client *c)
66 {
67 XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
68 XMoveWindow(dpy, c->title, c->tx + 2 * sw, c->ty);
69 }
71 void
72 focus(Client *c)
73 {
74 Client *old = sel;
76 if(!issel)
77 return;
78 if(!sel)
79 sel = c;
80 else if(sel != c) {
81 if(sel->ismax)
82 togglemax(NULL);
83 sel = c;
84 grabbutton(old, AnyButton, 0);
85 drawtitle(old);
86 }
87 ungrabbutton(c, AnyButton, 0);
88 drawtitle(c);
89 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
90 }
92 void
93 focusnext(Arg *arg)
94 {
95 Client *c;
97 if(!sel)
98 return;
100 if(!(c = getnext(sel->next)))
101 c = getnext(clients);
102 if(c) {
103 focus(c);
104 restack();
105 }
106 }
108 void
109 focusprev(Arg *arg)
110 {
111 Client *c;
113 if(!sel)
114 return;
116 if(!(c = getprev(sel->prev))) {
117 for(c = clients; c && c->next; c = c->next);
118 c = getprev(c);
119 }
120 if(c) {
121 focus(c);
122 restack();
123 }
124 }
126 Client *
127 getclient(Window w)
128 {
129 Client *c;
131 for(c = clients; c; c = c->next)
132 if(c->win == w)
133 return c;
134 return NULL;
135 }
137 Client *
138 getctitle(Window w)
139 {
140 Client *c;
142 for(c = clients; c; c = c->next)
143 if(c->title == w)
144 return c;
145 return NULL;
146 }
148 void
149 gravitate(Client *c, Bool invert)
150 {
151 int dx = 0, dy = 0;
153 switch(c->grav) {
154 default:
155 break;
156 case StaticGravity:
157 case NorthWestGravity:
158 case NorthGravity:
159 case NorthEastGravity:
160 dy = c->border;
161 break;
162 case EastGravity:
163 case CenterGravity:
164 case WestGravity:
165 dy = -(c->h / 2) + c->border;
166 break;
167 case SouthEastGravity:
168 case SouthGravity:
169 case SouthWestGravity:
170 dy = -(c->h);
171 break;
172 }
174 switch (c->grav) {
175 default:
176 break;
177 case StaticGravity:
178 case NorthWestGravity:
179 case WestGravity:
180 case SouthWestGravity:
181 dx = c->border;
182 break;
183 case NorthGravity:
184 case CenterGravity:
185 case SouthGravity:
186 dx = -(c->w / 2) + c->border;
187 break;
188 case NorthEastGravity:
189 case EastGravity:
190 case SouthEastGravity:
191 dx = -(c->w + c->border);
192 break;
193 }
195 if(invert) {
196 dx = -dx;
197 dy = -dy;
198 }
199 c->x += dx;
200 c->y += dy;
201 }
203 void
204 killclient(Arg *arg)
205 {
206 if(!sel)
207 return;
208 if(sel->proto & PROTODELWIN)
209 sendevent(sel->win, wmatom[WMProtocols], wmatom[WMDelete]);
210 else
211 XKillClient(dpy, sel->win);
212 }
214 void
215 manage(Window w, XWindowAttributes *wa)
216 {
217 unsigned int i;
218 Client *c, *tc;
219 Window trans;
220 XSetWindowAttributes twa;
222 c = emallocz(sizeof(Client));
223 c->tags = emallocz(ntags * sizeof(Bool));
224 c->win = w;
225 c->x = c->tx = wa->x;
226 c->y = c->ty = wa->y;
227 c->w = c->tw = wa->width;
228 c->h = wa->height;
229 c->th = bh;
231 c->border = 0;
232 setsize(c);
234 if(c->x + c->w + 2 > sw)
235 c->x = sw - c->w - 2;
236 if(c->x < 0)
237 c->x = 0;
238 if(c->y + c->h + 2 > sh)
239 c->y = sh - c->h - 2;
240 if(c->h != sh && c->y < bh)
241 c->y = bh;
243 c->proto = getproto(c->win);
244 XSelectInput(dpy, c->win,
245 StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
246 XGetTransientForHint(dpy, c->win, &trans);
247 twa.override_redirect = 1;
248 twa.background_pixmap = ParentRelative;
249 twa.event_mask = ExposureMask | EnterWindowMask;
251 c->title = XCreateWindow(dpy, root, c->tx, c->ty, c->tw, c->th,
252 0, DefaultDepth(dpy, screen), CopyFromParent,
253 DefaultVisual(dpy, screen),
254 CWOverrideRedirect | CWBackPixmap | CWEventMask, &twa);
256 if(clients)
257 clients->prev = c;
258 c->next = clients;
259 clients = c;
261 grabbutton(c, Button1, MODKEY);
262 grabbutton(c, Button2, MODKEY);
263 grabbutton(c, Button3, MODKEY);
265 if((tc = getclient(trans))) /* inherit tags */
266 for(i = 0; i < ntags; i++)
267 c->tags[i] = tc->tags[i];
268 else
269 settags(c);
270 if(!c->isfloat)
271 c->isfloat = trans
272 || (c->maxw && c->minw &&
273 c->maxw == c->minw && c->maxh == c->minh);
274 settitle(c);
275 if(isvisible(c))
276 sel = c;
277 arrange(NULL);
278 XMapWindow(dpy, c->win);
279 XMapWindow(dpy, c->title);
280 if(isvisible(c))
281 focus(c);
282 }
284 void
285 resize(Client *c, Bool sizehints, Corner sticky)
286 {
287 int bottom = c->y + c->h;
288 int right = c->x + c->w;
289 XWindowChanges wc;
291 if(sizehints) {
292 if(c->incw)
293 c->w -= (c->w - c->basew) % c->incw;
294 if(c->inch)
295 c->h -= (c->h - c->baseh) % c->inch;
296 if(c->minw && c->w < c->minw)
297 c->w = c->minw;
298 if(c->minh && c->h < c->minh)
299 c->h = c->minh;
300 if(c->maxw && c->w > c->maxw)
301 c->w = c->maxw;
302 if(c->maxh && c->h > c->maxh)
303 c->h = c->maxh;
304 }
305 if(sticky == TopRight || sticky == BotRight)
306 c->x = right - c->w;
307 if(sticky == BotLeft || sticky == BotRight)
308 c->y = bottom - c->h;
310 resizetitle(c);
311 wc.x = c->x;
312 wc.y = c->y;
313 wc.width = c->w;
314 wc.height = c->h;
315 if(c->w == sw && c->h == sh)
316 wc.border_width = 0;
317 else
318 wc.border_width = 1;
319 XConfigureWindow(dpy, c->win, CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
320 XSync(dpy, False);
321 }
323 void
324 setsize(Client *c)
325 {
326 long msize;
327 XSizeHints size;
329 if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
330 size.flags = PSize;
331 c->flags = size.flags;
332 if(c->flags & PBaseSize) {
333 c->basew = size.base_width;
334 c->baseh = size.base_height;
335 }
336 else
337 c->basew = c->baseh = 0;
338 if(c->flags & PResizeInc) {
339 c->incw = size.width_inc;
340 c->inch = size.height_inc;
341 }
342 else
343 c->incw = c->inch = 0;
344 if(c->flags & PMaxSize) {
345 c->maxw = size.max_width;
346 c->maxh = size.max_height;
347 }
348 else
349 c->maxw = c->maxh = 0;
350 if(c->flags & PMinSize) {
351 c->minw = size.min_width;
352 c->minh = size.min_height;
353 }
354 else
355 c->minw = c->minh = 0;
356 if(c->flags & PWinGravity)
357 c->grav = size.win_gravity;
358 else
359 c->grav = NorthWestGravity;
360 }
362 void
363 settitle(Client *c)
364 {
365 char **list = NULL;
366 int n;
367 XTextProperty name;
369 name.nitems = 0;
370 c->name[0] = 0;
371 XGetTextProperty(dpy, c->win, &name, netatom[NetWMName]);
372 if(!name.nitems)
373 XGetWMName(dpy, c->win, &name);
374 if(!name.nitems)
375 return;
376 if(name.encoding == XA_STRING)
377 strncpy(c->name, (char *)name.value, sizeof(c->name));
378 else {
379 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
380 && n > 0 && *list)
381 {
382 strncpy(c->name, *list, sizeof(c->name));
383 XFreeStringList(list);
384 }
385 }
386 XFree(name.value);
387 resizetitle(c);
388 }
390 void
391 togglemax(Arg *arg)
392 {
393 int ox, oy, ow, oh;
394 XEvent ev;
396 if(!sel)
397 return;
399 if((sel->ismax = !sel->ismax)) {
400 ox = sel->x;
401 oy = sel->y;
402 ow = sel->w;
403 oh = sel->h;
404 sel->x = sx;
405 sel->y = sy + bh;
406 sel->w = sw - 2;
407 sel->h = sh - 2 - bh;
409 restack();
410 resize(sel, arrange == dofloat, TopLeft);
412 sel->x = ox;
413 sel->y = oy;
414 sel->w = ow;
415 sel->h = oh;
416 }
417 else
418 resize(sel, False, TopLeft);
419 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
420 }
422 void
423 unmanage(Client *c)
424 {
425 XGrabServer(dpy);
426 XSetErrorHandler(xerrordummy);
428 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
429 XDestroyWindow(dpy, c->title);
431 if(c->prev)
432 c->prev->next = c->next;
433 if(c->next)
434 c->next->prev = c->prev;
435 if(c == clients)
436 clients = c->next;
437 if(sel == c)
438 sel = getnext(clients);
439 free(c->tags);
440 free(c);
442 XSync(dpy, False);
443 XSetErrorHandler(xerror);
444 XUngrabServer(dpy);
445 if(sel)
446 focus(sel);
447 arrange(NULL);
448 }
450 void
451 zoom(Arg *arg)
452 {
453 Client *c;
455 if(!sel || (arrange != dotile) || sel->isfloat || sel->ismax)
456 return;
458 if(sel == getnext(clients)) {
459 if((c = getnext(sel->next)))
460 sel = c;
461 else
462 return;
463 }
465 /* pop */
466 sel->prev->next = sel->next;
467 if(sel->next)
468 sel->next->prev = sel->prev;
469 sel->prev = NULL;
470 clients->prev = sel;
471 sel->next = clients;
472 clients = sel;
473 focus(sel);
474 arrange(NULL);
475 }