From 8179c0025a44a769ab6ad2e5c0a58f545517280c Mon Sep 17 00:00:00 2001 From: Case Duckworth Date: Wed, 17 Jan 2024 00:24:11 -0600 Subject: Change organization and massively refactor --- lib/events.sls | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 lib/events.sls (limited to 'lib/events.sls') diff --git a/lib/events.sls b/lib/events.sls new file mode 100644 index 0000000..9bb78ee --- /dev/null +++ b/lib/events.sls @@ -0,0 +1,83 @@ +;;; (acdwm events) --- event handlers + +(import (acdwm keys) + (acdwm xlib) + (acdwm util) + (srfi 1)) + +(export make-drag drag? drag-type drag-event drag-window drag-attrs) +(define-record drag type event window attrs) + +(define drag (make-parameter #f)) +(define drag-types '(move resize)) + +(define-public (initialize-drag type event) + (assert (memq type drag-types)) + (let ((win (and (buttonpress? event) + (xevent-xbutton-subwindow event)))) + (and win + (drag (make-drag type event win (get-window-attributes win)))))) + +(define-public (cancel-drag) + (free-xevent (drag-event (drag))) + (free-xwindowattributes (drag-attrs (drag))) + (drag #f)) + +(define-public (handle-events #!optional (dpy (get-display))) + (let loop ((this (get-next-event dpy))) + (dprint "handling a " (xevent-type-name (xevent-type this))) + (cond + ((keypress? this) + (handle-keypress this dpy) + (loop (get-next-event dpy))) + ((buttonpress? this) + (handle-buttonpress this) + (loop (get-next-event dpy))) + ((buttonrelease? this) + (handle-buttonrelease this) + (loop (get-next-event dpy))) + ((motionnotify? this) + (handle-motion this) + (loop (get-next-event dpy))) + ;; Make sure this is at the end! + (else + (loop (get-next-event dpy)))))) + +;;; Handlers + +(define-public (handle-keypress event + #!optional (dpy (get-display))) + (let ((binding (find-keybinding event))) + (dprint "handle-keypress: " binding) + (and binding ((keybind-proc binding) event dpy)))) + +(define-public (handle-buttonpress event + #!optional (dpy (get-display))) + (let ((binding (find-mousebinding event))) + (dprint "handle-buttonpress: " binding) + (and binding ((mousebind-proc binding) event dpy)))) + +(define-public (handle-buttonrelease event + #!optional (dpy (get-display))) + (dprint "handle-buttonrelease") + (cancel-drag)) + +(define-public (handle-motion event + #!optional (dpy (get-display))) + (dprint "handle-motion") + (when (drag) + (when (some? (drag-window (drag))) + (let ((xd (- (xevent-xbutton-x_root event) + (xevent-xbutton-x_root (drag-event (drag))))) + (yd (- (xevent-xbutton-y_root event) + (xevent-xbutton-y_root (drag-event (drag)))))) + (let ((xnew (+ (xwindowattributes-x (drag-attrs (drag))) + (if (eq? (drag-type (drag)) 'move) xd 0))) + (ynew (+ (xwindowattributes-y (drag-attrs (drag))) + (if (eq? (drag-type (drag)) 'move) yd 0))) + (wnew (max 1 (+ (xwindowattributes-width (drag-attrs (drag))) + (if (eq? (drag-type (drag)) 'resize) xd 0)))) + (hnew (max 1 (+ (xwindowattributes-height (drag-attrs (drag))) + (if (eq? (drag-type (drag)) 'resize) yd 0))))) + (dprint wnew "x" hnew "+" xnew "+" ynew) + (xmoveresizewindow dpy (drag-window (drag)) xnew ynew wnew hnew)))))) -- cgit 1.4.1-21-gabe81