aewl

view client.c @ 314:8bafc3ac9f58

made a new client position strategy similiar to that one proposed by Sander, but which takes top bar into account
author Anselm R.Garbe <arg@10ksloc.org>
date Fri, 18 Aug 2006 08:39:33 +0200
parents 8b109976c594
children d9bef4067cd5
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(isvisible(c))
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;
54 if (!issel)
55 return;
56 if(sel && sel->ismax)
57 togglemax(NULL);
58 sel = c;
59 if(old && old != c)
60 drawtitle(old);
61 drawtitle(c);
62 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
63 }
65 void
66 focusnext(Arg *arg)
67 {
68 Client *c;
70 if(!sel)
71 return;
73 if(!(c = getnext(sel->next)))
74 c = getnext(clients);
75 if(c) {
76 focus(c);
77 restack();
78 }
79 }
81 void
82 focusprev(Arg *arg)
83 {
84 Client *c;
86 if(!sel)
87 return;
89 if(!(c = getprev(sel->prev))) {
90 for(c = clients; c && c->next; c = c->next);
91 c = getprev(c);
92 }
93 if(c) {
94 focus(c);
95 restack();
96 }
97 }
99 Client *
100 getclient(Window w)
101 {
102 Client *c;
104 for(c = clients; c; c = c->next)
105 if(c->win == w)
106 return c;
107 return NULL;
108 }
110 Client *
111 getctitle(Window w)
112 {
113 Client *c;
115 for(c = clients; c; c = c->next)
116 if(c->title == w)
117 return c;
118 return NULL;
119 }
121 void
122 gravitate(Client *c, Bool invert)
123 {
124 int dx = 0, dy = 0;
126 switch(c->grav) {
127 default:
128 break;
129 case StaticGravity:
130 case NorthWestGravity:
131 case NorthGravity:
132 case NorthEastGravity:
133 dy = c->border;
134 break;
135 case EastGravity:
136 case CenterGravity:
137 case WestGravity:
138 dy = -(c->h / 2) + c->border;
139 break;
140 case SouthEastGravity:
141 case SouthGravity:
142 case SouthWestGravity:
143 dy = -(c->h);
144 break;
145 }
147 switch (c->grav) {
148 default:
149 break;
150 case StaticGravity:
151 case NorthWestGravity:
152 case WestGravity:
153 case SouthWestGravity:
154 dx = c->border;
155 break;
156 case NorthGravity:
157 case CenterGravity:
158 case SouthGravity:
159 dx = -(c->w / 2) + c->border;
160 break;
161 case NorthEastGravity:
162 case EastGravity:
163 case SouthEastGravity:
164 dx = -(c->w + c->border);
165 break;
166 }
168 if(invert) {
169 dx = -dx;
170 dy = -dy;
171 }
172 c->x += dx;
173 c->y += dy;
174 }
176 void
177 killclient(Arg *arg)
178 {
179 if(!sel)
180 return;
181 if(sel->proto & PROTODELWIN)
182 sendevent(sel->win, wmatom[WMProtocols], wmatom[WMDelete]);
183 else
184 XKillClient(dpy, sel->win);
185 }
187 void
188 manage(Window w, XWindowAttributes *wa)
189 {
190 Client *c, *tc;
191 Window trans;
192 XSetWindowAttributes twa;
194 c = emallocz(sizeof(Client));
195 c->tags = emallocz(ntags * sizeof(Bool));
196 c->win = w;
197 c->x = c->tx = wa->x;
198 c->y = c->ty = wa->y;
199 c->w = c->tw = wa->width;
200 c->h = wa->height;
201 c->th = bh;
203 c->border = 0;
204 setsize(c);
206 if(c->x + c->w > sw)
207 c->x = sw - c->w - 2;
208 if(c->x < 0)
209 c->x = 0;
210 if(c->y + c->h > sh)
211 c->y = sh - c->h - 2;
212 if(c->h != sh && c->y < bh)
213 c->y = bh;
215 c->proto = getproto(c->win);
216 XSelectInput(dpy, c->win,
217 StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
218 XGetTransientForHint(dpy, c->win, &trans);
219 twa.override_redirect = 1;
220 twa.background_pixmap = ParentRelative;
221 twa.event_mask = ExposureMask | EnterWindowMask;
223 c->title = XCreateWindow(dpy, root, c->tx, c->ty, c->tw, c->th,
224 0, DefaultDepth(dpy, screen), CopyFromParent,
225 DefaultVisual(dpy, screen),
226 CWOverrideRedirect | CWBackPixmap | CWEventMask, &twa);
228 if(clients)
229 clients->prev = c;
230 c->next = clients;
231 clients = c;
233 XGrabButton(dpy, Button1, MODKEY, c->win, False, BUTTONMASK,
234 GrabModeAsync, GrabModeSync, None, None);
235 XGrabButton(dpy, Button1, MODKEY | LockMask, c->win, False, BUTTONMASK,
236 GrabModeAsync, GrabModeSync, None, None);
237 XGrabButton(dpy, Button1, MODKEY | numlockmask, c->win, False, BUTTONMASK,
238 GrabModeAsync, GrabModeSync, None, None);
239 XGrabButton(dpy, Button1, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
240 GrabModeAsync, GrabModeSync, None, None);
242 XGrabButton(dpy, Button2, MODKEY, c->win, False, BUTTONMASK,
243 GrabModeAsync, GrabModeSync, None, None);
244 XGrabButton(dpy, Button2, MODKEY | LockMask, c->win, False, BUTTONMASK,
245 GrabModeAsync, GrabModeSync, None, None);
246 XGrabButton(dpy, Button2, MODKEY | numlockmask, c->win, False, BUTTONMASK,
247 GrabModeAsync, GrabModeSync, None, None);
248 XGrabButton(dpy, Button2, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
249 GrabModeAsync, GrabModeSync, None, None);
251 XGrabButton(dpy, Button3, MODKEY, c->win, False, BUTTONMASK,
252 GrabModeAsync, GrabModeSync, None, None);
253 XGrabButton(dpy, Button3, MODKEY | LockMask, c->win, False, BUTTONMASK,
254 GrabModeAsync, GrabModeSync, None, None);
255 XGrabButton(dpy, Button3, MODKEY | numlockmask, c->win, False, BUTTONMASK,
256 GrabModeAsync, GrabModeSync, None, None);
257 XGrabButton(dpy, Button3, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
258 GrabModeAsync, GrabModeSync, None, None);
260 settags(c);
261 if(!c->isfloat)
262 c->isfloat = trans
263 || (c->maxw && c->minw &&
264 c->maxw == c->minw && c->maxh == c->minh);
265 settitle(c);
266 if(isvisible(c))
267 sel = c;
268 arrange(NULL);
269 XMapWindow(dpy, c->win);
270 XMapWindow(dpy, c->title);
271 if(isvisible(c))
272 focus(c);
273 }
275 void
276 resize(Client *c, Bool sizehints, Corner sticky)
277 {
278 int bottom = c->y + c->h;
279 int right = c->x + c->w;
280 XWindowChanges wc;
282 if(sizehints) {
283 if(c->incw)
284 c->w -= (c->w - c->basew) % c->incw;
285 if(c->inch)
286 c->h -= (c->h - c->baseh) % c->inch;
287 if(c->minw && c->w < c->minw)
288 c->w = c->minw;
289 if(c->minh && c->h < c->minh)
290 c->h = c->minh;
291 if(c->maxw && c->w > c->maxw)
292 c->w = c->maxw;
293 if(c->maxh && c->h > c->maxh)
294 c->h = c->maxh;
295 }
296 if(c->x > sw) /* might happen on restart */
297 c->x = sw - c->w;
298 if(c->y > sh)
299 c->y = sh - c->h;
300 if(sticky == TopRight || sticky == BotRight)
301 c->x = right - c->w;
302 if(sticky == BotLeft || sticky == BotRight)
303 c->y = bottom - c->h;
305 resizetitle(c);
306 wc.x = c->x;
307 wc.y = c->y;
308 wc.width = c->w;
309 wc.height = c->h;
310 if(c->w == sw && c->h == sh)
311 wc.border_width = 0;
312 else
313 wc.border_width = 1;
314 XConfigureWindow(dpy, c->win, CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
315 XSync(dpy, False);
316 }
318 void
319 setsize(Client *c)
320 {
321 long msize;
322 XSizeHints size;
324 if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
325 size.flags = PSize;
326 c->flags = size.flags;
327 if(c->flags & PBaseSize) {
328 c->basew = size.base_width;
329 c->baseh = size.base_height;
330 }
331 else
332 c->basew = c->baseh = 0;
333 if(c->flags & PResizeInc) {
334 c->incw = size.width_inc;
335 c->inch = size.height_inc;
336 }
337 else
338 c->incw = c->inch = 0;
339 if(c->flags & PMaxSize) {
340 c->maxw = size.max_width;
341 c->maxh = size.max_height;
342 }
343 else
344 c->maxw = c->maxh = 0;
345 if(c->flags & PMinSize) {
346 c->minw = size.min_width;
347 c->minh = size.min_height;
348 }
349 else
350 c->minw = c->minh = 0;
351 if(c->flags & PWinGravity)
352 c->grav = size.win_gravity;
353 else
354 c->grav = NorthWestGravity;
355 }
357 void
358 settitle(Client *c)
359 {
360 char **list = NULL;
361 int n;
362 XTextProperty name;
364 name.nitems = 0;
365 c->name[0] = 0;
366 XGetTextProperty(dpy, c->win, &name, netatom[NetWMName]);
367 if(!name.nitems)
368 XGetWMName(dpy, c->win, &name);
369 if(!name.nitems)
370 return;
371 if(name.encoding == XA_STRING)
372 strncpy(c->name, (char *)name.value, sizeof(c->name));
373 else {
374 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
375 && n > 0 && *list)
376 {
377 strncpy(c->name, *list, sizeof(c->name));
378 XFreeStringList(list);
379 }
380 }
381 XFree(name.value);
382 resizetitle(c);
383 }
385 void
386 togglemax(Arg *arg)
387 {
388 int ox, oy, ow, oh;
389 XEvent ev;
391 if(!sel)
392 return;
394 if((sel->ismax = !sel->ismax)) {
395 ox = sel->x;
396 oy = sel->y;
397 ow = sel->w;
398 oh = sel->h;
399 sel->x = sx;
400 sel->y = sy + bh;
401 sel->w = sw - 2;
402 sel->h = sh - 2 - bh;
404 restack();
405 resize(sel, arrange == dofloat, TopLeft);
407 sel->x = ox;
408 sel->y = oy;
409 sel->w = ow;
410 sel->h = oh;
411 }
412 else
413 resize(sel, False, TopLeft);
414 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
415 }
417 void
418 unmanage(Client *c)
419 {
420 XGrabServer(dpy);
421 XSetErrorHandler(xerrordummy);
423 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
424 XDestroyWindow(dpy, c->title);
426 if(c->prev)
427 c->prev->next = c->next;
428 if(c->next)
429 c->next->prev = c->prev;
430 if(c == clients)
431 clients = c->next;
432 if(sel == c)
433 sel = getnext(clients);
434 free(c->tags);
435 free(c);
437 XSync(dpy, False);
438 XSetErrorHandler(xerror);
439 XUngrabServer(dpy);
440 if(sel)
441 focus(sel);
442 arrange(NULL);
443 }
445 void
446 zoom(Arg *arg)
447 {
448 Client *c;
450 if(!sel || (arrange != dotile) || sel->isfloat || sel->ismax)
451 return;
453 if(sel == getnext(clients)) {
454 if((c = getnext(sel->next)))
455 sel = c;
456 else
457 return;
458 }
460 /* pop */
461 sel->prev->next = sel->next;
462 if(sel->next)
463 sel->next->prev = sel->prev;
464 sel->prev = NULL;
465 clients->prev = sel;
466 sel->next = clients;
467 clients = sel;
468 focus(sel);
469 arrange(NULL);
470 }