aewl

view client.c @ 99:a19556fe83b5

applied Sanders resize patch, fixed lower bug
author arg@10ksloc.org
date Wed, 19 Jul 2006 11:31:04 +0200
parents 5d88952cbf96
children bb3803fb560c
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->bw = 0;
20 for(i = 0; i < TLast; i++)
21 if(c->tags[i])
22 c->bw += textw(c->tags[i]);
23 c->bw += textw(c->name);
24 if(c->bw > *c->w)
25 c->bw = *c->w + 2;
26 c->bx = *c->x + *c->w - c->bw + 2;
27 c->by = *c->y;
28 XMoveResizeWindow(dpy, c->title, c->bx, c->by, c->bw, c->bh);
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->bx + 2 * sw, c->by);
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, tsel)))
70 c = getnext(clients, tsel);
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;
196 XSetWindowAttributes twa;
197 Window trans;
199 c = emallocz(sizeof(Client));
200 c->win = w;
201 c->bx = c->fx = c->tx = wa->x;
202 c->by = c->fy = c->ty = wa->y;
203 if(c->fy < bh)
204 c->by = c->fy = c->ty += bh;
205 c->bw = c->fw = c->tw = wa->width;
206 c->fh = c->th = wa->height;
207 c->bh = 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->bx, c->by, c->bw, c->bh,
219 0, DefaultDepth(dpy, screen), CopyFromParent,
220 DefaultVisual(dpy, screen),
221 CWOverrideRedirect | CWBackPixmap | CWEventMask, &twa);
223 settags(c);
225 c->next = clients;
226 clients = c;
228 XGrabButton(dpy, Button1, ControlMask, c->win, False, ButtonPressMask,
229 GrabModeAsync, GrabModeSync, None, None);
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->isfloat)
238 c->isfloat = trans
239 || ((c->maxw == c->minw) && (c->maxh == c->minh));
241 setgeom(c);
242 settitle(c);
244 arrange(NULL);
246 /* mapping the window now prevents flicker */
247 if(c->tags[tsel]) {
248 XMapRaised(dpy, c->win);
249 XMapRaised(dpy, c->title);
250 focus(c);
251 }
252 else {
253 ban(c);
254 XMapRaised(dpy, c->win);
255 XMapRaised(dpy, c->title);
256 XSync(dpy, False);
257 }
258 }
260 void
261 maximize(Arg *arg)
262 {
263 if(!sel)
264 return;
265 *sel->x = sx;
266 *sel->y = sy + bh;
267 *sel->w = sw - 2 * sel->border;
268 *sel->h = sh - 2 * sel->border - bh;
269 higher(sel);
270 resize(sel, False, TopLeft);
271 }
273 void
274 pop(Client *c)
275 {
276 Client **l;
277 for(l = &clients; *l && *l != c; l = &(*l)->next);
278 *l = c->next;
280 c->next = clients; /* pop */
281 clients = c;
282 arrange(NULL);
283 }
285 void
286 resize(Client *c, Bool inc, Corner sticky)
287 {
288 XConfigureEvent e;
289 int right = *c->x + *c->w;
290 int bottom = *c->y + *c->h;
292 if(inc) {
293 if(c->incw)
294 *c->w -= (*c->w - c->basew) % c->incw;
295 if(c->inch)
296 *c->h -= (*c->h - c->baseh) % c->inch;
297 }
298 if(*c->x > sw) /* might happen on restart */
299 *c->x = sw - *c->w;
300 if(*c->y > sh)
301 *c->y = sh - *c->h;
302 if(c->minw && *c->w < c->minw)
303 *c->w = c->minw;
304 if(c->minh && *c->h < c->minh)
305 *c->h = c->minh;
306 if(c->maxw && *c->w > c->maxw)
307 *c->w = c->maxw;
308 if(c->maxh && *c->h > c->maxh)
309 *c->h = c->maxh;
310 if(sticky == TopRight || sticky == BottomRight)
311 *c->x = right - *c->w;
312 if(sticky == BottomLeft || sticky == BottomRight)
313 *c->y = bottom - *c->h;
314 resizetitle(c);
315 XSetWindowBorderWidth(dpy, c->win, 1);
316 XMoveResizeWindow(dpy, c->win, *c->x, *c->y, *c->w, *c->h);
317 e.type = ConfigureNotify;
318 e.event = c->win;
319 e.window = c->win;
320 e.x = *c->x;
321 e.y = *c->y;
322 e.width = *c->w;
323 e.height = *c->h;
324 e.border_width = c->border;
325 e.above = None;
326 e.override_redirect = False;
327 XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&e);
328 XSync(dpy, False);
329 }
331 void
332 setgeom(Client *c)
333 {
334 if((arrange == dotile) && !c->isfloat) {
335 c->x = &c->tx;
336 c->y = &c->ty;
337 c->w = &c->tw;
338 c->h = &c->th;
339 }
340 else {
341 c->x = &c->fx;
342 c->y = &c->fy;
343 c->w = &c->fw;
344 c->h = &c->fh;
345 }
346 }
348 void
349 setsize(Client *c)
350 {
351 XSizeHints size;
352 long msize;
353 if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
354 size.flags = PSize;
355 c->flags = size.flags;
356 if(c->flags & PBaseSize) {
357 c->basew = size.base_width;
358 c->baseh = size.base_height;
359 }
360 else
361 c->basew = c->baseh = 0;
362 if(c->flags & PResizeInc) {
363 c->incw = size.width_inc;
364 c->inch = size.height_inc;
365 }
366 else
367 c->incw = c->inch = 0;
368 if(c->flags & PMaxSize) {
369 c->maxw = size.max_width;
370 c->maxh = size.max_height;
371 }
372 else
373 c->maxw = c->maxh = 0;
374 if(c->flags & PMinSize) {
375 c->minw = size.min_width;
376 c->minh = size.min_height;
377 }
378 else
379 c->minw = c->minh = 0;
380 if(c->flags & PWinGravity)
381 c->grav = size.win_gravity;
382 else
383 c->grav = NorthWestGravity;
384 }
386 void
387 settitle(Client *c)
388 {
389 XTextProperty name;
390 int n;
391 char **list = NULL;
393 name.nitems = 0;
394 c->name[0] = 0;
395 XGetTextProperty(dpy, c->win, &name, netatom[NetWMName]);
396 if(!name.nitems)
397 XGetWMName(dpy, c->win, &name);
398 if(!name.nitems)
399 return;
400 if(name.encoding == XA_STRING)
401 strncpy(c->name, (char *)name.value, sizeof(c->name));
402 else {
403 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
404 && n > 0 && *list)
405 {
406 strncpy(c->name, *list, sizeof(c->name));
407 XFreeStringList(list);
408 }
409 }
410 XFree(name.value);
411 resizetitle(c);
412 }
414 void
415 unmanage(Client *c)
416 {
417 Client **l;
419 XGrabServer(dpy);
420 XSetErrorHandler(xerrordummy);
422 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
423 XDestroyWindow(dpy, c->title);
425 for(l = &clients; *l && *l != c; l = &(*l)->next);
426 *l = c->next;
427 for(l = &clients; *l; l = &(*l)->next)
428 if((*l)->revert == c)
429 (*l)->revert = NULL;
430 if(sel == c)
431 sel = sel->revert ? sel->revert : clients;
433 free(c);
435 XSync(dpy, False);
436 XSetErrorHandler(xerror);
437 XUngrabServer(dpy);
438 arrange(NULL);
439 if(sel)
440 focus(sel);
441 }
443 void
444 zoom(Arg *arg)
445 {
446 Client *c;
448 if(!sel)
449 return;
451 if(sel == getnext(clients, tsel) && sel->next) {
452 if((c = getnext(sel->next, tsel)))
453 sel = c;
454 }
456 pop(sel);
457 focus(sel);
458 }