dwm-meillo

view client.c @ 721:d3876aa79292

prepared yet another hotfix release
author Anselm R. Garbe <arg@suckless.org>
date Thu, 25 Jan 2007 09:59:48 +0100
parents 09452e717bfd
children d99be681d502
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(!selscreen)
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 + c->w + 2 * c->border > wax + waw)
146 c->x = wax + waw - c->w - 2 * c->border;
147 if(c->y + c->h + 2 * c->border > way + wah)
148 c->y = way + wah - c->h - 2 * c->border;
149 if(c->x < wax)
150 c->x = wax;
151 if(c->y < way)
152 c->y = way;
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(c->w <= 0 || c->h <= 0)
182 return;
183 if(sizehints) {
184 if(c->incw)
185 c->w -= (c->w - c->basew) % c->incw;
186 if(c->inch)
187 c->h -= (c->h - c->baseh) % c->inch;
188 if(c->minw && c->w < c->minw)
189 c->w = c->minw;
190 if(c->minh && c->h < c->minh)
191 c->h = c->minh;
192 if(c->maxw && c->w > c->maxw)
193 c->w = c->maxw;
194 if(c->maxh && c->h > c->maxh)
195 c->h = c->maxh;
196 }
197 if(c->w == sw && c->h == sh)
198 c->border = 0;
199 else
200 c->border = BORDERPX;
201 /* offscreen appearance fixes */
202 if(c->x > sw)
203 c->x = sw - c->w - 2 * c->border;
204 if(c->y > sh)
205 c->y = sh - c->h - 2 * c->border;
206 if(c->x + c->w + 2 * c->border < sx)
207 c->x = sx;
208 if(c->y + c->h + 2 * c->border < sy)
209 c->y = sy;
210 wc.x = c->x;
211 wc.y = c->y;
212 wc.width = c->w;
213 wc.height = c->h;
214 wc.border_width = c->border;
215 XConfigureWindow(dpy, c->win, CWX | CWY | CWWidth | CWHeight | CWBorderWidth, &wc);
216 configure(c);
217 XSync(dpy, False);
218 }
220 void
221 updatesizehints(Client *c) {
222 long msize;
223 XSizeHints size;
225 if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
226 size.flags = PSize;
227 c->flags = size.flags;
228 if(c->flags & PBaseSize) {
229 c->basew = size.base_width;
230 c->baseh = size.base_height;
231 }
232 else
233 c->basew = c->baseh = 0;
234 if(c->flags & PResizeInc) {
235 c->incw = size.width_inc;
236 c->inch = size.height_inc;
237 }
238 else
239 c->incw = c->inch = 0;
240 if(c->flags & PMaxSize) {
241 c->maxw = size.max_width;
242 c->maxh = size.max_height;
243 }
244 else
245 c->maxw = c->maxh = 0;
246 if(c->flags & PMinSize) {
247 c->minw = size.min_width;
248 c->minh = size.min_height;
249 }
250 else
251 c->minw = c->minh = 0;
252 c->isfixed = (c->maxw && c->minw && c->maxh && c->minh &&
253 c->maxw == c->minw && c->maxh == c->minh);
254 }
256 void
257 updatetitle(Client *c) {
258 char **list = NULL;
259 int n;
260 XTextProperty name;
262 name.nitems = 0;
263 c->name[0] = 0;
264 XGetTextProperty(dpy, c->win, &name, netatom[NetWMName]);
265 if(!name.nitems)
266 XGetWMName(dpy, c->win, &name);
267 if(!name.nitems)
268 return;
269 if(name.encoding == XA_STRING)
270 strncpy(c->name, (char *)name.value, sizeof c->name);
271 else {
272 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
273 && n > 0 && *list)
274 {
275 strncpy(c->name, *list, sizeof c->name);
276 XFreeStringList(list);
277 }
278 }
279 XFree(name.value);
280 }
282 void
283 unmanage(Client *c) {
284 Client *nc;
286 /* The server grab construct avoids race conditions. */
287 XGrabServer(dpy);
288 XSetErrorHandler(xerrordummy);
289 detach(c);
290 detachstack(c);
291 if(sel == c) {
292 for(nc = stack; nc && !isvisible(nc); nc = nc->snext);
293 focus(nc);
294 }
295 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
296 free(c->tags);
297 free(c);
298 XSync(dpy, False);
299 XSetErrorHandler(xerror);
300 XUngrabServer(dpy);
301 arrange();
302 }