2 * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
3 * See LICENSE file for license details.
12 #include <sys/types.h>
15 #include <X11/cursorfont.h>
16 #include <X11/Xatom.h>
17 #include <X11/Xproto.h>
21 /********** CUSTOMIZE **********/
24 [Tscratch] = "scratch",
32 static char *cmdwallpaper[] = {
33 "feh", "--bg-scale", "/home/garbeam/wallpaper/bg.jpg", NULL
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
42 /********** CUSTOMIZE **********/
47 Atom wm_atom[WMLast], net_atom[NetLast];
48 Cursor cursor[CurLast];
53 int tsel = Tdev; /* default tag */
54 int screen, sx, sy, sw, sh, bx, by, bw, bh;
57 Client *clients = 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 *);
66 usage() { error("usage: gridwm [-v]\n"); }
76 if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
77 for(i = 0; i < num; i++) {
78 if(!XGetWindowAttributes(dpy, wins[i], &wa))
80 if(wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
82 if(wa.map_state == IsViewable)
91 win_property(Window w, Atom a, Atom t, long l, unsigned char **prop)
95 unsigned long res, extra;
98 status = XGetWindowProperty(dpy, w, a, 0L, l, False, t, &real, &format,
101 if(status != Success || *prop == 0) {
105 free((void *) *prop);
113 unsigned char *protocols;
118 res = win_property(w, wm_atom[WMProtocols], XA_ATOM, 20L, &protocols);
122 for(i = 0; i < res; i++) {
123 if(protocols[i] == wm_atom[WMDelete])
124 protos |= WM_PROTOCOL_DELWIN;
126 free((char *) protocols);
131 send_message(Window w, Atom a, long value)
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);
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
152 error_handler(Display *dpy, XErrorEvent *error)
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))
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() */
174 * Startup Error handler to check if another window manager
175 * is already running.
178 startup_error_handler(Display *dpy, XErrorEvent *error)
180 other_wm_running = True;
189 XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
205 main(int argc, char *argv[])
208 XSetWindowAttributes wa;
213 struct timeval t, timeout = {
215 .tv_sec = STATUSDELAY,
218 /* command line args */
219 for(i = 1; (i < argc) && (argv[i][0] == '-'); i++) {
220 switch (argv[i][1]) {
222 fprintf(stdout, "%s", version);
231 dpy = XOpenDisplay(0);
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);
246 error("gridwm: another window manager is already running\n");
248 spawn(dpy, cmdwallpaper);
250 sw = DisplayWidth(dpy, screen);
251 sh = DisplayHeight(dpy, screen);
252 issel = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask);
255 x_error_handler = XSetErrorHandler(error_handler);
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);
268 cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
269 cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
270 cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
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;
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);
297 wa.event_mask = SubstructureRedirectMask | EnterWindowMask \
299 wa.cursor = cursor[CurNormal];
300 XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
306 if(XPending(dpy) > 0) {
307 XNextEvent(dpy, &ev);
309 (handler[ev.type]) (&ev); /* call handler */
313 FD_SET(ConnectionNumber(dpy), &fds);
315 if(select(ConnectionNumber(dpy) + 1, &fds, NULL, NULL, &t) > 0)
317 else if(errno != EINTR) {
318 pipe_spawn(stext, sizeof(stext), dpy, cmdstatus);