-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevents.scm
572 lines (510 loc) · 20 KB
/
events.scm
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
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
;;; ezd - easy drawing for X11 displays.
;;;
;;; Event handling.
;* Copyright 1990-1993 Digital Equipment Corporation
;* All Rights Reserved
;*
;* Permission to use, copy, and modify this software and its documentation is
;* hereby granted only under the following terms and conditions. Both the
;* above copyright notice and this permission notice must appear in all copies
;* of the software, derivative works or modified versions, and any portions
;* thereof, and both notices must appear in supporting documentation.
;*
;* Users of this software agree to the terms and conditions set forth herein,
;* and hereby grant back to Digital a non-exclusive, unrestricted, royalty-free
;* right and license under any changes, enhancements or extensions made to the
;* core functions of the software, including but not limited to those affording
;* compatibility with other hardware or software environments, but excluding
;* applications which incorporate this software. Users further agree to use
;* their best efforts to return to Digital any such changes, enhancements or
;* extensions that they make and inform Digital of noteworthy uses of this
;* software. Correspondence should be provided to Digital at:
;*
;* Director of Licensing
;* Western Research Laboratory
;* Digital Equipment Corporation
;* 250 University Avenue
;* Palo Alto, California 94301
;*
;* This software may be distributed (but not offered for sale or transferred
;* for compensation) to third parties, provided such third parties agree to
;* abide by the terms and conditions of this notice.
;*
;* THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
;* WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
;* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
;* CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
;* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
;* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
;* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
;* SOFTWARE.
;;; Pointing device motion and keyboard input are reported to ezd by events.
;;; Events are represented by event records of the following form. Lists of
;;; events are a part of each drawing and graphic.
(define-structure event
type ;;; Symbolic event type.
modifier-mask ;;; Button modifier bitmask.
modifier-names ;;; Button modifier list of symbols.
action) ;;; Action procedure.
;;; Event handlers are defined by the "when" command that has the following
;;; syntax:
;;;
;;; (when <object> <event> <action>)
;;;
;;; <object> specifies the object name where the event is to be handled. The
;;; object name "*" indicates that the event handler is for any object that
;;; does not have an explicit event handler.
;;;
;;; <event> is one of the following event types:
;;;
;;; ENTER pointing device entered the object.
;;; EXIT pointing device exitied the object.
;;; MOTION pointing device moved within the object.
;;; BUTTONbUP button "b" up in the object.
;;; BUTTONbDOWN button "b" down in the object. A down button event
;;; may be preceeded by any combination of SHIFT, META,
;;; CTRL, and LOCK indicating that those keys must be down
;;; for the event to occur.
;;; KEYPRESS any keyboard key down in the object.
;;; KEYRELEASE any keyboard key up in the object.
;;;
;;; RESIZE window containing the drawing was resized.
;;; EXPOSE window containing the drawing was exposed.
;;; OVERLAY the drawing was overlayed or underlayed into a window.
;;; VISIBLE a portion of the drawing is now visible in some window.
;;;
;;; GET-ATTRIBUTES message to read attribute values.
;;; SET-ATTRIBUTES message to set attribute values.
;;;
;;; * only allowed with action equal to #f to delete all
;;; event handlers.
;;;
;;; <action> is either the procedure handling the event, or a Scheme expression
;;; to be evaluated when the event occurs. When the event occurs, the action
;;; is invoked, and the following information is available as top-level
;;; bindings:
;;;
;;; *USER-EVENT-WINDOW* window name
;;; *USER-EVENT-DRAWING* drawing name
;;; *USER-EVENT-OBJECT* object name or #f
;;; *USER-EVENT-X* mouse position in drawing coordinate system
;;; *USER-EVENT-Y*
;;; *USER-EVENT-TYPE* event type
;;; *USER-EVENT-XEVENT* X event structure or #f
;;; *USER-EVENT-MISC* event specific items
;;;
;;; When the event type is SEND, the *USER-EVENT-WINDOW*, *USER-EVENT-X*,
;;; and *USER-EVENT-Y* fields are not valid.
(define (when-event name event action)
(define (encode-modifier)
(let loop ((mods (car event)) (mask 0))
(if (pair? mods)
(loop (cdr mods)
(bit-or mask
(cadr (assoc (car mods)
`((shift ,SHIFTMASK)
(lock ,LOCKMASK)
(capslock ,LOCKMASK)
(control ,CONTROLMASK)
(ctrl ,CONTROLMASK)
(meta ,MOD1MASK)
(compose ,MOD1MASK))))))
mask)))
(let ((modifier (if (symbol? event)
0
(encode-modifier)))
(type (if (symbol? event) event (cadr event)))
(action (if (or (procedure? action) (eq? action #f))
action
(lambda () (eval action)))))
(define (delete-event evl)
(if (pair? evl)
(let ((e (car evl)))
(if (and (equal? (event-type e) type)
(equal? (event-modifier-mask e) modifier))
(cdr evl)
(cons (car evl) (delete-event (cdr evl)))))
'()))
(define (add-event evl)
(if action
(append evl
(list (make-event type modifier
(if (zero? modifier) '() (car event))
action)))
evl))
(cond ((eq? type '*)
(cond ((not (eq? action #f))
(ezd-error 'WHEN-EVENT
"Event type * requires action #f"))
((not (eq? name '*))
(graphic-events! (name->graphic name) '())
(drawing-window-watch! *current-drawing*
(remq name
(drawing-window-watch *current-drawing*))))
(else (drawing-events! *current-drawing* '()))))
((not (eq? name '*))
(let ((g (name->graphic name)))
(if (and (memq type '(expose overlay resize visible))
(not (memq name (drawing-window-watch
*current-drawing*))))
(drawing-window-watch! *current-drawing*
(cons name
(drawing-window-watch *current-drawing*))))
(graphic-events! g
(add-event (delete-event (graphic-events g))))))
(else (drawing-events! *current-drawing*
(add-event (delete-event (drawing-events
*current-drawing*))))))))
(define (button-modifier? x)
(memq x '(shift meta control ctrl lock capslock hyper super)))
(define (button-down? x)
(memq x '(button1down button2down button3down button4down button5down)))
(define (when-event? x)
(memq x '(button1down button2down button3down button4down button5down
button1up button2up button3up button4up button5up
enter exit motion keypress keyrelease resize expose overlay
visible get-attributes set-attributes *)))
(define-ezd-command
`(when ,symbol?
(or ((repeat ,button-modifier?) ,button-down?)
(,when-event?))
,any?)
"(when <object-name> <event> ... <action>)"
when-event)
;;; A specific event is looked up in an event list by the following procedure.
;;; It returns the event or #f.
(define (find-when-event events type modifier)
(let loop ((events events))
(if (pair? events)
(let ((event (car events)))
(if (and (eq? (event-type event) type)
(eq? (event-modifier-mask event) modifier))
event
(loop (cdr events))))
#f)))
;;; When a window is resized or exposed, all drawings expecting the event are
;;; notified by the following procedure.
(define (handle-window-events window event-type event args)
(for-each
(lambda (view)
(handle-view-events view event-type event args)
(if (eq? event-type 'resize) (handle-visible-events view)))
(window-views window)))
;;; When a window related event happens to a specific view, the following
;;; procedure is called to invoke the event handlers. The general event
;;; handler (object = *) is called before any specific object event handlers.
(define (handle-view-events view event-type event args)
(let ((save-current-drawing *current-drawing*))
(set! *current-drawing* (view-drawing view))
(set-view view '())
(user-action (find-when-event (drawing-events (view-drawing view))
event-type 0) view #f event-type event args)
(for-each
(lambda (graphic-name)
(let ((graphic (name-of-graphic? graphic-name)))
(if graphic
(user-action
(find-when-event (graphic-events graphic)
event-type 0)
view graphic event-type event args))))
(drawing-window-watch (view-drawing view)))
(set! *current-drawing* save-current-drawing)))
;;; When an attribute message is sent to an object, the following procedure
;;; finds the event handler and calls it. If the event handler does not exist,
;;; then the message is ignored.
(define (handle-attribute-events drawing object event-type arguments)
(let ((user-event (find-when-event
(append (drawing-events (name->drawing drawing))
(graphic-events (getprop object drawing)))
event-type 0)))
(if user-event
(let ((save-current-drawing *current-drawing*))
(set-drawing drawing)
(set! *user-event-window* #f)
(set! *user-event-drawing* drawing)
(set! *user-event-object* object)
(set! *user-event-x* #f)
(set! *user-event-y* #f)
(set! *user-event-type* event-type)
(set! *user-event-xevent* #f)
(set! *user-event-misc* arguments)
(let ((result ((event-action user-event))))
(set! *current-drawing* save-current-drawing)
result))
#f)))
;;; The following global variables maintain the current mouse state
;;; They are automatically updated when each X event is processed, or when
;;; changes are made to drawings displayed in the window containing the
;;; mouse.
(define *mouse-x* 0)
(define *mouse-y* 0)
(define *mouse-xwindow* #f)
(define *mouse-window* #f)
(define *mouse-window-x* 0)
(define *mouse-window-y* 0)
(define *mouse-view* #f)
(define *mouse-object* #f)
(define *mouse-button1* #f)
(define *mouse-button2* #f)
(define *mouse-button3* #f)
(define *mouse-button4* #f)
(define *mouse-button5* #f)
;;; Mouse state is maintained by the following procedure.
(define (update-mouse event)
(let ((event-type (xevent-type event)))
(cond ((or (eq? event-type BUTTONPRESS)
(eq? event-type BUTTONRELEASE))
(set! *mouse-x* (xevent-xbutton-x_root event))
(set! *mouse-y* (xevent-xbutton-y_root event))
(set! *mouse-xwindow* (xevent-xbutton-window event))
(if (eq? event-type BUTTONPRESS)
(case (xevent-xbutton-button event)
((1) (set! *mouse-button1* #t))
((2) (set! *mouse-button2* #t))
((3) (set! *mouse-button3* #t))
((4) (set! *mouse-button4* #t))
((5) (set! *mouse-button5* #t)))
(case (xevent-xbutton-button event)
((1) (set! *mouse-button1* #f))
((2) (set! *mouse-button2* #f))
((3) (set! *mouse-button3* #f))
((4) (set! *mouse-button4* #f))
((5) (set! *mouse-button5* #f)))))
((eq? event-type ENTERNOTIFY)
(set! *mouse-x* (xevent-xcrossing-x_root event))
(set! *mouse-y* (xevent-xcrossing-y_root event))
(set! *mouse-xwindow* (xevent-xcrossing-window event))
(set! *mouse-object* ""))
((eq? event-type LEAVENOTIFY)
(set! *mouse-x* (xevent-xcrossing-x_root event))
(set! *mouse-y* (xevent-xcrossing-y_root event))
(set! *mouse-window* #f)
(set! *mouse-xwindow* #f)
(set! *mouse-object* ""))
((eq? event-type MOTIONNOTIFY)
(set! *mouse-x* (xevent-xmotion-x_root event))
(set! *mouse-y* (xevent-xmotion-y_root event))
(set! *mouse-xwindow* (xevent-xmotion-window event))))
(if *trace-events*
(format *trace-events* "~s (~s,~s) ~s~s~s~s~s ==> "
(if *mouse-xwindow*
(window-name
(xwindow->window *mouse-xwindow*))
#f)
*mouse-x* *mouse-y* *mouse-button1* *mouse-button2*
*mouse-button3* *mouse-button4* *mouse-button5*))))
;;; X event code to event name conversion.
(define (event->name code)
(cond ((eq? code MOTIONNOTIFY) 'motionnotify)
((eq? code BUTTONPRESS) 'buttonpress)
((eq? code BUTTONRELEASE) 'buttonrelease)
((eq? code ENTERNOTIFY) 'enternotify)
((eq? code LEAVENOTIFY) 'leavenotify)
((eq? code EXPOSE) 'expose)
((eq? code KEYPRESS) 'keypress)
((eq? code KEYRELEASE) 'keyrelease)
(else code)))
;;; Translate X button events to button symbol.
(define (button-down-symbol event)
(case (xevent-xbutton-button event)
((1) 'button1down)
((2) 'button2down)
((3) 'button3down)
((4) 'button4down)
((5) 'button5down)))
(define (button-up-symbol event)
(case (xevent-xbutton-button event)
((1) 'button1up)
((2) 'button2up)
((3) 'button3up)
((4) 'button4up)
((5) 'button5up)))
;;; Object entry, exit, and motion events are generated by the following
;;; procedure. Each time the mouse has moved, it is called with a window and
;;; position in window.
(define (exit-enter event window x y)
(let ((was-in *mouse-object*)
(was-in-view *mouse-view*)
(was-x *mouse-window-x*)
(was-y *mouse-window-y*)
(was-in-exit (find-when-event (is-in-events) 'exit 0)))
(set! *mouse-window* window)
(set! *mouse-window-x* x)
(set! *mouse-window-y* y)
(let loop ((views (reverse (window-views window))))
(if (pair? views)
(let* ((view (car views))
(object (and (or (not (view-clip-minx view))
(and (<= (view-clip-minx view)
*mouse-window-x*
(view-clip-maxx view))
(<= (view-clip-miny view)
*mouse-window-y*
(view-clip-maxy view))))
(bbgraphics-really-intersect view
(- x 1) (- y 1) (+ x 1) (+ y 1)))))
(if object
(begin (set! *mouse-view* view)
(set! *mouse-object* object))
(loop (cdr views))))
(begin (set! *mouse-view* #f)
(set! *mouse-object* #f))))
(if (eq? was-in *mouse-object*)
(if (and (isa-graphic? *mouse-object*)
(or (not (= was-x *mouse-window-x*))
(not (= was-y *mouse-window-y*))))
(user-action (find-when-event (is-in-events) 'motion 0)
*mouse-view* *mouse-object* 'motion event '()))
(begin (user-action was-in-exit was-in-view was-in 'exit event
(if *mouse-view*
`(,(window-name (view-window *mouse-view*))
,(drawing-name (view-drawing *mouse-view*))
,(graphic-name *mouse-object*))
'(#f #f #f)))
(if (isa-graphic? *mouse-object*)
(user-action
(find-when-event (is-in-events) 'enter 0)
*mouse-view* *mouse-object* 'enter event '()))))))
;;; Return a list of possible events for a given window and the current mouse
;;; object.
(define (is-in-events)
(if (isa-graphic? *mouse-object*)
(append (graphic-events *mouse-object*)
(drawing-events (view-drawing *mouse-view*)))
'()))
;;; Signal a possible user event. Once the user event has been run, the mouse
;;; position is recomputed if there was anything drawn in the mouse window.
(define *clean-mouse-window* #f)
(define *user-event-window* #f)
(define *user-event-drawing* #f)
(define *user-event-object* #f)
(define *user-event-x* #f)
(define *user-event-y* #f)
(define *user-event-type* #f)
(define *user-event-xevent* #f)
(define *user-event-misc* #f)
(define (user-action user-event view object user-event-type event misc)
(when user-event
(if *trace-events* (format *trace-events* "~s " user-event-type))
(if object
(begin (if (pair? (view-new-transform view))
(display-event-handler *display*))
(set! *user-event-object* (graphic-name object))
(set! *user-event-x*
((view-x->user view) *mouse-window-x*))
(set! *user-event-y*
((view-y->user view) *mouse-window-y*)))
(begin (set! *user-event-object* '*)
(set! *user-event-x* 0)
(set! *user-event-y* 0)))
(set! *clean-mouse-window* *mouse-window*)
(set! *user-event-window* (window-name (view-window view)))
(set! *user-event-drawing* (drawing-name (view-drawing view)))
(set! *user-event-type* user-event-type)
(set! *user-event-xevent* event)
(set! *user-event-misc* misc)
(set-drawing *user-event-drawing*)
((event-action user-event))
(when (or (and (eq? user-event-type 'visible)
(eq? *clean-mouse-window* (view-window view)))
(and (not *clean-mouse-window*) *mouse-window*))
(set! *clean-mouse-window* #f)
(exit-enter #f *mouse-window* *mouse-window-x*
*mouse-window-y*))
(set! *clean-mouse-window* #f)))
;;; When an event occurs in a window, the window's views are examined from top
;;; to bottom until an object in which the event occurred is found. Before
;;; that object's event handler is called with the appropriate event, EXIT and
;;; ENTER events are generated as needed.
(define *trace-events* #f)
(define (handle-when-events window event)
(let ((event-type (xevent-type event)))
(if *trace-events*
(format *trace-events* "X: ~s ~s ==> " (window-name window)
(event->name event-type)))
(cond ((eq? event-type BUTTONPRESS)
(update-mouse event)
(exit-enter event window
(xevent-xbutton-x event) (xevent-xbutton-y event))
(let ((user-event (find-when-event (is-in-events)
(button-down-symbol event)
(xevent-xbutton-state event))))
(if user-event
(user-action user-event *mouse-view* *mouse-object*
(button-down-symbol event) event
(event-modifier-names user-event)))))
((eq? event-type BUTTONRELEASE)
(update-mouse event)
(exit-enter event window
(xevent-xbutton-x event) (xevent-xbutton-y event))
(user-action
(find-when-event (is-in-events)
(button-up-symbol event) 0)
*mouse-view* *mouse-object* (button-up-symbol event)
event '()))
((eq? event-type ENTERNOTIFY)
(update-mouse event)
(exit-enter event window (xevent-xcrossing-x event)
(xevent-xcrossing-y event)))
((eq? event-type LEAVENOTIFY)
(let ((was-in *mouse-object*)
(was-in-view *mouse-view*)
(was-in-exit (find-when-event (is-in-events) 'exit 0)))
(update-mouse event)
(user-action was-in-exit was-in-view was-in
'exit event '(#f #f #f))))
((eq? event-type KEYPRESS)
(user-action
(find-when-event (is-in-events) 'keypress 0)
*mouse-view* *mouse-object* 'keypress event
(ylookupstring event #t)))
((eq? event-type KEYRELEASE)
(user-action
(find-when-event (is-in-events) 'keyrelease 0)
*mouse-view* *mouse-object* 'keyrelease event
(ylookupstring event #t)))
((eq? event-type MOTIONNOTIFY)
(let loop ((event event))
(if (and (> (xeventsqueued *dpy* QUEUEDAFTERREADING) 0)
(let-temporary ((ev (make-xevent) free-xevent))
(xpeekevent *dpy* ev)
(eq? (xevent-type ev)
MOTIONNOTIFY)))
(begin
(xnextevent *dpy* event)
(loop event))
(when (and (< -1 (xevent-xmotion-x event)
(window-width window))
(< -1 (xevent-xmotion-y event)
(window-height window)))
(update-mouse event)
(exit-enter event window
(xevent-xmotion-x event)
(xevent-xmotion-y event))))))
((eq? event-type EXPOSE)
(handle-window-events window 'expose event '())))
(if *trace-events* (format *trace-events* "~%"))))
;;; Module reset/initialization
(define (events-module-init)
(set! *mouse-x* 0)
(set! *mouse-y* 0)
(set! *mouse-xwindow* #f)
(set! *mouse-window* #f)
(set! *mouse-window-x* 0)
(set! *mouse-window-y* 0)
(set! *mouse-view* #f)
(set! *mouse-object* #f)
(set! *mouse-button1* #f)
(set! *mouse-button2* #f)
(set! *mouse-button3* #f)
(set! *mouse-button4* #f)
(set! *mouse-button5* #f)
(set! *clean-mouse-window* #f)
(set! *user-event-window* #f)
(set! *user-event-drawing* #f)
(set! *user-event-object* #f)
(set! *user-event-x* #f)
(set! *user-event-y* #f)
(set! *user-event-type* #f)
(set! *user-event-xevent* #f)
#t)