forked from admich/Doors
-
Notifications
You must be signed in to change notification settings - Fork 1
/
input.lisp
256 lines (246 loc) · 12.7 KB
/
input.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
;;;; Copyright (C) 2020 Andrea De Michele
;;;;
;;;; This library is free software; you can redistribute it and/or
;;;; modify it under the terms of the GNU Lesser General Public
;;;; License as published by the Free Software Foundation; either
;;;; version 2.1 of the License, or (at your option) any later version.
;;;;
;;;; This library is distributed in the hope that it will be useful,
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;;;; Lesser General Public License for more details.
;;;;
;;;; You should have received a copy of the GNU Lesser General Public
;;;; License along with this library; if not, write to the Free Software
;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
;;;; USA
(in-package :clim-doors)
(climi::define-event-class window-manager-configuration-request-event (window-manager-event)
((window :initarg :window :reader window-manager-configuration-request-event-window)
(x :initarg :x :reader window-manager-configuration-request-event-native-x)
(y :initarg :y :reader window-manager-configuration-request-event-native-y)
(width :initarg :width :reader window-manager-configuration-request-event-width)
(height :initarg :height :reader window-manager-configuration-request-event-height)))
(climi::define-event-class window-manager-map-request-event (window-manager-event)
((window :initarg :window :reader window-manager-map-request-event-window)))
;;; change dispatch and then handle-event
(defmethod distribute-event ((port doors-port) (event window-manager-configuration-request-event))
(grant-configure-request event))
(defmethod distribute-event ((port doors-port) (event keyboard-event))
(let ((sheet (event-sheet event)))
(if (member sheet (list (graft sheet) (frame-query-io *wm-application*)))
(dispatch-event (frame-query-io *wm-application*) event)
(dispatch-event (port-keyboard-input-focus port) event))))
(defvar *doors-port*)
(defvar *wait-function*)
(defmethod process-next-event ((port doors-port) &key wait-function (timeout nil))
(let ((*doors-port* port)
(*wait-function* wait-function))
(let ((event (xlib:process-event (clx-port-display port)
:timeout timeout
:handler #'event-handler
:discard-p t
:force-output-p t)))
(case event
((nil) (values nil :timeout))
((t) (values nil :wait-function))
(otherwise
(prog1 t (distribute-event port event)))))))
(defun event-handler (&key display event-window window kind event-key code state mode time
type width height x y root-x root-y
data override-redirect-p send-event-p
target property requestor selection
request first-keycode count value-mask child
&allow-other-keys)
(declare (ignore first-keycode count))
(when (eql event-key :configure-request)
(let ((sheet (and window
(or (port-lookup-sheet *doors-port* window)
(port-lookup-foreign-sheet *doors-port* window)))))
(return-from event-handler
(make-instance 'window-manager-configuration-request-event
:sheet (or sheet (find-graft))
:window window
:x (and (= 1 (logand value-mask 1)) x)
:y (and (= 2 (logand value-mask 2)) y)
:width (and (= 4 (logand value-mask 4)) width)
:height (and (= 8 (logand value-mask 8)) height)))))
(when (eql event-key :mapping-notify)
(xlib:mapping-notify display request 0 0)
(return-from event-handler (maybe-funcall *wait-function*)))
(when (eql event-key :map-request)
(unless (port-lookup-foreign-sheet *doors-port* window)
(make-foreign-application window))
(return-from event-handler (maybe-funcall *wait-function*)))
(when-let ((sheet (and window
(or (port-lookup-sheet *doors-port* window)
(port-lookup-foreign-sheet *doors-port* window)))))
(case event-key
((:key-press :key-release)
(multiple-value-bind (keyname modifier-state keysym-name)
(clim-clx::x-event-to-key-name-and-modifiers *doors-port*
event-key code state)
(make-instance (if (eq event-key :key-press)
'key-press-event
'key-release-event)
:key-name keysym-name
:key-character (and (characterp keyname) keyname)
:x x :y y
:graft-x root-x
:graft-y root-y
:sheet (or (and (graftp sheet) (frame-query-io *wm-application*))
(frame-properties (pane-frame sheet) 'focus) sheet)
:modifier-state modifier-state :timestamp time)))
((:button-press :button-release)
;; :button-press on a foreign-application change the focus on
;; that application and the click is replay on the foreign
;; application.
(when (typep sheet 'foreign-application-pane)
(xlib:allow-events display :replay-pointer time)
(when (not (eq (active-frame *doors-port*) (pane-frame sheet)))
(setf (active-frame *doors-port*) (pane-frame sheet)))
(return-from event-handler (maybe-funcall *wait-function*)))
(let ((modifier-state (clim-xcommon:x-event-state-modifiers *doors-port* state))
(button (clim-clx::decode-x-button-code code)))
(if (member button '(#.+pointer-wheel-up+
#.+pointer-wheel-down+
#.+pointer-wheel-left+
#.+pointer-wheel-right+))
;; Pointer scroll generates button press and button
;; release event. We ignore the latter. -- jd 2019-09-01
(when (eq event-key :button-press)
(make-instance 'climi::pointer-scroll-event
:pointer 0
:button button :x x :y y
:graft-x root-x
:graft-y root-y
:sheet sheet
:modifier-state modifier-state
:delta-x (case button
(#.+pointer-wheel-left+ -1)
(#.+pointer-wheel-right+ 1)
(otherwise 0))
:delta-y (case button
(#.+pointer-wheel-up+ -1)
(#.+pointer-wheel-down+ 1)
(otherwise 0))
:timestamp time))
(make-instance (if (eq event-key :button-press)
'pointer-button-press-event
'pointer-button-release-event)
:pointer 0
:button button :x x :y y
:graft-x root-x
:graft-y root-y
:sheet sheet :modifier-state modifier-state
:timestamp time))))
((:leave-notify :enter-notify)
;; Ignore :{ENTER,LEAVE}-NOTIFY events of kind :INFERIOR unless
;; the mode is :[UN]GRAB.
;;
;; The :INFERIOR kind corresponds to the pointer moving from a
;; parent window to a child window which we do not consider
;; leaving the parent.
;;
;; But we cannot ignore any :[UN]GRAB events since doing so
;; would violate the stack-properties of enter/exit event
;; sequences.
;;
;; The event kinds filtered here must be coordinated with the
;; processing in the DISTRIBUTE-EVENTS method for BASIC-PORT
;; and related methods.
(when (or (not (eq kind :inferior))
(member mode '(:grab :ungrab)))
(make-instance (case event-key
(:leave-notify (case mode
(:grab 'pointer-grab-leave-event)
(:ungrab 'pointer-ungrab-leave-event)
(t 'pointer-exit-event)))
(:enter-notify (case mode
(:grab 'pointer-grab-enter-event)
(:ungrab 'pointer-ungrab-enter-event)
(t 'pointer-enter-event))))
:pointer 0 :button code
:x x :y y
:graft-x root-x
:graft-y root-y
:sheet sheet
:modifier-state (clim-xcommon:x-event-state-modifiers
*doors-port* state)
:timestamp time)))
(:configure-notify
(make-instance 'window-configuration-event
:sheet sheet
:x x :y y :width width :height height))
(:destroy-notify
(make-instance 'window-destroy-event :sheet sheet))
(:motion-notify
(let ((modifier-state (clim-xcommon:x-event-state-modifiers *doors-port*
state)))
(make-instance 'pointer-motion-event
:pointer 0 :button code
:x x :y y
:graft-x root-x
:graft-y root-y
:sheet sheet
:modifier-state modifier-state
:timestamp time)))
((:exposure :display :graphics-exposure)
;; Notes:
;; . Do not compare count with 0 here, last rectangle in an
;; :exposure event sequence does not cover the whole region.
;;
;; . Do not transform the event region here, since
;; WINDOW-EVENT-REGION does it already. And rightfully so.
;; (think about changing a sheet's native transformation).
;;--GB
;;
;; Mike says:
;; One of the lisps is bogusly sending a :display event instead of an
;; :exposure event. I don't remember if it's CMUCL or SBCL. So the
;; :display event should be left in.
;;
(make-instance 'window-repaint-event
:timestamp time
:sheet sheet
:region (make-rectangle* x y (+ x width) (+ y height))))
;; port processes selection events synchronously and there is
;; no event passed to the rest of the system.
(:selection-notify
(clim-clx::process-selection-notify *doors-port* window target property selection time)
(maybe-funcall *wait-function*))
(:selection-clear
(clim-clx::process-selection-clear *doors-port* selection)
(maybe-funcall *wait-function*))
(:selection-request
(clim-clx::process-selection-request *doors-port* window sheet target property requestor selection time)
(maybe-funcall *wait-function*))
(:client-message
(or (port-client-message sheet time type data)
(maybe-funcall *wait-function*)))
(t
(unless (xlib:event-listen (clx-port-display *doors-port*))
(xlib:display-force-output (clx-port-display *doors-port*)))
(maybe-funcall *wait-function*)))))
;; copied from clim-clx
(defun port-client-message (sheet time type data)
(case type
(:wm_protocols
(let ((message (xlib:atom-name (slot-value *doors-port* 'clim-clx::display) (aref data 0))))
(case message
(:wm_take_focus
;; hmm, this message seems to be sent twice.
(when-let ((mirror (sheet-xmirror sheet)))
(xlib:set-input-focus (clx-port-display *doors-port*)
mirror :parent (elt data 1)))
(make-instance 'window-manager-focus-event :sheet sheet :timestamp time))
(:wm_delete_window
(make-instance 'window-manager-delete-event :sheet sheet :timestamp time))
(otherwise
(warn "Unprocessed WM Protocols message: ~:_message = ~S;~:_ data = ~S;~_ sheet = ~S."
message data sheet)))))
(otherwise
(warn "Unprocessed client message: ~:_type = ~S;~:_ data = ~S;~_ sheet = ~S."
type data sheet))))
(defmethod queue-event ((client application-frame) event)
(climi::event-queue-append (climi::frame-event-queue client) event))