aewl
view client.c @ 45:7943c72e34eb
added pointer warps on kb-driven/manage-driven focus
author | Anselm R. Garbe <garbeam@wmii.de> |
---|---|
date | Thu, 13 Jul 2006 12:00:20 +0200 |
parents | a36f95a4841a |
children | 58307ad56ec1 |
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) = floating;
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 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w / 2, c->h / 2);
105 focus(c);
106 }
108 void
109 ckill(void *aux)
110 {
111 Client *c = stack;
113 if(!c)
114 return;
115 if(c->proto & WM_PROTOCOL_DELWIN)
116 send_message(c->win, wm_atom[WMProtocols], wm_atom[WMDelete]);
117 else
118 XKillClient(dpy, c->win);
119 }
121 static void
122 resize_title(Client *c)
123 {
124 int i;
126 c->tw = 0;
127 for(i = 0; i < TLast; i++)
128 if(c->tags[i])
129 c->tw += textw(c->tags[i]) + dc.font.height;
130 c->tw += textw(c->name) + dc.font.height;
131 if(c->tw > c->w)
132 c->tw = c->w + 2;
133 c->tx = c->x + c->w - c->tw + 2;
134 c->ty = c->y;
135 XMoveResizeWindow(dpy, c->title, c->tx, c->ty, c->tw, c->th);
136 }
138 void
139 update_name(Client *c)
140 {
141 XTextProperty name;
142 int n;
143 char **list = NULL;
145 name.nitems = 0;
146 c->name[0] = 0;
147 XGetTextProperty(dpy, c->win, &name, net_atom[NetWMName]);
148 if(!name.nitems)
149 XGetWMName(dpy, c->win, &name);
150 if(!name.nitems)
151 return;
152 if(name.encoding == XA_STRING)
153 strncpy(c->name, (char *)name.value, sizeof(c->name));
154 else {
155 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
156 && n > 0 && *list)
157 {
158 strncpy(c->name, *list, sizeof(c->name));
159 XFreeStringList(list);
160 }
161 }
162 XFree(name.value);
163 resize_title(c);
164 }
166 void
167 update_size(Client *c)
168 {
169 XSizeHints size;
170 long msize;
171 if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
172 size.flags = PSize;
173 c->flags = size.flags;
174 if(c->flags & PBaseSize) {
175 c->basew = size.base_width;
176 c->baseh = size.base_height;
177 }
178 else
179 c->basew = c->baseh = 0;
180 if(c->flags & PResizeInc) {
181 c->incw = size.width_inc;
182 c->inch = size.height_inc;
183 }
184 else
185 c->incw = c->inch = 0;
186 if(c->flags & PMaxSize) {
187 c->maxw = size.max_width;
188 c->maxh = size.max_height;
189 }
190 else
191 c->maxw = c->maxh = 0;
192 if(c->flags & PMinSize) {
193 c->minw = size.min_width;
194 c->minh = size.min_height;
195 }
196 else
197 c->minw = c->minh = 0;
198 if(c->flags & PWinGravity)
199 c->grav = size.win_gravity;
200 else
201 c->grav = NorthWestGravity;
202 }
204 void
205 craise(Client *c)
206 {
207 XRaiseWindow(dpy, c->win);
208 XRaiseWindow(dpy, c->title);
209 }
211 void
212 lower(Client *c)
213 {
214 XLowerWindow(dpy, c->title);
215 XLowerWindow(dpy, c->win);
216 }
218 void
219 focus(Client *c)
220 {
221 Client **l, *old;
223 old = stack;
224 for(l = &stack; *l && *l != c; l = &(*l)->snext);
225 if(*l)
226 *l = c->snext;
227 c->snext = stack;
228 stack = c;
229 if(old && old != c) {
230 XSetWindowBorder(dpy, old->win, dc.bg);
231 XMapWindow(dpy, old->title);
232 draw_client(old);
233 }
234 XUnmapWindow(dpy, c->title);
235 XSetWindowBorder(dpy, c->win, dc.fg);
236 draw_client(c);
237 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
238 XFlush(dpy);
239 discard_events(EnterWindowMask);
240 }
242 void
243 manage(Window w, XWindowAttributes *wa)
244 {
245 Client *c, **l;
246 XSetWindowAttributes twa;
248 c = emallocz(sizeof(Client));
249 c->win = w;
250 c->tx = c->x = wa->x;
251 c->ty = c->y = wa->y;
252 c->tw = c->w = wa->width;
253 c->h = wa->height;
254 c->th = th;
255 c->border = 1;
256 update_size(c);
257 XSelectInput(dpy, c->win,
258 StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
259 XGetTransientForHint(dpy, c->win, &c->trans);
260 twa.override_redirect = 1;
261 twa.background_pixmap = ParentRelative;
262 twa.event_mask = ExposureMask;
264 c->tags[tsel] = tags[tsel];
265 c->title = XCreateWindow(dpy, root, c->tx, c->ty, c->tw, c->th,
266 0, DefaultDepth(dpy, screen), CopyFromParent,
267 DefaultVisual(dpy, screen),
268 CWOverrideRedirect | CWBackPixmap | CWEventMask, &twa);
270 update_name(c);
271 for(l=&clients; *l; l=&(*l)->next);
272 c->next = *l; /* *l == nil */
273 *l = c;
274 XSetWindowBorderWidth(dpy, c->win, 1);
275 XMapRaised(dpy, c->win);
276 XMapRaised(dpy, c->title);
277 XGrabButton(dpy, Button1, Mod1Mask, c->win, False, ButtonPressMask,
278 GrabModeAsync, GrabModeSync, None, None);
279 XGrabButton(dpy, Button2, Mod1Mask, c->win, False, ButtonPressMask,
280 GrabModeAsync, GrabModeSync, None, None);
281 XGrabButton(dpy, Button3, Mod1Mask, c->win, False, ButtonPressMask,
282 GrabModeAsync, GrabModeSync, None, None);
283 arrange();
284 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w / 2, c->h / 2);
285 focus(c);
286 }
288 void
289 gravitate(Client *c, Bool invert)
290 {
291 int dx = 0, dy = 0;
293 switch(c->grav) {
294 case StaticGravity:
295 case NorthWestGravity:
296 case NorthGravity:
297 case NorthEastGravity:
298 dy = c->border;
299 break;
300 case EastGravity:
301 case CenterGravity:
302 case WestGravity:
303 dy = -(c->h / 2) + c->border;
304 break;
305 case SouthEastGravity:
306 case SouthGravity:
307 case SouthWestGravity:
308 dy = -c->h;
309 break;
310 default:
311 break;
312 }
314 switch (c->grav) {
315 case StaticGravity:
316 case NorthWestGravity:
317 case WestGravity:
318 case SouthWestGravity:
319 dx = c->border;
320 break;
321 case NorthGravity:
322 case CenterGravity:
323 case SouthGravity:
324 dx = -(c->w / 2) + c->border;
325 break;
326 case NorthEastGravity:
327 case EastGravity:
328 case SouthEastGravity:
329 dx = -(c->w + c->border);
330 break;
331 default:
332 break;
333 }
335 if(invert) {
336 dx = -dx;
337 dy = -dy;
338 }
339 c->x += dx;
340 c->y += dy;
341 }
344 void
345 resize(Client *c)
346 {
347 XConfigureEvent e;
349 if(c->incw)
350 c->w -= (c->w - c->basew) % c->incw;
351 if(c->inch)
352 c->h -= (c->h - c->baseh) % c->inch;
353 if(c->minw && c->w < c->minw)
354 c->w = c->minw;
355 if(c->minh && c->h < c->minh)
356 c->h = c->minh;
357 if(c->maxw && c->w > c->maxw)
358 c->w = c->maxw;
359 if(c->maxh && c->h > c->maxh)
360 c->h = c->maxh;
361 resize_title(c);
362 XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
363 e.type = ConfigureNotify;
364 e.event = c->win;
365 e.window = c->win;
366 e.x = c->x;
367 e.y = c->y;
368 e.width = c->w;
369 e.height = c->h;
370 e.border_width = c->border;
371 e.above = None;
372 e.override_redirect = False;
373 XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&e);
374 XFlush(dpy);
375 }
377 static int
378 dummy_error_handler(Display *dsply, XErrorEvent *err)
379 {
380 return 0;
381 }
383 void
384 unmanage(Client *c)
385 {
386 Client **l;
388 XGrabServer(dpy);
389 XSetErrorHandler(dummy_error_handler);
391 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
392 XDestroyWindow(dpy, c->title);
394 for(l=&clients; *l && *l != c; l=&(*l)->next);
395 *l = c->next;
396 for(l=&stack; *l && *l != c; l=&(*l)->snext);
397 *l = c->snext;
398 free(c);
400 XFlush(dpy);
401 XSetErrorHandler(error_handler);
402 XUngrabServer(dpy);
403 arrange();
404 if(stack)
405 focus(stack);
406 }
408 Client *
409 gettitle(Window w)
410 {
411 Client *c;
412 for(c = clients; c; c = c->next)
413 if(c->title == w)
414 return c;
415 return NULL;
416 }
418 Client *
419 getclient(Window w)
420 {
421 Client *c;
422 for(c = clients; c; c = c->next)
423 if(c->win == w)
424 return c;
425 return NULL;
426 }
428 void
429 draw_client(Client *c)
430 {
431 int i;
432 if(c == stack)
433 return;
435 dc.x = dc.y = 0;
436 dc.h = c->th;
438 dc.w = 0;
439 for(i = 0; i < TLast; i++) {
440 if(c->tags[i]) {
441 dc.x += dc.w;
442 dc.w = textw(c->tags[i]) + dc.font.height;
443 draw(True, c->tags[i]);
444 }
445 }
446 dc.x += dc.w;
447 dc.w = textw(c->name) + dc.font.height;
448 draw(True, c->name);
449 XCopyArea(dpy, dc.drawable, c->title, dc.gc,
450 0, 0, c->tw, c->th, 0, 0);
451 XFlush(dpy);
452 }