dwm-meillo

annotate main.c @ 138:c1185dc7a36e

some cleanups/fixes inspired by Jukka Salmi's feedback
author arg@10ksloc.org
date Fri, 21 Jul 2006 18:34:10 +0200
parents 77922a389fa8
children f328ce9c558c
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@76 6 #include "dwm.h"
garbeam@76 7
garbeam@59 8 #include <errno.h>
garbeam@0 9 #include <stdio.h>
garbeam@0 10 #include <stdlib.h>
garbeam@57 11 #include <string.h>
garbeam@59 12 #include <unistd.h>
arg@138 13 #include <sys/select.h>
garbeam@0 14 #include <X11/cursorfont.h>
garbeam@0 15 #include <X11/Xatom.h>
garbeam@0 16 #include <X11/Xproto.h>
garbeam@0 17
garbeam@3 18
garbeam@84 19 /* static */
garbeam@0 20
arg@123 21 static int (*xerrorxlib)(Display *, XErrorEvent *);
garbeam@75 22 static Bool otherwm;
garbeam@0 23
garbeam@0 24 static void
garbeam@76 25 cleanup()
garbeam@76 26 {
garbeam@76 27 while(sel) {
arg@99 28 resize(sel, True, TopLeft);
garbeam@76 29 unmanage(sel);
garbeam@76 30 }
garbeam@76 31 XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
garbeam@76 32 }
garbeam@0 33
garbeam@0 34 static void
garbeam@75 35 scan()
garbeam@0 36 {
garbeam@0 37 unsigned int i, num;
arg@123 38 Window *wins, d1, d2;
garbeam@0 39 XWindowAttributes wa;
garbeam@0 40
garbeam@0 41 if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
garbeam@0 42 for(i = 0; i < num; i++) {
garbeam@0 43 if(!XGetWindowAttributes(dpy, wins[i], &wa))
garbeam@0 44 continue;
garbeam@0 45 if(wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
garbeam@0 46 continue;
garbeam@0 47 if(wa.map_state == IsViewable)
garbeam@10 48 manage(wins[i], &wa);
garbeam@0 49 }
garbeam@0 50 }
garbeam@0 51 if(wins)
garbeam@0 52 XFree(wins);
garbeam@0 53 }
garbeam@0 54
garbeam@13 55 static int
garbeam@13 56 win_property(Window w, Atom a, Atom t, long l, unsigned char **prop)
garbeam@13 57 {
arg@123 58 int status, format;
arg@123 59 unsigned long res, extra;
garbeam@13 60 Atom real;
garbeam@13 61
garbeam@13 62 status = XGetWindowProperty(dpy, w, a, 0L, l, False, t, &real, &format,
garbeam@13 63 &res, &extra, prop);
garbeam@13 64
garbeam@13 65 if(status != Success || *prop == 0) {
garbeam@13 66 return 0;
garbeam@13 67 }
garbeam@13 68 if(res == 0) {
garbeam@13 69 free((void *) *prop);
garbeam@13 70 }
garbeam@13 71 return res;
garbeam@13 72 }
garbeam@13 73
garbeam@76 74 /*
garbeam@76 75 * Startup Error handler to check if another window manager
garbeam@76 76 * is already running.
garbeam@76 77 */
garbeam@76 78 static int
garbeam@76 79 xerrorstart(Display *dsply, XErrorEvent *ee)
garbeam@76 80 {
garbeam@76 81 otherwm = True;
garbeam@76 82 return -1;
garbeam@76 83 }
garbeam@76 84
garbeam@84 85 /* extern */
garbeam@84 86
garbeam@84 87 char stext[1024];
arg@126 88 /* CUSTOMIZE */
garbeam@84 89 int tsel = Tdev; /* default tag */
arg@126 90 /* END CUSTOMIZE */
garbeam@84 91 int screen, sx, sy, sw, sh, bx, by, bw, bh, mw;
garbeam@84 92 Atom wmatom[WMLast], netatom[NetLast];
garbeam@84 93 Bool running = True;
garbeam@84 94 Bool issel = True;
garbeam@84 95 Client *clients = NULL;
garbeam@84 96 Client *sel = NULL;
garbeam@84 97 Cursor cursor[CurLast];
garbeam@84 98 Display *dpy;
garbeam@84 99 DC dc = {0};
garbeam@84 100 Window root, barwin;
garbeam@76 101
garbeam@13 102 int
garbeam@75 103 getproto(Window w)
garbeam@13 104 {
garbeam@13 105 int protos = 0;
garbeam@13 106 int i;
arg@123 107 long res;
arg@123 108 unsigned char *protocols;
garbeam@13 109
garbeam@77 110 res = win_property(w, wmatom[WMProtocols], XA_ATOM, 20L, &protocols);
garbeam@13 111 if(res <= 0) {
garbeam@13 112 return protos;
garbeam@13 113 }
garbeam@13 114 for(i = 0; i < res; i++) {
garbeam@77 115 if(protocols[i] == wmatom[WMDelete])
garbeam@13 116 protos |= WM_PROTOCOL_DELWIN;
garbeam@13 117 }
garbeam@13 118 free((char *) protocols);
garbeam@13 119 return protos;
garbeam@13 120 }
garbeam@13 121
garbeam@13 122 void
garbeam@74 123 sendevent(Window w, Atom a, long value)
garbeam@13 124 {
garbeam@13 125 XEvent e;
garbeam@13 126
garbeam@13 127 e.type = ClientMessage;
garbeam@13 128 e.xclient.window = w;
garbeam@13 129 e.xclient.message_type = a;
garbeam@13 130 e.xclient.format = 32;
garbeam@13 131 e.xclient.data.l[0] = value;
garbeam@13 132 e.xclient.data.l[1] = CurrentTime;
garbeam@13 133 XSendEvent(dpy, w, False, NoEventMask, &e);
garbeam@79 134 XSync(dpy, False);
garbeam@13 135 }
garbeam@13 136
garbeam@76 137 void
garbeam@76 138 quit(Arg *arg)
garbeam@75 139 {
garbeam@76 140 running = False;
garbeam@75 141 }
garbeam@75 142
garbeam@75 143 /*
garbeam@84 144 * There's no way to check accesses to destroyed windows, thus those cases are
garbeam@84 145 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
garbeam@84 146 * default error handler, which calls exit().
garbeam@0 147 */
garbeam@10 148 int
garbeam@75 149 xerror(Display *dpy, XErrorEvent *ee)
garbeam@0 150 {
garbeam@75 151 if(ee->error_code == BadWindow
arg@123 152 || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
arg@123 153 || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
arg@123 154 || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
arg@123 155 || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
arg@123 156 || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
arg@123 157 || (ee->request_code == X_GrabKey && ee->error_code == BadAccess))
garbeam@0 158 return 0;
garbeam@34 159 fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
arg@123 160 ee->request_code, ee->error_code);
garbeam@75 161 return xerrorxlib(dpy, ee); /* may call exit() */
garbeam@27 162 }
garbeam@27 163
garbeam@0 164 int
garbeam@0 165 main(int argc, char *argv[])
garbeam@0 166 {
arg@138 167 int i;
arg@123 168 unsigned int mask;
garbeam@59 169 fd_set rd;
arg@137 170 Bool readin = True;
garbeam@0 171 Window w;
garbeam@5 172 XEvent ev;
arg@123 173 XSetWindowAttributes wa;
garbeam@0 174
arg@137 175 if(argc == 2 && !strncmp("-v", argv[1], 3)) {
arg@137 176 fputs("dwm-"VERSION", (C)opyright MMVI Anselm R. Garbe\n", stdout);
arg@137 177 exit(EXIT_SUCCESS);
garbeam@0 178 }
arg@137 179 else if(argc != 1)
arg@137 180 eprint("usage: dwm [-v]\n");
garbeam@0 181
garbeam@0 182 dpy = XOpenDisplay(0);
garbeam@0 183 if(!dpy)
garbeam@75 184 eprint("dwm: cannot connect X server\n");
garbeam@0 185
garbeam@0 186 screen = DefaultScreen(dpy);
garbeam@0 187 root = RootWindow(dpy, screen);
garbeam@0 188
garbeam@0 189 /* check if another WM is already running */
garbeam@75 190 otherwm = False;
garbeam@75 191 XSetErrorHandler(xerrorstart);
garbeam@0 192 /* this causes an error if some other WM is running */
garbeam@0 193 XSelectInput(dpy, root, SubstructureRedirectMask);
garbeam@78 194 XSync(dpy, False);
garbeam@0 195
garbeam@75 196 if(otherwm)
garbeam@75 197 eprint("dwm: another window manager is already running\n");
garbeam@0 198
garbeam@78 199 XSetErrorHandler(NULL);
garbeam@75 200 xerrorxlib = XSetErrorHandler(xerror);
garbeam@0 201
garbeam@0 202 /* init atoms */
garbeam@77 203 wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
garbeam@77 204 wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
garbeam@77 205 netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
garbeam@77 206 netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
garbeam@77 207 XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
garbeam@77 208 PropModeReplace, (unsigned char *) netatom, NetLast);
garbeam@0 209
garbeam@0 210 /* init cursors */
garbeam@0 211 cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
garbeam@0 212 cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
garbeam@0 213 cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
garbeam@0 214
garbeam@73 215 grabkeys();
garbeam@0 216
garbeam@5 217 /* style */
garbeam@74 218 dc.bg = getcolor(BGCOLOR);
garbeam@74 219 dc.fg = getcolor(FGCOLOR);
garbeam@74 220 dc.border = getcolor(BORDERCOLOR);
garbeam@74 221 setfont(FONT);
garbeam@5 222
garbeam@57 223 sx = sy = 0;
garbeam@57 224 sw = DisplayWidth(dpy, screen);
garbeam@57 225 sh = DisplayHeight(dpy, screen);
garbeam@57 226 mw = (sw * MASTERW) / 100;
garbeam@5 227
garbeam@57 228 wa.override_redirect = 1;
garbeam@57 229 wa.background_pixmap = ParentRelative;
garbeam@58 230 wa.event_mask = ButtonPressMask | ExposureMask;
garbeam@57 231
garbeam@57 232 bx = by = 0;
garbeam@57 233 bw = sw;
garbeam@57 234 dc.h = bh = dc.font.height + 4;
garbeam@57 235 barwin = XCreateWindow(dpy, root, bx, by, bw, bh, 0, DefaultDepth(dpy, screen),
garbeam@57 236 CopyFromParent, DefaultVisual(dpy, screen),
garbeam@57 237 CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
garbeam@57 238 XDefineCursor(dpy, barwin, cursor[CurNormal]);
garbeam@57 239 XMapRaised(dpy, barwin);
garbeam@57 240
garbeam@60 241 dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
garbeam@60 242 dc.gc = XCreateGC(dpy, root, 0, 0);
garbeam@74 243 drawstatus();
garbeam@60 244
garbeam@57 245 issel = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask);
garbeam@21 246
arg@123 247 wa.event_mask = SubstructureRedirectMask | EnterWindowMask | LeaveWindowMask;
garbeam@0 248 wa.cursor = cursor[CurNormal];
garbeam@0 249 XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
garbeam@0 250
garbeam@57 251 strcpy(stext, "dwm-"VERSION);
garbeam@75 252 scan();
garbeam@3 253
garbeam@59 254 /* main event loop, reads status text from stdin as well */
garbeam@5 255 while(running) {
garbeam@59 256 FD_ZERO(&rd);
arg@137 257 if(readin)
garbeam@65 258 FD_SET(STDIN_FILENO, &rd);
garbeam@59 259 FD_SET(ConnectionNumber(dpy), &rd);
garbeam@59 260
garbeam@59 261 i = select(ConnectionNumber(dpy) + 1, &rd, 0, 0, 0);
garbeam@59 262 if(i == -1 && errno == EINTR)
garbeam@59 263 continue;
garbeam@59 264 if(i < 0)
garbeam@75 265 eprint("select failed\n");
garbeam@59 266 else if(i > 0) {
garbeam@64 267 if(FD_ISSET(ConnectionNumber(dpy), &rd)) {
garbeam@64 268 while(XPending(dpy)) {
garbeam@64 269 XNextEvent(dpy, &ev);
garbeam@64 270 if(handler[ev.type])
garbeam@64 271 (handler[ev.type])(&ev); /* call handler */
garbeam@64 272 }
garbeam@59 273 }
arg@137 274 if(readin && FD_ISSET(STDIN_FILENO, &rd)) {
arg@137 275 readin = NULL != fgets(stext, sizeof(stext), stdin);
arg@137 276 if(readin)
arg@137 277 stext[strlen(stext) - 1] = 0;
arg@137 278 else
arg@137 279 strcpy(stext, "broken pipe");
garbeam@74 280 drawstatus();
garbeam@59 281 }
garbeam@59 282 }
garbeam@5 283 }
garbeam@0 284
garbeam@0 285 cleanup();
garbeam@0 286 XCloseDisplay(dpy);
garbeam@0 287
garbeam@0 288 return 0;
garbeam@0 289 }