git.strcat.st

/strcat/wm.git/ - summarytreelogarchivereleases

subject
added: workspaces
commit
c6aa6efa8f40a9d279c8a2fef48cdb03cd12b1b7
date
2026-04-20T20:25:02Z
message
diff
 wm.c | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 67 insertions(+), 4 deletions(-)

diff --git a/wm.c b/wm.c
index e5091e7..d21cbfd 100644
--- a/wm.c
+++ b/wm.c
@@ -1,7 +1,27 @@
 #include <X11/Xlib.h>
+#include <X11/keysym.h>
 
 #define MAX(a, b) ((a) > (b) ? (a) : (b))
 #define MODKEY Mod1Mask
+#define MAXWINS 4096
+
+static Window wins[MAXWINS];
+static int ws[MAXWINS], nwin, curws;
+
+static int
+getidx(Window w)
+{
+	int i;
+
+	for (i = 0; i < nwin; i++)
+	    if (wins[i] == w)
+	        return i;
+	if (w == None || nwin >= MAXWINS)
+	    return -1;
+	wins[nwin] = w;
+	ws[nwin] = curws;
+	return nwin++;
+}
 
 int
 main(void)
@@ -9,10 +29,13 @@ main(void)
 	Display *dpy;
 	Window root;
 	Window dragwin;
+	Window rootret, parent, *children, focus;
 	XWindowAttributes attr;
 	XButtonEvent start;
 	XEvent ev;
 	int dragging;
+	int i, j, revert;
+	KeySym ks;
 
 	dragging = 0;
 	dragwin = None;
@@ -21,8 +44,14 @@ main(void)
 
 	root = DefaultRootWindow(dpy);
 
-	XGrabKey(dpy, XKeysymToKeycode(dpy, XStringToKeysym("F1")), MODKEY, root,
+	XGrabKey(dpy, XKeysymToKeycode(dpy, XK_F1), MODKEY, root,
 	    True, GrabModeAsync, GrabModeAsync);
+	for (i = 0; i < 9; i++) {
+	    XGrabKey(dpy, XKeysymToKeycode(dpy, XK_1 + i), MODKEY, root,
+	        True, GrabModeAsync, GrabModeAsync);
+	    XGrabKey(dpy, XKeysymToKeycode(dpy, XK_1 + i), MODKEY | ShiftMask,
+	        root, True, GrabModeAsync, GrabModeAsync);
+	}
 	XGrabButton(dpy, 1, MODKEY, root, True, ButtonPressMask, GrabModeAsync,
 	    GrabModeAsync, None, None);
 	XGrabButton(dpy, 3, MODKEY, root, True, ButtonPressMask, GrabModeAsync,
@@ -30,10 +59,44 @@ main(void)
 
 	for (;;) {
 	    XNextEvent(dpy, &ev);
-	    if (ev.type == KeyPress && ev.xkey.subwindow != None)
-	        XRaiseWindow(dpy, ev.xkey.subwindow);
-	    else if (ev.type == ButtonPress && ev.xbutton.subwindow != None) {
+	    if (ev.type == KeyPress) {
+	        ks = XLookupKeysym(&ev.xkey, 0);
+	        if (ks >= XK_1 && ks <= XK_9) {
+	            i = (int)(ks - XK_1);
+	            if (ev.xkey.state & ShiftMask) {
+	                focus = ev.xkey.subwindow;
+	                if (focus == None)
+	                    XGetInputFocus(dpy, &focus, &revert);
+	                j = getidx(focus);
+	                if (j >= 0) {
+	                    ws[j] = i;
+	                    if (i != curws)
+	                        XUnmapWindow(dpy, focus);
+	                }
+	            } else if (i != curws) {
+	                curws = i;
+	                if (XQueryTree(dpy, root, &rootret, &parent, &children,
+	                    (unsigned int *)&j)) {
+	                    for (i = 0; i < j; i++) {
+	                        revert = getidx(children[i]);
+	                        if (revert >= 0) {
+	                            if (ws[revert] == curws)
+	                                XMapWindow(dpy, children[i]);
+	                            else
+	                                XUnmapWindow(dpy, children[i]);
+	                        }
+	                    }
+	                    if (children != None)
+	                        XFree(children);
+	                }
+	            }
+	        } else if (ks == XK_F1 && ev.xkey.subwindow != None)
+	            XRaiseWindow(dpy, ev.xkey.subwindow);
+	    } else if (ev.type == ButtonPress && ev.xbutton.subwindow != None) {
 	        dragwin = ev.xbutton.subwindow;
+	        j = getidx(dragwin);
+	        if (j >= 0)
+	            ws[j] = curws;
 	        XGrabPointer(dpy, dragwin, True,
 	            PointerMotionMask | ButtonReleaseMask,
 	            GrabModeAsync, GrabModeAsync, None, None, CurrentTime);