dwm-meillo

annotate main.c @ 307:ec8a66a2b9cc

fixed issue reported by sander
author Anselm R.Garbe <arg@10ksloc.org>
date Wed, 16 Aug 2006 17:56:04 +0200
parents 7565875d07d3
children 204427dcc087
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 XDestroyWindow(dpy, barwin);
arg@295 37 XFreePixmap(dpy, dc.drawable);
arg@295 38 XFreeGC(dpy, dc.gc);
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@13 65 static int
garbeam@13 66 win_property(Window w, Atom a, Atom t, long l, unsigned char **prop)
garbeam@13 67 {
arg@123 68 int status, format;
arg@123 69 unsigned long res, extra;
garbeam@13 70 Atom real;
garbeam@13 71
garbeam@13 72 status = XGetWindowProperty(dpy, w, a, 0L, l, False, t, &real, &format,
garbeam@13 73 &res, &extra, prop);
garbeam@13 74
garbeam@13 75 if(status != Success || *prop == 0) {
garbeam@13 76 return 0;
garbeam@13 77 }
garbeam@13 78 if(res == 0) {
garbeam@13 79 free((void *) *prop);
garbeam@13 80 }
garbeam@13 81 return res;
garbeam@13 82 }
garbeam@13 83
garbeam@76 84 /*
garbeam@76 85 * Startup Error handler to check if another window manager
garbeam@76 86 * is already running.
garbeam@76 87 */
garbeam@76 88 static int
garbeam@76 89 xerrorstart(Display *dsply, XErrorEvent *ee)
garbeam@76 90 {
garbeam@76 91 otherwm = True;
garbeam@76 92 return -1;
garbeam@76 93 }
garbeam@76 94
garbeam@84 95 /* extern */
garbeam@84 96
garbeam@84 97 char stext[1024];
arg@262 98 Bool *seltag;
garbeam@84 99 int screen, sx, sy, sw, sh, bx, by, bw, bh, mw;
arg@291 100 unsigned int ntags, numlockmask;
garbeam@84 101 Atom wmatom[WMLast], netatom[NetLast];
garbeam@84 102 Bool running = True;
garbeam@84 103 Bool issel = True;
garbeam@84 104 Client *clients = NULL;
garbeam@84 105 Client *sel = NULL;
garbeam@84 106 Cursor cursor[CurLast];
garbeam@84 107 Display *dpy;
garbeam@84 108 DC dc = {0};
garbeam@84 109 Window root, barwin;
garbeam@76 110
garbeam@13 111 int
garbeam@75 112 getproto(Window w)
garbeam@13 113 {
garbeam@13 114 int protos = 0;
garbeam@13 115 int i;
arg@123 116 long res;
arg@203 117 Atom *protocols;
garbeam@13 118
arg@203 119 res = win_property(w, wmatom[WMProtocols], XA_ATOM, 20L,
arg@203 120 ((unsigned char **)&protocols));
garbeam@13 121 if(res <= 0) {
garbeam@13 122 return protos;
garbeam@13 123 }
garbeam@13 124 for(i = 0; i < res; i++) {
garbeam@77 125 if(protocols[i] == wmatom[WMDelete])
arg@157 126 protos |= PROTODELWIN;
garbeam@13 127 }
garbeam@13 128 free((char *) protocols);
garbeam@13 129 return protos;
garbeam@13 130 }
garbeam@13 131
garbeam@13 132 void
garbeam@74 133 sendevent(Window w, Atom a, long value)
garbeam@13 134 {
garbeam@13 135 XEvent e;
garbeam@13 136
garbeam@13 137 e.type = ClientMessage;
garbeam@13 138 e.xclient.window = w;
garbeam@13 139 e.xclient.message_type = a;
garbeam@13 140 e.xclient.format = 32;
garbeam@13 141 e.xclient.data.l[0] = value;
garbeam@13 142 e.xclient.data.l[1] = CurrentTime;
garbeam@13 143 XSendEvent(dpy, w, False, NoEventMask, &e);
garbeam@79 144 XSync(dpy, False);
garbeam@13 145 }
garbeam@13 146
garbeam@76 147 void
garbeam@76 148 quit(Arg *arg)
garbeam@75 149 {
arg@302 150 readin = running = False;
garbeam@75 151 }
garbeam@75 152
garbeam@75 153 /*
garbeam@84 154 * There's no way to check accesses to destroyed windows, thus those cases are
garbeam@84 155 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
garbeam@84 156 * default error handler, which calls exit().
garbeam@0 157 */
garbeam@10 158 int
garbeam@75 159 xerror(Display *dpy, XErrorEvent *ee)
garbeam@0 160 {
garbeam@75 161 if(ee->error_code == BadWindow
arg@123 162 || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
arg@123 163 || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
arg@123 164 || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
arg@123 165 || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
arg@123 166 || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
arg@123 167 || (ee->request_code == X_GrabKey && ee->error_code == BadAccess))
garbeam@0 168 return 0;
garbeam@34 169 fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
arg@123 170 ee->request_code, ee->error_code);
garbeam@75 171 return xerrorxlib(dpy, ee); /* may call exit() */
garbeam@27 172 }
garbeam@27 173
garbeam@0 174 int
garbeam@0 175 main(int argc, char *argv[])
garbeam@0 176 {
arg@291 177 int i, j, xfd;
arg@123 178 unsigned int mask;
garbeam@59 179 fd_set rd;
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();
arg@302 282 readin = True;
garbeam@5 283 while(running) {
garbeam@59 284 FD_ZERO(&rd);
arg@164 285 if(readin)
arg@164 286 FD_SET(STDIN_FILENO, &rd);
arg@265 287 FD_SET(xfd, &rd);
arg@266 288 i = select(xfd + 1, &rd, NULL, NULL, NULL);
garbeam@59 289 if(i == -1 && errno == EINTR)
garbeam@59 290 continue;
garbeam@59 291 if(i < 0)
garbeam@75 292 eprint("select failed\n");
garbeam@59 293 else if(i > 0) {
arg@164 294 if(readin && FD_ISSET(STDIN_FILENO, &rd)) {
arg@164 295 readin = NULL != fgets(stext, sizeof(stext), stdin);
arg@164 296 if(readin)
arg@164 297 stext[strlen(stext) - 1] = 0;
arg@162 298 else
arg@164 299 strcpy(stext, "broken pipe");
garbeam@74 300 drawstatus();
garbeam@59 301 }
garbeam@59 302 }
arg@293 303 procevent();
garbeam@5 304 }
garbeam@0 305 cleanup();
garbeam@0 306 XCloseDisplay(dpy);
garbeam@0 307
garbeam@0 308 return 0;
garbeam@0 309 }