dwm-meillo

view client.c @ 123:61490330e90a

cleaned up code
author arg@10ksloc.org
date Thu, 20 Jul 2006 12:18:06 +0200
parents 329fd7dae530
children 75576e44c1d8
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"
7 #include <stdlib.h>
8 #include <string.h>
9 #include <X11/Xatom.h>
10 #include <X11/Xutil.h>
12 /* static functions */
14 static void
15 resizetitle(Client *c)
16 {
17 int i;
19 c->tw = 0;
20 for(i = 0; i < TLast; i++)
21 if(c->tags[i])
22 c->tw += textw(c->tags[i]);
23 c->tw += textw(c->name);
24 if(c->tw > c->w)
25 c->tw = c->w + 2;
26 c->tx = c->x + c->w - c->tw + 2;
27 c->ty = c->y;
28 if(c->tags[tsel])
29 XMoveResizeWindow(dpy, c->title, c->tx, c->ty, c->tw, c->th);
30 else
31 XMoveResizeWindow(dpy, c->title, c->tx + 2 * sw, c->ty, c->tw, c->th);
33 }
35 static int
36 xerrordummy(Display *dsply, XErrorEvent *ee)
37 {
38 return 0;
39 }
41 /* extern functions */
43 void
44 ban(Client *c)
45 {
46 XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
47 XMoveWindow(dpy, c->title, c->tx + 2 * sw, c->ty);
48 }
50 void
51 focus(Client *c)
52 {
53 Client *old = sel;
54 XEvent ev;
56 sel = c;
57 if(old && old != c)
58 drawtitle(old);
59 drawtitle(c);
60 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
61 XSync(dpy, False);
62 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
63 }
65 void
66 focusnext(Arg *arg)
67 {
68 Client *c;
70 if(!sel)
71 return;
73 if(!(c = getnext(sel->next, tsel)))
74 c = getnext(clients, tsel);
75 if(c) {
76 higher(c);
77 c->revert = sel;
78 focus(c);
79 }
80 }
82 void
83 focusprev(Arg *arg)
84 {
85 Client *c;
87 if(!sel)
88 return;
90 if((c = sel->revert && sel->revert->tags[tsel] ? sel->revert : NULL)) {
91 higher(c);
92 focus(c);
93 }
94 }
96 Client *
97 getclient(Window w)
98 {
99 Client *c;
101 for(c = clients; c; c = c->next)
102 if(c->win == w)
103 return c;
104 return NULL;
105 }
107 Client *
108 getctitle(Window w)
109 {
110 Client *c;
112 for(c = clients; c; c = c->next)
113 if(c->title == w)
114 return c;
115 return NULL;
116 }
118 void
119 gravitate(Client *c, Bool invert)
120 {
121 int dx = 0, dy = 0;
123 switch(c->grav) {
124 case StaticGravity:
125 case NorthWestGravity:
126 case NorthGravity:
127 case NorthEastGravity:
128 dy = c->border;
129 break;
130 case EastGravity:
131 case CenterGravity:
132 case WestGravity:
133 dy = -(c->h / 2) + c->border;
134 break;
135 case SouthEastGravity:
136 case SouthGravity:
137 case SouthWestGravity:
138 dy = -(c->h);
139 break;
140 default:
141 break;
142 }
144 switch (c->grav) {
145 case StaticGravity:
146 case NorthWestGravity:
147 case WestGravity:
148 case SouthWestGravity:
149 dx = c->border;
150 break;
151 case NorthGravity:
152 case CenterGravity:
153 case SouthGravity:
154 dx = -(c->w / 2) + c->border;
155 break;
156 case NorthEastGravity:
157 case EastGravity:
158 case SouthEastGravity:
159 dx = -(c->w + c->border);
160 break;
161 default:
162 break;
163 }
165 if(invert) {
166 dx = -dx;
167 dy = -dy;
168 }
169 c->x += dx;
170 c->y += dy;
171 }
173 void
174 higher(Client *c)
175 {
176 XRaiseWindow(dpy, c->win);
177 XRaiseWindow(dpy, c->title);
178 }
180 void
181 killclient(Arg *arg)
182 {
183 if(!sel)
184 return;
185 if(sel->proto & WM_PROTOCOL_DELWIN)
186 sendevent(sel->win, wmatom[WMProtocols], wmatom[WMDelete]);
187 else
188 XKillClient(dpy, sel->win);
189 }
191 void
192 lower(Client *c)
193 {
194 XLowerWindow(dpy, c->title);
195 XLowerWindow(dpy, c->win);
196 }
198 void
199 manage(Window w, XWindowAttributes *wa)
200 {
201 int diff;
202 Client *c;
203 Window trans;
204 XSetWindowAttributes twa;
206 c = emallocz(sizeof(Client));
207 c->win = w;
208 c->x = c->tx = wa->x;
209 c->y = c->ty = wa->y;
210 c->w = c->tw = wa->width;
211 c->h = wa->height;
212 c->th = bh;
214 if(c->y < bh)
215 c->y = c->ty = bh;
217 c->border = 1;
218 c->proto = getproto(c->win);
219 setsize(c);
220 XSelectInput(dpy, c->win,
221 StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
222 XGetTransientForHint(dpy, c->win, &trans);
223 twa.override_redirect = 1;
224 twa.background_pixmap = ParentRelative;
225 twa.event_mask = ExposureMask;
227 c->title = XCreateWindow(dpy, root, c->tx, c->ty, c->tw, c->th,
228 0, DefaultDepth(dpy, screen), CopyFromParent,
229 DefaultVisual(dpy, screen),
230 CWOverrideRedirect | CWBackPixmap | CWEventMask, &twa);
232 settags(c);
234 c->next = clients;
235 clients = c;
237 XGrabButton(dpy, Button1, ControlMask, c->win, False, ButtonPressMask,
238 GrabModeAsync, GrabModeSync, None, None);
239 XGrabButton(dpy, Button1, MODKEY, c->win, False, ButtonPressMask,
240 GrabModeAsync, GrabModeSync, None, None);
241 XGrabButton(dpy, Button2, MODKEY, c->win, False, ButtonPressMask,
242 GrabModeAsync, GrabModeSync, None, None);
243 XGrabButton(dpy, Button3, MODKEY, c->win, False, ButtonPressMask,
244 GrabModeAsync, GrabModeSync, None, None);
246 if(!c->isfloat)
247 c->isfloat = trans || (c->maxw && c->minw &&
248 (c->maxw == c->minw) && (c->maxh == c->minh));
251 settitle(c);
252 arrange(NULL);
254 /* mapping the window now prevents flicker */
255 if(c->tags[tsel]) {
256 XMapRaised(dpy, c->win);
257 XMapRaised(dpy, c->title);
258 focus(c);
259 }
260 else {
261 XMapRaised(dpy, c->win);
262 XMapRaised(dpy, c->title);
263 }
264 }
266 void
267 maximize(Arg *arg)
268 {
269 if(!sel)
270 return;
271 sel->x = sx;
272 sel->y = sy + bh;
273 sel->w = sw - 2 * sel->border;
274 sel->h = sh - 2 * sel->border - bh;
275 higher(sel);
276 resize(sel, False, TopLeft);
277 }
279 void
280 pop(Client *c)
281 {
282 Client **l;
284 for(l = &clients; *l && *l != c; l = &(*l)->next);
285 *l = c->next;
287 c->next = clients; /* pop */
288 clients = c;
289 arrange(NULL);
290 }
292 void
293 resize(Client *c, Bool inc, Corner sticky)
294 {
295 int bottom = c->y + c->h;
296 int right = c->x + c->w;
297 XConfigureEvent e;
299 if(inc) {
300 if(c->incw)
301 c->w -= (c->w - c->basew) % c->incw;
302 if(c->inch)
303 c->h -= (c->h - c->baseh) % c->inch;
304 }
305 if(c->x > sw) /* might happen on restart */
306 c->x = sw - c->w;
307 if(c->y > sh)
308 c->y = sh - c->h;
309 if(c->minw && c->w < c->minw)
310 c->w = c->minw;
311 if(c->minh && c->h < c->minh)
312 c->h = c->minh;
313 if(c->maxw && c->w > c->maxw)
314 c->w = c->maxw;
315 if(c->maxh && c->h > c->maxh)
316 c->h = c->maxh;
317 if(sticky == TopRight || sticky == BotRight)
318 c->x = right - c->w;
319 if(sticky == BotLeft || sticky == BotRight)
320 c->y = bottom - c->h;
322 resizetitle(c);
323 XSetWindowBorderWidth(dpy, c->win, 1);
324 XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
326 e.type = ConfigureNotify;
327 e.event = c->win;
328 e.window = c->win;
329 e.x = c->x;
330 e.y = c->y;
331 e.width = c->w;
332 e.height = c->h;
333 e.border_width = c->border;
334 e.above = None;
335 e.override_redirect = False;
336 XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&e);
337 XSync(dpy, False);
338 }
340 void
341 setsize(Client *c)
342 {
343 long msize;
344 XSizeHints size;
346 if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
347 size.flags = PSize;
348 c->flags = size.flags;
349 if(c->flags & PBaseSize) {
350 c->basew = size.base_width;
351 c->baseh = size.base_height;
352 }
353 else
354 c->basew = c->baseh = 0;
355 if(c->flags & PResizeInc) {
356 c->incw = size.width_inc;
357 c->inch = size.height_inc;
358 }
359 else
360 c->incw = c->inch = 0;
361 if(c->flags & PMaxSize) {
362 c->maxw = size.max_width;
363 c->maxh = size.max_height;
364 }
365 else
366 c->maxw = c->maxh = 0;
367 if(c->flags & PMinSize) {
368 c->minw = size.min_width;
369 c->minh = size.min_height;
370 }
371 else
372 c->minw = c->minh = 0;
373 if(c->flags & PWinGravity)
374 c->grav = size.win_gravity;
375 else
376 c->grav = NorthWestGravity;
377 }
379 void
380 settitle(Client *c)
381 {
382 char **list = NULL;
383 int n;
384 XTextProperty name;
386 name.nitems = 0;
387 c->name[0] = 0;
388 XGetTextProperty(dpy, c->win, &name, netatom[NetWMName]);
389 if(!name.nitems)
390 XGetWMName(dpy, c->win, &name);
391 if(!name.nitems)
392 return;
393 if(name.encoding == XA_STRING)
394 strncpy(c->name, (char *)name.value, sizeof(c->name));
395 else {
396 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
397 && n > 0 && *list)
398 {
399 strncpy(c->name, *list, sizeof(c->name));
400 XFreeStringList(list);
401 }
402 }
403 XFree(name.value);
404 resizetitle(c);
405 }
407 void
408 unmanage(Client *c)
409 {
410 Client **l;
412 XGrabServer(dpy);
413 XSetErrorHandler(xerrordummy);
415 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
416 XDestroyWindow(dpy, c->title);
418 for(l = &clients; *l && *l != c; l = &(*l)->next);
419 *l = c->next;
420 for(l = &clients; *l; l = &(*l)->next)
421 if((*l)->revert == c)
422 (*l)->revert = NULL;
423 if(sel == c)
424 sel = sel->revert ? sel->revert : clients;
426 free(c);
428 XSync(dpy, False);
429 XSetErrorHandler(xerror);
430 XUngrabServer(dpy);
431 arrange(NULL);
432 if(sel)
433 focus(sel);
434 }
436 void
437 zoom(Arg *arg)
438 {
439 Client *c;
441 if(!sel)
442 return;
444 if(sel == getnext(clients, tsel) && sel->next) {
445 if((c = getnext(sel->next, tsel)))
446 sel = c;
447 }
449 pop(sel);
450 focus(sel);
451 }