dwm-meillo

view client.c @ 46:58307ad56ec1

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