dwm-meillo
view client.c @ 229:f4f5d000ce7a
implemented NET_ACTIVE_WINDOW support
author | Anselm R.Garbe <arg@10ksloc.org> |
---|---|
date | Tue, 08 Aug 2006 17:08:45 +0200 |
parents | f5b952e566da |
children | b92bbc2487c9 |
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 resizetitle(Client *c)
15 {
16 int i;
18 c->tw = 0;
19 for(i = 0; i < ntags; i++)
20 if(c->tags[i])
21 c->tw += textw(tags[i]);
22 c->tw += textw(c->name);
23 if(c->tw > c->w)
24 c->tw = c->w + 2;
25 c->tx = c->x + c->w - c->tw + 2;
26 c->ty = c->y;
27 if(c->tags[tsel])
28 XMoveResizeWindow(dpy, c->title, c->tx, c->ty, c->tw, c->th);
29 else
30 XMoveResizeWindow(dpy, c->title, c->tx + 2 * sw, c->ty, c->tw, c->th);
32 }
34 static int
35 xerrordummy(Display *dsply, XErrorEvent *ee)
36 {
37 return 0;
38 }
40 /* extern functions */
42 void
43 ban(Client *c)
44 {
45 XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
46 XMoveWindow(dpy, c->title, c->tx + 2 * sw, c->ty);
47 }
49 void
50 focus(Client *c)
51 {
52 Client *old = sel;
53 XEvent ev;
55 sel = c;
56 if(old && old != c)
57 drawtitle(old);
58 drawtitle(c);
59 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
60 XSync(dpy, False);
61 XChangeProperty(dpy, root, netatom[NetActiveWindow], XA_WINDOW, 32,
62 PropModeReplace, (unsigned char *)&c->win, 1);
63 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
64 }
66 void
67 focusnext(Arg *arg)
68 {
69 Client *c;
71 if(!sel)
72 return;
74 if(sel->ismax)
75 togglemax(NULL);
77 if(!(c = getnext(sel->next)))
78 c = getnext(clients);
79 if(c) {
80 higher(c);
81 focus(c);
82 }
83 }
85 void
86 focusprev(Arg *arg)
87 {
88 Client *c;
90 if(!sel)
91 return;
93 if(sel->ismax)
94 togglemax(NULL);
96 if(!(c = getprev(sel->prev))) {
97 for(c = clients; c && c->next; c = c->next);
98 c = getprev(c);
99 }
100 if(c) {
101 higher(c);
102 focus(c);
103 }
104 }
106 Client *
107 getclient(Window w)
108 {
109 Client *c;
111 for(c = clients; c; c = c->next)
112 if(c->win == w)
113 return c;
114 return NULL;
115 }
117 Client *
118 getctitle(Window w)
119 {
120 Client *c;
122 for(c = clients; c; c = c->next)
123 if(c->title == w)
124 return c;
125 return NULL;
126 }
128 void
129 gravitate(Client *c, Bool invert)
130 {
131 int dx = 0, dy = 0;
133 switch(c->grav) {
134 default:
135 break;
136 case StaticGravity:
137 case NorthWestGravity:
138 case NorthGravity:
139 case NorthEastGravity:
140 dy = c->border;
141 break;
142 case EastGravity:
143 case CenterGravity:
144 case WestGravity:
145 dy = -(c->h / 2) + c->border;
146 break;
147 case SouthEastGravity:
148 case SouthGravity:
149 case SouthWestGravity:
150 dy = -(c->h);
151 break;
152 }
154 switch (c->grav) {
155 default:
156 break;
157 case StaticGravity:
158 case NorthWestGravity:
159 case WestGravity:
160 case SouthWestGravity:
161 dx = c->border;
162 break;
163 case NorthGravity:
164 case CenterGravity:
165 case SouthGravity:
166 dx = -(c->w / 2) + c->border;
167 break;
168 case NorthEastGravity:
169 case EastGravity:
170 case SouthEastGravity:
171 dx = -(c->w + c->border);
172 break;
173 }
175 if(invert) {
176 dx = -dx;
177 dy = -dy;
178 }
179 c->x += dx;
180 c->y += dy;
181 }
183 void
184 higher(Client *c)
185 {
186 XRaiseWindow(dpy, c->win);
187 XRaiseWindow(dpy, c->title);
188 }
190 void
191 killclient(Arg *arg)
192 {
193 if(!sel)
194 return;
195 if(sel->proto & PROTODELWIN)
196 sendevent(sel->win, wmatom[WMProtocols], wmatom[WMDelete]);
197 else
198 XKillClient(dpy, sel->win);
199 }
201 void
202 lower(Client *c)
203 {
204 XLowerWindow(dpy, c->title);
205 XLowerWindow(dpy, c->win);
206 }
208 void
209 manage(Window w, XWindowAttributes *wa)
210 {
211 Client *c;
212 Window trans;
213 XSetWindowAttributes twa;
215 c = emallocz(sizeof(Client));
216 c->tags = emallocz(ntags * sizeof(Bool));
217 c->win = w;
218 c->x = c->tx = wa->x;
219 c->y = c->ty = wa->y;
220 c->w = c->tw = wa->width;
221 c->h = wa->height;
222 c->th = bh;
224 c->border = 0;
225 setsize(c);
227 if(c->h != sh && c->y < bh)
228 c->y = c->ty = bh;
230 c->proto = getproto(c->win);
231 XSelectInput(dpy, c->win,
232 StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
233 XGetTransientForHint(dpy, c->win, &trans);
234 twa.override_redirect = 1;
235 twa.background_pixmap = ParentRelative;
236 twa.event_mask = ExposureMask | EnterWindowMask;
238 c->title = XCreateWindow(dpy, root, c->tx, c->ty, c->tw, c->th,
239 0, DefaultDepth(dpy, screen), CopyFromParent,
240 DefaultVisual(dpy, screen),
241 CWOverrideRedirect | CWBackPixmap | CWEventMask, &twa);
243 if(clients)
244 clients->prev = c;
245 c->next = clients;
246 clients = c;
248 XGrabButton(dpy, Button1, MODKEY, c->win, False, BUTTONMASK,
249 GrabModeAsync, GrabModeSync, None, None);
250 XGrabButton(dpy, Button1, MODKEY | LockMask, c->win, False, BUTTONMASK,
251 GrabModeAsync, GrabModeSync, None, None);
252 XGrabButton(dpy, Button1, MODKEY | NUMLOCKMASK, c->win, False, BUTTONMASK,
253 GrabModeAsync, GrabModeSync, None, None);
254 XGrabButton(dpy, Button1, MODKEY | NUMLOCKMASK | LockMask, c->win, False, BUTTONMASK,
255 GrabModeAsync, GrabModeSync, None, None);
257 XGrabButton(dpy, Button2, MODKEY, c->win, False, BUTTONMASK,
258 GrabModeAsync, GrabModeSync, None, None);
259 XGrabButton(dpy, Button2, MODKEY | LockMask, c->win, False, BUTTONMASK,
260 GrabModeAsync, GrabModeSync, None, None);
261 XGrabButton(dpy, Button2, MODKEY | NUMLOCKMASK, c->win, False, BUTTONMASK,
262 GrabModeAsync, GrabModeSync, None, None);
263 XGrabButton(dpy, Button2, MODKEY | NUMLOCKMASK | LockMask, c->win, False, BUTTONMASK,
264 GrabModeAsync, GrabModeSync, None, None);
266 XGrabButton(dpy, Button3, MODKEY, c->win, False, BUTTONMASK,
267 GrabModeAsync, GrabModeSync, None, None);
268 XGrabButton(dpy, Button3, MODKEY | LockMask, c->win, False, BUTTONMASK,
269 GrabModeAsync, GrabModeSync, None, None);
270 XGrabButton(dpy, Button3, MODKEY | NUMLOCKMASK, c->win, False, BUTTONMASK,
271 GrabModeAsync, GrabModeSync, None, None);
272 XGrabButton(dpy, Button3, MODKEY | NUMLOCKMASK | LockMask, c->win, False, BUTTONMASK,
273 GrabModeAsync, GrabModeSync, None, None);
275 settags(c);
276 if(!c->isfloat)
277 c->isfloat = trans
278 || (c->maxw && c->minw &&
279 c->maxw == c->minw && c->maxh == c->minh);
280 settitle(c);
281 arrange(NULL);
283 /* mapping the window now prevents flicker */
284 XMapRaised(dpy, c->win);
285 XMapRaised(dpy, c->title);
286 if(c->tags[tsel])
287 focus(c);
288 }
290 void
291 resize(Client *c, Bool sizehints, Corner sticky)
292 {
293 int bottom = c->y + c->h;
294 int right = c->x + c->w;
295 /*XConfigureEvent e;*/
296 XWindowChanges wc;
298 if(sizehints) {
299 if(c->incw)
300 c->w -= (c->w - c->basew) % c->incw;
301 if(c->inch)
302 c->h -= (c->h - c->baseh) % c->inch;
303 if(c->minw && c->w < c->minw)
304 c->w = c->minw;
305 if(c->minh && c->h < c->minh)
306 c->h = c->minh;
307 if(c->maxw && c->w > c->maxw)
308 c->w = c->maxw;
309 if(c->maxh && c->h > c->maxh)
310 c->h = c->maxh;
311 }
312 if(c->x > right) /* might happen on restart */
313 c->x = right - c->w;
314 if(c->y > bottom)
315 c->y = bottom - c->h;
316 if(sticky == TopRight || sticky == BotRight)
317 c->x = right - c->w;
318 if(sticky == BotLeft || sticky == BotRight)
319 c->y = bottom - c->h;
321 resizetitle(c);
322 wc.x = c->x;
323 wc.y = c->y;
324 wc.width = c->w;
325 wc.height = c->h;
326 if(c->w == sw && c->h == sh)
327 wc.border_width = 0;
328 else
329 wc.border_width = 1;
330 XConfigureWindow(dpy, c->win, CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
331 XSync(dpy, False);
332 }
334 void
335 setsize(Client *c)
336 {
337 long msize;
338 XSizeHints size;
340 if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
341 size.flags = PSize;
342 c->flags = size.flags;
343 if(c->flags & PBaseSize) {
344 c->basew = size.base_width;
345 c->baseh = size.base_height;
346 }
347 else
348 c->basew = c->baseh = 0;
349 if(c->flags & PResizeInc) {
350 c->incw = size.width_inc;
351 c->inch = size.height_inc;
352 }
353 else
354 c->incw = c->inch = 0;
355 if(c->flags & PMaxSize) {
356 c->maxw = size.max_width;
357 c->maxh = size.max_height;
358 }
359 else
360 c->maxw = c->maxh = 0;
361 if(c->flags & PMinSize) {
362 c->minw = size.min_width;
363 c->minh = size.min_height;
364 }
365 else
366 c->minw = c->minh = 0;
367 if(c->flags & PWinGravity)
368 c->grav = size.win_gravity;
369 else
370 c->grav = NorthWestGravity;
371 }
373 void
374 settitle(Client *c)
375 {
376 char **list = NULL;
377 int n;
378 XTextProperty name;
380 name.nitems = 0;
381 c->name[0] = 0;
382 XGetTextProperty(dpy, c->win, &name, netatom[NetWMName]);
383 if(!name.nitems)
384 XGetWMName(dpy, c->win, &name);
385 if(!name.nitems)
386 return;
387 if(name.encoding == XA_STRING)
388 strncpy(c->name, (char *)name.value, sizeof(c->name));
389 else {
390 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
391 && n > 0 && *list)
392 {
393 strncpy(c->name, *list, sizeof(c->name));
394 XFreeStringList(list);
395 }
396 }
397 XFree(name.value);
398 resizetitle(c);
399 }
401 void
402 togglemax(Arg *arg)
403 {
404 int ox, oy, ow, oh;
405 XEvent ev;
407 if(!sel)
408 return;
410 if((sel->ismax = !sel->ismax)) {
411 ox = sel->x;
412 oy = sel->y;
413 ow = sel->w;
414 oh = sel->h;
415 sel->x = sx;
416 sel->y = sy + bh;
417 sel->w = sw - 2;
418 sel->h = sh - 2 - bh;
420 higher(sel);
421 resize(sel, False, TopLeft);
423 sel->x = ox;
424 sel->y = oy;
425 sel->w = ow;
426 sel->h = oh;
427 }
428 else
429 resize(sel, False, TopLeft);
430 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
431 }
433 void
434 unmanage(Client *c)
435 {
436 XGrabServer(dpy);
437 XSetErrorHandler(xerrordummy);
439 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
440 XDestroyWindow(dpy, c->title);
442 if(c->prev)
443 c->prev->next = c->next;
444 if(c->next)
445 c->next->prev = c->prev;
446 if(c == clients)
447 clients = c->next;
448 if(sel == c) {
449 sel = getnext(c->next);
450 if(!sel)
451 sel = getprev(c->prev);
452 if(!sel)
453 sel = clients;
454 }
455 free(c->tags);
456 free(c);
458 XSync(dpy, False);
459 XSetErrorHandler(xerror);
460 XUngrabServer(dpy);
461 arrange(NULL);
462 if(sel)
463 focus(sel);
464 else
465 XChangeProperty(dpy, root, netatom[NetActiveWindow], XA_WINDOW, 32,
466 PropModeReplace, (unsigned char *)NULL, 1);
467 }
469 void
470 zoom(Arg *arg)
471 {
472 Client *c;
474 if(!sel || (arrange != dotile) || sel->isfloat)
475 return;
477 if(sel == getnext(clients)) {
478 if((c = getnext(sel->next)))
479 sel = c;
480 else
481 return;
482 }
484 /* pop */
485 if(sel->prev)
486 sel->prev->next = sel->next;
487 if(sel->next)
488 sel->next->prev = sel->prev;
489 sel->prev = NULL;
490 if(clients)
491 clients->prev = sel;
492 sel->next = clients;
493 clients = sel;
494 arrange(NULL);
495 focus(sel);
496 }