dwm-meillo

view client.c @ 483:0db173d7e59c

man page fix
author arg@mmvi
date Fri, 22 Sep 2006 14:02:41 +0200
parents 2d8af0d7920d
children 12395ef46d97
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;
210 c->border = 0;
211 updatesize(c);
213 if(c->x + c->w + 2 > sw)
214 c->x = sw - c->w - 2;
215 if(c->x < 0)
216 c->x = 0;
217 if(c->y + c->h + 2 > sh)
218 c->y = sh - c->h - 2;
219 if(c->h != sh && c->y < bh)
220 c->y = bh;
222 c->proto = getproto(c->win);
223 XSelectInput(dpy, c->win,
224 StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
225 XGetTransientForHint(dpy, c->win, &trans);
226 twa.override_redirect = 1;
227 twa.background_pixmap = ParentRelative;
228 twa.event_mask = ExposureMask | EnterWindowMask;
230 c->twin = XCreateWindow(dpy, root, c->tx, c->ty, c->tw, c->th,
231 0, DefaultDepth(dpy, screen), CopyFromParent,
232 DefaultVisual(dpy, screen),
233 CWOverrideRedirect | CWBackPixmap | CWEventMask, &twa);
235 grabbuttons(c, False);
236 settags(c, getclient(trans));
237 if(!c->isfloat)
238 c->isfloat = trans
239 || (c->maxw && c->minw &&
240 c->maxw == c->minw && c->maxh == c->minh);
242 if(clients)
243 clients->prev = c;
244 c->next = clients;
245 c->snext = stack;
246 stack = clients = c;
248 updatetitle(c);
249 ban(c);
250 XMapWindow(dpy, c->win);
251 XMapWindow(dpy, c->twin);
252 if(isvisible(c))
253 focus(c);
254 arrange(NULL);
255 }
257 void
258 resize(Client *c, Bool sizehints, Corner sticky) {
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 /* offscreen appearance fixes */
283 if(c->x + c->w < 0)
284 c->x = 0;
285 if(c->y + c->h < bh)
286 c->y = bh;
287 if(c->x > sw)
288 c->x = sw - c->w;
289 if(c->y > sh)
290 c->y = sh - 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 updatesize(Client *c) {
307 long msize;
308 XSizeHints size;
310 if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
311 size.flags = PSize;
312 c->flags = size.flags;
313 if(c->flags & PBaseSize) {
314 c->basew = size.base_width;
315 c->baseh = size.base_height;
316 }
317 else
318 c->basew = c->baseh = 0;
319 if(c->flags & PResizeInc) {
320 c->incw = size.width_inc;
321 c->inch = size.height_inc;
322 }
323 else
324 c->incw = c->inch = 0;
325 if(c->flags & PMaxSize) {
326 c->maxw = size.max_width;
327 c->maxh = size.max_height;
328 }
329 else
330 c->maxw = c->maxh = 0;
331 if(c->flags & PMinSize) {
332 c->minw = size.min_width;
333 c->minh = size.min_height;
334 }
335 else
336 c->minw = c->minh = 0;
337 if(c->flags & PWinGravity)
338 c->grav = size.win_gravity;
339 else
340 c->grav = NorthWestGravity;
341 }
343 void
344 updatetitle(Client *c) {
345 char **list = NULL;
346 int n;
347 XTextProperty name;
349 name.nitems = 0;
350 c->name[0] = 0;
351 XGetTextProperty(dpy, c->win, &name, netatom[NetWMName]);
352 if(!name.nitems)
353 XGetWMName(dpy, c->win, &name);
354 if(!name.nitems)
355 return;
356 if(name.encoding == XA_STRING)
357 strncpy(c->name, (char *)name.value, sizeof(c->name));
358 else {
359 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
360 && n > 0 && *list)
361 {
362 strncpy(c->name, *list, sizeof(c->name));
363 XFreeStringList(list);
364 }
365 }
366 XFree(name.value);
367 resizetitle(c);
368 }
370 void
371 unmanage(Client *c) {
372 Client *nc;
374 /* The server grab construct avoids race conditions. */
375 XGrabServer(dpy);
376 XSetErrorHandler(xerrordummy);
378 detach(c);
379 detachstack(c);
380 if(sel == c) {
381 for(nc = stack; nc && !isvisible(nc); nc = nc->snext);
382 focus(nc);
383 }
385 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
386 XDestroyWindow(dpy, c->twin);
388 free(c->tags);
389 free(c);
391 XSync(dpy, False);
392 XSetErrorHandler(xerror);
393 XUngrabServer(dpy);
394 arrange(NULL);
395 }