X-Git-Url: https://jasonwoof.com/gitweb/?a=blobdiff_plain;f=main.c;h=a1cf245e34e4006d4194cb346f4f35876599bda6;hb=c0705eeb65733e8c5091e47d5bdc701a0779a949;hp=543d156cad6bc89222d3ec5137a4ce18fd808813;hpb=c47da143bdf5b4e3924a411f42648d4b3e86ff00;p=dwm.git diff --git a/main.c b/main.c index 543d156..a1cf245 100644 --- a/main.c +++ b/main.c @@ -3,9 +3,12 @@ * See LICENSE file for license details. */ +#include #include #include #include +#include +#include #include #include @@ -18,7 +21,6 @@ char *tags[TLast] = { [Tscratch] = "scratch", [Tdev] = "dev", - [Tirc] = "irc", [Twww] = "www", [Twork] = "work", }; @@ -33,19 +35,18 @@ Cursor cursor[CurLast]; Bool running = True; Bool issel; -char stext[1024]; int tsel = Tdev; /* default tag */ -int screen, sx, sy, sw, sh, th; +int screen, sx, sy, sw, sh, bx, by, bw, bh, mw; +char stext[1024]; DC dc = {0}; -Client *cstart = NULL; -Client *cend = NULL; -Client *csel = NULL; +Client *clients = NULL; +Client *sel = NULL; static Bool other_wm_running; static const char version[] = - "dwm - " VERSION ", (C)opyright MMVI Anselm R. Garbe\n"; -static int (*x_error_handler) (Display *, XErrorEvent *); + "dwm-" VERSION ", (C)opyright MMVI Anselm R. Garbe\n"; +static int (*x_xerror) (Display *, XErrorEvent *); static void usage() { error("usage: dwm [-v]\n"); } @@ -93,7 +94,7 @@ win_property(Window w, Atom a, Atom t, long l, unsigned char **prop) } int -win_proto(Window w) +proto(Window w) { unsigned char *protocols; long res; @@ -113,7 +114,7 @@ win_proto(Window w) } void -send_message(Window w, Atom a, long value) +sendevent(Window w, Atom a, long value) { XEvent e; @@ -134,7 +135,7 @@ send_message(Window w, Atom a, long value) * calls exit(). */ int -error_handler(Display *dpy, XErrorEvent *error) +xerror(Display *dpy, XErrorEvent *error) { if(error->error_code == BadWindow || (error->request_code == X_SetInputFocus @@ -152,7 +153,7 @@ error_handler(Display *dpy, XErrorEvent *error) return 0; fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n", error->request_code, error->error_code); - return x_error_handler(dpy, error); /* may call exit() */ + return x_xerror(dpy, error); /* may call exit() */ } /* @@ -160,7 +161,7 @@ error_handler(Display *dpy, XErrorEvent *error) * is already running. */ static int -startup_error_handler(Display *dpy, XErrorEvent *error) +startup_xerror(Display *dpy, XErrorEvent *error) { other_wm_running = True; return -1; @@ -169,8 +170,10 @@ startup_error_handler(Display *dpy, XErrorEvent *error) static void cleanup() { - while(csel) - unmanage(csel); + while(sel) { + resize(sel, True); + unmanage(sel); + } XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime); } @@ -183,13 +186,14 @@ quit(Arg *arg) int main(int argc, char *argv[]) { - int i; + int i, n; + fd_set rd; XSetWindowAttributes wa; unsigned int mask; + Bool readstdin = True; Window w; XEvent ev; - /* command line args */ for(i = 1; (i < argc) && (argv[i][0] == '-'); i++) { switch (argv[i][1]) { case 'v': @@ -211,7 +215,7 @@ main(int argc, char *argv[]) /* check if another WM is already running */ other_wm_running = False; - XSetErrorHandler(startup_error_handler); + XSetErrorHandler(startup_xerror); /* this causes an error if some other WM is running */ XSelectInput(dpy, root, SubstructureRedirectMask); XFlush(dpy); @@ -219,53 +223,101 @@ main(int argc, char *argv[]) if(other_wm_running) error("dwm: another window manager is already running\n"); - sx = sy = 0; - sw = DisplayWidth(dpy, screen); - sh = DisplayHeight(dpy, screen); - issel = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask); - XSetErrorHandler(0); - x_error_handler = XSetErrorHandler(error_handler); + x_xerror = XSetErrorHandler(xerror); /* init atoms */ wm_atom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False); wm_atom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False); net_atom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False); net_atom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False); - XChangeProperty(dpy, root, net_atom[NetSupported], XA_ATOM, 32, PropModeReplace, (unsigned char *) net_atom, NetLast); - /* init cursors */ cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr); cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing); cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur); - update_keys(); + grabkeys(); /* style */ - dc.bg = initcolor(BGCOLOR); - dc.fg = initcolor(FGCOLOR); - dc.border = initcolor(BORDERCOLOR); - initfont(FONT); - - th = dc.font.height + 4; + dc.bg = getcolor(BGCOLOR); + dc.fg = getcolor(FGCOLOR); + dc.border = getcolor(BORDERCOLOR); + setfont(FONT); - dc.drawable = XCreatePixmap(dpy, root, sw, th, DefaultDepth(dpy, screen)); + sx = sy = 0; + sw = DisplayWidth(dpy, screen); + sh = DisplayHeight(dpy, screen); + mw = (sw * MASTERW) / 100; + + wa.override_redirect = 1; + wa.background_pixmap = ParentRelative; + wa.event_mask = ButtonPressMask | ExposureMask; + + bx = by = 0; + bw = sw; + dc.h = bh = dc.font.height + 4; + barwin = XCreateWindow(dpy, root, bx, by, bw, bh, 0, DefaultDepth(dpy, screen), + CopyFromParent, DefaultVisual(dpy, screen), + CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa); + XDefineCursor(dpy, barwin, cursor[CurNormal]); + XMapRaised(dpy, barwin); + + dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen)); dc.gc = XCreateGC(dpy, root, 0, 0); + drawstatus(); + + issel = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask); wa.event_mask = SubstructureRedirectMask | EnterWindowMask \ | LeaveWindowMask; wa.cursor = cursor[CurNormal]; + XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa); + strcpy(stext, "dwm-"VERSION); scan_wins(); + /* main event loop, reads status text from stdin as well */ +Mainloop: while(running) { - XNextEvent(dpy, &ev); - if(handler[ev.type]) - (handler[ev.type])(&ev); /* call handler */ + FD_ZERO(&rd); + if(readstdin) + FD_SET(STDIN_FILENO, &rd); + FD_SET(ConnectionNumber(dpy), &rd); + + i = select(ConnectionNumber(dpy) + 1, &rd, 0, 0, 0); + if(i == -1 && errno == EINTR) + continue; + if(i < 0) + error("select failed\n"); + else if(i > 0) { + if(FD_ISSET(ConnectionNumber(dpy), &rd)) { + while(XPending(dpy)) { + XNextEvent(dpy, &ev); + if(handler[ev.type]) + (handler[ev.type])(&ev); /* call handler */ + } + } + if(readstdin && FD_ISSET(STDIN_FILENO, &rd)) { + i = n = 0; + for(;;) { + if((i = getchar()) == EOF) { + /* broken pipe/end of producer */ + readstdin = False; + strcpy(stext, "broken pipe"); + goto Mainloop; + } + if(i == '\n' || n >= sizeof(stext) - 1) + break; + stext[n++] = i; + } + stext[n] = 0; + drawstatus(); + } + } } cleanup();