chiark / gitweb /
Build instructions updated
[clg] / glib / gcallback.lisp
index 7d72ff611846ec49ca9286d965c9539e1daf097c..28acd48e00f426e1034208b58d94e56fab3069ab 100644 (file)
@@ -15,7 +15,7 @@
 ;; License along with this library; if not, write to the Free Software
 ;; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
-;; $Id: gcallback.lisp,v 1.3 2001-05-11 16:08:52 espen Exp $
+;; $Id: gcallback.lisp,v 1.10 2004-10-30 19:26:02 espen Exp $
 
 (in-package "GLIB")
 
@@ -29,17 +29,19 @@ (deftype gclosure () 'pointer)
 (defbinding lisp-callback-closure-new () gclosure
   (callback-id unsigned-int))
 
+(defun register-callback-function (function)
+  (check-type function (or null symbol function))
+  (register-user-data function))
 
+(defun make-callback-closure (function)
+  (lisp-callback-closure-new (register-callback-function function)))
 
-;;;; Callback mechanism
 
-(defun register-callback-function (function)
-  (check-type function (or null symbol function))
-  (lisp-callback-closure-new (register-user-data function)))
+;;;; Callback mechanism
 
 (defun callback-trampoline (callback-id params return-value)
   (let* ((return-type (unless (null-pointer-p return-value)
-                       (type-from-number (gvalue-type return-value))))
+                       (gvalue-type return-value)))
         (args nil)
         (callback-function (find-user-data callback-id)))
 
@@ -50,7 +52,7 @@ (defun callback-trampoline (callback-id params return-value)
     (labels ((invoke-callback ()
               (restart-case
                   (unwind-protect
-                      (let ((result (apply callback-function args)))
+                      (let ((result (apply callback-function (reverse args))))
                         (when return-type
                           (gvalue-set return-value result))))
                
@@ -66,22 +68,32 @@ (defun callback-trampoline (callback-id params return-value)
                  (invoke-callback)))))
       (invoke-callback))))
 
-(defun after-gc-hook ()
-  (setf
-   (extern-alien "callback_trampoline" system-area-pointer)
-   (make-pointer (kernel:get-lisp-obj-address #'callback-trampoline))
-   (extern-alien "destroy_user_data" system-area-pointer)
-   (make-pointer (kernel:get-lisp-obj-address #'destroy-user-data))))
 
-(pushnew 'after-gc-hook ext:*after-gc-hooks*)
-(after-gc-hook)
+;;;; Timeouts and idle functions
 
+(defvar *source-callback-marshal*
+  (system:foreign-symbol-address "source_callback_marshal"))
+(defvar *destroy-notify*
+  (system:foreign-symbol-address "destroy_notify"))
+
+(defbinding (timeout-add "g_timeout_add_full")
+    (function interval &optional (priority 0)) unsigned-int 
+  (priority int)
+  (interval unsigned-int)
+  (*source-callback-marshal* pointer)
+  ((register-callback-function function) unsigned-long)
+  (*destroy-notify* pointer))
+
+(defbinding (idle-add "g_idle_add_full")
+    (function &optional (priority 0)) unsigned-int 
+  (priority int)
+  (*source-callback-marshal* pointer)
+  ((register-callback-function function) unsigned-long)
+  (*destroy-notify* pointer))
 
 
-;;;; Signals
 
-(defun signal-name-to-string (name)
-  (substitute #\_ #\- (string-downcase (string name))))
+;;;; Signals
 
 (defbinding signal-lookup (name itype) unsigned-int
   ((signal-name-to-string name) string)
@@ -137,14 +149,41 @@ (defbinding signal-handler-disconnect () nil
   (handler unsigned-int))
 
 
-(defun signal-connect (instance signal function &key after object)
-  (let ((callback
-        (cond
-         ((or (eq object t) (eq object instance)) function)
-         ((not object)
-          #'(lambda (&rest args) (apply function (cdr args))))
-         (t
-          #'(lambda (&rest args) (apply function object (rest args)))))))
-    
-    (signal-connect-closure
-     instance signal (register-callback-function callback) :after after)))
+(defmethod signal-connect ((gobject gobject) signal function &key after object)
+"Connects a callback function to a signal for a particular object. If :OBJECT 
+ is T, the object connected to is passed as the first argument to the callback 
+ function, or if :OBJECT is any other non NIL value, it is passed as the first 
+ argument instead. If :AFTER is non NIL, the handler will be called after the 
+ default handler of the signal."
+  (let ((callback-id
+        (make-callback-closure
+         (cond
+          ((or (eq object t) (eq object gobject)) function)
+          ((not object)
+           #'(lambda (&rest args) (apply function (cdr args))))
+          (t
+           #'(lambda (&rest args) (apply function object (rest args))))))))
+    (signal-connect-closure gobject signal callback-id :after after)))
+
+
+;;; Message logging
+
+;; TODO: define and signal conditions based on log-level
+(defun log-handler (domain log-level message)
+  (declare (ignore log-level))
+  (error "~A: ~A" domain message))
+
+
+;;;
+
+(defun after-gc-hook ()
+  (setf
+   (extern-alien "callback_trampoline" system-area-pointer)
+   (make-pointer (kernel:get-lisp-obj-address #'callback-trampoline))
+   (extern-alien "destroy_user_data" system-area-pointer)
+   (make-pointer (kernel:get-lisp-obj-address #'destroy-user-data))
+   (extern-alien "log_handler" system-area-pointer)
+   (make-pointer (kernel:get-lisp-obj-address #'log-handler))))
+
+(pushnew 'after-gc-hook ext:*after-gc-hooks*)
+(after-gc-hook)