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