aewl

annotate main.c @ 293:093744cc5353

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