dwm-meillo
view client.c @ 318:1b45d6f14fca
applied Sanders focus_* patches, removed the unnecessary clean-prefix from the new function names
author | Anselm R.Garbe <arg@10ksloc.org> |
---|---|
date | Mon, 21 Aug 2006 09:03:14 +0200 |
parents | d9bef4067cd5 |
children | 94d2d7658673 |
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 grabbutton(Client *c, unsigned int button, unsigned int modifier)
15 {
16 XGrabButton(dpy, button, modifier, c->win, False, BUTTONMASK,
17 GrabModeAsync, GrabModeSync, None, None);
18 XGrabButton(dpy, button, modifier | LockMask, c->win, False, BUTTONMASK,
19 GrabModeAsync, GrabModeSync, None, None);
20 XGrabButton(dpy, button, modifier | numlockmask, c->win, False, BUTTONMASK,
21 GrabModeAsync, GrabModeSync, None, None);
22 XGrabButton(dpy, button, modifier | numlockmask | LockMask, c->win, False, BUTTONMASK,
23 GrabModeAsync, GrabModeSync, None, None);
24 }
26 static void
27 resizetitle(Client *c)
28 {
29 int i;
31 c->tw = 0;
32 for(i = 0; i < ntags; i++)
33 if(c->tags[i])
34 c->tw += textw(tags[i]);
35 c->tw += textw(c->name);
36 if(c->tw > c->w)
37 c->tw = c->w + 2;
38 c->tx = c->x + c->w - c->tw + 2;
39 c->ty = c->y;
40 if(isvisible(c))
41 XMoveResizeWindow(dpy, c->title, c->tx, c->ty, c->tw, c->th);
42 else
43 XMoveResizeWindow(dpy, c->title, c->tx + 2 * sw, c->ty, c->tw, c->th);
45 }
47 static void
48 ungrabbutton(Client *c, unsigned int button, unsigned int modifier)
49 {
50 XUngrabButton(dpy, button, modifier, c->win);
51 XUngrabButton(dpy, button, modifier | LockMask, c->win);
52 XUngrabButton(dpy, button, modifier | numlockmask, c->win);
53 XUngrabButton(dpy, button, modifier | numlockmask | LockMask, c->win);
54 }
56 static int
57 xerrordummy(Display *dsply, XErrorEvent *ee)
58 {
59 return 0;
60 }
62 /* extern functions */
64 void
65 ban(Client *c)
66 {
67 XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
68 XMoveWindow(dpy, c->title, c->tx + 2 * sw, c->ty);
69 }
71 void
72 focus(Client *c)
73 {
74 Client *old = sel;
76 if (!issel)
77 return;
78 if(sel && sel->ismax && sel != c)
79 togglemax(NULL);
80 sel = c;
81 if(old && old != c) {
82 grabbutton(old, AnyButton, 0);
83 drawtitle(old);
84 }
85 ungrabbutton(c, AnyButton, 0);
86 drawtitle(c);
87 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
88 }
90 void
91 focusnext(Arg *arg)
92 {
93 Client *c;
95 if(!sel)
96 return;
98 if(!(c = getnext(sel->next)))
99 c = getnext(clients);
100 if(c) {
101 focus(c);
102 restack();
103 }
104 }
106 void
107 focusprev(Arg *arg)
108 {
109 Client *c;
111 if(!sel)
112 return;
114 if(!(c = getprev(sel->prev))) {
115 for(c = clients; c && c->next; c = c->next);
116 c = getprev(c);
117 }
118 if(c) {
119 focus(c);
120 restack();
121 }
122 }
124 Client *
125 getclient(Window w)
126 {
127 Client *c;
129 for(c = clients; c; c = c->next)
130 if(c->win == w)
131 return c;
132 return NULL;
133 }
135 Client *
136 getctitle(Window w)
137 {
138 Client *c;
140 for(c = clients; c; c = c->next)
141 if(c->title == w)
142 return c;
143 return NULL;
144 }
146 void
147 gravitate(Client *c, Bool invert)
148 {
149 int dx = 0, dy = 0;
151 switch(c->grav) {
152 default:
153 break;
154 case StaticGravity:
155 case NorthWestGravity:
156 case NorthGravity:
157 case NorthEastGravity:
158 dy = c->border;
159 break;
160 case EastGravity:
161 case CenterGravity:
162 case WestGravity:
163 dy = -(c->h / 2) + c->border;
164 break;
165 case SouthEastGravity:
166 case SouthGravity:
167 case SouthWestGravity:
168 dy = -(c->h);
169 break;
170 }
172 switch (c->grav) {
173 default:
174 break;
175 case StaticGravity:
176 case NorthWestGravity:
177 case WestGravity:
178 case SouthWestGravity:
179 dx = c->border;
180 break;
181 case NorthGravity:
182 case CenterGravity:
183 case SouthGravity:
184 dx = -(c->w / 2) + c->border;
185 break;
186 case NorthEastGravity:
187 case EastGravity:
188 case SouthEastGravity:
189 dx = -(c->w + c->border);
190 break;
191 }
193 if(invert) {
194 dx = -dx;
195 dy = -dy;
196 }
197 c->x += dx;
198 c->y += dy;
199 }
201 void
202 killclient(Arg *arg)
203 {
204 if(!sel)
205 return;
206 if(sel->proto & PROTODELWIN)
207 sendevent(sel->win, wmatom[WMProtocols], wmatom[WMDelete]);
208 else
209 XKillClient(dpy, sel->win);
210 }
212 void
213 manage(Window w, XWindowAttributes *wa)
214 {
215 Client *c, *tc;
216 Window trans;
217 XSetWindowAttributes twa;
219 c = emallocz(sizeof(Client));
220 c->tags = emallocz(ntags * sizeof(Bool));
221 c->win = w;
222 c->x = c->tx = wa->x;
223 c->y = c->ty = wa->y;
224 c->w = c->tw = wa->width;
225 c->h = wa->height;
226 c->th = bh;
228 c->border = 0;
229 setsize(c);
231 if(c->x + c->w + 2 > sw)
232 c->x = sw - c->w - 2;
233 if(c->x < 0)
234 c->x = 0;
235 if(c->y + c->h + 2 > sh)
236 c->y = sh - c->h - 2;
237 if(c->h != sh && c->y < bh)
238 c->y = bh;
240 c->proto = getproto(c->win);
241 XSelectInput(dpy, c->win,
242 StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
243 XGetTransientForHint(dpy, c->win, &trans);
244 twa.override_redirect = 1;
245 twa.background_pixmap = ParentRelative;
246 twa.event_mask = ExposureMask | EnterWindowMask;
248 c->title = XCreateWindow(dpy, root, c->tx, c->ty, c->tw, c->th,
249 0, DefaultDepth(dpy, screen), CopyFromParent,
250 DefaultVisual(dpy, screen),
251 CWOverrideRedirect | CWBackPixmap | CWEventMask, &twa);
253 if(clients)
254 clients->prev = c;
255 c->next = clients;
256 clients = c;
258 grabbutton(c, Button1, MODKEY);
259 grabbutton(c, Button2, MODKEY);
260 grabbutton(c, Button3, MODKEY);
262 settags(c);
263 if(!c->isfloat)
264 c->isfloat = trans
265 || (c->maxw && c->minw &&
266 c->maxw == c->minw && c->maxh == c->minh);
267 settitle(c);
268 if(isvisible(c))
269 sel = c;
270 arrange(NULL);
271 XMapWindow(dpy, c->win);
272 XMapWindow(dpy, c->title);
273 if(isvisible(c))
274 focus(c);
275 }
277 void
278 resize(Client *c, Bool sizehints, Corner sticky)
279 {
280 int bottom = c->y + c->h;
281 int right = c->x + c->w;
282 XWindowChanges wc;
284 if(sizehints) {
285 if(c->incw)
286 c->w -= (c->w - c->basew) % c->incw;
287 if(c->inch)
288 c->h -= (c->h - c->baseh) % c->inch;
289 if(c->minw && c->w < c->minw)
290 c->w = c->minw;
291 if(c->minh && c->h < c->minh)
292 c->h = c->minh;
293 if(c->maxw && c->w > c->maxw)
294 c->w = c->maxw;
295 if(c->maxh && c->h > c->maxh)
296 c->h = c->maxh;
297 }
298 if(sticky == TopRight || sticky == BotRight)
299 c->x = right - c->w;
300 if(sticky == BotLeft || sticky == BotRight)
301 c->y = bottom - c->h;
303 resizetitle(c);
304 wc.x = c->x;
305 wc.y = c->y;
306 wc.width = c->w;
307 wc.height = c->h;
308 if(c->w == sw && c->h == sh)
309 wc.border_width = 0;
310 else
311 wc.border_width = 1;
312 XConfigureWindow(dpy, c->win, CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
313 XSync(dpy, False);
314 }
316 void
317 setsize(Client *c)
318 {
319 long msize;
320 XSizeHints size;
322 if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
323 size.flags = PSize;
324 c->flags = size.flags;
325 if(c->flags & PBaseSize) {
326 c->basew = size.base_width;
327 c->baseh = size.base_height;
328 }
329 else
330 c->basew = c->baseh = 0;
331 if(c->flags & PResizeInc) {
332 c->incw = size.width_inc;
333 c->inch = size.height_inc;
334 }
335 else
336 c->incw = c->inch = 0;
337 if(c->flags & PMaxSize) {
338 c->maxw = size.max_width;
339 c->maxh = size.max_height;
340 }
341 else
342 c->maxw = c->maxh = 0;
343 if(c->flags & PMinSize) {
344 c->minw = size.min_width;
345 c->minh = size.min_height;
346 }
347 else
348 c->minw = c->minh = 0;
349 if(c->flags & PWinGravity)
350 c->grav = size.win_gravity;
351 else
352 c->grav = NorthWestGravity;
353 }
355 void
356 settitle(Client *c)
357 {
358 char **list = NULL;
359 int n;
360 XTextProperty name;
362 name.nitems = 0;
363 c->name[0] = 0;
364 XGetTextProperty(dpy, c->win, &name, netatom[NetWMName]);
365 if(!name.nitems)
366 XGetWMName(dpy, c->win, &name);
367 if(!name.nitems)
368 return;
369 if(name.encoding == XA_STRING)
370 strncpy(c->name, (char *)name.value, sizeof(c->name));
371 else {
372 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
373 && n > 0 && *list)
374 {
375 strncpy(c->name, *list, sizeof(c->name));
376 XFreeStringList(list);
377 }
378 }
379 XFree(name.value);
380 resizetitle(c);
381 }
383 void
384 togglemax(Arg *arg)
385 {
386 int ox, oy, ow, oh;
387 XEvent ev;
389 if(!sel)
390 return;
392 if((sel->ismax = !sel->ismax)) {
393 ox = sel->x;
394 oy = sel->y;
395 ow = sel->w;
396 oh = sel->h;
397 sel->x = sx;
398 sel->y = sy + bh;
399 sel->w = sw - 2;
400 sel->h = sh - 2 - bh;
402 restack();
403 resize(sel, arrange == dofloat, TopLeft);
405 sel->x = ox;
406 sel->y = oy;
407 sel->w = ow;
408 sel->h = oh;
409 }
410 else
411 resize(sel, False, TopLeft);
412 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
413 }
415 void
416 unmanage(Client *c)
417 {
418 XGrabServer(dpy);
419 XSetErrorHandler(xerrordummy);
421 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
422 XDestroyWindow(dpy, c->title);
424 if(c->prev)
425 c->prev->next = c->next;
426 if(c->next)
427 c->next->prev = c->prev;
428 if(c == clients)
429 clients = c->next;
430 if(sel == c)
431 sel = getnext(clients);
432 free(c->tags);
433 free(c);
435 XSync(dpy, False);
436 XSetErrorHandler(xerror);
437 XUngrabServer(dpy);
438 if(sel)
439 focus(sel);
440 arrange(NULL);
441 }
443 void
444 zoom(Arg *arg)
445 {
446 Client *c;
448 if(!sel || (arrange != dotile) || sel->isfloat || sel->ismax)
449 return;
451 if(sel == getnext(clients)) {
452 if((c = getnext(sel->next)))
453 sel = c;
454 else
455 return;
456 }
458 /* pop */
459 sel->prev->next = sel->next;
460 if(sel->next)
461 sel->next->prev = sel->prev;
462 sel->prev = NULL;
463 clients->prev = sel;
464 sel->next = clients;
465 clients = sel;
466 focus(sel);
467 arrange(NULL);
468 }