dwm-meillo

annotate wm.c @ 32:082c75b937b5

removed unnecessary crap
author Anselm R. Garbe <garbeam@wmii.de>
date Thu, 13 Jul 2006 01:30:55 +0200
parents 386649deb651
children e90449e03167
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@31 16 /********** CUSTOMIZE **********/
garbeam@31 17
garbeam@31 18 char *tags[TLast] = {
garbeam@31 19 [Tscratch] = "scratch",
garbeam@31 20 [Tdev] = "dev",
garbeam@31 21 [Tirc] = "irc",
garbeam@31 22 [Twww] = "www",
garbeam@31 23 [Twork] = "work",
garbeam@31 24 };
garbeam@31 25
garbeam@31 26 /********** CUSTOMIZE **********/
garbeam@31 27
garbeam@3 28 /* X structs */
garbeam@0 29 Display *dpy;
garbeam@5 30 Window root, barwin;
garbeam@13 31 Atom wm_atom[WMLast], net_atom[NetLast];
garbeam@0 32 Cursor cursor[CurLast];
garbeam@5 33 Bool running = True;
garbeam@31 34 Bool issel;
garbeam@3 35
garbeam@31 36 char stext[1024];
garbeam@31 37 int tsel = Tdev; /* default tag */
garbeam@32 38 int screen, sx, sy, sw, sh, th;
garbeam@3 39
garbeam@3 40 Brush brush = {0};
garbeam@10 41 Client *clients = NULL;
garbeam@13 42 Client *stack = NULL;
garbeam@0 43
garbeam@0 44 static Bool other_wm_running;
garbeam@31 45 static const char version[] =
garbeam@31 46 "gridwm - " VERSION ", (C)opyright MMVI Anselm R. Garbe\n";
garbeam@0 47 static int (*x_error_handler) (Display *, XErrorEvent *);
garbeam@0 48
garbeam@0 49 static void
garbeam@31 50 usage() { error("usage: gridwm [-v]\n"); }
garbeam@0 51
garbeam@0 52 static void
garbeam@0 53 scan_wins()
garbeam@0 54 {
garbeam@0 55 unsigned int i, num;
garbeam@0 56 Window *wins;
garbeam@0 57 XWindowAttributes wa;
garbeam@0 58 Window d1, d2;
garbeam@0 59
garbeam@0 60 if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
garbeam@0 61 for(i = 0; i < num; i++) {
garbeam@0 62 if(!XGetWindowAttributes(dpy, wins[i], &wa))
garbeam@0 63 continue;
garbeam@0 64 if(wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
garbeam@0 65 continue;
garbeam@0 66 if(wa.map_state == IsViewable)
garbeam@10 67 manage(wins[i], &wa);
garbeam@0 68 }
garbeam@0 69 }
garbeam@0 70 if(wins)
garbeam@0 71 XFree(wins);
garbeam@0 72 }
garbeam@0 73
garbeam@13 74 static int
garbeam@13 75 win_property(Window w, Atom a, Atom t, long l, unsigned char **prop)
garbeam@13 76 {
garbeam@13 77 Atom real;
garbeam@13 78 int format;
garbeam@13 79 unsigned long res, extra;
garbeam@13 80 int status;
garbeam@13 81
garbeam@13 82 status = XGetWindowProperty(dpy, w, a, 0L, l, False, t, &real, &format,
garbeam@13 83 &res, &extra, prop);
garbeam@13 84
garbeam@13 85 if(status != Success || *prop == 0) {
garbeam@13 86 return 0;
garbeam@13 87 }
garbeam@13 88 if(res == 0) {
garbeam@13 89 free((void *) *prop);
garbeam@13 90 }
garbeam@13 91 return res;
garbeam@13 92 }
garbeam@13 93
garbeam@13 94 int
garbeam@13 95 win_proto(Window w)
garbeam@13 96 {
garbeam@30 97 unsigned char *protocols;
garbeam@13 98 long res;
garbeam@13 99 int protos = 0;
garbeam@13 100 int i;
garbeam@13 101
garbeam@30 102 res = win_property(w, wm_atom[WMProtocols], XA_ATOM, 20L, &protocols);
garbeam@13 103 if(res <= 0) {
garbeam@13 104 return protos;
garbeam@13 105 }
garbeam@13 106 for(i = 0; i < res; i++) {
garbeam@13 107 if(protocols[i] == wm_atom[WMDelete])
garbeam@13 108 protos |= WM_PROTOCOL_DELWIN;
garbeam@13 109 }
garbeam@13 110 free((char *) protocols);
garbeam@13 111 return protos;
garbeam@13 112 }
garbeam@13 113
garbeam@13 114 void
garbeam@13 115 send_message(Window w, Atom a, long value)
garbeam@13 116 {
garbeam@13 117 XEvent e;
garbeam@13 118
garbeam@13 119 e.type = ClientMessage;
garbeam@13 120 e.xclient.window = w;
garbeam@13 121 e.xclient.message_type = a;
garbeam@13 122 e.xclient.format = 32;
garbeam@13 123 e.xclient.data.l[0] = value;
garbeam@13 124 e.xclient.data.l[1] = CurrentTime;
garbeam@13 125 XSendEvent(dpy, w, False, NoEventMask, &e);
garbeam@13 126 XFlush(dpy);
garbeam@13 127 }
garbeam@13 128
garbeam@0 129 /*
garbeam@0 130 * There's no way to check accesses to destroyed windows, thus
garbeam@0 131 * those cases are ignored (especially on UnmapNotify's).
garbeam@0 132 * Other types of errors call Xlib's default error handler, which
garbeam@0 133 * calls exit().
garbeam@0 134 */
garbeam@10 135 int
garbeam@0 136 error_handler(Display *dpy, XErrorEvent *error)
garbeam@0 137 {
garbeam@0 138 if(error->error_code == BadWindow
garbeam@0 139 || (error->request_code == X_SetInputFocus
garbeam@0 140 && error->error_code == BadMatch)
garbeam@0 141 || (error->request_code == X_PolyText8
garbeam@0 142 && error->error_code == BadDrawable)
garbeam@0 143 || (error->request_code == X_PolyFillRectangle
garbeam@0 144 && error->error_code == BadDrawable)
garbeam@0 145 || (error->request_code == X_PolySegment
garbeam@0 146 && error->error_code == BadDrawable)
garbeam@0 147 || (error->request_code == X_ConfigureWindow
garbeam@0 148 && error->error_code == BadMatch)
garbeam@0 149 || (error->request_code == X_GrabKey
garbeam@0 150 && error->error_code == BadAccess))
garbeam@0 151 return 0;
garbeam@0 152 fprintf(stderr, "gridwm: fatal error: request code=%d, error code=%d\n",
garbeam@0 153 error->request_code, error->error_code);
garbeam@0 154 return x_error_handler(dpy, error); /* may call exit() */
garbeam@0 155 }
garbeam@0 156
garbeam@0 157 /*
garbeam@0 158 * Startup Error handler to check if another window manager
garbeam@0 159 * is already running.
garbeam@0 160 */
garbeam@0 161 static int
garbeam@0 162 startup_error_handler(Display *dpy, XErrorEvent *error)
garbeam@0 163 {
garbeam@0 164 other_wm_running = True;
garbeam@0 165 return -1;
garbeam@0 166 }
garbeam@0 167
garbeam@0 168 static void
garbeam@0 169 cleanup()
garbeam@0 170 {
garbeam@14 171 while(clients)
garbeam@14 172 unmanage(clients);
garbeam@0 173 XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
garbeam@0 174 }
garbeam@0 175
garbeam@27 176 void
garbeam@27 177 run(void *aux)
garbeam@27 178 {
garbeam@27 179 spawn(dpy, aux);
garbeam@27 180 }
garbeam@27 181
garbeam@27 182 void
garbeam@27 183 quit(void *aux)
garbeam@27 184 {
garbeam@27 185 running = False;
garbeam@27 186 }
garbeam@27 187
garbeam@0 188 int
garbeam@0 189 main(int argc, char *argv[])
garbeam@0 190 {
garbeam@0 191 int i;
garbeam@0 192 XSetWindowAttributes wa;
garbeam@0 193 unsigned int mask;
garbeam@0 194 Window w;
garbeam@5 195 XEvent ev;
garbeam@0 196
garbeam@0 197 /* command line args */
garbeam@0 198 for(i = 1; (i < argc) && (argv[i][0] == '-'); i++) {
garbeam@0 199 switch (argv[i][1]) {
garbeam@0 200 case 'v':
garbeam@0 201 fprintf(stdout, "%s", version);
garbeam@0 202 exit(0);
garbeam@0 203 break;
garbeam@0 204 default:
garbeam@0 205 usage();
garbeam@0 206 break;
garbeam@0 207 }
garbeam@0 208 }
garbeam@0 209
garbeam@0 210 dpy = XOpenDisplay(0);
garbeam@0 211 if(!dpy)
garbeam@0 212 error("gridwm: cannot connect X server\n");
garbeam@0 213
garbeam@0 214 screen = DefaultScreen(dpy);
garbeam@0 215 root = RootWindow(dpy, screen);
garbeam@0 216
garbeam@0 217 /* check if another WM is already running */
garbeam@0 218 other_wm_running = False;
garbeam@0 219 XSetErrorHandler(startup_error_handler);
garbeam@0 220 /* this causes an error if some other WM is running */
garbeam@0 221 XSelectInput(dpy, root, SubstructureRedirectMask);
garbeam@3 222 XFlush(dpy);
garbeam@0 223
garbeam@0 224 if(other_wm_running)
garbeam@0 225 error("gridwm: another window manager is already running\n");
garbeam@0 226
garbeam@26 227 sx = sy = 0;
garbeam@26 228 sw = DisplayWidth(dpy, screen);
garbeam@26 229 sh = DisplayHeight(dpy, screen);
garbeam@31 230 issel = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask);
garbeam@0 231
garbeam@0 232 XSetErrorHandler(0);
garbeam@0 233 x_error_handler = XSetErrorHandler(error_handler);
garbeam@0 234
garbeam@0 235 /* init atoms */
garbeam@13 236 wm_atom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
garbeam@13 237 wm_atom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
garbeam@0 238 net_atom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
garbeam@0 239 net_atom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
garbeam@0 240
garbeam@0 241 XChangeProperty(dpy, root, net_atom[NetSupported], XA_ATOM, 32,
garbeam@0 242 PropModeReplace, (unsigned char *) net_atom, NetLast);
garbeam@0 243
garbeam@0 244
garbeam@0 245 /* init cursors */
garbeam@0 246 cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
garbeam@0 247 cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
garbeam@0 248 cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
garbeam@0 249
garbeam@8 250 update_keys();
garbeam@0 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@32 256 th = texth(&brush.font);
garbeam@5 257
garbeam@32 258 brush.drawable = XCreatePixmap(dpy, root, sw, th, DefaultDepth(dpy, screen));
garbeam@21 259 brush.gc = XCreateGC(dpy, root, 0, 0);
garbeam@21 260
garbeam@9 261 wa.event_mask = SubstructureRedirectMask | EnterWindowMask \
garbeam@9 262 | LeaveWindowMask;
garbeam@0 263 wa.cursor = cursor[CurNormal];
garbeam@0 264 XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
garbeam@0 265
garbeam@31 266 arrange = grid;
garbeam@5 267 scan_wins();
garbeam@3 268
garbeam@5 269 while(running) {
garbeam@32 270 XNextEvent(dpy, &ev);
garbeam@32 271 if(handler[ev.type])
garbeam@32 272 (handler[ev.type])(&ev); /* call handler */
garbeam@5 273 }
garbeam@0 274
garbeam@0 275 cleanup();
garbeam@0 276 XCloseDisplay(dpy);
garbeam@0 277
garbeam@0 278 return 0;
garbeam@0 279 }