aewl

view client.c @ 79:aabebd6e61f3

fixed XSync handling and finished man page
author Anselm R. Garbe <garbeam@wmii.de>
date Sat, 15 Jul 2006 18:51:44 +0200
parents 38c8f7f7d401
children 8125f908c80c
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"
7 #include <stdlib.h>
8 #include <string.h>
9 #include <X11/Xatom.h>
10 #include <X11/Xutil.h>
12 /* static functions */
14 static void
15 resizetitle(Client *c)
16 {
17 int i;
19 c->tw = 0;
20 for(i = 0; i < TLast; i++)
21 if(c->tags[i])
22 c->tw += textw(c->tags[i]);
23 c->tw += textw(c->name);
24 if(c->tw > c->w)
25 c->tw = c->w + 2;
26 c->tx = c->x + c->w - c->tw + 2;
27 c->ty = c->y;
28 XMoveResizeWindow(dpy, c->title, c->tx, c->ty, c->tw, c->th);
29 }
31 static int
32 xerrordummy(Display *dsply, XErrorEvent *ee)
33 {
34 return 0;
35 }
37 /* extern functions */
39 void
40 ban(Client *c)
41 {
42 XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
43 XMoveWindow(dpy, c->title, c->tx + 2 * sw, c->ty);
44 }
46 void
47 focus(Client *c)
48 {
49 Client *old = sel;
50 XEvent ev;
52 sel = c;
53 if(old && old != c)
54 drawtitle(old);
55 drawtitle(c);
56 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
57 XSync(dpy, False);
58 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
59 }
61 void
62 focusnext(Arg *arg)
63 {
64 Client *c;
66 if(!sel)
67 return;
69 if(!(c = getnext(sel->next)))
70 c = getnext(clients);
71 if(c) {
72 higher(c);
73 c->revert = sel;
74 focus(c);
75 }
76 }
78 void
79 focusprev(Arg *arg)
80 {
81 Client *c;
83 if(!sel)
84 return;
86 if((c = sel->revert && sel->revert->tags[tsel] ? sel->revert : NULL)) {
87 higher(c);
88 focus(c);
89 }
90 }
92 Client *
93 getclient(Window w)
94 {
95 Client *c;
96 for(c = clients; c; c = c->next)
97 if(c->win == w)
98 return c;
99 return NULL;
100 }
102 Client *
103 getctitle(Window w)
104 {
105 Client *c;
106 for(c = clients; c; c = c->next)
107 if(c->title == w)
108 return c;
109 return NULL;
110 }
112 void
113 gravitate(Client *c, Bool invert)
114 {
115 int dx = 0, dy = 0;
117 switch(c->grav) {
118 case StaticGravity:
119 case NorthWestGravity:
120 case NorthGravity:
121 case NorthEastGravity:
122 dy = c->border;
123 break;
124 case EastGravity:
125 case CenterGravity:
126 case WestGravity:
127 dy = -(c->h / 2) + c->border;
128 break;
129 case SouthEastGravity:
130 case SouthGravity:
131 case SouthWestGravity:
132 dy = -c->h;
133 break;
134 default:
135 break;
136 }
138 switch (c->grav) {
139 case StaticGravity:
140 case NorthWestGravity:
141 case WestGravity:
142 case SouthWestGravity:
143 dx = c->border;
144 break;
145 case NorthGravity:
146 case CenterGravity:
147 case SouthGravity:
148 dx = -(c->w / 2) + c->border;
149 break;
150 case NorthEastGravity:
151 case EastGravity:
152 case SouthEastGravity:
153 dx = -(c->w + c->border);
154 break;
155 default:
156 break;
157 }
159 if(invert) {
160 dx = -dx;
161 dy = -dy;
162 }
163 c->x += dx;
164 c->y += dy;
165 }
167 void
168 higher(Client *c)
169 {
170 XRaiseWindow(dpy, c->win);
171 XRaiseWindow(dpy, c->title);
172 }
174 void
175 killclient(Arg *arg)
176 {
177 if(!sel)
178 return;
179 if(sel->proto & WM_PROTOCOL_DELWIN)
180 sendevent(sel->win, wmatom[WMProtocols], wmatom[WMDelete]);
181 else
182 XKillClient(dpy, sel->win);
183 }
185 void
186 lower(Client *c)
187 {
188 XLowerWindow(dpy, c->title);
189 XLowerWindow(dpy, c->win);
190 }
192 void
193 manage(Window w, XWindowAttributes *wa)
194 {
195 Client *c, **l;
196 XSetWindowAttributes twa;
197 Window trans;
199 c = emallocz(sizeof(Client));
200 c->win = w;
201 c->tx = c->x = wa->x;
202 c->ty = c->y = wa->y;
203 if(c->y < bh)
204 c->ty = c->y += bh;
205 c->tw = c->w = wa->width;
206 c->h = wa->height;
207 c->th = bh;
208 c->border = 1;
209 c->proto = getproto(c->win);
210 setsize(c);
211 XSelectInput(dpy, c->win,
212 StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
213 XGetTransientForHint(dpy, c->win, &trans);
214 twa.override_redirect = 1;
215 twa.background_pixmap = ParentRelative;
216 twa.event_mask = ExposureMask;
218 c->title = XCreateWindow(dpy, root, c->tx, c->ty, c->tw, c->th,
219 0, DefaultDepth(dpy, screen), CopyFromParent,
220 DefaultVisual(dpy, screen),
221 CWOverrideRedirect | CWBackPixmap | CWEventMask, &twa);
223 settitle(c);
224 settags(c);
226 for(l = &clients; *l; l = &(*l)->next);
227 c->next = *l; /* *l == nil */
228 *l = c;
230 XGrabButton(dpy, Button1, Mod1Mask, c->win, False, ButtonPressMask,
231 GrabModeAsync, GrabModeSync, None, None);
232 XGrabButton(dpy, Button2, Mod1Mask, c->win, False, ButtonPressMask,
233 GrabModeAsync, GrabModeSync, None, None);
234 XGrabButton(dpy, Button3, Mod1Mask, c->win, False, ButtonPressMask,
235 GrabModeAsync, GrabModeSync, None, None);
237 if(!c->dofloat)
238 c->dofloat = trans
239 || ((c->maxw == c->minw) && (c->maxh == c->minh));
241 arrange(NULL);
242 /* mapping the window now prevents flicker */
243 if(c->tags[tsel]) {
244 XMapRaised(dpy, c->win);
245 XMapRaised(dpy, c->title);
246 focus(c);
247 }
248 else {
249 ban(c);
250 XMapRaised(dpy, c->win);
251 XMapRaised(dpy, c->title);
252 }
253 }
255 void
256 maximize(Arg *arg)
257 {
258 if(!sel)
259 return;
260 sel->x = sx;
261 sel->y = sy + bh;
262 sel->w = sw - 2 * sel->border;
263 sel->h = sh - 2 * sel->border - bh;
264 higher(sel);
265 resize(sel, False);
266 }
268 void
269 resize(Client *c, Bool inc)
270 {
271 XConfigureEvent e;
273 if(inc) {
274 if(c->incw)
275 c->w -= (c->w - c->basew) % c->incw;
276 if(c->inch)
277 c->h -= (c->h - c->baseh) % c->inch;
278 }
279 if(c->x > sw) /* might happen on restart */
280 c->x = sw - c->w;
281 if(c->y > sh)
282 c->ty = c->y = sh - c->h;
283 if(c->minw && c->w < c->minw)
284 c->w = c->minw;
285 if(c->minh && c->h < c->minh)
286 c->h = c->minh;
287 if(c->maxw && c->w > c->maxw)
288 c->w = c->maxw;
289 if(c->maxh && c->h > c->maxh)
290 c->h = c->maxh;
291 resizetitle(c);
292 XSetWindowBorderWidth(dpy, c->win, 1);
293 XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
294 e.type = ConfigureNotify;
295 e.event = c->win;
296 e.window = c->win;
297 e.x = c->x;
298 e.y = c->y;
299 e.width = c->w;
300 e.height = c->h;
301 e.border_width = c->border;
302 e.above = None;
303 e.override_redirect = False;
304 XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&e);
305 XSync(dpy, False);
306 }
308 void
309 setsize(Client *c)
310 {
311 XSizeHints size;
312 long msize;
313 if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
314 size.flags = PSize;
315 c->flags = size.flags;
316 if(c->flags & PBaseSize) {
317 c->basew = size.base_width;
318 c->baseh = size.base_height;
319 }
320 else
321 c->basew = c->baseh = 0;
322 if(c->flags & PResizeInc) {
323 c->incw = size.width_inc;
324 c->inch = size.height_inc;
325 }
326 else
327 c->incw = c->inch = 0;
328 if(c->flags & PMaxSize) {
329 c->maxw = size.max_width;
330 c->maxh = size.max_height;
331 }
332 else
333 c->maxw = c->maxh = 0;
334 if(c->flags & PMinSize) {
335 c->minw = size.min_width;
336 c->minh = size.min_height;
337 }
338 else
339 c->minw = c->minh = 0;
340 if(c->flags & PWinGravity)
341 c->grav = size.win_gravity;
342 else
343 c->grav = NorthWestGravity;
344 }
346 void
347 settitle(Client *c)
348 {
349 XTextProperty name;
350 int n;
351 char **list = NULL;
353 name.nitems = 0;
354 c->name[0] = 0;
355 XGetTextProperty(dpy, c->win, &name, netatom[NetWMName]);
356 if(!name.nitems)
357 XGetWMName(dpy, c->win, &name);
358 if(!name.nitems)
359 return;
360 if(name.encoding == XA_STRING)
361 strncpy(c->name, (char *)name.value, sizeof(c->name));
362 else {
363 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
364 && n > 0 && *list)
365 {
366 strncpy(c->name, *list, sizeof(c->name));
367 XFreeStringList(list);
368 }
369 }
370 XFree(name.value);
371 resizetitle(c);
372 }
374 void
375 unmanage(Client *c)
376 {
377 Client **l;
379 XGrabServer(dpy);
380 XSetErrorHandler(xerrordummy);
382 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
383 XDestroyWindow(dpy, c->title);
385 for(l = &clients; *l && *l != c; l = &(*l)->next);
386 *l = c->next;
387 for(l = &clients; *l; l = &(*l)->next)
388 if((*l)->revert == c)
389 (*l)->revert = NULL;
390 if(sel == c)
391 sel = sel->revert ? sel->revert : clients;
393 free(c);
395 XSync(dpy, False);
396 XSetErrorHandler(xerror);
397 XUngrabServer(dpy);
398 arrange(NULL);
399 if(sel)
400 focus(sel);
401 }
403 void
404 zoom(Arg *arg)
405 {
406 Client **l, *c;
408 if(!sel)
409 return;
411 if(sel == getnext(clients) && sel->next) {
412 if((c = getnext(sel->next)))
413 sel = c;
414 }
416 for(l = &clients; *l && *l != sel; l = &(*l)->next);
417 *l = sel->next;
419 sel->next = clients; /* pop */
420 clients = sel;
421 arrange(NULL);
422 focus(sel);
423 }