garbeam@0: /* garbeam@0: * (C)opyright MMVI Anselm R. Garbe garbeam@0: * See LICENSE file for license details. garbeam@0: */ garbeam@0: garbeam@14: #include garbeam@14: garbeam@0: #include garbeam@0: #include garbeam@0: #include garbeam@0: garbeam@14: #include garbeam@14: #include garbeam@14: garbeam@0: #include garbeam@0: #include garbeam@0: #include garbeam@0: garbeam@0: #include "wm.h" garbeam@0: garbeam@3: /* X structs */ garbeam@0: Display *dpy; garbeam@5: Window root, barwin; garbeam@13: Atom wm_atom[WMLast], net_atom[NetLast]; garbeam@0: Cursor cursor[CurLast]; garbeam@5: XRectangle rect, barrect; garbeam@5: Bool running = True; garbeam@13: Bool sel_screen; garbeam@3: garbeam@7: char *bartext, tag[256]; garbeam@13: int screen; garbeam@3: garbeam@3: Brush brush = {0}; garbeam@10: Client *clients = NULL; garbeam@13: Client *stack = NULL; garbeam@0: garbeam@0: static Bool other_wm_running; garbeam@10: static char version[] = "gridwm - " VERSION ", (C)opyright MMVI Anselm R. Garbe\n"; garbeam@0: static int (*x_error_handler) (Display *, XErrorEvent *); garbeam@0: garbeam@0: static void garbeam@0: usage() garbeam@0: { garbeam@0: fputs("usage: gridwm [-v]\n", stderr); garbeam@0: exit(1); garbeam@0: } garbeam@0: garbeam@0: static void garbeam@0: scan_wins() garbeam@0: { garbeam@0: unsigned int i, num; garbeam@0: Window *wins; garbeam@0: XWindowAttributes wa; garbeam@0: Window d1, d2; garbeam@0: garbeam@0: if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) { garbeam@0: for(i = 0; i < num; i++) { garbeam@0: if(!XGetWindowAttributes(dpy, wins[i], &wa)) garbeam@0: continue; garbeam@0: if(wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1)) garbeam@0: continue; garbeam@0: if(wa.map_state == IsViewable) garbeam@10: manage(wins[i], &wa); garbeam@0: } garbeam@0: } garbeam@0: if(wins) garbeam@0: XFree(wins); garbeam@0: } garbeam@0: garbeam@13: static int garbeam@13: win_property(Window w, Atom a, Atom t, long l, unsigned char **prop) garbeam@13: { garbeam@13: Atom real; garbeam@13: int format; garbeam@13: unsigned long res, extra; garbeam@13: int status; garbeam@13: garbeam@13: status = XGetWindowProperty(dpy, w, a, 0L, l, False, t, &real, &format, garbeam@13: &res, &extra, prop); garbeam@13: garbeam@13: if(status != Success || *prop == 0) { garbeam@13: return 0; garbeam@13: } garbeam@13: if(res == 0) { garbeam@13: free((void *) *prop); garbeam@13: } garbeam@13: return res; garbeam@13: } garbeam@13: garbeam@13: int garbeam@13: win_proto(Window w) garbeam@13: { garbeam@13: Atom *protocols; garbeam@13: long res; garbeam@13: int protos = 0; garbeam@13: int i; garbeam@13: garbeam@13: res = win_property(w, wm_atom[WMProtocols], XA_ATOM, 20L, garbeam@13: ((unsigned char **) &protocols)); garbeam@13: if(res <= 0) { garbeam@13: return protos; garbeam@13: } garbeam@13: for(i = 0; i < res; i++) { garbeam@13: if(protocols[i] == wm_atom[WMDelete]) garbeam@13: protos |= WM_PROTOCOL_DELWIN; garbeam@13: } garbeam@13: free((char *) protocols); garbeam@13: return protos; garbeam@13: } garbeam@13: garbeam@13: void garbeam@13: send_message(Window w, Atom a, long value) garbeam@13: { garbeam@13: XEvent e; garbeam@13: garbeam@13: e.type = ClientMessage; garbeam@13: e.xclient.window = w; garbeam@13: e.xclient.message_type = a; garbeam@13: e.xclient.format = 32; garbeam@13: e.xclient.data.l[0] = value; garbeam@13: e.xclient.data.l[1] = CurrentTime; garbeam@13: XSendEvent(dpy, w, False, NoEventMask, &e); garbeam@13: XFlush(dpy); garbeam@13: } garbeam@13: garbeam@0: /* garbeam@0: * There's no way to check accesses to destroyed windows, thus garbeam@0: * those cases are ignored (especially on UnmapNotify's). garbeam@0: * Other types of errors call Xlib's default error handler, which garbeam@0: * calls exit(). garbeam@0: */ garbeam@10: int garbeam@0: error_handler(Display *dpy, XErrorEvent *error) garbeam@0: { garbeam@0: if(error->error_code == BadWindow garbeam@0: || (error->request_code == X_SetInputFocus garbeam@0: && error->error_code == BadMatch) garbeam@0: || (error->request_code == X_PolyText8 garbeam@0: && error->error_code == BadDrawable) garbeam@0: || (error->request_code == X_PolyFillRectangle garbeam@0: && error->error_code == BadDrawable) garbeam@0: || (error->request_code == X_PolySegment garbeam@0: && error->error_code == BadDrawable) garbeam@0: || (error->request_code == X_ConfigureWindow garbeam@0: && error->error_code == BadMatch) garbeam@0: || (error->request_code == X_GrabKey garbeam@0: && error->error_code == BadAccess)) garbeam@0: return 0; garbeam@0: fprintf(stderr, "gridwm: fatal error: request code=%d, error code=%d\n", garbeam@0: error->request_code, error->error_code); garbeam@0: return x_error_handler(dpy, error); /* may call exit() */ garbeam@0: } garbeam@0: garbeam@0: /* garbeam@0: * Startup Error handler to check if another window manager garbeam@0: * is already running. garbeam@0: */ garbeam@0: static int garbeam@0: startup_error_handler(Display *dpy, XErrorEvent *error) garbeam@0: { garbeam@0: other_wm_running = True; garbeam@0: return -1; garbeam@0: } garbeam@0: garbeam@0: static void garbeam@0: cleanup() garbeam@0: { garbeam@14: while(clients) garbeam@14: unmanage(clients); garbeam@0: XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime); garbeam@0: } garbeam@0: garbeam@0: int garbeam@0: main(int argc, char *argv[]) garbeam@0: { garbeam@0: int i; garbeam@0: XSetWindowAttributes wa; garbeam@0: unsigned int mask; garbeam@0: Window w; garbeam@5: XEvent ev; garbeam@14: fd_set fds; garbeam@14: struct timeval t, timeout = { garbeam@14: .tv_usec = 0, garbeam@14: .tv_sec = STATUSDELAY, garbeam@14: }; garbeam@0: garbeam@0: /* command line args */ garbeam@0: for(i = 1; (i < argc) && (argv[i][0] == '-'); i++) { garbeam@0: switch (argv[i][1]) { garbeam@0: case 'v': garbeam@0: fprintf(stdout, "%s", version); garbeam@0: exit(0); garbeam@0: break; garbeam@0: default: garbeam@0: usage(); garbeam@0: break; garbeam@0: } garbeam@0: } garbeam@0: garbeam@0: dpy = XOpenDisplay(0); garbeam@0: if(!dpy) garbeam@0: error("gridwm: cannot connect X server\n"); garbeam@0: garbeam@0: screen = DefaultScreen(dpy); garbeam@0: root = RootWindow(dpy, screen); garbeam@0: garbeam@0: /* check if another WM is already running */ garbeam@0: other_wm_running = False; garbeam@0: XSetErrorHandler(startup_error_handler); garbeam@0: /* this causes an error if some other WM is running */ garbeam@0: XSelectInput(dpy, root, SubstructureRedirectMask); garbeam@3: XFlush(dpy); garbeam@0: garbeam@0: if(other_wm_running) garbeam@0: error("gridwm: another window manager is already running\n"); garbeam@0: garbeam@0: rect.x = rect.y = 0; garbeam@0: rect.width = DisplayWidth(dpy, screen); garbeam@0: rect.height = DisplayHeight(dpy, screen); garbeam@0: sel_screen = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask); garbeam@0: garbeam@0: XSetErrorHandler(0); garbeam@0: x_error_handler = XSetErrorHandler(error_handler); garbeam@0: garbeam@0: /* init atoms */ garbeam@13: wm_atom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False); garbeam@13: wm_atom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False); garbeam@0: net_atom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False); garbeam@0: net_atom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False); garbeam@0: garbeam@0: XChangeProperty(dpy, root, net_atom[NetSupported], XA_ATOM, 32, garbeam@0: PropModeReplace, (unsigned char *) net_atom, NetLast); garbeam@0: garbeam@0: garbeam@0: /* init cursors */ garbeam@0: cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr); garbeam@0: cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing); garbeam@0: cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur); garbeam@0: garbeam@8: update_keys(); garbeam@0: garbeam@5: brush.drawable = XCreatePixmap(dpy, root, rect.width, rect.height, garbeam@0: DefaultDepth(dpy, screen)); garbeam@5: brush.gc = XCreateGC(dpy, root, 0, 0); garbeam@5: garbeam@5: /* style */ garbeam@5: loadcolors(dpy, screen, &brush, BGCOLOR, FGCOLOR, BORDERCOLOR); garbeam@5: loadfont(dpy, &brush.font, FONT); garbeam@5: garbeam@5: wa.override_redirect = 1; garbeam@5: wa.background_pixmap = ParentRelative; garbeam@5: wa.event_mask = ExposureMask; garbeam@5: garbeam@5: barrect = rect; garbeam@5: barrect.height = labelheight(&brush.font); garbeam@5: barrect.y = rect.height - barrect.height; garbeam@5: barwin = XCreateWindow(dpy, root, barrect.x, barrect.y, garbeam@5: barrect.width, barrect.height, 0, DefaultDepth(dpy, screen), garbeam@5: CopyFromParent, DefaultVisual(dpy, screen), garbeam@5: CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa); garbeam@7: bartext = NULL; garbeam@5: XDefineCursor(dpy, barwin, cursor[CurNormal]); garbeam@5: XMapRaised(dpy, barwin); garbeam@5: draw_bar(); garbeam@0: garbeam@9: wa.event_mask = SubstructureRedirectMask | EnterWindowMask \ garbeam@9: | LeaveWindowMask; garbeam@0: wa.cursor = cursor[CurNormal]; garbeam@0: XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa); garbeam@0: garbeam@5: scan_wins(); garbeam@3: garbeam@5: while(running) { garbeam@14: if(XPending(dpy) > 0) { garbeam@14: XNextEvent(dpy, &ev); garbeam@14: if(handler[ev.type]) garbeam@14: (handler[ev.type]) (&ev); /* call handler */ garbeam@14: continue; garbeam@14: } garbeam@14: FD_ZERO(&fds); garbeam@14: FD_SET(ConnectionNumber(dpy), &fds); garbeam@14: t = timeout; garbeam@14: if(select(ConnectionNumber(dpy) + 1, &fds, NULL, NULL, &t) > 0) garbeam@14: continue; garbeam@14: else if(errno != EINTR) garbeam@14: draw_bar(); garbeam@5: } garbeam@0: garbeam@0: cleanup(); garbeam@0: XCloseDisplay(dpy); garbeam@0: garbeam@0: return 0; garbeam@0: }