dwm-meillo
view client.c @ 272:be40a56df248
fixed a core dump
author | Anselm R.Garbe <arg@10ksloc.org> |
---|---|
date | Mon, 14 Aug 2006 10:49:22 +0200 |
parents | dacd3f3c5823 |
children | 134804791d73 |
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;
56 sel = c;
57 if(old && old != c)
58 drawtitle(old);
59 drawtitle(c);
60 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
61 }
63 void
64 focusnext(Arg *arg)
65 {
66 Client *c;
68 if(!sel)
69 return;
71 if(sel->ismax)
72 togglemax(NULL);
74 if(!(c = getnext(sel->next)))
75 c = getnext(clients);
76 if(c) {
77 focus(c);
78 restack();
79 }
80 }
82 void
83 focusprev(Arg *arg)
84 {
85 Client *c;
87 if(!sel)
88 return;
90 if(sel->ismax)
91 togglemax(NULL);
93 if(!(c = getprev(sel->prev))) {
94 for(c = clients; c && c->next; c = c->next);
95 c = getprev(c);
96 }
97 if(c) {
98 focus(c);
99 restack();
100 }
101 }
103 Client *
104 getclient(Window w)
105 {
106 Client *c;
108 for(c = clients; c; c = c->next)
109 if(c->win == w)
110 return c;
111 return NULL;
112 }
114 Client *
115 getctitle(Window w)
116 {
117 Client *c;
119 for(c = clients; c; c = c->next)
120 if(c->title == w)
121 return c;
122 return NULL;
123 }
125 void
126 gravitate(Client *c, Bool invert)
127 {
128 int dx = 0, dy = 0;
130 switch(c->grav) {
131 default:
132 break;
133 case StaticGravity:
134 case NorthWestGravity:
135 case NorthGravity:
136 case NorthEastGravity:
137 dy = c->border;
138 break;
139 case EastGravity:
140 case CenterGravity:
141 case WestGravity:
142 dy = -(c->h / 2) + c->border;
143 break;
144 case SouthEastGravity:
145 case SouthGravity:
146 case SouthWestGravity:
147 dy = -(c->h);
148 break;
149 }
151 switch (c->grav) {
152 default:
153 break;
154 case StaticGravity:
155 case NorthWestGravity:
156 case WestGravity:
157 case SouthWestGravity:
158 dx = c->border;
159 break;
160 case NorthGravity:
161 case CenterGravity:
162 case SouthGravity:
163 dx = -(c->w / 2) + c->border;
164 break;
165 case NorthEastGravity:
166 case EastGravity:
167 case SouthEastGravity:
168 dx = -(c->w + c->border);
169 break;
170 }
172 if(invert) {
173 dx = -dx;
174 dy = -dy;
175 }
176 c->x += dx;
177 c->y += dy;
178 }
180 void
181 killclient(Arg *arg)
182 {
183 if(!sel)
184 return;
185 if(sel->proto & PROTODELWIN)
186 sendevent(sel->win, wmatom[WMProtocols], wmatom[WMDelete]);
187 else
188 XKillClient(dpy, sel->win);
189 }
191 void
192 manage(Window w, XWindowAttributes *wa)
193 {
194 Client *c;
195 Window trans;
196 XSetWindowAttributes twa;
198 c = emallocz(sizeof(Client));
199 c->tags = emallocz(ntags * sizeof(Bool));
200 c->win = w;
201 c->x = c->tx = wa->x;
202 c->y = c->ty = wa->y;
203 c->w = c->tw = wa->width;
204 c->h = wa->height;
205 c->th = bh;
207 c->border = 0;
208 setsize(c);
210 if(c->h != sh && c->y < bh)
211 c->y = c->ty = bh;
213 c->proto = getproto(c->win);
214 XSelectInput(dpy, c->win,
215 StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
216 XGetTransientForHint(dpy, c->win, &trans);
217 twa.override_redirect = 1;
218 twa.background_pixmap = ParentRelative;
219 twa.event_mask = ExposureMask | EnterWindowMask;
221 c->title = XCreateWindow(dpy, root, c->tx, c->ty, c->tw, c->th,
222 0, DefaultDepth(dpy, screen), CopyFromParent,
223 DefaultVisual(dpy, screen),
224 CWOverrideRedirect | CWBackPixmap | CWEventMask, &twa);
226 if(clients)
227 clients->prev = c;
228 c->next = clients;
229 clients = c;
231 XGrabButton(dpy, Button1, MODKEY, c->win, False, BUTTONMASK,
232 GrabModeAsync, GrabModeSync, None, None);
233 XGrabButton(dpy, Button1, MODKEY | LockMask, c->win, False, BUTTONMASK,
234 GrabModeAsync, GrabModeSync, None, None);
235 XGrabButton(dpy, Button1, MODKEY | NUMLOCKMASK, c->win, False, BUTTONMASK,
236 GrabModeAsync, GrabModeSync, None, None);
237 XGrabButton(dpy, Button1, MODKEY | NUMLOCKMASK | LockMask, c->win, False, BUTTONMASK,
238 GrabModeAsync, GrabModeSync, None, None);
240 XGrabButton(dpy, Button2, MODKEY, c->win, False, BUTTONMASK,
241 GrabModeAsync, GrabModeSync, None, None);
242 XGrabButton(dpy, Button2, MODKEY | LockMask, c->win, False, BUTTONMASK,
243 GrabModeAsync, GrabModeSync, None, None);
244 XGrabButton(dpy, Button2, MODKEY | NUMLOCKMASK, c->win, False, BUTTONMASK,
245 GrabModeAsync, GrabModeSync, None, None);
246 XGrabButton(dpy, Button2, MODKEY | NUMLOCKMASK | LockMask, c->win, False, BUTTONMASK,
247 GrabModeAsync, GrabModeSync, None, None);
249 XGrabButton(dpy, Button3, MODKEY, c->win, False, BUTTONMASK,
250 GrabModeAsync, GrabModeSync, None, None);
251 XGrabButton(dpy, Button3, MODKEY | LockMask, c->win, False, BUTTONMASK,
252 GrabModeAsync, GrabModeSync, None, None);
253 XGrabButton(dpy, Button3, MODKEY | NUMLOCKMASK, c->win, False, BUTTONMASK,
254 GrabModeAsync, GrabModeSync, None, None);
255 XGrabButton(dpy, Button3, MODKEY | NUMLOCKMASK | LockMask, c->win, False, BUTTONMASK,
256 GrabModeAsync, GrabModeSync, None, None);
258 settags(c);
259 if(!c->isfloat)
260 c->isfloat = trans
261 || (c->maxw && c->minw &&
262 c->maxw == c->minw && c->maxh == c->minh);
263 settitle(c);
265 XMapWindow(dpy, c->win);
266 XMapWindow(dpy, c->title);
267 if(isvisible(c))
268 focus(c);
269 arrange(NULL);
270 }
272 void
273 resize(Client *c, Bool sizehints, Corner sticky)
274 {
275 int bottom = c->y + c->h;
276 int right = c->x + c->w;
277 /*XConfigureEvent e;*/
278 XWindowChanges wc;
280 if(sizehints) {
281 if(c->incw)
282 c->w -= (c->w - c->basew) % c->incw;
283 if(c->inch)
284 c->h -= (c->h - c->baseh) % c->inch;
285 if(c->minw && c->w < c->minw)
286 c->w = c->minw;
287 if(c->minh && c->h < c->minh)
288 c->h = c->minh;
289 if(c->maxw && c->w > c->maxw)
290 c->w = c->maxw;
291 if(c->maxh && c->h > c->maxh)
292 c->h = c->maxh;
293 }
294 if(c->x > right) /* might happen on restart */
295 c->x = right - c->w;
296 if(c->y > bottom)
297 c->y = bottom - c->h;
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 }