aewl

view client.c @ 120:1a394bc49922

using O3 instead of Os, binary size still < 40kb
author arg@10ksloc.org
date Thu, 20 Jul 2006 10:26:36 +0200
parents dfa5cd0969a6
children 61490330e90a
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;
100 for(c = clients; c; c = c->next)
101 if(c->win == w)
102 return c;
103 return NULL;
104 }
106 Client *
107 getctitle(Window w)
108 {
109 Client *c;
110 for(c = clients; c; c = c->next)
111 if(c->title == w)
112 return c;
113 return NULL;
114 }
116 void
117 gravitate(Client *c, Bool invert)
118 {
119 int dx = 0, dy = 0;
121 switch(c->grav) {
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 default:
139 break;
140 }
142 switch (c->grav) {
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 default:
160 break;
161 }
163 if(invert) {
164 dx = -dx;
165 dy = -dy;
166 }
167 c->x += dx;
168 c->y += dy;
169 }
171 void
172 higher(Client *c)
173 {
174 XRaiseWindow(dpy, c->win);
175 XRaiseWindow(dpy, c->title);
176 }
178 void
179 killclient(Arg *arg)
180 {
181 if(!sel)
182 return;
183 if(sel->proto & WM_PROTOCOL_DELWIN)
184 sendevent(sel->win, wmatom[WMProtocols], wmatom[WMDelete]);
185 else
186 XKillClient(dpy, sel->win);
187 }
189 void
190 lower(Client *c)
191 {
192 XLowerWindow(dpy, c->title);
193 XLowerWindow(dpy, c->win);
194 }
196 void
197 manage(Window w, XWindowAttributes *wa)
198 {
199 int diff;
200 Client *c;
201 XSetWindowAttributes twa;
202 Window trans;
204 c = emallocz(sizeof(Client));
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 if(c->y < bh)
213 c->y = c->ty = bh;
215 c->border = 1;
216 c->proto = getproto(c->win);
217 setsize(c);
218 XSelectInput(dpy, c->win,
219 StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
220 XGetTransientForHint(dpy, c->win, &trans);
221 twa.override_redirect = 1;
222 twa.background_pixmap = ParentRelative;
223 twa.event_mask = ExposureMask;
225 c->title = XCreateWindow(dpy, root, c->tx, c->ty, c->tw, c->th,
226 0, DefaultDepth(dpy, screen), CopyFromParent,
227 DefaultVisual(dpy, screen),
228 CWOverrideRedirect | CWBackPixmap | CWEventMask, &twa);
230 settags(c);
232 c->next = clients;
233 clients = c;
235 XGrabButton(dpy, Button1, ControlMask, c->win, False, ButtonPressMask,
236 GrabModeAsync, GrabModeSync, None, None);
237 XGrabButton(dpy, Button1, MODKEY, c->win, False, ButtonPressMask,
238 GrabModeAsync, GrabModeSync, None, None);
239 XGrabButton(dpy, Button2, MODKEY, c->win, False, ButtonPressMask,
240 GrabModeAsync, GrabModeSync, None, None);
241 XGrabButton(dpy, Button3, MODKEY, c->win, False, ButtonPressMask,
242 GrabModeAsync, GrabModeSync, None, None);
244 if(!c->isfloat)
245 c->isfloat = trans || (c->maxw && c->minw &&
246 (c->maxw == c->minw) && (c->maxh == c->minh));
249 settitle(c);
250 arrange(NULL);
252 /* mapping the window now prevents flicker */
253 if(c->tags[tsel]) {
254 XMapRaised(dpy, c->win);
255 XMapRaised(dpy, c->title);
256 focus(c);
257 }
258 else {
259 XMapRaised(dpy, c->win);
260 XMapRaised(dpy, c->title);
261 }
262 }
264 void
265 maximize(Arg *arg)
266 {
267 if(!sel)
268 return;
269 sel->x = sx;
270 sel->y = sy + bh;
271 sel->w = sw - 2 * sel->border;
272 sel->h = sh - 2 * sel->border - bh;
273 higher(sel);
274 resize(sel, False, TopLeft);
275 }
277 void
278 pop(Client *c)
279 {
280 Client **l;
281 for(l = &clients; *l && *l != c; l = &(*l)->next);
282 *l = c->next;
284 c->next = clients; /* pop */
285 clients = c;
286 arrange(NULL);
287 }
289 void
290 resize(Client *c, Bool inc, Corner sticky)
291 {
292 XConfigureEvent e;
293 int right = c->x + c->w;
294 int bottom = c->y + c->h;
296 if(inc) {
297 if(c->incw)
298 c->w -= (c->w - c->basew) % c->incw;
299 if(c->inch)
300 c->h -= (c->h - c->baseh) % c->inch;
301 }
302 if(c->x > sw) /* might happen on restart */
303 c->x = sw - c->w;
304 if(c->y > sh)
305 c->y = sh - c->h;
306 if(c->minw && c->w < c->minw)
307 c->w = c->minw;
308 if(c->minh && c->h < c->minh)
309 c->h = c->minh;
310 if(c->maxw && c->w > c->maxw)
311 c->w = c->maxw;
312 if(c->maxh && c->h > c->maxh)
313 c->h = c->maxh;
314 if(sticky == TopRight || sticky == BotRight)
315 c->x = right - c->w;
316 if(sticky == BotLeft || sticky == BotRight)
317 c->y = bottom - c->h;
319 resizetitle(c);
320 XSetWindowBorderWidth(dpy, c->win, 1);
321 XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
323 e.type = ConfigureNotify;
324 e.event = c->win;
325 e.window = c->win;
326 e.x = c->x;
327 e.y = c->y;
328 e.width = c->w;
329 e.height = c->h;
330 e.border_width = c->border;
331 e.above = None;
332 e.override_redirect = False;
333 XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&e);
334 XSync(dpy, False);
335 }
337 void
338 setsize(Client *c)
339 {
340 XSizeHints size;
341 long msize;
342 if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
343 size.flags = PSize;
344 c->flags = size.flags;
345 if(c->flags & PBaseSize) {
346 c->basew = size.base_width;
347 c->baseh = size.base_height;
348 }
349 else
350 c->basew = c->baseh = 0;
351 if(c->flags & PResizeInc) {
352 c->incw = size.width_inc;
353 c->inch = size.height_inc;
354 }
355 else
356 c->incw = c->inch = 0;
357 if(c->flags & PMaxSize) {
358 c->maxw = size.max_width;
359 c->maxh = size.max_height;
360 }
361 else
362 c->maxw = c->maxh = 0;
363 if(c->flags & PMinSize) {
364 c->minw = size.min_width;
365 c->minh = size.min_height;
366 }
367 else
368 c->minw = c->minh = 0;
369 if(c->flags & PWinGravity)
370 c->grav = size.win_gravity;
371 else
372 c->grav = NorthWestGravity;
373 }
375 void
376 settitle(Client *c)
377 {
378 XTextProperty name;
379 int n;
380 char **list = NULL;
382 name.nitems = 0;
383 c->name[0] = 0;
384 XGetTextProperty(dpy, c->win, &name, netatom[NetWMName]);
385 if(!name.nitems)
386 XGetWMName(dpy, c->win, &name);
387 if(!name.nitems)
388 return;
389 if(name.encoding == XA_STRING)
390 strncpy(c->name, (char *)name.value, sizeof(c->name));
391 else {
392 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
393 && n > 0 && *list)
394 {
395 strncpy(c->name, *list, sizeof(c->name));
396 XFreeStringList(list);
397 }
398 }
399 XFree(name.value);
400 resizetitle(c);
401 }
403 void
404 unmanage(Client *c)
405 {
406 Client **l;
408 XGrabServer(dpy);
409 XSetErrorHandler(xerrordummy);
411 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
412 XDestroyWindow(dpy, c->title);
414 for(l = &clients; *l && *l != c; l = &(*l)->next);
415 *l = c->next;
416 for(l = &clients; *l; l = &(*l)->next)
417 if((*l)->revert == c)
418 (*l)->revert = NULL;
419 if(sel == c)
420 sel = sel->revert ? sel->revert : clients;
422 free(c);
424 XSync(dpy, False);
425 XSetErrorHandler(xerror);
426 XUngrabServer(dpy);
427 arrange(NULL);
428 if(sel)
429 focus(sel);
430 }
432 void
433 zoom(Arg *arg)
434 {
435 Client *c;
437 if(!sel)
438 return;
440 if(sel == getnext(clients, tsel) && sel->next) {
441 if((c = getnext(sel->next, tsel)))
442 sel = c;
443 }
445 pop(sel);
446 focus(sel);
447 }