git.strcat.st

/strcat/wm.git/ - summarytreelogarchivereleases

subject
changed a few things
commit
fb9ef84d128add83c01d5653661f8109a49eeef3
date
2026-04-20T17:59:30Z
message
XOpenDisplay(0x0) -> XOpenDisplay(NULL)

added explicit drag state

motion handling only runs while dragging is active

stopped using ev.xmotion.window for resize target; using dragwin instead
some other shit..

diff
 Makefile | 13 +++++++++
 wm.c     | 93 +++++++++++++++++++++++++++++++++++-----------------------------
 2 files changed, 64 insertions(+), 42 deletions(-)

diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..97e855a
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,13 @@
+CFLAGS = -std=c89 -Wall -Wextra -Werror -pedantic
+LDFLAGS = -lX11
+
+all: tinywm
+
+tinywm: wm.c
+	cc $(CFLAGS) -o tinywm wm.c $(LDFLAGS)
+
+run: tinywm
+	./tinywm
+
+clean:
+	rm -f tinywm
diff --git a/wm.c b/wm.c
index ee4c746..f0fbc4c 100644
--- a/wm.c
+++ b/wm.c
@@ -1,52 +1,61 @@
 #include <X11/Xlib.h>
 
 #define MAX(a, b) ((a) > (b) ? (a) : (b))
+#define MODKEY Mod1Mask
 
-int main()
+int
+main(void)
 {
-    Display * dpy;
-    Window root;
-    XWindowAttributes attr;
-    XButtonEvent start;
-    XEvent ev;
+	Display *dpy;
+	Window root;
+	Window dragwin;
+	XWindowAttributes attr;
+	XButtonEvent start;
+	XEvent ev;
+	int dragging;
 
-    if(!(dpy = XOpenDisplay(0x0))) return 1;
+	dragging = 0;
+	dragwin = None;
+	if ((dpy = XOpenDisplay(NULL)) == NULL)
+	    return 1;
 
-    root = DefaultRootWindow(dpy);
+	root = DefaultRootWindow(dpy);
 
-    XGrabKey(dpy, XKeysymToKeycode(dpy, XStringToKeysym("F1")), Mod1Mask, root,
-            True, GrabModeAsync, GrabModeAsync);
-    XGrabButton(dpy, 1, Mod1Mask, root, True, ButtonPressMask, GrabModeAsync,
-            GrabModeAsync, None, None);
-    XGrabButton(dpy, 3, Mod1Mask, root, True, ButtonPressMask, GrabModeAsync,
-            GrabModeAsync, None, None);
+	XGrabKey(dpy, XKeysymToKeycode(dpy, XStringToKeysym("F1")), MODKEY, root,
+	    True, GrabModeAsync, GrabModeAsync);
+	XGrabButton(dpy, 1, MODKEY, root, True, ButtonPressMask, GrabModeAsync,
+	    GrabModeAsync, None, None);
+	XGrabButton(dpy, 3, MODKEY, root, True, ButtonPressMask, GrabModeAsync,
+	    GrabModeAsync, None, None);
 
-    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)
-        {
-            XGrabPointer(dpy, ev.xbutton.subwindow, True,
-                    PointerMotionMask|ButtonReleaseMask, GrabModeAsync,
-                    GrabModeAsync, None, None, CurrentTime);
-            XGetWindowAttributes(dpy, ev.xbutton.subwindow, &attr);
-            start = ev.xbutton;
-        }
-        else if(ev.type == MotionNotify)
-        {
-            int xdiff, ydiff;
-            while(XCheckTypedEvent(dpy, MotionNotify, &ev));
-            xdiff = ev.xbutton.x_root - start.x_root;
-            ydiff = ev.xbutton.y_root - start.y_root;
-            XMoveResizeWindow(dpy, ev.xmotion.window,
-                attr.x + (start.button==1 ? xdiff : 0),
-                attr.y + (start.button==1 ? ydiff : 0),
-                MAX(1, attr.width + (start.button==3 ? xdiff : 0)),
-                MAX(1, attr.height + (start.button==3 ? ydiff : 0)));
-        }
-        else if(ev.type == ButtonRelease)
-            XUngrabPointer(dpy, CurrentTime);
-    }
+	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) {
+	        dragwin = ev.xbutton.subwindow;
+	        XGrabPointer(dpy, dragwin, True,
+	            PointerMotionMask | ButtonReleaseMask,
+	            GrabModeAsync, GrabModeAsync, None, None, CurrentTime);
+	        XGetWindowAttributes(dpy, dragwin, &attr);
+	        start = ev.xbutton;
+	        dragging = 1;
+	    } else if (ev.type == MotionNotify && dragging) {
+	        int xdiff, ydiff;
+
+	        while (XCheckTypedEvent(dpy, MotionNotify, &ev))
+	            continue;
+	        xdiff = ev.xmotion.x_root - start.x_root;
+	        ydiff = ev.xmotion.y_root - start.y_root;
+	        XMoveResizeWindow(dpy, dragwin,
+	            attr.x + (start.button == 1 ? xdiff : 0),
+	            attr.y + (start.button == 1 ? ydiff : 0),
+	            MAX(1, attr.width + (start.button == 3 ? xdiff : 0)),
+	            MAX(1, attr.height + (start.button == 3 ? ydiff : 0)));
+	    } else if (ev.type == ButtonRelease) {
+	        dragging = 0;
+	        dragwin = None;
+	        XUngrabPointer(dpy, CurrentTime);
+	    }
+	}
 }