-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtimer.lisp
36 lines (28 loc) · 1014 Bytes
/
timer.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
(in-package :cl-nextstep)
(cffi:defcfun ("make_timer" %make-timer) :pointer
(id :int)
(timer-fn :pointer)
(interval :double))
(defvar *timer-table* (make-hash-table))
(cffi:defcallback timer-callback :void ((id :int))
(let* ((timer (gethash id *timer-table*)))
(funcall (timer-fn timer))))
(defclass timer ()
((id :accessor id)
(g-id :initform 0 :accessor g-id :allocation :class)
(timer-fn :initarg :timer-fn :initform (error "timer-fn should be specified") :reader timer-fn)
(cocoa-ref :accessor cocoa-ref)))
(defmethod initialize-instance :after ((self timer) &key (interval 1.0))
(setf (id self) (g-id self))
(incf (g-id self))
(setf (gethash (id self) *timer-table*) self)
(setf (cocoa-ref self)
(autorelease
(objc (objc "LispTimer" "alloc" :pointer)
"initWithID:timerFn:timerInterval:"
:int (id self)
:pointer (cffi:callback timer-callback)
:double (float interval 1.0d0)
:pointer))))
(defun invalidate (timer)
(objc timer "invalidate"))