Compare

0c2f6b6f77fc5ebb35e03b43ec9db72988506a57..a0296e8f7685b3c487dbf75f257dffdb21f81d27

clwm-ext: mousebinds-mixin

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

Browse files
10 files changed, +410 −3
modified clwm-ext.lisp
@@ -4,8 +4,30 @@
4 4
5 5 (defpackage #:clwm-ext
6 6 (:use :cl :clwm)
7 + (:import-from #:alexandria
8 + #:when-let #:lastcar #:format-symbol #:clamp)
7 9 (:export
8 10 ;; sloppy-focus-follows-mouse-mixin
9 11 #:sloppy-focus-follows-mouse-mixin
10 12 ;; focus-tracking-mixin
11 - #:focus-tracking-mixin #:focused-client))
13 + #:focus-tracking-mixin #:focused-client
14 + ;; ignored-clients-mixin
15 + #:ignored-clients-mixin #:ignore-window-p #:ignored-window-names
16 + ;; interaction mixin
17 + #:interaction-mixin #:current-interaction #:interaction
18 + #:interaction-client #:start-interaction #:stop-interaction
19 + #:interaction-event #:stop-current-interaction
20 + ;; bindings
21 + #:*lock-modifiers* #:*key-names*
22 + ;; keybinds-mixin
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))
added clwm-ext/bindings.lisp mode 100644
@@ -0,0 +1,72 @@
1 + ;;;; common lisp window manager
2 + ;;;;
3 + ;;;; helpers for key and mouse bindings
4 +
5 + (in-package #:clwm-ext)
6 +
7 + ;; the overall binding shape is ('(modifiers... key) . function)
8 + ;; this depends on xmodmap and stuff, but what's probably the case is:
9 + ;; :control = ctrl
10 + ;; :shift = shift
11 + ;; :mod-1 = alt
12 + ;; :mod-2 = num lock (!)
13 + ;; :mod-4 = that additional modifier key
14 + ;; :lock = scroll lock
15 + ;; and we don't want scroll lock or num lock to affect keybinds.
16 + ;; this is a parameter not a gf because if it's not constant it's deep
17 + ;; in the user keyboard weeds.
18 + (defparameter *lock-modifiers* '(:lock :mod-2)
19 + "Modifiers that bindings should always ignore.")
20 +
21 + ;; name some keys that CLX doesn't name.
22 + (defparameter *key-names*
23 + (append '((:backspace . #xff08) (:tab . #xff09) (:return . #xff0d)
24 + (:pause . #xff13) (:escape . #xff1b) (:delete . #xffff)
25 + (:home . #xff50) (:left . #xff51) (:up . #xff52)
26 + (:right . #xff53) (:down . #xff54) (:page-up . #xff55)
27 + (:page-down . #xff56) (:end . #xff57) (:print . #xff61)
28 + (:insert . #xff63) (:menu . #xff67) (:space . #x20))
29 + (loop for n from 1 to 24
30 + collect (cons
31 + (format-symbol :keyword "F~d" n)
32 + (+ #xffbd n))))
33 + "Keyword names for keys we can't name with escapes.")
34 +
35 + (defun binding-modifier-mask (spec)
36 + "The X state mask for the modifiers in a binding spec."
37 + (apply #'xlib:make-state-mask (butlast spec)))
38 +
39 + (defun binding-key (spec)
40 + "The key part of a binding spec."
41 + (lastcar spec))
42 +
43 + (defun binding-grab-masks (spec)
44 + "The modifier masks to grab for a binding spec,
45 + ignoring *LOCK-MODIFIERS*."
46 + (reduce
47 + (lambda (masks modifier)
48 + (append masks
49 + (mapcar (lambda (mask)
50 + (logior mask (xlib:make-state-mask modifier)))
51 + masks)))
52 + *lock-modifiers*
53 + :initial-value (list (binding-modifier-mask spec))))
54 +
55 + (defun find-binding (bindings state key &key (test #'eql))
56 + "The command bound to a key under an event's modifier state."
57 + (let ((mask
58 + (logandc2 (logand state #xff)
59 + (apply #'xlib:make-state-mask *lock-modifiers*))))
60 + (cdr (find-if (lambda (spec)
61 + (and (funcall test key (binding-key spec))
62 + (= (binding-modifier-mask spec) mask)))
63 + bindings
64 + :key #'car))))
65 +
66 + (defun key-keysyms (key display)
67 + "Turn a key from a binding into a keysym on the display."
68 + (etypecase key
69 + (character (xlib:character->keysyms key display))
70 + (keyword (list (or (cdr (assoc key *key-names*))
71 + (error "No key named ~s in *KEY-NAMES*." key))))
72 + (integer (list key))))
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/ignored-clients-mixin.lisp mode 100644
@@ -0,0 +1,32 @@
1 + ;;;; common lisp window manager
2 + ;;;;
3 + ;;;; ignore some clients
4 +
5 + (in-package #:clwm-ext)
6 +
7 + (defclass ignored-clients-mixin () ()
8 + (:documentation "Leave some windows unmanaged."))
9 +
10 + (defgeneric ignore-window-p (wm window)
11 + (:documentation "Whether to leave a window unmanaged.")
12 + (:method-combination or))
13 + (defgeneric ignored-window-names (wm)
14 + (:documentation "Names to match against WM_CLASS and WM_NAME,
15 + and ignore if they match.")
16 + (:method-combination append))
17 +
18 + (defmethod ignore-window-p or ((wm ignored-clients-mixin) window)
19 + (let ((names (ignored-window-names wm)))
20 + (and names
21 + (multiple-value-bind
22 + (instance class) (xlib:get-wm-class window)
23 + (let ((window-names
24 + (list instance class (xlib:wm-name window))))
25 + (some (lambda (name)
26 + (member name window-names :test #'equal))
27 + names))))))
28 + (defmethod ignored-window-names append ((wm ignored-clients-mixin))
29 + '())
30 +
31 + (defmethod manage-window-p and ((wm ignored-clients-mixin) window)
32 + (not (ignore-window-p wm window)))
added clwm-ext/interaction-mixin.lisp mode 100644
@@ -0,0 +1,61 @@
1 + ;;;; common lisp window manager
2 + ;;;;
3 + ;;;; modal interactions that get all events
4 +
5 + (in-package #:clwm-ext)
6 +
7 + (defclass interaction-mixin ()
8 + ((interaction :initform nil :accessor current-interaction
9 + :documentation "The current modal interaction or NIL."))
10 + (:documentation
11 + "Allow a single modal interaction, which may get first refusal on
12 + all events."))
13 +
14 + (defclass interaction ()
15 + ((client :initarg :client :initform nil :reader interaction-client))
16 + (:documentation
17 + "A modal interaction which may get first refusal on all events."))
18 +
19 + (defgeneric start-interaction (wm interaction)
20 + (:documentation "Make an interaction the WM's current interaction."))
21 + (defgeneric stop-interaction (wm interaction)
22 + (:documentation "Stop an interaction if it's the current one."))
23 + (defgeneric interaction-event
24 + (wm interaction event-key &key &allow-other-keys)
25 + (:documentation "Offer an event to an interaction.
26 + Non-NIL return consumes the event.")
27 + (:method (wm interaction event-key &key) nil))
28 +
29 + (defun stop-current-interaction (wm)
30 + "End the current interaction, whatever it may or may not be.
31 + Expects INTERACTION-MIXIN."
32 + (when-let ((interaction (current-interaction wm)))
33 + (stop-interaction wm interaction)))
34 +
35 + (defmethod start-interaction
36 + ((wm interaction-mixin) (interaction interaction))
37 + (stop-current-interaction wm)
38 + (setf (current-interaction wm) interaction))
39 + (defmethod stop-interaction
40 + ((wm interaction-mixin) (interaction interaction))
41 + (when (eql interaction (current-interaction wm))
42 + (setf (current-interaction wm) nil)))
43 +
44 + (defmethod handle-event :around
45 + ((wm interaction-mixin) event-key &rest e &key)
46 + (let ((interaction (current-interaction wm)))
47 + (unless (and interaction
48 + (apply #'interaction-event wm interaction event-key e))
49 + (call-next-method))))
50 +
51 + (defmethod unmanage-client :before ((wm interaction-mixin) client)
52 + (let ((interaction (current-interaction wm)))
53 + (when (and interaction
54 + (eql client (interaction-client interaction)))
55 + (stop-interaction wm interaction))))
56 +
57 + (defmethod uninitialize-wm :before ((wm interaction-mixin))
58 + (stop-current-interaction wm))
59 +
60 + (defmethod shutdown-wm :before ((wm interaction-mixin))
61 + (stop-current-interaction wm))
added clwm-ext/keybinds-mixin.lisp mode 100644
@@ -0,0 +1,55 @@
1 + ;;;; common lisp window manager
2 + ;;;;
3 + ;;;; keybinds.
4 + ;;;; the base convention is (lambda (wm) ...)
5 +
6 + (in-package #:clwm-ext)
7 +
8 + (defclass keybinds-mixin () ()
9 + (:documentation "Keys grabbed on the root to execute forms."))
10 +
11 + (defgeneric key-bindings (wm)
12 + (:documentation "Bindings from keybinding specs to functions.")
13 + (:method-combination append))
14 + (defgeneric grab-keys (wm)
15 + (:documentation "Grab the keys of the key bindings on the root."))
16 + (defgeneric call-key-command (wm command &key &allow-other-keys)
17 + (:documentation "Run a key binding's command."))
18 +
19 + (defmethod key-bindings append ((wm keybinds-mixin)) '())
20 +
21 + (defmethod grab-keys ((wm keybinds-mixin))
22 + (xlib:ungrab-key (root wm) :any :modifiers :any)
23 + (loop for (spec) in (key-bindings wm) do
24 + (dolist (keysym (key-keysyms (binding-key spec) (display wm)))
25 + (dolist (code (multiple-value-list
26 + (xlib:keysym->keycodes (display wm) keysym)))
27 + (dolist (mask (binding-grab-masks spec))
28 + (xlib:grab-key (root wm) code :modifiers mask))))))
29 +
30 + (defmethod initialize-wm :after ((wm keybinds-mixin))
31 + (grab-keys wm))
32 +
33 + (defmethod call-key-command ((wm keybinds-mixin) command &key)
34 + (funcall command wm))
35 +
36 + (defmethod handle-event
37 + ((wm keybinds-mixin) (event-key (eql :key-press))
38 + &rest e &key code state)
39 + (let ((command (find-binding
40 + (key-bindings wm) state
41 + (xlib:keycode->keysym (display wm) code 0)
42 + :test
43 + (lambda (keysym key)
44 + (member keysym (key-keysyms key (display wm)))))))
45 + (if command
46 + (apply #'call-key-command wm command e)
47 + (call-next-method))))
48 +
49 + (defmethod handle-event
50 + ((wm keybinds-mixin) (event-key (eql :mapping-notify))
51 + &key request start count)
52 + (xlib:mapping-notify (display wm) request start count)
53 + (when (eql request :keyboard)
54 + (grab-keys wm))
55 + (call-next-method))
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))
modified clwm.asd
@@ -15,5 +15,12 @@
15 15 :pathname "clwm-ext"
16 16 :serial t
17 17 :components ((:file "sloppy-focus-follows-mouse-mixin")
18 - (:file "focus-tracking-mixin")))
18 + (:file "focus-tracking-mixin")
19 + (:file "ignored-clients-mixin")
20 + (:file "interaction-mixin")
21 + (:file "bindings")
22 + (:file "keybinds-mixin")
23 + (:file "pointer-keybinds-mixin")
24 + (:file "commands")
25 + (:file "mousebinds-mixin")))
19 26 (:file "clwm-user")))
modified example-init.lisp
@@ -4,11 +4,25 @@
4 4
5 5 (in-package #:clwm-user)
6 6
7 - (defclass my-wm (focus-tracking-mixin
7 + (defclass my-wm (mousebinds-mixin
8 + pointer-keybinds-mixin
9 + ignored-clients-mixin
10 + focus-tracking-mixin
8 11 sloppy-focus-follows-mouse-mixin
9 12 wm)
10 13 ())
11 14
15 + (defmethod key-bindings append ((wm my-wm))
16 + `(((:control :mod-1 #\Return) . ,(spawn-command "xterm"))
17 + ((:control :mod-1 #\x) . close-command)
18 + ((:mod-1 #\Return) . hide-command)
19 + ((:mod-1 :up) . raise-command)
20 + ((:mod-1 :down) . lower-command)))
21 +
22 + (defmethod mouse-bindings append ((wm my-wm))
23 + '(((:mod-1 1) . drag-move)
24 + ((:mod-1 3) . drag-resize)))
25 +
12 26 (run-wm 'my-wm)
13 27
14 28 ;; if slynk is desired