dwm-meillo

view client.c @ 100:bb3803fb560c

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