dwm-meillo
view client.c @ 95:5d88952cbf96
implemened distinguishing float/managed geometries of clients (works quite well)
author | Anselm R. Garbe <garbeam@wmii.de> |
---|---|
date | Tue, 18 Jul 2006 12:36:57 +0200 |
parents | 6efe82c775c9 |
children | a19556fe83b5 |
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 Client *c;
196 XSetWindowAttributes twa;
197 Window trans;
199 c = emallocz(sizeof(Client));
200 c->win = w;
201 c->bx = c->fx = c->tx = wa->x;
202 c->by = c->fy = c->ty = wa->y;
203 if(c->fy < bh)
204 c->by = c->fy = c->ty += bh;
205 c->bw = c->fw = c->tw = wa->width;
206 c->fh = c->th = wa->height;
207 c->bh = bh;
208 c->border = 1;
209 c->proto = getproto(c->win);
210 setsize(c);
211 XSelectInput(dpy, c->win,
212 StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
213 XGetTransientForHint(dpy, c->win, &trans);
214 twa.override_redirect = 1;
215 twa.background_pixmap = ParentRelative;
216 twa.event_mask = ExposureMask;
218 c->title = XCreateWindow(dpy, root, c->bx, c->by, c->bw, c->bh,
219 0, DefaultDepth(dpy, screen), CopyFromParent,
220 DefaultVisual(dpy, screen),
221 CWOverrideRedirect | CWBackPixmap | CWEventMask, &twa);
223 settags(c);
225 c->next = clients;
226 clients = c;
228 XGrabButton(dpy, Button1, ControlMask, c->win, False, ButtonPressMask,
229 GrabModeAsync, GrabModeSync, None, None);
230 XGrabButton(dpy, Button1, Mod1Mask, c->win, False, ButtonPressMask,
231 GrabModeAsync, GrabModeSync, None, None);
232 XGrabButton(dpy, Button2, Mod1Mask, c->win, False, ButtonPressMask,
233 GrabModeAsync, GrabModeSync, None, None);
234 XGrabButton(dpy, Button3, Mod1Mask, c->win, False, ButtonPressMask,
235 GrabModeAsync, GrabModeSync, None, None);
237 if(!c->isfloat)
238 c->isfloat = trans
239 || ((c->maxw == c->minw) && (c->maxh == c->minh));
241 setgeom(c);
242 settitle(c);
244 arrange(NULL);
246 /* mapping the window now prevents flicker */
247 if(c->tags[tsel]) {
248 XMapRaised(dpy, c->win);
249 XMapRaised(dpy, c->title);
250 focus(c);
251 }
252 else {
253 ban(c);
254 XMapRaised(dpy, c->win);
255 XMapRaised(dpy, c->title);
256 XSync(dpy, False);
257 }
258 }
260 void
261 maximize(Arg *arg)
262 {
263 if(!sel)
264 return;
265 *sel->x = sx;
266 *sel->y = sy + bh;
267 *sel->w = sw - 2 * sel->border;
268 *sel->h = sh - 2 * sel->border - bh;
269 higher(sel);
270 resize(sel, False);
271 }
273 void
274 pop(Client *c)
275 {
276 Client **l;
277 for(l = &clients; *l && *l != c; l = &(*l)->next);
278 *l = c->next;
280 c->next = clients; /* pop */
281 clients = c;
282 arrange(NULL);
283 }
285 void
286 resize(Client *c, Bool inc)
287 {
288 XConfigureEvent e;
290 if(inc) {
291 if(c->incw)
292 *c->w -= (*c->w - c->basew) % c->incw;
293 if(c->inch)
294 *c->h -= (*c->h - c->baseh) % c->inch;
295 }
296 if(*c->x > sw) /* might happen on restart */
297 *c->x = sw - *c->w;
298 if(*c->y > sh)
299 *c->y = sh - *c->h;
300 if(c->minw && *c->w < c->minw)
301 *c->w = c->minw;
302 if(c->minh && *c->h < c->minh)
303 *c->h = c->minh;
304 if(c->maxw && *c->w > c->maxw)
305 *c->w = c->maxw;
306 if(c->maxh && *c->h > c->maxh)
307 *c->h = c->maxh;
308 resizetitle(c);
309 XSetWindowBorderWidth(dpy, c->win, 1);
310 XMoveResizeWindow(dpy, c->win, *c->x, *c->y, *c->w, *c->h);
311 e.type = ConfigureNotify;
312 e.event = c->win;
313 e.window = c->win;
314 e.x = *c->x;
315 e.y = *c->y;
316 e.width = *c->w;
317 e.height = *c->h;
318 e.border_width = c->border;
319 e.above = None;
320 e.override_redirect = False;
321 XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&e);
322 XSync(dpy, False);
323 }
325 void
326 setgeom(Client *c)
327 {
328 if((arrange == dotile) && !c->isfloat) {
329 c->x = &c->tx;
330 c->y = &c->ty;
331 c->w = &c->tw;
332 c->h = &c->th;
333 }
334 else {
335 c->x = &c->fx;
336 c->y = &c->fy;
337 c->w = &c->fw;
338 c->h = &c->fh;
339 }
340 }
342 void
343 setsize(Client *c)
344 {
345 XSizeHints size;
346 long msize;
347 if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
348 size.flags = PSize;
349 c->flags = size.flags;
350 if(c->flags & PBaseSize) {
351 c->basew = size.base_width;
352 c->baseh = size.base_height;
353 }
354 else
355 c->basew = c->baseh = 0;
356 if(c->flags & PResizeInc) {
357 c->incw = size.width_inc;
358 c->inch = size.height_inc;
359 }
360 else
361 c->incw = c->inch = 0;
362 if(c->flags & PMaxSize) {
363 c->maxw = size.max_width;
364 c->maxh = size.max_height;
365 }
366 else
367 c->maxw = c->maxh = 0;
368 if(c->flags & PMinSize) {
369 c->minw = size.min_width;
370 c->minh = size.min_height;
371 }
372 else
373 c->minw = c->minh = 0;
374 if(c->flags & PWinGravity)
375 c->grav = size.win_gravity;
376 else
377 c->grav = NorthWestGravity;
378 }
380 void
381 settitle(Client *c)
382 {
383 XTextProperty name;
384 int n;
385 char **list = NULL;
387 name.nitems = 0;
388 c->name[0] = 0;
389 XGetTextProperty(dpy, c->win, &name, netatom[NetWMName]);
390 if(!name.nitems)
391 XGetWMName(dpy, c->win, &name);
392 if(!name.nitems)
393 return;
394 if(name.encoding == XA_STRING)
395 strncpy(c->name, (char *)name.value, sizeof(c->name));
396 else {
397 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
398 && n > 0 && *list)
399 {
400 strncpy(c->name, *list, sizeof(c->name));
401 XFreeStringList(list);
402 }
403 }
404 XFree(name.value);
405 resizetitle(c);
406 }
408 void
409 unmanage(Client *c)
410 {
411 Client **l;
413 XGrabServer(dpy);
414 XSetErrorHandler(xerrordummy);
416 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
417 XDestroyWindow(dpy, c->title);
419 for(l = &clients; *l && *l != c; l = &(*l)->next);
420 *l = c->next;
421 for(l = &clients; *l; l = &(*l)->next)
422 if((*l)->revert == c)
423 (*l)->revert = NULL;
424 if(sel == c)
425 sel = sel->revert ? sel->revert : clients;
427 free(c);
429 XSync(dpy, False);
430 XSetErrorHandler(xerror);
431 XUngrabServer(dpy);
432 arrange(NULL);
433 if(sel)
434 focus(sel);
435 }
437 void
438 zoom(Arg *arg)
439 {
440 Client *c;
442 if(!sel)
443 return;
445 if(sel == getnext(clients, tsel) && sel->next) {
446 if((c = getnext(sel->next, tsel)))
447 sel = c;
448 }
450 pop(sel);
451 focus(sel);
452 }