aewl

view client.c @ 379:fc279cd6c7be

fix client position in list also on tag and toggletag
author Anselm R. Garbe <arg@10kloc.org>
date Tue, 29 Aug 2006 09:25:14 +0200
parents b1159a638d0a
children b00cc483d13b
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 grabbuttons(Client *c, Bool focus)
15 {
16 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
18 if(focus) {
19 XGrabButton(dpy, Button1, MODKEY, c->win, False, BUTTONMASK,
20 GrabModeAsync, GrabModeSync, None, None);
21 XGrabButton(dpy, Button1, MODKEY | LockMask, c->win, False, BUTTONMASK,
22 GrabModeAsync, GrabModeSync, None, None);
23 XGrabButton(dpy, Button1, MODKEY | numlockmask, c->win, False, BUTTONMASK,
24 GrabModeAsync, GrabModeSync, None, None);
25 XGrabButton(dpy, Button1, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
26 GrabModeAsync, GrabModeSync, None, None);
28 XGrabButton(dpy, Button2, MODKEY, c->win, False, BUTTONMASK,
29 GrabModeAsync, GrabModeSync, None, None);
30 XGrabButton(dpy, Button2, MODKEY | LockMask, c->win, False, BUTTONMASK,
31 GrabModeAsync, GrabModeSync, None, None);
32 XGrabButton(dpy, Button2, MODKEY | numlockmask, c->win, False, BUTTONMASK,
33 GrabModeAsync, GrabModeSync, None, None);
34 XGrabButton(dpy, Button2, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
35 GrabModeAsync, GrabModeSync, None, None);
37 XGrabButton(dpy, Button3, MODKEY, c->win, False, BUTTONMASK,
38 GrabModeAsync, GrabModeSync, None, None);
39 XGrabButton(dpy, Button3, MODKEY | LockMask, c->win, False, BUTTONMASK,
40 GrabModeAsync, GrabModeSync, None, None);
41 XGrabButton(dpy, Button3, MODKEY | numlockmask, c->win, False, BUTTONMASK,
42 GrabModeAsync, GrabModeSync, None, None);
43 XGrabButton(dpy, Button3, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
44 GrabModeAsync, GrabModeSync, None, None);
45 }
46 else
47 XGrabButton(dpy, AnyButton, AnyModifier, c->win, False, BUTTONMASK,
48 GrabModeAsync, GrabModeSync, None, None);
50 }
52 static void
53 resizetitle(Client *c)
54 {
55 c->tw = textw(c->name);
56 if(c->tw > c->w)
57 c->tw = c->w + 2;
58 c->tx = c->x + c->w - c->tw + 2;
59 c->ty = c->y;
60 if(isvisible(c))
61 XMoveResizeWindow(dpy, c->twin, c->tx, c->ty, c->tw, c->th);
62 else
63 XMoveResizeWindow(dpy, c->twin, c->tx + 2 * sw, c->ty, c->tw, c->th);
65 }
67 static int
68 xerrordummy(Display *dsply, XErrorEvent *ee)
69 {
70 return 0;
71 }
73 /* extern functions */
75 void
76 ban(Client *c)
77 {
78 XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
79 XMoveWindow(dpy, c->twin, c->tx + 2 * sw, c->ty);
80 }
82 void
83 focus(Client *c)
84 {
85 Client *old = sel;
87 if(!issel)
88 return;
89 if(!sel)
90 sel = c;
91 else if(sel != c) {
92 if(sel->ismax)
93 togglemax(NULL);
94 sel = c;
95 grabbuttons(old, False);
96 drawtitle(old);
97 }
98 grabbuttons(c, True);
99 drawtitle(c);
100 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
101 }
103 Client *
104 getclient(Window w)
105 {
106 Client *c;
108 for(c = clients; c; c = c->next)
109 if(c->win == w)
110 return c;
111 return NULL;
112 }
114 Client *
115 getctitle(Window w)
116 {
117 Client *c;
119 for(c = clients; c; c = c->next)
120 if(c->twin == w)
121 return c;
122 return NULL;
123 }
125 void
126 gravitate(Client *c, Bool invert)
127 {
128 int dx = 0, dy = 0;
130 switch(c->grav) {
131 default:
132 break;
133 case StaticGravity:
134 case NorthWestGravity:
135 case NorthGravity:
136 case NorthEastGravity:
137 dy = c->border;
138 break;
139 case EastGravity:
140 case CenterGravity:
141 case WestGravity:
142 dy = -(c->h / 2) + c->border;
143 break;
144 case SouthEastGravity:
145 case SouthGravity:
146 case SouthWestGravity:
147 dy = -(c->h);
148 break;
149 }
151 switch (c->grav) {
152 default:
153 break;
154 case StaticGravity:
155 case NorthWestGravity:
156 case WestGravity:
157 case SouthWestGravity:
158 dx = c->border;
159 break;
160 case NorthGravity:
161 case CenterGravity:
162 case SouthGravity:
163 dx = -(c->w / 2) + c->border;
164 break;
165 case NorthEastGravity:
166 case EastGravity:
167 case SouthEastGravity:
168 dx = -(c->w + c->border);
169 break;
170 }
172 if(invert) {
173 dx = -dx;
174 dy = -dy;
175 }
176 c->x += dx;
177 c->y += dy;
178 }
180 void
181 killclient(Arg *arg)
182 {
183 if(!sel)
184 return;
185 if(sel->proto & PROTODELWIN)
186 sendevent(sel->win, wmatom[WMProtocols], wmatom[WMDelete]);
187 else
188 XKillClient(dpy, sel->win);
189 }
191 void
192 manage(Window w, XWindowAttributes *wa)
193 {
194 unsigned int i;
195 Client *c, *tc;
196 Window trans;
197 XSetWindowAttributes twa;
199 c = emallocz(sizeof(Client));
200 c->tags = emallocz(ntags * sizeof(Bool));
201 c->win = w;
202 c->x = c->tx = wa->x;
203 c->y = c->ty = wa->y;
204 c->w = c->tw = wa->width;
205 c->h = wa->height;
206 c->th = bh;
208 c->border = 0;
209 setsize(c);
211 if(c->x + c->w + 2 > sw)
212 c->x = sw - c->w - 2;
213 if(c->x < 0)
214 c->x = 0;
215 if(c->y + c->h + 2 > sh)
216 c->y = sh - c->h - 2;
217 if(c->h != sh && c->y < bh)
218 c->y = bh;
220 c->proto = getproto(c->win);
221 XSelectInput(dpy, c->win,
222 StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
223 XGetTransientForHint(dpy, c->win, &trans);
224 twa.override_redirect = 1;
225 twa.background_pixmap = ParentRelative;
226 twa.event_mask = ExposureMask | EnterWindowMask;
228 c->twin = XCreateWindow(dpy, root, c->tx, c->ty, c->tw, c->th,
229 0, DefaultDepth(dpy, screen), CopyFromParent,
230 DefaultVisual(dpy, screen),
231 CWOverrideRedirect | CWBackPixmap | CWEventMask, &twa);
233 grabbuttons(c, False);
234 if((tc = getclient(trans))) /* inherit tags */
235 for(i = 0; i < ntags; i++)
236 c->tags[i] = tc->tags[i];
237 else
238 settags(c);
239 if(!c->isfloat)
240 c->isfloat = trans
241 || (c->maxw && c->minw &&
242 c->maxw == c->minw && c->maxh == c->minh);
244 attach(c);
246 settitle(c);
247 if(isvisible(c))
248 sel = c;
249 arrange(NULL);
250 XMapWindow(dpy, c->win);
251 XMapWindow(dpy, c->twin);
252 if(isvisible(c))
253 focus(c);
254 }
256 void
257 resize(Client *c, Bool sizehints, Corner sticky)
258 {
259 int bottom = c->y + c->h;
260 int right = c->x + c->w;
261 XWindowChanges wc;
263 if(sizehints) {
264 if(c->incw)
265 c->w -= (c->w - c->basew) % c->incw;
266 if(c->inch)
267 c->h -= (c->h - c->baseh) % c->inch;
268 if(c->minw && c->w < c->minw)
269 c->w = c->minw;
270 if(c->minh && c->h < c->minh)
271 c->h = c->minh;
272 if(c->maxw && c->w > c->maxw)
273 c->w = c->maxw;
274 if(c->maxh && c->h > c->maxh)
275 c->h = c->maxh;
276 }
277 if(sticky == TopRight || sticky == BotRight)
278 c->x = right - c->w;
279 if(sticky == BotLeft || sticky == BotRight)
280 c->y = bottom - c->h;
282 resizetitle(c);
283 wc.x = c->x;
284 wc.y = c->y;
285 wc.width = c->w;
286 wc.height = c->h;
287 if(c->w == sw && c->h == sh)
288 wc.border_width = 0;
289 else
290 wc.border_width = 1;
291 XConfigureWindow(dpy, c->win, CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
292 XSync(dpy, False);
293 }
295 void
296 setsize(Client *c)
297 {
298 long msize;
299 XSizeHints size;
301 if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
302 size.flags = PSize;
303 c->flags = size.flags;
304 if(c->flags & PBaseSize) {
305 c->basew = size.base_width;
306 c->baseh = size.base_height;
307 }
308 else
309 c->basew = c->baseh = 0;
310 if(c->flags & PResizeInc) {
311 c->incw = size.width_inc;
312 c->inch = size.height_inc;
313 }
314 else
315 c->incw = c->inch = 0;
316 if(c->flags & PMaxSize) {
317 c->maxw = size.max_width;
318 c->maxh = size.max_height;
319 }
320 else
321 c->maxw = c->maxh = 0;
322 if(c->flags & PMinSize) {
323 c->minw = size.min_width;
324 c->minh = size.min_height;
325 }
326 else
327 c->minw = c->minh = 0;
328 if(c->flags & PWinGravity)
329 c->grav = size.win_gravity;
330 else
331 c->grav = NorthWestGravity;
332 }
334 void
335 settitle(Client *c)
336 {
337 char **list = NULL;
338 int n;
339 XTextProperty name;
341 name.nitems = 0;
342 c->name[0] = 0;
343 XGetTextProperty(dpy, c->win, &name, netatom[NetWMName]);
344 if(!name.nitems)
345 XGetWMName(dpy, c->win, &name);
346 if(!name.nitems)
347 return;
348 if(name.encoding == XA_STRING)
349 strncpy(c->name, (char *)name.value, sizeof(c->name));
350 else {
351 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
352 && n > 0 && *list)
353 {
354 strncpy(c->name, *list, sizeof(c->name));
355 XFreeStringList(list);
356 }
357 }
358 XFree(name.value);
359 resizetitle(c);
360 }
362 void
363 togglemax(Arg *arg)
364 {
365 int ox, oy, ow, oh;
366 XEvent ev;
368 if(!sel)
369 return;
371 if((sel->ismax = !sel->ismax)) {
372 ox = sel->x;
373 oy = sel->y;
374 ow = sel->w;
375 oh = sel->h;
376 sel->x = sx;
377 sel->y = sy + bh;
378 sel->w = sw - 2;
379 sel->h = sh - 2 - bh;
381 restack();
382 resize(sel, arrange == dofloat, TopLeft);
384 sel->x = ox;
385 sel->y = oy;
386 sel->w = ow;
387 sel->h = oh;
388 }
389 else
390 resize(sel, False, TopLeft);
391 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
392 }
394 void
395 unmanage(Client *c)
396 {
397 Client *tc;
398 Window trans;
399 XGrabServer(dpy);
400 XSetErrorHandler(xerrordummy);
402 XGetTransientForHint(dpy, c->win, &trans);
404 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
405 XDestroyWindow(dpy, c->twin);
407 detach(c);
408 if(sel == c) {
409 if(trans && (tc = getclient(trans)) && isvisible(tc))
410 sel = tc;
411 else
412 sel = getnext(clients);
413 }
414 free(c->tags);
415 free(c);
417 XSync(dpy, False);
418 XSetErrorHandler(xerror);
419 XUngrabServer(dpy);
420 if(sel)
421 focus(sel);
422 arrange(NULL);
423 }