X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~mdw/git/clg/blobdiff_plain/d4f4418a3709df05aae88e08f6e56b0bf60b7a4f..db7a1e771d6aec41d0489b07c546d99ebbbc1019:/glib/gcallback.lisp diff --git a/glib/gcallback.lisp b/glib/gcallback.lisp index 10f5aab..b2fd8cb 100644 --- a/glib/gcallback.lisp +++ b/glib/gcallback.lisp @@ -20,14 +20,14 @@ ;; TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE ;; SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -;; $Id: gcallback.lisp,v 1.37 2006-08-30 09:52:10 espen Exp $ +;; $Id: gcallback.lisp,v 1.48 2007-10-18 10:39:32 espen Exp $ (in-package "GLIB") (use-prefix "g") -;;;; Callback invokation +;;;; Callback invocation (deftype gclosure () 'pointer) (register-type 'gclosure '|g_closure_get_type|) @@ -36,26 +36,37 @@ (defun register-callback-function (function) (check-type function (or null symbol function)) (register-user-data function)) +(deftype user-callback () '(or function symbol)) + +(define-type-method alien-type ((type user-callback)) + (declare (ignore type)) + (alien-type 'pointer-data)) + +(define-type-method to-alien-form ((type user-callback) func &optional copy-p) + (declare (ignore type copy-p)) + `(register-callback-function ,func)) + + ;; Callback marshaller for regular signal handlers (define-callback signal-handler-marshal nil ((gclosure gclosure) (return-value gvalue) (n-params unsigned-int) (param-values pointer) (invocation-hint pointer) - (callback-id unsigned-long)) + (callback-id pointer-data)) (declare (ignore gclosure invocation-hint)) (callback-trampoline #'invoke-signal-handler callback-id n-params param-values return-value)) -;; Callback marshaller for class handlers +;; Callback marshaller for class handlers (define-callback class-handler-marshal nil ((gclosure gclosure) (return-value gvalue) (n-params unsigned-int) (param-values pointer) (invocation-hint pointer) - (callback-id unsigned-long)) + (callback-id pointer-data)) (declare (ignore gclosure invocation-hint)) (callback-trampoline #'invoke-callback callback-id n-params param-values return-value)) ;; Callback marshaller for emission hooks (define-callback emission-hook-marshal nil ((invocation-hint pointer) (n-params unsigned-int) (param-values pointer) - (callback-id unsigned-long)) + (callback-id pointer-data)) (declare (ignore invocation-hint)) (callback-trampoline #'invoke-callback callback-id n-params param-values)) @@ -87,12 +98,12 @@ (defun invoke-signal-handler (callback-id return-type &rest args) (signal-handler-block instance handler-id) (unwind-protect (restart-case (apply #'invoke-callback callback-id nil args) - (abort () :report "Disconnect and exit signal handler" + (disconnect () :report "Disconnect and exit signal handler" (when (signal-handler-is-connected-p instance handler-id) (signal-handler-disconnect instance handler-id)) - (values nil t)))) + (values nil t))) (when (signal-handler-is-connected-p instance handler-id) - (signal-handler-unblock instance handler-id)))) + (signal-handler-unblock instance handler-id))))) (defun invoke-callback (callback-id return-type &rest args) (restart-case (apply (find-user-data callback-id) args) @@ -118,8 +129,18 @@ (defconstant +priority-low+ 300) (defbinding source-remove () boolean (tag unsigned-int)) -(define-callback source-callback-marshal nil ((callback-id unsigned-int)) - (callback-trampoline callback-id 0 nil)) +(define-callback source-callback-marshal boolean ((callback-id unsigned-int)) + (invoke-source-callback callback-id)) + +(defun invoke-source-callback (callback-id &rest args) + (restart-case (apply (find-user-data callback-id) args) + (remove () :report "Exit and remove source callback" + nil) + (continue () :report "Return from source callback" + t) + (re-invoke nil :report "Re-invoke source callback" + (apply #'invoke-source-callback callback-id args)))) + (defbinding (timeout-add "g_timeout_add_full") (interval function &optional (priority +priority-default+)) unsigned-int @@ -207,20 +228,24 @@ (defun signal-param-types (info) (defun describe-signal (signal-id &optional type) (let ((info (signal-query (ensure-signal-id-from-type signal-id type)))) (with-slots (id name type flags return-type n-params) info - (format t "The signal with id ~D is named '~A' and may be emitted on instances of type ~S~%~%" id name (type-from-number type t)) - (format t "Signal handlers should return ~A and take ~A~%" - (cond - ((= return-type (find-type-number "void")) "no values") - ((not (type-from-number return-type)) "values of unknown type") - ((format nil "values of type ~S" (type-from-number return-type)))) + (format t "The signal with id ~D is named '~A' and may be emitted on instances of type ~S." id name (type-from-number type t)) + (when flags + (format t " It has the followin invocation flags: ~{~S ~}" flags)) + (format t "~%~%Signal handlers should take ~A and return ~A~%" (if (zerop n-params) "no arguments" (format nil "arguments with the following types: ~A" - (signal-param-types info))))))) + (signal-param-types info))) + (cond + ((= return-type (find-type-number "void")) "no values") + ((not (type-from-number return-type)) "values of unknown type") + ((format nil "values of type ~S" (type-from-number return-type)))))))) ;;;; Signal connecting and controlling +(define-flags-type connect-flags :after :swapped) + (defvar *overridden-signals* (make-hash-table :test 'equalp)) (defbinding %signal-override-class-closure () nil @@ -350,7 +375,7 @@ (defbinding signal-handler-find () unsigned-long (detail quark) (closure (or null pointer)) (func (or null pointer)) - (data unsigned-long)) + (data pointer-data)) (defbinding signal-handler-disconnect () nil (instance ginstance) @@ -375,23 +400,34 @@ (defun callback-closure-new (callback-id callback destroy-notify) (closure-set-meta-marshal gclosure callback-id callback) gclosure)) -(defun make-callback-closure (function marshaller) +(defun make-callback-closure (function &optional (marshaller signal-handler-marshal)) (let ((callback-id (register-callback-function function))) (values (callback-closure-new callback-id marshaller user-data-destroy-callback) callback-id))) -(defgeneric compute-signal-function (gobject signal function object)) +(defgeneric compute-signal-function (gobject signal function object args)) -(defmethod compute-signal-function ((gobject gobject) signal function object) +(defmethod compute-signal-function ((gobject gobject) signal function object args) (declare (ignore signal)) (cond - ((or (eq object t) (eq object gobject)) function) - ((not object) - #'(lambda (&rest args) (apply function (rest args)))) + ((or (eq object t) (eq object gobject)) + (if args + #'(lambda (&rest emission-args) + (apply function (nconc emission-args args))) + function)) + (object + (if args + #'(lambda (&rest emission-args) + (apply function object (nconc (rest emission-args) args))) + #'(lambda (&rest emission-args) + (apply function object (rest emission-args))))) + (args + #'(lambda (&rest emission-args) + (apply function (nconc (rest emission-args) args)))) (t - #'(lambda (&rest args) (apply function object (rest args)))))) - + #'(lambda (&rest emission-args) + (apply function (rest emission-args)))))) (defgeneric compute-signal-id (gobject signal)) @@ -399,7 +435,7 @@ (defmethod compute-signal-id ((gobject gobject) signal) (ensure-signal-id signal gobject)) -(defgeneric signal-connect (gobject signal function &key detail after object remove)) +(defgeneric signal-connect (gobject signal function &key detail after object remove args)) (defmethod signal-connect :around ((gobject gobject) signal function &rest args) (declare (ignore gobject signal args)) @@ -408,20 +444,21 @@ (defmethod signal-connect :around ((gobject gobject) signal function &rest args) (defmethod signal-connect ((gobject gobject) signal function - &key detail after object remove) + &key detail after object remove args) "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 for the signal. If :REMOVE is non NIL, the handler will be removed after beeing invoked -once." +once. ARGS is a list of additional arguments passed to the callback +function." (let* ((signal-id (compute-signal-id gobject signal)) (detail-quark (if detail (quark-intern detail) 0)) (signal-stop-emission #'(lambda () (%signal-stop-emission gobject signal-id detail-quark))) - (callback (compute-signal-function gobject signal function object)) + (callback (compute-signal-function gobject signal function object args)) (wrapper #'(lambda (&rest args) (let ((*signal-stop-emission* signal-stop-emission)) (apply callback args))))) @@ -459,7 +496,7 @@ (defun create-signal-emit-function (signal-id) (params (allocate-memory (* n-params +gvalue-size+)))) #'(lambda (detail object &rest args) (unless (= (length args) (1- n-params)) - (error "Invalid number of arguments: ~A" (+ 2 (length args)))) + (error "Invalid number of arguments in emmision of signal ~A: ~A" signal-id (length args))) (unwind-protect (loop for arg in (cons object args) @@ -469,7 +506,7 @@ (defun create-signal-emit-function (signal-id) finally (if return-type (return - (with-gvalue (return-value) + (with-gvalue (return-value return-type) (%signal-emitv params signal-id detail return-value))) (%signal-emitv params signal-id detail (make-pointer 0)))) (loop @@ -491,6 +528,25 @@ (defun signal-emit (object signal &rest args) (apply #'signal-emit-with-detail object signal 0 args)) +;;;; Signal registration + +(defbinding %signal-newv (name itype flags return-type param-types) + unsigned-int + ((signal-name-to-string name) string) + (itype gtype) + (flags signal-flags) + (nil null) ; class closure + (nil null) ; accumulator + (nil null) ; accumulator data + (nil null) ; c marshaller + (return-type gtype) + ((length param-types) unsigned-int) + (param-types (vector gtype))) + +(defun signal-new (name itype flags return-type param-types) + (when (zerop (signal-lookup name itype)) + (%signal-newv name itype flags return-type param-types))) + ;;;; Convenient macros (defmacro define-callback-marshal (name return-type args &key (callback-id :last)) @@ -517,8 +573,8 @@ (defmacro define-callback-marshal (name return-type args &key (callback-id :last (t (second arg)))))) `(define-callback ,name ,return-type ,(ecase callback-id - (:first `((callback-id unsigned-int) ,@(mapcar #'list names types))) - (:last `(,@(mapcar #'list names types) (callback-id unsigned-int)))) + (:first `((callback-id pointer-data) ,@(mapcar #'list names types))) + (:last `(,@(mapcar #'list names types) (callback-id pointer-data)))) (declare (ignore ,@ignore)) (invoke-callback callback-id ',return-type ,@(nreverse params)))))