aewl

view client.c @ 155:55f74b956675

small fixes to dwm.html
author arg@10ksloc.org
date Tue, 01 Aug 2006 16:35:54 +0200
parents 5267e1204367
children 93012e947eae
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(sel->ismax)
74 togglemax(NULL);
76 if(!(c = getnext(sel->next)))
77 c = getnext(clients);
78 if(c) {
79 higher(c);
80 focus(c);
81 }
82 }
84 void
85 focusprev(Arg *arg)
86 {
87 Client *c;
89 if(!sel)
90 return;
92 if(sel->ismax)
93 togglemax(NULL);
95 if(!(c = getprev(sel->prev))) {
96 for(c = clients; c && c->next; c = c->next);
97 c = getprev(c);
98 }
99 if(c) {
100 higher(c);
101 focus(c);
102 }
103 }
105 Client *
106 getclient(Window w)
107 {
108 Client *c;
110 for(c = clients; c; c = c->next)
111 if(c->win == w)
112 return c;
113 return NULL;
114 }
116 Client *
117 getctitle(Window w)
118 {
119 Client *c;
121 for(c = clients; c; c = c->next)
122 if(c->title == w)
123 return c;
124 return NULL;
125 }
127 void
128 gravitate(Client *c, Bool invert)
129 {
130 int dx = 0, dy = 0;
132 switch(c->grav) {
133 default:
134 break;
135 case StaticGravity:
136 case NorthWestGravity:
137 case NorthGravity:
138 case NorthEastGravity:
139 dy = c->border;
140 break;
141 case EastGravity:
142 case CenterGravity:
143 case WestGravity:
144 dy = -(c->h / 2) + c->border;
145 break;
146 case SouthEastGravity:
147 case SouthGravity:
148 case SouthWestGravity:
149 dy = -(c->h);
150 break;
151 }
153 switch (c->grav) {
154 default:
155 break;
156 case StaticGravity:
157 case NorthWestGravity:
158 case WestGravity:
159 case SouthWestGravity:
160 dx = c->border;
161 break;
162 case NorthGravity:
163 case CenterGravity:
164 case SouthGravity:
165 dx = -(c->w / 2) + c->border;
166 break;
167 case NorthEastGravity:
168 case EastGravity:
169 case SouthEastGravity:
170 dx = -(c->w + c->border);
171 break;
172 }
174 if(invert) {
175 dx = -dx;
176 dy = -dy;
177 }
178 c->x += dx;
179 c->y += dy;
180 }
182 void
183 higher(Client *c)
184 {
185 XRaiseWindow(dpy, c->win);
186 XRaiseWindow(dpy, c->title);
187 }
189 void
190 killclient(Arg *arg)
191 {
192 if(!sel)
193 return;
194 if(sel->proto & WM_PROTOCOL_DELWIN)
195 sendevent(sel->win, wmatom[WMProtocols], wmatom[WMDelete]);
196 else
197 XKillClient(dpy, sel->win);
198 }
200 void
201 lower(Client *c)
202 {
203 XLowerWindow(dpy, c->title);
204 XLowerWindow(dpy, c->win);
205 }
207 void
208 manage(Window w, XWindowAttributes *wa)
209 {
210 Client *c;
211 Window trans;
212 XSetWindowAttributes twa;
214 c = emallocz(sizeof(Client));
215 c->win = w;
216 c->x = c->tx = wa->x;
217 c->y = c->ty = wa->y;
218 c->w = c->tw = wa->width;
219 c->h = wa->height;
220 c->th = bh;
222 if(c->y < bh)
223 c->y = c->ty = bh;
225 c->border = 1;
226 c->proto = getproto(c->win);
227 setsize(c);
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;
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 settags(c);
242 if(clients)
243 clients->prev = c;
244 c->next = clients;
245 clients = c;
247 XGrabButton(dpy, Button1, MODKEY, c->win, False, BUTTONMASK,
248 GrabModeAsync, GrabModeSync, None, None);
249 XGrabButton(dpy, Button2, MODKEY, c->win, False, BUTTONMASK,
250 GrabModeAsync, GrabModeSync, None, None);
251 XGrabButton(dpy, Button3, MODKEY, c->win, False, BUTTONMASK,
252 GrabModeAsync, GrabModeSync, None, None);
254 if(!c->isfloat)
255 c->isfloat = trans || (c->maxw && c->minw &&
256 (c->maxw == c->minw) && (c->maxh == c->minh));
259 settitle(c);
260 arrange(NULL);
262 /* mapping the window now prevents flicker */
263 XMapRaised(dpy, c->win);
264 XMapRaised(dpy, c->title);
265 if(c->tags[tsel])
266 focus(c);
267 }
269 void
270 resize(Client *c, Bool sizehints, Corner sticky)
271 {
272 int bottom = c->y + c->h;
273 int right = c->x + c->w;
274 XConfigureEvent e;
276 if(sizehints) {
277 if(c->incw)
278 c->w -= (c->w - c->basew) % c->incw;
279 if(c->inch)
280 c->h -= (c->h - c->baseh) % c->inch;
281 if(c->minw && c->w < c->minw)
282 c->w = c->minw;
283 if(c->minh && c->h < c->minh)
284 c->h = c->minh;
285 if(c->maxw && c->w > c->maxw)
286 c->w = c->maxw;
287 if(c->maxh && c->h > c->maxh)
288 c->h = c->maxh;
289 }
290 if(c->x > sw) /* might happen on restart */
291 c->x = sw - c->w;
292 if(c->y > sh)
293 c->y = sh - c->h;
294 if(sticky == TopRight || sticky == BotRight)
295 c->x = right - c->w;
296 if(sticky == BotLeft || sticky == BotRight)
297 c->y = bottom - c->h;
299 resizetitle(c);
300 XSetWindowBorderWidth(dpy, c->win, 1);
301 XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
303 e.type = ConfigureNotify;
304 e.event = c->win;
305 e.window = c->win;
306 e.x = c->x;
307 e.y = c->y;
308 e.width = c->w;
309 e.height = c->h;
310 e.border_width = c->border;
311 e.above = None;
312 e.override_redirect = False;
313 XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&e);
314 XSync(dpy, False);
315 }
317 void
318 setsize(Client *c)
319 {
320 long msize;
321 XSizeHints size;
323 if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
324 size.flags = PSize;
325 c->flags = size.flags;
326 if(c->flags & PBaseSize) {
327 c->basew = size.base_width;
328 c->baseh = size.base_height;
329 }
330 else
331 c->basew = c->baseh = 0;
332 if(c->flags & PResizeInc) {
333 c->incw = size.width_inc;
334 c->inch = size.height_inc;
335 }
336 else
337 c->incw = c->inch = 0;
338 if(c->flags & PMaxSize) {
339 c->maxw = size.max_width;
340 c->maxh = size.max_height;
341 }
342 else
343 c->maxw = c->maxh = 0;
344 if(c->flags & PMinSize) {
345 c->minw = size.min_width;
346 c->minh = size.min_height;
347 }
348 else
349 c->minw = c->minh = 0;
350 if(c->flags & PWinGravity)
351 c->grav = size.win_gravity;
352 else
353 c->grav = NorthWestGravity;
354 }
356 void
357 settitle(Client *c)
358 {
359 char **list = NULL;
360 int n;
361 XTextProperty name;
363 name.nitems = 0;
364 c->name[0] = 0;
365 XGetTextProperty(dpy, c->win, &name, netatom[NetWMName]);
366 if(!name.nitems)
367 XGetWMName(dpy, c->win, &name);
368 if(!name.nitems)
369 return;
370 if(name.encoding == XA_STRING)
371 strncpy(c->name, (char *)name.value, sizeof(c->name));
372 else {
373 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
374 && n > 0 && *list)
375 {
376 strncpy(c->name, *list, sizeof(c->name));
377 XFreeStringList(list);
378 }
379 }
380 XFree(name.value);
381 resizetitle(c);
382 }
384 void
385 togglemax(Arg *arg)
386 {
387 int ox, oy, ow, oh;
388 XEvent ev;
390 if(!sel)
391 return;
393 if((sel->ismax = !sel->ismax)) {
394 ox = sel->x;
395 oy = sel->y;
396 ow = sel->w;
397 oh = sel->h;
398 sel->x = sx;
399 sel->y = sy + bh;
400 sel->w = sw - 2 * sel->border;
401 sel->h = sh - 2 * sel->border - bh;
403 higher(sel);
404 resize(sel, False, TopLeft);
406 sel->x = ox;
407 sel->y = oy;
408 sel->w = ow;
409 sel->h = oh;
410 }
411 else
412 resize(sel, False, TopLeft);
413 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
414 }
416 void
417 unmanage(Client *c)
418 {
419 XGrabServer(dpy);
420 XSetErrorHandler(xerrordummy);
422 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
423 XDestroyWindow(dpy, c->title);
425 if(c->prev)
426 c->prev->next = c->next;
427 if(c->next)
428 c->next->prev = c->prev;
429 if(c == clients)
430 clients = c->next;
431 if(sel == c) {
432 sel = getnext(c->next);
433 if(!sel)
434 sel = getprev(c->prev);
435 if(!sel)
436 sel = clients;
437 }
438 free(c);
440 XSync(dpy, False);
441 XSetErrorHandler(xerror);
442 XUngrabServer(dpy);
443 arrange(NULL);
444 if(sel)
445 focus(sel);
446 }
448 void
449 zoom(Arg *arg)
450 {
451 Client *c;
453 if(!sel)
454 return;
456 if(sel == getnext(clients) && sel->next) {
457 if((c = getnext(sel->next)))
458 sel = c;
459 }
461 /* pop */
462 if(sel->prev)
463 sel->prev->next = sel->next;
464 if(sel->next)
465 sel->next->prev = sel->prev;
466 sel->prev = NULL;
467 if(clients)
468 clients->prev = sel;
469 sel->next = clients;
470 clients = sel;
471 arrange(NULL);
472 focus(sel);
473 }