aewl
view client.c @ 33:e90449e03167
new stuff (some warning elimination)
author | Anselm R. Garbe <garbeam@wmii.de> |
---|---|
date | Thu, 13 Jul 2006 01:55:54 +0200 |
parents | 082c75b937b5 |
children | cd30cce52b78 |
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 "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 = sy;
23 stack->w = sw - 2 * stack->border;
24 stack->h = sh - 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 - 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 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 craise(c);
93 focus(c);
94 }
96 void
97 ckill(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]) + brush.font.height;
118 c->tw += textw(&brush.font, c->name) + brush.font.height;
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 craise(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 c->tw = c->w = wa->width;
238 c->h = wa->height;
239 c->th = th;
240 c->border = 1;
241 update_size(c);
242 XSetWindowBorderWidth(dpy, c->win, 1);
243 XSetWindowBorder(dpy, c->win, brush.border);
244 XSelectInput(dpy, c->win,
245 StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
246 XGetTransientForHint(dpy, c->win, &c->trans);
247 twa.override_redirect = 1;
248 twa.background_pixmap = ParentRelative;
249 twa.event_mask = ExposureMask;
251 c->tags[tsel] = tags[tsel];
252 c->title = XCreateWindow(dpy, root, c->tx, c->ty, c->tw, c->th,
253 0, DefaultDepth(dpy, screen), CopyFromParent,
254 DefaultVisual(dpy, screen),
255 CWOverrideRedirect | CWBackPixmap | CWEventMask, &twa);
257 update_name(c);
258 for(l=&clients; *l; l=&(*l)->next);
259 c->next = *l; /* *l == nil */
260 *l = c;
261 XMapRaised(dpy, c->win);
262 XMapRaised(dpy, c->title);
263 XGrabButton(dpy, Button1, Mod1Mask, c->win, False, ButtonPressMask,
264 GrabModeAsync, GrabModeSync, None, None);
265 XGrabButton(dpy, Button2, Mod1Mask, c->win, False, ButtonPressMask,
266 GrabModeAsync, GrabModeSync, None, None);
267 XGrabButton(dpy, Button3, Mod1Mask, c->win, False, ButtonPressMask,
268 GrabModeAsync, GrabModeSync, None, None);
269 arrange(NULL);
270 focus(c);
271 }
273 void
274 gravitate(Client *c, Bool invert)
275 {
276 int dx = 0, dy = 0;
278 switch(c->grav) {
279 case StaticGravity:
280 case NorthWestGravity:
281 case NorthGravity:
282 case NorthEastGravity:
283 dy = c->border;
284 break;
285 case EastGravity:
286 case CenterGravity:
287 case WestGravity:
288 dy = -(c->h / 2) + c->border;
289 break;
290 case SouthEastGravity:
291 case SouthGravity:
292 case SouthWestGravity:
293 dy = -c->h;
294 break;
295 default:
296 break;
297 }
299 switch (c->grav) {
300 case StaticGravity:
301 case NorthWestGravity:
302 case WestGravity:
303 case SouthWestGravity:
304 dx = c->border;
305 break;
306 case NorthGravity:
307 case CenterGravity:
308 case SouthGravity:
309 dx = -(c->w / 2) + c->border;
310 break;
311 case NorthEastGravity:
312 case EastGravity:
313 case SouthEastGravity:
314 dx = -(c->w + c->border);
315 break;
316 default:
317 break;
318 }
320 if(invert) {
321 dx = -dx;
322 dy = -dy;
323 }
324 c->x += dx;
325 c->y += dy;
326 }
329 void
330 resize(Client *c)
331 {
332 XConfigureEvent e;
334 if(c->incw)
335 c->w -= (c->w - c->basew) % c->incw;
336 if(c->inch)
337 c->h -= (c->h - c->baseh) % c->inch;
338 if(c->minw && c->w < c->minw)
339 c->w = c->minw;
340 if(c->minh && c->h < c->minh)
341 c->h = c->minh;
342 if(c->maxw && c->w > c->maxw)
343 c->w = c->maxw;
344 if(c->maxh && c->h > c->maxh)
345 c->h = c->maxh;
346 resize_title(c);
347 XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
348 e.type = ConfigureNotify;
349 e.event = c->win;
350 e.window = c->win;
351 e.x = c->x;
352 e.y = c->y;
353 e.width = c->w;
354 e.height = c->h;
355 e.border_width = c->border;
356 e.above = None;
357 e.override_redirect = False;
358 XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&e);
359 XFlush(dpy);
360 }
362 static int
363 dummy_error_handler(Display *dsply, XErrorEvent *err)
364 {
365 return 0;
366 }
368 void
369 unmanage(Client *c)
370 {
371 Client **l;
373 XGrabServer(dpy);
374 XSetErrorHandler(dummy_error_handler);
376 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
377 XDestroyWindow(dpy, c->title);
379 for(l=&clients; *l && *l != c; l=&(*l)->next);
380 *l = c->next;
381 for(l=&stack; *l && *l != c; l=&(*l)->snext);
382 *l = c->snext;
383 free(c);
385 XFlush(dpy);
386 XSetErrorHandler(error_handler);
387 XUngrabServer(dpy);
388 arrange(NULL);
389 if(stack)
390 focus(stack);
391 }
393 Client *
394 gettitle(Window w)
395 {
396 Client *c;
397 for(c = clients; c; c = c->next)
398 if(c->title == w)
399 return c;
400 return NULL;
401 }
403 Client *
404 getclient(Window w)
405 {
406 Client *c;
407 for(c = clients; c; c = c->next)
408 if(c->win == w)
409 return c;
410 return NULL;
411 }
413 void
414 draw_client(Client *c)
415 {
416 int i;
417 if(c == stack)
418 return;
420 brush.x = brush.y = 0;
421 brush.h = c->th;
423 brush.w = 0;
424 for(i = 0; i < TLast; i++) {
425 if(c->tags[i]) {
426 brush.x += brush.w;
427 brush.w = textw(&brush.font, c->tags[i]) + brush.font.height;
428 draw(&brush, True, c->tags[i]);
429 }
430 }
431 brush.x += brush.w;
432 brush.w = textw(&brush.font, c->name) + brush.font.height;
433 draw(&brush, True, c->name);
434 XCopyArea(dpy, brush.drawable, c->title, brush.gc,
435 0, 0, c->tw, c->th, 0, 0);
436 XFlush(dpy);
437 }