dwm-meillo
view client.c @ 250:a9392d33865a
removed the if(clients) check from popping code
author | Anselm R.Garbe <arg@10ksloc.org> |
---|---|
date | Fri, 11 Aug 2006 08:34:42 +0200 |
parents | 1227c21588e2 |
children | 5c72602fe587 |
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(c->tags[tsel])
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 XSync(dpy, False);
63 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
64 }
66 void
67 focusnext(Arg *arg)
68 {
69 Client *c;
71 if(!sel)
72 return;
74 if(sel->ismax)
75 togglemax(NULL);
77 if(!(c = getnext(sel->next)))
78 c = getnext(clients);
79 if(c) {
80 higher(c);
81 focus(c);
82 }
83 }
85 void
86 focusprev(Arg *arg)
87 {
88 Client *c;
90 if(!sel)
91 return;
93 if(sel->ismax)
94 togglemax(NULL);
96 if(!(c = getprev(sel->prev))) {
97 for(c = clients; c && c->next; c = c->next);
98 c = getprev(c);
99 }
100 if(c) {
101 higher(c);
102 focus(c);
103 }
104 }
106 Client *
107 getclient(Window w)
108 {
109 Client *c;
111 for(c = clients; c; c = c->next)
112 if(c->win == w)
113 return c;
114 return NULL;
115 }
117 Client *
118 getctitle(Window w)
119 {
120 Client *c;
122 for(c = clients; c; c = c->next)
123 if(c->title == w)
124 return c;
125 return NULL;
126 }
128 void
129 gravitate(Client *c, Bool invert)
130 {
131 int dx = 0, dy = 0;
133 switch(c->grav) {
134 default:
135 break;
136 case StaticGravity:
137 case NorthWestGravity:
138 case NorthGravity:
139 case NorthEastGravity:
140 dy = c->border;
141 break;
142 case EastGravity:
143 case CenterGravity:
144 case WestGravity:
145 dy = -(c->h / 2) + c->border;
146 break;
147 case SouthEastGravity:
148 case SouthGravity:
149 case SouthWestGravity:
150 dy = -(c->h);
151 break;
152 }
154 switch (c->grav) {
155 default:
156 break;
157 case StaticGravity:
158 case NorthWestGravity:
159 case WestGravity:
160 case SouthWestGravity:
161 dx = c->border;
162 break;
163 case NorthGravity:
164 case CenterGravity:
165 case SouthGravity:
166 dx = -(c->w / 2) + c->border;
167 break;
168 case NorthEastGravity:
169 case EastGravity:
170 case SouthEastGravity:
171 dx = -(c->w + c->border);
172 break;
173 }
175 if(invert) {
176 dx = -dx;
177 dy = -dy;
178 }
179 c->x += dx;
180 c->y += dy;
181 }
183 void
184 higher(Client *c)
185 {
186 XRaiseWindow(dpy, c->win);
187 XRaiseWindow(dpy, c->title);
188 }
190 void
191 killclient(Arg *arg)
192 {
193 if(!sel)
194 return;
195 if(sel->proto & PROTODELWIN)
196 sendevent(sel->win, wmatom[WMProtocols], wmatom[WMDelete]);
197 else
198 XKillClient(dpy, sel->win);
199 }
201 void
202 manage(Window w, XWindowAttributes *wa)
203 {
204 Client *c;
205 Window trans;
206 XSetWindowAttributes twa;
208 c = emallocz(sizeof(Client));
209 c->tags = emallocz(ntags * sizeof(Bool));
210 c->win = w;
211 c->x = c->tx = wa->x;
212 c->y = c->ty = wa->y;
213 c->w = c->tw = wa->width;
214 c->h = wa->height;
215 c->th = bh;
217 c->border = 0;
218 setsize(c);
220 if(c->h != sh && c->y < bh)
221 c->y = c->ty = bh;
223 c->proto = getproto(c->win);
224 XSelectInput(dpy, c->win,
225 StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
226 XGetTransientForHint(dpy, c->win, &trans);
227 twa.override_redirect = 1;
228 twa.background_pixmap = ParentRelative;
229 twa.event_mask = ExposureMask | EnterWindowMask;
231 c->title = XCreateWindow(dpy, root, c->tx, c->ty, c->tw, c->th,
232 0, DefaultDepth(dpy, screen), CopyFromParent,
233 DefaultVisual(dpy, screen),
234 CWOverrideRedirect | CWBackPixmap | CWEventMask, &twa);
236 if(clients)
237 clients->prev = c;
238 c->next = clients;
239 clients = c;
241 XGrabButton(dpy, Button1, MODKEY, c->win, False, BUTTONMASK,
242 GrabModeAsync, GrabModeSync, None, None);
243 XGrabButton(dpy, Button1, MODKEY | LockMask, c->win, False, BUTTONMASK,
244 GrabModeAsync, GrabModeSync, None, None);
245 XGrabButton(dpy, Button1, MODKEY | NUMLOCKMASK, c->win, False, BUTTONMASK,
246 GrabModeAsync, GrabModeSync, None, None);
247 XGrabButton(dpy, Button1, MODKEY | NUMLOCKMASK | LockMask, c->win, False, BUTTONMASK,
248 GrabModeAsync, GrabModeSync, None, None);
250 XGrabButton(dpy, Button2, MODKEY, c->win, False, BUTTONMASK,
251 GrabModeAsync, GrabModeSync, None, None);
252 XGrabButton(dpy, Button2, MODKEY | LockMask, c->win, False, BUTTONMASK,
253 GrabModeAsync, GrabModeSync, None, None);
254 XGrabButton(dpy, Button2, MODKEY | NUMLOCKMASK, c->win, False, BUTTONMASK,
255 GrabModeAsync, GrabModeSync, None, None);
256 XGrabButton(dpy, Button2, MODKEY | NUMLOCKMASK | LockMask, c->win, False, BUTTONMASK,
257 GrabModeAsync, GrabModeSync, None, None);
259 XGrabButton(dpy, Button3, MODKEY, c->win, False, BUTTONMASK,
260 GrabModeAsync, GrabModeSync, None, None);
261 XGrabButton(dpy, Button3, MODKEY | LockMask, c->win, False, BUTTONMASK,
262 GrabModeAsync, GrabModeSync, None, None);
263 XGrabButton(dpy, Button3, MODKEY | NUMLOCKMASK, c->win, False, BUTTONMASK,
264 GrabModeAsync, GrabModeSync, None, None);
265 XGrabButton(dpy, Button3, MODKEY | NUMLOCKMASK | LockMask, c->win, False, BUTTONMASK,
266 GrabModeAsync, GrabModeSync, None, None);
268 settags(c);
269 if(!c->isfloat)
270 c->isfloat = trans
271 || (c->maxw && c->minw &&
272 c->maxw == c->minw && c->maxh == c->minh);
273 settitle(c);
274 arrange(NULL);
276 /* mapping the window now prevents flicker */
277 XMapRaised(dpy, c->win);
278 XMapRaised(dpy, c->title);
279 if(c->tags[tsel])
280 focus(c);
281 }
283 void
284 resize(Client *c, Bool sizehints, Corner sticky)
285 {
286 int bottom = c->y + c->h;
287 int right = c->x + c->w;
288 /*XConfigureEvent e;*/
289 XWindowChanges wc;
291 if(sizehints) {
292 if(c->incw)
293 c->w -= (c->w - c->basew) % c->incw;
294 if(c->inch)
295 c->h -= (c->h - c->baseh) % c->inch;
296 if(c->minw && c->w < c->minw)
297 c->w = c->minw;
298 if(c->minh && c->h < c->minh)
299 c->h = c->minh;
300 if(c->maxw && c->w > c->maxw)
301 c->w = c->maxw;
302 if(c->maxh && c->h > c->maxh)
303 c->h = c->maxh;
304 }
305 if(c->x > right) /* might happen on restart */
306 c->x = right - c->w;
307 if(c->y > bottom)
308 c->y = bottom - c->h;
309 if(sticky == TopRight || sticky == BotRight)
310 c->x = right - c->w;
311 if(sticky == BotLeft || sticky == BotRight)
312 c->y = bottom - c->h;
314 resizetitle(c);
315 wc.x = c->x;
316 wc.y = c->y;
317 wc.width = c->w;
318 wc.height = c->h;
319 if(c->w == sw && c->h == sh)
320 wc.border_width = 0;
321 else
322 wc.border_width = 1;
323 XConfigureWindow(dpy, c->win, CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
324 XSync(dpy, False);
325 }
327 void
328 setsize(Client *c)
329 {
330 long msize;
331 XSizeHints size;
333 if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
334 size.flags = PSize;
335 c->flags = size.flags;
336 if(c->flags & PBaseSize) {
337 c->basew = size.base_width;
338 c->baseh = size.base_height;
339 }
340 else
341 c->basew = c->baseh = 0;
342 if(c->flags & PResizeInc) {
343 c->incw = size.width_inc;
344 c->inch = size.height_inc;
345 }
346 else
347 c->incw = c->inch = 0;
348 if(c->flags & PMaxSize) {
349 c->maxw = size.max_width;
350 c->maxh = size.max_height;
351 }
352 else
353 c->maxw = c->maxh = 0;
354 if(c->flags & PMinSize) {
355 c->minw = size.min_width;
356 c->minh = size.min_height;
357 }
358 else
359 c->minw = c->minh = 0;
360 if(c->flags & PWinGravity)
361 c->grav = size.win_gravity;
362 else
363 c->grav = NorthWestGravity;
364 }
366 void
367 settitle(Client *c)
368 {
369 char **list = NULL;
370 int n;
371 XTextProperty name;
373 name.nitems = 0;
374 c->name[0] = 0;
375 XGetTextProperty(dpy, c->win, &name, netatom[NetWMName]);
376 if(!name.nitems)
377 XGetWMName(dpy, c->win, &name);
378 if(!name.nitems)
379 return;
380 if(name.encoding == XA_STRING)
381 strncpy(c->name, (char *)name.value, sizeof(c->name));
382 else {
383 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
384 && n > 0 && *list)
385 {
386 strncpy(c->name, *list, sizeof(c->name));
387 XFreeStringList(list);
388 }
389 }
390 XFree(name.value);
391 resizetitle(c);
392 }
394 void
395 togglemax(Arg *arg)
396 {
397 int ox, oy, ow, oh;
398 XEvent ev;
400 if(!sel)
401 return;
403 if((sel->ismax = !sel->ismax)) {
404 ox = sel->x;
405 oy = sel->y;
406 ow = sel->w;
407 oh = sel->h;
408 sel->x = sx;
409 sel->y = sy + bh;
410 sel->w = sw - 2;
411 sel->h = sh - 2 - bh;
413 higher(sel);
414 resize(sel, arrange == dofloat, TopLeft);
416 sel->x = ox;
417 sel->y = oy;
418 sel->w = ow;
419 sel->h = oh;
420 }
421 else
422 resize(sel, False, TopLeft);
423 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
424 }
426 void
427 unmanage(Client *c)
428 {
429 XGrabServer(dpy);
430 XSetErrorHandler(xerrordummy);
432 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
433 XDestroyWindow(dpy, c->title);
435 if(c->prev)
436 c->prev->next = c->next;
437 if(c->next)
438 c->next->prev = c->prev;
439 if(c == clients)
440 clients = c->next;
441 if(sel == c) {
442 sel = getnext(c->next);
443 if(!sel)
444 sel = getprev(c->prev);
445 if(!sel)
446 sel = clients;
447 }
448 free(c->tags);
449 free(c);
451 XSync(dpy, False);
452 XSetErrorHandler(xerror);
453 XUngrabServer(dpy);
454 arrange(NULL);
455 if(sel)
456 focus(sel);
457 }
459 void
460 zoom(Arg *arg)
461 {
462 Client *c;
464 if(!sel || (arrange != dotile) || sel->isfloat || sel->ismax)
465 return;
467 if(sel == getnext(clients)) {
468 if((c = getnext(sel->next)))
469 sel = c;
470 else
471 return;
472 }
474 /* pop */
475 if(sel->prev)
476 sel->prev->next = sel->next;
477 if(sel->next)
478 sel->next->prev = sel->prev;
479 sel->prev = NULL;
480 clients->prev = sel;
481 sel->next = clients;
482 clients = sel;
483 arrange(NULL);
484 focus(sel);
485 }