at commit 6b56d7d8

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))