dwm-meillo

view client.c @ 372:a9b4077ec058

applied sanders focus_ patches
author Anselm R. Garbe <arg@10kloc.org>
date Mon, 28 Aug 2006 08:06:50 +0200
parents a1901753deef
children b1159a638d0a
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 grabbuttons(Client *c, Bool focus)
15 {
16 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
18 if(focus) {
19 XGrabButton(dpy, Button1, MODKEY, c->win, False, BUTTONMASK,
20 GrabModeAsync, GrabModeSync, None, None);
21 XGrabButton(dpy, Button1, MODKEY | LockMask, c->win, False, BUTTONMASK,
22 GrabModeAsync, GrabModeSync, None, None);
23 XGrabButton(dpy, Button1, MODKEY | numlockmask, c->win, False, BUTTONMASK,
24 GrabModeAsync, GrabModeSync, None, None);
25 XGrabButton(dpy, Button1, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
26 GrabModeAsync, GrabModeSync, None, None);
28 XGrabButton(dpy, Button2, MODKEY, c->win, False, BUTTONMASK,
29 GrabModeAsync, GrabModeSync, None, None);
30 XGrabButton(dpy, Button2, MODKEY | LockMask, c->win, False, BUTTONMASK,
31 GrabModeAsync, GrabModeSync, None, None);
32 XGrabButton(dpy, Button2, MODKEY | numlockmask, c->win, False, BUTTONMASK,
33 GrabModeAsync, GrabModeSync, None, None);
34 XGrabButton(dpy, Button2, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
35 GrabModeAsync, GrabModeSync, None, None);
37 XGrabButton(dpy, Button3, MODKEY, c->win, False, BUTTONMASK,
38 GrabModeAsync, GrabModeSync, None, None);
39 XGrabButton(dpy, Button3, MODKEY | LockMask, c->win, False, BUTTONMASK,
40 GrabModeAsync, GrabModeSync, None, None);
41 XGrabButton(dpy, Button3, MODKEY | numlockmask, c->win, False, BUTTONMASK,
42 GrabModeAsync, GrabModeSync, None, None);
43 XGrabButton(dpy, Button3, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
44 GrabModeAsync, GrabModeSync, None, None);
45 }
46 else
47 XGrabButton(dpy, AnyButton, AnyModifier, c->win, False, BUTTONMASK,
48 GrabModeAsync, GrabModeSync, None, None);
50 }
52 static void
53 resizetitle(Client *c)
54 {
55 int i;
57 c->tw = textw(c->name);
58 if(c->tw > c->w)
59 c->tw = c->w + 2;
60 c->tx = c->x + c->w - c->tw + 2;
61 c->ty = c->y;
62 if(isvisible(c))
63 XMoveResizeWindow(dpy, c->twin, c->tx, c->ty, c->tw, c->th);
64 else
65 XMoveResizeWindow(dpy, c->twin, c->tx + 2 * sw, c->ty, c->tw, c->th);
67 }
69 static int
70 xerrordummy(Display *dsply, XErrorEvent *ee)
71 {
72 return 0;
73 }
75 /* extern functions */
77 void
78 ban(Client *c)
79 {
80 XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
81 XMoveWindow(dpy, c->twin, c->tx + 2 * sw, c->ty);
82 }
84 void
85 focus(Client *c)
86 {
87 Client *old = sel;
89 if(!issel)
90 return;
91 if(!sel)
92 sel = c;
93 else if(sel != c) {
94 if(sel->ismax)
95 togglemax(NULL);
96 sel = c;
97 grabbuttons(old, False);
98 drawtitle(old);
99 }
100 grabbuttons(c, True);
101 drawtitle(c);
102 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
103 }
105 Client *
106 getclient(Window w)
107 {
108 Client *c;
110 for(c = clients; c; c = c->next)
111 if(c->win == w)
112 return c;
113 return NULL;
114 }
116 Client *
117 getctitle(Window w)
118 {
119 Client *c;
121 for(c = clients; c; c = c->next)
122 if(c->twin == w)
123 return c;
124 return NULL;
125 }
127 void
128 gravitate(Client *c, Bool invert)
129 {
130 int dx = 0, dy = 0;
132 switch(c->grav) {
133 default:
134 break;
135 case StaticGravity:
136 case NorthWestGravity:
137 case NorthGravity:
138 case NorthEastGravity:
139 dy = c->border;
140 break;
141 case EastGravity:
142 case CenterGravity:
143 case WestGravity:
144 dy = -(c->h / 2) + c->border;
145 break;
146 case SouthEastGravity:
147 case SouthGravity:
148 case SouthWestGravity:
149 dy = -(c->h);
150 break;
151 }
153 switch (c->grav) {
154 default:
155 break;
156 case StaticGravity:
157 case NorthWestGravity:
158 case WestGravity:
159 case SouthWestGravity:
160 dx = c->border;
161 break;
162 case NorthGravity:
163 case CenterGravity:
164 case SouthGravity:
165 dx = -(c->w / 2) + c->border;
166 break;
167 case NorthEastGravity:
168 case EastGravity:
169 case SouthEastGravity:
170 dx = -(c->w + c->border);
171 break;
172 }
174 if(invert) {
175 dx = -dx;
176 dy = -dy;
177 }
178 c->x += dx;
179 c->y += dy;
180 }
182 void
183 killclient(Arg *arg)
184 {
185 if(!sel)
186 return;
187 if(sel->proto & PROTODELWIN)
188 sendevent(sel->win, wmatom[WMProtocols], wmatom[WMDelete]);
189 else
190 XKillClient(dpy, sel->win);
191 }
193 void
194 manage(Window w, XWindowAttributes *wa)
195 {
196 unsigned int i;
197 Client *c, *tc;
198 Window trans;
199 XSetWindowAttributes twa;
201 c = emallocz(sizeof(Client));
202 c->tags = emallocz(ntags * sizeof(Bool));
203 c->win = w;
204 c->x = c->tx = wa->x;
205 c->y = c->ty = wa->y;
206 c->w = c->tw = wa->width;
207 c->h = wa->height;
208 c->th = bh;
210 c->border = 0;
211 setsize(c);
213 if(c->x + c->w + 2 > sw)
214 c->x = sw - c->w - 2;
215 if(c->x < 0)
216 c->x = 0;
217 if(c->y + c->h + 2 > sh)
218 c->y = sh - c->h - 2;
219 if(c->h != sh && c->y < bh)
220 c->y = bh;
222 c->proto = getproto(c->win);
223 XSelectInput(dpy, c->win,
224 StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
225 XGetTransientForHint(dpy, c->win, &trans);
226 twa.override_redirect = 1;
227 twa.background_pixmap = ParentRelative;
228 twa.event_mask = ExposureMask | EnterWindowMask;
230 c->twin = XCreateWindow(dpy, root, c->tx, c->ty, c->tw, c->th,
231 0, DefaultDepth(dpy, screen), CopyFromParent,
232 DefaultVisual(dpy, screen),
233 CWOverrideRedirect | CWBackPixmap | CWEventMask, &twa);
235 if(clients)
236 clients->prev = c;
237 c->next = clients;
238 clients = c;
240 grabbuttons(c, False);
242 if((tc = getclient(trans))) /* inherit tags */
243 for(i = 0; i < ntags; i++)
244 c->tags[i] = tc->tags[i];
245 else
246 settags(c);
247 if(!c->isfloat)
248 c->isfloat = trans
249 || (c->maxw && c->minw &&
250 c->maxw == c->minw && c->maxh == c->minh);
251 settitle(c);
252 if(isvisible(c))
253 sel = c;
254 arrange(NULL);
255 XMapWindow(dpy, c->win);
256 XMapWindow(dpy, c->twin);
257 if(isvisible(c))
258 focus(c);
259 }
261 void
262 resize(Client *c, Bool sizehints, Corner sticky)
263 {
264 int bottom = c->y + c->h;
265 int right = c->x + c->w;
266 XWindowChanges wc;
268 if(sizehints) {
269 if(c->incw)
270 c->w -= (c->w - c->basew) % c->incw;
271 if(c->inch)
272 c->h -= (c->h - c->baseh) % c->inch;
273 if(c->minw && c->w < c->minw)
274 c->w = c->minw;
275 if(c->minh && c->h < c->minh)
276 c->h = c->minh;
277 if(c->maxw && c->w > c->maxw)
278 c->w = c->maxw;
279 if(c->maxh && c->h > c->maxh)
280 c->h = c->maxh;
281 }
282 if(sticky == TopRight || sticky == BotRight)
283 c->x = right - c->w;
284 if(sticky == BotLeft || sticky == BotRight)
285 c->y = bottom - c->h;
287 resizetitle(c);
288 wc.x = c->x;
289 wc.y = c->y;
290 wc.width = c->w;
291 wc.height = c->h;
292 if(c->w == sw && c->h == sh)
293 wc.border_width = 0;
294 else
295 wc.border_width = 1;
296 XConfigureWindow(dpy, c->win, CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
297 XSync(dpy, False);
298 }
300 void
301 setsize(Client *c)
302 {
303 long msize;
304 XSizeHints size;
306 if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
307 size.flags = PSize;
308 c->flags = size.flags;
309 if(c->flags & PBaseSize) {
310 c->basew = size.base_width;
311 c->baseh = size.base_height;
312 }
313 else
314 c->basew = c->baseh = 0;
315 if(c->flags & PResizeInc) {
316 c->incw = size.width_inc;
317 c->inch = size.height_inc;
318 }
319 else
320 c->incw = c->inch = 0;
321 if(c->flags & PMaxSize) {
322 c->maxw = size.max_width;
323 c->maxh = size.max_height;
324 }
325 else
326 c->maxw = c->maxh = 0;
327 if(c->flags & PMinSize) {
328 c->minw = size.min_width;
329 c->minh = size.min_height;
330 }
331 else
332 c->minw = c->minh = 0;
333 if(c->flags & PWinGravity)
334 c->grav = size.win_gravity;
335 else
336 c->grav = NorthWestGravity;
337 }
339 void
340 settitle(Client *c)
341 {
342 char **list = NULL;
343 int i, n;
344 XTextProperty name;
346 name.nitems = 0;
347 c->name[0] = 0;
348 XGetTextProperty(dpy, c->win, &name, netatom[NetWMName]);
349 if(!name.nitems)
350 XGetWMName(dpy, c->win, &name);
351 if(!name.nitems)
352 return;
353 if(name.encoding == XA_STRING)
354 strncpy(c->name, (char *)name.value, sizeof(c->name));
355 else {
356 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
357 && n > 0 && *list)
358 {
359 strncpy(c->name, *list, sizeof(c->name));
360 XFreeStringList(list);
361 }
362 }
363 XFree(name.value);
364 resizetitle(c);
365 }
367 void
368 togglemax(Arg *arg)
369 {
370 int ox, oy, ow, oh;
371 XEvent ev;
373 if(!sel)
374 return;
376 if((sel->ismax = !sel->ismax)) {
377 ox = sel->x;
378 oy = sel->y;
379 ow = sel->w;
380 oh = sel->h;
381 sel->x = sx;
382 sel->y = sy + bh;
383 sel->w = sw - 2;
384 sel->h = sh - 2 - bh;
386 restack();
387 resize(sel, arrange == dofloat, TopLeft);
389 sel->x = ox;
390 sel->y = oy;
391 sel->w = ow;
392 sel->h = oh;
393 }
394 else
395 resize(sel, False, TopLeft);
396 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
397 }
399 void
400 unmanage(Client *c)
401 {
402 Client *tc;
403 Window trans;
404 XGrabServer(dpy);
405 XSetErrorHandler(xerrordummy);
407 XGetTransientForHint(dpy, c->win, &trans);
409 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
410 XDestroyWindow(dpy, c->twin);
412 if(c->prev)
413 c->prev->next = c->next;
414 if(c->next)
415 c->next->prev = c->prev;
416 if(c == clients)
417 clients = c->next;
418 if(sel == c) {
419 if(trans && (tc = getclient(trans)) && isvisible(tc))
420 sel = tc;
421 else
422 sel = getnext(clients);
423 }
424 free(c->tags);
425 free(c);
427 XSync(dpy, False);
428 XSetErrorHandler(xerror);
429 XUngrabServer(dpy);
430 if(sel)
431 focus(sel);
432 arrange(NULL);
433 }