dwm-meillo

view client.c @ 628:a75123ef4b5f

made gravitate effectless, waiting for complains ;)
author arg@mig29
date Mon, 11 Dec 2006 08:31:30 +0100
parents 601842ee4484
children 58851a8806b9
line source
1 /* (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
2 * See LICENSE file for license details.
3 */
4 #include "dwm.h"
5 #include <stdlib.h>
6 #include <string.h>
7 #include <X11/Xatom.h>
8 #include <X11/Xutil.h>
10 /* static functions */
12 static void
13 detachstack(Client *c) {
14 Client **tc;
15 for(tc=&stack; *tc && *tc != c; tc=&(*tc)->snext);
16 *tc = c->snext;
17 }
19 static void
20 grabbuttons(Client *c, Bool focused) {
21 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
23 if(focused) {
24 XGrabButton(dpy, Button1, MODKEY, c->win, False, BUTTONMASK,
25 GrabModeAsync, GrabModeSync, None, None);
26 XGrabButton(dpy, Button1, MODKEY | LockMask, c->win, False, BUTTONMASK,
27 GrabModeAsync, GrabModeSync, None, None);
28 XGrabButton(dpy, Button1, MODKEY | numlockmask, c->win, False, BUTTONMASK,
29 GrabModeAsync, GrabModeSync, None, None);
30 XGrabButton(dpy, Button1, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
31 GrabModeAsync, GrabModeSync, None, None);
33 XGrabButton(dpy, Button2, MODKEY, c->win, False, BUTTONMASK,
34 GrabModeAsync, GrabModeSync, None, None);
35 XGrabButton(dpy, Button2, MODKEY | LockMask, c->win, False, BUTTONMASK,
36 GrabModeAsync, GrabModeSync, None, None);
37 XGrabButton(dpy, Button2, MODKEY | numlockmask, c->win, False, BUTTONMASK,
38 GrabModeAsync, GrabModeSync, None, None);
39 XGrabButton(dpy, Button2, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
40 GrabModeAsync, GrabModeSync, None, None);
42 XGrabButton(dpy, Button3, MODKEY, c->win, False, BUTTONMASK,
43 GrabModeAsync, GrabModeSync, None, None);
44 XGrabButton(dpy, Button3, MODKEY | LockMask, c->win, False, BUTTONMASK,
45 GrabModeAsync, GrabModeSync, None, None);
46 XGrabButton(dpy, Button3, MODKEY | numlockmask, c->win, False, BUTTONMASK,
47 GrabModeAsync, GrabModeSync, None, None);
48 XGrabButton(dpy, Button3, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
49 GrabModeAsync, GrabModeSync, None, None);
50 }
51 else
52 XGrabButton(dpy, AnyButton, AnyModifier, c->win, False, BUTTONMASK,
53 GrabModeAsync, GrabModeSync, None, None);
54 }
56 static int
57 xerrordummy(Display *dsply, XErrorEvent *ee) {
58 return 0;
59 }
61 /* extern functions */
63 void
64 ban(Client *c) {
65 XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
66 XMoveWindow(dpy, c->twin, c->tx + 2 * sw, c->ty);
67 }
69 void
70 configure(Client *c) {
71 XEvent synev;
73 synev.type = ConfigureNotify;
74 synev.xconfigure.display = dpy;
75 synev.xconfigure.event = c->win;
76 synev.xconfigure.window = c->win;
77 synev.xconfigure.x = c->x;
78 synev.xconfigure.y = c->y;
79 synev.xconfigure.width = c->w;
80 synev.xconfigure.height = c->h;
81 synev.xconfigure.border_width = c->border;
82 synev.xconfigure.above = None;
83 XSendEvent(dpy, c->win, True, NoEventMask, &synev);
84 }
86 void
87 focus(Client *c) {
88 Client *old;
90 if(!issel || (c && !isvisible(c)))
91 return;
92 if(!sel)
93 sel = c;
94 else if(sel != c) {
95 old = sel;
96 sel = c;
97 if(old) {
98 grabbuttons(old, False);
99 drawtitle(old);
100 }
101 }
102 if(c) {
103 detachstack(c);
104 c->snext = stack;
105 stack = c;
106 grabbuttons(c, True);
107 drawtitle(c);
108 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
109 }
110 else
111 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
112 }
114 Client *
115 getclient(Window w) {
116 Client *c;
118 for(c = clients; c; c = c->next)
119 if(c->win == w)
120 return c;
121 return NULL;
122 }
124 Client *
125 getctitle(Window w) {
126 Client *c;
128 for(c = clients; c; c = c->next)
129 if(c->twin == w)
130 return c;
131 return NULL;
132 }
134 void
135 gravitate(Client *c, Bool invert) {
136 int dx = 0, dy = 0;
138 return;
139 switch(c->grav) {
140 default:
141 break;
142 case StaticGravity:
143 case NorthWestGravity:
144 case NorthGravity:
145 case NorthEastGravity:
146 dy = c->border;
147 break;
148 case EastGravity:
149 case CenterGravity:
150 case WestGravity:
151 dy = -(c->h / 2) + c->border;
152 break;
153 case SouthEastGravity:
154 case SouthGravity:
155 case SouthWestGravity:
156 dy = -(c->h);
157 break;
158 }
159 switch (c->grav) {
160 default:
161 break;
162 case StaticGravity:
163 case NorthWestGravity:
164 case WestGravity:
165 case SouthWestGravity:
166 dx = c->border;
167 break;
168 case NorthGravity:
169 case CenterGravity:
170 case SouthGravity:
171 dx = -(c->w / 2) + c->border;
172 break;
173 case NorthEastGravity:
174 case EastGravity:
175 case SouthEastGravity:
176 dx = -(c->w + c->border);
177 break;
178 }
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 if(!sel)
190 return;
191 if(sel->proto & PROTODELWIN)
192 sendevent(sel->win, wmatom[WMProtocols], wmatom[WMDelete]);
193 else
194 XKillClient(dpy, sel->win);
195 }
197 void
198 manage(Window w, XWindowAttributes *wa) {
199 Client *c;
200 Window trans;
201 XSetWindowAttributes twa;
203 c = emallocz(sizeof(Client));
204 c->tags = emallocz(ntags * sizeof(Bool));
205 c->win = w;
206 c->border = 0;
207 c->x = c->tx = wa->x;
208 c->y = c->ty = wa->y;
209 c->w = c->tw = wa->width;
210 c->h = wa->height;
211 c->th = bh;
212 updatesize(c);
213 if(c->x + c->w + 2 * BORDERPX > sw)
214 c->x = sw - c->w - 2 * BORDERPX;
215 if(c->x < sx)
216 c->x = sx;
217 if(c->y + c->h + 2 * BORDERPX > sh)
218 c->y = sh - c->h - 2 * BORDERPX;
219 if(c->h != sh && c->y < bh)
220 c->y = bh;
221 c->proto = getproto(c->win);
222 XSelectInput(dpy, c->win,
223 StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
224 XGetTransientForHint(dpy, c->win, &trans);
225 twa.override_redirect = 1;
226 twa.background_pixmap = ParentRelative;
227 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);
232 grabbuttons(c, False);
233 updatetitle(c);
234 settags(c, getclient(trans));
235 if(!c->isfloat)
236 c->isfloat = trans || c->isfixed;
237 resizetitle(c);
238 if(clients)
239 clients->prev = c;
240 c->next = clients;
241 c->snext = stack;
242 stack = clients = c;
243 ban(c);
244 XMapWindow(dpy, c->win);
245 XMapWindow(dpy, c->twin);
246 if(isvisible(c))
247 focus(c);
248 arrange();
249 }
251 void
252 resize(Client *c, Bool sizehints, Corner sticky) {
253 int bottom = c->y + c->h;
254 int right = c->x + c->w;
255 XWindowChanges wc;
257 if(sizehints) {
258 if(c->incw)
259 c->w -= (c->w - c->basew) % c->incw;
260 if(c->inch)
261 c->h -= (c->h - c->baseh) % c->inch;
262 if(c->minw && c->w < c->minw)
263 c->w = c->minw;
264 if(c->minh && c->h < c->minh)
265 c->h = c->minh;
266 if(c->maxw && c->w > c->maxw)
267 c->w = c->maxw;
268 if(c->maxh && c->h > c->maxh)
269 c->h = c->maxh;
270 }
271 if(sticky == TopRight || sticky == BotRight)
272 c->x = right - c->w;
273 if(sticky == BotLeft || sticky == BotRight)
274 c->y = bottom - c->h;
275 /* offscreen appearance fixes */
276 if(c->x + c->w < sx)
277 c->x = sx;
278 if(c->y + c->h < bh)
279 c->y = bh;
280 if(c->x > sw)
281 c->x = sw - c->w;
282 if(c->y > sh)
283 c->y = sh - c->h;
284 resizetitle(c);
285 wc.x = c->x;
286 wc.y = c->y;
287 wc.width = c->w;
288 wc.height = c->h;
289 if(c->w == sw && c->h == sh)
290 wc.border_width = 0;
291 else
292 wc.border_width = BORDERPX;
293 XConfigureWindow(dpy, c->win, CWX | CWY | CWWidth | CWHeight | CWBorderWidth, &wc);
294 configure(c);
295 XSync(dpy, False);
296 }
298 void
299 resizetitle(Client *c) {
300 c->tw = textw(c->name);
301 if(c->tw > c->w)
302 c->tw = c->w + 2 * BORDERPX;
303 c->tx = c->x + c->w - c->tw + 2 * BORDERPX;
304 c->ty = c->y;
305 if(isvisible(c))
306 XMoveResizeWindow(dpy, c->twin, c->tx, c->ty, c->tw, c->th);
307 else
308 XMoveResizeWindow(dpy, c->twin, c->tx + 2 * sw, c->ty, c->tw, c->th);
309 }
311 void
312 updatesize(Client *c) {
313 long msize;
314 XSizeHints size;
316 if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
317 size.flags = PSize;
318 c->flags = size.flags;
319 if(c->flags & PBaseSize) {
320 c->basew = size.base_width;
321 c->baseh = size.base_height;
322 }
323 else
324 c->basew = c->baseh = 0;
325 if(c->flags & PResizeInc) {
326 c->incw = size.width_inc;
327 c->inch = size.height_inc;
328 }
329 else
330 c->incw = c->inch = 0;
331 if(c->flags & PMaxSize) {
332 c->maxw = size.max_width;
333 c->maxh = size.max_height;
334 }
335 else
336 c->maxw = c->maxh = 0;
337 if(c->flags & PMinSize) {
338 c->minw = size.min_width;
339 c->minh = size.min_height;
340 }
341 else
342 c->minw = c->minh = 0;
343 c->isfixed = (c->maxw && c->minw && c->maxh && c->minh &&
344 c->maxw == c->minw && c->maxh == c->minh);
345 if(c->flags & PWinGravity)
346 c->grav = size.win_gravity;
347 else
348 c->grav = NorthWestGravity;
349 }
351 void
352 updatetitle(Client *c) {
353 char **list = NULL;
354 int n;
355 XTextProperty name;
357 name.nitems = 0;
358 c->name[0] = 0;
359 XGetTextProperty(dpy, c->win, &name, netatom[NetWMName]);
360 if(!name.nitems)
361 XGetWMName(dpy, c->win, &name);
362 if(!name.nitems)
363 return;
364 if(name.encoding == XA_STRING)
365 strncpy(c->name, (char *)name.value, sizeof c->name);
366 else {
367 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
368 && n > 0 && *list)
369 {
370 strncpy(c->name, *list, sizeof c->name);
371 XFreeStringList(list);
372 }
373 }
374 XFree(name.value);
375 }
377 void
378 unmanage(Client *c) {
379 Client *nc;
381 /* The server grab construct avoids race conditions. */
382 XGrabServer(dpy);
383 XSetErrorHandler(xerrordummy);
384 detach(c);
385 detachstack(c);
386 if(sel == c) {
387 for(nc = stack; nc && !isvisible(nc); nc = nc->snext);
388 focus(nc);
389 }
390 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
391 XDestroyWindow(dpy, c->twin);
392 free(c->tags);
393 free(c);
394 XSync(dpy, False);
395 XSetErrorHandler(xerror);
396 XUngrabServer(dpy);
397 arrange();
398 }