aewl

annotate wm.c @ 7:49e2fc9fb94f

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