dwm-meillo

view client.c @ 31:386649deb651

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