aewl

annotate main.c @ 60:24f9c674d03f

made stdin reader more robust
author Anselm R. Garbe <garbeam@wmii.de>
date Fri, 14 Jul 2006 12:08:32 +0200
parents 5d4653de9a1c
children db93644de522
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@59 6 #include <errno.h>
garbeam@0 7 #include <stdarg.h>
garbeam@0 8 #include <stdio.h>
garbeam@0 9 #include <stdlib.h>
garbeam@57 10 #include <string.h>
garbeam@59 11 #include <unistd.h>
garbeam@0 12
garbeam@0 13 #include <X11/cursorfont.h>
garbeam@0 14 #include <X11/Xatom.h>
garbeam@0 15 #include <X11/Xproto.h>
garbeam@0 16
garbeam@43 17 #include "dwm.h"
garbeam@0 18
garbeam@31 19 /********** CUSTOMIZE **********/
garbeam@31 20
garbeam@31 21 char *tags[TLast] = {
garbeam@31 22 [Tscratch] = "scratch",
garbeam@31 23 [Tdev] = "dev",
garbeam@31 24 [Twww] = "www",
garbeam@31 25 [Twork] = "work",
garbeam@31 26 };
garbeam@31 27
garbeam@31 28 /********** CUSTOMIZE **********/
garbeam@31 29
garbeam@3 30 /* X structs */
garbeam@0 31 Display *dpy;
garbeam@5 32 Window root, barwin;
garbeam@13 33 Atom wm_atom[WMLast], net_atom[NetLast];
garbeam@0 34 Cursor cursor[CurLast];
garbeam@5 35 Bool running = True;
garbeam@31 36 Bool issel;
garbeam@3 37
garbeam@31 38 int tsel = Tdev; /* default tag */
garbeam@57 39 int screen, sx, sy, sw, sh, bx, by, bw, bh, mw;
garbeam@57 40 char stext[1024];
garbeam@3 41
garbeam@34 42 DC dc = {0};
garbeam@50 43 Client *clients = NULL;
garbeam@50 44 Client *sel = NULL;
garbeam@0 45
garbeam@0 46 static Bool other_wm_running;
garbeam@31 47 static const char version[] =
garbeam@57 48 "dwm-" VERSION ", (C)opyright MMVI Anselm R. Garbe\n";
garbeam@0 49 static int (*x_error_handler) (Display *, XErrorEvent *);
garbeam@0 50
garbeam@0 51 static void
garbeam@34 52 usage() { error("usage: dwm [-v]\n"); }
garbeam@0 53
garbeam@0 54 static void
garbeam@0 55 scan_wins()
garbeam@0 56 {
garbeam@0 57 unsigned int i, num;
garbeam@0 58 Window *wins;
garbeam@0 59 XWindowAttributes wa;
garbeam@0 60 Window d1, d2;
garbeam@0 61
garbeam@0 62 if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
garbeam@0 63 for(i = 0; i < num; i++) {
garbeam@0 64 if(!XGetWindowAttributes(dpy, wins[i], &wa))
garbeam@0 65 continue;
garbeam@0 66 if(wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
garbeam@0 67 continue;
garbeam@0 68 if(wa.map_state == IsViewable)
garbeam@10 69 manage(wins[i], &wa);
garbeam@0 70 }
garbeam@0 71 }
garbeam@0 72 if(wins)
garbeam@0 73 XFree(wins);
garbeam@0 74 }
garbeam@0 75
garbeam@13 76 static int
garbeam@13 77 win_property(Window w, Atom a, Atom t, long l, unsigned char **prop)
garbeam@13 78 {
garbeam@13 79 Atom real;
garbeam@13 80 int format;
garbeam@13 81 unsigned long res, extra;
garbeam@13 82 int status;
garbeam@13 83
garbeam@13 84 status = XGetWindowProperty(dpy, w, a, 0L, l, False, t, &real, &format,
garbeam@13 85 &res, &extra, prop);
garbeam@13 86
garbeam@13 87 if(status != Success || *prop == 0) {
garbeam@13 88 return 0;
garbeam@13 89 }
garbeam@13 90 if(res == 0) {
garbeam@13 91 free((void *) *prop);
garbeam@13 92 }
garbeam@13 93 return res;
garbeam@13 94 }
garbeam@13 95
garbeam@13 96 int
garbeam@13 97 win_proto(Window w)
garbeam@13 98 {
garbeam@30 99 unsigned char *protocols;
garbeam@13 100 long res;
garbeam@13 101 int protos = 0;
garbeam@13 102 int i;
garbeam@13 103
garbeam@30 104 res = win_property(w, wm_atom[WMProtocols], XA_ATOM, 20L, &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@34 154 fprintf(stderr, "dwm: 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@50 173 while(sel) {
garbeam@52 174 resize(sel, True);
garbeam@50 175 unmanage(sel);
garbeam@50 176 }
garbeam@0 177 XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
garbeam@0 178 }
garbeam@0 179
garbeam@27 180 void
garbeam@49 181 quit(Arg *arg)
garbeam@27 182 {
garbeam@27 183 running = False;
garbeam@27 184 }
garbeam@27 185
garbeam@0 186 int
garbeam@0 187 main(int argc, char *argv[])
garbeam@0 188 {
garbeam@59 189 int i, n;
garbeam@59 190 fd_set rd;
garbeam@0 191 XSetWindowAttributes wa;
garbeam@0 192 unsigned int mask;
garbeam@0 193 Window w;
garbeam@5 194 XEvent ev;
garbeam@0 195
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@34 210 error("dwm: 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@3 220 XFlush(dpy);
garbeam@0 221
garbeam@0 222 if(other_wm_running)
garbeam@34 223 error("dwm: another window manager is already running\n");
garbeam@0 224
garbeam@0 225 XSetErrorHandler(0);
garbeam@0 226 x_error_handler = XSetErrorHandler(error_handler);
garbeam@0 227
garbeam@0 228 /* init atoms */
garbeam@13 229 wm_atom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
garbeam@13 230 wm_atom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
garbeam@0 231 net_atom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
garbeam@0 232 net_atom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
garbeam@0 233 XChangeProperty(dpy, root, net_atom[NetSupported], XA_ATOM, 32,
garbeam@0 234 PropModeReplace, (unsigned char *) net_atom, NetLast);
garbeam@0 235
garbeam@0 236 /* init cursors */
garbeam@0 237 cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
garbeam@0 238 cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
garbeam@0 239 cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
garbeam@0 240
garbeam@8 241 update_keys();
garbeam@0 242
garbeam@5 243 /* style */
garbeam@43 244 dc.bg = initcolor(BGCOLOR);
garbeam@43 245 dc.fg = initcolor(FGCOLOR);
garbeam@43 246 dc.border = initcolor(BORDERCOLOR);
garbeam@43 247 initfont(FONT);
garbeam@5 248
garbeam@57 249 sx = sy = 0;
garbeam@57 250 sw = DisplayWidth(dpy, screen);
garbeam@57 251 sh = DisplayHeight(dpy, screen);
garbeam@57 252 mw = (sw * MASTERW) / 100;
garbeam@5 253
garbeam@57 254 wa.override_redirect = 1;
garbeam@57 255 wa.background_pixmap = ParentRelative;
garbeam@58 256 wa.event_mask = ButtonPressMask | ExposureMask;
garbeam@57 257
garbeam@57 258 bx = by = 0;
garbeam@57 259 bw = sw;
garbeam@57 260 dc.h = bh = dc.font.height + 4;
garbeam@57 261 barwin = XCreateWindow(dpy, root, bx, by, bw, bh, 0, DefaultDepth(dpy, screen),
garbeam@57 262 CopyFromParent, DefaultVisual(dpy, screen),
garbeam@57 263 CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
garbeam@57 264 XDefineCursor(dpy, barwin, cursor[CurNormal]);
garbeam@57 265 XMapRaised(dpy, barwin);
garbeam@57 266
garbeam@60 267 dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
garbeam@60 268 dc.gc = XCreateGC(dpy, root, 0, 0);
garbeam@60 269 draw_bar();
garbeam@60 270
garbeam@57 271 issel = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask);
garbeam@21 272
garbeam@9 273 wa.event_mask = SubstructureRedirectMask | EnterWindowMask \
garbeam@9 274 | LeaveWindowMask;
garbeam@0 275 wa.cursor = cursor[CurNormal];
garbeam@57 276
garbeam@0 277 XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
garbeam@0 278
garbeam@57 279 strcpy(stext, "dwm-"VERSION);
garbeam@5 280 scan_wins();
garbeam@3 281
garbeam@59 282 /* main event loop, reads status text from stdin as well */
garbeam@5 283 while(running) {
garbeam@60 284 Mainloop:
garbeam@59 285 FD_ZERO(&rd);
garbeam@59 286 FD_SET(0, &rd);
garbeam@59 287 FD_SET(ConnectionNumber(dpy), &rd);
garbeam@59 288
garbeam@59 289 i = select(ConnectionNumber(dpy) + 1, &rd, 0, 0, 0);
garbeam@59 290 if(i == -1 && errno == EINTR)
garbeam@59 291 continue;
garbeam@59 292 if(i < 0)
garbeam@59 293 error("select failed\n");
garbeam@59 294 else if(i > 0) {
garbeam@59 295 if(FD_ISSET(ConnectionNumber(dpy), &rd) && XPending(dpy) > 0) {
garbeam@59 296 XNextEvent(dpy, &ev);
garbeam@59 297 if(handler[ev.type])
garbeam@59 298 (handler[ev.type])(&ev); /* call handler */
garbeam@59 299 }
garbeam@59 300 if(FD_ISSET(0, &rd)) {
garbeam@59 301 i = n = 0;
garbeam@60 302 for(;;) {
garbeam@60 303 if((i = getchar()) == EOF) {
garbeam@60 304 stext[0] = 0;
garbeam@60 305 goto Mainloop;
garbeam@60 306 }
garbeam@60 307 if(i == '\n' || n >= sizeof(stext) - 1)
garbeam@60 308 break;
garbeam@59 309 stext[n++] = i;
garbeam@60 310 }
garbeam@59 311 stext[n] = 0;
garbeam@59 312 draw_bar();
garbeam@59 313 }
garbeam@59 314 }
garbeam@5 315 }
garbeam@0 316
garbeam@0 317 cleanup();
garbeam@0 318 XCloseDisplay(dpy);
garbeam@0 319
garbeam@0 320 return 0;
garbeam@0 321 }