aewl

view client.c @ 730:8997e28553a8

made some changes more concistent
author Anselm R. Garbe <arg@suckless.org>
date Mon, 05 Feb 2007 11:19:46 +0100
parents d99be681d502
children 29c9b557ed95
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 */
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 void
57 setclientstate(Client *c, long state) {
58 long data[] = {state, None};
59 XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
60 PropModeReplace, (unsigned char *)data, 2);
61 }
63 static int
64 xerrordummy(Display *dsply, XErrorEvent *ee) {
65 return 0;
66 }
68 /* extern */
70 void
71 configure(Client *c) {
72 XEvent synev;
74 synev.type = ConfigureNotify;
75 synev.xconfigure.display = dpy;
76 synev.xconfigure.event = c->win;
77 synev.xconfigure.window = c->win;
78 synev.xconfigure.x = c->x;
79 synev.xconfigure.y = c->y;
80 synev.xconfigure.width = c->w;
81 synev.xconfigure.height = c->h;
82 synev.xconfigure.border_width = c->border;
83 synev.xconfigure.above = None;
84 XSendEvent(dpy, c->win, True, NoEventMask, &synev);
85 }
87 void
88 focus(Client *c) {
89 if(c && !isvisible(c))
90 return;
91 if(sel && sel != c) {
92 grabbuttons(sel, False);
93 XSetWindowBorder(dpy, sel->win, dc.norm[ColBorder]);
94 }
95 if(c) {
96 detachstack(c);
97 c->snext = stack;
98 stack = c;
99 grabbuttons(c, True);
100 }
101 sel = c;
102 drawstatus();
103 if(!selscreen)
104 return;
105 if(c) {
106 XSetWindowBorder(dpy, c->win, dc.sel[ColBorder]);
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 void
124 killclient(Arg *arg) {
125 if(!sel)
126 return;
127 if(sel->proto & PROTODELWIN)
128 sendevent(sel->win, wmatom[WMProtocols], wmatom[WMDelete]);
129 else
130 XKillClient(dpy, sel->win);
131 }
133 void
134 manage(Window w, XWindowAttributes *wa) {
135 Client *c;
136 Window trans;
138 c = emallocz(sizeof(Client));
139 c->tags = emallocz(ntags * sizeof(Bool));
140 c->win = w;
141 c->x = wa->x;
142 c->y = wa->y;
143 c->w = wa->width;
144 c->h = wa->height;
145 if(c->w == sw && c->h == sh) {
146 c->border = 0;
147 c->x = sx;
148 c->y = sy;
149 }
150 else {
151 c->border = BORDERPX;
152 if(c->x + c->w + 2 * c->border > wax + waw)
153 c->x = wax + waw - c->w - 2 * c->border;
154 if(c->y + c->h + 2 * c->border > way + wah)
155 c->y = way + wah - c->h - 2 * c->border;
156 if(c->x < wax)
157 c->x = wax;
158 if(c->y < way)
159 c->y = way;
160 }
161 updatesizehints(c);
162 c->proto = getproto(c->win);
163 XSelectInput(dpy, c->win,
164 StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
165 XGetTransientForHint(dpy, c->win, &trans);
166 grabbuttons(c, False);
167 XSetWindowBorder(dpy, c->win, dc.norm[ColBorder]);
168 updatetitle(c);
169 settags(c, getclient(trans));
170 if(!c->isfloat)
171 c->isfloat = trans || c->isfixed;
172 if(clients)
173 clients->prev = c;
174 c->next = clients;
175 c->snext = stack;
176 stack = clients = c;
177 XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
178 XMapWindow(dpy, c->win);
179 setclientstate(c, NormalState);
180 if(isvisible(c))
181 focus(c);
182 arrange();
183 }
185 void
186 resize(Client *c, Bool sizehints) {
187 XWindowChanges wc;
189 if(c->w <= 0 || c->h <= 0)
190 return;
191 if(sizehints) {
192 if(c->incw)
193 c->w -= (c->w - c->basew) % c->incw;
194 if(c->inch)
195 c->h -= (c->h - c->baseh) % c->inch;
196 if(c->minw && c->w < c->minw)
197 c->w = c->minw;
198 if(c->minh && c->h < c->minh)
199 c->h = c->minh;
200 if(c->maxw && c->w > c->maxw)
201 c->w = c->maxw;
202 if(c->maxh && c->h > c->maxh)
203 c->h = c->maxh;
204 }
205 if(c->w == sw && c->h == sh)
206 c->border = 0;
207 else
208 c->border = BORDERPX;
209 /* offscreen appearance fixes */
210 if(c->x > sw)
211 c->x = sw - c->w - 2 * c->border;
212 if(c->y > sh)
213 c->y = sh - c->h - 2 * c->border;
214 if(c->x + c->w + 2 * c->border < sx)
215 c->x = sx;
216 if(c->y + c->h + 2 * c->border < sy)
217 c->y = sy;
218 wc.x = c->x;
219 wc.y = c->y;
220 wc.width = c->w;
221 wc.height = c->h;
222 wc.border_width = c->border;
223 XConfigureWindow(dpy, c->win, CWX | CWY | CWWidth | CWHeight | CWBorderWidth, &wc);
224 configure(c);
225 XSync(dpy, False);
226 }
228 void
229 updatesizehints(Client *c) {
230 long msize;
231 XSizeHints size;
233 if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
234 size.flags = PSize;
235 c->flags = size.flags;
236 if(c->flags & PBaseSize) {
237 c->basew = size.base_width;
238 c->baseh = size.base_height;
239 }
240 else
241 c->basew = c->baseh = 0;
242 if(c->flags & PResizeInc) {
243 c->incw = size.width_inc;
244 c->inch = size.height_inc;
245 }
246 else
247 c->incw = c->inch = 0;
248 if(c->flags & PMaxSize) {
249 c->maxw = size.max_width;
250 c->maxh = size.max_height;
251 }
252 else
253 c->maxw = c->maxh = 0;
254 if(c->flags & PMinSize) {
255 c->minw = size.min_width;
256 c->minh = size.min_height;
257 }
258 else
259 c->minw = c->minh = 0;
260 c->isfixed = (c->maxw && c->minw && c->maxh && c->minh &&
261 c->maxw == c->minw && c->maxh == c->minh);
262 }
264 void
265 updatetitle(Client *c) {
266 char **list = NULL;
267 int n;
268 XTextProperty name;
270 name.nitems = 0;
271 c->name[0] = 0;
272 XGetTextProperty(dpy, c->win, &name, netatom[NetWMName]);
273 if(!name.nitems)
274 XGetWMName(dpy, c->win, &name);
275 if(!name.nitems)
276 return;
277 if(name.encoding == XA_STRING)
278 strncpy(c->name, (char *)name.value, sizeof c->name);
279 else {
280 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
281 && n > 0 && *list)
282 {
283 strncpy(c->name, *list, sizeof c->name);
284 XFreeStringList(list);
285 }
286 }
287 XFree(name.value);
288 }
290 void
291 unmanage(Client *c) {
292 Client *nc;
294 /* The server grab construct avoids race conditions. */
295 XGrabServer(dpy);
296 XSetErrorHandler(xerrordummy);
297 detach(c);
298 detachstack(c);
299 if(sel == c) {
300 for(nc = stack; nc && !isvisible(nc); nc = nc->snext);
301 focus(nc);
302 }
303 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
304 setclientstate(c, WithdrawnState);
305 free(c->tags);
306 free(c);
307 XSync(dpy, False);
308 XSetErrorHandler(xerror);
309 XUngrabServer(dpy);
310 arrange();
311 }