rev |
line source |
garbeam@5
|
1 /*
|
garbeam@5
|
2 * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
|
garbeam@5
|
3 * See LICENSE file for license details.
|
garbeam@5
|
4 */
|
garbeam@5
|
5
|
garbeam@27
|
6 #include <math.h>
|
garbeam@10
|
7 #include <stdlib.h>
|
garbeam@5
|
8 #include <string.h>
|
garbeam@5
|
9 #include <X11/Xatom.h>
|
garbeam@32
|
10 #include <X11/Xutil.h>
|
garbeam@5
|
11
|
garbeam@43
|
12 #include "dwm.h"
|
garbeam@5
|
13
|
garbeam@34
|
14 static void floating(void);
|
garbeam@34
|
15 static void tiling(void);
|
garbeam@34
|
16 static void (*arrange)(void) = tiling;
|
garbeam@31
|
17
|
garbeam@27
|
18 void
|
garbeam@28
|
19 max(void *aux)
|
garbeam@28
|
20 {
|
garbeam@28
|
21 if(!stack)
|
garbeam@28
|
22 return;
|
garbeam@28
|
23 stack->x = sx;
|
garbeam@32
|
24 stack->y = sy;
|
garbeam@29
|
25 stack->w = sw - 2 * stack->border;
|
garbeam@32
|
26 stack->h = sh - 2 * stack->border;
|
garbeam@28
|
27 resize(stack);
|
garbeam@29
|
28 discard_events(EnterWindowMask);
|
garbeam@28
|
29 }
|
garbeam@28
|
30
|
garbeam@34
|
31 static void
|
garbeam@34
|
32 floating(void)
|
garbeam@31
|
33 {
|
garbeam@31
|
34 Client *c;
|
garbeam@31
|
35
|
garbeam@31
|
36 for(c = stack; c; c = c->snext)
|
garbeam@31
|
37 resize(c);
|
garbeam@31
|
38 discard_events(EnterWindowMask);
|
garbeam@31
|
39 }
|
garbeam@31
|
40
|
garbeam@34
|
41 static void
|
garbeam@34
|
42 tiling(void)
|
garbeam@27
|
43 {
|
garbeam@27
|
44 Client *c;
|
garbeam@27
|
45 int n, cols, rows, gw, gh, i, j;
|
garbeam@27
|
46 float rt, fd;
|
garbeam@27
|
47
|
garbeam@27
|
48 if(!clients)
|
garbeam@27
|
49 return;
|
garbeam@27
|
50 for(n = 0, c = clients; c; c = c->next, n++);
|
garbeam@27
|
51 rt = sqrt(n);
|
garbeam@27
|
52 if(modff(rt, &fd) < 0.5)
|
garbeam@27
|
53 rows = floor(rt);
|
garbeam@27
|
54 else
|
garbeam@27
|
55 rows = ceil(rt);
|
garbeam@27
|
56 if(rows * rows < n)
|
garbeam@27
|
57 cols = rows + 1;
|
garbeam@27
|
58 else
|
garbeam@27
|
59 cols = rows;
|
garbeam@27
|
60
|
garbeam@30
|
61 gw = (sw - 2) / cols;
|
garbeam@32
|
62 gh = (sh - 2) / rows;
|
garbeam@27
|
63
|
garbeam@27
|
64 for(i = j = 0, c = clients; c; c = c->next) {
|
garbeam@27
|
65 c->x = i * gw;
|
garbeam@32
|
66 c->y = j * gh;
|
garbeam@27
|
67 c->w = gw;
|
garbeam@27
|
68 c->h = gh;
|
garbeam@27
|
69 resize(c);
|
garbeam@27
|
70 if(++i == cols) {
|
garbeam@27
|
71 j++;
|
garbeam@27
|
72 i = 0;
|
garbeam@27
|
73 }
|
garbeam@27
|
74 }
|
garbeam@29
|
75 discard_events(EnterWindowMask);
|
garbeam@27
|
76 }
|
garbeam@27
|
77
|
garbeam@27
|
78 void
|
garbeam@34
|
79 toggle(void *aux)
|
garbeam@34
|
80 {
|
garbeam@34
|
81 if(arrange == floating)
|
garbeam@34
|
82 arrange = tiling;
|
garbeam@34
|
83 else
|
garbeam@34
|
84 arrange = floating;
|
garbeam@34
|
85 arrange();
|
garbeam@34
|
86 }
|
garbeam@34
|
87
|
garbeam@34
|
88
|
garbeam@34
|
89 void
|
garbeam@27
|
90 sel(void *aux)
|
garbeam@27
|
91 {
|
garbeam@27
|
92 const char *arg = aux;
|
garbeam@27
|
93 Client *c = NULL;
|
garbeam@27
|
94
|
garbeam@27
|
95 if(!arg || !stack)
|
garbeam@27
|
96 return;
|
garbeam@27
|
97 if(!strncmp(arg, "next", 5))
|
garbeam@27
|
98 c = stack->snext ? stack->snext : stack;
|
garbeam@27
|
99 else if(!strncmp(arg, "prev", 5))
|
garbeam@27
|
100 for(c = stack; c && c->snext; c = c->snext);
|
garbeam@27
|
101 if(!c)
|
garbeam@27
|
102 c = stack;
|
garbeam@32
|
103 craise(c);
|
garbeam@27
|
104 focus(c);
|
garbeam@27
|
105 }
|
garbeam@27
|
106
|
garbeam@27
|
107 void
|
garbeam@32
|
108 ckill(void *aux)
|
garbeam@27
|
109 {
|
garbeam@27
|
110 Client *c = stack;
|
garbeam@27
|
111
|
garbeam@27
|
112 if(!c)
|
garbeam@27
|
113 return;
|
garbeam@27
|
114 if(c->proto & WM_PROTOCOL_DELWIN)
|
garbeam@27
|
115 send_message(c->win, wm_atom[WMProtocols], wm_atom[WMDelete]);
|
garbeam@27
|
116 else
|
garbeam@27
|
117 XKillClient(dpy, c->win);
|
garbeam@27
|
118 }
|
garbeam@27
|
119
|
garbeam@26
|
120 static void
|
garbeam@26
|
121 resize_title(Client *c)
|
garbeam@26
|
122 {
|
garbeam@31
|
123 int i;
|
garbeam@31
|
124
|
garbeam@31
|
125 c->tw = 0;
|
garbeam@31
|
126 for(i = 0; i < TLast; i++)
|
garbeam@31
|
127 if(c->tags[i])
|
garbeam@43
|
128 c->tw += textw(c->tags[i]) + dc.font.height;
|
garbeam@43
|
129 c->tw += textw(c->name) + dc.font.height;
|
garbeam@26
|
130 if(c->tw > c->w)
|
garbeam@26
|
131 c->tw = c->w + 2;
|
garbeam@26
|
132 c->tx = c->x + c->w - c->tw + 2;
|
garbeam@26
|
133 c->ty = c->y;
|
garbeam@26
|
134 XMoveResizeWindow(dpy, c->title, c->tx, c->ty, c->tw, c->th);
|
garbeam@26
|
135 }
|
garbeam@18
|
136
|
garbeam@13
|
137 void
|
garbeam@13
|
138 update_name(Client *c)
|
garbeam@5
|
139 {
|
garbeam@5
|
140 XTextProperty name;
|
garbeam@5
|
141 int n;
|
garbeam@7
|
142 char **list = NULL;
|
garbeam@5
|
143
|
garbeam@5
|
144 name.nitems = 0;
|
garbeam@5
|
145 c->name[0] = 0;
|
garbeam@5
|
146 XGetTextProperty(dpy, c->win, &name, net_atom[NetWMName]);
|
garbeam@5
|
147 if(!name.nitems)
|
garbeam@5
|
148 XGetWMName(dpy, c->win, &name);
|
garbeam@5
|
149 if(!name.nitems)
|
garbeam@5
|
150 return;
|
garbeam@5
|
151 if(name.encoding == XA_STRING)
|
garbeam@5
|
152 strncpy(c->name, (char *)name.value, sizeof(c->name));
|
garbeam@5
|
153 else {
|
garbeam@5
|
154 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
|
garbeam@5
|
155 && n > 0 && *list)
|
garbeam@5
|
156 {
|
garbeam@5
|
157 strncpy(c->name, *list, sizeof(c->name));
|
garbeam@5
|
158 XFreeStringList(list);
|
garbeam@5
|
159 }
|
garbeam@5
|
160 }
|
garbeam@5
|
161 XFree(name.value);
|
garbeam@26
|
162 resize_title(c);
|
garbeam@5
|
163 }
|
garbeam@5
|
164
|
garbeam@10
|
165 void
|
garbeam@20
|
166 update_size(Client *c)
|
garbeam@20
|
167 {
|
garbeam@20
|
168 XSizeHints size;
|
garbeam@20
|
169 long msize;
|
garbeam@20
|
170 if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
|
garbeam@20
|
171 size.flags = PSize;
|
garbeam@20
|
172 c->flags = size.flags;
|
garbeam@21
|
173 if(c->flags & PBaseSize) {
|
garbeam@21
|
174 c->basew = size.base_width;
|
garbeam@21
|
175 c->baseh = size.base_height;
|
garbeam@21
|
176 }
|
garbeam@21
|
177 else
|
garbeam@21
|
178 c->basew = c->baseh = 0;
|
garbeam@21
|
179 if(c->flags & PResizeInc) {
|
garbeam@21
|
180 c->incw = size.width_inc;
|
garbeam@21
|
181 c->inch = size.height_inc;
|
garbeam@21
|
182 }
|
garbeam@21
|
183 else
|
garbeam@21
|
184 c->incw = c->inch = 0;
|
garbeam@21
|
185 if(c->flags & PMaxSize) {
|
garbeam@21
|
186 c->maxw = size.max_width;
|
garbeam@21
|
187 c->maxh = size.max_height;
|
garbeam@21
|
188 }
|
garbeam@21
|
189 else
|
garbeam@21
|
190 c->maxw = c->maxh = 0;
|
garbeam@21
|
191 if(c->flags & PMinSize) {
|
garbeam@21
|
192 c->minw = size.min_width;
|
garbeam@21
|
193 c->minh = size.min_height;
|
garbeam@21
|
194 }
|
garbeam@21
|
195 else
|
garbeam@21
|
196 c->minw = c->minh = 0;
|
garbeam@29
|
197 if(c->flags & PWinGravity)
|
garbeam@29
|
198 c->grav = size.win_gravity;
|
garbeam@29
|
199 else
|
garbeam@29
|
200 c->grav = NorthWestGravity;
|
garbeam@20
|
201 }
|
garbeam@20
|
202
|
garbeam@20
|
203 void
|
garbeam@32
|
204 craise(Client *c)
|
garbeam@26
|
205 {
|
garbeam@26
|
206 XRaiseWindow(dpy, c->win);
|
garbeam@26
|
207 XRaiseWindow(dpy, c->title);
|
garbeam@26
|
208 }
|
garbeam@26
|
209
|
garbeam@26
|
210 void
|
garbeam@26
|
211 lower(Client *c)
|
garbeam@26
|
212 {
|
garbeam@26
|
213 XLowerWindow(dpy, c->title);
|
garbeam@26
|
214 XLowerWindow(dpy, c->win);
|
garbeam@26
|
215 }
|
garbeam@26
|
216
|
garbeam@26
|
217 void
|
garbeam@13
|
218 focus(Client *c)
|
garbeam@13
|
219 {
|
garbeam@21
|
220 Client **l, *old;
|
garbeam@21
|
221
|
garbeam@21
|
222 old = stack;
|
garbeam@26
|
223 for(l = &stack; *l && *l != c; l = &(*l)->snext);
|
garbeam@31
|
224 if(*l)
|
garbeam@31
|
225 *l = c->snext;
|
garbeam@13
|
226 c->snext = stack;
|
garbeam@13
|
227 stack = c;
|
garbeam@21
|
228 if(old && old != c) {
|
garbeam@43
|
229 XSetWindowBorder(dpy, old->win, dc.bg);
|
garbeam@21
|
230 XMapWindow(dpy, old->title);
|
garbeam@21
|
231 draw_client(old);
|
garbeam@21
|
232 }
|
garbeam@21
|
233 XUnmapWindow(dpy, c->title);
|
garbeam@43
|
234 XSetWindowBorder(dpy, c->win, dc.fg);
|
garbeam@27
|
235 draw_client(c);
|
garbeam@26
|
236 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
|
garbeam@13
|
237 XFlush(dpy);
|
garbeam@13
|
238 }
|
garbeam@13
|
239
|
garbeam@13
|
240 void
|
garbeam@10
|
241 manage(Window w, XWindowAttributes *wa)
|
garbeam@5
|
242 {
|
garbeam@10
|
243 Client *c, **l;
|
garbeam@5
|
244 XSetWindowAttributes twa;
|
garbeam@5
|
245
|
garbeam@5
|
246 c = emallocz(sizeof(Client));
|
garbeam@5
|
247 c->win = w;
|
garbeam@22
|
248 c->tx = c->x = wa->x;
|
garbeam@22
|
249 c->ty = c->y = wa->y;
|
garbeam@22
|
250 c->tw = c->w = wa->width;
|
garbeam@20
|
251 c->h = wa->height;
|
garbeam@32
|
252 c->th = th;
|
garbeam@29
|
253 c->border = 1;
|
garbeam@20
|
254 update_size(c);
|
garbeam@26
|
255 XSelectInput(dpy, c->win,
|
garbeam@26
|
256 StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
|
garbeam@5
|
257 XGetTransientForHint(dpy, c->win, &c->trans);
|
garbeam@5
|
258 twa.override_redirect = 1;
|
garbeam@5
|
259 twa.background_pixmap = ParentRelative;
|
garbeam@23
|
260 twa.event_mask = ExposureMask;
|
garbeam@5
|
261
|
garbeam@31
|
262 c->tags[tsel] = tags[tsel];
|
garbeam@22
|
263 c->title = XCreateWindow(dpy, root, c->tx, c->ty, c->tw, c->th,
|
garbeam@20
|
264 0, DefaultDepth(dpy, screen), CopyFromParent,
|
garbeam@5
|
265 DefaultVisual(dpy, screen),
|
garbeam@5
|
266 CWOverrideRedirect | CWBackPixmap | CWEventMask, &twa);
|
garbeam@31
|
267
|
garbeam@21
|
268 update_name(c);
|
garbeam@10
|
269 for(l=&clients; *l; l=&(*l)->next);
|
garbeam@10
|
270 c->next = *l; /* *l == nil */
|
garbeam@10
|
271 *l = c;
|
garbeam@43
|
272 XSetWindowBorderWidth(dpy, c->win, 1);
|
garbeam@26
|
273 XMapRaised(dpy, c->win);
|
garbeam@26
|
274 XMapRaised(dpy, c->title);
|
garbeam@19
|
275 XGrabButton(dpy, Button1, Mod1Mask, c->win, False, ButtonPressMask,
|
garbeam@19
|
276 GrabModeAsync, GrabModeSync, None, None);
|
garbeam@19
|
277 XGrabButton(dpy, Button2, Mod1Mask, c->win, False, ButtonPressMask,
|
garbeam@19
|
278 GrabModeAsync, GrabModeSync, None, None);
|
garbeam@19
|
279 XGrabButton(dpy, Button3, Mod1Mask, c->win, False, ButtonPressMask,
|
garbeam@18
|
280 GrabModeAsync, GrabModeSync, None, None);
|
garbeam@34
|
281 arrange();
|
garbeam@13
|
282 focus(c);
|
garbeam@5
|
283 }
|
garbeam@9
|
284
|
garbeam@18
|
285 void
|
garbeam@29
|
286 gravitate(Client *c, Bool invert)
|
garbeam@29
|
287 {
|
garbeam@29
|
288 int dx = 0, dy = 0;
|
garbeam@29
|
289
|
garbeam@29
|
290 switch(c->grav) {
|
garbeam@29
|
291 case StaticGravity:
|
garbeam@29
|
292 case NorthWestGravity:
|
garbeam@29
|
293 case NorthGravity:
|
garbeam@29
|
294 case NorthEastGravity:
|
garbeam@29
|
295 dy = c->border;
|
garbeam@29
|
296 break;
|
garbeam@29
|
297 case EastGravity:
|
garbeam@29
|
298 case CenterGravity:
|
garbeam@29
|
299 case WestGravity:
|
garbeam@29
|
300 dy = -(c->h / 2) + c->border;
|
garbeam@29
|
301 break;
|
garbeam@29
|
302 case SouthEastGravity:
|
garbeam@29
|
303 case SouthGravity:
|
garbeam@29
|
304 case SouthWestGravity:
|
garbeam@29
|
305 dy = -c->h;
|
garbeam@29
|
306 break;
|
garbeam@29
|
307 default:
|
garbeam@29
|
308 break;
|
garbeam@29
|
309 }
|
garbeam@29
|
310
|
garbeam@29
|
311 switch (c->grav) {
|
garbeam@29
|
312 case StaticGravity:
|
garbeam@29
|
313 case NorthWestGravity:
|
garbeam@29
|
314 case WestGravity:
|
garbeam@29
|
315 case SouthWestGravity:
|
garbeam@29
|
316 dx = c->border;
|
garbeam@29
|
317 break;
|
garbeam@29
|
318 case NorthGravity:
|
garbeam@29
|
319 case CenterGravity:
|
garbeam@29
|
320 case SouthGravity:
|
garbeam@29
|
321 dx = -(c->w / 2) + c->border;
|
garbeam@29
|
322 break;
|
garbeam@29
|
323 case NorthEastGravity:
|
garbeam@29
|
324 case EastGravity:
|
garbeam@29
|
325 case SouthEastGravity:
|
garbeam@29
|
326 dx = -(c->w + c->border);
|
garbeam@29
|
327 break;
|
garbeam@29
|
328 default:
|
garbeam@29
|
329 break;
|
garbeam@29
|
330 }
|
garbeam@29
|
331
|
garbeam@29
|
332 if(invert) {
|
garbeam@29
|
333 dx = -dx;
|
garbeam@29
|
334 dy = -dy;
|
garbeam@29
|
335 }
|
garbeam@29
|
336 c->x += dx;
|
garbeam@29
|
337 c->y += dy;
|
garbeam@29
|
338 }
|
garbeam@29
|
339
|
garbeam@31
|
340
|
garbeam@29
|
341 void
|
garbeam@18
|
342 resize(Client *c)
|
garbeam@18
|
343 {
|
garbeam@18
|
344 XConfigureEvent e;
|
garbeam@18
|
345
|
garbeam@31
|
346 if(c->incw)
|
garbeam@31
|
347 c->w -= (c->w - c->basew) % c->incw;
|
garbeam@31
|
348 if(c->inch)
|
garbeam@31
|
349 c->h -= (c->h - c->baseh) % c->inch;
|
garbeam@31
|
350 if(c->minw && c->w < c->minw)
|
garbeam@31
|
351 c->w = c->minw;
|
garbeam@31
|
352 if(c->minh && c->h < c->minh)
|
garbeam@31
|
353 c->h = c->minh;
|
garbeam@31
|
354 if(c->maxw && c->w > c->maxw)
|
garbeam@31
|
355 c->w = c->maxw;
|
garbeam@31
|
356 if(c->maxh && c->h > c->maxh)
|
garbeam@31
|
357 c->h = c->maxh;
|
garbeam@26
|
358 resize_title(c);
|
garbeam@20
|
359 XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
|
garbeam@18
|
360 e.type = ConfigureNotify;
|
garbeam@18
|
361 e.event = c->win;
|
garbeam@18
|
362 e.window = c->win;
|
garbeam@20
|
363 e.x = c->x;
|
garbeam@20
|
364 e.y = c->y;
|
garbeam@20
|
365 e.width = c->w;
|
garbeam@20
|
366 e.height = c->h;
|
garbeam@29
|
367 e.border_width = c->border;
|
garbeam@18
|
368 e.above = None;
|
garbeam@18
|
369 e.override_redirect = False;
|
garbeam@18
|
370 XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&e);
|
garbeam@18
|
371 XFlush(dpy);
|
garbeam@18
|
372 }
|
garbeam@18
|
373
|
garbeam@10
|
374 static int
|
garbeam@33
|
375 dummy_error_handler(Display *dsply, XErrorEvent *err)
|
garbeam@10
|
376 {
|
garbeam@10
|
377 return 0;
|
garbeam@10
|
378 }
|
garbeam@10
|
379
|
garbeam@10
|
380 void
|
garbeam@10
|
381 unmanage(Client *c)
|
garbeam@10
|
382 {
|
garbeam@10
|
383 Client **l;
|
garbeam@10
|
384
|
garbeam@10
|
385 XGrabServer(dpy);
|
garbeam@10
|
386 XSetErrorHandler(dummy_error_handler);
|
garbeam@10
|
387
|
garbeam@18
|
388 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
|
garbeam@10
|
389 XDestroyWindow(dpy, c->title);
|
garbeam@10
|
390
|
garbeam@10
|
391 for(l=&clients; *l && *l != c; l=&(*l)->next);
|
garbeam@10
|
392 *l = c->next;
|
garbeam@13
|
393 for(l=&stack; *l && *l != c; l=&(*l)->snext);
|
garbeam@13
|
394 *l = c->snext;
|
garbeam@10
|
395 free(c);
|
garbeam@10
|
396
|
garbeam@10
|
397 XFlush(dpy);
|
garbeam@10
|
398 XSetErrorHandler(error_handler);
|
garbeam@10
|
399 XUngrabServer(dpy);
|
garbeam@34
|
400 arrange();
|
garbeam@14
|
401 if(stack)
|
garbeam@14
|
402 focus(stack);
|
garbeam@10
|
403 }
|
garbeam@10
|
404
|
garbeam@23
|
405 Client *
|
garbeam@23
|
406 gettitle(Window w)
|
garbeam@23
|
407 {
|
garbeam@23
|
408 Client *c;
|
garbeam@23
|
409 for(c = clients; c; c = c->next)
|
garbeam@23
|
410 if(c->title == w)
|
garbeam@23
|
411 return c;
|
garbeam@23
|
412 return NULL;
|
garbeam@23
|
413 }
|
garbeam@10
|
414
|
garbeam@9
|
415 Client *
|
garbeam@9
|
416 getclient(Window w)
|
garbeam@9
|
417 {
|
garbeam@9
|
418 Client *c;
|
garbeam@9
|
419 for(c = clients; c; c = c->next)
|
garbeam@9
|
420 if(c->win == w)
|
garbeam@9
|
421 return c;
|
garbeam@9
|
422 return NULL;
|
garbeam@9
|
423 }
|
garbeam@13
|
424
|
garbeam@14
|
425 void
|
garbeam@14
|
426 draw_client(Client *c)
|
garbeam@14
|
427 {
|
garbeam@31
|
428 int i;
|
garbeam@32
|
429 if(c == stack)
|
garbeam@26
|
430 return;
|
garbeam@14
|
431
|
garbeam@34
|
432 dc.x = dc.y = 0;
|
garbeam@34
|
433 dc.h = c->th;
|
garbeam@14
|
434
|
garbeam@34
|
435 dc.w = 0;
|
garbeam@31
|
436 for(i = 0; i < TLast; i++) {
|
garbeam@31
|
437 if(c->tags[i]) {
|
garbeam@34
|
438 dc.x += dc.w;
|
garbeam@43
|
439 dc.w = textw(c->tags[i]) + dc.font.height;
|
garbeam@34
|
440 draw(True, c->tags[i]);
|
garbeam@31
|
441 }
|
garbeam@31
|
442 }
|
garbeam@34
|
443 dc.x += dc.w;
|
garbeam@43
|
444 dc.w = textw(c->name) + dc.font.height;
|
garbeam@34
|
445 draw(True, c->name);
|
garbeam@34
|
446 XCopyArea(dpy, dc.drawable, c->title, dc.gc,
|
garbeam@22
|
447 0, 0, c->tw, c->th, 0, 0);
|
garbeam@21
|
448 XFlush(dpy);
|
garbeam@14
|
449 }
|