X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~mdw/git/clg/blobdiff_plain/6851535e7fccf7e832d85b8cd8d819c9990fe9f7..afa01e1b95c480818761b9e337ff08da44e9d49f:/glib/gcallback.lisp diff --git a/glib/gcallback.lisp b/glib/gcallback.lisp index 1d3dd14..7e3ccd2 100644 --- a/glib/gcallback.lisp +++ b/glib/gcallback.lisp @@ -20,7 +20,7 @@ ;; 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.31 2006-02-19 19:53:52 espen Exp $ +;; $Id: gcallback.lisp,v 1.38 2006-09-12 14:00:59 espen Exp $ (in-package "GLIB") @@ -36,48 +36,75 @@ (defun register-callback-function (function) (check-type function (or null symbol function)) (register-user-data function)) -;; Callback marshal for regular signal handlers -(define-callback closure-marshal nil +;; 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-int)) + (callback-id unsigned-long)) (declare (ignore gclosure invocation-hint)) - (callback-trampoline callback-id n-params param-values return-value)) + (callback-trampoline #'invoke-signal-handler callback-id n-params param-values return-value)) -;; Callback function for emission hooks -(define-callback signal-emission-hook nil +;; 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)) + (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-int)) - (callback-trampoline callback-id n-params param-values)) + (callback-id unsigned-long)) + (declare (ignore invocation-hint)) + (callback-trampoline #'invoke-callback callback-id n-params param-values)) -(defun callback-trampoline (callback-id n-params param-values &optional - (return-value (make-pointer 0))) +(defun callback-trampoline (restart-wrapper callback-id n-params param-values + &optional (return-value (make-pointer 0))) (let* ((return-type (unless (null-pointer-p return-value) (gvalue-type return-value))) (args (loop for n from 0 below n-params for offset from 0 by +gvalue-size+ - collect (gvalue-get (sap+ param-values offset) t)))) + collect (gvalue-peek (pointer+ param-values offset))))) (unwind-protect - (let ((result (apply #'invoke-callback callback-id return-type args))) - (when return-type + (multiple-value-bind (result aborted-p) + (apply restart-wrapper callback-id nil args) + (when (and return-type (not aborted-p)) (gvalue-set return-value result))) + ;; TODO: this should be made more general, by adding a type + ;; method to return invalidating functions. (loop for arg in args - when (typep arg 'proxy) + when (typep arg 'struct) do (invalidate-instance arg))))) +(defun invoke-signal-handler (callback-id return-type &rest args) + (declare (ignore return-type)) + (let* ((instance (first args)) + (handler-id (signal-handler-find instance '(:data) + 0 0 nil nil callback-id))) + (signal-handler-block instance handler-id) + (unwind-protect + (restart-case (apply #'invoke-callback callback-id nil args) + (abort () :report "Disconnect and exit signal handler" + (when (signal-handler-is-connected-p instance handler-id) + (signal-handler-disconnect instance handler-id)) + (values nil t)))) + (when (signal-handler-is-connected-p 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) + (restart-case (apply (find-user-data callback-id) args) (continue nil :report "Return from callback function" - (when return-type - (format *query-io* "Enter return value of type ~S: " return-type) - (force-output *query-io*) - (eval (read *query-io*)))) + (cond + (return-type + (format *query-io* "Enter return value of type ~S: " return-type) + (force-output *query-io*) + (eval (read *query-io*))) + (t (values nil t)))) (re-invoke nil :report "Re-invoke callback function" - (apply #'invoke-callback callback-id return-type args)))) + (apply #'invoke-callback callback-id return-type args)))) ;;;; Timeouts and idle functions @@ -92,7 +119,7 @@ (defbinding source-remove () boolean (tag unsigned-int)) (define-callback source-callback-marshal nil ((callback-id unsigned-int)) - (callback-trampoline callback-id 0 nil)) + (callback-trampoline #'invoke-callback callback-id 0 nil)) (defbinding (timeout-add "g_timeout_add_full") (interval function &optional (priority +priority-default+)) unsigned-int @@ -151,6 +178,9 @@ (deftype signal-flags () '(flags :run-first :run-last :run-cleanup :no-recurse :detailed :action :no-hooks)) + (define-flags-type signal-match-type + :id :detail :closure :func :data :unblocked) + (defclass signal-query (struct) ((id :allocation :alien :type unsigned-int) (name :allocation :alien :type (copy-of string)) @@ -164,7 +194,7 @@ (defclass signal-query (struct) (defbinding signal-query (signal-id &optional (signal-query (make-instance 'signal-query))) nil (signal-id unsigned-int) - (signal-query signal-query :return)) + (signal-query signal-query :in/return)) (defun signal-param-types (info) (with-slots (n-params param-types) info @@ -206,7 +236,7 @@ (defun signal-override-class-closure (name type function) (if callback-id (update-user-data callback-id function) (multiple-value-bind (callback-closure callback-id) - (make-callback-closure function) + (make-callback-closure function class-handler-marshal) (%signal-override-class-closure signal-id type-number callback-closure) (setf (gethash (cons type-number signal-id) *overridden-signals*) @@ -224,7 +254,7 @@ (defun %call-next-handler (n-params types args return-type) for arg in args for type in types for offset from 0 by +gvalue-size+ - do (gvalue-init (sap+ params offset) type arg)) + do (gvalue-init (pointer+ params offset) type arg)) (unwind-protect (if return-type @@ -235,7 +265,7 @@ (defun %call-next-handler (n-params types args return-type) (loop repeat n-params for offset from 0 by +gvalue-size+ - do (gvalue-unset (sap+ params offset))) + do (gvalue-unset (pointer+ params offset))) (deallocate-memory params))))) @@ -258,8 +288,8 @@ (default (make-symbol "DEFAULT"))) (let ((,default (list* ,object ,@vars ,rest))) (flet ((call-next-handler (&rest ,next) (%call-next-handler - ,n-params ',types (or ,next ,default) ',return-type)))) - ,@body))) + ,n-params ',types (or ,next ,default) ',return-type))) + ,@body)))) ',name))) @@ -278,16 +308,16 @@ (defun signal-stop-emission () (defbinding signal-add-emission-hook (type signal function &key (detail 0)) - unsigned-int + unsigned-long ((ensure-signal-id-from-type signal type) unsigned-int) (detail quark) - (signal-emission-hook callback) + (emission-hook-marshal callback) ((register-callback-function function) unsigned-int) (user-data-destroy-callback callback)) (defbinding signal-remove-emission-hook (type signal hook-id) nil ((ensure-signal-id-from-type signal type) unsigned-int) - (hook-id unsigned-int)) + (hook-id unsigned-long)) (defbinding (signal-has-handler-pending-p "g_signal_has_handler_pending") @@ -297,7 +327,7 @@ (defbinding (signal-has-handler-pending-p "g_signal_has_handler_pending") ((or detail 0) quark) (blocked boolean)) -(defbinding %signal-connect-closure-by-id () unsigned-int +(defbinding %signal-connect-closure-by-id () unsigned-long (instance ginstance) (signal-id unsigned-int) (detail quark) @@ -306,29 +336,49 @@ (defbinding %signal-connect-closure-by-id () unsigned-int (defbinding signal-handler-block () nil (instance ginstance) - (handler-id unsigned-int)) + (handler-id unsigned-long)) (defbinding signal-handler-unblock () nil (instance ginstance) - (handler-id unsigned-int)) + (handler-id unsigned-long)) + +;; Internal +(defbinding signal-handler-find () unsigned-long + (instance gobject) + (mask signal-match-type) + (signal-id unsigned-int) + (detail quark) + (closure (or null pointer)) + (func (or null pointer)) + (data unsigned-long)) (defbinding signal-handler-disconnect () nil (instance ginstance) - (handler-id unsigned-int)) + (handler-id unsigned-long)) (defbinding signal-handler-is-connected-p () boolean (instance ginstance) - (handler-id unsigned-int)) + (handler-id unsigned-long)) -(defbinding (callback-closure-new "clg_callback_closure_new") () gclosure +(defbinding (closure-new "g_cclosure_new") () gclosure + ((make-pointer #xFFFFFFFF) pointer) (callback-id unsigned-int) - (callback callback) (destroy-notify callback)) -(defun make-callback-closure (function) +(defbinding closure-set-meta-marshal () nil + (gclosure gclosure) + (callback-id unsigned-int) + (callback callback)) + +(defun callback-closure-new (callback-id callback destroy-notify) + (let ((gclosure (closure-new callback-id destroy-notify))) + (closure-set-meta-marshal gclosure callback-id callback) + gclosure)) + +(defun make-callback-closure (function marshaller) (let ((callback-id (register-callback-function function))) (values - (callback-closure-new callback-id closure-marshal user-data-destroy-callback) + (callback-closure-new callback-id marshaller user-data-destroy-callback) callback-id))) (defgeneric compute-signal-function (gobject signal function object)) @@ -376,7 +426,7 @@ (let* ((signal-id (compute-signal-id gobject signal)) (let ((*signal-stop-emission* signal-stop-emission)) (apply callback args))))) (multiple-value-bind (closure-id callback-id) - (make-callback-closure wrapper) + (make-callback-closure wrapper signal-handler-marshal) (let ((handler-id (%signal-connect-closure-by-id gobject signal-id detail-quark closure-id after))) (when remove @@ -385,7 +435,8 @@ (let* ((signal-id (compute-signal-id gobject signal)) (unwind-protect (let ((*signal-stop-emission* signal-stop-emission)) (apply callback args)) - (signal-handler-disconnect gobject handler-id))))) + (when (signal-handler-is-connected-p gobject handler-id) + (signal-handler-disconnect gobject handler-id)))))) handler-id)))) @@ -413,7 +464,7 @@ (defun create-signal-emit-function (signal-id) (loop for arg in (cons object args) for type in param-types - as tmp = params then (sap+ tmp +gvalue-size+) + as tmp = params then (pointer+ tmp +gvalue-size+) do (gvalue-init tmp type arg) finally (if return-type @@ -423,7 +474,7 @@ (defun create-signal-emit-function (signal-id) (%signal-emitv params signal-id detail (make-pointer 0)))) (loop repeat n-params - as tmp = params then (sap+ tmp +gvalue-size+) + as tmp = params then (pointer+ tmp +gvalue-size+) while (gvalue-p tmp) do (gvalue-unset tmp))))))) @@ -469,14 +520,10 @@ (defmacro define-callback-marshal (name return-type args &key (callback-id :last (:first `((callback-id unsigned-int) ,@(mapcar #'list names types))) (:last `(,@(mapcar #'list names types) (callback-id unsigned-int)))) (declare (ignore ,@ignore)) - (invoke-callback callback-id ',return-type ,@params)))) + (invoke-callback callback-id ',return-type ,@(nreverse params))))) (defmacro with-callback-function ((id function) &body body) `(let ((,id (register-callback-function ,function))) (unwind-protect (progn ,@body) (destroy-user-data ,id)))) - -;; For backward compatibility -(defmacro def-callback-marshal (name (return-type &rest args)) - `(define-callback-marshal ,name ,return-type ,args))