]> git.mdlowis.com Git - proto/anvil.git/commitdiff
initial commit
authorMichael D. Lowis <mike.lowis@gentex.com>
Tue, 4 Feb 2020 19:34:10 +0000 (14:34 -0500)
committerMichael D. Lowis <mike.lowis@gentex.com>
Tue, 4 Feb 2020 19:34:10 +0000 (14:34 -0500)
anvil.c [new file with mode: 0644]
tiny.c [new file with mode: 0644]

diff --git a/anvil.c b/anvil.c
new file mode 100644 (file)
index 0000000..01d07d1
--- /dev/null
+++ b/anvil.c
@@ -0,0 +1,89 @@
+#include <X11/Xlib.h>
+#include <stdlib.h>
+#include <stdint.h>
+
+#define MAX(a, b) ((a) > (b) ? (a) : (b))
+
+struct {
+    Display* disp;
+    Window root;
+    void (*eventfns[LASTEvent])(XEvent*);
+} X;
+XWindowAttributes attr;
+XButtonEvent start;
+
+static void check(intptr_t stat)
+{
+    if (!stat)
+    {
+        exit(1);
+    }
+}
+
+void xkeypress(XEvent* ev)
+{
+    if (ev->xbutton.subwindow != None)
+    {
+        XRaiseWindow(X.disp, ev->xkey.subwindow);
+    }
+}
+
+void xbtnpress(XEvent* ev)
+{
+    if (ev->xbutton.subwindow != None)
+    {
+        XGrabPointer(X.disp, ev->xbutton.subwindow, True,
+                PointerMotionMask|ButtonReleaseMask, GrabModeAsync,
+                GrabModeAsync, None, None, CurrentTime);
+        XGetWindowAttributes(X.disp, ev->xbutton.subwindow, &attr);
+        start = ev->xbutton;
+    }
+}
+
+void xbtnrelease(XEvent* ev)
+{
+    XUngrabPointer(X.disp, CurrentTime);
+}
+
+void xmotionnotify(XEvent* ev)
+{
+    int xdiff, ydiff;
+    while (XCheckTypedEvent(X.disp, MotionNotify, ev));
+    xdiff = ev->xbutton.x_root - start.x_root;
+    ydiff = ev->xbutton.y_root - start.y_root;
+    XMoveResizeWindow(X.disp, 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)));
+}
+
+int main(int argc, char** argv)
+{
+    XWindowAttributes attr;
+    XButtonEvent start;
+    XEvent ev;
+
+    /* Initialize X server*/
+    check( !(X.disp = XOpenDisplay(0)) );
+    X.root = DefaultRootWindow(X.disp);
+    XGrabKey(X.disp, XKeysymToKeycode(X.disp, XStringToKeysym("F1")), Mod1Mask, X.root,
+            True, GrabModeAsync, GrabModeAsync);
+    XGrabButton(X.disp, 1, Mod1Mask, X.root, True, ButtonPressMask, GrabModeAsync,
+            GrabModeAsync, None, None);
+    XGrabButton(X.disp, 3, Mod1Mask, X.root, True, ButtonPressMask, GrabModeAsync,
+            GrabModeAsync, None, None);
+    X.eventfns[KeyPress] = xkeypress;
+    X.eventfns[ButtonPress] = xbtnpress;
+    X.eventfns[ButtonRelease] = xbtnrelease;
+    X.eventfns[MotionNotify] = xmotionnotify;
+
+    for (;;)
+    {
+        XNextEvent(X.disp, &ev);
+        if (X.eventfns[ev.type])
+        {
+            X.eventfns[ev.type](&ev);
+        }
+    }
+}
\ No newline at end of file
diff --git a/tiny.c b/tiny.c
new file mode 100644 (file)
index 0000000..01d07d1
--- /dev/null
+++ b/tiny.c
@@ -0,0 +1,89 @@
+#include <X11/Xlib.h>
+#include <stdlib.h>
+#include <stdint.h>
+
+#define MAX(a, b) ((a) > (b) ? (a) : (b))
+
+struct {
+    Display* disp;
+    Window root;
+    void (*eventfns[LASTEvent])(XEvent*);
+} X;
+XWindowAttributes attr;
+XButtonEvent start;
+
+static void check(intptr_t stat)
+{
+    if (!stat)
+    {
+        exit(1);
+    }
+}
+
+void xkeypress(XEvent* ev)
+{
+    if (ev->xbutton.subwindow != None)
+    {
+        XRaiseWindow(X.disp, ev->xkey.subwindow);
+    }
+}
+
+void xbtnpress(XEvent* ev)
+{
+    if (ev->xbutton.subwindow != None)
+    {
+        XGrabPointer(X.disp, ev->xbutton.subwindow, True,
+                PointerMotionMask|ButtonReleaseMask, GrabModeAsync,
+                GrabModeAsync, None, None, CurrentTime);
+        XGetWindowAttributes(X.disp, ev->xbutton.subwindow, &attr);
+        start = ev->xbutton;
+    }
+}
+
+void xbtnrelease(XEvent* ev)
+{
+    XUngrabPointer(X.disp, CurrentTime);
+}
+
+void xmotionnotify(XEvent* ev)
+{
+    int xdiff, ydiff;
+    while (XCheckTypedEvent(X.disp, MotionNotify, ev));
+    xdiff = ev->xbutton.x_root - start.x_root;
+    ydiff = ev->xbutton.y_root - start.y_root;
+    XMoveResizeWindow(X.disp, 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)));
+}
+
+int main(int argc, char** argv)
+{
+    XWindowAttributes attr;
+    XButtonEvent start;
+    XEvent ev;
+
+    /* Initialize X server*/
+    check( !(X.disp = XOpenDisplay(0)) );
+    X.root = DefaultRootWindow(X.disp);
+    XGrabKey(X.disp, XKeysymToKeycode(X.disp, XStringToKeysym("F1")), Mod1Mask, X.root,
+            True, GrabModeAsync, GrabModeAsync);
+    XGrabButton(X.disp, 1, Mod1Mask, X.root, True, ButtonPressMask, GrabModeAsync,
+            GrabModeAsync, None, None);
+    XGrabButton(X.disp, 3, Mod1Mask, X.root, True, ButtonPressMask, GrabModeAsync,
+            GrabModeAsync, None, None);
+    X.eventfns[KeyPress] = xkeypress;
+    X.eventfns[ButtonPress] = xbtnpress;
+    X.eventfns[ButtonRelease] = xbtnrelease;
+    X.eventfns[MotionNotify] = xmotionnotify;
+
+    for (;;)
+    {
+        XNextEvent(X.disp, &ev);
+        if (X.eventfns[ev.type])
+        {
+            X.eventfns[ev.type](&ev);
+        }
+    }
+}
\ No newline at end of file