aewl

annotate wm.c @ 13:5cc5e55a132d

added protocol killing stuff
author Anselm R. Garbe <garbeam@wmii.de>
date Tue, 11 Jul 2006 16:14:22 +0200
parents 703255003abb
children 5c078b66347b
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@13 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@13 23 Bool sel_screen;
garbeam@3 24
garbeam@7 25 char *bartext, tag[256];
garbeam@13 26 int screen;
garbeam@3 27
garbeam@3 28 Brush brush = {0};
garbeam@10 29 Client *clients = NULL;
garbeam@13 30 Client *stack = NULL;
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@13 65 static int
garbeam@13 66 win_property(Window w, Atom a, Atom t, long l, unsigned char **prop)
garbeam@13 67 {
garbeam@13 68 Atom real;
garbeam@13 69 int format;
garbeam@13 70 unsigned long res, extra;
garbeam@13 71 int status;
garbeam@13 72
garbeam@13 73 status = XGetWindowProperty(dpy, w, a, 0L, l, False, t, &real, &format,
garbeam@13 74 &res, &extra, prop);
garbeam@13 75
garbeam@13 76 if(status != Success || *prop == 0) {
garbeam@13 77 return 0;
garbeam@13 78 }
garbeam@13 79 if(res == 0) {
garbeam@13 80 free((void *) *prop);
garbeam@13 81 }
garbeam@13 82 return res;
garbeam@13 83 }
garbeam@13 84
garbeam@13 85 int
garbeam@13 86 win_proto(Window w)
garbeam@13 87 {
garbeam@13 88 Atom *protocols;
garbeam@13 89 long res;
garbeam@13 90 int protos = 0;
garbeam@13 91 int i;
garbeam@13 92
garbeam@13 93 res = win_property(w, wm_atom[WMProtocols], XA_ATOM, 20L,
garbeam@13 94 ((unsigned char **) &protocols));
garbeam@13 95 if(res <= 0) {
garbeam@13 96 return protos;
garbeam@13 97 }
garbeam@13 98 for(i = 0; i < res; i++) {
garbeam@13 99 if(protocols[i] == wm_atom[WMDelete])
garbeam@13 100 protos |= WM_PROTOCOL_DELWIN;
garbeam@13 101 }
garbeam@13 102 free((char *) protocols);
garbeam@13 103 return protos;
garbeam@13 104 }
garbeam@13 105
garbeam@13 106 void
garbeam@13 107 send_message(Window w, Atom a, long value)
garbeam@13 108 {
garbeam@13 109 XEvent e;
garbeam@13 110
garbeam@13 111 e.type = ClientMessage;
garbeam@13 112 e.xclient.window = w;
garbeam@13 113 e.xclient.message_type = a;
garbeam@13 114 e.xclient.format = 32;
garbeam@13 115 e.xclient.data.l[0] = value;
garbeam@13 116 e.xclient.data.l[1] = CurrentTime;
garbeam@13 117 XSendEvent(dpy, w, False, NoEventMask, &e);
garbeam@13 118 XFlush(dpy);
garbeam@13 119 }
garbeam@13 120
garbeam@0 121 /*
garbeam@0 122 * There's no way to check accesses to destroyed windows, thus
garbeam@0 123 * those cases are ignored (especially on UnmapNotify's).
garbeam@0 124 * Other types of errors call Xlib's default error handler, which
garbeam@0 125 * calls exit().
garbeam@0 126 */
garbeam@10 127 int
garbeam@0 128 error_handler(Display *dpy, XErrorEvent *error)
garbeam@0 129 {
garbeam@0 130 if(error->error_code == BadWindow
garbeam@0 131 || (error->request_code == X_SetInputFocus
garbeam@0 132 && error->error_code == BadMatch)
garbeam@0 133 || (error->request_code == X_PolyText8
garbeam@0 134 && error->error_code == BadDrawable)
garbeam@0 135 || (error->request_code == X_PolyFillRectangle
garbeam@0 136 && error->error_code == BadDrawable)
garbeam@0 137 || (error->request_code == X_PolySegment
garbeam@0 138 && error->error_code == BadDrawable)
garbeam@0 139 || (error->request_code == X_ConfigureWindow
garbeam@0 140 && error->error_code == BadMatch)
garbeam@0 141 || (error->request_code == X_GrabKey
garbeam@0 142 && error->error_code == BadAccess))
garbeam@0 143 return 0;
garbeam@0 144 fprintf(stderr, "gridwm: fatal error: request code=%d, error code=%d\n",
garbeam@0 145 error->request_code, error->error_code);
garbeam@0 146 return x_error_handler(dpy, error); /* may call exit() */
garbeam@0 147 }
garbeam@0 148
garbeam@0 149 /*
garbeam@0 150 * Startup Error handler to check if another window manager
garbeam@0 151 * is already running.
garbeam@0 152 */
garbeam@0 153 static int
garbeam@0 154 startup_error_handler(Display *dpy, XErrorEvent *error)
garbeam@0 155 {
garbeam@0 156 other_wm_running = True;
garbeam@0 157 return -1;
garbeam@0 158 }
garbeam@0 159
garbeam@0 160 static void
garbeam@0 161 cleanup()
garbeam@0 162 {
garbeam@0 163 /*
garbeam@0 164 Client *c;
garbeam@0 165 for(c=client; c; c=c->next)
garbeam@0 166 reparent_client(c, root, c->sel->rect.x, c->sel->rect.y);
garbeam@0 167 XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
garbeam@0 168 */
garbeam@0 169 }
garbeam@0 170
garbeam@0 171 int
garbeam@0 172 main(int argc, char *argv[])
garbeam@0 173 {
garbeam@0 174 int i;
garbeam@0 175 XSetWindowAttributes wa;
garbeam@0 176 unsigned int mask;
garbeam@0 177 Window w;
garbeam@5 178 XEvent ev;
garbeam@0 179
garbeam@0 180 /* command line args */
garbeam@0 181 for(i = 1; (i < argc) && (argv[i][0] == '-'); i++) {
garbeam@0 182 switch (argv[i][1]) {
garbeam@0 183 case 'v':
garbeam@0 184 fprintf(stdout, "%s", version);
garbeam@0 185 exit(0);
garbeam@0 186 break;
garbeam@0 187 default:
garbeam@0 188 usage();
garbeam@0 189 break;
garbeam@0 190 }
garbeam@0 191 }
garbeam@0 192
garbeam@0 193 dpy = XOpenDisplay(0);
garbeam@0 194 if(!dpy)
garbeam@0 195 error("gridwm: cannot connect X server\n");
garbeam@0 196
garbeam@0 197 screen = DefaultScreen(dpy);
garbeam@0 198 root = RootWindow(dpy, screen);
garbeam@0 199
garbeam@0 200 /* check if another WM is already running */
garbeam@0 201 other_wm_running = False;
garbeam@0 202 XSetErrorHandler(startup_error_handler);
garbeam@0 203 /* this causes an error if some other WM is running */
garbeam@0 204 XSelectInput(dpy, root, SubstructureRedirectMask);
garbeam@3 205 XFlush(dpy);
garbeam@0 206
garbeam@0 207 if(other_wm_running)
garbeam@0 208 error("gridwm: another window manager is already running\n");
garbeam@0 209
garbeam@0 210 rect.x = rect.y = 0;
garbeam@0 211 rect.width = DisplayWidth(dpy, screen);
garbeam@0 212 rect.height = DisplayHeight(dpy, screen);
garbeam@0 213 sel_screen = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask);
garbeam@0 214
garbeam@0 215 XSetErrorHandler(0);
garbeam@0 216 x_error_handler = XSetErrorHandler(error_handler);
garbeam@0 217
garbeam@0 218 /* init atoms */
garbeam@13 219 wm_atom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
garbeam@13 220 wm_atom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
garbeam@0 221 net_atom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
garbeam@0 222 net_atom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
garbeam@0 223
garbeam@0 224 XChangeProperty(dpy, root, net_atom[NetSupported], XA_ATOM, 32,
garbeam@0 225 PropModeReplace, (unsigned char *) net_atom, NetLast);
garbeam@0 226
garbeam@0 227
garbeam@0 228 /* init cursors */
garbeam@0 229 cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
garbeam@0 230 cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
garbeam@0 231 cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
garbeam@0 232
garbeam@8 233 update_keys();
garbeam@0 234
garbeam@5 235 brush.drawable = XCreatePixmap(dpy, root, rect.width, rect.height,
garbeam@0 236 DefaultDepth(dpy, screen));
garbeam@5 237 brush.gc = XCreateGC(dpy, root, 0, 0);
garbeam@5 238
garbeam@5 239 /* style */
garbeam@5 240 loadcolors(dpy, screen, &brush, BGCOLOR, FGCOLOR, BORDERCOLOR);
garbeam@5 241 loadfont(dpy, &brush.font, FONT);
garbeam@5 242
garbeam@5 243 wa.override_redirect = 1;
garbeam@5 244 wa.background_pixmap = ParentRelative;
garbeam@5 245 wa.event_mask = ExposureMask;
garbeam@5 246
garbeam@5 247 barrect = rect;
garbeam@5 248 barrect.height = labelheight(&brush.font);
garbeam@5 249 barrect.y = rect.height - barrect.height;
garbeam@5 250 barwin = XCreateWindow(dpy, root, barrect.x, barrect.y,
garbeam@5 251 barrect.width, barrect.height, 0, DefaultDepth(dpy, screen),
garbeam@5 252 CopyFromParent, DefaultVisual(dpy, screen),
garbeam@5 253 CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
garbeam@7 254 bartext = NULL;
garbeam@5 255 XDefineCursor(dpy, barwin, cursor[CurNormal]);
garbeam@5 256 XMapRaised(dpy, barwin);
garbeam@5 257 draw_bar();
garbeam@0 258
garbeam@9 259 wa.event_mask = SubstructureRedirectMask | EnterWindowMask \
garbeam@9 260 | LeaveWindowMask;
garbeam@0 261 wa.cursor = cursor[CurNormal];
garbeam@0 262 XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
garbeam@0 263
garbeam@5 264 scan_wins();
garbeam@3 265
garbeam@5 266 while(running) {
garbeam@5 267 XNextEvent(dpy, &ev);
garbeam@5 268 if(handler[ev.type])
garbeam@5 269 (handler[ev.type]) (&ev); /* call handler */
garbeam@5 270 }
garbeam@0 271
garbeam@0 272 cleanup();
garbeam@0 273 XCloseDisplay(dpy);
garbeam@0 274
garbeam@0 275 return 0;
garbeam@0 276 }