rev |
line source |
arg@644
|
1 /* (C)opyright MMVI-MMVII Anselm R. Garbe <garbeam at gmail dot com>
|
meillo@759
|
2 * (C)opyright MMVIII markus schnalke <meillo at marmaro dot de>
|
garbeam@0
|
3 * See LICENSE file for license details.
|
meillo@754
|
4 *
|
meillo@754
|
5 * dynamic window manager is designed like any other X client as well. It is
|
meillo@754
|
6 * driven through handling X events. In contrast to other X clients, a window
|
meillo@754
|
7 * manager selects for SubstructureRedirectMask on the root window, to receive
|
meillo@754
|
8 * events about window (dis-)appearance. Only one X connection at a time is
|
meillo@754
|
9 * allowed to select for this event mask.
|
meillo@754
|
10 *
|
meillo@754
|
11 * Calls to fetch an X event from the event queue are blocking. Due reading
|
meillo@754
|
12 * status text from standard input, a select()-driven main loop has been
|
meillo@754
|
13 * implemented which selects for reads on the X connection and STDIN_FILENO to
|
meillo@754
|
14 * handle all data smoothly. The event handlers of dwm are organized in an
|
meillo@754
|
15 * array which is accessed whenever a new event has been fetched. This allows
|
meillo@754
|
16 * event dispatching in O(1) time.
|
meillo@754
|
17 *
|
meillo@754
|
18 * Each child of the root window is called a client, except windows which have
|
meillo@754
|
19 * set the override_redirect flag. Clients are organized in a global
|
meillo@754
|
20 * doubly-linked client list, the focus history is remembered through a global
|
meillo@766
|
21 * stack list. [...]
|
meillo@754
|
22 *
|
meillo@754
|
23 * Keys and tagging rules are organized as arrays and defined in the config.h
|
meillo@771
|
24 * file. [...] The current mode is represented by the arrange() function
|
meillo@771
|
25 * pointer, which wether points to [domax()] or dotile().
|
meillo@754
|
26 *
|
meillo@754
|
27 * To understand everything else, start reading main.c:main().
|
meillo@766
|
28 *
|
meillo@766
|
29 * -- and now about aewl --
|
meillo@766
|
30 *
|
meillo@766
|
31 * aewl is a stripped down dwm. It stated as a patchset, but finally forked off
|
meillo@766
|
32 * completely. The reason for this was the increasing gap between my wish to
|
meillo@766
|
33 * stay where dwm was, and dwm direction to go further. Further more did I
|
meillo@766
|
34 * always use only a small subset of dwm's features, so condencing dwm had been
|
meillo@766
|
35 * my wish for a long time.
|
meillo@766
|
36 *
|
meillo@766
|
37 * In aewl clients are either tagged or not (only one tag). Visible are either
|
meillo@766
|
38 * all tagged clients or all without the tag.
|
garbeam@0
|
39 */
|
garbeam@0
|
40
|
meillo@766
|
41
|
garbeam@59
|
42 #include <errno.h>
|
arg@619
|
43 #include <locale.h>
|
garbeam@0
|
44 #include <stdio.h>
|
meillo@754
|
45 #include <stdarg.h>
|
garbeam@0
|
46 #include <stdlib.h>
|
garbeam@57
|
47 #include <string.h>
|
garbeam@59
|
48 #include <unistd.h>
|
arg@138
|
49 #include <sys/select.h>
|
meillo@776
|
50 #include <sys/signal.h>
|
meillo@754
|
51 #include <sys/types.h>
|
meillo@754
|
52 #include <sys/wait.h>
|
garbeam@0
|
53 #include <X11/cursorfont.h>
|
arg@291
|
54 #include <X11/keysym.h>
|
garbeam@0
|
55 #include <X11/Xatom.h>
|
meillo@754
|
56 #include <X11/Xlib.h>
|
garbeam@0
|
57 #include <X11/Xproto.h>
|
meillo@754
|
58 #include <X11/Xutil.h>
|
garbeam@0
|
59
|
meillo@769
|
60 #include "config.h"
|
meillo@769
|
61
|
meillo@769
|
62
|
meillo@754
|
63 /* mask shorthands, used in event.c and client.c */
|
meillo@754
|
64 #define BUTTONMASK (ButtonPressMask | ButtonReleaseMask)
|
arg@333
|
65
|
meillo@754
|
66 enum { NetSupported, NetWMName, NetLast }; /* EWMH atoms */
|
meillo@754
|
67 enum { WMProtocols, WMDelete, WMState, WMLast }; /* default atoms */
|
meillo@754
|
68 enum { CurNormal, CurResize, CurMove, CurLast }; /* cursor */
|
meillo@764
|
69 enum { ColFG, ColBG, ColLast }; /* color */
|
meillo@754
|
70
|
meillo@754
|
71 typedef struct {
|
meillo@754
|
72 int ascent;
|
meillo@754
|
73 int descent;
|
meillo@754
|
74 int height;
|
meillo@754
|
75 XFontSet set;
|
meillo@754
|
76 XFontStruct *xfont;
|
meillo@754
|
77 } Fnt;
|
meillo@754
|
78
|
meillo@754
|
79 typedef struct {
|
meillo@754
|
80 int x, y, w, h;
|
meillo@754
|
81 unsigned long norm[ColLast];
|
meillo@754
|
82 unsigned long sel[ColLast];
|
meillo@754
|
83 Drawable drawable;
|
meillo@754
|
84 Fnt font;
|
meillo@754
|
85 GC gc;
|
meillo@754
|
86 } DC; /* draw context */
|
meillo@754
|
87
|
meillo@754
|
88 typedef struct Client Client;
|
meillo@754
|
89 struct Client {
|
meillo@754
|
90 char name[256];
|
meillo@754
|
91 int x, y, w, h;
|
meillo@754
|
92 int rx, ry, rw, rh; /* revert geometry */
|
meillo@754
|
93 int basew, baseh, incw, inch, maxw, maxh, minw, minh;
|
meillo@754
|
94 int minax, minay, maxax, maxay;
|
meillo@754
|
95 long flags;
|
meillo@754
|
96 unsigned int border;
|
meillo@754
|
97 Bool isfixed, isfloat, ismax;
|
meillo@766
|
98 Bool tag;
|
meillo@754
|
99 Client *next;
|
meillo@754
|
100 Client *prev;
|
meillo@754
|
101 Client *snext;
|
meillo@754
|
102 Window win;
|
meillo@754
|
103 };
|
meillo@754
|
104
|
meillo@754
|
105 typedef struct {
|
meillo@767
|
106 const char* class;
|
meillo@767
|
107 const char* instance;
|
meillo@767
|
108 const char* title;
|
meillo@766
|
109 int tag;
|
meillo@754
|
110 Bool isfloat;
|
meillo@754
|
111 } Rule;
|
meillo@754
|
112
|
meillo@754
|
113
|
meillo@754
|
114 typedef struct {
|
meillo@754
|
115 unsigned long mod;
|
meillo@754
|
116 KeySym keysym;
|
meillo@758
|
117 void (*func)(const char* cmd);
|
meillo@758
|
118 const char* cmd;
|
meillo@754
|
119 } Key;
|
meillo@754
|
120
|
meillo@754
|
121
|
meillo@754
|
122 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
|
meillo@754
|
123 #define MOUSEMASK (BUTTONMASK | PointerMotionMask)
|
meillo@754
|
124
|
meillo@754
|
125
|
meillo@754
|
126
|
meillo@754
|
127 char stext[256]; /* status text */
|
meillo@771
|
128 int bh; /* bar height */
|
meillo@754
|
129 int screen, sx, sy, sw, sh; /* screen geometry */
|
meillo@754
|
130 int wax, way, wah, waw; /* windowarea geometry */
|
meillo@754
|
131 unsigned int nmaster; /* number of master clients */
|
meillo@761
|
132 unsigned int numlockmask; /* dynamic lock mask */
|
meillo@754
|
133 void (*handler[LASTEvent])(XEvent *); /* event handler */
|
meillo@754
|
134 void (*arrange)(void); /* arrange function, indicates mode */
|
arg@333
|
135 Atom wmatom[WMLast], netatom[NetLast];
|
meillo@772
|
136 Bool running = True;
|
meillo@772
|
137 Bool selscreen = True;
|
meillo@772
|
138 Bool seltag;
|
meillo@772
|
139 Client* clients = NULL; /* global client list */
|
meillo@772
|
140 Client* stack = NULL; /* global client stack */
|
meillo@772
|
141 Client* sel = NULL; /* selected client */
|
meillo@754
|
142 Cursor cursor[CurLast];
|
meillo@772
|
143 DC dc = {0}; /* global draw context */
|
meillo@754
|
144 Display *dpy;
|
meillo@754
|
145 Window root, barwin;
|
meillo@754
|
146
|
meillo@754
|
147 static int (*xerrorxlib)(Display *, XErrorEvent *);
|
meillo@754
|
148 static Bool otherwm, readin;
|
meillo@754
|
149 static unsigned int len = 0;
|
meillo@754
|
150
|
meillo@754
|
151
|
meillo@772
|
152
|
meillo@754
|
153 void configure(Client *c); /* send synthetic configure event */
|
meillo@754
|
154 void focus(Client *c); /* focus c, c may be NULL */
|
meillo@754
|
155 Client *getclient(Window w); /* return client of w */
|
meillo@754
|
156 Bool isprotodel(Client *c); /* returns True if c->win supports wmatom[WMDelete] */
|
meillo@754
|
157 void manage(Window w, XWindowAttributes *wa); /* manage new client */
|
meillo@754
|
158 void resize(Client *c, Bool sizehints); /* resize c*/
|
meillo@754
|
159 void updatesizehints(Client *c); /* update the size hint variables of c */
|
meillo@754
|
160 void updatetitle(Client *c); /* update the name of c */
|
meillo@754
|
161 void unmanage(Client *c); /* destroy c */
|
meillo@754
|
162
|
meillo@754
|
163 void drawstatus(void); /* draw the bar */
|
meillo@754
|
164 unsigned long getcolor(const char *colstr); /* return color of colstr */
|
meillo@754
|
165 void setfont(const char *fontstr); /* set the font for DC */
|
meillo@754
|
166 unsigned int textw(const char *text); /* return the width of text in px*/
|
meillo@754
|
167
|
meillo@754
|
168 void grabkeys(void); /* grab all keys defined in config.h */
|
meillo@754
|
169 void procevent(void); /* process pending X events */
|
meillo@754
|
170
|
meillo@754
|
171 void sendevent(Window w, Atom a, long value); /* send synthetic event to w */
|
meillo@772
|
172 int xerror(Display *dsply, XErrorEvent *ee); /* X error handler */
|
meillo@754
|
173
|
meillo@754
|
174 Client *getnext(Client *c); /* returns next visible client */
|
meillo@766
|
175 void settag(Client *c, Client *trans); /* sets tag of c */
|
meillo@754
|
176
|
meillo@754
|
177 void *emallocz(unsigned int size); /* allocates zero-initialized memory, exits on error */
|
meillo@754
|
178 void eprint(const char *errstr, ...); /* prints errstr and exits with 1 */
|
meillo@754
|
179
|
meillo@754
|
180 void detach(Client *c); /* detaches c from global client list */
|
meillo@754
|
181 void dotile(void); /* arranges all windows tiled */
|
meillo@754
|
182 void domax(void); /* arranges all windows fullscreen */
|
meillo@758
|
183 Bool isvisible(Client *c); /* returns True if client is visible */
|
meillo@758
|
184 void restack(void); /* restores z layers of all clients */
|
meillo@758
|
185
|
meillo@766
|
186 void toggleview(void); /* toggle the view */
|
meillo@760
|
187 void focusnext(void); /* focuses next visible client */
|
meillo@760
|
188 void zoom(void); /* zooms the focused client to master area */
|
meillo@760
|
189 void killclient(void); /* kill c nicely */
|
meillo@772
|
190 void quit(void); /* quit nicely */
|
meillo@761
|
191 void togglemode(void); /* toggles global arrange function (dotile/domax) */
|
meillo@760
|
192 void togglefloat(void); /* toggles focusesd client between floating/non-floating state */
|
meillo@760
|
193 void incnmaster(void); /* increments nmaster */
|
meillo@760
|
194 void decnmaster(void); /* decrements nmaster */
|
meillo@766
|
195 void toggletag(void); /* toggles tag of c */
|
meillo@758
|
196 void spawn(const char* cmd); /* forks a new subprocess with cmd */
|
meillo@754
|
197
|
meillo@754
|
198
|
meillo@754
|
199
|
meillo@773
|
200 RULES
|
meillo@773
|
201 KEYS
|
meillo@773
|
202
|
meillo@773
|
203
|
meillo@754
|
204
|
meillo@754
|
205
|
meillo@754
|
206
|
meillo@754
|
207
|
meillo@754
|
208
|
meillo@754
|
209
|
meillo@754
|
210
|
meillo@754
|
211 /* from view.c */
|
meillo@754
|
212
|
meillo@773
|
213 Client *
|
meillo@754
|
214 nexttiled(Client *c) {
|
meillo@754
|
215 for(c = getnext(c); c && c->isfloat; c = getnext(c->next));
|
meillo@754
|
216 return c;
|
meillo@754
|
217 }
|
meillo@754
|
218
|
meillo@773
|
219 void
|
meillo@754
|
220 togglemax(Client *c) {
|
meillo@754
|
221 XEvent ev;
|
meillo@754
|
222
|
meillo@754
|
223 if(c->isfixed)
|
meillo@754
|
224 return;
|
meillo@754
|
225
|
meillo@754
|
226 if((c->ismax = !c->ismax)) {
|
meillo@769
|
227 c->rx = c->x;
|
meillo@769
|
228 c->ry = c->y;
|
meillo@769
|
229 c->rw = c->w;
|
meillo@769
|
230 c->rh = c->h;
|
meillo@769
|
231 c->x = wax;
|
meillo@769
|
232 c->y = way;
|
meillo@769
|
233 c->w = waw - 2 * BORDERPX;
|
meillo@769
|
234 c->h = wah - 2 * BORDERPX;
|
meillo@769
|
235 } else {
|
meillo@754
|
236 c->x = c->rx;
|
meillo@754
|
237 c->y = c->ry;
|
meillo@754
|
238 c->w = c->rw;
|
meillo@754
|
239 c->h = c->rh;
|
meillo@754
|
240 }
|
meillo@769
|
241 resize(c, False);
|
meillo@754
|
242 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
|
meillo@754
|
243 }
|
meillo@754
|
244
|
meillo@754
|
245
|
meillo@754
|
246
|
meillo@754
|
247
|
meillo@754
|
248 void
|
meillo@754
|
249 detach(Client *c) {
|
meillo@754
|
250 if(c->prev)
|
meillo@754
|
251 c->prev->next = c->next;
|
meillo@754
|
252 if(c->next)
|
meillo@754
|
253 c->next->prev = c->prev;
|
meillo@754
|
254 if(c == clients)
|
meillo@754
|
255 clients = c->next;
|
meillo@754
|
256 c->next = c->prev = NULL;
|
meillo@754
|
257 }
|
meillo@754
|
258
|
meillo@754
|
259 void
|
meillo@754
|
260 dotile(void) {
|
meillo@754
|
261 unsigned int i, n, mw, mh, tw, th;
|
meillo@754
|
262 Client *c;
|
meillo@754
|
263
|
meillo@754
|
264 for(n = 0, c = nexttiled(clients); c; c = nexttiled(c->next))
|
meillo@754
|
265 n++;
|
meillo@754
|
266 /* window geoms */
|
meillo@754
|
267 mh = (n > nmaster) ? wah / nmaster : wah / (n > 0 ? n : 1);
|
meillo@754
|
268 mw = (n > nmaster) ? waw / 2 : waw;
|
meillo@754
|
269 th = (n > nmaster) ? wah / (n - nmaster) : 0;
|
meillo@754
|
270 tw = waw - mw;
|
meillo@754
|
271
|
meillo@754
|
272 for(i = 0, c = clients; c; c = c->next)
|
meillo@754
|
273 if(isvisible(c)) {
|
meillo@754
|
274 if(c->isfloat) {
|
meillo@754
|
275 resize(c, True);
|
meillo@754
|
276 continue;
|
meillo@754
|
277 }
|
meillo@754
|
278 c->ismax = False;
|
meillo@754
|
279 c->x = wax;
|
meillo@754
|
280 c->y = way;
|
meillo@754
|
281 if(i < nmaster) {
|
meillo@754
|
282 c->y += i * mh;
|
meillo@754
|
283 c->w = mw - 2 * BORDERPX;
|
meillo@754
|
284 c->h = mh - 2 * BORDERPX;
|
meillo@754
|
285 }
|
meillo@754
|
286 else { /* tile window */
|
meillo@754
|
287 c->x += mw;
|
meillo@754
|
288 c->w = tw - 2 * BORDERPX;
|
meillo@754
|
289 if(th > 2 * BORDERPX) {
|
meillo@754
|
290 c->y += (i - nmaster) * th;
|
meillo@754
|
291 c->h = th - 2 * BORDERPX;
|
meillo@754
|
292 }
|
meillo@754
|
293 else /* fallback if th <= 2 * BORDERPX */
|
meillo@754
|
294 c->h = wah - 2 * BORDERPX;
|
meillo@754
|
295 }
|
meillo@754
|
296 resize(c, False);
|
meillo@754
|
297 i++;
|
meillo@754
|
298 }
|
meillo@754
|
299 else
|
meillo@754
|
300 XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
|
meillo@754
|
301 if(!sel || !isvisible(sel)) {
|
meillo@754
|
302 for(c = stack; c && !isvisible(c); c = c->snext);
|
meillo@754
|
303 focus(c);
|
meillo@754
|
304 }
|
meillo@754
|
305 restack();
|
meillo@754
|
306 }
|
meillo@754
|
307
|
meillo@754
|
308 void
|
meillo@754
|
309 domax(void) {
|
meillo@754
|
310 Client *c;
|
meillo@754
|
311
|
meillo@754
|
312 for(c = clients; c; c = c->next) {
|
meillo@754
|
313 if(isvisible(c)) {
|
meillo@754
|
314 if(c->isfloat) {
|
meillo@754
|
315 resize(c, True);
|
meillo@754
|
316 continue;
|
meillo@754
|
317 }
|
meillo@768
|
318 c->ismax = True;
|
meillo@769
|
319 c->x = wax;
|
meillo@769
|
320 c->y = way;
|
meillo@769
|
321 c->w = waw - 2 * BORDERPX;
|
meillo@769
|
322 c->h = wah - 2 * BORDERPX;
|
meillo@769
|
323 resize(c, False);
|
meillo@754
|
324 } else {
|
meillo@754
|
325 XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
|
meillo@754
|
326 }
|
meillo@754
|
327 }
|
meillo@754
|
328 if(!sel || !isvisible(sel)) {
|
meillo@754
|
329 for(c = stack; c && !isvisible(c); c = c->snext);
|
meillo@754
|
330 focus(c);
|
meillo@754
|
331 }
|
meillo@754
|
332 restack();
|
meillo@754
|
333 }
|
meillo@754
|
334
|
meillo@754
|
335 void
|
meillo@756
|
336 focusnext() {
|
meillo@754
|
337 Client *c;
|
meillo@754
|
338
|
meillo@754
|
339 if(!sel)
|
meillo@754
|
340 return;
|
meillo@754
|
341 if(!(c = getnext(sel->next)))
|
meillo@754
|
342 c = getnext(clients);
|
meillo@754
|
343 if(c) {
|
meillo@754
|
344 focus(c);
|
meillo@754
|
345 restack();
|
meillo@754
|
346 }
|
meillo@754
|
347 }
|
meillo@754
|
348
|
meillo@754
|
349 void
|
meillo@756
|
350 incnmaster() {
|
meillo@761
|
351 if(wah / (nmaster + 1) <= 2 * BORDERPX)
|
meillo@754
|
352 return;
|
meillo@756
|
353 nmaster++;
|
meillo@756
|
354 if(sel)
|
meillo@756
|
355 arrange();
|
meillo@756
|
356 }
|
meillo@756
|
357
|
meillo@756
|
358 void
|
meillo@756
|
359 decnmaster() {
|
meillo@761
|
360 if(nmaster <= 1)
|
meillo@756
|
361 return;
|
meillo@756
|
362 nmaster--;
|
meillo@754
|
363 if(sel)
|
meillo@754
|
364 arrange();
|
meillo@754
|
365 }
|
meillo@754
|
366
|
meillo@754
|
367 Bool
|
meillo@754
|
368 isvisible(Client *c) {
|
meillo@766
|
369 return (c->tag == seltag);
|
meillo@754
|
370 }
|
meillo@754
|
371
|
meillo@754
|
372 void
|
meillo@754
|
373 restack(void) {
|
meillo@754
|
374 Client *c;
|
meillo@754
|
375 XEvent ev;
|
meillo@754
|
376
|
meillo@754
|
377 drawstatus();
|
meillo@754
|
378 if(!sel)
|
meillo@754
|
379 return;
|
meillo@761
|
380 if(sel->isfloat)
|
meillo@754
|
381 XRaiseWindow(dpy, sel->win);
|
meillo@754
|
382
|
meillo@768
|
383 if(!sel->isfloat)
|
meillo@768
|
384 XLowerWindow(dpy, sel->win);
|
meillo@768
|
385 for(c = nexttiled(clients); c; c = nexttiled(c->next)) {
|
meillo@768
|
386 if(c == sel)
|
meillo@768
|
387 continue;
|
meillo@768
|
388 XLowerWindow(dpy, c->win);
|
meillo@768
|
389 }
|
meillo@754
|
390
|
meillo@754
|
391 XSync(dpy, False);
|
meillo@754
|
392 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
|
meillo@754
|
393 }
|
meillo@754
|
394
|
meillo@754
|
395 void
|
meillo@756
|
396 togglefloat() {
|
meillo@761
|
397 if (!sel)
|
meillo@754
|
398 return;
|
meillo@754
|
399 sel->isfloat = !sel->isfloat;
|
meillo@754
|
400 arrange();
|
meillo@754
|
401 }
|
meillo@754
|
402
|
meillo@754
|
403 void
|
meillo@756
|
404 togglemode() {
|
meillo@754
|
405 /* only toggle between tile and max - float is just available through togglefloat */
|
meillo@754
|
406 arrange = (arrange == dotile) ? domax : dotile;
|
meillo@754
|
407 if(sel)
|
meillo@754
|
408 arrange();
|
meillo@776
|
409 zoom();
|
meillo@754
|
410 }
|
meillo@754
|
411
|
meillo@754
|
412 void
|
meillo@755
|
413 toggleview() {
|
meillo@766
|
414 seltag = !seltag;
|
meillo@754
|
415 arrange();
|
meillo@754
|
416 }
|
meillo@754
|
417
|
meillo@754
|
418 void
|
meillo@756
|
419 zoom() {
|
meillo@754
|
420 unsigned int n;
|
meillo@754
|
421 Client *c;
|
meillo@754
|
422
|
meillo@754
|
423 if(!sel)
|
meillo@754
|
424 return;
|
meillo@761
|
425 if(sel->isfloat) {
|
meillo@754
|
426 togglemax(sel);
|
meillo@754
|
427 return;
|
meillo@754
|
428 }
|
meillo@754
|
429 for(n = 0, c = nexttiled(clients); c; c = nexttiled(c->next))
|
meillo@754
|
430 n++;
|
meillo@754
|
431
|
meillo@754
|
432 if((c = sel) == nexttiled(clients))
|
meillo@754
|
433 if(!(c = nexttiled(c->next)))
|
meillo@754
|
434 return;
|
meillo@754
|
435 detach(c);
|
meillo@754
|
436 if(clients)
|
meillo@754
|
437 clients->prev = c;
|
meillo@754
|
438 c->next = clients;
|
meillo@754
|
439 clients = c;
|
meillo@754
|
440 focus(c);
|
meillo@754
|
441 arrange();
|
meillo@754
|
442 }
|
meillo@754
|
443
|
meillo@754
|
444 /* from util.c */
|
meillo@754
|
445
|
meillo@754
|
446 void *
|
meillo@754
|
447 emallocz(unsigned int size) {
|
meillo@754
|
448 void *res = calloc(1, size);
|
meillo@754
|
449
|
meillo@754
|
450 if(!res)
|
meillo@754
|
451 eprint("fatal: could not malloc() %u bytes\n", size);
|
meillo@754
|
452 return res;
|
meillo@754
|
453 }
|
meillo@754
|
454
|
meillo@754
|
455 void
|
meillo@754
|
456 eprint(const char *errstr, ...) {
|
meillo@754
|
457 va_list ap;
|
meillo@754
|
458
|
meillo@754
|
459 va_start(ap, errstr);
|
meillo@754
|
460 vfprintf(stderr, errstr, ap);
|
meillo@754
|
461 va_end(ap);
|
meillo@754
|
462 exit(EXIT_FAILURE);
|
meillo@754
|
463 }
|
meillo@754
|
464
|
meillo@754
|
465 void
|
meillo@758
|
466 spawn(const char* cmd) {
|
meillo@754
|
467 static char *shell = NULL;
|
meillo@754
|
468
|
meillo@758
|
469 if(!cmd)
|
meillo@754
|
470 return;
|
meillo@769
|
471 if(!(shell = getenv("SHELL")))
|
meillo@769
|
472 shell = "/bin/sh";
|
meillo@754
|
473 /* The double-fork construct avoids zombie processes and keeps the code
|
meillo@754
|
474 * clean from stupid signal handlers. */
|
meillo@754
|
475 if(fork() == 0) {
|
meillo@754
|
476 if(fork() == 0) {
|
meillo@754
|
477 if(dpy)
|
meillo@754
|
478 close(ConnectionNumber(dpy));
|
meillo@754
|
479 setsid();
|
meillo@758
|
480 execl(shell, shell, "-c", cmd, (char *)NULL);
|
meillo@772
|
481 fprintf(stderr, "aewl: execl '%s -c %s'", shell, cmd);
|
meillo@754
|
482 perror(" failed");
|
meillo@754
|
483 }
|
meillo@754
|
484 exit(0);
|
meillo@754
|
485 }
|
meillo@754
|
486 wait(0);
|
meillo@754
|
487 }
|
meillo@754
|
488
|
meillo@754
|
489 /* from tag.c */
|
arg@333
|
490
|
meillo@754
|
491 Client *
|
meillo@754
|
492 getnext(Client *c) {
|
meillo@768
|
493 while(c && !isvisible(c)) {
|
meillo@768
|
494 c = c->next;
|
meillo@768
|
495 }
|
meillo@754
|
496 return c;
|
meillo@754
|
497 }
|
meillo@754
|
498
|
meillo@754
|
499 void
|
meillo@767
|
500 settag(Client *c, Client *trans) {
|
meillo@754
|
501 unsigned int i;
|
meillo@767
|
502 XClassHint ch = { 0 };
|
meillo@754
|
503
|
meillo@767
|
504 if(trans) {
|
meillo@767
|
505 c->tag = trans->tag;
|
meillo@754
|
506 return;
|
meillo@767
|
507 }
|
meillo@767
|
508 c->tag = seltag; /* default */
|
meillo@767
|
509 XGetClassHint(dpy, c->win, &ch);
|
meillo@754
|
510 len = sizeof rule / sizeof rule[0];
|
meillo@754
|
511 for(i = 0; i < len; i++) {
|
meillo@767
|
512 if((rule[i].title && strstr(c->name, rule[i].title))
|
meillo@767
|
513 || (ch.res_class && rule[i].class && strstr(ch.res_class, rule[i].class))
|
meillo@767
|
514 || (ch.res_name && rule[i].instance && strstr(ch.res_name, rule[i].instance))) {
|
meillo@767
|
515 c->isfloat = rule[i].isfloat;
|
meillo@767
|
516 if (rule[i].tag < 0) {
|
meillo@767
|
517 c->tag = seltag;
|
meillo@767
|
518 } else if (rule[i].tag) {
|
meillo@767
|
519 c->tag = True;
|
meillo@767
|
520 } else {
|
meillo@767
|
521 c->tag = False;
|
meillo@767
|
522 }
|
meillo@767
|
523 break;
|
meillo@754
|
524 }
|
meillo@754
|
525 }
|
meillo@767
|
526 if(ch.res_class)
|
meillo@767
|
527 XFree(ch.res_class);
|
meillo@767
|
528 if(ch.res_name)
|
meillo@767
|
529 XFree(ch.res_name);
|
meillo@754
|
530 }
|
meillo@754
|
531
|
meillo@754
|
532 void
|
meillo@766
|
533 toggletag() {
|
meillo@754
|
534 if(!sel)
|
meillo@754
|
535 return;
|
meillo@766
|
536 sel->tag = !sel->tag;
|
meillo@755
|
537 toggleview();
|
meillo@754
|
538 }
|
meillo@754
|
539
|
meillo@773
|
540 /* from event.c */
|
meillo@754
|
541
|
meillo@773
|
542 void
|
meillo@754
|
543 movemouse(Client *c) {
|
meillo@754
|
544 int x1, y1, ocx, ocy, di;
|
meillo@754
|
545 unsigned int dui;
|
meillo@754
|
546 Window dummy;
|
meillo@754
|
547 XEvent ev;
|
meillo@754
|
548
|
meillo@754
|
549 ocx = c->x;
|
meillo@754
|
550 ocy = c->y;
|
meillo@754
|
551 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
|
meillo@754
|
552 None, cursor[CurMove], CurrentTime) != GrabSuccess)
|
meillo@754
|
553 return;
|
meillo@754
|
554 c->ismax = False;
|
meillo@754
|
555 XQueryPointer(dpy, root, &dummy, &dummy, &x1, &y1, &di, &di, &dui);
|
meillo@754
|
556 for(;;) {
|
meillo@754
|
557 XMaskEvent(dpy, MOUSEMASK | ExposureMask | SubstructureRedirectMask, &ev);
|
meillo@754
|
558 switch (ev.type) {
|
meillo@754
|
559 case ButtonRelease:
|
meillo@754
|
560 resize(c, True);
|
meillo@754
|
561 XUngrabPointer(dpy, CurrentTime);
|
meillo@754
|
562 return;
|
meillo@754
|
563 case ConfigureRequest:
|
meillo@754
|
564 case Expose:
|
meillo@754
|
565 case MapRequest:
|
meillo@754
|
566 handler[ev.type](&ev);
|
meillo@754
|
567 break;
|
meillo@754
|
568 case MotionNotify:
|
meillo@754
|
569 XSync(dpy, False);
|
meillo@754
|
570 c->x = ocx + (ev.xmotion.x - x1);
|
meillo@754
|
571 c->y = ocy + (ev.xmotion.y - y1);
|
meillo@754
|
572 if(abs(wax + c->x) < SNAP)
|
meillo@754
|
573 c->x = wax;
|
meillo@754
|
574 else if(abs((wax + waw) - (c->x + c->w + 2 * c->border)) < SNAP)
|
meillo@754
|
575 c->x = wax + waw - c->w - 2 * c->border;
|
meillo@754
|
576 if(abs(way - c->y) < SNAP)
|
meillo@754
|
577 c->y = way;
|
meillo@754
|
578 else if(abs((way + wah) - (c->y + c->h + 2 * c->border)) < SNAP)
|
meillo@754
|
579 c->y = way + wah - c->h - 2 * c->border;
|
meillo@754
|
580 resize(c, False);
|
meillo@754
|
581 break;
|
meillo@754
|
582 }
|
meillo@754
|
583 }
|
meillo@754
|
584 }
|
meillo@754
|
585
|
meillo@773
|
586 void
|
meillo@754
|
587 resizemouse(Client *c) {
|
meillo@754
|
588 int ocx, ocy;
|
meillo@754
|
589 int nw, nh;
|
meillo@754
|
590 XEvent ev;
|
meillo@754
|
591
|
meillo@754
|
592 ocx = c->x;
|
meillo@754
|
593 ocy = c->y;
|
meillo@754
|
594 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
|
meillo@754
|
595 None, cursor[CurResize], CurrentTime) != GrabSuccess)
|
meillo@754
|
596 return;
|
meillo@754
|
597 c->ismax = False;
|
meillo@754
|
598 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->border - 1, c->h + c->border - 1);
|
meillo@754
|
599 for(;;) {
|
meillo@754
|
600 XMaskEvent(dpy, MOUSEMASK | ExposureMask | SubstructureRedirectMask , &ev);
|
meillo@754
|
601 switch(ev.type) {
|
meillo@754
|
602 case ButtonRelease:
|
meillo@754
|
603 resize(c, True);
|
meillo@754
|
604 XUngrabPointer(dpy, CurrentTime);
|
meillo@754
|
605 return;
|
meillo@754
|
606 case ConfigureRequest:
|
meillo@754
|
607 case Expose:
|
meillo@754
|
608 case MapRequest:
|
meillo@754
|
609 handler[ev.type](&ev);
|
meillo@754
|
610 break;
|
meillo@754
|
611 case MotionNotify:
|
meillo@754
|
612 XSync(dpy, False);
|
meillo@754
|
613 nw = ev.xmotion.x - ocx - 2 * c->border + 1;
|
meillo@754
|
614 c->w = nw > 0 ? nw : 1;
|
meillo@754
|
615 nh = ev.xmotion.y - ocy - 2 * c->border + 1;
|
meillo@754
|
616 c->h = nh > 0 ? nh : 1;
|
meillo@754
|
617 resize(c, True);
|
meillo@754
|
618 break;
|
meillo@754
|
619 }
|
meillo@754
|
620 }
|
meillo@754
|
621 }
|
meillo@754
|
622
|
meillo@773
|
623 void
|
meillo@754
|
624 buttonpress(XEvent *e) {
|
meillo@754
|
625 Client *c;
|
meillo@754
|
626 XButtonPressedEvent *ev = &e->xbutton;
|
meillo@754
|
627
|
meillo@754
|
628 if(barwin == ev->window) {
|
meillo@758
|
629 return;
|
meillo@763
|
630 }
|
meillo@763
|
631 if((c = getclient(ev->window))) {
|
meillo@754
|
632 focus(c);
|
meillo@754
|
633 if(CLEANMASK(ev->state) != MODKEY)
|
meillo@754
|
634 return;
|
meillo@761
|
635 if(ev->button == Button1 && c->isfloat) {
|
meillo@754
|
636 restack();
|
meillo@754
|
637 movemouse(c);
|
meillo@761
|
638 } else if(ev->button == Button3 && c->isfloat && !c->isfixed) {
|
meillo@754
|
639 restack();
|
meillo@754
|
640 resizemouse(c);
|
meillo@754
|
641 }
|
meillo@754
|
642 }
|
meillo@754
|
643 }
|
meillo@754
|
644
|
meillo@773
|
645 void
|
meillo@754
|
646 configurerequest(XEvent *e) {
|
meillo@754
|
647 unsigned long newmask;
|
meillo@754
|
648 Client *c;
|
meillo@754
|
649 XConfigureRequestEvent *ev = &e->xconfigurerequest;
|
meillo@754
|
650 XWindowChanges wc;
|
meillo@754
|
651
|
meillo@754
|
652 if((c = getclient(ev->window))) {
|
meillo@754
|
653 c->ismax = False;
|
meillo@754
|
654 if(ev->value_mask & CWX)
|
meillo@754
|
655 c->x = ev->x;
|
meillo@754
|
656 if(ev->value_mask & CWY)
|
meillo@754
|
657 c->y = ev->y;
|
meillo@754
|
658 if(ev->value_mask & CWWidth)
|
meillo@754
|
659 c->w = ev->width;
|
meillo@754
|
660 if(ev->value_mask & CWHeight)
|
meillo@754
|
661 c->h = ev->height;
|
meillo@754
|
662 if(ev->value_mask & CWBorderWidth)
|
meillo@754
|
663 c->border = ev->border_width;
|
meillo@754
|
664 wc.x = c->x;
|
meillo@754
|
665 wc.y = c->y;
|
meillo@754
|
666 wc.width = c->w;
|
meillo@754
|
667 wc.height = c->h;
|
meillo@754
|
668 newmask = ev->value_mask & (~(CWSibling | CWStackMode | CWBorderWidth));
|
meillo@754
|
669 if(newmask)
|
meillo@754
|
670 XConfigureWindow(dpy, c->win, newmask, &wc);
|
meillo@754
|
671 else
|
meillo@754
|
672 configure(c);
|
meillo@754
|
673 XSync(dpy, False);
|
meillo@754
|
674 if(c->isfloat) {
|
meillo@754
|
675 resize(c, False);
|
meillo@754
|
676 if(!isvisible(c))
|
meillo@754
|
677 XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
|
meillo@754
|
678 }
|
meillo@754
|
679 else
|
meillo@754
|
680 arrange();
|
meillo@758
|
681 } else {
|
meillo@754
|
682 wc.x = ev->x;
|
meillo@754
|
683 wc.y = ev->y;
|
meillo@754
|
684 wc.width = ev->width;
|
meillo@754
|
685 wc.height = ev->height;
|
meillo@754
|
686 wc.border_width = ev->border_width;
|
meillo@754
|
687 wc.sibling = ev->above;
|
meillo@754
|
688 wc.stack_mode = ev->detail;
|
meillo@754
|
689 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
|
meillo@754
|
690 XSync(dpy, False);
|
meillo@754
|
691 }
|
meillo@754
|
692 }
|
meillo@754
|
693
|
meillo@773
|
694 void
|
meillo@754
|
695 destroynotify(XEvent *e) {
|
meillo@754
|
696 Client *c;
|
meillo@754
|
697 XDestroyWindowEvent *ev = &e->xdestroywindow;
|
meillo@754
|
698
|
meillo@754
|
699 if((c = getclient(ev->window)))
|
meillo@754
|
700 unmanage(c);
|
meillo@754
|
701 }
|
meillo@754
|
702
|
meillo@773
|
703 void
|
meillo@754
|
704 enternotify(XEvent *e) {
|
meillo@754
|
705 Client *c;
|
meillo@754
|
706 XCrossingEvent *ev = &e->xcrossing;
|
meillo@754
|
707
|
meillo@754
|
708 if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
|
meillo@754
|
709 return;
|
meillo@754
|
710 if((c = getclient(ev->window)) && isvisible(c))
|
meillo@754
|
711 focus(c);
|
meillo@754
|
712 else if(ev->window == root) {
|
meillo@754
|
713 selscreen = True;
|
meillo@754
|
714 for(c = stack; c && !isvisible(c); c = c->snext);
|
meillo@754
|
715 focus(c);
|
meillo@754
|
716 }
|
meillo@754
|
717 }
|
meillo@754
|
718
|
meillo@773
|
719 void
|
meillo@754
|
720 expose(XEvent *e) {
|
meillo@754
|
721 XExposeEvent *ev = &e->xexpose;
|
meillo@754
|
722
|
meillo@754
|
723 if(ev->count == 0) {
|
meillo@754
|
724 if(barwin == ev->window)
|
meillo@754
|
725 drawstatus();
|
meillo@754
|
726 }
|
meillo@754
|
727 }
|
meillo@754
|
728
|
meillo@773
|
729 void
|
meillo@754
|
730 keypress(XEvent *e) {
|
meillo@754
|
731 static unsigned int len = sizeof key / sizeof key[0];
|
meillo@754
|
732 unsigned int i;
|
meillo@754
|
733 KeySym keysym;
|
meillo@754
|
734 XKeyEvent *ev = &e->xkey;
|
meillo@754
|
735
|
meillo@754
|
736 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
|
meillo@754
|
737 for(i = 0; i < len; i++) {
|
meillo@756
|
738 if(keysym == key[i].keysym && CLEANMASK(key[i].mod) == CLEANMASK(ev->state)) {
|
meillo@754
|
739 if(key[i].func)
|
meillo@758
|
740 key[i].func(key[i].cmd);
|
meillo@754
|
741 }
|
meillo@754
|
742 }
|
meillo@754
|
743 }
|
meillo@754
|
744
|
meillo@773
|
745 void
|
meillo@754
|
746 leavenotify(XEvent *e) {
|
meillo@754
|
747 XCrossingEvent *ev = &e->xcrossing;
|
meillo@754
|
748
|
meillo@754
|
749 if((ev->window == root) && !ev->same_screen) {
|
meillo@754
|
750 selscreen = False;
|
meillo@754
|
751 focus(NULL);
|
meillo@754
|
752 }
|
meillo@754
|
753 }
|
meillo@754
|
754
|
meillo@773
|
755 void
|
meillo@754
|
756 mappingnotify(XEvent *e) {
|
meillo@754
|
757 XMappingEvent *ev = &e->xmapping;
|
meillo@754
|
758
|
meillo@754
|
759 XRefreshKeyboardMapping(ev);
|
meillo@754
|
760 if(ev->request == MappingKeyboard)
|
meillo@754
|
761 grabkeys();
|
meillo@754
|
762 }
|
meillo@754
|
763
|
meillo@773
|
764 void
|
meillo@754
|
765 maprequest(XEvent *e) {
|
meillo@754
|
766 static XWindowAttributes wa;
|
meillo@754
|
767 XMapRequestEvent *ev = &e->xmaprequest;
|
meillo@754
|
768
|
meillo@754
|
769 if(!XGetWindowAttributes(dpy, ev->window, &wa))
|
meillo@754
|
770 return;
|
meillo@754
|
771 if(wa.override_redirect) {
|
meillo@754
|
772 XSelectInput(dpy, ev->window,
|
meillo@754
|
773 (StructureNotifyMask | PropertyChangeMask));
|
meillo@754
|
774 return;
|
meillo@754
|
775 }
|
meillo@754
|
776 if(!getclient(ev->window))
|
meillo@754
|
777 manage(ev->window, &wa);
|
meillo@754
|
778 }
|
meillo@754
|
779
|
meillo@773
|
780 void
|
meillo@754
|
781 propertynotify(XEvent *e) {
|
meillo@754
|
782 Client *c;
|
meillo@754
|
783 Window trans;
|
meillo@754
|
784 XPropertyEvent *ev = &e->xproperty;
|
meillo@754
|
785
|
meillo@754
|
786 if(ev->state == PropertyDelete)
|
meillo@754
|
787 return; /* ignore */
|
meillo@754
|
788 if((c = getclient(ev->window))) {
|
meillo@754
|
789 switch (ev->atom) {
|
meillo@754
|
790 default: break;
|
meillo@754
|
791 case XA_WM_TRANSIENT_FOR:
|
meillo@754
|
792 XGetTransientForHint(dpy, c->win, &trans);
|
meillo@754
|
793 if(!c->isfloat && (c->isfloat = (trans != 0)))
|
meillo@754
|
794 arrange();
|
meillo@754
|
795 break;
|
meillo@754
|
796 case XA_WM_NORMAL_HINTS:
|
meillo@754
|
797 updatesizehints(c);
|
meillo@754
|
798 break;
|
meillo@754
|
799 }
|
meillo@754
|
800 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
|
meillo@754
|
801 updatetitle(c);
|
meillo@754
|
802 if(c == sel)
|
meillo@754
|
803 drawstatus();
|
meillo@754
|
804 }
|
meillo@754
|
805 }
|
meillo@754
|
806 }
|
meillo@754
|
807
|
meillo@773
|
808 void
|
meillo@754
|
809 unmapnotify(XEvent *e) {
|
meillo@754
|
810 Client *c;
|
meillo@754
|
811 XUnmapEvent *ev = &e->xunmap;
|
meillo@754
|
812
|
meillo@754
|
813 if((c = getclient(ev->window)))
|
meillo@754
|
814 unmanage(c);
|
meillo@754
|
815 }
|
meillo@754
|
816
|
meillo@754
|
817
|
meillo@754
|
818
|
meillo@754
|
819 void (*handler[LASTEvent]) (XEvent *) = {
|
meillo@754
|
820 [ButtonPress] = buttonpress,
|
meillo@754
|
821 [ConfigureRequest] = configurerequest,
|
meillo@754
|
822 [DestroyNotify] = destroynotify,
|
meillo@754
|
823 [EnterNotify] = enternotify,
|
meillo@754
|
824 [LeaveNotify] = leavenotify,
|
meillo@754
|
825 [Expose] = expose,
|
meillo@754
|
826 [KeyPress] = keypress,
|
meillo@754
|
827 [MappingNotify] = mappingnotify,
|
meillo@754
|
828 [MapRequest] = maprequest,
|
meillo@754
|
829 [PropertyNotify] = propertynotify,
|
meillo@754
|
830 [UnmapNotify] = unmapnotify
|
meillo@754
|
831 };
|
meillo@754
|
832
|
meillo@754
|
833 void
|
meillo@754
|
834 grabkeys(void) {
|
meillo@754
|
835 static unsigned int len = sizeof key / sizeof key[0];
|
meillo@754
|
836 unsigned int i;
|
meillo@754
|
837 KeyCode code;
|
meillo@754
|
838
|
meillo@754
|
839 XUngrabKey(dpy, AnyKey, AnyModifier, root);
|
meillo@754
|
840 for(i = 0; i < len; i++) {
|
meillo@754
|
841 code = XKeysymToKeycode(dpy, key[i].keysym);
|
meillo@754
|
842 XGrabKey(dpy, code, key[i].mod, root, True,
|
meillo@754
|
843 GrabModeAsync, GrabModeAsync);
|
meillo@754
|
844 XGrabKey(dpy, code, key[i].mod | LockMask, root, True,
|
meillo@754
|
845 GrabModeAsync, GrabModeAsync);
|
meillo@754
|
846 XGrabKey(dpy, code, key[i].mod | numlockmask, root, True,
|
meillo@754
|
847 GrabModeAsync, GrabModeAsync);
|
meillo@754
|
848 XGrabKey(dpy, code, key[i].mod | numlockmask | LockMask, root, True,
|
meillo@754
|
849 GrabModeAsync, GrabModeAsync);
|
meillo@754
|
850 }
|
meillo@754
|
851 }
|
meillo@754
|
852
|
meillo@754
|
853 void
|
meillo@754
|
854 procevent(void) {
|
meillo@754
|
855 XEvent ev;
|
meillo@754
|
856
|
meillo@754
|
857 while(XPending(dpy)) {
|
meillo@754
|
858 XNextEvent(dpy, &ev);
|
meillo@754
|
859 if(handler[ev.type])
|
meillo@754
|
860 (handler[ev.type])(&ev); /* call handler */
|
meillo@754
|
861 }
|
meillo@754
|
862 }
|
meillo@754
|
863
|
meillo@773
|
864 /* from draw.c */
|
meillo@754
|
865
|
meillo@773
|
866 unsigned int
|
meillo@754
|
867 textnw(const char *text, unsigned int len) {
|
meillo@754
|
868 XRectangle r;
|
meillo@754
|
869
|
meillo@754
|
870 if(dc.font.set) {
|
meillo@754
|
871 XmbTextExtents(dc.font.set, text, len, NULL, &r);
|
meillo@754
|
872 return r.width;
|
meillo@754
|
873 }
|
meillo@754
|
874 return XTextWidth(dc.font.xfont, text, len);
|
meillo@754
|
875 }
|
meillo@754
|
876
|
meillo@773
|
877 void
|
meillo@754
|
878 drawtext(const char *text, unsigned long col[ColLast]) {
|
meillo@754
|
879 int x, y, w, h;
|
meillo@754
|
880 static char buf[256];
|
meillo@754
|
881 unsigned int len, olen;
|
meillo@754
|
882 XGCValues gcv;
|
meillo@754
|
883 XRectangle r = { dc.x, dc.y, dc.w, dc.h };
|
meillo@754
|
884
|
meillo@754
|
885 XSetForeground(dpy, dc.gc, col[ColBG]);
|
meillo@754
|
886 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
|
meillo@754
|
887 if(!text)
|
meillo@754
|
888 return;
|
meillo@754
|
889 w = 0;
|
meillo@754
|
890 olen = len = strlen(text);
|
meillo@754
|
891 if(len >= sizeof buf)
|
meillo@754
|
892 len = sizeof buf - 1;
|
meillo@754
|
893 memcpy(buf, text, len);
|
meillo@754
|
894 buf[len] = 0;
|
meillo@754
|
895 h = dc.font.ascent + dc.font.descent;
|
meillo@754
|
896 y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
|
meillo@754
|
897 x = dc.x + (h / 2);
|
meillo@754
|
898 /* shorten text if necessary */
|
meillo@754
|
899 while(len && (w = textnw(buf, len)) > dc.w - h)
|
meillo@754
|
900 buf[--len] = 0;
|
meillo@754
|
901 if(len < olen) {
|
meillo@754
|
902 if(len > 1)
|
meillo@754
|
903 buf[len - 1] = '.';
|
meillo@754
|
904 if(len > 2)
|
meillo@754
|
905 buf[len - 2] = '.';
|
meillo@754
|
906 if(len > 3)
|
meillo@754
|
907 buf[len - 3] = '.';
|
meillo@754
|
908 }
|
meillo@754
|
909 if(w > dc.w)
|
meillo@754
|
910 return; /* too long */
|
meillo@754
|
911 gcv.foreground = col[ColFG];
|
meillo@754
|
912 if(dc.font.set) {
|
meillo@754
|
913 XChangeGC(dpy, dc.gc, GCForeground, &gcv);
|
meillo@754
|
914 XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
|
meillo@754
|
915 } else {
|
meillo@754
|
916 gcv.font = dc.font.xfont->fid;
|
meillo@754
|
917 XChangeGC(dpy, dc.gc, GCForeground | GCFont, &gcv);
|
meillo@754
|
918 XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
|
meillo@754
|
919 }
|
meillo@754
|
920 }
|
meillo@754
|
921
|
meillo@754
|
922 void
|
meillo@754
|
923 drawstatus(void) {
|
meillo@756
|
924 int x;
|
meillo@771
|
925 unsigned long black[ColLast];
|
meillo@771
|
926 black[ColBG] = getcolor("#000000");
|
meillo@771
|
927 black[ColFG] = getcolor("#ffffff");
|
meillo@754
|
928
|
meillo@771
|
929 /* views */
|
meillo@754
|
930 dc.x = dc.y = 0;
|
meillo@766
|
931 dc.w = textw(NAMETAGGED);
|
meillo@766
|
932 drawtext(NAMETAGGED, ( seltag ? dc.sel : dc.norm ));
|
meillo@771
|
933
|
meillo@771
|
934 dc.x += dc.w;
|
meillo@771
|
935 dc.w = BORDERPX;
|
meillo@771
|
936 drawtext(NULL, black);
|
meillo@771
|
937
|
meillo@771
|
938 dc.x += dc.w;
|
meillo@766
|
939 dc.w = textw(NAMEUNTAGGED);
|
meillo@766
|
940 drawtext(NAMEUNTAGGED, ( seltag ? dc.norm : dc.sel ));
|
meillo@771
|
941
|
meillo@771
|
942 dc.x += dc.w;
|
meillo@771
|
943 dc.w = BORDERPX;
|
meillo@771
|
944 drawtext(NULL, black);
|
meillo@771
|
945
|
meillo@771
|
946 /* status text */
|
meillo@754
|
947 x = dc.x + dc.w;
|
meillo@754
|
948 dc.w = textw(stext);
|
meillo@754
|
949 dc.x = sw - dc.w;
|
meillo@754
|
950 if(dc.x < x) {
|
meillo@754
|
951 dc.x = x;
|
meillo@754
|
952 dc.w = sw - x;
|
meillo@754
|
953 }
|
meillo@754
|
954 drawtext(stext, dc.norm);
|
meillo@771
|
955
|
meillo@771
|
956 /* client title */
|
meillo@754
|
957 if((dc.w = dc.x - x) > bh) {
|
meillo@754
|
958 dc.x = x;
|
meillo@754
|
959 drawtext(sel ? sel->name : NULL, dc.norm);
|
meillo@771
|
960
|
meillo@771
|
961 dc.x += dc.w;
|
meillo@771
|
962 dc.w = BORDERPX;
|
meillo@771
|
963 drawtext(NULL, black);
|
meillo@754
|
964 }
|
meillo@771
|
965
|
meillo@754
|
966 XCopyArea(dpy, dc.drawable, barwin, dc.gc, 0, 0, sw, bh, 0, 0);
|
meillo@754
|
967 XSync(dpy, False);
|
meillo@754
|
968 }
|
meillo@754
|
969
|
meillo@754
|
970 unsigned long
|
meillo@754
|
971 getcolor(const char *colstr) {
|
meillo@754
|
972 Colormap cmap = DefaultColormap(dpy, screen);
|
meillo@754
|
973 XColor color;
|
meillo@754
|
974
|
meillo@754
|
975 if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
|
meillo@754
|
976 eprint("error, cannot allocate color '%s'\n", colstr);
|
meillo@754
|
977 return color.pixel;
|
meillo@754
|
978 }
|
meillo@754
|
979
|
meillo@754
|
980 void
|
meillo@754
|
981 setfont(const char *fontstr) {
|
meillo@754
|
982 char *def, **missing;
|
meillo@754
|
983 int i, n;
|
meillo@754
|
984
|
meillo@754
|
985 missing = NULL;
|
meillo@754
|
986 if(dc.font.set)
|
meillo@754
|
987 XFreeFontSet(dpy, dc.font.set);
|
meillo@754
|
988 dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
|
meillo@754
|
989 if(missing) {
|
meillo@754
|
990 while(n--)
|
meillo@754
|
991 fprintf(stderr, "missing fontset: %s\n", missing[n]);
|
meillo@754
|
992 XFreeStringList(missing);
|
meillo@754
|
993 }
|
meillo@754
|
994 if(dc.font.set) {
|
meillo@754
|
995 XFontSetExtents *font_extents;
|
meillo@754
|
996 XFontStruct **xfonts;
|
meillo@754
|
997 char **font_names;
|
meillo@754
|
998 dc.font.ascent = dc.font.descent = 0;
|
meillo@754
|
999 font_extents = XExtentsOfFontSet(dc.font.set);
|
meillo@754
|
1000 n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
|
meillo@754
|
1001 for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
|
meillo@754
|
1002 if(dc.font.ascent < (*xfonts)->ascent)
|
meillo@754
|
1003 dc.font.ascent = (*xfonts)->ascent;
|
meillo@754
|
1004 if(dc.font.descent < (*xfonts)->descent)
|
meillo@754
|
1005 dc.font.descent = (*xfonts)->descent;
|
meillo@754
|
1006 xfonts++;
|
meillo@754
|
1007 }
|
meillo@754
|
1008 } else {
|
meillo@754
|
1009 if(dc.font.xfont)
|
meillo@754
|
1010 XFreeFont(dpy, dc.font.xfont);
|
meillo@754
|
1011 dc.font.xfont = NULL;
|
meillo@754
|
1012 if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr)))
|
meillo@754
|
1013 eprint("error, cannot load font: '%s'\n", fontstr);
|
meillo@754
|
1014 dc.font.ascent = dc.font.xfont->ascent;
|
meillo@754
|
1015 dc.font.descent = dc.font.xfont->descent;
|
meillo@754
|
1016 }
|
meillo@754
|
1017 dc.font.height = dc.font.ascent + dc.font.descent;
|
meillo@754
|
1018 }
|
meillo@754
|
1019
|
meillo@754
|
1020 unsigned int
|
meillo@754
|
1021 textw(const char *text) {
|
meillo@754
|
1022 return textnw(text, strlen(text)) + dc.font.height;
|
meillo@754
|
1023 }
|
meillo@754
|
1024
|
meillo@773
|
1025 /* from client.c */
|
meillo@754
|
1026
|
meillo@773
|
1027 void
|
meillo@754
|
1028 detachstack(Client *c) {
|
meillo@754
|
1029 Client **tc;
|
meillo@754
|
1030 for(tc=&stack; *tc && *tc != c; tc=&(*tc)->snext);
|
meillo@754
|
1031 *tc = c->snext;
|
meillo@754
|
1032 }
|
meillo@754
|
1033
|
meillo@773
|
1034 void
|
meillo@754
|
1035 grabbuttons(Client *c, Bool focused) {
|
meillo@754
|
1036 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
|
meillo@754
|
1037
|
meillo@754
|
1038 if(focused) {
|
meillo@754
|
1039 XGrabButton(dpy, Button1, MODKEY, c->win, False, BUTTONMASK,
|
meillo@754
|
1040 GrabModeAsync, GrabModeSync, None, None);
|
meillo@754
|
1041 XGrabButton(dpy, Button1, MODKEY | LockMask, c->win, False, BUTTONMASK,
|
meillo@754
|
1042 GrabModeAsync, GrabModeSync, None, None);
|
meillo@754
|
1043 XGrabButton(dpy, Button1, MODKEY | numlockmask, c->win, False, BUTTONMASK,
|
meillo@754
|
1044 GrabModeAsync, GrabModeSync, None, None);
|
meillo@754
|
1045 XGrabButton(dpy, Button1, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
|
meillo@754
|
1046 GrabModeAsync, GrabModeSync, None, None);
|
meillo@754
|
1047
|
meillo@754
|
1048 XGrabButton(dpy, Button2, MODKEY, c->win, False, BUTTONMASK,
|
meillo@754
|
1049 GrabModeAsync, GrabModeSync, None, None);
|
meillo@754
|
1050 XGrabButton(dpy, Button2, MODKEY | LockMask, c->win, False, BUTTONMASK,
|
meillo@754
|
1051 GrabModeAsync, GrabModeSync, None, None);
|
meillo@754
|
1052 XGrabButton(dpy, Button2, MODKEY | numlockmask, c->win, False, BUTTONMASK,
|
meillo@754
|
1053 GrabModeAsync, GrabModeSync, None, None);
|
meillo@754
|
1054 XGrabButton(dpy, Button2, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
|
meillo@754
|
1055 GrabModeAsync, GrabModeSync, None, None);
|
meillo@754
|
1056
|
meillo@754
|
1057 XGrabButton(dpy, Button3, MODKEY, c->win, False, BUTTONMASK,
|
meillo@754
|
1058 GrabModeAsync, GrabModeSync, None, None);
|
meillo@754
|
1059 XGrabButton(dpy, Button3, MODKEY | LockMask, c->win, False, BUTTONMASK,
|
meillo@754
|
1060 GrabModeAsync, GrabModeSync, None, None);
|
meillo@754
|
1061 XGrabButton(dpy, Button3, MODKEY | numlockmask, c->win, False, BUTTONMASK,
|
meillo@754
|
1062 GrabModeAsync, GrabModeSync, None, None);
|
meillo@754
|
1063 XGrabButton(dpy, Button3, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
|
meillo@754
|
1064 GrabModeAsync, GrabModeSync, None, None);
|
meillo@754
|
1065 } else {
|
meillo@754
|
1066 XGrabButton(dpy, AnyButton, AnyModifier, c->win, False, BUTTONMASK,
|
meillo@754
|
1067 GrabModeAsync, GrabModeSync, None, None);
|
meillo@754
|
1068 }
|
meillo@754
|
1069 }
|
meillo@754
|
1070
|
meillo@773
|
1071 void
|
meillo@754
|
1072 setclientstate(Client *c, long state) {
|
meillo@754
|
1073 long data[] = {state, None};
|
meillo@754
|
1074 XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
|
meillo@754
|
1075 PropModeReplace, (unsigned char *)data, 2);
|
meillo@754
|
1076 }
|
meillo@754
|
1077
|
meillo@773
|
1078 int
|
meillo@754
|
1079 xerrordummy(Display *dsply, XErrorEvent *ee) {
|
meillo@754
|
1080 return 0;
|
meillo@754
|
1081 }
|
meillo@754
|
1082
|
meillo@754
|
1083 void
|
meillo@754
|
1084 configure(Client *c) {
|
meillo@754
|
1085 XEvent synev;
|
meillo@754
|
1086
|
meillo@754
|
1087 synev.type = ConfigureNotify;
|
meillo@754
|
1088 synev.xconfigure.display = dpy;
|
meillo@754
|
1089 synev.xconfigure.event = c->win;
|
meillo@754
|
1090 synev.xconfigure.window = c->win;
|
meillo@754
|
1091 synev.xconfigure.x = c->x;
|
meillo@754
|
1092 synev.xconfigure.y = c->y;
|
meillo@754
|
1093 synev.xconfigure.width = c->w;
|
meillo@754
|
1094 synev.xconfigure.height = c->h;
|
meillo@754
|
1095 synev.xconfigure.border_width = c->border;
|
meillo@754
|
1096 synev.xconfigure.above = None;
|
meillo@754
|
1097 XSendEvent(dpy, c->win, True, NoEventMask, &synev);
|
meillo@754
|
1098 }
|
meillo@754
|
1099
|
meillo@754
|
1100 void
|
meillo@754
|
1101 focus(Client *c) {
|
meillo@754
|
1102 if(c && !isvisible(c))
|
meillo@754
|
1103 return;
|
meillo@754
|
1104 if(sel && sel != c) {
|
meillo@754
|
1105 grabbuttons(sel, False);
|
meillo@764
|
1106 XSetWindowBorder(dpy, sel->win, dc.norm[ColBG]);
|
meillo@754
|
1107 }
|
meillo@754
|
1108 if(c) {
|
meillo@754
|
1109 detachstack(c);
|
meillo@754
|
1110 c->snext = stack;
|
meillo@754
|
1111 stack = c;
|
meillo@754
|
1112 grabbuttons(c, True);
|
meillo@754
|
1113 }
|
meillo@754
|
1114 sel = c;
|
meillo@754
|
1115 drawstatus();
|
meillo@754
|
1116 if(!selscreen)
|
meillo@754
|
1117 return;
|
meillo@754
|
1118 if(c) {
|
meillo@764
|
1119 XSetWindowBorder(dpy, c->win, dc.sel[ColBG]);
|
meillo@754
|
1120 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
|
meillo@754
|
1121 } else {
|
meillo@754
|
1122 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
|
meillo@754
|
1123 }
|
meillo@754
|
1124 }
|
meillo@754
|
1125
|
meillo@754
|
1126 Client *
|
meillo@754
|
1127 getclient(Window w) {
|
meillo@754
|
1128 Client *c;
|
meillo@754
|
1129
|
meillo@754
|
1130 for(c = clients; c; c = c->next) {
|
meillo@754
|
1131 if(c->win == w) {
|
meillo@754
|
1132 return c;
|
meillo@754
|
1133 }
|
meillo@754
|
1134 }
|
meillo@754
|
1135 return NULL;
|
meillo@754
|
1136 }
|
meillo@754
|
1137
|
meillo@754
|
1138 Bool
|
meillo@754
|
1139 isprotodel(Client *c) {
|
meillo@754
|
1140 int i, n;
|
meillo@754
|
1141 Atom *protocols;
|
meillo@754
|
1142 Bool ret = False;
|
meillo@754
|
1143
|
meillo@754
|
1144 if(XGetWMProtocols(dpy, c->win, &protocols, &n)) {
|
meillo@754
|
1145 for(i = 0; !ret && i < n; i++)
|
meillo@754
|
1146 if(protocols[i] == wmatom[WMDelete])
|
meillo@754
|
1147 ret = True;
|
meillo@754
|
1148 XFree(protocols);
|
meillo@754
|
1149 }
|
meillo@754
|
1150 return ret;
|
meillo@754
|
1151 }
|
meillo@754
|
1152
|
meillo@754
|
1153 void
|
meillo@756
|
1154 killclient() {
|
meillo@754
|
1155 if(!sel)
|
meillo@754
|
1156 return;
|
meillo@754
|
1157 if(isprotodel(sel))
|
meillo@754
|
1158 sendevent(sel->win, wmatom[WMProtocols], wmatom[WMDelete]);
|
meillo@754
|
1159 else
|
meillo@754
|
1160 XKillClient(dpy, sel->win);
|
meillo@754
|
1161 }
|
meillo@754
|
1162
|
meillo@754
|
1163 void
|
meillo@754
|
1164 manage(Window w, XWindowAttributes *wa) {
|
meillo@754
|
1165 Client *c;
|
meillo@754
|
1166 Window trans;
|
meillo@754
|
1167
|
meillo@754
|
1168 c = emallocz(sizeof(Client));
|
meillo@766
|
1169 c->tag = True;
|
meillo@754
|
1170 c->win = w;
|
meillo@754
|
1171 c->x = wa->x;
|
meillo@754
|
1172 c->y = wa->y;
|
meillo@754
|
1173 c->w = wa->width;
|
meillo@754
|
1174 c->h = wa->height;
|
meillo@754
|
1175 if(c->w == sw && c->h == sh) {
|
meillo@754
|
1176 c->border = 0;
|
meillo@754
|
1177 c->x = sx;
|
meillo@754
|
1178 c->y = sy;
|
meillo@754
|
1179 } else {
|
meillo@754
|
1180 c->border = BORDERPX;
|
meillo@754
|
1181 if(c->x + c->w + 2 * c->border > wax + waw)
|
meillo@754
|
1182 c->x = wax + waw - c->w - 2 * c->border;
|
meillo@754
|
1183 if(c->y + c->h + 2 * c->border > way + wah)
|
meillo@754
|
1184 c->y = way + wah - c->h - 2 * c->border;
|
meillo@754
|
1185 if(c->x < wax)
|
meillo@754
|
1186 c->x = wax;
|
meillo@754
|
1187 if(c->y < way)
|
meillo@754
|
1188 c->y = way;
|
meillo@754
|
1189 }
|
meillo@754
|
1190 updatesizehints(c);
|
meillo@754
|
1191 XSelectInput(dpy, c->win,
|
meillo@754
|
1192 StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
|
meillo@754
|
1193 XGetTransientForHint(dpy, c->win, &trans);
|
meillo@754
|
1194 grabbuttons(c, False);
|
meillo@764
|
1195 XSetWindowBorder(dpy, c->win, dc.norm[ColBG]);
|
meillo@754
|
1196 updatetitle(c);
|
meillo@766
|
1197 settag(c, getclient(trans));
|
meillo@754
|
1198 if(!c->isfloat)
|
meillo@754
|
1199 c->isfloat = trans || c->isfixed;
|
meillo@754
|
1200 if(clients)
|
meillo@754
|
1201 clients->prev = c;
|
meillo@754
|
1202 c->next = clients;
|
meillo@754
|
1203 c->snext = stack;
|
meillo@754
|
1204 stack = clients = c;
|
meillo@754
|
1205 XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
|
meillo@754
|
1206 XMapWindow(dpy, c->win);
|
meillo@754
|
1207 setclientstate(c, NormalState);
|
meillo@754
|
1208 if(isvisible(c))
|
meillo@754
|
1209 focus(c);
|
meillo@754
|
1210 arrange();
|
meillo@754
|
1211 }
|
meillo@754
|
1212
|
meillo@754
|
1213 void
|
meillo@754
|
1214 resize(Client *c, Bool sizehints) {
|
meillo@754
|
1215 float actual, dx, dy, max, min;
|
meillo@754
|
1216 XWindowChanges wc;
|
meillo@754
|
1217
|
meillo@754
|
1218 if(c->w <= 0 || c->h <= 0)
|
meillo@754
|
1219 return;
|
meillo@754
|
1220 if(sizehints) {
|
meillo@754
|
1221 if(c->minw && c->w < c->minw)
|
meillo@754
|
1222 c->w = c->minw;
|
meillo@754
|
1223 if(c->minh && c->h < c->minh)
|
meillo@754
|
1224 c->h = c->minh;
|
meillo@754
|
1225 if(c->maxw && c->w > c->maxw)
|
meillo@754
|
1226 c->w = c->maxw;
|
meillo@754
|
1227 if(c->maxh && c->h > c->maxh)
|
meillo@754
|
1228 c->h = c->maxh;
|
meillo@754
|
1229 /* inspired by algorithm from fluxbox */
|
meillo@754
|
1230 if(c->minay > 0 && c->maxay && (c->h - c->baseh) > 0) {
|
meillo@754
|
1231 dx = (float)(c->w - c->basew);
|
meillo@754
|
1232 dy = (float)(c->h - c->baseh);
|
meillo@754
|
1233 min = (float)(c->minax) / (float)(c->minay);
|
meillo@754
|
1234 max = (float)(c->maxax) / (float)(c->maxay);
|
meillo@754
|
1235 actual = dx / dy;
|
meillo@754
|
1236 if(max > 0 && min > 0 && actual > 0) {
|
meillo@754
|
1237 if(actual < min) {
|
meillo@754
|
1238 dy = (dx * min + dy) / (min * min + 1);
|
meillo@754
|
1239 dx = dy * min;
|
meillo@754
|
1240 c->w = (int)dx + c->basew;
|
meillo@754
|
1241 c->h = (int)dy + c->baseh;
|
meillo@754
|
1242 }
|
meillo@754
|
1243 else if(actual > max) {
|
meillo@754
|
1244 dy = (dx * min + dy) / (max * max + 1);
|
meillo@754
|
1245 dx = dy * min;
|
meillo@754
|
1246 c->w = (int)dx + c->basew;
|
meillo@754
|
1247 c->h = (int)dy + c->baseh;
|
meillo@754
|
1248 }
|
meillo@754
|
1249 }
|
meillo@754
|
1250 }
|
meillo@754
|
1251 if(c->incw)
|
meillo@754
|
1252 c->w -= (c->w - c->basew) % c->incw;
|
meillo@754
|
1253 if(c->inch)
|
meillo@754
|
1254 c->h -= (c->h - c->baseh) % c->inch;
|
meillo@754
|
1255 }
|
meillo@754
|
1256 if(c->w == sw && c->h == sh)
|
meillo@754
|
1257 c->border = 0;
|
meillo@754
|
1258 else
|
meillo@754
|
1259 c->border = BORDERPX;
|
meillo@754
|
1260 /* offscreen appearance fixes */
|
meillo@754
|
1261 if(c->x > sw)
|
meillo@754
|
1262 c->x = sw - c->w - 2 * c->border;
|
meillo@754
|
1263 if(c->y > sh)
|
meillo@754
|
1264 c->y = sh - c->h - 2 * c->border;
|
meillo@754
|
1265 if(c->x + c->w + 2 * c->border < sx)
|
meillo@754
|
1266 c->x = sx;
|
meillo@754
|
1267 if(c->y + c->h + 2 * c->border < sy)
|
meillo@754
|
1268 c->y = sy;
|
meillo@754
|
1269 wc.x = c->x;
|
meillo@754
|
1270 wc.y = c->y;
|
meillo@754
|
1271 wc.width = c->w;
|
meillo@754
|
1272 wc.height = c->h;
|
meillo@754
|
1273 wc.border_width = c->border;
|
meillo@754
|
1274 XConfigureWindow(dpy, c->win, CWX | CWY | CWWidth | CWHeight | CWBorderWidth, &wc);
|
meillo@754
|
1275 configure(c);
|
meillo@754
|
1276 XSync(dpy, False);
|
meillo@754
|
1277 }
|
meillo@754
|
1278
|
meillo@754
|
1279 void
|
meillo@754
|
1280 updatesizehints(Client *c) {
|
meillo@754
|
1281 long msize;
|
meillo@754
|
1282 XSizeHints size;
|
meillo@754
|
1283
|
meillo@754
|
1284 if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
|
meillo@754
|
1285 size.flags = PSize;
|
meillo@754
|
1286 c->flags = size.flags;
|
meillo@754
|
1287 if(c->flags & PBaseSize) {
|
meillo@754
|
1288 c->basew = size.base_width;
|
meillo@754
|
1289 c->baseh = size.base_height;
|
meillo@754
|
1290 } else {
|
meillo@754
|
1291 c->basew = c->baseh = 0;
|
meillo@754
|
1292 }
|
meillo@754
|
1293 if(c->flags & PResizeInc) {
|
meillo@754
|
1294 c->incw = size.width_inc;
|
meillo@754
|
1295 c->inch = size.height_inc;
|
meillo@754
|
1296 } else {
|
meillo@754
|
1297 c->incw = c->inch = 0;
|
meillo@754
|
1298 }
|
meillo@754
|
1299 if(c->flags & PMaxSize) {
|
meillo@754
|
1300 c->maxw = size.max_width;
|
meillo@754
|
1301 c->maxh = size.max_height;
|
meillo@754
|
1302 } else {
|
meillo@754
|
1303 c->maxw = c->maxh = 0;
|
meillo@754
|
1304 }
|
meillo@754
|
1305 if(c->flags & PMinSize) {
|
meillo@754
|
1306 c->minw = size.min_width;
|
meillo@754
|
1307 c->minh = size.min_height;
|
meillo@754
|
1308 } else {
|
meillo@754
|
1309 c->minw = c->minh = 0;
|
meillo@754
|
1310 }
|
meillo@754
|
1311 if(c->flags & PAspect) {
|
meillo@754
|
1312 c->minax = size.min_aspect.x;
|
meillo@754
|
1313 c->minay = size.min_aspect.y;
|
meillo@754
|
1314 c->maxax = size.max_aspect.x;
|
meillo@754
|
1315 c->maxay = size.max_aspect.y;
|
meillo@754
|
1316 } else {
|
meillo@754
|
1317 c->minax = c->minay = c->maxax = c->maxay = 0;
|
meillo@754
|
1318 }
|
meillo@754
|
1319 c->isfixed = (c->maxw && c->minw && c->maxh && c->minh &&
|
meillo@754
|
1320 c->maxw == c->minw && c->maxh == c->minh);
|
meillo@754
|
1321 }
|
meillo@754
|
1322
|
meillo@754
|
1323 void
|
meillo@754
|
1324 updatetitle(Client *c) {
|
meillo@754
|
1325 char **list = NULL;
|
meillo@754
|
1326 int n;
|
meillo@754
|
1327 XTextProperty name;
|
meillo@754
|
1328
|
meillo@754
|
1329 name.nitems = 0;
|
meillo@754
|
1330 c->name[0] = 0;
|
meillo@754
|
1331 XGetTextProperty(dpy, c->win, &name, netatom[NetWMName]);
|
meillo@754
|
1332 if(!name.nitems)
|
meillo@754
|
1333 XGetWMName(dpy, c->win, &name);
|
meillo@754
|
1334 if(!name.nitems)
|
meillo@754
|
1335 return;
|
meillo@754
|
1336 if(name.encoding == XA_STRING)
|
meillo@754
|
1337 strncpy(c->name, (char *)name.value, sizeof c->name);
|
meillo@754
|
1338 else {
|
meillo@754
|
1339 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success && n > 0 && *list) {
|
meillo@754
|
1340 strncpy(c->name, *list, sizeof c->name);
|
meillo@754
|
1341 XFreeStringList(list);
|
meillo@754
|
1342 }
|
meillo@754
|
1343 }
|
meillo@754
|
1344 XFree(name.value);
|
meillo@754
|
1345 }
|
meillo@754
|
1346
|
meillo@754
|
1347 void
|
meillo@754
|
1348 unmanage(Client *c) {
|
meillo@754
|
1349 Client *nc;
|
meillo@754
|
1350
|
meillo@754
|
1351 /* The server grab construct avoids race conditions. */
|
meillo@754
|
1352 XGrabServer(dpy);
|
meillo@754
|
1353 XSetErrorHandler(xerrordummy);
|
meillo@754
|
1354 detach(c);
|
meillo@754
|
1355 detachstack(c);
|
meillo@754
|
1356 if(sel == c) {
|
meillo@754
|
1357 for(nc = stack; nc && !isvisible(nc); nc = nc->snext);
|
meillo@754
|
1358 focus(nc);
|
meillo@754
|
1359 }
|
meillo@754
|
1360 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
|
meillo@754
|
1361 setclientstate(c, WithdrawnState);
|
meillo@754
|
1362 free(c);
|
meillo@754
|
1363 XSync(dpy, False);
|
meillo@754
|
1364 XSetErrorHandler(xerror);
|
meillo@754
|
1365 XUngrabServer(dpy);
|
meillo@754
|
1366 arrange();
|
meillo@754
|
1367 }
|
meillo@754
|
1368
|
meillo@773
|
1369 /* from main.c */
|
meillo@754
|
1370
|
meillo@773
|
1371 void
|
arg@487
|
1372 cleanup(void) {
|
arg@302
|
1373 close(STDIN_FILENO);
|
arg@645
|
1374 while(stack) {
|
arg@708
|
1375 resize(stack, True);
|
arg@645
|
1376 unmanage(stack);
|
garbeam@76
|
1377 }
|
arg@295
|
1378 if(dc.font.set)
|
arg@295
|
1379 XFreeFontSet(dpy, dc.font.set);
|
arg@295
|
1380 else
|
arg@295
|
1381 XFreeFont(dpy, dc.font.xfont);
|
arg@292
|
1382 XUngrabKey(dpy, AnyKey, AnyModifier, root);
|
arg@295
|
1383 XFreePixmap(dpy, dc.drawable);
|
arg@295
|
1384 XFreeGC(dpy, dc.gc);
|
arg@309
|
1385 XDestroyWindow(dpy, barwin);
|
arg@616
|
1386 XFreeCursor(dpy, cursor[CurNormal]);
|
arg@616
|
1387 XFreeCursor(dpy, cursor[CurResize]);
|
arg@616
|
1388 XFreeCursor(dpy, cursor[CurMove]);
|
garbeam@76
|
1389 XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
|
arg@292
|
1390 XSync(dpy, False);
|
garbeam@76
|
1391 }
|
garbeam@0
|
1392
|
meillo@773
|
1393 void
|
arg@487
|
1394 scan(void) {
|
garbeam@0
|
1395 unsigned int i, num;
|
arg@123
|
1396 Window *wins, d1, d2;
|
garbeam@0
|
1397 XWindowAttributes wa;
|
garbeam@0
|
1398
|
arg@292
|
1399 wins = NULL;
|
garbeam@0
|
1400 if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
|
garbeam@0
|
1401 for(i = 0; i < num; i++) {
|
garbeam@0
|
1402 if(!XGetWindowAttributes(dpy, wins[i], &wa))
|
garbeam@0
|
1403 continue;
|
garbeam@0
|
1404 if(wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
|
garbeam@0
|
1405 continue;
|
garbeam@0
|
1406 if(wa.map_state == IsViewable)
|
garbeam@10
|
1407 manage(wins[i], &wa);
|
garbeam@0
|
1408 }
|
garbeam@0
|
1409 }
|
garbeam@0
|
1410 if(wins)
|
garbeam@0
|
1411 XFree(wins);
|
garbeam@0
|
1412 }
|
garbeam@0
|
1413
|
meillo@773
|
1414 void
|
arg@487
|
1415 setup(void) {
|
arg@333
|
1416 int i, j;
|
arg@333
|
1417 unsigned int mask;
|
arg@333
|
1418 Window w;
|
arg@333
|
1419 XModifierKeymap *modmap;
|
arg@333
|
1420 XSetWindowAttributes wa;
|
arg@333
|
1421
|
arg@333
|
1422 /* init atoms */
|
arg@333
|
1423 wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
|
arg@333
|
1424 wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
|
arg@725
|
1425 wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
|
arg@333
|
1426 netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
|
arg@333
|
1427 netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
|
arg@333
|
1428 XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
|
arg@333
|
1429 PropModeReplace, (unsigned char *) netatom, NetLast);
|
arg@333
|
1430 /* init cursors */
|
arg@333
|
1431 cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
|
arg@333
|
1432 cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
|
arg@333
|
1433 cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
|
arg@532
|
1434 /* init modifier map */
|
arg@679
|
1435 numlockmask = 0;
|
arg@333
|
1436 modmap = XGetModifierMapping(dpy);
|
arg@333
|
1437 for (i = 0; i < 8; i++) {
|
arg@333
|
1438 for (j = 0; j < modmap->max_keypermod; j++) {
|
arg@333
|
1439 if(modmap->modifiermap[i * modmap->max_keypermod + j] == XKeysymToKeycode(dpy, XK_Num_Lock))
|
arg@333
|
1440 numlockmask = (1 << i);
|
arg@333
|
1441 }
|
arg@333
|
1442 }
|
arg@616
|
1443 XFreeModifiermap(modmap);
|
arg@532
|
1444 /* select for events */
|
arg@461
|
1445 wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask
|
arg@461
|
1446 | EnterWindowMask | LeaveWindowMask;
|
arg@333
|
1447 wa.cursor = cursor[CurNormal];
|
arg@333
|
1448 XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
|
arg@333
|
1449 grabkeys();
|
meillo@766
|
1450 seltag = True;
|
arg@333
|
1451 /* style */
|
arg@353
|
1452 dc.norm[ColBG] = getcolor(NORMBGCOLOR);
|
arg@353
|
1453 dc.norm[ColFG] = getcolor(NORMFGCOLOR);
|
arg@353
|
1454 dc.sel[ColBG] = getcolor(SELBGCOLOR);
|
arg@353
|
1455 dc.sel[ColFG] = getcolor(SELFGCOLOR);
|
arg@333
|
1456 setfont(FONT);
|
arg@532
|
1457 /* geometry */
|
arg@333
|
1458 sx = sy = 0;
|
arg@333
|
1459 sw = DisplayWidth(dpy, screen);
|
arg@333
|
1460 sh = DisplayHeight(dpy, screen);
|
arg@650
|
1461 nmaster = NMASTER;
|
meillo@773
|
1462 arrange = DEFMODE;
|
arg@532
|
1463 /* bar */
|
arg@353
|
1464 dc.h = bh = dc.font.height + 2;
|
arg@333
|
1465 wa.override_redirect = 1;
|
arg@333
|
1466 wa.background_pixmap = ParentRelative;
|
arg@333
|
1467 wa.event_mask = ButtonPressMask | ExposureMask;
|
meillo@754
|
1468 barwin = XCreateWindow(dpy, root, sx, sy, sw, bh, 0,
|
arg@739
|
1469 DefaultDepth(dpy, screen), CopyFromParent, DefaultVisual(dpy, screen),
|
arg@333
|
1470 CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
|
arg@333
|
1471 XDefineCursor(dpy, barwin, cursor[CurNormal]);
|
arg@333
|
1472 XMapRaised(dpy, barwin);
|
meillo@772
|
1473 strcpy(stext, "aewl-"VERSION);
|
arg@565
|
1474 /* windowarea */
|
arg@565
|
1475 wax = sx;
|
meillo@754
|
1476 way = sy + bh;
|
arg@565
|
1477 wah = sh - bh;
|
arg@565
|
1478 waw = sw;
|
arg@532
|
1479 /* pixmap for everything */
|
arg@333
|
1480 dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
|
arg@333
|
1481 dc.gc = XCreateGC(dpy, root, 0, 0);
|
arg@344
|
1482 XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
|
arg@532
|
1483 /* multihead support */
|
arg@716
|
1484 selscreen = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask);
|
arg@333
|
1485 }
|
arg@333
|
1486
|
garbeam@76
|
1487 /*
|
garbeam@76
|
1488 * Startup Error handler to check if another window manager
|
garbeam@76
|
1489 * is already running.
|
garbeam@76
|
1490 */
|
meillo@773
|
1491 int
|
arg@461
|
1492 xerrorstart(Display *dsply, XErrorEvent *ee) {
|
garbeam@76
|
1493 otherwm = True;
|
garbeam@76
|
1494 return -1;
|
garbeam@76
|
1495 }
|
garbeam@76
|
1496
|
garbeam@13
|
1497 void
|
arg@461
|
1498 sendevent(Window w, Atom a, long value) {
|
garbeam@13
|
1499 XEvent e;
|
garbeam@13
|
1500
|
garbeam@13
|
1501 e.type = ClientMessage;
|
garbeam@13
|
1502 e.xclient.window = w;
|
garbeam@13
|
1503 e.xclient.message_type = a;
|
garbeam@13
|
1504 e.xclient.format = 32;
|
garbeam@13
|
1505 e.xclient.data.l[0] = value;
|
garbeam@13
|
1506 e.xclient.data.l[1] = CurrentTime;
|
garbeam@13
|
1507 XSendEvent(dpy, w, False, NoEventMask, &e);
|
garbeam@79
|
1508 XSync(dpy, False);
|
garbeam@13
|
1509 }
|
garbeam@13
|
1510
|
garbeam@76
|
1511 void
|
meillo@756
|
1512 quit() {
|
arg@302
|
1513 readin = running = False;
|
garbeam@75
|
1514 }
|
garbeam@75
|
1515
|
arg@532
|
1516 /* There's no way to check accesses to destroyed windows, thus those cases are
|
garbeam@84
|
1517 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
|
arg@455
|
1518 * default error handler, which may call exit.
|
garbeam@0
|
1519 */
|
garbeam@10
|
1520 int
|
arg@461
|
1521 xerror(Display *dpy, XErrorEvent *ee) {
|
garbeam@75
|
1522 if(ee->error_code == BadWindow
|
arg@123
|
1523 || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
|
arg@123
|
1524 || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
|
arg@123
|
1525 || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
|
arg@123
|
1526 || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
|
arg@123
|
1527 || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
|
arg@458
|
1528 || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
|
arg@458
|
1529 || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
|
garbeam@0
|
1530 return 0;
|
meillo@772
|
1531 fprintf(stderr, "aewl: fatal error: request code=%d, error code=%d\n",
|
arg@123
|
1532 ee->request_code, ee->error_code);
|
arg@455
|
1533 return xerrorxlib(dpy, ee); /* may call exit */
|
garbeam@27
|
1534 }
|
garbeam@27
|
1535
|
garbeam@0
|
1536 int
|
arg@461
|
1537 main(int argc, char *argv[]) {
|
arg@581
|
1538 char *p;
|
arg@333
|
1539 int r, xfd;
|
garbeam@59
|
1540 fd_set rd;
|
garbeam@0
|
1541
|
arg@137
|
1542 if(argc == 2 && !strncmp("-v", argv[1], 3)) {
|
meillo@772
|
1543 fputs("aewl-"VERSION", Copyright 2008 markus schnalke <meillo@marmaro.de>\n", stdout);
|
meillo@772
|
1544 fputs("forked off dwm-3.4, (C)opyright MMVI-MMVII Anselm R. Garbe\n", stdout);
|
arg@137
|
1545 exit(EXIT_SUCCESS);
|
meillo@755
|
1546 } else if(argc != 1) {
|
meillo@772
|
1547 eprint("usage: aewl [-v]\n");
|
garbeam@0
|
1548 }
|
arg@619
|
1549 setlocale(LC_CTYPE, "");
|
garbeam@0
|
1550 dpy = XOpenDisplay(0);
|
meillo@755
|
1551 if(!dpy) {
|
meillo@772
|
1552 eprint("aewl: cannot open display\n");
|
meillo@755
|
1553 }
|
arg@265
|
1554 xfd = ConnectionNumber(dpy);
|
garbeam@0
|
1555 screen = DefaultScreen(dpy);
|
garbeam@0
|
1556 root = RootWindow(dpy, screen);
|
garbeam@75
|
1557 otherwm = False;
|
garbeam@75
|
1558 XSetErrorHandler(xerrorstart);
|
arg@197
|
1559 /* this causes an error if some other window manager is running */
|
garbeam@0
|
1560 XSelectInput(dpy, root, SubstructureRedirectMask);
|
garbeam@78
|
1561 XSync(dpy, False);
|
meillo@755
|
1562 if(otherwm) {
|
meillo@772
|
1563 eprint("aewl: another window manager is already running\n");
|
meillo@755
|
1564 }
|
garbeam@0
|
1565
|
arg@292
|
1566 XSync(dpy, False);
|
garbeam@78
|
1567 XSetErrorHandler(NULL);
|
garbeam@75
|
1568 xerrorxlib = XSetErrorHandler(xerror);
|
arg@275
|
1569 XSync(dpy, False);
|
arg@333
|
1570 setup();
|
garbeam@74
|
1571 drawstatus();
|
garbeam@75
|
1572 scan();
|
garbeam@3
|
1573
|
arg@214
|
1574 /* main event loop, also reads status text from stdin */
|
arg@242
|
1575 XSync(dpy, False);
|
arg@292
|
1576 procevent();
|
arg@302
|
1577 readin = True;
|
garbeam@5
|
1578 while(running) {
|
garbeam@59
|
1579 FD_ZERO(&rd);
|
arg@164
|
1580 if(readin)
|
arg@164
|
1581 FD_SET(STDIN_FILENO, &rd);
|
arg@265
|
1582 FD_SET(xfd, &rd);
|
arg@578
|
1583 if(select(xfd + 1, &rd, NULL, NULL, NULL) == -1) {
|
arg@578
|
1584 if(errno == EINTR)
|
arg@578
|
1585 continue;
|
arg@581
|
1586 eprint("select failed\n");
|
arg@578
|
1587 }
|
arg@578
|
1588 if(FD_ISSET(STDIN_FILENO, &rd)) {
|
arg@581
|
1589 switch(r = read(STDIN_FILENO, stext, sizeof stext - 1)) {
|
arg@578
|
1590 case -1:
|
arg@581
|
1591 strncpy(stext, strerror(errno), sizeof stext - 1);
|
arg@583
|
1592 stext[sizeof stext - 1] = '\0';
|
arg@578
|
1593 readin = False;
|
arg@578
|
1594 break;
|
arg@578
|
1595 case 0:
|
arg@583
|
1596 strncpy(stext, "EOF", 4);
|
arg@578
|
1597 readin = False;
|
arg@578
|
1598 break;
|
arg@578
|
1599 default:
|
arg@582
|
1600 for(stext[r] = '\0', p = stext + strlen(stext) - 1; p >= stext && *p == '\n'; *p-- = '\0');
|
arg@744
|
1601 for(; p >= stext && *p != '\n'; --p);
|
arg@581
|
1602 if(p > stext)
|
arg@581
|
1603 strncpy(stext, p + 1, sizeof stext);
|
garbeam@59
|
1604 }
|
arg@578
|
1605 drawstatus();
|
garbeam@59
|
1606 }
|
arg@578
|
1607 if(FD_ISSET(xfd, &rd))
|
arg@578
|
1608 procevent();
|
garbeam@5
|
1609 }
|
garbeam@0
|
1610 cleanup();
|
garbeam@0
|
1611 XCloseDisplay(dpy);
|
garbeam@0
|
1612 return 0;
|
garbeam@0
|
1613 }
|