forked from slyrus/mcclim-old
-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.lisp
524 lines (441 loc) · 18.8 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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
;;; -*- Mode: Lisp; Package: CLIM-INTERNALS -*-
;;; (c) copyright 1998,1999,2000 by Michael McDonald ([email protected])
;;; (c) copyright 2002 by Gilbert Baumann <[email protected]>
;;; This library is free software; you can redistribute it and/or
;;; modify it under the terms of the GNU Library General Public
;;; License as published by the Free Software Foundation; either
;;; version 2 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
;;; Library General Public License for more details.
;;;
;;; You should have received a copy of the GNU Library General Public
;;; License along with this library; if not, write to the
;;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;;; Boston, MA 02111-1307 USA.
(in-package :clim-internals)
;;; Input Protocol Classes
;; Event queues
(defclass standard-event-queue ()
((lock :initform (make-lock "event queue")
:reader event-queue-lock)
(head :initform nil
:accessor event-queue-head
:documentation "Head pointer of event queue.")
(tail :initform nil
:accessor event-queue-tail
:documentation "Tail pointer of event queue.")
(processes
:initform (make-condition-variable)
:accessor event-queue-processes
:documentation "Condition variable for waiting processes")
;; experimental extension for scheduled event insersion
(schedule-time
:initform nil
:accessor event-schedule-time
:documentation "The next time an event should be scheduled.")
(schedule
:initform nil
;; :accessor event-queue-schedule
;; this accessor conflicts with the method below.
;; noted by mikemac. I recommend renaming the slot.
;; --GB 2002-11-10
:documentation "Time ordered queue of events to schedule.")))
(defclass port-event-queue (standard-event-queue)
((port :initform nil
:initarg :port
:accessor event-queue-port
:documentation "The port which will be generating events for the queue.")))
(defmethod event-queue-read-no-hang/locked ((eq standard-event-queue))
(check-schedule eq)
(let ((res (pop (event-queue-head eq))))
(when (null (event-queue-head eq))
(setf (event-queue-tail eq) nil))
res))
(defmethod event-queue-read-no-hang ((eq standard-event-queue))
"Reads one event from the queue, if there is no event just return NIL."
(with-lock-held ((event-queue-lock eq))
(event-queue-read-no-hang/locked eq)))
(defmethod event-queue-read ((eq standard-event-queue))
"Reads one event from the queue, if there is no event, hang until here is one."
(let ((lock (event-queue-lock eq)))
(with-lock-held (lock)
(loop
(check-schedule eq)
(let ((res (event-queue-read-no-hang/locked eq)))
(when res
(return res))
;; is there an event waiting to be scheduled?
(with-slots (schedule-time) eq
(let* ((now (now))
(timeout (when schedule-time (- schedule-time now))))
(condition-wait (event-queue-processes eq) lock timeout))))))))
;;; XXX Should we do something with the wait function? I suspect that
;;; it's not compatible with the brave new world of native threads.
(defmethod event-queue-read-with-timeout ((eq standard-event-queue)
timeout wait-function)
(let ((lock (event-queue-lock eq)))
(with-lock-held (lock)
(loop
(check-schedule eq)
(let ((res (event-queue-read-no-hang/locked eq)))
(when res
(return res))
(when wait-function
(warn "event-queue-read-with-timeout ignoring predicate"))
(unless (condition-wait (event-queue-processes eq) lock timeout)
(return)))))))
(defmethod event-queue-append ((eq standard-event-queue) item)
"Append the item at the end of the queue. Does event compression."
(with-lock-held ((event-queue-lock eq))
(labels ((append-event ()
(cond ((null (event-queue-tail eq))
(setf (event-queue-head eq) (cons item nil)
(event-queue-tail eq) (event-queue-head eq)))
(t
(setf (event-queue-tail eq)
(setf (cdr (event-queue-tail eq)) (cons item nil))))))
(event-delete-if (predicate)
(when (not (null (event-queue-head eq)))
(setf (event-queue-head eq)
(delete-if predicate (event-queue-head eq))
(event-queue-tail eq)
(last (event-queue-head eq))))))
(cond
;; Motion Event Compression
;;
;; . find the (at most one) motion event
;; . delete it
;; . append item to queue
;;
;; But leave enter/exit events.
;;
((and (typep item 'pointer-motion-event)
(not (typep item 'pointer-boundary-event)))
(let ((sheet (event-sheet item)))
(event-delete-if
#'(lambda (x)
(and (typep x 'pointer-motion-event)
(not (typep x 'pointer-boundary-event))
(eq (event-sheet x) sheet))))
(append-event)))
;;
;; Resize event compression
;;
((typep item 'window-configuration-event)
(when (typep (event-sheet item) 'top-level-sheet-pane)
(let ((sheet (event-sheet item)))
(event-delete-if
#'(lambda (ev)
(and (typep ev 'window-configuration-event)
(eq (event-sheet ev) sheet)))))
(append-event)))
;;
;; Repaint event compression
;;
((typep item 'window-repaint-event)
(let ((region (window-event-native-region item))
(sheet (event-sheet item))
(did-something-p nil))
(labels ((fun (xs)
(cond ((null xs)
;; We reached the queue's tail: Append the new event, construct a new
;; one if necessary.
(when did-something-p
(setf item
(make-instance 'window-repaint-event
:timestamp (event-timestamp item)
:sheet (event-sheet item)
:region region)))
(setf (event-queue-tail eq) (cons item nil)) )
;;
((and (typep (car xs) 'window-repaint-event)
(eq (event-sheet (car xs)) sheet))
;; This is a repaint event for the same sheet, delete it and combine
;; its region into the new event.
(setf region
(region-union region (window-event-native-region (car xs))))
;; Here is an alternative, which just takes the bounding rectangle.
;; NOTE: When doing this also take care that the new region really
;; is cleared.
;; (setf region
;; (let ((old-region (window-event-native-region (car xs))))
;; (make-rectangle*
;; (min (bounding-rectangle-min-x region)
;; (bounding-rectangle-min-x old-region))
;; (min (bounding-rectangle-min-y region)
;; (bounding-rectangle-min-y old-region))
;; (max (bounding-rectangle-max-x region)
;; (bounding-rectangle-max-x old-region))
;; (max (bounding-rectangle-max-y region)
;; (bounding-rectangle-max-y old-region)))))
(setf did-something-p t)
(fun (cdr xs)))
;;
(t
(setf (cdr xs) (fun (cdr xs)))
xs))))
(setf (event-queue-head eq) (fun (event-queue-head eq))))))
;; Regular events are just appended:
(t (append-event))))
(condition-notify (event-queue-processes eq))))
(defmethod event-queue-prepend ((eq standard-event-queue) item)
"Prepend the item to the beginning of the queue."
(with-lock-held ((event-queue-lock eq))
(cond ((null (event-queue-tail eq))
(setf (event-queue-head eq) (cons item nil)
(event-queue-tail eq) (event-queue-head eq)))
(t
(push item (event-queue-head eq))))
(condition-notify (event-queue-processes eq))))
(defmethod event-queue-peek ((eq standard-event-queue))
(with-lock-held ((event-queue-lock eq))
(check-schedule eq)
(first (event-queue-head eq))))
(defmethod event-queue-peek-if (predicate (eq standard-event-queue))
"Goes thru the whole event queue and returns the first event, which
satisfies 'predicate' and leaves the event in the queue.
Returns NIL, if there is no such event."
(with-lock-held ((event-queue-lock eq))
(find-if predicate (event-queue-head eq))))
(defmethod event-queue-listen ((eq standard-event-queue))
(check-schedule eq)
(not (null (event-queue-head eq))))
(defun now ()
(/ (get-internal-real-time)
internal-time-units-per-second))
(defmethod event-queue-listen-or-wait ((eq standard-event-queue) &key timeout)
(check-schedule eq)
(let ((lock (event-queue-lock eq)))
(with-lock-held (lock)
(with-slots (schedule-time) eq
(flet ((pred ()
(not (null (event-queue-head eq)))))
(cond
(timeout
(loop as timeout-time = (+ now timeout)
with now = (now)
do (when (pred)
(return t))
do (when (>= now timeout-time)
(return nil))
do (let ((timeout (if schedule-time
(min (- schedule-time now)
(- timeout-time now))
(- timeout-time now))))
(condition-wait (event-queue-processes eq)
lock timeout))
do (check-schedule eq)))
(schedule-time
(loop do (when (pred)
(return t))
do (condition-wait
(event-queue-processes eq) lock (- schedule-time (now)))
do (check-schedule eq)))
(t
(or (pred)
(progn
(condition-wait (event-queue-processes eq) lock)
t)))))))))
(defmethod check-schedule ((eq standard-event-queue))
; see if it's time to inject a scheduled event into the queue.
(with-slots (schedule-time schedule) eq
(when (and schedule-time
(> (now) schedule-time))
(let* ((event (pop schedule))
(sheet (pop schedule)))
(setf schedule-time (pop schedule))
(dispatch-event sheet event))
t)))
; ugh. FIXME when I work - build a priority queue or something
(defmethod schedule-event-queue ((eq standard-event-queue) sheet event delay)
(with-slots (schedule-time schedule) eq
(let ((when (+ (now) delay)))
(if schedule
(cond
((< when schedule-time)
(push schedule-time schedule)
(push sheet schedule)
(push event schedule)
(setf schedule-time when))
(t
; (format *trace-output* "queue = ~A~%" schedule)
(do* ((prev (cdr schedule) (cdddr prev))
(point (cddr schedule) (cdddr point))
(time (car point)))
((or (null point)
(< when time))
(setf (cdr prev)
(cons when (cons event (cons sheet (cdr prev)))))))))
(progn
(setf schedule-time when)
(push sheet schedule)
(push event schedule))))))
;; PORT-EVENT-QUEUE methods
(defun do-port-force-output (port-event-queue)
(let ((port (event-queue-port port-event-queue)))
(when port (port-force-output port))))
(defmethod event-queue-read :before ((eq port-event-queue))
(do-port-force-output eq))
(defmethod event-queue-read-no-hang :before ((eq port-event-queue))
(do-port-force-output eq))
(defmethod event-queue-read-with-timeout :before ((eq port-event-queue)
timeout wait-function)
(declare (ignore timeout wait-function))
(do-port-force-output eq))
(defmethod event-queue-listen :before ((eq port-event-queue))
(do-port-force-output eq))
(defmethod event-queue-listen-or-wait :before ((eq standard-event-queue)
&key timeout)
(declare (ignore timeout))
(do-port-force-output eq))
(defmethod event-queue-peek :before ((eq port-event-queue))
(do-port-force-output eq))
(defmethod event-queue-peek-if :before (predicate (eq port-event-queue))
(declare (ignore predicate))
(do-port-force-output eq))
;; STANDARD-SHEET-INPUT-MIXIN
(defclass standard-sheet-input-mixin ()
((queue :initform (make-instance 'port-event-queue)
:reader sheet-event-queue
:initarg :input-buffer)
(port :initform nil
:initarg :port
:reader port)))
(defmethod stream-input-buffer ((stream standard-sheet-input-mixin))
(sheet-event-queue stream))
(defmethod (setf stream-input-buffer) (new-val
(stream standard-sheet-input-mixin))
(setf (slot-value stream 'queue) new-val))
;(defmethod dispatch-event ((sheet standard-sheet-input-mixin) event)
; (if (typep event 'device-event)
; (queue-event sheet event)
; (handle-event sheet event)))
(defmethod dispatch-event ((sheet standard-sheet-input-mixin) event)
(queue-event sheet event))
(defmethod queue-event ((sheet standard-sheet-input-mixin) event)
(with-slots (queue) sheet
(event-queue-append queue event)))
(defmethod schedule-event ((sheet standard-sheet-input-mixin) event delay)
(with-slots (queue) sheet
(schedule-event-queue queue sheet event delay)))
(defmethod handle-event ((sheet standard-sheet-input-mixin) event)
;; Standard practice is to ignore events
(declare (ignore event))
nil)
(defmethod event-read ((sheet standard-sheet-input-mixin))
(with-slots (queue) sheet
(event-queue-read queue)))
(defmethod event-read-with-timeout ((sheet standard-sheet-input-mixin)
&key (timeout nil) (wait-function nil))
;; This one is not in the spec ;-( --GB
(with-slots (queue) sheet
(event-queue-read-with-timeout queue timeout wait-function)))
(defmethod event-read-no-hang ((sheet standard-sheet-input-mixin))
(with-slots (queue) sheet
(event-queue-read-no-hang queue)))
(defmethod event-peek ((sheet standard-sheet-input-mixin) &optional event-type)
(with-slots (queue) sheet
(if event-type
(event-queue-peek-if (lambda (x)
(typep x event-type))
queue)
(event-queue-peek-if (lambda (x) (declare (ignore x)) t)
queue))))
(defmethod event-unread ((sheet standard-sheet-input-mixin) event)
(with-slots (queue) sheet
(event-queue-prepend queue event)))
(defmethod event-listen ((sheet standard-sheet-input-mixin))
(with-slots (queue) sheet
(event-queue-listen queue)))
;;;;
;;; Support for callers that want to set an event queue for every pane.
(defclass no-event-queue-mixin ()
())
(defmethod initialize-instance :after ((obj no-event-queue-mixin)
&key input-buffer)
(declare (ignore input-buffer))
nil)
(defmethod (setf stream-input-buffer) (new-val (stream no-event-queue-mixin))
new-val)
(defclass immediate-sheet-input-mixin (no-event-queue-mixin)
())
(defmethod dispatch-event ((sheet immediate-sheet-input-mixin) event)
(handle-event sheet event))
(defmethod handle-event ((sheet immediate-sheet-input-mixin) event)
(declare (ignore event))
nil)
(define-condition sheet-is-mute-for-input (error)
())
(defclass sheet-mute-input-mixin (no-event-queue-mixin)
())
(defmethod dispatch-event ((sheet sheet-mute-input-mixin) event)
(declare (ignore event))
(error 'sheet-is-mute-for-input))
(defmethod queue-event ((sheet sheet-mute-input-mixin) event)
(declare (ignore event))
(error 'sheet-is-mute-for-input))
(defmethod schedule-event ((sheet sheet-mute-input-mixin) event delay)
(declare (ignore event delay))
(error 'sheet-is-mute-for-input))
(defmethod handle-event ((sheet sheet-mute-input-mixin) event)
(declare (ignore event))
(error 'sheet-is-mute-for-input))
(defmethod event-read ((sheet sheet-mute-input-mixin))
(error 'sheet-is-mute-for-input))
(defmethod event-read-with-timeout ((sheet sheet-mute-input-mixin)
&key (timeout nil) (wait-function nil))
(declare (ignore timeout wait-function))
(error 'sheet-is-mute-for-input))
(defmethod event-read-no-hang ((sheet sheet-mute-input-mixin))
(error 'sheet-is-mute-for-input))
(defmethod event-peek ((sheet sheet-mute-input-mixin) &optional event-type)
(declare (ignore event-type))
(error 'sheet-is-mute-for-input))
(defmethod event-unread ((sheet sheet-mute-input-mixin) event)
(declare (ignore event))
(error 'sheet-is-mute-for-input))
(defmethod event-listen ((sheet sheet-mute-input-mixin))
(error 'sheet-is-mute-for-input))
;;;;
(defclass delegate-sheet-input-mixin ()
((delegate :initform nil
:initarg :delegate
:accessor delegate-sheet-delegate) ))
;;; Don't know if this event queue stuff is completely right, or if it matters
;;; much...
(defmethod initialize-instance :after ((obj delegate-sheet-input-mixin)
&key input-buffer)
(declare (ignore input-buffer)))
(defmethod stream-input-buffer ((stream delegate-sheet-input-mixin))
(sheet-event-queue (delegate-sheet-delegate stream)))
(defmethod (setf stream-input-buffer) (new-val
(stream delegate-sheet-input-mixin))
(setf (stream-input-buffer (delegate-sheet-delegate stream)) new-val))
(defmethod dispatch-event ((sheet delegate-sheet-input-mixin) event)
(dispatch-event (delegate-sheet-delegate sheet) event))
(defmethod queue-event ((sheet delegate-sheet-input-mixin) event)
(queue-event (delegate-sheet-delegate sheet) event))
(defmethod schedule-event ((sheet delegate-sheet-input-mixin) event delay)
(schedule-event (delegate-sheet-delegate sheet) event delay))
(defmethod handle-event ((sheet delegate-sheet-input-mixin) event)
(handle-event (delegate-sheet-delegate sheet) event))
(defmethod event-read ((sheet delegate-sheet-input-mixin))
(event-read (delegate-sheet-delegate sheet)))
(defmethod event-read-with-timeout ((sheet delegate-sheet-input-mixin)
&key (timeout nil) (wait-function nil))
(event-read-with-timeout (delegate-sheet-delegate sheet)
:timeout timeout :wait-function wait-function))
(defmethod event-read-no-hang ((sheet delegate-sheet-input-mixin))
(event-read-no-hang (delegate-sheet-delegate sheet)))
(defmethod event-peek ((sheet delegate-sheet-input-mixin) &optional event-type)
(event-peek (delegate-sheet-delegate sheet) event-type))
(defmethod event-unread ((sheet delegate-sheet-input-mixin) event)
(event-unread (delegate-sheet-delegate sheet) event))
(defmethod event-listen ((sheet delegate-sheet-input-mixin))
(event-listen (delegate-sheet-delegate sheet)))
;;; Class actually used by panes.
(defclass clim-sheet-input-mixin (standard-sheet-input-mixin)
())