dwm-meillo
view client.c @ 166:e0535db04dfe
removed the CONFIG variable from config.mk, renamed config.h into config.default.h, after first clone/extract one needs to copy config.default.h to config.h, that is easier than always heavy typing make CONFIG=blafasel
author | arg@10ksloc.org |
---|---|
date | Wed, 02 Aug 2006 16:46:59 +0200 |
parents | e2e1de08341d |
children | 1db04019684e |
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 < TLast; i++)
20 if(c->tags[i])
21 c->tw += textw(c->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 Client *old = sel;
53 XEvent ev;
55 sel = c;
56 if(old && old != c)
57 drawtitle(old);
58 drawtitle(c);
59 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
60 XSync(dpy, False);
61 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
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 higher(c);
79 focus(c);
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 higher(c);
100 focus(c);
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 higher(Client *c)
183 {
184 XRaiseWindow(dpy, c->win);
185 XRaiseWindow(dpy, c->title);
186 }
188 void
189 killclient(Arg *arg)
190 {
191 if(!sel)
192 return;
193 if(sel->proto & PROTODELWIN)
194 sendevent(sel->win, wmatom[WMProtocols], wmatom[WMDelete]);
195 else
196 XKillClient(dpy, sel->win);
197 }
199 void
200 lower(Client *c)
201 {
202 XLowerWindow(dpy, c->title);
203 XLowerWindow(dpy, c->win);
204 }
206 void
207 manage(Window w, XWindowAttributes *wa)
208 {
209 Client *c;
210 Window trans;
211 XSetWindowAttributes twa;
213 c = emallocz(sizeof(Client));
214 c->win = w;
215 c->x = c->tx = wa->x;
216 c->y = c->ty = wa->y;
217 c->w = c->tw = wa->width;
218 c->h = wa->height;
219 c->th = bh;
221 c->border = 0;
222 setsize(c);
224 if(c->h != sh && c->y < bh)
225 c->y = c->ty = bh;
227 c->proto = getproto(c->win);
228 XSelectInput(dpy, c->win,
229 StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
230 XGetTransientForHint(dpy, c->win, &trans);
231 twa.override_redirect = 1;
232 twa.background_pixmap = ParentRelative;
233 twa.event_mask = ExposureMask | EnterWindowMask;
235 c->title = XCreateWindow(dpy, root, c->tx, c->ty, c->tw, c->th,
236 0, DefaultDepth(dpy, screen), CopyFromParent,
237 DefaultVisual(dpy, screen),
238 CWOverrideRedirect | CWBackPixmap | CWEventMask, &twa);
240 if(clients)
241 clients->prev = c;
242 c->next = clients;
243 clients = c;
245 XGrabButton(dpy, Button1, MODKEY, c->win, False, BUTTONMASK,
246 GrabModeAsync, GrabModeSync, None, None);
247 XGrabButton(dpy, Button2, MODKEY, c->win, False, BUTTONMASK,
248 GrabModeAsync, GrabModeSync, None, None);
249 XGrabButton(dpy, Button3, MODKEY, c->win, False, BUTTONMASK,
250 GrabModeAsync, GrabModeSync, None, None);
252 settags(c);
253 if(!c->isfloat)
254 c->isfloat = trans
255 || (c->maxw && c->minw &&
256 c->maxw == c->minw && c->maxh == c->minh);
257 settitle(c);
258 arrange(NULL);
260 /* mapping the window now prevents flicker */
261 XMapRaised(dpy, c->win);
262 XMapRaised(dpy, c->title);
263 if(c->tags[tsel])
264 focus(c);
265 }
267 void
268 resize(Client *c, Bool sizehints, Corner sticky)
269 {
270 int bottom = c->y + c->h;
271 int right = c->x + c->w;
272 /*XConfigureEvent e;*/
273 XWindowChanges wc;
275 if(sizehints) {
276 if(c->incw)
277 c->w -= (c->w - c->basew) % c->incw;
278 if(c->inch)
279 c->h -= (c->h - c->baseh) % c->inch;
280 if(c->minw && c->w < c->minw)
281 c->w = c->minw;
282 if(c->minh && c->h < c->minh)
283 c->h = c->minh;
284 if(c->maxw && c->w > c->maxw)
285 c->w = c->maxw;
286 if(c->maxh && c->h > c->maxh)
287 c->h = c->maxh;
288 }
289 if(c->x > right) /* might happen on restart */
290 c->x = right - c->w;
291 if(c->y > bottom)
292 c->y = bottom - c->h;
293 if(sticky == TopRight || sticky == BotRight)
294 c->x = right - c->w;
295 if(sticky == BotLeft || sticky == BotRight)
296 c->y = bottom - c->h;
298 resizetitle(c);
299 wc.x = c->x;
300 wc.y = c->y;
301 wc.width = c->w;
302 wc.height = c->h;
303 if(c->w == sw && c->h == sh)
304 wc.border_width = 0;
305 else
306 wc.border_width = 1;
307 XConfigureWindow(dpy, c->win, CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
308 XSync(dpy, False);
309 }
311 void
312 setsize(Client *c)
313 {
314 long msize;
315 XSizeHints size;
317 if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
318 size.flags = PSize;
319 c->flags = size.flags;
320 if(c->flags & PBaseSize) {
321 c->basew = size.base_width;
322 c->baseh = size.base_height;
323 }
324 else
325 c->basew = c->baseh = 0;
326 if(c->flags & PResizeInc) {
327 c->incw = size.width_inc;
328 c->inch = size.height_inc;
329 }
330 else
331 c->incw = c->inch = 0;
332 if(c->flags & PMaxSize) {
333 c->maxw = size.max_width;
334 c->maxh = size.max_height;
335 }
336 else
337 c->maxw = c->maxh = 0;
338 if(c->flags & PMinSize) {
339 c->minw = size.min_width;
340 c->minh = size.min_height;
341 }
342 else
343 c->minw = c->minh = 0;
344 if(c->flags & PWinGravity)
345 c->grav = size.win_gravity;
346 else
347 c->grav = NorthWestGravity;
348 }
350 void
351 settitle(Client *c)
352 {
353 char **list = NULL;
354 int n;
355 XTextProperty name;
357 name.nitems = 0;
358 c->name[0] = 0;
359 XGetTextProperty(dpy, c->win, &name, netatom[NetWMName]);
360 if(!name.nitems)
361 XGetWMName(dpy, c->win, &name);
362 if(!name.nitems)
363 return;
364 if(name.encoding == XA_STRING)
365 strncpy(c->name, (char *)name.value, sizeof(c->name));
366 else {
367 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
368 && n > 0 && *list)
369 {
370 strncpy(c->name, *list, sizeof(c->name));
371 XFreeStringList(list);
372 }
373 }
374 XFree(name.value);
375 resizetitle(c);
376 }
378 void
379 togglemax(Arg *arg)
380 {
381 int ox, oy, ow, oh;
382 XEvent ev;
384 if(!sel)
385 return;
387 if((sel->ismax = !sel->ismax)) {
388 ox = sel->x;
389 oy = sel->y;
390 ow = sel->w;
391 oh = sel->h;
392 sel->x = sx;
393 sel->y = sy + bh;
394 sel->w = sw - 2;
395 sel->h = sh - 2 - bh;
397 higher(sel);
398 resize(sel, False, TopLeft);
400 sel->x = ox;
401 sel->y = oy;
402 sel->w = ow;
403 sel->h = oh;
404 }
405 else
406 resize(sel, False, TopLeft);
407 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
408 }
410 void
411 unmanage(Client *c)
412 {
413 XGrabServer(dpy);
414 XSetErrorHandler(xerrordummy);
416 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
417 XDestroyWindow(dpy, c->title);
419 if(c->prev)
420 c->prev->next = c->next;
421 if(c->next)
422 c->next->prev = c->prev;
423 if(c == clients)
424 clients = c->next;
425 if(sel == c) {
426 sel = getnext(c->next);
427 if(!sel)
428 sel = getprev(c->prev);
429 if(!sel)
430 sel = clients;
431 }
432 free(c);
434 XSync(dpy, False);
435 XSetErrorHandler(xerror);
436 XUngrabServer(dpy);
437 arrange(NULL);
438 if(sel)
439 focus(sel);
440 }
442 void
443 zoom(Arg *arg)
444 {
445 Client *c;
447 if(!sel)
448 return;
450 if(sel == getnext(clients) && sel->next) {
451 if((c = getnext(sel->next)))
452 sel = c;
453 }
455 /* pop */
456 if(sel->prev)
457 sel->prev->next = sel->next;
458 if(sel->next)
459 sel->next->prev = sel->prev;
460 sel->prev = NULL;
461 if(clients)
462 clients->prev = sel;
463 sel->next = clients;
464 clients = sel;
465 arrange(NULL);
466 focus(sel);
467 }