at commit a82c185d

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 (defun abort-current-interaction (wm)
45 "Tear down the current interaction after an error, best-effort.
46 Releases grabs and flushes, so a debugger can be used afterwards.
47 Expects INTERACTION-MIXIN."
48 (ignore-errors (stop-current-interaction wm))
49 ;; in case stop-interaction itself died partway
50 (ignore-errors (xlib:ungrab-keyboard (display wm))
51 (xlib:ungrab-pointer (display wm)))
52 ;; CLX buffers requests; flush before we block in a debugger
53 (ignore-errors (xlib:display-finish-output (display wm))))
54
55 (defmacro with-interaction-abort-on-error ((wm) &body body)
56 "Abort WM's current interaction on any error in BODY, then decline,
57 so outer handlers (SKIP-EVENT, the debugger) still see the error."
58 (let ((condition (gensym "CONDITION")))
59 `(handler-bind ((error (lambda (,condition)
60 (declare (ignore ,condition))
61 (abort-current-interaction ,wm))))
62 ,@body)))
63
64 (defmethod handle-event :around
65 ((wm interaction-mixin) event-key &rest e &key)
66 (with-interaction-abort-on-error (wm)
67 (let ((interaction (current-interaction wm)))
68 (unless (and interaction
69 (apply #'interaction-event
70 wm interaction event-key e))
71 (call-next-method)))))
72
73 ;; async X errors often surface outside HANDLE-EVENT, in the event loop
74 (defmethod handle-x-error :around
75 ((wm interaction-mixin) error-key &key)
76 (with-interaction-abort-on-error (wm)
77 (call-next-method)))
78
79 (defmethod unmanage-client :before ((wm interaction-mixin) client)
80 (let ((interaction (current-interaction wm)))
81 (when (and interaction
82 (eql client (interaction-client interaction)))
83 (stop-interaction wm interaction))))
84
85 (defmethod uninitialize-wm :before ((wm interaction-mixin))
86 (stop-current-interaction wm))
87
88 (defmethod shutdown-wm :before ((wm interaction-mixin))
89 (stop-current-interaction wm))