dwm-meillo

view client.c @ 461:9d23330a5268

removed a bunch of lines through making function signatures more consistent with my style ( { does not belong to a new line, if function args are single-lined)
author Anselm R. Garbe <arg@10kloc.org>
date Tue, 12 Sep 2006 10:57:28 +0200
parents ffb462fb7903
children bd32d89ed9d5
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 detachstack(Client *c) {
15 Client **tc;
16 for(tc=&stack; *tc && *tc != c; tc=&(*tc)->snext);
17 *tc = c->snext;
18 }
20 static void
21 grabbuttons(Client *c, Bool focused) {
22 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
24 if(focused) {
25 XGrabButton(dpy, Button1, MODKEY, c->win, False, BUTTONMASK,
26 GrabModeAsync, GrabModeSync, None, None);
27 XGrabButton(dpy, Button1, MODKEY | LockMask, c->win, False, BUTTONMASK,
28 GrabModeAsync, GrabModeSync, None, None);
29 XGrabButton(dpy, Button1, MODKEY | numlockmask, c->win, False, BUTTONMASK,
30 GrabModeAsync, GrabModeSync, None, None);
31 XGrabButton(dpy, Button1, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
32 GrabModeAsync, GrabModeSync, None, None);
34 XGrabButton(dpy, Button2, MODKEY, c->win, False, BUTTONMASK,
35 GrabModeAsync, GrabModeSync, None, None);
36 XGrabButton(dpy, Button2, MODKEY | LockMask, c->win, False, BUTTONMASK,
37 GrabModeAsync, GrabModeSync, None, None);
38 XGrabButton(dpy, Button2, MODKEY | numlockmask, c->win, False, BUTTONMASK,
39 GrabModeAsync, GrabModeSync, None, None);
40 XGrabButton(dpy, Button2, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
41 GrabModeAsync, GrabModeSync, None, None);
43 XGrabButton(dpy, Button3, MODKEY, c->win, False, BUTTONMASK,
44 GrabModeAsync, GrabModeSync, None, None);
45 XGrabButton(dpy, Button3, MODKEY | LockMask, c->win, False, BUTTONMASK,
46 GrabModeAsync, GrabModeSync, None, None);
47 XGrabButton(dpy, Button3, MODKEY | numlockmask, c->win, False, BUTTONMASK,
48 GrabModeAsync, GrabModeSync, None, None);
49 XGrabButton(dpy, Button3, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
50 GrabModeAsync, GrabModeSync, None, None);
51 }
52 else
53 XGrabButton(dpy, AnyButton, AnyModifier, c->win, False, BUTTONMASK,
54 GrabModeAsync, GrabModeSync, None, None);
55 }
57 static void
58 resizetitle(Client *c) {
59 c->tw = textw(c->name);
60 if(c->tw > c->w)
61 c->tw = c->w + 2;
62 c->tx = c->x + c->w - c->tw + 2;
63 c->ty = c->y;
64 if(isvisible(c))
65 XMoveResizeWindow(dpy, c->twin, c->tx, c->ty, c->tw, c->th);
66 else
67 XMoveResizeWindow(dpy, c->twin, c->tx + 2 * sw, c->ty, c->tw, c->th);
68 }
70 static int
71 xerrordummy(Display *dsply, XErrorEvent *ee) {
72 return 0;
73 }
75 /* extern functions */
77 void
78 ban(Client *c) {
79 XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
80 XMoveWindow(dpy, c->twin, c->tx + 2 * sw, c->ty);
81 }
83 void
84 focus(Client *c) {
85 Client *old;
87 if(!issel)
88 return;
89 if(!sel)
90 sel = c;
91 else if(sel != c) {
92 if(maximized)
93 togglemax(NULL);
94 old = sel;
95 sel = c;
96 if(old) {
97 grabbuttons(old, False);
98 drawtitle(old);
99 }
100 }
101 if(c) {
102 detachstack(c);
103 c->snext = stack;
104 stack = c;
105 grabbuttons(c, True);
106 drawtitle(c);
107 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
108 }
109 else
110 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
111 }
113 Client *
114 getclient(Window w) {
115 Client *c;
117 for(c = clients; c; c = c->next)
118 if(c->win == w)
119 return c;
120 return NULL;
121 }
123 Client *
124 getctitle(Window w) {
125 Client *c;
127 for(c = clients; c; c = c->next)
128 if(c->twin == w)
129 return c;
130 return NULL;
131 }
133 void
134 gravitate(Client *c, Bool invert) {
135 int dx = 0, dy = 0;
137 switch(c->grav) {
138 default:
139 break;
140 case StaticGravity:
141 case NorthWestGravity:
142 case NorthGravity:
143 case NorthEastGravity:
144 dy = c->border;
145 break;
146 case EastGravity:
147 case CenterGravity:
148 case WestGravity:
149 dy = -(c->h / 2) + c->border;
150 break;
151 case SouthEastGravity:
152 case SouthGravity:
153 case SouthWestGravity:
154 dy = -(c->h);
155 break;
156 }
158 switch (c->grav) {
159 default:
160 break;
161 case StaticGravity:
162 case NorthWestGravity:
163 case WestGravity:
164 case SouthWestGravity:
165 dx = c->border;
166 break;
167 case NorthGravity:
168 case CenterGravity:
169 case SouthGravity:
170 dx = -(c->w / 2) + c->border;
171 break;
172 case NorthEastGravity:
173 case EastGravity:
174 case SouthEastGravity:
175 dx = -(c->w + c->border);
176 break;
177 }
179 if(invert) {
180 dx = -dx;
181 dy = -dy;
182 }
183 c->x += dx;
184 c->y += dy;
185 }
187 void
188 killclient(Arg *arg) {
189 if(!sel)
190 return;
191 if(sel->proto & PROTODELWIN)
192 sendevent(sel->win, wmatom[WMProtocols], wmatom[WMDelete]);
193 else
194 XKillClient(dpy, sel->win);
195 }
197 void
198 manage(Window w, XWindowAttributes *wa) {
199 Client *c;
200 Window trans;
201 XSetWindowAttributes twa;
203 c = emallocz(sizeof(Client));
204 c->tags = emallocz(ntags * sizeof(Bool));
205 c->win = w;
206 c->x = c->tx = wa->x;
207 c->y = c->ty = wa->y;
208 c->w = c->tw = wa->width;
209 c->h = wa->height;
210 c->th = bh;
212 c->border = 0;
213 updatesize(c);
215 if(c->x + c->w + 2 > sw)
216 c->x = sw - c->w - 2;
217 if(c->x < 0)
218 c->x = 0;
219 if(c->y + c->h + 2 > sh)
220 c->y = sh - c->h - 2;
221 if(c->h != sh && c->y < bh)
222 c->y = bh;
224 c->proto = getproto(c->win);
225 XSelectInput(dpy, c->win,
226 StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
227 XGetTransientForHint(dpy, c->win, &trans);
228 twa.override_redirect = 1;
229 twa.background_pixmap = ParentRelative;
230 twa.event_mask = ExposureMask | EnterWindowMask;
232 c->twin = XCreateWindow(dpy, root, c->tx, c->ty, c->tw, c->th,
233 0, DefaultDepth(dpy, screen), CopyFromParent,
234 DefaultVisual(dpy, screen),
235 CWOverrideRedirect | CWBackPixmap | CWEventMask, &twa);
237 grabbuttons(c, False);
238 settags(c, getclient(trans));
239 if(!c->isfloat)
240 c->isfloat = trans
241 || (c->maxw && c->minw &&
242 c->maxw == c->minw && c->maxh == c->minh);
244 if(clients)
245 clients->prev = c;
246 c->next = clients;
247 c->snext = stack;
248 stack = clients = c;
250 updatetitle(c);
251 ban(c);
252 XMapWindow(dpy, c->win);
253 XMapWindow(dpy, c->twin);
254 if(isvisible(c))
255 focus(c);
256 arrange(NULL);
257 }
259 void
260 resize(Client *c, Bool sizehints, Corner sticky) {
261 int bottom = c->y + c->h;
262 int right = c->x + c->w;
263 XWindowChanges wc;
265 if(sizehints) {
266 if(c->incw)
267 c->w -= (c->w - c->basew) % c->incw;
268 if(c->inch)
269 c->h -= (c->h - c->baseh) % c->inch;
270 if(c->minw && c->w < c->minw)
271 c->w = c->minw;
272 if(c->minh && c->h < c->minh)
273 c->h = c->minh;
274 if(c->maxw && c->w > c->maxw)
275 c->w = c->maxw;
276 if(c->maxh && c->h > c->maxh)
277 c->h = c->maxh;
278 }
279 if(sticky == TopRight || sticky == BotRight)
280 c->x = right - c->w;
281 if(sticky == BotLeft || sticky == BotRight)
282 c->y = bottom - c->h;
284 resizetitle(c);
285 wc.x = c->x;
286 wc.y = c->y;
287 wc.width = c->w;
288 wc.height = c->h;
289 if(c->w == sw && c->h == sh)
290 wc.border_width = 0;
291 else
292 wc.border_width = 1;
293 XConfigureWindow(dpy, c->win, CWX | CWY | CWWidth | CWHeight | CWBorderWidth, &wc);
294 XSync(dpy, False);
295 }
297 void
298 updatesize(Client *c) {
299 long msize;
300 XSizeHints size;
302 if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
303 size.flags = PSize;
304 c->flags = size.flags;
305 if(c->flags & PBaseSize) {
306 c->basew = size.base_width;
307 c->baseh = size.base_height;
308 }
309 else
310 c->basew = c->baseh = 0;
311 if(c->flags & PResizeInc) {
312 c->incw = size.width_inc;
313 c->inch = size.height_inc;
314 }
315 else
316 c->incw = c->inch = 0;
317 if(c->flags & PMaxSize) {
318 c->maxw = size.max_width;
319 c->maxh = size.max_height;
320 }
321 else
322 c->maxw = c->maxh = 0;
323 if(c->flags & PMinSize) {
324 c->minw = size.min_width;
325 c->minh = size.min_height;
326 }
327 else
328 c->minw = c->minh = 0;
329 if(c->flags & PWinGravity)
330 c->grav = size.win_gravity;
331 else
332 c->grav = NorthWestGravity;
333 }
335 void
336 updatetitle(Client *c) {
337 char **list = NULL;
338 int n;
339 XTextProperty name;
341 name.nitems = 0;
342 c->name[0] = 0;
343 XGetTextProperty(dpy, c->win, &name, netatom[NetWMName]);
344 if(!name.nitems)
345 XGetWMName(dpy, c->win, &name);
346 if(!name.nitems)
347 return;
348 if(name.encoding == XA_STRING)
349 strncpy(c->name, (char *)name.value, sizeof(c->name));
350 else {
351 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
352 && n > 0 && *list)
353 {
354 strncpy(c->name, *list, sizeof(c->name));
355 XFreeStringList(list);
356 }
357 }
358 XFree(name.value);
359 resizetitle(c);
360 }
362 void
363 togglemax(Arg *arg) {
364 int ox, oy, ow, oh;
365 Client *c;
366 XEvent ev;
368 if(!sel)
369 return;
371 if((maximized = !maximized)) {
372 ox = sel->x;
373 oy = sel->y;
374 ow = sel->w;
375 oh = sel->h;
376 sel->x = sx;
377 sel->y = sy + bh;
378 sel->w = sw - 2;
379 sel->h = sh - 2 - bh;
381 restack();
382 for(c = getnext(clients); c; c = getnext(c->next))
383 if(c != sel)
384 ban(c);
385 resize(sel, arrange == dofloat, TopLeft);
387 sel->x = ox;
388 sel->y = oy;
389 sel->w = ow;
390 sel->h = oh;
391 }
392 else
393 arrange(NULL);
394 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
395 }
397 void
398 unmanage(Client *c) {
399 Client *nc;
401 XGrabServer(dpy);
402 XSetErrorHandler(xerrordummy);
404 detach(c);
405 detachstack(c);
406 if(sel == c) {
407 for(nc = stack; nc && !isvisible(nc); nc = nc->snext);
408 focus(nc);
409 }
411 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
412 XDestroyWindow(dpy, c->twin);
414 free(c->tags);
415 free(c);
417 XSync(dpy, False);
418 XSetErrorHandler(xerror);
419 XUngrabServer(dpy);
420 arrange(NULL);
421 }