dwm-meillo

annotate main.c @ 329:cea0c98495bc

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