aewl
view client.c @ 711:b40134b93de3
I think this is the best solution of multihead support
author | Anselm R. Garbe <arg@suckless.org> |
---|---|
date | Tue, 23 Jan 2007 11:49:16 +0100 |
parents | 6c2fcf88dd9f |
children | b288b57d81f8 |
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 = sel;
84 if(c && !isvisible(c))
85 return;
87 if(old && old != c) {
88 grabbuttons(old, False);
89 XSetWindowBorder(dpy, old->win, dc.norm[ColBorder]);
90 }
91 if(c) {
92 detachstack(c);
93 c->snext = stack;
94 stack = c;
95 grabbuttons(c, True);
96 XSetWindowBorder(dpy, c->win, dc.sel[ColBorder]);
97 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
98 }
99 else if(issel)
100 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
101 sel = c;
102 drawstatus();
103 }
105 Client *
106 getclient(Window w) {
107 Client *c;
109 for(c = clients; c; c = c->next)
110 if(c->win == w)
111 return c;
112 return NULL;
113 }
115 void
116 killclient(Arg *arg) {
117 if(!sel)
118 return;
119 if(sel->proto & PROTODELWIN)
120 sendevent(sel->win, wmatom[WMProtocols], wmatom[WMDelete]);
121 else
122 XKillClient(dpy, sel->win);
123 }
125 void
126 manage(Window w, XWindowAttributes *wa) {
127 Client *c;
128 Window trans;
130 c = emallocz(sizeof(Client));
131 c->tags = emallocz(ntags * sizeof(Bool));
132 c->win = w;
133 c->x = wa->x;
134 c->y = wa->y;
135 c->w = wa->width;
136 c->h = wa->height;
137 if(c->w == sw && c->h == sh) {
138 c->border = 0;
139 c->x = sx;
140 c->y = sy;
141 }
142 else {
143 c->border = BORDERPX;
144 if(c->x < wax)
145 c->x = wax;
146 if(c->y < way)
147 c->y = way;
148 if(c->x + c->w + 2 * c->border > wax + waw)
149 c->x = wax + waw - c->w - 2 * c->border;
150 if(c->y + c->h + 2 * c->border > way + wah)
151 c->y = way + wah - c->h - 2 * c->border;
152 }
153 updatesizehints(c);
154 c->proto = getproto(c->win);
155 XSelectInput(dpy, c->win,
156 StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
157 XGetTransientForHint(dpy, c->win, &trans);
158 grabbuttons(c, False);
159 updatetitle(c);
160 settags(c, getclient(trans));
161 if(!c->isfloat)
162 c->isfloat = trans || c->isfixed;
163 if(clients)
164 clients->prev = c;
165 c->next = clients;
166 c->snext = stack;
167 stack = clients = c;
168 XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
169 XMapWindow(dpy, c->win);
170 if(isvisible(c))
171 focus(c);
172 arrange();
173 }
175 void
176 resize(Client *c, Bool sizehints) {
177 XWindowChanges wc;
179 if(sizehints) {
180 if(c->incw)
181 c->w -= (c->w - c->basew) % c->incw;
182 if(c->inch)
183 c->h -= (c->h - c->baseh) % c->inch;
184 if(c->minw && c->w < c->minw)
185 c->w = c->minw;
186 if(c->minh && c->h < c->minh)
187 c->h = c->minh;
188 if(c->maxw && c->w > c->maxw)
189 c->w = c->maxw;
190 if(c->maxh && c->h > c->maxh)
191 c->h = c->maxh;
192 }
193 if(c->w == sw && c->h == sh)
194 c->border = 0;
195 else
196 c->border = BORDERPX;
197 /* offscreen appearance fixes */
198 if(c->x + c->w + 2 * c->border < sx)
199 c->x = sx;
200 if(c->y + c->h + 2 * c->border < sy)
201 c->y = sy;
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 wc.x = c->x;
207 wc.y = c->y;
208 wc.width = c->w;
209 wc.height = c->h;
210 wc.border_width = c->border;
211 XConfigureWindow(dpy, c->win, CWX | CWY | CWWidth | CWHeight | CWBorderWidth, &wc);
212 configure(c);
213 XSync(dpy, False);
214 }
216 void
217 updatesizehints(Client *c) {
218 long msize;
219 XSizeHints size;
221 if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
222 size.flags = PSize;
223 c->flags = size.flags;
224 if(c->flags & PBaseSize) {
225 c->basew = size.base_width;
226 c->baseh = size.base_height;
227 }
228 else
229 c->basew = c->baseh = 0;
230 if(c->flags & PResizeInc) {
231 c->incw = size.width_inc;
232 c->inch = size.height_inc;
233 }
234 else
235 c->incw = c->inch = 0;
236 if(c->flags & PMaxSize) {
237 c->maxw = size.max_width;
238 c->maxh = size.max_height;
239 }
240 else
241 c->maxw = c->maxh = 0;
242 if(c->flags & PMinSize) {
243 c->minw = size.min_width;
244 c->minh = size.min_height;
245 }
246 else
247 c->minw = c->minh = 0;
248 c->isfixed = (c->maxw && c->minw && c->maxh && c->minh &&
249 c->maxw == c->minw && c->maxh == c->minh);
250 }
252 void
253 updatetitle(Client *c) {
254 char **list = NULL;
255 int n;
256 XTextProperty name;
258 name.nitems = 0;
259 c->name[0] = 0;
260 XGetTextProperty(dpy, c->win, &name, netatom[NetWMName]);
261 if(!name.nitems)
262 XGetWMName(dpy, c->win, &name);
263 if(!name.nitems)
264 return;
265 if(name.encoding == XA_STRING)
266 strncpy(c->name, (char *)name.value, sizeof c->name);
267 else {
268 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
269 && n > 0 && *list)
270 {
271 strncpy(c->name, *list, sizeof c->name);
272 XFreeStringList(list);
273 }
274 }
275 XFree(name.value);
276 }
278 void
279 unmanage(Client *c) {
280 Client *nc;
282 /* The server grab construct avoids race conditions. */
283 XGrabServer(dpy);
284 XSetErrorHandler(xerrordummy);
285 detach(c);
286 detachstack(c);
287 if(sel == c) {
288 for(nc = stack; nc && !isvisible(nc); nc = nc->snext);
289 focus(nc);
290 }
291 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
292 free(c->tags);
293 free(c);
294 XSync(dpy, False);
295 XSetErrorHandler(xerror);
296 XUngrabServer(dpy);
297 arrange();
298 }