aewl

annotate wm.c @ 10:703255003abb

changed how manage client works
author Anselm R. Garbe <garbeam@wmii.de>
date Tue, 11 Jul 2006 13:02:22 +0200
parents d567f430a81d
children 5cc5e55a132d
rev   line source
garbeam@0 1 /*
garbeam@0 2 * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
garbeam@0 3 * See LICENSE file for license details.
garbeam@0 4 */
garbeam@0 5
garbeam@0 6 #include <stdarg.h>
garbeam@0 7 #include <stdio.h>
garbeam@0 8 #include <stdlib.h>
garbeam@0 9
garbeam@0 10 #include <X11/cursorfont.h>
garbeam@0 11 #include <X11/Xatom.h>
garbeam@0 12 #include <X11/Xproto.h>
garbeam@0 13
garbeam@0 14 #include "wm.h"
garbeam@0 15
garbeam@3 16 /* X structs */
garbeam@0 17 Display *dpy;
garbeam@5 18 Window root, barwin;
garbeam@9 19 Atom net_atom[NetLast];
garbeam@0 20 Cursor cursor[CurLast];
garbeam@5 21 XRectangle rect, barrect;
garbeam@5 22 Bool running = True;
garbeam@3 23
garbeam@7 24 char *bartext, tag[256];
garbeam@3 25 int screen, sel_screen;
garbeam@3 26
garbeam@3 27 Brush brush = {0};
garbeam@10 28 Client *clients = NULL;
garbeam@0 29
garbeam@0 30 enum { WM_PROTOCOL_DELWIN = 1 };
garbeam@0 31
garbeam@0 32 static Bool other_wm_running;
garbeam@10 33 static char version[] = "gridwm - " VERSION ", (C)opyright MMVI Anselm R. Garbe\n";
garbeam@0 34 static int (*x_error_handler) (Display *, XErrorEvent *);
garbeam@0 35
garbeam@0 36 static void
garbeam@0 37 usage()
garbeam@0 38 {
garbeam@0 39 fputs("usage: gridwm [-v]\n", stderr);
garbeam@0 40 exit(1);
garbeam@0 41 }
garbeam@0 42
garbeam@0 43 static void
garbeam@0 44 scan_wins()
garbeam@0 45 {
garbeam@0 46 unsigned int i, num;
garbeam@0 47 Window *wins;
garbeam@0 48 XWindowAttributes wa;
garbeam@0 49 Window d1, d2;
garbeam@0 50
garbeam@0 51 if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
garbeam@0 52 for(i = 0; i < num; i++) {
garbeam@0 53 if(!XGetWindowAttributes(dpy, wins[i], &wa))
garbeam@0 54 continue;
garbeam@0 55 if(wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
garbeam@0 56 continue;
garbeam@0 57 if(wa.map_state == IsViewable)
garbeam@10 58 manage(wins[i], &wa);
garbeam@0 59 }
garbeam@0 60 }
garbeam@0 61 if(wins)
garbeam@0 62 XFree(wins);
garbeam@0 63 }
garbeam@0 64
garbeam@0 65 /*
garbeam@0 66 * There's no way to check accesses to destroyed windows, thus
garbeam@0 67 * those cases are ignored (especially on UnmapNotify's).
garbeam@0 68 * Other types of errors call Xlib's default error handler, which
garbeam@0 69 * calls exit().
garbeam@0 70 */
garbeam@10 71 int
garbeam@0 72 error_handler(Display *dpy, XErrorEvent *error)
garbeam@0 73 {
garbeam@0 74 if(error->error_code == BadWindow
garbeam@0 75 || (error->request_code == X_SetInputFocus
garbeam@0 76 && error->error_code == BadMatch)
garbeam@0 77 || (error->request_code == X_PolyText8
garbeam@0 78 && error->error_code == BadDrawable)
garbeam@0 79 || (error->request_code == X_PolyFillRectangle
garbeam@0 80 && error->error_code == BadDrawable)
garbeam@0 81 || (error->request_code == X_PolySegment
garbeam@0 82 && error->error_code == BadDrawable)
garbeam@0 83 || (error->request_code == X_ConfigureWindow
garbeam@0 84 && error->error_code == BadMatch)
garbeam@0 85 || (error->request_code == X_GrabKey
garbeam@0 86 && error->error_code == BadAccess))
garbeam@0 87 return 0;
garbeam@0 88 fprintf(stderr, "gridwm: fatal error: request code=%d, error code=%d\n",
garbeam@0 89 error->request_code, error->error_code);
garbeam@0 90 return x_error_handler(dpy, error); /* may call exit() */
garbeam@0 91 }
garbeam@0 92
garbeam@0 93 /*
garbeam@0 94 * Startup Error handler to check if another window manager
garbeam@0 95 * is already running.
garbeam@0 96 */
garbeam@0 97 static int
garbeam@0 98 startup_error_handler(Display *dpy, XErrorEvent *error)
garbeam@0 99 {
garbeam@0 100 other_wm_running = True;
garbeam@0 101 return -1;
garbeam@0 102 }
garbeam@0 103
garbeam@0 104 static void
garbeam@0 105 cleanup()
garbeam@0 106 {
garbeam@0 107 /*
garbeam@0 108 Client *c;
garbeam@0 109 for(c=client; c; c=c->next)
garbeam@0 110 reparent_client(c, root, c->sel->rect.x, c->sel->rect.y);
garbeam@0 111 XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
garbeam@0 112 */
garbeam@0 113 }
garbeam@0 114
garbeam@0 115 int
garbeam@0 116 main(int argc, char *argv[])
garbeam@0 117 {
garbeam@0 118 int i;
garbeam@0 119 XSetWindowAttributes wa;
garbeam@0 120 unsigned int mask;
garbeam@0 121 Window w;
garbeam@5 122 XEvent ev;
garbeam@0 123
garbeam@0 124 /* command line args */
garbeam@0 125 for(i = 1; (i < argc) && (argv[i][0] == '-'); i++) {
garbeam@0 126 switch (argv[i][1]) {
garbeam@0 127 case 'v':
garbeam@0 128 fprintf(stdout, "%s", version);
garbeam@0 129 exit(0);
garbeam@0 130 break;
garbeam@0 131 default:
garbeam@0 132 usage();
garbeam@0 133 break;
garbeam@0 134 }
garbeam@0 135 }
garbeam@0 136
garbeam@0 137 dpy = XOpenDisplay(0);
garbeam@0 138 if(!dpy)
garbeam@0 139 error("gridwm: cannot connect X server\n");
garbeam@0 140
garbeam@0 141 screen = DefaultScreen(dpy);
garbeam@0 142 root = RootWindow(dpy, screen);
garbeam@0 143
garbeam@0 144 /* check if another WM is already running */
garbeam@0 145 other_wm_running = False;
garbeam@0 146 XSetErrorHandler(startup_error_handler);
garbeam@0 147 /* this causes an error if some other WM is running */
garbeam@0 148 XSelectInput(dpy, root, SubstructureRedirectMask);
garbeam@3 149 XFlush(dpy);
garbeam@0 150
garbeam@0 151 if(other_wm_running)
garbeam@0 152 error("gridwm: another window manager is already running\n");
garbeam@0 153
garbeam@0 154 rect.x = rect.y = 0;
garbeam@0 155 rect.width = DisplayWidth(dpy, screen);
garbeam@0 156 rect.height = DisplayHeight(dpy, screen);
garbeam@0 157 sel_screen = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask);
garbeam@0 158
garbeam@0 159 XSetErrorHandler(0);
garbeam@0 160 x_error_handler = XSetErrorHandler(error_handler);
garbeam@0 161
garbeam@0 162 /* init atoms */
garbeam@0 163 net_atom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
garbeam@0 164 net_atom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
garbeam@0 165
garbeam@0 166 XChangeProperty(dpy, root, net_atom[NetSupported], XA_ATOM, 32,
garbeam@0 167 PropModeReplace, (unsigned char *) net_atom, NetLast);
garbeam@0 168
garbeam@0 169
garbeam@0 170 /* init cursors */
garbeam@0 171 cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
garbeam@0 172 cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
garbeam@0 173 cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
garbeam@0 174
garbeam@8 175 update_keys();
garbeam@0 176
garbeam@5 177 brush.drawable = XCreatePixmap(dpy, root, rect.width, rect.height,
garbeam@0 178 DefaultDepth(dpy, screen));
garbeam@5 179 brush.gc = XCreateGC(dpy, root, 0, 0);
garbeam@5 180
garbeam@5 181 /* style */
garbeam@5 182 loadcolors(dpy, screen, &brush, BGCOLOR, FGCOLOR, BORDERCOLOR);
garbeam@5 183 loadfont(dpy, &brush.font, FONT);
garbeam@5 184
garbeam@5 185 wa.override_redirect = 1;
garbeam@5 186 wa.background_pixmap = ParentRelative;
garbeam@5 187 wa.event_mask = ExposureMask;
garbeam@5 188
garbeam@5 189 barrect = rect;
garbeam@5 190 barrect.height = labelheight(&brush.font);
garbeam@5 191 barrect.y = rect.height - barrect.height;
garbeam@5 192 barwin = XCreateWindow(dpy, root, barrect.x, barrect.y,
garbeam@5 193 barrect.width, barrect.height, 0, DefaultDepth(dpy, screen),
garbeam@5 194 CopyFromParent, DefaultVisual(dpy, screen),
garbeam@5 195 CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
garbeam@7 196 bartext = NULL;
garbeam@5 197 XDefineCursor(dpy, barwin, cursor[CurNormal]);
garbeam@5 198 XMapRaised(dpy, barwin);
garbeam@5 199 draw_bar();
garbeam@0 200
garbeam@9 201 wa.event_mask = SubstructureRedirectMask | EnterWindowMask \
garbeam@9 202 | LeaveWindowMask;
garbeam@0 203 wa.cursor = cursor[CurNormal];
garbeam@0 204 XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
garbeam@0 205
garbeam@5 206 scan_wins();
garbeam@3 207
garbeam@5 208 while(running) {
garbeam@5 209 XNextEvent(dpy, &ev);
garbeam@5 210 if(handler[ev.type])
garbeam@5 211 (handler[ev.type]) (&ev); /* call handler */
garbeam@5 212 }
garbeam@0 213
garbeam@0 214 cleanup();
garbeam@0 215 XCloseDisplay(dpy);
garbeam@0 216
garbeam@0 217 return 0;
garbeam@0 218 }