aewl

annotate main.c @ 328:083f1f3e1e93

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