aewl

view client.c @ 113:b2445fd41f5e

applied Jukka's patch with s/ModKeyMask/MODKEY/g
author arg@10ksloc.org
date Wed, 19 Jul 2006 16:38:39 +0200
parents c292574503dd
children dfa5cd0969a6
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
251 || ((c->maxw == c->minw) && (c->maxh == c->minh));
253 setgeom(c);
254 settitle(c);
256 arrange(NULL);
258 /* mapping the window now prevents flicker */
259 if(c->tags[tsel]) {
260 XMapRaised(dpy, c->win);
261 XMapRaised(dpy, c->title);
262 focus(c);
263 }
264 else {
265 XMapRaised(dpy, c->win);
266 XMapRaised(dpy, c->title);
267 }
268 }
270 void
271 maximize(Arg *arg)
272 {
273 if(!sel)
274 return;
275 *sel->x = sx;
276 *sel->y = sy + bh;
277 *sel->w = sw - 2 * sel->border;
278 *sel->h = sh - 2 * sel->border - bh;
279 higher(sel);
280 resize(sel, False, TopLeft);
281 }
283 void
284 pop(Client *c)
285 {
286 Client **l;
287 for(l = &clients; *l && *l != c; l = &(*l)->next);
288 *l = c->next;
290 c->next = clients; /* pop */
291 clients = c;
292 arrange(NULL);
293 }
295 void
296 resize(Client *c, Bool inc, Corner sticky)
297 {
298 XConfigureEvent e;
299 int right = *c->x + *c->w;
300 int bottom = *c->y + *c->h;
302 if(inc) {
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 }
308 if(*c->x > sw) /* might happen on restart */
309 *c->x = sw - *c->w;
310 if(*c->y > sh)
311 *c->y = sh - *c->h;
312 if(c->minw && *c->w < c->minw)
313 *c->w = c->minw;
314 if(c->minh && *c->h < c->minh)
315 *c->h = c->minh;
316 if(c->maxw && *c->w > c->maxw)
317 *c->w = c->maxw;
318 if(c->maxh && *c->h > c->maxh)
319 *c->h = c->maxh;
320 if(sticky == TopRight || sticky == BotRight)
321 *c->x = right - *c->w;
322 if(sticky == BotLeft || sticky == BotRight)
323 *c->y = bottom - *c->h;
325 resizetitle(c);
326 XSetWindowBorderWidth(dpy, c->win, 1);
327 XMoveResizeWindow(dpy, c->win, *c->x, *c->y, *c->w, *c->h);
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 }
343 void
344 setgeom(Client *c)
345 {
346 if((arrange == dotile) && !c->isfloat) {
347 c->x = &c->tx;
348 c->y = &c->ty;
349 c->w = &c->tw;
350 c->h = &c->th;
351 }
352 else {
353 c->x = &c->fx;
354 c->y = &c->fy;
355 c->w = &c->fw;
356 c->h = &c->fh;
357 }
358 }
360 void
361 setsize(Client *c)
362 {
363 XSizeHints size;
364 long msize;
365 if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
366 size.flags = PSize;
367 c->flags = size.flags;
368 if(c->flags & PBaseSize) {
369 c->basew = size.base_width;
370 c->baseh = size.base_height;
371 }
372 else
373 c->basew = c->baseh = 0;
374 if(c->flags & PResizeInc) {
375 c->incw = size.width_inc;
376 c->inch = size.height_inc;
377 }
378 else
379 c->incw = c->inch = 0;
380 if(c->flags & PMaxSize) {
381 c->maxw = size.max_width;
382 c->maxh = size.max_height;
383 }
384 else
385 c->maxw = c->maxh = 0;
386 if(c->flags & PMinSize) {
387 c->minw = size.min_width;
388 c->minh = size.min_height;
389 }
390 else
391 c->minw = c->minh = 0;
392 if(c->flags & PWinGravity)
393 c->grav = size.win_gravity;
394 else
395 c->grav = NorthWestGravity;
396 }
398 void
399 settitle(Client *c)
400 {
401 XTextProperty name;
402 int n;
403 char **list = NULL;
405 name.nitems = 0;
406 c->name[0] = 0;
407 XGetTextProperty(dpy, c->win, &name, netatom[NetWMName]);
408 if(!name.nitems)
409 XGetWMName(dpy, c->win, &name);
410 if(!name.nitems)
411 return;
412 if(name.encoding == XA_STRING)
413 strncpy(c->name, (char *)name.value, sizeof(c->name));
414 else {
415 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
416 && n > 0 && *list)
417 {
418 strncpy(c->name, *list, sizeof(c->name));
419 XFreeStringList(list);
420 }
421 }
422 XFree(name.value);
423 resizetitle(c);
424 }
426 void
427 unmanage(Client *c)
428 {
429 Client **l;
431 XGrabServer(dpy);
432 XSetErrorHandler(xerrordummy);
434 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
435 XDestroyWindow(dpy, c->title);
437 for(l = &clients; *l && *l != c; l = &(*l)->next);
438 *l = c->next;
439 for(l = &clients; *l; l = &(*l)->next)
440 if((*l)->revert == c)
441 (*l)->revert = NULL;
442 if(sel == c)
443 sel = sel->revert ? sel->revert : clients;
445 free(c);
447 XSync(dpy, False);
448 XSetErrorHandler(xerror);
449 XUngrabServer(dpy);
450 arrange(NULL);
451 if(sel)
452 focus(sel);
453 }
455 void
456 zoom(Arg *arg)
457 {
458 Client *c;
460 if(!sel)
461 return;
463 if(sel == getnext(clients, tsel) && sel->next) {
464 if((c = getnext(sel->next, tsel)))
465 sel = c;
466 }
468 pop(sel);
469 focus(sel);
470 }