aewl

view client.c @ 328:083f1f3e1e93

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