aewl

annotate wm.c @ 1:f10194d4b76d

added gridmenu
author Anselm R. Garbe <garbeam@wmii.de>
date Mon, 10 Jul 2006 16:49:43 +0200
parents
children a79188fe4a40
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@0 16 Display *dpy;
garbeam@0 17 Window root;
garbeam@0 18 XRectangle rect;
garbeam@0 19 int screen, sel_screen;
garbeam@0 20 Atom wm_atom[WMLast];
garbeam@0 21 Atom net_atom[NetLast];
garbeam@0 22 Cursor cursor[CurLast];
garbeam@0 23 unsigned int kmask, numlock_mask;
garbeam@0 24 Pixmap pmap;
garbeam@0 25
garbeam@0 26 enum { WM_PROTOCOL_DELWIN = 1 };
garbeam@0 27
garbeam@0 28 static Bool other_wm_running;
garbeam@0 29 static int (*x_error_handler) (Display *, XErrorEvent *);
garbeam@0 30 static char version[] = "gridwm - " VERSION ", (C)opyright MMVI Anselm R. Garbe\n";
garbeam@0 31
garbeam@0 32 static void
garbeam@0 33 usage()
garbeam@0 34 {
garbeam@0 35 fputs("usage: gridwm [-v]\n", stderr);
garbeam@0 36 exit(1);
garbeam@0 37 }
garbeam@0 38
garbeam@0 39 void
garbeam@0 40 error(char *errstr, ...) {
garbeam@0 41 va_list ap;
garbeam@0 42 va_start(ap, errstr);
garbeam@0 43 vfprintf(stderr, errstr, ap);
garbeam@0 44 va_end(ap);
garbeam@0 45 exit(1);
garbeam@0 46 }
garbeam@0 47
garbeam@0 48 static void
garbeam@0 49 scan_wins()
garbeam@0 50 {
garbeam@0 51 unsigned int i, num;
garbeam@0 52 Window *wins;
garbeam@0 53 XWindowAttributes wa;
garbeam@0 54 Window d1, d2;
garbeam@0 55
garbeam@0 56 if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
garbeam@0 57 for(i = 0; i < num; i++) {
garbeam@0 58 if(!XGetWindowAttributes(dpy, wins[i], &wa))
garbeam@0 59 continue;
garbeam@0 60 if(wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
garbeam@0 61 continue;
garbeam@0 62 if(wa.map_state == IsViewable)
garbeam@0 63 /*manage*/;
garbeam@0 64 }
garbeam@0 65 }
garbeam@0 66 if(wins)
garbeam@0 67 XFree(wins);
garbeam@0 68 }
garbeam@0 69
garbeam@0 70 static int
garbeam@0 71 win_property(Window w, Atom a, Atom t, long l, unsigned char **prop)
garbeam@0 72 {
garbeam@0 73 Atom real;
garbeam@0 74 int format;
garbeam@0 75 unsigned long res, extra;
garbeam@0 76 int status;
garbeam@0 77
garbeam@0 78 status = XGetWindowProperty(dpy, w, a, 0L, l, False, t, &real, &format,
garbeam@0 79 &res, &extra, prop);
garbeam@0 80
garbeam@0 81 if(status != Success || *prop == 0) {
garbeam@0 82 return 0;
garbeam@0 83 }
garbeam@0 84 if(res == 0) {
garbeam@0 85 free((void *) *prop);
garbeam@0 86 }
garbeam@0 87 return res;
garbeam@0 88 }
garbeam@0 89
garbeam@0 90 int
garbeam@0 91 win_proto(Window w)
garbeam@0 92 {
garbeam@0 93 Atom *protocols;
garbeam@0 94 long res;
garbeam@0 95 int protos = 0;
garbeam@0 96 int i;
garbeam@0 97
garbeam@0 98 res = win_property(w, wm_atom[WMProtocols], XA_ATOM, 20L,
garbeam@0 99 ((unsigned char **) &protocols));
garbeam@0 100 if(res <= 0) {
garbeam@0 101 return protos;
garbeam@0 102 }
garbeam@0 103 for(i = 0; i < res; i++) {
garbeam@0 104 if(protocols[i] == wm_atom[WMDelete])
garbeam@0 105 protos |= WM_PROTOCOL_DELWIN;
garbeam@0 106 }
garbeam@0 107 free((char *) protocols);
garbeam@0 108 return protos;
garbeam@0 109 }
garbeam@0 110
garbeam@0 111 /*
garbeam@0 112 * There's no way to check accesses to destroyed windows, thus
garbeam@0 113 * those cases are ignored (especially on UnmapNotify's).
garbeam@0 114 * Other types of errors call Xlib's default error handler, which
garbeam@0 115 * calls exit().
garbeam@0 116 */
garbeam@0 117 static int
garbeam@0 118 error_handler(Display *dpy, XErrorEvent *error)
garbeam@0 119 {
garbeam@0 120 if(error->error_code == BadWindow
garbeam@0 121 || (error->request_code == X_SetInputFocus
garbeam@0 122 && error->error_code == BadMatch)
garbeam@0 123 || (error->request_code == X_PolyText8
garbeam@0 124 && error->error_code == BadDrawable)
garbeam@0 125 || (error->request_code == X_PolyFillRectangle
garbeam@0 126 && error->error_code == BadDrawable)
garbeam@0 127 || (error->request_code == X_PolySegment
garbeam@0 128 && error->error_code == BadDrawable)
garbeam@0 129 || (error->request_code == X_ConfigureWindow
garbeam@0 130 && error->error_code == BadMatch)
garbeam@0 131 || (error->request_code == X_GrabKey
garbeam@0 132 && error->error_code == BadAccess))
garbeam@0 133 return 0;
garbeam@0 134 fprintf(stderr, "gridwm: fatal error: request code=%d, error code=%d\n",
garbeam@0 135 error->request_code, error->error_code);
garbeam@0 136 return x_error_handler(dpy, error); /* may call exit() */
garbeam@0 137 }
garbeam@0 138
garbeam@0 139 /*
garbeam@0 140 * Startup Error handler to check if another window manager
garbeam@0 141 * is already running.
garbeam@0 142 */
garbeam@0 143 static int
garbeam@0 144 startup_error_handler(Display *dpy, XErrorEvent *error)
garbeam@0 145 {
garbeam@0 146 other_wm_running = True;
garbeam@0 147 return -1;
garbeam@0 148 }
garbeam@0 149
garbeam@0 150 static void
garbeam@0 151 init_lock_keys()
garbeam@0 152 {
garbeam@0 153 XModifierKeymap *modmap;
garbeam@0 154 KeyCode numlock;
garbeam@0 155 int i;
garbeam@0 156 static int masks[] = {
garbeam@0 157 ShiftMask, LockMask, ControlMask, Mod1Mask,
garbeam@0 158 Mod2Mask, Mod3Mask, Mod4Mask, Mod5Mask
garbeam@0 159 };
garbeam@0 160
garbeam@0 161 numlock_mask = 0;
garbeam@0 162 modmap = XGetModifierMapping(dpy);
garbeam@0 163 numlock = XKeysymToKeycode(dpy, XStringToKeysym("Num_Lock"));
garbeam@0 164
garbeam@0 165 if(modmap && modmap->max_keypermod > 0) {
garbeam@0 166 int max = (sizeof(masks) / sizeof(int)) * modmap->max_keypermod;
garbeam@0 167 for(i = 0; i < max; i++)
garbeam@0 168 if(numlock && (modmap->modifiermap[i] == numlock))
garbeam@0 169 numlock_mask = masks[i / modmap->max_keypermod];
garbeam@0 170 }
garbeam@0 171 XFreeModifiermap(modmap);
garbeam@0 172
garbeam@0 173 kmask = 255 & ~(numlock_mask | LockMask);
garbeam@0 174 }
garbeam@0 175
garbeam@0 176 static void
garbeam@0 177 cleanup()
garbeam@0 178 {
garbeam@0 179 /*
garbeam@0 180 Client *c;
garbeam@0 181 for(c=client; c; c=c->next)
garbeam@0 182 reparent_client(c, root, c->sel->rect.x, c->sel->rect.y);
garbeam@0 183 XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
garbeam@0 184 */
garbeam@0 185 }
garbeam@0 186
garbeam@0 187 int
garbeam@0 188 main(int argc, char *argv[])
garbeam@0 189 {
garbeam@0 190 int i;
garbeam@0 191 XSetWindowAttributes wa;
garbeam@0 192 unsigned int mask;
garbeam@0 193 Window w;
garbeam@0 194
garbeam@0 195 /* command line args */
garbeam@0 196 for(i = 1; (i < argc) && (argv[i][0] == '-'); i++) {
garbeam@0 197 switch (argv[i][1]) {
garbeam@0 198 case 'v':
garbeam@0 199 fprintf(stdout, "%s", version);
garbeam@0 200 exit(0);
garbeam@0 201 break;
garbeam@0 202 default:
garbeam@0 203 usage();
garbeam@0 204 break;
garbeam@0 205 }
garbeam@0 206 }
garbeam@0 207
garbeam@0 208 dpy = XOpenDisplay(0);
garbeam@0 209 if(!dpy)
garbeam@0 210 error("gridwm: cannot connect X server\n");
garbeam@0 211
garbeam@0 212 screen = DefaultScreen(dpy);
garbeam@0 213 root = RootWindow(dpy, screen);
garbeam@0 214
garbeam@0 215 /* check if another WM is already running */
garbeam@0 216 other_wm_running = False;
garbeam@0 217 XSetErrorHandler(startup_error_handler);
garbeam@0 218 /* this causes an error if some other WM is running */
garbeam@0 219 XSelectInput(dpy, root, SubstructureRedirectMask);
garbeam@0 220 XSync(dpy, False);
garbeam@0 221
garbeam@0 222 if(other_wm_running)
garbeam@0 223 error("gridwm: another window manager is already running\n");
garbeam@0 224
garbeam@0 225 rect.x = rect.y = 0;
garbeam@0 226 rect.width = DisplayWidth(dpy, screen);
garbeam@0 227 rect.height = DisplayHeight(dpy, screen);
garbeam@0 228 sel_screen = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask);
garbeam@0 229
garbeam@0 230 XSetErrorHandler(0);
garbeam@0 231 x_error_handler = XSetErrorHandler(error_handler);
garbeam@0 232
garbeam@0 233 /* init atoms */
garbeam@0 234 wm_atom[WMState] = XInternAtom(dpy, "WM_STATE", False);
garbeam@0 235 wm_atom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
garbeam@0 236 wm_atom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
garbeam@0 237 net_atom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
garbeam@0 238 net_atom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
garbeam@0 239
garbeam@0 240 XChangeProperty(dpy, root, net_atom[NetSupported], XA_ATOM, 32,
garbeam@0 241 PropModeReplace, (unsigned char *) net_atom, NetLast);
garbeam@0 242
garbeam@0 243
garbeam@0 244 /* init cursors */
garbeam@0 245 cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
garbeam@0 246 cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
garbeam@0 247 cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
garbeam@0 248
garbeam@0 249 init_lock_keys();
garbeam@0 250
garbeam@0 251 pmap = XCreatePixmap(dpy, root, rect.width, rect.height,
garbeam@0 252 DefaultDepth(dpy, screen));
garbeam@0 253
garbeam@0 254 wa.event_mask = SubstructureRedirectMask | EnterWindowMask | LeaveWindowMask;
garbeam@0 255 wa.cursor = cursor[CurNormal];
garbeam@0 256 XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
garbeam@0 257
garbeam@0 258 scan_wins();
garbeam@0 259
garbeam@0 260 cleanup();
garbeam@0 261 XCloseDisplay(dpy);
garbeam@0 262
garbeam@0 263 return 0;
garbeam@0 264 }