aewl
view client.c @ 713:ef2c267884f3
removed a blank line
author | Anselm R. Garbe <arg@suckless.org> |
---|---|
date | Tue, 23 Jan 2007 12:04:22 +0100 |
parents | b288b57d81f8 |
children | 7034ee0f48d6 |
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 sel = c;
89 if(!issel)
90 return;
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
100 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
101 drawstatus();
102 }
104 Client *
105 getclient(Window w) {
106 Client *c;
108 for(c = clients; c; c = c->next)
109 if(c->win == w)
110 return c;
111 return NULL;
112 }
114 void
115 killclient(Arg *arg) {
116 if(!sel)
117 return;
118 if(sel->proto & PROTODELWIN)
119 sendevent(sel->win, wmatom[WMProtocols], wmatom[WMDelete]);
120 else
121 XKillClient(dpy, sel->win);
122 }
124 void
125 manage(Window w, XWindowAttributes *wa) {
126 Client *c;
127 Window trans;
129 c = emallocz(sizeof(Client));
130 c->tags = emallocz(ntags * sizeof(Bool));
131 c->win = w;
132 c->x = wa->x;
133 c->y = wa->y;
134 c->w = wa->width;
135 c->h = wa->height;
136 if(c->w == sw && c->h == sh) {
137 c->border = 0;
138 c->x = sx;
139 c->y = sy;
140 }
141 else {
142 c->border = BORDERPX;
143 if(c->x < wax)
144 c->x = wax;
145 if(c->y < way)
146 c->y = way;
147 if(c->x + c->w + 2 * c->border > wax + waw)
148 c->x = wax + waw - c->w - 2 * c->border;
149 if(c->y + c->h + 2 * c->border > way + wah)
150 c->y = way + wah - c->h - 2 * c->border;
151 }
152 updatesizehints(c);
153 c->proto = getproto(c->win);
154 XSelectInput(dpy, c->win,
155 StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
156 XGetTransientForHint(dpy, c->win, &trans);
157 grabbuttons(c, False);
158 updatetitle(c);
159 settags(c, getclient(trans));
160 if(!c->isfloat)
161 c->isfloat = trans || c->isfixed;
162 if(clients)
163 clients->prev = c;
164 c->next = clients;
165 c->snext = stack;
166 stack = clients = c;
167 XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
168 XMapWindow(dpy, c->win);
169 if(isvisible(c))
170 focus(c);
171 arrange();
172 }
174 void
175 resize(Client *c, Bool sizehints) {
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(c->w == sw && c->h == sh)
193 c->border = 0;
194 else
195 c->border = BORDERPX;
196 /* offscreen appearance fixes */
197 if(c->x + c->w + 2 * c->border < sx)
198 c->x = sx;
199 if(c->y + c->h + 2 * c->border < sy)
200 c->y = sy;
201 if(c->x > sw)
202 c->x = sw - c->w - 2 * c->border;
203 if(c->y > sh)
204 c->y = sh - c->h - 2 * c->border;
205 wc.x = c->x;
206 wc.y = c->y;
207 wc.width = c->w;
208 wc.height = c->h;
209 wc.border_width = c->border;
210 XConfigureWindow(dpy, c->win, CWX | CWY | CWWidth | CWHeight | CWBorderWidth, &wc);
211 configure(c);
212 XSync(dpy, False);
213 }
215 void
216 updatesizehints(Client *c) {
217 long msize;
218 XSizeHints size;
220 if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
221 size.flags = PSize;
222 c->flags = size.flags;
223 if(c->flags & PBaseSize) {
224 c->basew = size.base_width;
225 c->baseh = size.base_height;
226 }
227 else
228 c->basew = c->baseh = 0;
229 if(c->flags & PResizeInc) {
230 c->incw = size.width_inc;
231 c->inch = size.height_inc;
232 }
233 else
234 c->incw = c->inch = 0;
235 if(c->flags & PMaxSize) {
236 c->maxw = size.max_width;
237 c->maxh = size.max_height;
238 }
239 else
240 c->maxw = c->maxh = 0;
241 if(c->flags & PMinSize) {
242 c->minw = size.min_width;
243 c->minh = size.min_height;
244 }
245 else
246 c->minw = c->minh = 0;
247 c->isfixed = (c->maxw && c->minw && c->maxh && c->minh &&
248 c->maxw == c->minw && c->maxh == c->minh);
249 }
251 void
252 updatetitle(Client *c) {
253 char **list = NULL;
254 int n;
255 XTextProperty name;
257 name.nitems = 0;
258 c->name[0] = 0;
259 XGetTextProperty(dpy, c->win, &name, netatom[NetWMName]);
260 if(!name.nitems)
261 XGetWMName(dpy, c->win, &name);
262 if(!name.nitems)
263 return;
264 if(name.encoding == XA_STRING)
265 strncpy(c->name, (char *)name.value, sizeof c->name);
266 else {
267 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
268 && n > 0 && *list)
269 {
270 strncpy(c->name, *list, sizeof c->name);
271 XFreeStringList(list);
272 }
273 }
274 XFree(name.value);
275 }
277 void
278 unmanage(Client *c) {
279 Client *nc;
281 /* The server grab construct avoids race conditions. */
282 XGrabServer(dpy);
283 XSetErrorHandler(xerrordummy);
284 detach(c);
285 detachstack(c);
286 if(sel == c) {
287 for(nc = stack; nc && !isvisible(nc); nc = nc->snext);
288 focus(nc);
289 }
290 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
291 free(c->tags);
292 free(c);
293 XSync(dpy, False);
294 XSetErrorHandler(xerror);
295 XUngrabServer(dpy);
296 arrange();
297 }