dwm-meillo
view client.c @ 306:71f02d14dce1
added general centralization rule for new clients (works around various borken apps)
author | Anselm R.Garbe <arg@10ksloc.org> |
---|---|
date | Wed, 16 Aug 2006 15:57:36 +0200 |
parents | 8e6e0aa5e2ae |
children | ec8a66a2b9cc |
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(isvisible(c))
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;
54 if (!issel)
55 return;
56 if(sel && sel->ismax)
57 togglemax(NULL);
58 sel = c;
59 if(old && old != c)
60 drawtitle(old);
61 drawtitle(c);
62 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
63 }
65 void
66 focusnext(Arg *arg)
67 {
68 Client *c;
70 if(!sel)
71 return;
73 if(!(c = getnext(sel->next)))
74 c = getnext(clients);
75 if(c) {
76 focus(c);
77 restack();
78 }
79 }
81 void
82 focusprev(Arg *arg)
83 {
84 Client *c;
86 if(!sel)
87 return;
89 if(!(c = getprev(sel->prev))) {
90 for(c = clients; c && c->next; c = c->next);
91 c = getprev(c);
92 }
93 if(c) {
94 focus(c);
95 restack();
96 }
97 }
99 Client *
100 getclient(Window w)
101 {
102 Client *c;
104 for(c = clients; c; c = c->next)
105 if(c->win == w)
106 return c;
107 return NULL;
108 }
110 Client *
111 getctitle(Window w)
112 {
113 Client *c;
115 for(c = clients; c; c = c->next)
116 if(c->title == w)
117 return c;
118 return NULL;
119 }
121 void
122 gravitate(Client *c, Bool invert)
123 {
124 int dx = 0, dy = 0;
126 switch(c->grav) {
127 default:
128 break;
129 case StaticGravity:
130 case NorthWestGravity:
131 case NorthGravity:
132 case NorthEastGravity:
133 dy = c->border;
134 break;
135 case EastGravity:
136 case CenterGravity:
137 case WestGravity:
138 dy = -(c->h / 2) + c->border;
139 break;
140 case SouthEastGravity:
141 case SouthGravity:
142 case SouthWestGravity:
143 dy = -(c->h);
144 break;
145 }
147 switch (c->grav) {
148 default:
149 break;
150 case StaticGravity:
151 case NorthWestGravity:
152 case WestGravity:
153 case SouthWestGravity:
154 dx = c->border;
155 break;
156 case NorthGravity:
157 case CenterGravity:
158 case SouthGravity:
159 dx = -(c->w / 2) + c->border;
160 break;
161 case NorthEastGravity:
162 case EastGravity:
163 case SouthEastGravity:
164 dx = -(c->w + c->border);
165 break;
166 }
168 if(invert) {
169 dx = -dx;
170 dy = -dy;
171 }
172 c->x += dx;
173 c->y += dy;
174 }
176 void
177 killclient(Arg *arg)
178 {
179 if(!sel)
180 return;
181 if(sel->proto & PROTODELWIN)
182 sendevent(sel->win, wmatom[WMProtocols], wmatom[WMDelete]);
183 else
184 XKillClient(dpy, sel->win);
185 }
187 void
188 manage(Window w, XWindowAttributes *wa)
189 {
190 Client *c, *tc;
191 Window trans;
192 XSetWindowAttributes twa;
194 c = emallocz(sizeof(Client));
195 c->tags = emallocz(ntags * sizeof(Bool));
196 c->win = w;
197 c->x = c->tx = wa->x;
198 c->y = c->ty = wa->y;
199 c->w = c->tw = wa->width;
200 c->h = wa->height;
201 c->th = bh;
203 c->border = 0;
204 setsize(c);
206 if(c->h != sh && c->y < bh)
207 c->y = c->ty = bh;
209 c->proto = getproto(c->win);
210 XSelectInput(dpy, c->win,
211 StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
212 XGetTransientForHint(dpy, c->win, &trans);
213 twa.override_redirect = 1;
214 twa.background_pixmap = ParentRelative;
215 twa.event_mask = ExposureMask | EnterWindowMask;
217 c->title = XCreateWindow(dpy, root, c->tx, c->ty, c->tw, c->th,
218 0, DefaultDepth(dpy, screen), CopyFromParent,
219 DefaultVisual(dpy, screen),
220 CWOverrideRedirect | CWBackPixmap | CWEventMask, &twa);
222 if(clients)
223 clients->prev = c;
224 c->next = clients;
225 clients = c;
227 XGrabButton(dpy, Button1, MODKEY, c->win, False, BUTTONMASK,
228 GrabModeAsync, GrabModeSync, None, None);
229 XGrabButton(dpy, Button1, MODKEY | LockMask, c->win, False, BUTTONMASK,
230 GrabModeAsync, GrabModeSync, None, None);
231 XGrabButton(dpy, Button1, MODKEY | numlockmask, c->win, False, BUTTONMASK,
232 GrabModeAsync, GrabModeSync, None, None);
233 XGrabButton(dpy, Button1, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
234 GrabModeAsync, GrabModeSync, None, None);
236 XGrabButton(dpy, Button2, MODKEY, c->win, False, BUTTONMASK,
237 GrabModeAsync, GrabModeSync, None, None);
238 XGrabButton(dpy, Button2, MODKEY | LockMask, c->win, False, BUTTONMASK,
239 GrabModeAsync, GrabModeSync, None, None);
240 XGrabButton(dpy, Button2, MODKEY | numlockmask, c->win, False, BUTTONMASK,
241 GrabModeAsync, GrabModeSync, None, None);
242 XGrabButton(dpy, Button2, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
243 GrabModeAsync, GrabModeSync, None, None);
245 XGrabButton(dpy, Button3, MODKEY, c->win, False, BUTTONMASK,
246 GrabModeAsync, GrabModeSync, None, None);
247 XGrabButton(dpy, Button3, MODKEY | LockMask, c->win, False, BUTTONMASK,
248 GrabModeAsync, GrabModeSync, None, None);
249 XGrabButton(dpy, Button3, MODKEY | numlockmask, c->win, False, BUTTONMASK,
250 GrabModeAsync, GrabModeSync, None, None);
251 XGrabButton(dpy, Button3, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
252 GrabModeAsync, GrabModeSync, None, None);
254 settags(c);
255 if(!c->isfloat)
256 c->isfloat = trans
257 || (c->maxw && c->minw &&
258 c->maxw == c->minw && c->maxh == c->minh);
259 settitle(c);
261 if(trans && (tc = getclient(trans))) {
262 c->x = (tc->x + tc->w / 2) - (c->w / 2);
263 c->y = (tc->y + tc->h / 2) - (c->h / 2);
264 }
265 else {
266 c->x = (sw / 2) - (c->w / 2);
267 c->y = ((sh - bh) / 2) - (c->h / 2) + bh;
268 }
270 if(isvisible(c))
271 sel = c;
272 arrange(NULL);
273 XMapWindow(dpy, c->win);
274 XMapWindow(dpy, c->title);
275 if(isvisible(c))
276 focus(c);
277 }
279 void
280 resize(Client *c, Bool sizehints, Corner sticky)
281 {
282 int bottom = c->y + c->h;
283 int right = c->x + c->w;
284 /*XConfigureEvent e;*/
285 XWindowChanges wc;
287 if(sizehints) {
288 if(c->incw)
289 c->w -= (c->w - c->basew) % c->incw;
290 if(c->inch)
291 c->h -= (c->h - c->baseh) % c->inch;
292 if(c->minw && c->w < c->minw)
293 c->w = c->minw;
294 if(c->minh && c->h < c->minh)
295 c->h = c->minh;
296 if(c->maxw && c->w > c->maxw)
297 c->w = c->maxw;
298 if(c->maxh && c->h > c->maxh)
299 c->h = c->maxh;
300 }
301 if(c->x > right) /* might happen on restart */
302 c->x = right - c->w;
303 if(c->y > bottom)
304 c->y = bottom - c->h;
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 }