garbeam@0: /* garbeam@0: * (C)opyright MMVI Anselm R. Garbe garbeam@0: * See LICENSE file for license details. garbeam@0: */ garbeam@0: garbeam@0: #include garbeam@0: #include garbeam@0: #include garbeam@0: garbeam@0: #include garbeam@0: #include garbeam@0: #include garbeam@0: garbeam@0: #include "wm.h" garbeam@0: garbeam@31: /********** CUSTOMIZE **********/ garbeam@31: garbeam@31: char *tags[TLast] = { garbeam@31: [Tscratch] = "scratch", garbeam@31: [Tdev] = "dev", garbeam@31: [Tirc] = "irc", garbeam@31: [Twww] = "www", garbeam@31: [Twork] = "work", garbeam@31: }; garbeam@31: garbeam@31: /********** CUSTOMIZE **********/ garbeam@31: 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: Bool running = True; garbeam@31: Bool issel; garbeam@3: garbeam@31: char stext[1024]; garbeam@31: int tsel = Tdev; /* default tag */ garbeam@32: int screen, sx, sy, sw, sh, th; 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@31: static const char version[] = garbeam@31: "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@31: usage() { error("usage: gridwm [-v]\n"); } 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@30: unsigned char *protocols; garbeam@13: long res; garbeam@13: int protos = 0; garbeam@13: int i; garbeam@13: garbeam@30: res = win_property(w, wm_atom[WMProtocols], XA_ATOM, 20L, &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@27: void garbeam@27: run(void *aux) garbeam@27: { garbeam@27: spawn(dpy, aux); garbeam@27: } garbeam@27: garbeam@27: void garbeam@27: quit(void *aux) garbeam@27: { garbeam@27: running = False; garbeam@27: } garbeam@27: 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@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@26: sx = sy = 0; garbeam@26: sw = DisplayWidth(dpy, screen); garbeam@26: sh = DisplayHeight(dpy, screen); garbeam@31: issel = 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: /* style */ garbeam@5: loadcolors(dpy, screen, &brush, BGCOLOR, FGCOLOR, BORDERCOLOR); garbeam@5: loadfont(dpy, &brush.font, FONT); garbeam@5: garbeam@32: th = texth(&brush.font); garbeam@5: garbeam@32: brush.drawable = XCreatePixmap(dpy, root, sw, th, DefaultDepth(dpy, screen)); garbeam@21: brush.gc = XCreateGC(dpy, root, 0, 0); garbeam@21: 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@31: arrange = grid; garbeam@5: scan_wins(); garbeam@3: garbeam@5: while(running) { garbeam@32: XNextEvent(dpy, &ev); garbeam@32: if(handler[ev.type]) garbeam@32: (handler[ev.type])(&ev); /* call handler */ garbeam@5: } garbeam@0: garbeam@0: cleanup(); garbeam@0: XCloseDisplay(dpy); garbeam@0: garbeam@0: return 0; garbeam@0: }