rev |
line source |
arg@644
|
1 /* (C)opyright MMVI-MMVII Anselm R. Garbe <garbeam at gmail dot com>
|
garbeam@5
|
2 * See LICENSE file for license details.
|
garbeam@5
|
3 */
|
garbeam@76
|
4 #include "dwm.h"
|
garbeam@10
|
5 #include <stdlib.h>
|
garbeam@5
|
6 #include <string.h>
|
garbeam@5
|
7 #include <X11/Xatom.h>
|
garbeam@32
|
8 #include <X11/Xutil.h>
|
garbeam@5
|
9
|
garbeam@76
|
10 /* static functions */
|
garbeam@50
|
11
|
garbeam@26
|
12 static void
|
arg@461
|
13 detachstack(Client *c) {
|
arg@446
|
14 Client **tc;
|
arg@446
|
15 for(tc=&stack; *tc && *tc != c; tc=&(*tc)->snext);
|
arg@446
|
16 *tc = c->snext;
|
arg@446
|
17 }
|
arg@446
|
18
|
arg@446
|
19 static void
|
arg@461
|
20 grabbuttons(Client *c, Bool focused) {
|
arg@372
|
21 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
|
arg@372
|
22
|
arg@452
|
23 if(focused) {
|
arg@372
|
24 XGrabButton(dpy, Button1, MODKEY, c->win, False, BUTTONMASK,
|
arg@372
|
25 GrabModeAsync, GrabModeSync, None, None);
|
arg@372
|
26 XGrabButton(dpy, Button1, MODKEY | LockMask, c->win, False, BUTTONMASK,
|
arg@372
|
27 GrabModeAsync, GrabModeSync, None, None);
|
arg@372
|
28 XGrabButton(dpy, Button1, MODKEY | numlockmask, c->win, False, BUTTONMASK,
|
arg@372
|
29 GrabModeAsync, GrabModeSync, None, None);
|
arg@372
|
30 XGrabButton(dpy, Button1, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
|
arg@372
|
31 GrabModeAsync, GrabModeSync, None, None);
|
arg@372
|
32
|
arg@372
|
33 XGrabButton(dpy, Button2, MODKEY, c->win, False, BUTTONMASK,
|
arg@372
|
34 GrabModeAsync, GrabModeSync, None, None);
|
arg@372
|
35 XGrabButton(dpy, Button2, MODKEY | LockMask, c->win, False, BUTTONMASK,
|
arg@372
|
36 GrabModeAsync, GrabModeSync, None, None);
|
arg@372
|
37 XGrabButton(dpy, Button2, MODKEY | numlockmask, c->win, False, BUTTONMASK,
|
arg@372
|
38 GrabModeAsync, GrabModeSync, None, None);
|
arg@372
|
39 XGrabButton(dpy, Button2, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
|
arg@372
|
40 GrabModeAsync, GrabModeSync, None, None);
|
arg@372
|
41
|
arg@372
|
42 XGrabButton(dpy, Button3, MODKEY, c->win, False, BUTTONMASK,
|
arg@372
|
43 GrabModeAsync, GrabModeSync, None, None);
|
arg@372
|
44 XGrabButton(dpy, Button3, MODKEY | LockMask, c->win, False, BUTTONMASK,
|
arg@372
|
45 GrabModeAsync, GrabModeSync, None, None);
|
arg@372
|
46 XGrabButton(dpy, Button3, MODKEY | numlockmask, c->win, False, BUTTONMASK,
|
arg@372
|
47 GrabModeAsync, GrabModeSync, None, None);
|
arg@372
|
48 XGrabButton(dpy, Button3, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
|
arg@372
|
49 GrabModeAsync, GrabModeSync, None, None);
|
arg@372
|
50 }
|
arg@372
|
51 else
|
arg@372
|
52 XGrabButton(dpy, AnyButton, AnyModifier, c->win, False, BUTTONMASK,
|
arg@372
|
53 GrabModeAsync, GrabModeSync, None, None);
|
arg@318
|
54 }
|
arg@318
|
55
|
garbeam@76
|
56 static int
|
arg@461
|
57 xerrordummy(Display *dsply, XErrorEvent *ee) {
|
garbeam@76
|
58 return 0;
|
garbeam@5
|
59 }
|
garbeam@5
|
60
|
garbeam@76
|
61 /* extern functions */
|
garbeam@20
|
62
|
garbeam@20
|
63 void
|
arg@491
|
64 configure(Client *c) {
|
arg@491
|
65 XEvent synev;
|
arg@491
|
66
|
arg@491
|
67 synev.type = ConfigureNotify;
|
arg@491
|
68 synev.xconfigure.display = dpy;
|
arg@491
|
69 synev.xconfigure.event = c->win;
|
arg@491
|
70 synev.xconfigure.window = c->win;
|
arg@491
|
71 synev.xconfigure.x = c->x;
|
arg@491
|
72 synev.xconfigure.y = c->y;
|
arg@491
|
73 synev.xconfigure.width = c->w;
|
arg@491
|
74 synev.xconfigure.height = c->h;
|
arg@491
|
75 synev.xconfigure.border_width = c->border;
|
arg@491
|
76 synev.xconfigure.above = None;
|
arg@491
|
77 XSendEvent(dpy, c->win, True, NoEventMask, &synev);
|
arg@491
|
78 }
|
arg@491
|
79
|
arg@491
|
80 void
|
arg@461
|
81 focus(Client *c) {
|
arg@709
|
82 if(c && !isvisible(c))
|
arg@239
|
83 return;
|
arg@712
|
84 if(sel && sel != c) {
|
arg@712
|
85 grabbuttons(sel, False);
|
arg@712
|
86 XSetWindowBorder(dpy, sel->win, dc.norm[ColBorder]);
|
arg@318
|
87 }
|
arg@712
|
88 sel = c;
|
arg@712
|
89 if(!issel)
|
arg@712
|
90 return;
|
arg@400
|
91 if(c) {
|
arg@711
|
92 detachstack(c);
|
arg@711
|
93 c->snext = stack;
|
arg@711
|
94 stack = c;
|
arg@711
|
95 grabbuttons(c, True);
|
arg@711
|
96 XSetWindowBorder(dpy, c->win, dc.sel[ColBorder]);
|
arg@711
|
97 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
|
arg@400
|
98 }
|
arg@712
|
99 else
|
arg@400
|
100 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
|
arg@690
|
101 drawstatus();
|
garbeam@13
|
102 }
|
garbeam@13
|
103
|
garbeam@76
|
104 Client *
|
arg@461
|
105 getclient(Window w) {
|
garbeam@76
|
106 Client *c;
|
arg@123
|
107
|
garbeam@76
|
108 for(c = clients; c; c = c->next)
|
garbeam@76
|
109 if(c->win == w)
|
garbeam@76
|
110 return c;
|
garbeam@76
|
111 return NULL;
|
garbeam@76
|
112 }
|
garbeam@76
|
113
|
garbeam@76
|
114 void
|
arg@461
|
115 killclient(Arg *arg) {
|
garbeam@76
|
116 if(!sel)
|
garbeam@76
|
117 return;
|
arg@157
|
118 if(sel->proto & PROTODELWIN)
|
garbeam@77
|
119 sendevent(sel->win, wmatom[WMProtocols], wmatom[WMDelete]);
|
garbeam@76
|
120 else
|
garbeam@76
|
121 XKillClient(dpy, sel->win);
|
garbeam@76
|
122 }
|
garbeam@76
|
123
|
garbeam@76
|
124 void
|
arg@461
|
125 manage(Window w, XWindowAttributes *wa) {
|
arg@431
|
126 Client *c;
|
arg@123
|
127 Window trans;
|
garbeam@5
|
128
|
garbeam@5
|
129 c = emallocz(sizeof(Client));
|
arg@178
|
130 c->tags = emallocz(ntags * sizeof(Bool));
|
garbeam@5
|
131 c->win = w;
|
arg@687
|
132 c->x = wa->x;
|
arg@687
|
133 c->y = wa->y;
|
arg@687
|
134 c->w = wa->width;
|
arg@115
|
135 c->h = wa->height;
|
arg@708
|
136 if(c->w == sw && c->h == sh) {
|
arg@708
|
137 c->border = 0;
|
arg@708
|
138 c->x = sx;
|
arg@708
|
139 c->y = sy;
|
arg@708
|
140 }
|
arg@708
|
141 else {
|
arg@708
|
142 c->border = BORDERPX;
|
arg@708
|
143 if(c->x < wax)
|
arg@708
|
144 c->x = wax;
|
arg@708
|
145 if(c->y < way)
|
arg@708
|
146 c->y = way;
|
arg@708
|
147 if(c->x + c->w + 2 * c->border > wax + waw)
|
arg@708
|
148 c->x = wax + waw - c->w - 2 * c->border;
|
arg@708
|
149 if(c->y + c->h + 2 * c->border > way + wah)
|
arg@708
|
150 c->y = way + wah - c->h - 2 * c->border;
|
arg@708
|
151 }
|
arg@639
|
152 updatesizehints(c);
|
garbeam@75
|
153 c->proto = getproto(c->win);
|
garbeam@26
|
154 XSelectInput(dpy, c->win,
|
arg@127
|
155 StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
|
garbeam@53
|
156 XGetTransientForHint(dpy, c->win, &trans);
|
arg@372
|
157 grabbuttons(c, False);
|
arg@500
|
158 updatetitle(c);
|
arg@431
|
159 settags(c, getclient(trans));
|
garbeam@80
|
160 if(!c->isfloat)
|
arg@549
|
161 c->isfloat = trans || c->isfixed;
|
arg@381
|
162 if(clients)
|
arg@381
|
163 clients->prev = c;
|
arg@381
|
164 c->next = clients;
|
arg@446
|
165 c->snext = stack;
|
arg@446
|
166 stack = clients = c;
|
arg@687
|
167 XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
|
arg@270
|
168 XMapWindow(dpy, c->win);
|
arg@261
|
169 if(isvisible(c))
|
garbeam@51
|
170 focus(c);
|
arg@533
|
171 arrange();
|
garbeam@94
|
172 }
|
garbeam@94
|
173
|
garbeam@94
|
174 void
|
arg@708
|
175 resize(Client *c, Bool sizehints) {
|
arg@163
|
176 XWindowChanges wc;
|
garbeam@18
|
177
|
arg@129
|
178 if(sizehints) {
|
garbeam@52
|
179 if(c->incw)
|
arg@115
|
180 c->w -= (c->w - c->basew) % c->incw;
|
garbeam@52
|
181 if(c->inch)
|
arg@115
|
182 c->h -= (c->h - c->baseh) % c->inch;
|
arg@129
|
183 if(c->minw && c->w < c->minw)
|
arg@129
|
184 c->w = c->minw;
|
arg@129
|
185 if(c->minh && c->h < c->minh)
|
arg@129
|
186 c->h = c->minh;
|
arg@129
|
187 if(c->maxw && c->w > c->maxw)
|
arg@129
|
188 c->w = c->maxw;
|
arg@129
|
189 if(c->maxh && c->h > c->maxh)
|
arg@129
|
190 c->h = c->maxh;
|
garbeam@52
|
191 }
|
arg@708
|
192 if(c->w == sw && c->h == sh)
|
arg@708
|
193 c->border = 0;
|
arg@708
|
194 else
|
arg@708
|
195 c->border = BORDERPX;
|
arg@465
|
196 /* offscreen appearance fixes */
|
arg@708
|
197 if(c->x + c->w + 2 * c->border < sx)
|
arg@517
|
198 c->x = sx;
|
arg@708
|
199 if(c->y + c->h + 2 * c->border < sy)
|
arg@708
|
200 c->y = sy;
|
arg@465
|
201 if(c->x > sw)
|
arg@708
|
202 c->x = sw - c->w - 2 * c->border;
|
arg@465
|
203 if(c->y > sh)
|
arg@708
|
204 c->y = sh - c->h - 2 * c->border;
|
arg@163
|
205 wc.x = c->x;
|
arg@163
|
206 wc.y = c->y;
|
arg@163
|
207 wc.width = c->w;
|
arg@163
|
208 wc.height = c->h;
|
arg@708
|
209 wc.border_width = c->border;
|
arg@452
|
210 XConfigureWindow(dpy, c->win, CWX | CWY | CWWidth | CWHeight | CWBorderWidth, &wc);
|
arg@630
|
211 configure(c);
|
garbeam@79
|
212 XSync(dpy, False);
|
garbeam@18
|
213 }
|
garbeam@18
|
214
|
garbeam@76
|
215 void
|
arg@639
|
216 updatesizehints(Client *c) {
|
arg@123
|
217 long msize;
|
garbeam@76
|
218 XSizeHints size;
|
arg@123
|
219
|
garbeam@76
|
220 if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
|
garbeam@76
|
221 size.flags = PSize;
|
garbeam@76
|
222 c->flags = size.flags;
|
garbeam@76
|
223 if(c->flags & PBaseSize) {
|
garbeam@76
|
224 c->basew = size.base_width;
|
garbeam@76
|
225 c->baseh = size.base_height;
|
garbeam@76
|
226 }
|
garbeam@76
|
227 else
|
garbeam@76
|
228 c->basew = c->baseh = 0;
|
garbeam@76
|
229 if(c->flags & PResizeInc) {
|
garbeam@76
|
230 c->incw = size.width_inc;
|
garbeam@76
|
231 c->inch = size.height_inc;
|
garbeam@76
|
232 }
|
garbeam@76
|
233 else
|
garbeam@76
|
234 c->incw = c->inch = 0;
|
garbeam@76
|
235 if(c->flags & PMaxSize) {
|
garbeam@76
|
236 c->maxw = size.max_width;
|
garbeam@76
|
237 c->maxh = size.max_height;
|
garbeam@76
|
238 }
|
garbeam@76
|
239 else
|
garbeam@76
|
240 c->maxw = c->maxh = 0;
|
garbeam@76
|
241 if(c->flags & PMinSize) {
|
garbeam@76
|
242 c->minw = size.min_width;
|
garbeam@76
|
243 c->minh = size.min_height;
|
garbeam@76
|
244 }
|
garbeam@76
|
245 else
|
garbeam@76
|
246 c->minw = c->minh = 0;
|
arg@550
|
247 c->isfixed = (c->maxw && c->minw && c->maxh && c->minh &&
|
arg@550
|
248 c->maxw == c->minw && c->maxh == c->minh);
|
garbeam@76
|
249 }
|
garbeam@76
|
250
|
garbeam@76
|
251 void
|
arg@461
|
252 updatetitle(Client *c) {
|
arg@123
|
253 char **list = NULL;
|
arg@377
|
254 int n;
|
garbeam@76
|
255 XTextProperty name;
|
garbeam@76
|
256
|
garbeam@76
|
257 name.nitems = 0;
|
garbeam@76
|
258 c->name[0] = 0;
|
garbeam@77
|
259 XGetTextProperty(dpy, c->win, &name, netatom[NetWMName]);
|
garbeam@76
|
260 if(!name.nitems)
|
garbeam@76
|
261 XGetWMName(dpy, c->win, &name);
|
garbeam@76
|
262 if(!name.nitems)
|
garbeam@76
|
263 return;
|
garbeam@76
|
264 if(name.encoding == XA_STRING)
|
arg@581
|
265 strncpy(c->name, (char *)name.value, sizeof c->name);
|
garbeam@76
|
266 else {
|
garbeam@76
|
267 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
|
garbeam@76
|
268 && n > 0 && *list)
|
garbeam@76
|
269 {
|
arg@581
|
270 strncpy(c->name, *list, sizeof c->name);
|
garbeam@76
|
271 XFreeStringList(list);
|
garbeam@76
|
272 }
|
garbeam@76
|
273 }
|
garbeam@76
|
274 XFree(name.value);
|
garbeam@10
|
275 }
|
garbeam@10
|
276
|
garbeam@10
|
277 void
|
arg@461
|
278 unmanage(Client *c) {
|
arg@450
|
279 Client *nc;
|
arg@450
|
280
|
arg@472
|
281 /* The server grab construct avoids race conditions. */
|
garbeam@10
|
282 XGrabServer(dpy);
|
garbeam@75
|
283 XSetErrorHandler(xerrordummy);
|
arg@400
|
284 detach(c);
|
arg@448
|
285 detachstack(c);
|
arg@400
|
286 if(sel == c) {
|
arg@450
|
287 for(nc = stack; nc && !isvisible(nc); nc = nc->snext);
|
arg@450
|
288 focus(nc);
|
arg@400
|
289 }
|
garbeam@18
|
290 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
|
arg@178
|
291 free(c->tags);
|
garbeam@10
|
292 free(c);
|
garbeam@79
|
293 XSync(dpy, False);
|
garbeam@74
|
294 XSetErrorHandler(xerror);
|
garbeam@10
|
295 XUngrabServer(dpy);
|
arg@533
|
296 arrange();
|
garbeam@10
|
297 }
|