aewl

view client.c @ 404:4439d1168b6c

Added tag 1.3 for changeset 6413ea66b1c2496cfc2173fe58df67cb965eb1da
author Anselm R. Garbe <arg@10kloc.org>
date Mon, 04 Sep 2006 17:18:01 +0200
parents 052657ff2e7b
children dd44dc937e8b
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;
87 if(!issel)
88 return;
89 if(!sel)
90 sel = c;
91 else if(sel != c) {
92 if(maximized)
93 togglemax(NULL);
94 old = sel;
95 sel = c;
96 if(old) {
97 grabbuttons(old, False);
98 drawtitle(old);
99 }
100 }
101 if(c) {
102 grabbuttons(c, True);
103 drawtitle(c);
104 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
105 }
106 else
107 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
108 }
110 Client *
111 getclient(Window w)
112 {
113 Client *c;
115 for(c = clients; c; c = c->next)
116 if(c->win == w)
117 return c;
118 return NULL;
119 }
121 Client *
122 getctitle(Window w)
123 {
124 Client *c;
126 for(c = clients; c; c = c->next)
127 if(c->twin == w)
128 return c;
129 return NULL;
130 }
132 void
133 gravitate(Client *c, Bool invert)
134 {
135 int dx = 0, dy = 0;
137 switch(c->grav) {
138 default:
139 break;
140 case StaticGravity:
141 case NorthWestGravity:
142 case NorthGravity:
143 case NorthEastGravity:
144 dy = c->border;
145 break;
146 case EastGravity:
147 case CenterGravity:
148 case WestGravity:
149 dy = -(c->h / 2) + c->border;
150 break;
151 case SouthEastGravity:
152 case SouthGravity:
153 case SouthWestGravity:
154 dy = -(c->h);
155 break;
156 }
158 switch (c->grav) {
159 default:
160 break;
161 case StaticGravity:
162 case NorthWestGravity:
163 case WestGravity:
164 case SouthWestGravity:
165 dx = c->border;
166 break;
167 case NorthGravity:
168 case CenterGravity:
169 case SouthGravity:
170 dx = -(c->w / 2) + c->border;
171 break;
172 case NorthEastGravity:
173 case EastGravity:
174 case SouthEastGravity:
175 dx = -(c->w + c->border);
176 break;
177 }
179 if(invert) {
180 dx = -dx;
181 dy = -dy;
182 }
183 c->x += dx;
184 c->y += dy;
185 }
187 void
188 killclient(Arg *arg)
189 {
190 if(!sel)
191 return;
192 if(sel->proto & PROTODELWIN)
193 sendevent(sel->win, wmatom[WMProtocols], wmatom[WMDelete]);
194 else
195 XKillClient(dpy, sel->win);
196 }
198 void
199 manage(Window w, XWindowAttributes *wa)
200 {
201 unsigned int i;
202 Client *c, *tc;
203 Window trans;
204 XSetWindowAttributes twa;
206 c = emallocz(sizeof(Client));
207 c->tags = emallocz(ntags * sizeof(Bool));
208 c->win = w;
209 c->x = c->tx = wa->x;
210 c->y = c->ty = wa->y;
211 c->w = c->tw = wa->width;
212 c->h = wa->height;
213 c->th = bh;
215 c->border = 0;
216 setsize(c);
218 if(c->x + c->w + 2 > sw)
219 c->x = sw - c->w - 2;
220 if(c->x < 0)
221 c->x = 0;
222 if(c->y + c->h + 2 > sh)
223 c->y = sh - c->h - 2;
224 if(c->h != sh && c->y < bh)
225 c->y = bh;
227 c->proto = getproto(c->win);
228 XSelectInput(dpy, c->win,
229 StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
230 XGetTransientForHint(dpy, c->win, &trans);
231 twa.override_redirect = 1;
232 twa.background_pixmap = ParentRelative;
233 twa.event_mask = ExposureMask | EnterWindowMask;
235 c->twin = XCreateWindow(dpy, root, c->tx, c->ty, c->tw, c->th,
236 0, DefaultDepth(dpy, screen), CopyFromParent,
237 DefaultVisual(dpy, screen),
238 CWOverrideRedirect | CWBackPixmap | CWEventMask, &twa);
240 grabbuttons(c, False);
241 if((tc = getclient(trans))) /* inherit tags */
242 for(i = 0; i < ntags; i++)
243 c->tags[i] = tc->tags[i];
244 else
245 settags(c);
246 if(!c->isfloat)
247 c->isfloat = trans
248 || (c->maxw && c->minw &&
249 c->maxw == c->minw && c->maxh == c->minh);
251 if(clients)
252 clients->prev = c;
253 c->next = clients;
254 clients = c;
256 settitle(c);
257 if(isvisible(c))
258 sel = c;
259 arrange(NULL);
260 XMapWindow(dpy, c->win);
261 XMapWindow(dpy, c->twin);
262 if(isvisible(c))
263 focus(c);
264 }
266 void
267 resize(Client *c, Bool sizehints, Corner sticky)
268 {
269 int bottom = c->y + c->h;
270 int right = c->x + c->w;
271 XWindowChanges wc;
273 if(sizehints) {
274 if(c->incw)
275 c->w -= (c->w - c->basew) % c->incw;
276 if(c->inch)
277 c->h -= (c->h - c->baseh) % c->inch;
278 if(c->minw && c->w < c->minw)
279 c->w = c->minw;
280 if(c->minh && c->h < c->minh)
281 c->h = c->minh;
282 if(c->maxw && c->w > c->maxw)
283 c->w = c->maxw;
284 if(c->maxh && c->h > c->maxh)
285 c->h = c->maxh;
286 }
287 if(sticky == TopRight || sticky == BotRight)
288 c->x = right - c->w;
289 if(sticky == BotLeft || sticky == BotRight)
290 c->y = bottom - c->h;
292 resizetitle(c);
293 wc.x = c->x;
294 wc.y = c->y;
295 wc.width = c->w;
296 wc.height = c->h;
297 if(c->w == sw && c->h == sh)
298 wc.border_width = 0;
299 else
300 wc.border_width = 1;
301 XConfigureWindow(dpy, c->win, CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
302 XSync(dpy, False);
303 }
305 void
306 setsize(Client *c)
307 {
308 long msize;
309 XSizeHints size;
311 if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
312 size.flags = PSize;
313 c->flags = size.flags;
314 if(c->flags & PBaseSize) {
315 c->basew = size.base_width;
316 c->baseh = size.base_height;
317 }
318 else
319 c->basew = c->baseh = 0;
320 if(c->flags & PResizeInc) {
321 c->incw = size.width_inc;
322 c->inch = size.height_inc;
323 }
324 else
325 c->incw = c->inch = 0;
326 if(c->flags & PMaxSize) {
327 c->maxw = size.max_width;
328 c->maxh = size.max_height;
329 }
330 else
331 c->maxw = c->maxh = 0;
332 if(c->flags & PMinSize) {
333 c->minw = size.min_width;
334 c->minh = size.min_height;
335 }
336 else
337 c->minw = c->minh = 0;
338 if(c->flags & PWinGravity)
339 c->grav = size.win_gravity;
340 else
341 c->grav = NorthWestGravity;
342 }
344 void
345 settitle(Client *c)
346 {
347 char **list = NULL;
348 int n;
349 XTextProperty name;
351 name.nitems = 0;
352 c->name[0] = 0;
353 XGetTextProperty(dpy, c->win, &name, netatom[NetWMName]);
354 if(!name.nitems)
355 XGetWMName(dpy, c->win, &name);
356 if(!name.nitems)
357 return;
358 if(name.encoding == XA_STRING)
359 strncpy(c->name, (char *)name.value, sizeof(c->name));
360 else {
361 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
362 && n > 0 && *list)
363 {
364 strncpy(c->name, *list, sizeof(c->name));
365 XFreeStringList(list);
366 }
367 }
368 XFree(name.value);
369 resizetitle(c);
370 }
372 void
373 togglemax(Arg *arg)
374 {
375 int ox, oy, ow, oh;
376 Client *c;
377 XEvent ev;
379 if(!sel)
380 return;
382 if((maximized = !maximized)) {
383 ox = sel->x;
384 oy = sel->y;
385 ow = sel->w;
386 oh = sel->h;
387 sel->x = sx;
388 sel->y = sy + bh;
389 sel->w = sw - 2;
390 sel->h = sh - 2 - bh;
392 restack();
393 for(c = getnext(clients); c; c = getnext(c->next))
394 if(c != sel)
395 ban(c);
396 resize(sel, arrange == dofloat, TopLeft);
398 sel->x = ox;
399 sel->y = oy;
400 sel->w = ow;
401 sel->h = oh;
402 }
403 else
404 arrange(NULL);
405 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
406 }
408 void
409 unmanage(Client *c)
410 {
411 Client *tc, *fc;
412 Window trans;
413 XGrabServer(dpy);
414 XSetErrorHandler(xerrordummy);
416 detach(c);
417 if(sel == c) {
418 XGetTransientForHint(dpy, c->win, &trans);
419 if(trans && (tc = getclient(trans)) && isvisible(tc))
420 fc = tc;
421 else
422 fc = getnext(clients);
423 focus(fc);
424 }
426 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
427 XDestroyWindow(dpy, c->twin);
429 free(c->tags);
430 free(c);
432 XSync(dpy, False);
433 XSetErrorHandler(xerror);
434 XUngrabServer(dpy);
435 arrange(NULL);
436 }