dwm-meillo
view client.c @ 475:898c54484cb2
slight change to config.arg.h (I use ff floating)
author | arg@mmvi |
---|---|
date | Fri, 22 Sep 2006 08:22:39 +0200 |
parents | 298be2d65d2e |
children | de69a7b0c8fa |
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 detachstack(Client *c) {
15 Client **tc;
16 for(tc=&stack; *tc && *tc != c; tc=&(*tc)->snext);
17 *tc = c->snext;
18 }
20 static void
21 grabbuttons(Client *c, Bool focused) {
22 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
24 if(focused) {
25 XGrabButton(dpy, Button1, MODKEY, c->win, False, BUTTONMASK,
26 GrabModeAsync, GrabModeSync, None, None);
27 XGrabButton(dpy, Button1, MODKEY | LockMask, c->win, False, BUTTONMASK,
28 GrabModeAsync, GrabModeSync, None, None);
29 XGrabButton(dpy, Button1, MODKEY | numlockmask, c->win, False, BUTTONMASK,
30 GrabModeAsync, GrabModeSync, None, None);
31 XGrabButton(dpy, Button1, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
32 GrabModeAsync, GrabModeSync, None, None);
34 XGrabButton(dpy, Button2, MODKEY, c->win, False, BUTTONMASK,
35 GrabModeAsync, GrabModeSync, None, None);
36 XGrabButton(dpy, Button2, MODKEY | LockMask, c->win, False, BUTTONMASK,
37 GrabModeAsync, GrabModeSync, None, None);
38 XGrabButton(dpy, Button2, MODKEY | numlockmask, c->win, False, BUTTONMASK,
39 GrabModeAsync, GrabModeSync, None, None);
40 XGrabButton(dpy, Button2, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
41 GrabModeAsync, GrabModeSync, None, None);
43 XGrabButton(dpy, Button3, MODKEY, c->win, False, BUTTONMASK,
44 GrabModeAsync, GrabModeSync, None, None);
45 XGrabButton(dpy, Button3, MODKEY | LockMask, c->win, False, BUTTONMASK,
46 GrabModeAsync, GrabModeSync, None, None);
47 XGrabButton(dpy, Button3, MODKEY | numlockmask, c->win, False, BUTTONMASK,
48 GrabModeAsync, GrabModeSync, None, None);
49 XGrabButton(dpy, Button3, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
50 GrabModeAsync, GrabModeSync, None, None);
51 }
52 else
53 XGrabButton(dpy, AnyButton, AnyModifier, c->win, False, BUTTONMASK,
54 GrabModeAsync, GrabModeSync, None, None);
55 }
57 static void
58 resizetitle(Client *c) {
59 c->tw = textw(c->name);
60 if(c->tw > c->w)
61 c->tw = c->w + 2;
62 c->tx = c->x + c->w - c->tw + 2;
63 c->ty = c->y;
64 if(isvisible(c))
65 XMoveResizeWindow(dpy, c->twin, c->tx, c->ty, c->tw, c->th);
66 else
67 XMoveResizeWindow(dpy, c->twin, c->tx + 2 * sw, c->ty, c->tw, c->th);
68 }
70 static int
71 xerrordummy(Display *dsply, XErrorEvent *ee) {
72 return 0;
73 }
75 /* extern functions */
77 void
78 ban(Client *c) {
79 XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
80 XMoveWindow(dpy, c->twin, c->tx + 2 * sw, c->ty);
81 }
83 void
84 focus(Client *c) {
85 Client *old;
87 if(!issel || (c && !isvisible(c)))
88 return;
89 if(!sel)
90 sel = c;
91 else if(sel != c) {
92 old = sel;
93 sel = c;
94 if(old) {
95 grabbuttons(old, False);
96 drawtitle(old);
97 }
98 }
99 if(c) {
100 detachstack(c);
101 c->snext = stack;
102 stack = c;
103 grabbuttons(c, True);
104 drawtitle(c);
105 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
106 }
107 else
108 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
109 }
111 Client *
112 getclient(Window w) {
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 Client *c;
125 for(c = clients; c; c = c->next)
126 if(c->twin == w)
127 return c;
128 return NULL;
129 }
131 void
132 gravitate(Client *c, Bool invert) {
133 int dx = 0, dy = 0;
135 switch(c->grav) {
136 default:
137 break;
138 case StaticGravity:
139 case NorthWestGravity:
140 case NorthGravity:
141 case NorthEastGravity:
142 dy = c->border;
143 break;
144 case EastGravity:
145 case CenterGravity:
146 case WestGravity:
147 dy = -(c->h / 2) + c->border;
148 break;
149 case SouthEastGravity:
150 case SouthGravity:
151 case SouthWestGravity:
152 dy = -(c->h);
153 break;
154 }
156 switch (c->grav) {
157 default:
158 break;
159 case StaticGravity:
160 case NorthWestGravity:
161 case WestGravity:
162 case SouthWestGravity:
163 dx = c->border;
164 break;
165 case NorthGravity:
166 case CenterGravity:
167 case SouthGravity:
168 dx = -(c->w / 2) + c->border;
169 break;
170 case NorthEastGravity:
171 case EastGravity:
172 case SouthEastGravity:
173 dx = -(c->w + c->border);
174 break;
175 }
177 if(invert) {
178 dx = -dx;
179 dy = -dy;
180 }
181 c->x += dx;
182 c->y += dy;
183 }
185 void
186 killclient(Arg *arg) {
187 if(!sel)
188 return;
189 if(sel->proto & PROTODELWIN)
190 sendevent(sel->win, wmatom[WMProtocols], wmatom[WMDelete]);
191 else
192 XKillClient(dpy, sel->win);
193 }
195 void
196 manage(Window w, XWindowAttributes *wa) {
197 Client *c;
198 Window trans;
199 XSetWindowAttributes twa;
201 c = emallocz(sizeof(Client));
202 c->tags = emallocz(ntags * sizeof(Bool));
203 c->win = w;
204 c->x = c->tx = wa->x;
205 c->y = c->ty = wa->y;
206 c->w = c->tw = wa->width;
207 c->h = wa->height;
208 c->th = bh;
209 c->rx = sx;
210 c->ry = bh;
211 c->rw = sw;
212 c->rh = sh - bh;
214 c->border = 0;
215 updatesize(c);
217 if(c->x + c->w + 2 > sw)
218 c->x = sw - c->w - 2;
219 if(c->x < 0)
220 c->x = 0;
221 if(c->y + c->h + 2 > sh)
222 c->y = sh - c->h - 2;
223 if(c->h != sh && c->y < bh)
224 c->y = bh;
226 c->proto = getproto(c->win);
227 XSelectInput(dpy, c->win,
228 StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
229 XGetTransientForHint(dpy, c->win, &trans);
230 twa.override_redirect = 1;
231 twa.background_pixmap = ParentRelative;
232 twa.event_mask = ExposureMask | EnterWindowMask;
234 c->twin = XCreateWindow(dpy, root, c->tx, c->ty, c->tw, c->th,
235 0, DefaultDepth(dpy, screen), CopyFromParent,
236 DefaultVisual(dpy, screen),
237 CWOverrideRedirect | CWBackPixmap | CWEventMask, &twa);
239 grabbuttons(c, False);
240 settags(c, getclient(trans));
241 if(!c->isfloat)
242 c->isfloat = trans
243 || (c->maxw && c->minw &&
244 c->maxw == c->minw && c->maxh == c->minh);
246 if(clients)
247 clients->prev = c;
248 c->next = clients;
249 c->snext = stack;
250 stack = clients = c;
252 updatetitle(c);
253 ban(c);
254 XMapWindow(dpy, c->win);
255 XMapWindow(dpy, c->twin);
256 if(isvisible(c))
257 focus(c);
258 arrange(NULL);
259 }
261 void
262 resize(Client *c, Bool sizehints, Corner sticky) {
263 int bottom = c->y + c->h;
264 int right = c->x + c->w;
265 XWindowChanges wc;
267 if(sizehints) {
268 if(c->incw)
269 c->w -= (c->w - c->basew) % c->incw;
270 if(c->inch)
271 c->h -= (c->h - c->baseh) % c->inch;
272 if(c->minw && c->w < c->minw)
273 c->w = c->minw;
274 if(c->minh && c->h < c->minh)
275 c->h = c->minh;
276 if(c->maxw && c->w > c->maxw)
277 c->w = c->maxw;
278 if(c->maxh && c->h > c->maxh)
279 c->h = c->maxh;
280 }
281 if(sticky == TopRight || sticky == BotRight)
282 c->x = right - c->w;
283 if(sticky == BotLeft || sticky == BotRight)
284 c->y = bottom - c->h;
286 /* offscreen appearance fixes */
287 if(c->x + c->w < 0)
288 c->x = 0;
289 if(c->y + c->h < bh)
290 c->y = bh;
291 if(c->x > sw)
292 c->x = sw - c->w;
293 if(c->y > sh)
294 c->y = sh - c->h;
296 resizetitle(c);
297 wc.x = c->x;
298 wc.y = c->y;
299 wc.width = c->w;
300 wc.height = c->h;
301 if(c->w == sw && c->h == sh)
302 wc.border_width = 0;
303 else
304 wc.border_width = 1;
305 XConfigureWindow(dpy, c->win, CWX | CWY | CWWidth | CWHeight | CWBorderWidth, &wc);
306 XSync(dpy, False);
307 }
309 void
310 updatesize(Client *c) {
311 long msize;
312 XSizeHints size;
314 if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
315 size.flags = PSize;
316 c->flags = size.flags;
317 if(c->flags & PBaseSize) {
318 c->basew = size.base_width;
319 c->baseh = size.base_height;
320 }
321 else
322 c->basew = c->baseh = 0;
323 if(c->flags & PResizeInc) {
324 c->incw = size.width_inc;
325 c->inch = size.height_inc;
326 }
327 else
328 c->incw = c->inch = 0;
329 if(c->flags & PMaxSize) {
330 c->maxw = size.max_width;
331 c->maxh = size.max_height;
332 }
333 else
334 c->maxw = c->maxh = 0;
335 if(c->flags & PMinSize) {
336 c->minw = size.min_width;
337 c->minh = size.min_height;
338 }
339 else
340 c->minw = c->minh = 0;
341 if(c->flags & PWinGravity)
342 c->grav = size.win_gravity;
343 else
344 c->grav = NorthWestGravity;
345 }
347 void
348 updatetitle(Client *c) {
349 char **list = NULL;
350 int n;
351 XTextProperty name;
353 name.nitems = 0;
354 c->name[0] = 0;
355 XGetTextProperty(dpy, c->win, &name, netatom[NetWMName]);
356 if(!name.nitems)
357 XGetWMName(dpy, c->win, &name);
358 if(!name.nitems)
359 return;
360 if(name.encoding == XA_STRING)
361 strncpy(c->name, (char *)name.value, sizeof(c->name));
362 else {
363 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
364 && n > 0 && *list)
365 {
366 strncpy(c->name, *list, sizeof(c->name));
367 XFreeStringList(list);
368 }
369 }
370 XFree(name.value);
371 resizetitle(c);
372 }
374 void
375 unmanage(Client *c) {
376 Client *nc;
378 /* The server grab construct avoids race conditions. */
379 XGrabServer(dpy);
380 XSetErrorHandler(xerrordummy);
382 detach(c);
383 detachstack(c);
384 if(sel == c) {
385 for(nc = stack; nc && !isvisible(nc); nc = nc->snext);
386 focus(nc);
387 }
389 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
390 XDestroyWindow(dpy, c->twin);
392 free(c->tags);
393 free(c);
395 XSync(dpy, False);
396 XSetErrorHandler(xerror);
397 XUngrabServer(dpy);
398 arrange(NULL);
399 }