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>
24 Atom wm_atom[WMLast], net_atom[NetLast];
25 Cursor cursor[CurLast];
26 XRectangle rect, barrect;
30 char *bartext, tag[256];
34 Client *clients = NULL;
37 static Bool other_wm_running;
38 static char version[] = "gridwm - " VERSION ", (C)opyright MMVI Anselm R. Garbe\n";
39 static int (*x_error_handler) (Display *, XErrorEvent *);
44 fputs("usage: gridwm [-v]\n", stderr);
56 if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
57 for(i = 0; i < num; i++) {
58 if(!XGetWindowAttributes(dpy, wins[i], &wa))
60 if(wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
62 if(wa.map_state == IsViewable)
71 win_property(Window w, Atom a, Atom t, long l, unsigned char **prop)
75 unsigned long res, extra;
78 status = XGetWindowProperty(dpy, w, a, 0L, l, False, t, &real, &format,
81 if(status != Success || *prop == 0) {
98 res = win_property(w, wm_atom[WMProtocols], XA_ATOM, 20L,
99 ((unsigned char **) &protocols));
103 for(i = 0; i < res; i++) {
104 if(protocols[i] == wm_atom[WMDelete])
105 protos |= WM_PROTOCOL_DELWIN;
107 free((char *) protocols);
112 send_message(Window w, Atom a, long value)
116 e.type = ClientMessage;
117 e.xclient.window = w;
118 e.xclient.message_type = a;
119 e.xclient.format = 32;
120 e.xclient.data.l[0] = value;
121 e.xclient.data.l[1] = CurrentTime;
122 XSendEvent(dpy, w, False, NoEventMask, &e);
127 * There's no way to check accesses to destroyed windows, thus
128 * those cases are ignored (especially on UnmapNotify's).
129 * Other types of errors call Xlib's default error handler, which
133 error_handler(Display *dpy, XErrorEvent *error)
135 if(error->error_code == BadWindow
136 || (error->request_code == X_SetInputFocus
137 && error->error_code == BadMatch)
138 || (error->request_code == X_PolyText8
139 && error->error_code == BadDrawable)
140 || (error->request_code == X_PolyFillRectangle
141 && error->error_code == BadDrawable)
142 || (error->request_code == X_PolySegment
143 && error->error_code == BadDrawable)
144 || (error->request_code == X_ConfigureWindow
145 && error->error_code == BadMatch)
146 || (error->request_code == X_GrabKey
147 && error->error_code == BadAccess))
149 fprintf(stderr, "gridwm: fatal error: request code=%d, error code=%d\n",
150 error->request_code, error->error_code);
151 return x_error_handler(dpy, error); /* may call exit() */
155 * Startup Error handler to check if another window manager
156 * is already running.
159 startup_error_handler(Display *dpy, XErrorEvent *error)
161 other_wm_running = True;
170 XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
174 main(int argc, char *argv[])
177 XSetWindowAttributes wa;
182 struct timeval t, timeout = {
184 .tv_sec = STATUSDELAY,
187 /* command line args */
188 for(i = 1; (i < argc) && (argv[i][0] == '-'); i++) {
189 switch (argv[i][1]) {
191 fprintf(stdout, "%s", version);
200 dpy = XOpenDisplay(0);
202 error("gridwm: cannot connect X server\n");
204 screen = DefaultScreen(dpy);
205 root = RootWindow(dpy, screen);
207 /* check if another WM is already running */
208 other_wm_running = False;
209 XSetErrorHandler(startup_error_handler);
210 /* this causes an error if some other WM is running */
211 XSelectInput(dpy, root, SubstructureRedirectMask);
215 error("gridwm: another window manager is already running\n");
218 rect.width = DisplayWidth(dpy, screen);
219 rect.height = DisplayHeight(dpy, screen);
220 sel_screen = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask);
223 x_error_handler = XSetErrorHandler(error_handler);
226 wm_atom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
227 wm_atom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
228 net_atom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
229 net_atom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
231 XChangeProperty(dpy, root, net_atom[NetSupported], XA_ATOM, 32,
232 PropModeReplace, (unsigned char *) net_atom, NetLast);
236 cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
237 cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
238 cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
242 brush.drawable = XCreatePixmap(dpy, root, rect.width, rect.height,
243 DefaultDepth(dpy, screen));
244 brush.gc = XCreateGC(dpy, root, 0, 0);
247 loadcolors(dpy, screen, &brush, BGCOLOR, FGCOLOR, BORDERCOLOR);
248 loadfont(dpy, &brush.font, FONT);
250 wa.override_redirect = 1;
251 wa.background_pixmap = ParentRelative;
252 wa.event_mask = ExposureMask;
255 barrect.height = labelheight(&brush.font);
256 barrect.y = rect.height - barrect.height;
257 barwin = XCreateWindow(dpy, root, barrect.x, barrect.y,
258 barrect.width, barrect.height, 0, DefaultDepth(dpy, screen),
259 CopyFromParent, DefaultVisual(dpy, screen),
260 CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
262 XDefineCursor(dpy, barwin, cursor[CurNormal]);
263 XMapRaised(dpy, barwin);
266 wa.event_mask = SubstructureRedirectMask | EnterWindowMask \
268 wa.cursor = cursor[CurNormal];
269 XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
274 if(XPending(dpy) > 0) {
275 XNextEvent(dpy, &ev);
277 (handler[ev.type]) (&ev); /* call handler */
281 FD_SET(ConnectionNumber(dpy), &fds);
283 if(select(ConnectionNumber(dpy) + 1, &fds, NULL, NULL, &t) > 0)
285 else if(errno != EINTR)