dwm-meillo

view client.c @ 701:fc5b982778df

applied a modified version of Christof Musik's multihead patch (though this is not sure if it works in all cases, have to wait for an ACK by Christof)
author Anselm R. Garbe <arg@suckless.org>
date Thu, 18 Jan 2007 11:11:40 +0100
parents a76799907854
children a2d568a5cdb8
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 Client *old;
84 if(!issel || (c && !isvisible(c)))
85 return;
86 if(!sel)
87 sel = c;
88 else if(sel != c) {
89 old = sel;
90 sel = c;
91 if(old) {
92 grabbuttons(old, False);
93 XSetWindowBorder(dpy, old->win, dc.norm[ColBorder]);
94 }
95 }
96 if(c) {
97 detachstack(c);
98 c->snext = stack;
99 stack = c;
100 grabbuttons(c, True);
101 XSetWindowBorder(dpy, c->win, dc.sel[ColBorder]);
102 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
103 }
104 else
105 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
106 drawstatus();
107 }
109 Client *
110 getclient(Window w) {
111 Client *c;
113 for(c = clients; c; c = c->next)
114 if(c->win == w)
115 return c;
116 return NULL;
117 }
119 void
120 killclient(Arg *arg) {
121 if(!sel)
122 return;
123 if(sel->proto & PROTODELWIN)
124 sendevent(sel->win, wmatom[WMProtocols], wmatom[WMDelete]);
125 else
126 XKillClient(dpy, sel->win);
127 }
129 void
130 manage(Window w, XWindowAttributes *wa) {
131 Client *c;
132 Window trans;
134 c = emallocz(sizeof(Client));
135 c->tags = emallocz(ntags * sizeof(Bool));
136 c->win = w;
137 c->border = 0;
138 c->x = wa->x;
139 c->y = wa->y;
140 c->w = wa->width;
141 c->h = wa->height;
142 updatesizehints(c);
143 if(c->x + c->w + 2 * BORDERPX > sw)
144 c->x = sw - c->w - 2 * BORDERPX;
145 if(c->x < sx)
146 c->x = sx;
147 if(c->y + c->h + 2 * BORDERPX > sh)
148 c->y = sh - c->h - 2 * BORDERPX;
149 if(c->h != sh && c->y < bh)
150 c->y = bh;
151 c->proto = getproto(c->win);
152 XSelectInput(dpy, c->win,
153 StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
154 XGetTransientForHint(dpy, c->win, &trans);
155 grabbuttons(c, False);
156 updatetitle(c);
157 settags(c, getclient(trans));
158 if(!c->isfloat)
159 c->isfloat = trans || c->isfixed;
160 if(clients)
161 clients->prev = c;
162 c->next = clients;
163 c->snext = stack;
164 stack = clients = c;
165 XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
166 XMapWindow(dpy, c->win);
167 if(isvisible(c))
168 focus(c);
169 arrange();
170 }
172 void
173 resize(Client *c, Bool sizehints, Corner sticky) {
174 int bottom = c->y + c->h;
175 int right = c->x + c->w;
176 XWindowChanges wc;
178 if(sizehints) {
179 if(c->incw)
180 c->w -= (c->w - c->basew) % c->incw;
181 if(c->inch)
182 c->h -= (c->h - c->baseh) % c->inch;
183 if(c->minw && c->w < c->minw)
184 c->w = c->minw;
185 if(c->minh && c->h < c->minh)
186 c->h = c->minh;
187 if(c->maxw && c->w > c->maxw)
188 c->w = c->maxw;
189 if(c->maxh && c->h > c->maxh)
190 c->h = c->maxh;
191 }
192 if(sticky == TopRight || sticky == BotRight)
193 c->x = right - c->w;
194 if(sticky == BotLeft || sticky == BotRight)
195 c->y = bottom - c->h;
196 /* offscreen appearance fixes */
197 if(c->x + c->w < sx)
198 c->x = sx;
199 if(c->y + c->h < bh)
200 c->y = bh;
201 if(c->x > sw)
202 c->x = sw - c->w;
203 if(c->y > sh)
204 c->y = sh - c->h;
205 wc.x = c->x;
206 wc.y = c->y;
207 wc.width = c->w;
208 wc.height = c->h;
209 if(c->w == sw && c->h == sh)
210 wc.border_width = 0;
211 else
212 wc.border_width = BORDERPX;
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 }