aewl

view wm.c @ 31:386649deb651

before leaning things up
author Anselm R. Garbe <garbeam@wmii.de>
date Thu, 13 Jul 2006 01:04:38 +0200
parents 2e0fb4130bfb
children 082c75b937b5
line source
1 /*
2 * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
3 * See LICENSE file for license details.
4 */
6 #include <errno.h>
8 #include <stdarg.h>
9 #include <stdio.h>
10 #include <stdlib.h>
12 #include <sys/types.h>
13 #include <sys/time.h>
15 #include <X11/cursorfont.h>
16 #include <X11/Xatom.h>
17 #include <X11/Xproto.h>
19 #include "wm.h"
21 /********** CUSTOMIZE **********/
23 char *tags[TLast] = {
24 [Tscratch] = "scratch",
25 [Tdev] = "dev",
26 [Tirc] = "irc",
27 [Twww] = "www",
28 [Twork] = "work",
29 };
31 /* commands */
32 static char *cmdwallpaper[] = {
33 "feh", "--bg-scale", "/home/garbeam/wallpaper/bg.jpg", NULL
34 };
36 static char *cmdstatus[] = {
37 "sh", "-c", "echo -n `date '+%Y-%m-%d %H:%M'`"
38 " `uptime | sed 's/.*://; s/,//g'`"
39 " `acpi | awk '{print $4}' | sed 's/,//'`", NULL
40 };
42 /********** CUSTOMIZE **********/
44 /* X structs */
45 Display *dpy;
46 Window root, barwin;
47 Atom wm_atom[WMLast], net_atom[NetLast];
48 Cursor cursor[CurLast];
49 Bool running = True;
50 Bool issel;
52 char stext[1024];
53 int tsel = Tdev; /* default tag */
54 int screen, sx, sy, sw, sh, bx, by, bw, bh;
56 Brush brush = {0};
57 Client *clients = NULL;
58 Client *stack = NULL;
60 static Bool other_wm_running;
61 static const char version[] =
62 "gridwm - " VERSION ", (C)opyright MMVI Anselm R. Garbe\n";
63 static int (*x_error_handler) (Display *, XErrorEvent *);
65 static void
66 usage() { error("usage: gridwm [-v]\n"); }
68 static void
69 scan_wins()
70 {
71 unsigned int i, num;
72 Window *wins;
73 XWindowAttributes wa;
74 Window d1, d2;
76 if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
77 for(i = 0; i < num; i++) {
78 if(!XGetWindowAttributes(dpy, wins[i], &wa))
79 continue;
80 if(wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
81 continue;
82 if(wa.map_state == IsViewable)
83 manage(wins[i], &wa);
84 }
85 }
86 if(wins)
87 XFree(wins);
88 }
90 static int
91 win_property(Window w, Atom a, Atom t, long l, unsigned char **prop)
92 {
93 Atom real;
94 int format;
95 unsigned long res, extra;
96 int status;
98 status = XGetWindowProperty(dpy, w, a, 0L, l, False, t, &real, &format,
99 &res, &extra, prop);
101 if(status != Success || *prop == 0) {
102 return 0;
103 }
104 if(res == 0) {
105 free((void *) *prop);
106 }
107 return res;
108 }
110 int
111 win_proto(Window w)
112 {
113 unsigned char *protocols;
114 long res;
115 int protos = 0;
116 int i;
118 res = win_property(w, wm_atom[WMProtocols], XA_ATOM, 20L, &protocols);
119 if(res <= 0) {
120 return protos;
121 }
122 for(i = 0; i < res; i++) {
123 if(protocols[i] == wm_atom[WMDelete])
124 protos |= WM_PROTOCOL_DELWIN;
125 }
126 free((char *) protocols);
127 return protos;
128 }
130 void
131 send_message(Window w, Atom a, long value)
132 {
133 XEvent e;
135 e.type = ClientMessage;
136 e.xclient.window = w;
137 e.xclient.message_type = a;
138 e.xclient.format = 32;
139 e.xclient.data.l[0] = value;
140 e.xclient.data.l[1] = CurrentTime;
141 XSendEvent(dpy, w, False, NoEventMask, &e);
142 XFlush(dpy);
143 }
145 /*
146 * There's no way to check accesses to destroyed windows, thus
147 * those cases are ignored (especially on UnmapNotify's).
148 * Other types of errors call Xlib's default error handler, which
149 * calls exit().
150 */
151 int
152 error_handler(Display *dpy, XErrorEvent *error)
153 {
154 if(error->error_code == BadWindow
155 || (error->request_code == X_SetInputFocus
156 && error->error_code == BadMatch)
157 || (error->request_code == X_PolyText8
158 && error->error_code == BadDrawable)
159 || (error->request_code == X_PolyFillRectangle
160 && error->error_code == BadDrawable)
161 || (error->request_code == X_PolySegment
162 && error->error_code == BadDrawable)
163 || (error->request_code == X_ConfigureWindow
164 && error->error_code == BadMatch)
165 || (error->request_code == X_GrabKey
166 && error->error_code == BadAccess))
167 return 0;
168 fprintf(stderr, "gridwm: fatal error: request code=%d, error code=%d\n",
169 error->request_code, error->error_code);
170 return x_error_handler(dpy, error); /* may call exit() */
171 }
173 /*
174 * Startup Error handler to check if another window manager
175 * is already running.
176 */
177 static int
178 startup_error_handler(Display *dpy, XErrorEvent *error)
179 {
180 other_wm_running = True;
181 return -1;
182 }
184 static void
185 cleanup()
186 {
187 while(clients)
188 unmanage(clients);
189 XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
190 }
192 void
193 run(void *aux)
194 {
195 spawn(dpy, aux);
196 }
198 void
199 quit(void *aux)
200 {
201 running = False;
202 }
204 int
205 main(int argc, char *argv[])
206 {
207 int i;
208 XSetWindowAttributes wa;
209 unsigned int mask;
210 Window w;
211 XEvent ev;
212 fd_set fds;
213 struct timeval t, timeout = {
214 .tv_usec = 0,
215 .tv_sec = STATUSDELAY,
216 };
218 /* command line args */
219 for(i = 1; (i < argc) && (argv[i][0] == '-'); i++) {
220 switch (argv[i][1]) {
221 case 'v':
222 fprintf(stdout, "%s", version);
223 exit(0);
224 break;
225 default:
226 usage();
227 break;
228 }
229 }
231 dpy = XOpenDisplay(0);
232 if(!dpy)
233 error("gridwm: cannot connect X server\n");
235 screen = DefaultScreen(dpy);
236 root = RootWindow(dpy, screen);
238 /* check if another WM is already running */
239 other_wm_running = False;
240 XSetErrorHandler(startup_error_handler);
241 /* this causes an error if some other WM is running */
242 XSelectInput(dpy, root, SubstructureRedirectMask);
243 XFlush(dpy);
245 if(other_wm_running)
246 error("gridwm: another window manager is already running\n");
248 spawn(dpy, cmdwallpaper);
249 sx = sy = 0;
250 sw = DisplayWidth(dpy, screen);
251 sh = DisplayHeight(dpy, screen);
252 issel = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask);
254 XSetErrorHandler(0);
255 x_error_handler = XSetErrorHandler(error_handler);
257 /* init atoms */
258 wm_atom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
259 wm_atom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
260 net_atom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
261 net_atom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
263 XChangeProperty(dpy, root, net_atom[NetSupported], XA_ATOM, 32,
264 PropModeReplace, (unsigned char *) net_atom, NetLast);
267 /* init cursors */
268 cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
269 cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
270 cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
272 update_keys();
274 /* style */
275 loadcolors(dpy, screen, &brush, BGCOLOR, FGCOLOR, BORDERCOLOR);
276 loadfont(dpy, &brush.font, FONT);
278 wa.override_redirect = 1;
279 wa.background_pixmap = ParentRelative;
280 wa.event_mask = ExposureMask;
282 bx = by = 0;
283 bw = sw;
284 bh = texth(&brush.font);
285 barwin = XCreateWindow(dpy, root, bx, by, bw, bh, 0, DefaultDepth(dpy, screen),
286 CopyFromParent, DefaultVisual(dpy, screen),
287 CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
288 XDefineCursor(dpy, barwin, cursor[CurNormal]);
289 XMapRaised(dpy, barwin);
291 brush.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
292 brush.gc = XCreateGC(dpy, root, 0, 0);
294 pipe_spawn(stext, sizeof(stext), dpy, cmdstatus);
295 draw_bar();
297 wa.event_mask = SubstructureRedirectMask | EnterWindowMask \
298 | LeaveWindowMask;
299 wa.cursor = cursor[CurNormal];
300 XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
302 arrange = grid;
303 scan_wins();
305 while(running) {
306 if(XPending(dpy) > 0) {
307 XNextEvent(dpy, &ev);
308 if(handler[ev.type])
309 (handler[ev.type]) (&ev); /* call handler */
310 continue;
311 }
312 FD_ZERO(&fds);
313 FD_SET(ConnectionNumber(dpy), &fds);
314 t = timeout;
315 if(select(ConnectionNumber(dpy) + 1, &fds, NULL, NULL, &t) > 0)
316 continue;
317 else if(errno != EINTR) {
318 pipe_spawn(stext, sizeof(stext), dpy, cmdstatus);
319 draw_bar();
320 }
321 }
323 cleanup();
324 XCloseDisplay(dpy);
326 return 0;
327 }