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