dwm-meillo

annotate wm.c @ 27:f96fb3fd8203

added grid mode on Mod1Mask g
author Anselm R. Garbe <garbeam@wmii.de>
date Wed, 12 Jul 2006 16:00:51 +0200
parents e8f627998d6f
children 2e0fb4130bfb
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@14 6 #include <errno.h>
garbeam@14 7
garbeam@0 8 #include <stdarg.h>
garbeam@0 9 #include <stdio.h>
garbeam@0 10 #include <stdlib.h>
garbeam@0 11
garbeam@14 12 #include <sys/types.h>
garbeam@14 13 #include <sys/time.h>
garbeam@14 14
garbeam@0 15 #include <X11/cursorfont.h>
garbeam@0 16 #include <X11/Xatom.h>
garbeam@0 17 #include <X11/Xproto.h>
garbeam@0 18
garbeam@0 19 #include "wm.h"
garbeam@0 20
garbeam@3 21 /* X structs */
garbeam@0 22 Display *dpy;
garbeam@5 23 Window root, barwin;
garbeam@13 24 Atom wm_atom[WMLast], net_atom[NetLast];
garbeam@0 25 Cursor cursor[CurLast];
garbeam@5 26 Bool running = True;
garbeam@13 27 Bool sel_screen;
garbeam@3 28
garbeam@16 29 char statustext[1024], tag[256];
garbeam@26 30 int screen, sx, sy, sw, sh, bx, by, bw, bh;
garbeam@3 31
garbeam@3 32 Brush brush = {0};
garbeam@10 33 Client *clients = NULL;
garbeam@13 34 Client *stack = NULL;
garbeam@0 35
garbeam@0 36 static Bool other_wm_running;
garbeam@16 37 static const char version[] = "gridwm - " VERSION ", (C)opyright MMVI Anselm R. Garbe\n";
garbeam@0 38 static int (*x_error_handler) (Display *, XErrorEvent *);
garbeam@0 39
garbeam@16 40 static const char *status[] = {
garbeam@26 41 "sh", "-c", "echo -n `date '+%Y-%m-%d %H:%M'`"
garbeam@16 42 " `uptime | sed 's/.*://; s/,//g'`"
garbeam@16 43 " `acpi | awk '{print $4}' | sed 's/,//'`", 0
garbeam@16 44 };
garbeam@16 45
garbeam@0 46 static void
garbeam@0 47 usage()
garbeam@0 48 {
garbeam@0 49 fputs("usage: gridwm [-v]\n", stderr);
garbeam@0 50 exit(1);
garbeam@0 51 }
garbeam@0 52
garbeam@0 53 static void
garbeam@0 54 scan_wins()
garbeam@0 55 {
garbeam@0 56 unsigned int i, num;
garbeam@0 57 Window *wins;
garbeam@0 58 XWindowAttributes wa;
garbeam@0 59 Window d1, d2;
garbeam@0 60
garbeam@0 61 if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
garbeam@0 62 for(i = 0; i < num; i++) {
garbeam@0 63 if(!XGetWindowAttributes(dpy, wins[i], &wa))
garbeam@0 64 continue;
garbeam@0 65 if(wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
garbeam@0 66 continue;
garbeam@0 67 if(wa.map_state == IsViewable)
garbeam@10 68 manage(wins[i], &wa);
garbeam@0 69 }
garbeam@0 70 }
garbeam@0 71 if(wins)
garbeam@0 72 XFree(wins);
garbeam@0 73 }
garbeam@0 74
garbeam@13 75 static int
garbeam@13 76 win_property(Window w, Atom a, Atom t, long l, unsigned char **prop)
garbeam@13 77 {
garbeam@13 78 Atom real;
garbeam@13 79 int format;
garbeam@13 80 unsigned long res, extra;
garbeam@13 81 int status;
garbeam@13 82
garbeam@13 83 status = XGetWindowProperty(dpy, w, a, 0L, l, False, t, &real, &format,
garbeam@13 84 &res, &extra, prop);
garbeam@13 85
garbeam@13 86 if(status != Success || *prop == 0) {
garbeam@13 87 return 0;
garbeam@13 88 }
garbeam@13 89 if(res == 0) {
garbeam@13 90 free((void *) *prop);
garbeam@13 91 }
garbeam@13 92 return res;
garbeam@13 93 }
garbeam@13 94
garbeam@13 95 int
garbeam@13 96 win_proto(Window w)
garbeam@13 97 {
garbeam@13 98 Atom *protocols;
garbeam@13 99 long res;
garbeam@13 100 int protos = 0;
garbeam@13 101 int i;
garbeam@13 102
garbeam@13 103 res = win_property(w, wm_atom[WMProtocols], XA_ATOM, 20L,
garbeam@13 104 ((unsigned char **) &protocols));
garbeam@13 105 if(res <= 0) {
garbeam@13 106 return protos;
garbeam@13 107 }
garbeam@13 108 for(i = 0; i < res; i++) {
garbeam@13 109 if(protocols[i] == wm_atom[WMDelete])
garbeam@13 110 protos |= WM_PROTOCOL_DELWIN;
garbeam@13 111 }
garbeam@13 112 free((char *) protocols);
garbeam@13 113 return protos;
garbeam@13 114 }
garbeam@13 115
garbeam@13 116 void
garbeam@13 117 send_message(Window w, Atom a, long value)
garbeam@13 118 {
garbeam@13 119 XEvent e;
garbeam@13 120
garbeam@13 121 e.type = ClientMessage;
garbeam@13 122 e.xclient.window = w;
garbeam@13 123 e.xclient.message_type = a;
garbeam@13 124 e.xclient.format = 32;
garbeam@13 125 e.xclient.data.l[0] = value;
garbeam@13 126 e.xclient.data.l[1] = CurrentTime;
garbeam@13 127 XSendEvent(dpy, w, False, NoEventMask, &e);
garbeam@13 128 XFlush(dpy);
garbeam@13 129 }
garbeam@13 130
garbeam@0 131 /*
garbeam@0 132 * There's no way to check accesses to destroyed windows, thus
garbeam@0 133 * those cases are ignored (especially on UnmapNotify's).
garbeam@0 134 * Other types of errors call Xlib's default error handler, which
garbeam@0 135 * calls exit().
garbeam@0 136 */
garbeam@10 137 int
garbeam@0 138 error_handler(Display *dpy, XErrorEvent *error)
garbeam@0 139 {
garbeam@0 140 if(error->error_code == BadWindow
garbeam@0 141 || (error->request_code == X_SetInputFocus
garbeam@0 142 && error->error_code == BadMatch)
garbeam@0 143 || (error->request_code == X_PolyText8
garbeam@0 144 && error->error_code == BadDrawable)
garbeam@0 145 || (error->request_code == X_PolyFillRectangle
garbeam@0 146 && error->error_code == BadDrawable)
garbeam@0 147 || (error->request_code == X_PolySegment
garbeam@0 148 && error->error_code == BadDrawable)
garbeam@0 149 || (error->request_code == X_ConfigureWindow
garbeam@0 150 && error->error_code == BadMatch)
garbeam@0 151 || (error->request_code == X_GrabKey
garbeam@0 152 && error->error_code == BadAccess))
garbeam@0 153 return 0;
garbeam@0 154 fprintf(stderr, "gridwm: fatal error: request code=%d, error code=%d\n",
garbeam@0 155 error->request_code, error->error_code);
garbeam@0 156 return x_error_handler(dpy, error); /* may call exit() */
garbeam@0 157 }
garbeam@0 158
garbeam@0 159 /*
garbeam@0 160 * Startup Error handler to check if another window manager
garbeam@0 161 * is already running.
garbeam@0 162 */
garbeam@0 163 static int
garbeam@0 164 startup_error_handler(Display *dpy, XErrorEvent *error)
garbeam@0 165 {
garbeam@0 166 other_wm_running = True;
garbeam@0 167 return -1;
garbeam@0 168 }
garbeam@0 169
garbeam@0 170 static void
garbeam@0 171 cleanup()
garbeam@0 172 {
garbeam@14 173 while(clients)
garbeam@14 174 unmanage(clients);
garbeam@0 175 XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
garbeam@0 176 }
garbeam@0 177
garbeam@27 178 void
garbeam@27 179 run(void *aux)
garbeam@27 180 {
garbeam@27 181 spawn(dpy, aux);
garbeam@27 182 }
garbeam@27 183
garbeam@27 184 void
garbeam@27 185 quit(void *aux)
garbeam@27 186 {
garbeam@27 187 running = False;
garbeam@27 188 }
garbeam@27 189
garbeam@0 190 int
garbeam@0 191 main(int argc, char *argv[])
garbeam@0 192 {
garbeam@0 193 int i;
garbeam@0 194 XSetWindowAttributes wa;
garbeam@0 195 unsigned int mask;
garbeam@0 196 Window w;
garbeam@5 197 XEvent ev;
garbeam@14 198 fd_set fds;
garbeam@14 199 struct timeval t, timeout = {
garbeam@14 200 .tv_usec = 0,
garbeam@14 201 .tv_sec = STATUSDELAY,
garbeam@14 202 };
garbeam@0 203
garbeam@0 204 /* command line args */
garbeam@0 205 for(i = 1; (i < argc) && (argv[i][0] == '-'); i++) {
garbeam@0 206 switch (argv[i][1]) {
garbeam@0 207 case 'v':
garbeam@0 208 fprintf(stdout, "%s", version);
garbeam@0 209 exit(0);
garbeam@0 210 break;
garbeam@0 211 default:
garbeam@0 212 usage();
garbeam@0 213 break;
garbeam@0 214 }
garbeam@0 215 }
garbeam@0 216
garbeam@0 217 dpy = XOpenDisplay(0);
garbeam@0 218 if(!dpy)
garbeam@0 219 error("gridwm: cannot connect X server\n");
garbeam@0 220
garbeam@0 221 screen = DefaultScreen(dpy);
garbeam@0 222 root = RootWindow(dpy, screen);
garbeam@0 223
garbeam@0 224 /* check if another WM is already running */
garbeam@0 225 other_wm_running = False;
garbeam@0 226 XSetErrorHandler(startup_error_handler);
garbeam@0 227 /* this causes an error if some other WM is running */
garbeam@0 228 XSelectInput(dpy, root, SubstructureRedirectMask);
garbeam@3 229 XFlush(dpy);
garbeam@0 230
garbeam@0 231 if(other_wm_running)
garbeam@0 232 error("gridwm: another window manager is already running\n");
garbeam@0 233
garbeam@26 234 sx = sy = 0;
garbeam@26 235 sw = DisplayWidth(dpy, screen);
garbeam@26 236 sh = DisplayHeight(dpy, screen);
garbeam@0 237 sel_screen = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask);
garbeam@0 238
garbeam@0 239 XSetErrorHandler(0);
garbeam@0 240 x_error_handler = XSetErrorHandler(error_handler);
garbeam@0 241
garbeam@0 242 /* init atoms */
garbeam@13 243 wm_atom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
garbeam@13 244 wm_atom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
garbeam@0 245 net_atom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
garbeam@0 246 net_atom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
garbeam@0 247
garbeam@0 248 XChangeProperty(dpy, root, net_atom[NetSupported], XA_ATOM, 32,
garbeam@0 249 PropModeReplace, (unsigned char *) net_atom, NetLast);
garbeam@0 250
garbeam@0 251
garbeam@0 252 /* init cursors */
garbeam@0 253 cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
garbeam@0 254 cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
garbeam@0 255 cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
garbeam@0 256
garbeam@8 257 update_keys();
garbeam@0 258
garbeam@5 259 /* style */
garbeam@5 260 loadcolors(dpy, screen, &brush, BGCOLOR, FGCOLOR, BORDERCOLOR);
garbeam@5 261 loadfont(dpy, &brush.font, FONT);
garbeam@5 262
garbeam@5 263 wa.override_redirect = 1;
garbeam@5 264 wa.background_pixmap = ParentRelative;
garbeam@5 265 wa.event_mask = ExposureMask;
garbeam@5 266
garbeam@26 267 bx = by = 0;
garbeam@26 268 bw = sw;
garbeam@26 269 bh = texth(&brush.font);
garbeam@26 270 barwin = XCreateWindow(dpy, root, bx, by, bw, bh, 0, DefaultDepth(dpy, screen),
garbeam@5 271 CopyFromParent, DefaultVisual(dpy, screen),
garbeam@5 272 CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
garbeam@5 273 XDefineCursor(dpy, barwin, cursor[CurNormal]);
garbeam@5 274 XMapRaised(dpy, barwin);
garbeam@21 275
garbeam@26 276 brush.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
garbeam@21 277 brush.gc = XCreateGC(dpy, root, 0, 0);
garbeam@21 278
garbeam@16 279 pipe_spawn(statustext, sizeof(statustext), dpy, (char **)status);
garbeam@5 280 draw_bar();
garbeam@0 281
garbeam@9 282 wa.event_mask = SubstructureRedirectMask | EnterWindowMask \
garbeam@9 283 | LeaveWindowMask;
garbeam@0 284 wa.cursor = cursor[CurNormal];
garbeam@0 285 XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
garbeam@0 286
garbeam@5 287 scan_wins();
garbeam@3 288
garbeam@5 289 while(running) {
garbeam@14 290 if(XPending(dpy) > 0) {
garbeam@14 291 XNextEvent(dpy, &ev);
garbeam@14 292 if(handler[ev.type])
garbeam@14 293 (handler[ev.type]) (&ev); /* call handler */
garbeam@14 294 continue;
garbeam@14 295 }
garbeam@14 296 FD_ZERO(&fds);
garbeam@14 297 FD_SET(ConnectionNumber(dpy), &fds);
garbeam@14 298 t = timeout;
garbeam@14 299 if(select(ConnectionNumber(dpy) + 1, &fds, NULL, NULL, &t) > 0)
garbeam@14 300 continue;
garbeam@16 301 else if(errno != EINTR) {
garbeam@16 302 pipe_spawn(statustext, sizeof(statustext), dpy, (char **)status);
garbeam@14 303 draw_bar();
garbeam@16 304 }
garbeam@5 305 }
garbeam@0 306
garbeam@0 307 cleanup();
garbeam@0 308 XCloseDisplay(dpy);
garbeam@0 309
garbeam@0 310 return 0;
garbeam@0 311 }