Commit

a0296e8f7685b3c487dbf75f257dffdb21f81d27

clwm-ext: mousebinds-mixin

Raymond E. Pasco <ray@ameretat.dev> · 2026-10-27 09:10 UTC

Browse files
5 files changed, +108 −5
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
@@ -23,6 +23,11 @@
23 23 #:keybinds-mixin #:key-bindings #:grab-keys #:call-key-command
24 24 ;; pointer-keybinds-mixin
25 25 #:pointer-keybinds-mixin
26 - ;; commands
26 + ;; pointer-keybinds respecting commands
27 27 #:spawn-command #:close-command #:hide-command
28 - #:raise-command #:lower-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))
modified clwm-ext/commands.lisp
@@ -36,3 +36,34 @@ Respects POINTER-KEYBINDS-MIXIN convention."
36 36 Respects POINTER-KEYBINDS-MIXIN convention."
37 37 (declare (ignore x y))
38 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)
modified clwm.asd
@@ -21,5 +21,6 @@
21 21 (:file "bindings")
22 22 (:file "keybinds-mixin")
23 23 (:file "pointer-keybinds-mixin")
24 - (:file "commands")))
24 + (:file "commands")
25 + (:file "mousebinds-mixin")))
25 26 (:file "clwm-user")))
modified example-init.lisp
@@ -4,7 +4,8 @@
4 4
5 5 (in-package #:clwm-user)
6 6
7 - (defclass my-wm (pointer-keybinds-mixin
7 + (defclass my-wm (mousebinds-mixin
8 + pointer-keybinds-mixin
8 9 ignored-clients-mixin
9 10 focus-tracking-mixin
10 11 sloppy-focus-follows-mouse-mixin
@@ -18,6 +19,10 @@
18 19 ((:mod-1 :up) . raise-command)
19 20 ((:mod-1 :down) . lower-command)))
20 21
22 + (defmethod mouse-bindings append ((wm my-wm))
23 + '(((:mod-1 1) . drag-move)
24 + ((:mod-1 3) . drag-resize)))
25 +
21 26 (run-wm 'my-wm)
22 27
23 28 ;; if slynk is desired