dwm-meillo
view client.c @ 469:a2cc7adf9d4d
improved intro comment in dwm.h, updated config.mk
author | arg@mmvi |
---|---|
date | Wed, 20 Sep 2006 09:40:35 +0200 |
parents | bd32d89ed9d5 |
children | 298be2d65d2e |
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 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 detachstack(c);
103 c->snext = stack;
104 stack = c;
105 grabbuttons(c, True);
106 drawtitle(c);
107 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
108 }
109 else
110 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
111 }
113 Client *
114 getclient(Window w) {
115 Client *c;
117 for(c = clients; c; c = c->next)
118 if(c->win == w)
119 return c;
120 return NULL;
121 }
123 Client *
124 getctitle(Window w) {
125 Client *c;
127 for(c = clients; c; c = c->next)
128 if(c->twin == w)
129 return c;
130 return NULL;
131 }
133 void
134 gravitate(Client *c, Bool invert) {
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 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->x = c->tx = wa->x;
207 c->y = c->ty = wa->y;
208 c->w = c->tw = wa->width;
209 c->h = wa->height;
210 c->th = bh;
212 c->border = 0;
213 updatesize(c);
215 if(c->x + c->w + 2 > sw)
216 c->x = sw - c->w - 2;
217 if(c->x < 0)
218 c->x = 0;
219 if(c->y + c->h + 2 > sh)
220 c->y = sh - c->h - 2;
221 if(c->h != sh && c->y < bh)
222 c->y = bh;
224 c->proto = getproto(c->win);
225 XSelectInput(dpy, c->win,
226 StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
227 XGetTransientForHint(dpy, c->win, &trans);
228 twa.override_redirect = 1;
229 twa.background_pixmap = ParentRelative;
230 twa.event_mask = ExposureMask | EnterWindowMask;
232 c->twin = XCreateWindow(dpy, root, c->tx, c->ty, c->tw, c->th,
233 0, DefaultDepth(dpy, screen), CopyFromParent,
234 DefaultVisual(dpy, screen),
235 CWOverrideRedirect | CWBackPixmap | CWEventMask, &twa);
237 grabbuttons(c, False);
238 settags(c, getclient(trans));
239 if(!c->isfloat)
240 c->isfloat = trans
241 || (c->maxw && c->minw &&
242 c->maxw == c->minw && c->maxh == c->minh);
244 if(clients)
245 clients->prev = c;
246 c->next = clients;
247 c->snext = stack;
248 stack = clients = c;
250 updatetitle(c);
251 ban(c);
252 XMapWindow(dpy, c->win);
253 XMapWindow(dpy, c->twin);
254 if(isvisible(c))
255 focus(c);
256 arrange(NULL);
257 }
259 void
260 resize(Client *c, Bool sizehints, Corner sticky) {
261 int bottom = c->y + c->h;
262 int right = c->x + c->w;
263 XWindowChanges wc;
265 if(sizehints) {
266 if(c->incw)
267 c->w -= (c->w - c->basew) % c->incw;
268 if(c->inch)
269 c->h -= (c->h - c->baseh) % c->inch;
270 if(c->minw && c->w < c->minw)
271 c->w = c->minw;
272 if(c->minh && c->h < c->minh)
273 c->h = c->minh;
274 if(c->maxw && c->w > c->maxw)
275 c->w = c->maxw;
276 if(c->maxh && c->h > c->maxh)
277 c->h = c->maxh;
278 }
279 if(sticky == TopRight || sticky == BotRight)
280 c->x = right - c->w;
281 if(sticky == BotLeft || sticky == BotRight)
282 c->y = bottom - c->h;
284 /* offscreen appearance fixes */
285 if(c->x + c->w < 0)
286 c->x = 0;
287 if(c->y + c->h < bh)
288 c->y = bh;
289 if(c->x > sw)
290 c->x = sw - c->w;
291 if(c->y > sh)
292 c->y = sh - c->h;
294 resizetitle(c);
295 wc.x = c->x;
296 wc.y = c->y;
297 wc.width = c->w;
298 wc.height = c->h;
299 if(c->w == sw && c->h == sh)
300 wc.border_width = 0;
301 else
302 wc.border_width = 1;
303 XConfigureWindow(dpy, c->win, CWX | CWY | CWWidth | CWHeight | CWBorderWidth, &wc);
304 XSync(dpy, False);
305 }
307 void
308 updatesize(Client *c) {
309 long msize;
310 XSizeHints size;
312 if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
313 size.flags = PSize;
314 c->flags = size.flags;
315 if(c->flags & PBaseSize) {
316 c->basew = size.base_width;
317 c->baseh = size.base_height;
318 }
319 else
320 c->basew = c->baseh = 0;
321 if(c->flags & PResizeInc) {
322 c->incw = size.width_inc;
323 c->inch = size.height_inc;
324 }
325 else
326 c->incw = c->inch = 0;
327 if(c->flags & PMaxSize) {
328 c->maxw = size.max_width;
329 c->maxh = size.max_height;
330 }
331 else
332 c->maxw = c->maxh = 0;
333 if(c->flags & PMinSize) {
334 c->minw = size.min_width;
335 c->minh = size.min_height;
336 }
337 else
338 c->minw = c->minh = 0;
339 if(c->flags & PWinGravity)
340 c->grav = size.win_gravity;
341 else
342 c->grav = NorthWestGravity;
343 }
345 void
346 updatetitle(Client *c) {
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 int ox, oy, ow, oh;
375 Client *c;
376 XEvent ev;
378 if(!sel)
379 return;
381 if((maximized = !maximized)) {
382 ox = sel->x;
383 oy = sel->y;
384 ow = sel->w;
385 oh = sel->h;
386 sel->x = sx;
387 sel->y = sy + bh;
388 sel->w = sw - 2;
389 sel->h = sh - 2 - bh;
391 restack();
392 for(c = getnext(clients); c; c = getnext(c->next))
393 if(c != sel)
394 ban(c);
395 resize(sel, arrange == dofloat, TopLeft);
397 sel->x = ox;
398 sel->y = oy;
399 sel->w = ow;
400 sel->h = oh;
401 }
402 else
403 arrange(NULL);
404 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
405 }
407 void
408 unmanage(Client *c) {
409 Client *nc;
411 XGrabServer(dpy);
412 XSetErrorHandler(xerrordummy);
414 detach(c);
415 detachstack(c);
416 if(sel == c) {
417 for(nc = stack; nc && !isvisible(nc); nc = nc->snext);
418 focus(nc);
419 }
421 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
422 XDestroyWindow(dpy, c->twin);
424 free(c->tags);
425 free(c);
427 XSync(dpy, False);
428 XSetErrorHandler(xerror);
429 XUngrabServer(dpy);
430 arrange(NULL);
431 }