aewl

view client.c @ 114:dfa5cd0969a6

implemented regexp matching for rules
author arg@10ksloc.org
date Wed, 19 Jul 2006 17:42:08 +0200
parents b2445fd41f5e
children 329fd7dae530
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 if(c->tags[tsel])
29 XMoveResizeWindow(dpy, c->title, c->bx, c->by, c->bw, c->bh);
30 else
31 XMoveResizeWindow(dpy, c->title, c->bx + 2 * sw, c->by, c->bw, c->bh);
33 }
35 static int
36 xerrordummy(Display *dsply, XErrorEvent *ee)
37 {
38 return 0;
39 }
41 /* extern functions */
43 void
44 ban(Client *c)
45 {
46 XMoveWindow(dpy, c->win, *c->x + 2 * sw, *c->y);
47 XMoveWindow(dpy, c->title, c->bx + 2 * sw, c->by);
48 }
50 void
51 focus(Client *c)
52 {
53 Client *old = sel;
54 XEvent ev;
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 }
65 void
66 focusnext(Arg *arg)
67 {
68 Client *c;
70 if(!sel)
71 return;
73 if(!(c = getnext(sel->next, tsel)))
74 c = getnext(clients, tsel);
75 if(c) {
76 higher(c);
77 c->revert = sel;
78 focus(c);
79 }
80 }
82 void
83 focusprev(Arg *arg)
84 {
85 Client *c;
87 if(!sel)
88 return;
90 if((c = sel->revert && sel->revert->tags[tsel] ? sel->revert : NULL)) {
91 higher(c);
92 focus(c);
93 }
94 }
96 Client *
97 getclient(Window w)
98 {
99 Client *c;
100 for(c = clients; c; c = c->next)
101 if(c->win == w)
102 return c;
103 return NULL;
104 }
106 Client *
107 getctitle(Window w)
108 {
109 Client *c;
110 for(c = clients; c; c = c->next)
111 if(c->title == w)
112 return c;
113 return NULL;
114 }
116 void
117 gravitate(Client *c, Bool invert)
118 {
119 int dx = 0, dy = 0;
121 switch(c->grav) {
122 case StaticGravity:
123 case NorthWestGravity:
124 case NorthGravity:
125 case NorthEastGravity:
126 dy = c->border;
127 break;
128 case EastGravity:
129 case CenterGravity:
130 case WestGravity:
131 dy = -(*c->h / 2) + c->border;
132 break;
133 case SouthEastGravity:
134 case SouthGravity:
135 case SouthWestGravity:
136 dy = -(*c->h);
137 break;
138 default:
139 break;
140 }
142 switch (c->grav) {
143 case StaticGravity:
144 case NorthWestGravity:
145 case WestGravity:
146 case SouthWestGravity:
147 dx = c->border;
148 break;
149 case NorthGravity:
150 case CenterGravity:
151 case SouthGravity:
152 dx = -(*c->w / 2) + c->border;
153 break;
154 case NorthEastGravity:
155 case EastGravity:
156 case SouthEastGravity:
157 dx = -(*c->w + c->border);
158 break;
159 default:
160 break;
161 }
163 if(invert) {
164 dx = -dx;
165 dy = -dy;
166 }
167 *c->x += dx;
168 *c->y += dy;
169 }
171 void
172 higher(Client *c)
173 {
174 XRaiseWindow(dpy, c->win);
175 XRaiseWindow(dpy, c->title);
176 }
178 void
179 killclient(Arg *arg)
180 {
181 if(!sel)
182 return;
183 if(sel->proto & WM_PROTOCOL_DELWIN)
184 sendevent(sel->win, wmatom[WMProtocols], wmatom[WMDelete]);
185 else
186 XKillClient(dpy, sel->win);
187 }
189 void
190 lower(Client *c)
191 {
192 XLowerWindow(dpy, c->title);
193 XLowerWindow(dpy, c->win);
194 }
196 void
197 manage(Window w, XWindowAttributes *wa)
198 {
199 int diff;
200 Client *c;
201 XSetWindowAttributes twa;
202 Window trans;
204 c = emallocz(sizeof(Client));
205 c->win = w;
206 c->bx = c->fx = c->tx = wa->x;
207 c->by = c->fy = c->ty = wa->y;
208 c->bw = c->fw = c->tw = wa->width;
209 c->fh = c->th = wa->height;
210 c->bh = bh;
212 diff = sw - c->fw;
213 c->fx = random() % (diff ? diff : 1);
214 diff = sh - c->fh - bh;
215 c->fy = random() % (diff ? diff : 1);
217 if(c->fy < bh)
218 c->by = c->fy = c->ty = bh;
220 c->border = 1;
221 c->proto = getproto(c->win);
222 setsize(c);
223 XSelectInput(dpy, c->win,
224 StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
225 XGetTransientForHint(dpy, c->win, &trans);
226 twa.override_redirect = 1;
227 twa.background_pixmap = ParentRelative;
228 twa.event_mask = ExposureMask;
230 c->title = XCreateWindow(dpy, root, c->bx, c->by, c->bw, c->bh,
231 0, DefaultDepth(dpy, screen), CopyFromParent,
232 DefaultVisual(dpy, screen),
233 CWOverrideRedirect | CWBackPixmap | CWEventMask, &twa);
235 settags(c);
237 c->next = clients;
238 clients = c;
240 XGrabButton(dpy, Button1, ControlMask, c->win, False, ButtonPressMask,
241 GrabModeAsync, GrabModeSync, None, None);
242 XGrabButton(dpy, Button1, MODKEY, c->win, False, ButtonPressMask,
243 GrabModeAsync, GrabModeSync, None, None);
244 XGrabButton(dpy, Button2, MODKEY, c->win, False, ButtonPressMask,
245 GrabModeAsync, GrabModeSync, None, None);
246 XGrabButton(dpy, Button3, MODKEY, c->win, False, ButtonPressMask,
247 GrabModeAsync, GrabModeSync, None, None);
249 if(!c->isfloat)
250 c->isfloat = trans || (c->maxw && c->minw &&
251 (c->maxw == c->minw) && (c->maxh == c->minh));
254 setgeom(c);
255 settitle(c);
257 arrange(NULL);
259 /* mapping the window now prevents flicker */
260 if(c->tags[tsel]) {
261 XMapRaised(dpy, c->win);
262 XMapRaised(dpy, c->title);
263 focus(c);
264 }
265 else {
266 XMapRaised(dpy, c->win);
267 XMapRaised(dpy, c->title);
268 }
269 }
271 void
272 maximize(Arg *arg)
273 {
274 if(!sel)
275 return;
276 *sel->x = sx;
277 *sel->y = sy + bh;
278 *sel->w = sw - 2 * sel->border;
279 *sel->h = sh - 2 * sel->border - bh;
280 higher(sel);
281 resize(sel, False, TopLeft);
282 }
284 void
285 pop(Client *c)
286 {
287 Client **l;
288 for(l = &clients; *l && *l != c; l = &(*l)->next);
289 *l = c->next;
291 c->next = clients; /* pop */
292 clients = c;
293 arrange(NULL);
294 }
296 void
297 resize(Client *c, Bool inc, Corner sticky)
298 {
299 XConfigureEvent e;
300 int right = *c->x + *c->w;
301 int bottom = *c->y + *c->h;
303 if(inc) {
304 if(c->incw)
305 *c->w -= (*c->w - c->basew) % c->incw;
306 if(c->inch)
307 *c->h -= (*c->h - c->baseh) % c->inch;
308 }
309 if(*c->x > sw) /* might happen on restart */
310 *c->x = sw - *c->w;
311 if(*c->y > sh)
312 *c->y = sh - *c->h;
313 if(c->minw && *c->w < c->minw)
314 *c->w = c->minw;
315 if(c->minh && *c->h < c->minh)
316 *c->h = c->minh;
317 if(c->maxw && *c->w > c->maxw)
318 *c->w = c->maxw;
319 if(c->maxh && *c->h > c->maxh)
320 *c->h = c->maxh;
321 if(sticky == TopRight || sticky == BotRight)
322 *c->x = right - *c->w;
323 if(sticky == BotLeft || sticky == BotRight)
324 *c->y = bottom - *c->h;
326 resizetitle(c);
327 XSetWindowBorderWidth(dpy, c->win, 1);
328 XMoveResizeWindow(dpy, c->win, *c->x, *c->y, *c->w, *c->h);
330 e.type = ConfigureNotify;
331 e.event = c->win;
332 e.window = c->win;
333 e.x = *c->x;
334 e.y = *c->y;
335 e.width = *c->w;
336 e.height = *c->h;
337 e.border_width = c->border;
338 e.above = None;
339 e.override_redirect = False;
340 XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&e);
341 XSync(dpy, False);
342 }
344 void
345 setgeom(Client *c)
346 {
347 if((arrange == dotile) && !c->isfloat) {
348 c->x = &c->tx;
349 c->y = &c->ty;
350 c->w = &c->tw;
351 c->h = &c->th;
352 }
353 else {
354 c->x = &c->fx;
355 c->y = &c->fy;
356 c->w = &c->fw;
357 c->h = &c->fh;
358 }
359 }
361 void
362 setsize(Client *c)
363 {
364 XSizeHints size;
365 long msize;
366 if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
367 size.flags = PSize;
368 c->flags = size.flags;
369 if(c->flags & PBaseSize) {
370 c->basew = size.base_width;
371 c->baseh = size.base_height;
372 }
373 else
374 c->basew = c->baseh = 0;
375 if(c->flags & PResizeInc) {
376 c->incw = size.width_inc;
377 c->inch = size.height_inc;
378 }
379 else
380 c->incw = c->inch = 0;
381 if(c->flags & PMaxSize) {
382 c->maxw = size.max_width;
383 c->maxh = size.max_height;
384 }
385 else
386 c->maxw = c->maxh = 0;
387 if(c->flags & PMinSize) {
388 c->minw = size.min_width;
389 c->minh = size.min_height;
390 }
391 else
392 c->minw = c->minh = 0;
393 if(c->flags & PWinGravity)
394 c->grav = size.win_gravity;
395 else
396 c->grav = NorthWestGravity;
397 }
399 void
400 settitle(Client *c)
401 {
402 XTextProperty name;
403 int n;
404 char **list = NULL;
406 name.nitems = 0;
407 c->name[0] = 0;
408 XGetTextProperty(dpy, c->win, &name, netatom[NetWMName]);
409 if(!name.nitems)
410 XGetWMName(dpy, c->win, &name);
411 if(!name.nitems)
412 return;
413 if(name.encoding == XA_STRING)
414 strncpy(c->name, (char *)name.value, sizeof(c->name));
415 else {
416 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
417 && n > 0 && *list)
418 {
419 strncpy(c->name, *list, sizeof(c->name));
420 XFreeStringList(list);
421 }
422 }
423 XFree(name.value);
424 resizetitle(c);
425 }
427 void
428 unmanage(Client *c)
429 {
430 Client **l;
432 XGrabServer(dpy);
433 XSetErrorHandler(xerrordummy);
435 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
436 XDestroyWindow(dpy, c->title);
438 for(l = &clients; *l && *l != c; l = &(*l)->next);
439 *l = c->next;
440 for(l = &clients; *l; l = &(*l)->next)
441 if((*l)->revert == c)
442 (*l)->revert = NULL;
443 if(sel == c)
444 sel = sel->revert ? sel->revert : clients;
446 free(c);
448 XSync(dpy, False);
449 XSetErrorHandler(xerror);
450 XUngrabServer(dpy);
451 arrange(NULL);
452 if(sel)
453 focus(sel);
454 }
456 void
457 zoom(Arg *arg)
458 {
459 Client *c;
461 if(!sel)
462 return;
464 if(sel == getnext(clients, tsel) && sel->next) {
465 if((c = getnext(sel->next, tsel)))
466 sel = c;
467 }
469 pop(sel);
470 focus(sel);
471 }