X-Git-Url: https://git.xinqibao.xyz/dwm.git/blobdiff_plain/9e8b3258a06de01c53e60243ffe3419b47d016dd..0a0759132c07ff78896fd8559e6f576898f266a3:/main.c

diff --git a/main.c b/main.c
index b63d07e..5dab0db 100644
--- a/main.c
+++ b/main.c
@@ -3,60 +3,67 @@
  * See LICENSE file for license details.
  */
 
-#include <stdarg.h>
+#include "dwm.h"
+#include <errno.h>
 #include <stdio.h>
 #include <stdlib.h>
-
+#include <string.h>
+#include <unistd.h>
+#include <sys/select.h>
 #include <X11/cursorfont.h>
+#include <X11/keysym.h>
 #include <X11/Xatom.h>
 #include <X11/Xproto.h>
 
-#include "dwm.h"
-
-/********** CUSTOMIZE **********/
+/* extern */
 
-char *tags[TLast] = {
-	[Tscratch] = "scratch",
-	[Tdev] = "dev",
-	[Tirc] = "irc",
-	[Twww] = "www",
-	[Twork] = "work",
-};
-
-/********** CUSTOMIZE **********/
-
-/* X structs */
+char stext[1024];
+Bool *seltag;
+int bx, by, bw, bh, bmw, mw, screen, sx, sy, sw, sh;
+unsigned int ntags, numlockmask;
+Atom wmatom[WMLast], netatom[NetLast];
+Bool running = True;
+Bool issel = True;
+Client *clients = NULL;
+Client *sel = NULL;
+Cursor cursor[CurLast];
 Display *dpy;
+DC dc = {0};
 Window root, barwin;
-Atom wm_atom[WMLast], net_atom[NetLast];
-Cursor cursor[CurLast];
-Bool running = True;
-Bool issel;
 
-char stext[1024];
-int tsel = Tdev; /* default tag */
-int screen, sx, sy, sw, sh, th;
+/* static */
 
-DC dc = {0};
-Client *clients = NULL;
-Client *stack = 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 *);
+static int (*xerrorxlib)(Display *, XErrorEvent *);
+static Bool otherwm, readin;
 
 static void
-usage() {	error("usage: dwm [-v]\n"); }
+cleanup()
+{
+	close(STDIN_FILENO);
+	while(sel) {
+		resize(sel, True, TopLeft);
+		unmanage(sel);
+	}
+	if(dc.font.set)
+		XFreeFontSet(dpy, dc.font.set);
+	else
+		XFreeFont(dpy, dc.font.xfont);
+	XUngrabKey(dpy, AnyKey, AnyModifier, root);
+	XFreePixmap(dpy, dc.drawable);
+	XFreeGC(dpy, dc.gc);
+	XDestroyWindow(dpy, barwin);
+	XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
+	XSync(dpy, False);
+}
 
 static void
-scan_wins()
+scan()
 {
 	unsigned int i, num;
-	Window *wins;
+	Window *wins, d1, d2;
 	XWindowAttributes wa;
-	Window d1, d2;
 
+	wins = NULL;
 	if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
 		for(i = 0; i < num; i++) {
 			if(!XGetWindowAttributes(dpy, wins[i], &wa))
@@ -71,48 +78,117 @@ scan_wins()
 		XFree(wins);
 }
 
-static int
-win_property(Window w, Atom a, Atom t, long l, unsigned char **prop)
+static void
+setup()
 {
-	Atom real;
-	int format;
-	unsigned long res, extra;
-	int status;
+	int i, j;
+	unsigned int mask;
+	Window w;
+	XModifierKeymap *modmap;
+	XSetWindowAttributes wa;
 
-	status = XGetWindowProperty(dpy, w, a, 0L, l, False, t, &real, &format,
-			&res, &extra, prop);
+	/* init atoms */
+	wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
+	wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
+	netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
+	netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
+	XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
+			PropModeReplace, (unsigned char *) netatom, NetLast);
 
-	if(status != Success || *prop == 0) {
-		return 0;
-	}
-	if(res == 0) {
-		free((void *) *prop);
+	/* init cursors */
+	cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
+	cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
+	cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
+
+	modmap = XGetModifierMapping(dpy);
+	for (i = 0; i < 8; i++) {
+		for (j = 0; j < modmap->max_keypermod; j++) {
+			if(modmap->modifiermap[i * modmap->max_keypermod + j] == XKeysymToKeycode(dpy, XK_Num_Lock))
+				numlockmask = (1 << i);
+		}
 	}
-	return res;
+	XFree(modmap);
+
+	wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask | EnterWindowMask | LeaveWindowMask;
+	wa.cursor = cursor[CurNormal];
+	XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
+
+	grabkeys();
+	initrregs();
+
+	for(ntags = 0; tags[ntags]; ntags++);
+	seltag = emallocz(sizeof(Bool) * ntags);
+	seltag[0] = True;
+
+	/* style */
+	dc.norm[ColBG] = getcolor(NORMBGCOLOR);
+	dc.norm[ColFG] = getcolor(NORMFGCOLOR);
+	dc.sel[ColBG] = getcolor(SELBGCOLOR);
+	dc.sel[ColFG] = getcolor(SELFGCOLOR);
+	dc.status[ColBG] = getcolor(STATUSBGCOLOR);
+	dc.status[ColFG] = getcolor(STATUSFGCOLOR);
+	setfont(FONT);
+
+	bmw = textw(FLOATSYMBOL) > textw(TILESYMBOL) ? textw(FLOATSYMBOL) : textw(TILESYMBOL);
+	sx = sy = 0;
+	sw = DisplayWidth(dpy, screen);
+	sh = DisplayHeight(dpy, screen);
+	mw = (sw * MASTERW) / 100;
+
+	bx = by = 0;
+	bw = sw;
+	dc.h = bh = dc.font.height + 2;
+	wa.override_redirect = 1;
+	wa.background_pixmap = ParentRelative;
+	wa.event_mask = ButtonPressMask | ExposureMask;
+	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);
+	XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
+
+	issel = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask);
+	strcpy(stext, "dwm-"VERSION);
 }
 
-int
-win_proto(Window w)
+/*
+ * Startup Error handler to check if another window manager
+ * is already running.
+ */
+static int
+xerrorstart(Display *dsply, XErrorEvent *ee)
 {
-	unsigned char *protocols;
-	long res;
-	int protos = 0;
-	int i;
+	otherwm = True;
+	return -1;
+}
+
+/* extern */
 
-	res = win_property(w, wm_atom[WMProtocols], XA_ATOM, 20L, &protocols);
-	if(res <= 0) {
+int
+getproto(Window w)
+{
+	int i, format, protos, status;
+	unsigned long extra, res;
+	Atom *protocols, real;
+
+	protos = 0;
+	status = XGetWindowProperty(dpy, w, wmatom[WMProtocols], 0L, 20L, False,
+			XA_ATOM, &real, &format, &res, &extra, (unsigned char **)&protocols);
+	if(status != Success || protocols == 0)
 		return protos;
-	}
-	for(i = 0; i < res; i++) {
-		if(protocols[i] == wm_atom[WMDelete])
-			protos |= WM_PROTOCOL_DELWIN;
-	}
-	free((char *) protocols);
+	for(i = 0; i < res; i++)
+		if(protocols[i] == wmatom[WMDelete])
+			protos |= PROTODELWIN;
+	free(protocols);
 	return protos;
 }
 
 void
-send_message(Window w, Atom a, long value)
+sendevent(Window w, Atom a, long value)
 {
 	XEvent e;
 
@@ -123,150 +199,101 @@ send_message(Window w, Atom a, long value)
 	e.xclient.data.l[0] = value;
 	e.xclient.data.l[1] = CurrentTime;
 	XSendEvent(dpy, w, False, NoEventMask, &e);
-	XFlush(dpy);
+	XSync(dpy, False);
 }
 
-/*
- * There's no way to check accesses to destroyed windows, thus
- * those cases are ignored (especially on UnmapNotify's).
- * Other types of errors call Xlib's default error handler, which
- * calls exit().
- */
-int
-error_handler(Display *dpy, XErrorEvent *error)
+void
+quit(Arg *arg)
 {
-	if(error->error_code == BadWindow
-			|| (error->request_code == X_SetInputFocus
-				&& error->error_code == BadMatch)
-			|| (error->request_code == X_PolyText8
-				&& error->error_code == BadDrawable)
-			|| (error->request_code == X_PolyFillRectangle
-				&& error->error_code == BadDrawable)
-			|| (error->request_code == X_PolySegment
-				&& error->error_code == BadDrawable)
-			|| (error->request_code == X_ConfigureWindow
-				&& error->error_code == BadMatch)
-			|| (error->request_code == X_GrabKey
-				&& error->error_code == BadAccess))
-		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() */
+	readin = running = False;
 }
 
 /*
- * Startup Error handler to check if another window manager
- * is already running.
+ * There's no way to check accesses to destroyed windows, thus those cases are
+ * ignored (especially on UnmapNotify's).  Other types of errors call Xlibs
+ * default error handler, which calls exit().
  */
-static int
-startup_error_handler(Display *dpy, XErrorEvent *error)
-{
-	other_wm_running = True;
-	return -1;
-}
-
-static void
-cleanup()
-{
-	while(clients)
-		unmanage(clients);
-	XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
-}
-
-void
-quit(void *aux)
+int
+xerror(Display *dpy, XErrorEvent *ee)
 {
-	running = False;
+	if(ee->error_code == BadWindow
+	|| (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
+	|| (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
+	|| (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
+	|| (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
+	|| (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
+	|| (ee->request_code == X_GrabKey && ee->error_code == BadAccess))
+		return 0;
+	fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
+		ee->request_code, ee->error_code);
+	return xerrorxlib(dpy, ee); /* may call exit() */
 }
 
 int
 main(int argc, char *argv[])
 {
-	int i;
-	XSetWindowAttributes wa;
-	unsigned int mask;
-	Window w;
-	XEvent ev;
-
-	/* command line args */
-	for(i = 1; (i < argc) && (argv[i][0] == '-'); i++) {
-		switch (argv[i][1]) {
-		case 'v':
-			fprintf(stdout, "%s", version);
-			exit(0);
-			break;
-		default:
-			usage();
-			break;
-		}
+	int r, xfd;
+	fd_set rd;
+
+	if(argc == 2 && !strncmp("-v", argv[1], 3)) {
+		fputs("dwm-"VERSION", (C)opyright MMVI Anselm R. Garbe\n", stdout);
+		exit(EXIT_SUCCESS);
 	}
+	else if(argc != 1)
+		eprint("usage: dwm [-v]\n");
 
 	dpy = XOpenDisplay(0);
 	if(!dpy)
-		error("dwm: cannot connect X server\n");
+		eprint("dwm: cannot open display\n");
 
+	xfd = ConnectionNumber(dpy);
 	screen = DefaultScreen(dpy);
 	root = RootWindow(dpy, screen);
 
-	/* check if another WM is already running */
-	other_wm_running = False;
-	XSetErrorHandler(startup_error_handler);
-	/* this causes an error if some other WM is running */
+	otherwm = False;
+	XSetErrorHandler(xerrorstart);
+	/* this causes an error if some other window manager is running */
 	XSelectInput(dpy, root, SubstructureRedirectMask);
-	XFlush(dpy);
-
-	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);
-
-	/* 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);
-
+	XSync(dpy, False);
 
-	/* init cursors */
-	cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
-	cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
-	cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
-
-	update_keys();
-
-	/* style */
-	dc.bg = initcolor(BGCOLOR);
-	dc.fg = initcolor(FGCOLOR);
-	dc.border = initcolor(BORDERCOLOR);
-	initfont(FONT);
+	if(otherwm)
+		eprint("dwm: another window manager is already running\n");
 
-	th = dc.font.height + 4;
+	XSync(dpy, False);
+	XSetErrorHandler(NULL);
+	xerrorxlib = XSetErrorHandler(xerror);
+	XSync(dpy, False);
 
-	dc.drawable = XCreatePixmap(dpy, root, sw, th, DefaultDepth(dpy, screen));
-	dc.gc = XCreateGC(dpy, root, 0, 0);
-
-	wa.event_mask = SubstructureRedirectMask | EnterWindowMask \
-					| LeaveWindowMask;
-	wa.cursor = cursor[CurNormal];
-	XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
-
-	scan_wins();
+	setup();
+	drawstatus();
+	scan();
 
+	/* main event loop, also reads status text from stdin */
+	XSync(dpy, False);
+	procevent();
+	readin = True;
 	while(running) {
-		XNextEvent(dpy, &ev);
-		if(handler[ev.type])
-			(handler[ev.type])(&ev); /* call handler */
+		FD_ZERO(&rd);
+		if(readin)
+			FD_SET(STDIN_FILENO, &rd);
+		FD_SET(xfd, &rd);
+		r = select(xfd + 1, &rd, NULL, NULL, NULL);
+		if((r == -1) && (errno == EINTR))
+			continue;
+		if(r > 0) {
+			if(readin && FD_ISSET(STDIN_FILENO, &rd)) {
+				readin = NULL != fgets(stext, sizeof(stext), stdin);
+				if(readin)
+					stext[strlen(stext) - 1] = 0;
+				else 
+					strcpy(stext, "broken pipe");
+				drawstatus();
+			}
+		}
+		else if(r < 0)
+			eprint("select failed\n");
+		procevent();
 	}
-
 	cleanup();
 	XCloseDisplay(dpy);