aewl

annotate main.c @ 301:87d1c9978ed2

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