aewl

view client.c @ 201:124671c251ee

updated man page
author arg@10ksloc.org
date Mon, 07 Aug 2006 07:36:36 +0200
parents 955b4f217c2d
children f5b952e566da
line source
1 /*
2 * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
3 * See LICENSE file for license details.
4 */
5 #include "dwm.h"
6 #include <stdlib.h>
7 #include <string.h>
8 #include <X11/Xatom.h>
9 #include <X11/Xutil.h>
11 /* static functions */
13 static void
14 resizetitle(Client *c)
15 {
16 int i;
18 c->tw = 0;
19 for(i = 0; i < ntags; i++)
20 if(c->tags[i])
21 c->tw += textw(tags[i]);
22 c->tw += textw(c->name);
23 if(c->tw > c->w)
24 c->tw = c->w + 2;
25 c->tx = c->x + c->w - c->tw + 2;
26 c->ty = c->y;
27 if(c->tags[tsel])
28 XMoveResizeWindow(dpy, c->title, c->tx, c->ty, c->tw, c->th);
29 else
30 XMoveResizeWindow(dpy, c->title, c->tx + 2 * sw, c->ty, c->tw, c->th);
32 }
34 static int
35 xerrordummy(Display *dsply, XErrorEvent *ee)
36 {
37 return 0;
38 }
40 /* extern functions */
42 void
43 ban(Client *c)
44 {
45 XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
46 XMoveWindow(dpy, c->title, c->tx + 2 * sw, c->ty);
47 }
49 void
50 focus(Client *c)
51 {
52 Client *old = sel;
53 XEvent ev;
55 sel = c;
56 if(old && old != c)
57 drawtitle(old);
58 drawtitle(c);
59 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
60 XSync(dpy, False);
61 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
62 }
64 void
65 focusnext(Arg *arg)
66 {
67 Client *c;
69 if(!sel)
70 return;
72 if(sel->ismax)
73 togglemax(NULL);
75 if(!(c = getnext(sel->next)))
76 c = getnext(clients);
77 if(c) {
78 higher(c);
79 focus(c);
80 }
81 }
83 void
84 focusprev(Arg *arg)
85 {
86 Client *c;
88 if(!sel)
89 return;
91 if(sel->ismax)
92 togglemax(NULL);
94 if(!(c = getprev(sel->prev))) {
95 for(c = clients; c && c->next; c = c->next);
96 c = getprev(c);
97 }
98 if(c) {
99 higher(c);
100 focus(c);
101 }
102 }
104 Client *
105 getclient(Window w)
106 {
107 Client *c;
109 for(c = clients; c; c = c->next)
110 if(c->win == w)
111 return c;
112 return NULL;
113 }
115 Client *
116 getctitle(Window w)
117 {
118 Client *c;
120 for(c = clients; c; c = c->next)
121 if(c->title == w)
122 return c;
123 return NULL;
124 }
126 void
127 gravitate(Client *c, Bool invert)
128 {
129 int dx = 0, dy = 0;
131 switch(c->grav) {
132 default:
133 break;
134 case StaticGravity:
135 case NorthWestGravity:
136 case NorthGravity:
137 case NorthEastGravity:
138 dy = c->border;
139 break;
140 case EastGravity:
141 case CenterGravity:
142 case WestGravity:
143 dy = -(c->h / 2) + c->border;
144 break;
145 case SouthEastGravity:
146 case SouthGravity:
147 case SouthWestGravity:
148 dy = -(c->h);
149 break;
150 }
152 switch (c->grav) {
153 default:
154 break;
155 case StaticGravity:
156 case NorthWestGravity:
157 case WestGravity:
158 case SouthWestGravity:
159 dx = c->border;
160 break;
161 case NorthGravity:
162 case CenterGravity:
163 case SouthGravity:
164 dx = -(c->w / 2) + c->border;
165 break;
166 case NorthEastGravity:
167 case EastGravity:
168 case SouthEastGravity:
169 dx = -(c->w + c->border);
170 break;
171 }
173 if(invert) {
174 dx = -dx;
175 dy = -dy;
176 }
177 c->x += dx;
178 c->y += dy;
179 }
181 void
182 higher(Client *c)
183 {
184 XRaiseWindow(dpy, c->win);
185 XRaiseWindow(dpy, c->title);
186 }
188 void
189 killclient(Arg *arg)
190 {
191 if(!sel)
192 return;
193 if(sel->proto & PROTODELWIN)
194 sendevent(sel->win, wmatom[WMProtocols], wmatom[WMDelete]);
195 else
196 XKillClient(dpy, sel->win);
197 }
199 void
200 lower(Client *c)
201 {
202 XLowerWindow(dpy, c->title);
203 XLowerWindow(dpy, c->win);
204 }
206 void
207 manage(Window w, XWindowAttributes *wa)
208 {
209 Client *c;
210 Window trans;
211 XSetWindowAttributes twa;
213 c = emallocz(sizeof(Client));
214 c->tags = emallocz(ntags * sizeof(Bool));
215 c->win = w;
216 c->x = c->tx = wa->x;
217 c->y = c->ty = wa->y;
218 c->w = c->tw = wa->width;
219 c->h = wa->height;
220 c->th = bh;
222 c->border = 0;
223 setsize(c);
225 if(c->h != sh && c->y < bh)
226 c->y = c->ty = bh;
228 c->proto = getproto(c->win);
229 XSelectInput(dpy, c->win,
230 StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
231 XGetTransientForHint(dpy, c->win, &trans);
232 twa.override_redirect = 1;
233 twa.background_pixmap = ParentRelative;
234 twa.event_mask = ExposureMask | EnterWindowMask;
236 c->title = XCreateWindow(dpy, root, c->tx, c->ty, c->tw, c->th,
237 0, DefaultDepth(dpy, screen), CopyFromParent,
238 DefaultVisual(dpy, screen),
239 CWOverrideRedirect | CWBackPixmap | CWEventMask, &twa);
241 if(clients)
242 clients->prev = c;
243 c->next = clients;
244 clients = c;
246 XGrabButton(dpy, Button1, MODKEY, c->win, False, BUTTONMASK,
247 GrabModeAsync, GrabModeSync, None, None);
248 XGrabButton(dpy, Button1, MODKEY | LockMask, c->win, False, BUTTONMASK,
249 GrabModeAsync, GrabModeSync, None, None);
250 XGrabButton(dpy, Button1, MODKEY | NUMLOCKMASK, c->win, False, BUTTONMASK,
251 GrabModeAsync, GrabModeSync, None, None);
252 XGrabButton(dpy, Button1, MODKEY | NUMLOCKMASK | LockMask, c->win, False, BUTTONMASK,
253 GrabModeAsync, GrabModeSync, None, None);
255 XGrabButton(dpy, Button2, MODKEY, c->win, False, BUTTONMASK,
256 GrabModeAsync, GrabModeSync, None, None);
257 XGrabButton(dpy, Button2, MODKEY | LockMask, c->win, False, BUTTONMASK,
258 GrabModeAsync, GrabModeSync, None, None);
259 XGrabButton(dpy, Button2, MODKEY | NUMLOCKMASK, c->win, False, BUTTONMASK,
260 GrabModeAsync, GrabModeSync, None, None);
261 XGrabButton(dpy, Button2, MODKEY | NUMLOCKMASK | LockMask, c->win, False, BUTTONMASK,
262 GrabModeAsync, GrabModeSync, None, None);
264 XGrabButton(dpy, Button3, MODKEY, c->win, False, BUTTONMASK,
265 GrabModeAsync, GrabModeSync, None, None);
266 XGrabButton(dpy, Button3, MODKEY | LockMask, c->win, False, BUTTONMASK,
267 GrabModeAsync, GrabModeSync, None, None);
268 XGrabButton(dpy, Button3, MODKEY | NUMLOCKMASK, c->win, False, BUTTONMASK,
269 GrabModeAsync, GrabModeSync, None, None);
270 XGrabButton(dpy, Button3, MODKEY | NUMLOCKMASK | LockMask, c->win, False, BUTTONMASK,
271 GrabModeAsync, GrabModeSync, None, None);
273 settags(c);
274 if(!c->isfloat)
275 c->isfloat = trans
276 || (c->maxw && c->minw &&
277 c->maxw == c->minw && c->maxh == c->minh);
278 settitle(c);
279 arrange(NULL);
281 /* mapping the window now prevents flicker */
282 XMapRaised(dpy, c->win);
283 XMapRaised(dpy, c->title);
284 if(c->tags[tsel])
285 focus(c);
286 }
288 void
289 resize(Client *c, Bool sizehints, Corner sticky)
290 {
291 int bottom = c->y + c->h;
292 int right = c->x + c->w;
293 /*XConfigureEvent e;*/
294 XWindowChanges wc;
296 if(sizehints) {
297 if(c->incw)
298 c->w -= (c->w - c->basew) % c->incw;
299 if(c->inch)
300 c->h -= (c->h - c->baseh) % c->inch;
301 if(c->minw && c->w < c->minw)
302 c->w = c->minw;
303 if(c->minh && c->h < c->minh)
304 c->h = c->minh;
305 if(c->maxw && c->w > c->maxw)
306 c->w = c->maxw;
307 if(c->maxh && c->h > c->maxh)
308 c->h = c->maxh;
309 }
310 if(c->x > right) /* might happen on restart */
311 c->x = right - c->w;
312 if(c->y > bottom)
313 c->y = bottom - c->h;
314 if(sticky == TopRight || sticky == BotRight)
315 c->x = right - c->w;
316 if(sticky == BotLeft || sticky == BotRight)
317 c->y = bottom - c->h;
319 resizetitle(c);
320 wc.x = c->x;
321 wc.y = c->y;
322 wc.width = c->w;
323 wc.height = c->h;
324 if(c->w == sw && c->h == sh)
325 wc.border_width = 0;
326 else
327 wc.border_width = 1;
328 XConfigureWindow(dpy, c->win, CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
329 XSync(dpy, False);
330 }
332 void
333 setsize(Client *c)
334 {
335 long msize;
336 XSizeHints size;
338 if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
339 size.flags = PSize;
340 c->flags = size.flags;
341 if(c->flags & PBaseSize) {
342 c->basew = size.base_width;
343 c->baseh = size.base_height;
344 }
345 else
346 c->basew = c->baseh = 0;
347 if(c->flags & PResizeInc) {
348 c->incw = size.width_inc;
349 c->inch = size.height_inc;
350 }
351 else
352 c->incw = c->inch = 0;
353 if(c->flags & PMaxSize) {
354 c->maxw = size.max_width;
355 c->maxh = size.max_height;
356 }
357 else
358 c->maxw = c->maxh = 0;
359 if(c->flags & PMinSize) {
360 c->minw = size.min_width;
361 c->minh = size.min_height;
362 }
363 else
364 c->minw = c->minh = 0;
365 if(c->flags & PWinGravity)
366 c->grav = size.win_gravity;
367 else
368 c->grav = NorthWestGravity;
369 }
371 void
372 settitle(Client *c)
373 {
374 char **list = NULL;
375 int n;
376 XTextProperty name;
378 name.nitems = 0;
379 c->name[0] = 0;
380 XGetTextProperty(dpy, c->win, &name, netatom[NetWMName]);
381 if(!name.nitems)
382 XGetWMName(dpy, c->win, &name);
383 if(!name.nitems)
384 return;
385 if(name.encoding == XA_STRING)
386 strncpy(c->name, (char *)name.value, sizeof(c->name));
387 else {
388 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
389 && n > 0 && *list)
390 {
391 strncpy(c->name, *list, sizeof(c->name));
392 XFreeStringList(list);
393 }
394 }
395 XFree(name.value);
396 resizetitle(c);
397 }
399 void
400 togglemax(Arg *arg)
401 {
402 int ox, oy, ow, oh;
403 XEvent ev;
405 if(!sel)
406 return;
408 if((sel->ismax = !sel->ismax)) {
409 ox = sel->x;
410 oy = sel->y;
411 ow = sel->w;
412 oh = sel->h;
413 sel->x = sx;
414 sel->y = sy + bh;
415 sel->w = sw - 2;
416 sel->h = sh - 2 - bh;
418 higher(sel);
419 resize(sel, False, TopLeft);
421 sel->x = ox;
422 sel->y = oy;
423 sel->w = ow;
424 sel->h = oh;
425 }
426 else
427 resize(sel, False, TopLeft);
428 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
429 }
431 void
432 unmanage(Client *c)
433 {
434 XGrabServer(dpy);
435 XSetErrorHandler(xerrordummy);
437 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
438 XDestroyWindow(dpy, c->title);
440 if(c->prev)
441 c->prev->next = c->next;
442 if(c->next)
443 c->next->prev = c->prev;
444 if(c == clients)
445 clients = c->next;
446 if(sel == c) {
447 sel = getnext(c->next);
448 if(!sel)
449 sel = getprev(c->prev);
450 if(!sel)
451 sel = clients;
452 }
453 free(c->tags);
454 free(c);
456 XSync(dpy, False);
457 XSetErrorHandler(xerror);
458 XUngrabServer(dpy);
459 arrange(NULL);
460 if(sel)
461 focus(sel);
462 }
464 void
465 zoom(Arg *arg)
466 {
467 Client *c;
469 if(!sel || (arrange != dotile) || sel->isfloat)
470 return;
472 if(sel == getnext(clients) && sel->next) {
473 if((c = getnext(sel->next)))
474 sel = c;
475 }
477 /* pop */
478 if(sel->prev)
479 sel->prev->next = sel->next;
480 if(sel->next)
481 sel->next->prev = sel->prev;
482 sel->prev = NULL;
483 if(clients)
484 clients->prev = sel;
485 sel->next = clients;
486 clients = sel;
487 arrange(NULL);
488 focus(sel);
489 }