2 * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
3 * See LICENSE file for license details.
10 #include <X11/cursorfont.h>
11 #include <X11/Xatom.h>
12 #include <X11/Xproto.h>
19 Atom wm_atom[WMLast], net_atom[NetLast];
20 Cursor cursor[CurLast];
21 XRectangle rect, barrect;
25 char *bartext, tag[256];
29 Client *clients = NULL;
32 static Bool other_wm_running;
33 static char version[] = "gridwm - " VERSION ", (C)opyright MMVI Anselm R. Garbe\n";
34 static int (*x_error_handler) (Display *, XErrorEvent *);
39 fputs("usage: gridwm [-v]\n", stderr);
51 if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
52 for(i = 0; i < num; i++) {
53 if(!XGetWindowAttributes(dpy, wins[i], &wa))
55 if(wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
57 if(wa.map_state == IsViewable)
66 win_property(Window w, Atom a, Atom t, long l, unsigned char **prop)
70 unsigned long res, extra;
73 status = XGetWindowProperty(dpy, w, a, 0L, l, False, t, &real, &format,
76 if(status != Success || *prop == 0) {
93 res = win_property(w, wm_atom[WMProtocols], XA_ATOM, 20L,
94 ((unsigned char **) &protocols));
98 for(i = 0; i < res; i++) {
99 if(protocols[i] == wm_atom[WMDelete])
100 protos |= WM_PROTOCOL_DELWIN;
102 free((char *) protocols);
107 send_message(Window w, Atom a, long value)
111 e.type = ClientMessage;
112 e.xclient.window = w;
113 e.xclient.message_type = a;
114 e.xclient.format = 32;
115 e.xclient.data.l[0] = value;
116 e.xclient.data.l[1] = CurrentTime;
117 XSendEvent(dpy, w, False, NoEventMask, &e);
122 * There's no way to check accesses to destroyed windows, thus
123 * those cases are ignored (especially on UnmapNotify's).
124 * Other types of errors call Xlib's default error handler, which
128 error_handler(Display *dpy, XErrorEvent *error)
130 if(error->error_code == BadWindow
131 || (error->request_code == X_SetInputFocus
132 && error->error_code == BadMatch)
133 || (error->request_code == X_PolyText8
134 && error->error_code == BadDrawable)
135 || (error->request_code == X_PolyFillRectangle
136 && error->error_code == BadDrawable)
137 || (error->request_code == X_PolySegment
138 && error->error_code == BadDrawable)
139 || (error->request_code == X_ConfigureWindow
140 && error->error_code == BadMatch)
141 || (error->request_code == X_GrabKey
142 && error->error_code == BadAccess))
144 fprintf(stderr, "gridwm: fatal error: request code=%d, error code=%d\n",
145 error->request_code, error->error_code);
146 return x_error_handler(dpy, error); /* may call exit() */
150 * Startup Error handler to check if another window manager
151 * is already running.
154 startup_error_handler(Display *dpy, XErrorEvent *error)
156 other_wm_running = True;
165 for(c=client; c; c=c->next)
166 reparent_client(c, root, c->sel->rect.x, c->sel->rect.y);
167 XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
172 main(int argc, char *argv[])
175 XSetWindowAttributes wa;
180 /* command line args */
181 for(i = 1; (i < argc) && (argv[i][0] == '-'); i++) {
182 switch (argv[i][1]) {
184 fprintf(stdout, "%s", version);
193 dpy = XOpenDisplay(0);
195 error("gridwm: cannot connect X server\n");
197 screen = DefaultScreen(dpy);
198 root = RootWindow(dpy, screen);
200 /* check if another WM is already running */
201 other_wm_running = False;
202 XSetErrorHandler(startup_error_handler);
203 /* this causes an error if some other WM is running */
204 XSelectInput(dpy, root, SubstructureRedirectMask);
208 error("gridwm: another window manager is already running\n");
211 rect.width = DisplayWidth(dpy, screen);
212 rect.height = DisplayHeight(dpy, screen);
213 sel_screen = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask);
216 x_error_handler = XSetErrorHandler(error_handler);
219 wm_atom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
220 wm_atom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
221 net_atom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
222 net_atom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
224 XChangeProperty(dpy, root, net_atom[NetSupported], XA_ATOM, 32,
225 PropModeReplace, (unsigned char *) net_atom, NetLast);
229 cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
230 cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
231 cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
235 brush.drawable = XCreatePixmap(dpy, root, rect.width, rect.height,
236 DefaultDepth(dpy, screen));
237 brush.gc = XCreateGC(dpy, root, 0, 0);
240 loadcolors(dpy, screen, &brush, BGCOLOR, FGCOLOR, BORDERCOLOR);
241 loadfont(dpy, &brush.font, FONT);
243 wa.override_redirect = 1;
244 wa.background_pixmap = ParentRelative;
245 wa.event_mask = ExposureMask;
248 barrect.height = labelheight(&brush.font);
249 barrect.y = rect.height - barrect.height;
250 barwin = XCreateWindow(dpy, root, barrect.x, barrect.y,
251 barrect.width, barrect.height, 0, DefaultDepth(dpy, screen),
252 CopyFromParent, DefaultVisual(dpy, screen),
253 CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
255 XDefineCursor(dpy, barwin, cursor[CurNormal]);
256 XMapRaised(dpy, barwin);
259 wa.event_mask = SubstructureRedirectMask | EnterWindowMask \
261 wa.cursor = cursor[CurNormal];
262 XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
267 XNextEvent(dpy, &ev);
269 (handler[ev.type]) (&ev); /* call handler */