aewl

annotate main.c @ 350:b10852dbbffe

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