aewl

view client.c @ 715:5b3e4cdb6674

implemented Sanders remarks
author Anselm R. Garbe <arg@suckless.org>
date Tue, 23 Jan 2007 13:10:35 +0100
parents 7034ee0f48d6
children 4ce65f61f01b
line source
1 /* (C)opyright MMVI-MMVII 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 configure(Client *c) {
65 XEvent synev;
67 synev.type = ConfigureNotify;
68 synev.xconfigure.display = dpy;
69 synev.xconfigure.event = c->win;
70 synev.xconfigure.window = c->win;
71 synev.xconfigure.x = c->x;
72 synev.xconfigure.y = c->y;
73 synev.xconfigure.width = c->w;
74 synev.xconfigure.height = c->h;
75 synev.xconfigure.border_width = c->border;
76 synev.xconfigure.above = None;
77 XSendEvent(dpy, c->win, True, NoEventMask, &synev);
78 }
80 void
81 focus(Client *c) {
82 if(c && !isvisible(c))
83 return;
84 if(sel && sel != c) {
85 grabbuttons(sel, False);
86 XSetWindowBorder(dpy, sel->win, dc.norm[ColBorder]);
87 }
88 if(c) {
89 detachstack(c);
90 c->snext = stack;
91 stack = c;
92 grabbuttons(c, True);
93 }
94 sel = c;
95 drawstatus();
96 if(!activescreen)
97 return;
98 if(c) {
99 XSetWindowBorder(dpy, c->win, dc.sel[ColBorder]);
100 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
101 }
102 else
103 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
104 }
106 Client *
107 getclient(Window w) {
108 Client *c;
110 for(c = clients; c; c = c->next)
111 if(c->win == w)
112 return c;
113 return NULL;
114 }
116 void
117 killclient(Arg *arg) {
118 if(!sel)
119 return;
120 if(sel->proto & PROTODELWIN)
121 sendevent(sel->win, wmatom[WMProtocols], wmatom[WMDelete]);
122 else
123 XKillClient(dpy, sel->win);
124 }
126 void
127 manage(Window w, XWindowAttributes *wa) {
128 Client *c;
129 Window trans;
131 c = emallocz(sizeof(Client));
132 c->tags = emallocz(ntags * sizeof(Bool));
133 c->win = w;
134 c->x = wa->x;
135 c->y = wa->y;
136 c->w = wa->width;
137 c->h = wa->height;
138 if(c->w == sw && c->h == sh) {
139 c->border = 0;
140 c->x = sx;
141 c->y = sy;
142 }
143 else {
144 c->border = BORDERPX;
145 if(c->x < wax)
146 c->x = wax;
147 if(c->y < way)
148 c->y = way;
149 if(c->x + c->w + 2 * c->border > wax + waw)
150 c->x = wax + waw - c->w - 2 * c->border;
151 if(c->y + c->h + 2 * c->border > way + wah)
152 c->y = way + wah - c->h - 2 * c->border;
153 }
154 updatesizehints(c);
155 c->proto = getproto(c->win);
156 XSelectInput(dpy, c->win,
157 StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
158 XGetTransientForHint(dpy, c->win, &trans);
159 grabbuttons(c, False);
160 XSetWindowBorder(dpy, c->win, dc.norm[ColBorder]);
161 updatetitle(c);
162 settags(c, getclient(trans));
163 if(!c->isfloat)
164 c->isfloat = trans || c->isfixed;
165 if(clients)
166 clients->prev = c;
167 c->next = clients;
168 c->snext = stack;
169 stack = clients = c;
170 XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
171 XMapWindow(dpy, c->win);
172 if(isvisible(c))
173 focus(c);
174 arrange();
175 }
177 void
178 resize(Client *c, Bool sizehints) {
179 XWindowChanges wc;
181 if(sizehints) {
182 if(c->incw)
183 c->w -= (c->w - c->basew) % c->incw;
184 if(c->inch)
185 c->h -= (c->h - c->baseh) % c->inch;
186 if(c->minw && c->w < c->minw)
187 c->w = c->minw;
188 if(c->minh && c->h < c->minh)
189 c->h = c->minh;
190 if(c->maxw && c->w > c->maxw)
191 c->w = c->maxw;
192 if(c->maxh && c->h > c->maxh)
193 c->h = c->maxh;
194 }
195 if(c->w == sw && c->h == sh)
196 c->border = 0;
197 else
198 c->border = BORDERPX;
199 /* offscreen appearance fixes */
200 if(c->x + c->w + 2 * c->border < sx)
201 c->x = sx;
202 if(c->y + c->h + 2 * c->border < sy)
203 c->y = sy;
204 if(c->x > sw)
205 c->x = sw - c->w - 2 * c->border;
206 if(c->y > sh)
207 c->y = sh - c->h - 2 * c->border;
208 wc.x = c->x;
209 wc.y = c->y;
210 wc.width = c->w;
211 wc.height = c->h;
212 wc.border_width = c->border;
213 XConfigureWindow(dpy, c->win, CWX | CWY | CWWidth | CWHeight | CWBorderWidth, &wc);
214 configure(c);
215 XSync(dpy, False);
216 }
218 void
219 updatesizehints(Client *c) {
220 long msize;
221 XSizeHints size;
223 if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
224 size.flags = PSize;
225 c->flags = size.flags;
226 if(c->flags & PBaseSize) {
227 c->basew = size.base_width;
228 c->baseh = size.base_height;
229 }
230 else
231 c->basew = c->baseh = 0;
232 if(c->flags & PResizeInc) {
233 c->incw = size.width_inc;
234 c->inch = size.height_inc;
235 }
236 else
237 c->incw = c->inch = 0;
238 if(c->flags & PMaxSize) {
239 c->maxw = size.max_width;
240 c->maxh = size.max_height;
241 }
242 else
243 c->maxw = c->maxh = 0;
244 if(c->flags & PMinSize) {
245 c->minw = size.min_width;
246 c->minh = size.min_height;
247 }
248 else
249 c->minw = c->minh = 0;
250 c->isfixed = (c->maxw && c->minw && c->maxh && c->minh &&
251 c->maxw == c->minw && c->maxh == c->minh);
252 }
254 void
255 updatetitle(Client *c) {
256 char **list = NULL;
257 int n;
258 XTextProperty name;
260 name.nitems = 0;
261 c->name[0] = 0;
262 XGetTextProperty(dpy, c->win, &name, netatom[NetWMName]);
263 if(!name.nitems)
264 XGetWMName(dpy, c->win, &name);
265 if(!name.nitems)
266 return;
267 if(name.encoding == XA_STRING)
268 strncpy(c->name, (char *)name.value, sizeof c->name);
269 else {
270 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
271 && n > 0 && *list)
272 {
273 strncpy(c->name, *list, sizeof c->name);
274 XFreeStringList(list);
275 }
276 }
277 XFree(name.value);
278 }
280 void
281 unmanage(Client *c) {
282 Client *nc;
284 /* The server grab construct avoids race conditions. */
285 XGrabServer(dpy);
286 XSetErrorHandler(xerrordummy);
287 detach(c);
288 detachstack(c);
289 if(sel == c) {
290 for(nc = stack; nc && !isvisible(nc); nc = nc->snext);
291 focus(nc);
292 }
293 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
294 free(c->tags);
295 free(c);
296 XSync(dpy, False);
297 XSetErrorHandler(xerror);
298 XUngrabServer(dpy);
299 arrange();
300 }