Compare

2478bf1189f2c104246182c7cdbf3aafdc618dcd..51ccea63d66e8b7c5b540df6d53055a6bfa8440d

clwm-ext: keybinds-mixin

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

Browse files
5 files changed, +141 −4
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)
8 + #:when-let #:lastcar #:format-symbol)
9 9 (:export
10 10 ;; sloppy-focus-follows-mouse-mixin
11 11 #:sloppy-focus-follows-mouse-mixin
@@ -16,4 +16,8 @@
16 16 ;; interaction mixin
17 17 #:interaction-mixin #:current-interaction #:interaction
18 18 #:interaction-client #:start-interaction #:stop-interaction
19 - #:interaction-event #:stop-current-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))
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/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))
modified clwm.asd
@@ -17,5 +17,7 @@
17 17 :components ((:file "sloppy-focus-follows-mouse-mixin")
18 18 (:file "focus-tracking-mixin")
19 19 (:file "ignored-clients-mixin")
20 - (:file "interaction-mixin")))
20 + (:file "interaction-mixin")
21 + (:file "bindings")
22 + (:file "keybinds-mixin")))
21 23 (:file "clwm-user")))
modified example-init.lisp
@@ -4,12 +4,16 @@
4 4
5 5 (in-package #:clwm-user)
6 6
7 - (defclass my-wm (ignored-clients-mixin
7 + (defclass my-wm (keybinds-mixin
8 + ignored-clients-mixin
8 9 focus-tracking-mixin
9 10 sloppy-focus-follows-mouse-mixin
10 11 wm)
11 12 ())
12 13
14 + (defmethod key-bindings append ((wm my-wm))
15 + `(((:control :mod-1 #\Return) . ,(lambda (wm) (spawn wm "xterm")))))
16 +
13 17 (run-wm 'my-wm)
14 18
15 19 ;; if slynk is desired