dwm-meillo
view client.c @ 37:eb14fb2c0ee0
small changes to README
author | Anselm R. Garbe <garbeam@wmii.de> |
---|---|
date | Thu, 13 Jul 2006 10:03:56 +0200 |
parents | e90449e03167 |
children | 989178822938 |
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 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(&dc.font, c->tags[i]) + dc.font.height;
129 c->tw += textw(&dc.font, 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 XMapWindow(dpy, old->title);
230 draw_client(old);
231 }
232 XUnmapWindow(dpy, c->title);
233 draw_client(c);
234 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
235 XFlush(dpy);
236 }
238 void
239 manage(Window w, XWindowAttributes *wa)
240 {
241 Client *c, **l;
242 XSetWindowAttributes twa;
244 c = emallocz(sizeof(Client));
245 c->win = w;
246 c->tx = c->x = wa->x;
247 c->ty = c->y = wa->y;
248 c->tw = c->w = wa->width;
249 c->h = wa->height;
250 c->th = th;
251 c->border = 1;
252 update_size(c);
253 XSetWindowBorderWidth(dpy, c->win, 1);
254 XSetWindowBorder(dpy, c->win, dc.border);
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 XMapRaised(dpy, c->win);
273 XMapRaised(dpy, c->title);
274 XGrabButton(dpy, Button1, Mod1Mask, c->win, False, ButtonPressMask,
275 GrabModeAsync, GrabModeSync, None, None);
276 XGrabButton(dpy, Button2, Mod1Mask, c->win, False, ButtonPressMask,
277 GrabModeAsync, GrabModeSync, None, None);
278 XGrabButton(dpy, Button3, Mod1Mask, c->win, False, ButtonPressMask,
279 GrabModeAsync, GrabModeSync, None, None);
280 arrange();
281 focus(c);
282 }
284 void
285 gravitate(Client *c, Bool invert)
286 {
287 int dx = 0, dy = 0;
289 switch(c->grav) {
290 case StaticGravity:
291 case NorthWestGravity:
292 case NorthGravity:
293 case NorthEastGravity:
294 dy = c->border;
295 break;
296 case EastGravity:
297 case CenterGravity:
298 case WestGravity:
299 dy = -(c->h / 2) + c->border;
300 break;
301 case SouthEastGravity:
302 case SouthGravity:
303 case SouthWestGravity:
304 dy = -c->h;
305 break;
306 default:
307 break;
308 }
310 switch (c->grav) {
311 case StaticGravity:
312 case NorthWestGravity:
313 case WestGravity:
314 case SouthWestGravity:
315 dx = c->border;
316 break;
317 case NorthGravity:
318 case CenterGravity:
319 case SouthGravity:
320 dx = -(c->w / 2) + c->border;
321 break;
322 case NorthEastGravity:
323 case EastGravity:
324 case SouthEastGravity:
325 dx = -(c->w + c->border);
326 break;
327 default:
328 break;
329 }
331 if(invert) {
332 dx = -dx;
333 dy = -dy;
334 }
335 c->x += dx;
336 c->y += dy;
337 }
340 void
341 resize(Client *c)
342 {
343 XConfigureEvent e;
345 if(c->incw)
346 c->w -= (c->w - c->basew) % c->incw;
347 if(c->inch)
348 c->h -= (c->h - c->baseh) % c->inch;
349 if(c->minw && c->w < c->minw)
350 c->w = c->minw;
351 if(c->minh && c->h < c->minh)
352 c->h = c->minh;
353 if(c->maxw && c->w > c->maxw)
354 c->w = c->maxw;
355 if(c->maxh && c->h > c->maxh)
356 c->h = c->maxh;
357 resize_title(c);
358 XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
359 e.type = ConfigureNotify;
360 e.event = c->win;
361 e.window = c->win;
362 e.x = c->x;
363 e.y = c->y;
364 e.width = c->w;
365 e.height = c->h;
366 e.border_width = c->border;
367 e.above = None;
368 e.override_redirect = False;
369 XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&e);
370 XFlush(dpy);
371 }
373 static int
374 dummy_error_handler(Display *dsply, XErrorEvent *err)
375 {
376 return 0;
377 }
379 void
380 unmanage(Client *c)
381 {
382 Client **l;
384 XGrabServer(dpy);
385 XSetErrorHandler(dummy_error_handler);
387 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
388 XDestroyWindow(dpy, c->title);
390 for(l=&clients; *l && *l != c; l=&(*l)->next);
391 *l = c->next;
392 for(l=&stack; *l && *l != c; l=&(*l)->snext);
393 *l = c->snext;
394 free(c);
396 XFlush(dpy);
397 XSetErrorHandler(error_handler);
398 XUngrabServer(dpy);
399 arrange();
400 if(stack)
401 focus(stack);
402 }
404 Client *
405 gettitle(Window w)
406 {
407 Client *c;
408 for(c = clients; c; c = c->next)
409 if(c->title == w)
410 return c;
411 return NULL;
412 }
414 Client *
415 getclient(Window w)
416 {
417 Client *c;
418 for(c = clients; c; c = c->next)
419 if(c->win == w)
420 return c;
421 return NULL;
422 }
424 void
425 draw_client(Client *c)
426 {
427 int i;
428 if(c == stack)
429 return;
431 dc.x = dc.y = 0;
432 dc.h = c->th;
434 dc.w = 0;
435 for(i = 0; i < TLast; i++) {
436 if(c->tags[i]) {
437 dc.x += dc.w;
438 dc.w = textw(&dc.font, c->tags[i]) + dc.font.height;
439 draw(True, c->tags[i]);
440 }
441 }
442 dc.x += dc.w;
443 dc.w = textw(&dc.font, c->name) + dc.font.height;
444 draw(True, c->name);
445 XCopyArea(dpy, dc.drawable, c->title, dc.gc,
446 0, 0, c->tw, c->th, 0, 0);
447 XFlush(dpy);
448 }