aboutsummaryrefslogtreecommitdiff
path: root/guile/pdp_guile.scm
blob: 8ca17f291f58166df40580a4c85ceee98c9de924 (plain)
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
; pdp_guile.scm - a simple event dispatcher to be used with pdp_guile

; some global variables
(define input-hash (make-hash-table 31))
(define input-loop-flag #t)
(define input-loop-interval-ms 10)

; add an input handler
(define (add-input! tag handler)
  (hashq-create-handle! input-hash tag handler))

; the main input dispatcher loop
(define (input-loop)
  (while input-loop-flag
	 (usleep (* input-loop-interval-ms 1000))
	 (let nextmsg ((msg (in)))
	   (if msg
	       (begin
		 (let ((fn (hashq-ref input-hash (car msg))))
		   (if fn (fn (cadr msg))))
		 (nextmsg (in)))))))

(define (start)
  (set! input-loop-flag #t)
  (out 'start 'bang)
  (input-loop))


; the control message handler
(add-input! 'control
	    (lambda (thing)
	      (case thing
		('stop (set! input-loop-flag #f))    ; stop the input loop and return to interpreter
		('gc (gc)))))                        ; call the garbage collector