dwm-meillo

view client.c @ 271:e9dc5a9f9480

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