Compare
51ccea63d66e8b7c5b540df6d53055a6bfa8440d..5502bb9bc77d4f0a7721f2cdecb8748962006f94
clwm-ext: warp-pointer-mixin
Raymond E. Pasco <ray@ameretat.dev> · 2026-10-27 09:23 UTC
7 files changed,
+233
−5
- clwm-ext.lisp
- clwm-ext/commands.lisp
- clwm-ext/mousebinds-mixin.lisp
- clwm-ext/pointer-keybinds-mixin.lisp
- clwm-ext/warp-pointer-mixin.lisp
- clwm.asd
- example-init.lisp
modified clwm-ext.lisp
| @@ -5,7 +5,7 @@ | |||
| 5 | 5 | (defpackage #:clwm-ext | |
| 6 | 6 | (:use :cl :clwm) | |
| 7 | 7 | (:import-from #:alexandria | |
| 8 | - | #:when-let #:lastcar #:format-symbol) | |
| 8 | + | #:when-let #:lastcar #:format-symbol #:clamp) | |
| 9 | 9 | (:export | |
| 10 | 10 | ;; sloppy-focus-follows-mouse-mixin | |
| 11 | 11 | #:sloppy-focus-follows-mouse-mixin | |
| @@ -20,4 +20,16 @@ | |||
| 20 | 20 | ;; bindings | |
| 21 | 21 | #:*lock-modifiers* #:*key-names* | |
| 22 | 22 | ;; keybinds-mixin | |
| 23 | - | #:keybinds-mixin #:key-bindings #:grab-keys #:call-key-command)) | |
| 23 | + | #:keybinds-mixin #:key-bindings #:grab-keys #:call-key-command | |
| 24 | + | ;; pointer-keybinds-mixin | |
| 25 | + | #:pointer-keybinds-mixin | |
| 26 | + | ;; pointer-keybinds respecting commands | |
| 27 | + | #:spawn-command #:close-command #:hide-command | |
| 28 | + | #:raise-command #:lower-command | |
| 29 | + | ;; mousebinds-mixin | |
| 30 | + | #:mousebinds-mixin #:mouse-bindings #:grab-buttons #:drag | |
| 31 | + | #:drag-function | |
| 32 | + | ;; mousebinds respecting commands | |
| 33 | + | #:drag-move #:drag-resize | |
| 34 | + | ;; warp-pointer-mixin | |
| 35 | + | #:warp-pointer-mixin #:warp-pointer-client #:client-pointer)) | |
added clwm-ext/commands.lisp mode 100644
| @@ -0,0 +1,69 @@ | |||
| 1 | + | ;;;; common lisp window manager | |
| 2 | + | ;;;; | |
| 3 | + | ;;;; forms intended to be executed as wm commands | |
| 4 | + | ||
| 5 | + | (in-package #:clwm-ext) | |
| 6 | + | ||
| 7 | + | ;;; respecting POINTER-KEYBINDS-MIXIN convention | |
| 8 | + | ||
| 9 | + | (defun spawn-command (program &rest args) | |
| 10 | + | "Spawns a program. | |
| 11 | + | Respects POINTER-KEYBINDS-MIXIN convention." | |
| 12 | + | (lambda (wm &rest pointer) | |
| 13 | + | (declare (ignore pointer)) | |
| 14 | + | (apply #'spawn wm program args))) | |
| 15 | + | ||
| 16 | + | (defun close-command (wm client x y) | |
| 17 | + | "Close the client under the pointer. | |
| 18 | + | Respects POINTER-KEYBINDS-MIXIN convention." | |
| 19 | + | (declare (ignore x y)) | |
| 20 | + | (when client (close-client wm client))) | |
| 21 | + | ||
| 22 | + | (defun hide-command (wm client x y) | |
| 23 | + | "Hide the client under the pointer. | |
| 24 | + | Respects POINTER-KEYBINDS-MIXIN convention." | |
| 25 | + | (declare (ignore x y)) | |
| 26 | + | (when client (hide-client wm client))) | |
| 27 | + | ||
| 28 | + | (defun raise-command (wm client x y) | |
| 29 | + | "Raise the client under the pointer. | |
| 30 | + | Respects POINTER-KEYBINDS-MIXIN convention." | |
| 31 | + | (declare (ignore x y)) | |
| 32 | + | (when client (configure-client wm client :stack-mode :above))) | |
| 33 | + | ||
| 34 | + | (defun lower-command (wm client x y) | |
| 35 | + | "Lower the client under the pointer. | |
| 36 | + | Respects POINTER-KEYBINDS-MIXIN convention." | |
| 37 | + | (declare (ignore x y)) | |
| 38 | + | (when client (configure-client wm client :stack-mode :below))) | |
| 39 | + | ||
| 40 | + | ;;; respecting MOUSEBINDS-MIXIN convention | |
| 41 | + | ||
| 42 | + | (defun grab-offset (pointer edge size) | |
| 43 | + | "Helper to determine where the pointer is along a client's edge." | |
| 44 | + | (clamp (- pointer edge) 0 (1- size))) | |
| 45 | + | ||
| 46 | + | (defun drag-move (wm client x y) | |
| 47 | + | "Raise the client and move it with the pointer. | |
| 48 | + | Respects MOUSEBINDS-MIXIN convention." | |
| 49 | + | (configure-client wm client :stack-mode :above) | |
| 50 | + | (multiple-value-bind (client-x client-y width height) | |
| 51 | + | (client-geometry wm client) | |
| 52 | + | (declare (ignore width height)) | |
| 53 | + | (let ((dx (- client-x x)) | |
| 54 | + | (dy (- client-y y))) | |
| 55 | + | (lambda (x y) | |
| 56 | + | (configure-client wm client :x (+ x dx) :y (+ y dy)))))) | |
| 57 | + | ||
| 58 | + | (defun drag-resize (wm client x y) | |
| 59 | + | "Raise the client and resize it with the pointer. | |
| 60 | + | Respects MOUSEBINDS-MIXIN convention." | |
| 61 | + | (configure-client wm client :stack-mode :above) | |
| 62 | + | (multiple-value-bind (client-x client-y width height) | |
| 63 | + | (client-geometry wm client) | |
| 64 | + | (declare (ignore client-x client-y)) | |
| 65 | + | ;; todo: resize from other corners than bottom right | |
| 66 | + | (lambda (new-x new-y) | |
| 67 | + | (configure-client wm client | |
| 68 | + | :width (+ width (- new-x x)) | |
| 69 | + | :height (+ height (- new-y y)))))) | |
added clwm-ext/mousebinds-mixin.lisp mode 100644
| @@ -0,0 +1,61 @@ | |||
| 1 | + | ;;;; common lisp window manager | |
| 2 | + | ;;;; | |
| 3 | + | ;;;; mouse bindings | |
| 4 | + | ;;;; the convention is (lambda (wm client x y) ...) | |
| 5 | + | ;;;; possibly returning a (lambda (x y) ...) | |
| 6 | + | ;;;; which is called on every move until the interaction ends | |
| 7 | + | ||
| 8 | + | (in-package #:clwm-ext) | |
| 9 | + | ||
| 10 | + | (defclass mousebinds-mixin (interaction-mixin) () | |
| 11 | + | (:documentation "Buttons (and modifiers) grabbed on the root | |
| 12 | + | to execute forms.")) | |
| 13 | + | ||
| 14 | + | (defclass drag (interaction) | |
| 15 | + | ((function :initarg :function :reader drag-function)) | |
| 16 | + | (:documentation "Call a function on pointer motion until release.")) | |
| 17 | + | ||
| 18 | + | (defgeneric mouse-bindings (wm) | |
| 19 | + | (:documentation "Bindings of (modifiers... button) to functions.") | |
| 20 | + | (:method-combination append)) | |
| 21 | + | (defgeneric grab-buttons (wm) | |
| 22 | + | (:documentation | |
| 23 | + | "Grab the buttons of the mouse bindings on the root.")) | |
| 24 | + | ||
| 25 | + | (defmethod mouse-bindings append ((wm mousebinds-mixin)) '()) | |
| 26 | + | ||
| 27 | + | (defmethod grab-buttons ((wm mousebinds-mixin)) | |
| 28 | + | (xlib:ungrab-button (root wm) :any :modifiers :any) | |
| 29 | + | (loop for (spec) in (mouse-bindings wm) | |
| 30 | + | do (dolist (mask (binding-grab-masks spec)) | |
| 31 | + | (xlib:grab-button (root wm) (binding-key spec) | |
| 32 | + | '(:button-press :button-release | |
| 33 | + | :pointer-motion) | |
| 34 | + | :modifiers mask)))) | |
| 35 | + | ||
| 36 | + | (defmethod initialize-wm :after ((wm mousebinds-mixin)) | |
| 37 | + | (grab-buttons wm)) | |
| 38 | + | ||
| 39 | + | (defmethod handle-client-event | |
| 40 | + | ((wm mousebinds-mixin) client (event-key (eql :button-press)) | |
| 41 | + | &key code state root-x root-y) | |
| 42 | + | (let ((command (find-binding (mouse-bindings wm) state code))) | |
| 43 | + | (if command | |
| 44 | + | (let ((function (funcall command wm client root-x root-y))) | |
| 45 | + | (when (functionp function) | |
| 46 | + | (start-interaction | |
| 47 | + | wm | |
| 48 | + | (make-instance 'drag :client client :function function)))) | |
| 49 | + | (call-next-method)))) | |
| 50 | + | ||
| 51 | + | (defmethod interaction-event | |
| 52 | + | ((wm interaction-mixin) (drag drag) (event-key (eql :motion-notify)) | |
| 53 | + | &key root-x root-y) | |
| 54 | + | (funcall (drag-function drag) root-x root-y) | |
| 55 | + | t) | |
| 56 | + | ||
| 57 | + | (defmethod interaction-event | |
| 58 | + | ((wm interaction-mixin) (drag drag) | |
| 59 | + | (event-key (eql :button-release)) &key) | |
| 60 | + | (stop-interaction wm drag) | |
| 61 | + | t) | |
added clwm-ext/pointer-keybinds-mixin.lisp mode 100644
| @@ -0,0 +1,14 @@ | |||
| 1 | + | ;;;; common lisp window manager | |
| 2 | + | ;;;; | |
| 3 | + | ;;;; keybinds that know where the pointer is | |
| 4 | + | ;;;; the convention is (lambda (wm client x y) ...) | |
| 5 | + | ||
| 6 | + | (in-package #:clwm-ext) | |
| 7 | + | ||
| 8 | + | (defclass pointer-keybinds-mixin (keybinds-mixin) () | |
| 9 | + | (:documentation "Keybinds which know where the pointer is, | |
| 10 | + | and what client is under it.")) | |
| 11 | + | ||
| 12 | + | (defmethod call-key-command ((wm pointer-keybinds-mixin) command | |
| 13 | + | &rest e &key root-x root-y) | |
| 14 | + | (funcall command wm (apply #'event-client wm :key-press e) root-x root-y)) | |
added clwm-ext/warp-pointer-mixin.lisp mode 100644
| @@ -0,0 +1,58 @@ | |||
| 1 | + | ;;;; common lisp window manager | |
| 2 | + | ;;;; | |
| 3 | + | ;;;; save a pointer warp position on every client and use on focus | |
| 4 | + | ||
| 5 | + | (in-package #:clwm-ext) | |
| 6 | + | ||
| 7 | + | (defclass warp-pointer-mixin () () | |
| 8 | + | (:documentation "Focusing a client by means other than moving the | |
| 9 | + | pointer warps the pointer to where it last was on that client (falling | |
| 10 | + | back to the center).")) | |
| 11 | + | ||
| 12 | + | (defclass warp-pointer-client () | |
| 13 | + | ((pointer :initform nil :accessor client-pointer | |
| 14 | + | :documentation | |
| 15 | + | "Where the pointer last was on the client, or NIL.")) | |
| 16 | + | (:documentation "A client that remembers where the pointer last was | |
| 17 | + | on it.")) | |
| 18 | + | ||
| 19 | + | (defmethod client-mixins append ((wm warp-pointer-mixin)) | |
| 20 | + | '(warp-pointer-client)) | |
| 21 | + | ||
| 22 | + | (defun pointer-client (wm) | |
| 23 | + | "Helper to find the client the pointer is on and the pointer's | |
| 24 | + | coordinates." | |
| 25 | + | (multiple-value-bind (x y same-screen-p child) | |
| 26 | + | (xlib:query-pointer (root wm)) | |
| 27 | + | (declare (ignore same-screen-p)) | |
| 28 | + | (values (and child (find-client wm child)) x y))) | |
| 29 | + | ||
| 30 | + | (defun remember-pointer (wm client x y) | |
| 31 | + | "Remember where the pointer was on a client. | |
| 32 | + | Expects WARP-POINTER-MIXIN." | |
| 33 | + | (multiple-value-bind (client-x client-y) (client-geometry wm client) | |
| 34 | + | (setf (client-pointer client) | |
| 35 | + | (list (- x client-x) (- y client-y))))) | |
| 36 | + | ||
| 37 | + | (defun warp-to-client (wm client) | |
| 38 | + | "Warp the pointer to where it last was on a client, or fall back | |
| 39 | + | to the center. | |
| 40 | + | Expects WARP-POINTER-MIXIN." | |
| 41 | + | (multiple-value-bind (x y width height) (client-geometry wm client) | |
| 42 | + | (destructuring-bind (&optional px py) (client-pointer client) | |
| 43 | + | (unless (and px (< -1 px width) (< -1 py height)) | |
| 44 | + | (setf px (floor width 2) | |
| 45 | + | py (floor height 2))) | |
| 46 | + | (xlib:warp-pointer (root wm) (+ x px) (+ y py))))) | |
| 47 | + | ||
| 48 | + | (defmethod focus-client :after ((wm warp-pointer-mixin) client) | |
| 49 | + | (multiple-value-bind (under x y) (pointer-client wm) | |
| 50 | + | (unless (eql under client) | |
| 51 | + | (when under | |
| 52 | + | (remember-pointer wm under x y)) | |
| 53 | + | (warp-to-client wm client)))) | |
| 54 | + | ||
| 55 | + | (defmethod hide-client :before ((wm warp-pointer-mixin) client) | |
| 56 | + | (multiple-value-bind (under x y) (pointer-client wm) | |
| 57 | + | (when (eql under client) | |
| 58 | + | (remember-pointer wm client x y)))) | |
modified clwm.asd
| @@ -19,5 +19,9 @@ | |||
| 19 | 19 | (:file "ignored-clients-mixin") | |
| 20 | 20 | (:file "interaction-mixin") | |
| 21 | 21 | (:file "bindings") | |
| 22 | - | (:file "keybinds-mixin"))) | |
| 22 | + | (:file "keybinds-mixin") | |
| 23 | + | (:file "pointer-keybinds-mixin") | |
| 24 | + | (:file "commands") | |
| 25 | + | (:file "mousebinds-mixin") | |
| 26 | + | (:file "warp-pointer-mixin"))) | |
| 23 | 27 | (:file "clwm-user"))) | |
modified example-init.lisp
| @@ -4,7 +4,9 @@ | |||
| 4 | 4 | ||
| 5 | 5 | (in-package #:clwm-user) | |
| 6 | 6 | ||
| 7 | - | (defclass my-wm (keybinds-mixin | |
| 7 | + | (defclass my-wm (warp-pointer-mixin | |
| 8 | + | mousebinds-mixin | |
| 9 | + | pointer-keybinds-mixin | |
| 8 | 10 | ignored-clients-mixin | |
| 9 | 11 | focus-tracking-mixin | |
| 10 | 12 | sloppy-focus-follows-mouse-mixin | |
| @@ -12,7 +14,15 @@ | |||
| 12 | 14 | ()) | |
| 13 | 15 | ||
| 14 | 16 | (defmethod key-bindings append ((wm my-wm)) | |
| 15 | - | `(((:control :mod-1 #\Return) . ,(lambda (wm) (spawn wm "xterm"))))) | |
| 17 | + | `(((:control :mod-1 #\Return) . ,(spawn-command "xterm")) | |
| 18 | + | ((:control :mod-1 #\x) . close-command) | |
| 19 | + | ((:mod-1 #\Return) . hide-command) | |
| 20 | + | ((:mod-1 :up) . raise-command) | |
| 21 | + | ((:mod-1 :down) . lower-command))) | |
| 22 | + | ||
| 23 | + | (defmethod mouse-bindings append ((wm my-wm)) | |
| 24 | + | '(((:mod-1 1) . drag-move) | |
| 25 | + | ((:mod-1 3) . drag-resize))) | |
| 16 | 26 | ||
| 17 | 27 | (run-wm 'my-wm) | |
| 18 | 28 | ||