(:file "gobject.gvalue")
(:file "gobject.foreign")
(:file "gobject.stable-pointer")
- (:file "gobject.closure")
(:file "gobject.object.low")
(:file "gobject.object.high")
-
+ (:file "gobject.signals")
+
(:file "gobject.meta")
(:file "gobject.generating")
(:file "gobject.object-defs")
+++ /dev/null
-(in-package :gobject)
-
-(defcstruct lisp-closure
- (:parent-instance g-closure)
- (:function-id :pointer))
-
-(defun finalize-lisp-closure (closure)
- (let ((function-id (foreign-slot-value closure 'lisp-closure :function-id)))
- (free-stable-pointer function-id)))
-
-(defcallback lisp-closure-finalize :void ((data :pointer)
- (closure (:pointer lisp-closure)))
- (declare (ignore data))
- (finalize-lisp-closure closure))
-
-(defun call-with-restarts (fn args)
- (restart-case
- (apply fn args)
- (return-from-g-closure (&optional v) :report "Return value from closure" v)))
-
-(defcallback lisp-closure-marshal :void ((closure (:pointer lisp-closure))
- (return-value (:pointer g-value))
- (count-of-args :uint)
- (args (:pointer g-value))
- (invocation-hint :pointer)
- (marshal-data :pointer))
- (declare (ignore invocation-hint marshal-data))
- (let* ((args (parse-closure-arguments count-of-args args))
- (function-id (foreign-slot-value closure 'lisp-closure :function-id))
- (return-type (and (not (null-pointer-p return-value))
- (g-value-type return-value)))
- (fn (get-stable-pointer-value function-id))
- (fn-result (call-with-restarts fn args)))
- (when return-type
- (set-g-value return-value fn-result return-type :g-value-init nil))))
-
-(defun parse-closure-arguments (count-of-args args)
- (loop
- for i from 0 below count-of-args
- collect (parse-g-value (mem-aref args 'g-value i))))
-
-(defun create-g-closure (fn)
- (let ((function-id (allocate-stable-pointer fn))
- (closure (g-closure-new-simple (foreign-type-size 'lisp-closure) (null-pointer))))
- (setf (foreign-slot-value closure 'lisp-closure :function-id) function-id)
- (g-closure-add-finalize-notifier closure (null-pointer)
- (callback lisp-closure-finalize))
- (g-closure-set-marshal closure (callback lisp-closure-marshal))
- closure))
(defstruct object-type name class parent interfaces properties)
(defun instance-init (instance class)
- (debugf "Initializing instance ~A for type ~A (creating ~A)~%" instance (g-type-name (foreign-slot-value class 'g-type-class :type)) *current-creating-object*)
+ (log-for :subclass "Initializing instance ~A for type ~A (creating ~A)~%" instance (g-type-name (foreign-slot-value class 'g-type-class :type)) *current-creating-object*)
(unless (or *current-creating-object*
(gethash (pointer-address instance) *foreign-gobjects-strong*)
(gethash (pointer-address instance) *foreign-gobjects-weak*))
- (debugf " Proceeding with initialization...")
+ (log-for :subclass " Proceeding with initialization...")
(let* ((g-type (foreign-slot-value class 'g-type-class :type))
(type-name (g-type-name g-type))
(lisp-type-info (gethash type-name *registered-types*))
(iter (for property in (object-type-properties lisp-type-info))
(for param-spec = (property->param-spec property))
(for property-id from 123)
- (debugf "installing property ~A~%" property)
+ (log-for :subclass "installing property ~A~%" property)
(g-object-class-install-property class property-id param-spec))))
(defun vtable-item->cstruct-item (item)
(declare (ignorable class-name))
(let* ((vtable (gethash interface-name *vtables*))
(vtable-cstruct (vtable-description-cstruct-name vtable)))
- (debugf "interface-init for class ~A and interface ~A~%" class-name interface-name)
+ (log-for :subclass "interface-init for class ~A and interface ~A~%" class-name interface-name)
(iter (for method in (vtable-description-methods vtable))
(setf (foreign-slot-value iface vtable-cstruct (vtable-method-info-name method)) (get-callback (vtable-method-info-callback-name method)))))))
(defun class-init (class data)
(declare (ignore data))
- (debugf "class-init for ~A~%" (g-type-name (g-type-from-class class)))
+ (log-for :subclass "class-init for ~A~%" (g-type-name (g-type-from-class class)))
(setf (foreign-slot-value class 'g-object-class :get-property)
(callback c-object-property-get)
(foreign-slot-value class 'g-object-class :set-property)
(lisp-type-info (gethash type-name *registered-types*))
(property-info (find property-name (object-type-properties lisp-type-info) :test 'string= :key 'first))
(property-get-fn (fourth property-info)))
- (debugf "get(~A,'~A')~%" lisp-object property-name)
+ (log-for :subclass "get(~A,'~A')~%" lisp-object property-name)
(let ((value (restart-case
(funcall property-get-fn lisp-object)
- (return-from-property-getter (value) :interactive (lambda () (debugf "Enter new value: ") (list (eval (read)))) value))))
+ (return-from-property-getter (value) :interactive (lambda () (format t "Enter new value: ") (list (eval (read)))) value))))
(set-g-value g-value value property-type))))
(defcallback c-object-property-get :void ((object :pointer) (property-id :uint) (value :pointer) (pspec :pointer))
(property-info (find property-name (object-type-properties lisp-type-info) :test 'string= :key 'first))
(property-set-fn (fifth property-info))
(new-value (parse-g-value value)))
- (debugf "set(~A,'~A',~A)~%" lisp-object property-name new-value)
+ (log-for :subclass "set(~A,'~A',~A)~%" lisp-object property-name new-value)
(restart-case
(funcall property-set-fn new-value lisp-object)
(return-without-error-from-property-setter () nil))))
`(progn
(setf (gethash ,name *registered-types*) (make-object-type :name ,name :class ',class :parent ,parent :interfaces ',interfaces :properties ',properties))
(at-init (',class)
- (debugf "Registering GObject type implementation ~A for type ~A~%" ',class ,name)
+ (log-for :subclass "Registering GObject type implementation ~A for type ~A~%" ',class ,name)
(with-foreign-object (query 'g-type-query)
(g-type-query (g-type-from-name ,parent) query)
(g-type-register-static-simple (g-type-from-name ,parent)
(has-reference
:type boolean
:accessor g-object-has-reference
- :initform nil))
+ :initform nil)
+ (signal-handlers
+ :type (array t *)
+ :initform (make-array 0 :adjustable t :fill-pointer t)
+ :reader g-object-signal-handlers))
(:documentation
"Base class for GObject classes hierarchy."))
(defvar *foreign-gobjects-weak* (make-weak-hash-table :test 'equal :weakness :value))
(defvar *foreign-gobjects-strong* (make-hash-table :test 'equal))
(defvar *current-creating-object* nil)
+(defvar *current-object-from-pointer* nil)
(defun ref-count (pointer)
(foreign-slot-value (if (pointerp pointer) pointer (pointer pointer)) 'g-object-struct :ref-count))
(s (format nil "~A" obj)))
(finalize obj
(lambda ()
- (debugf "~A is queued for GC (having ~A refs)~%" pointer (ref-count pointer))
+ (log-for :gc "~A ~A is queued for GC (having ~A refs)~%"
+ (g-type-from-object pointer) pointer (ref-count pointer))
(handler-case
(g-object-dispose-carefully pointer)
- (error (e) (format t "Error in finalizer for ~A: ~A~%" s e))))))
+ (error (e)
+ (log-for :gc "Error in finalizer for ~A: ~A~%" s e)
+ (format t "Error in finalizer for ~A: ~A~%" s e))))))
(register-g-object obj)
(activate-gc-hooks))
(defun activate-gc-hooks ()
(with-recursive-lock-held (*gobject-gc-hooks-lock*)
(when *gobject-gc-hooks*
- (debugf "activating gc hooks for objects: ~A~%" *gobject-gc-hooks*)
+ (log-for :gc "activating gc hooks for objects: ~A~%" *gobject-gc-hooks*)
(loop
for pointer in *gobject-gc-hooks*
do (g-object-remove-toggle-ref pointer (callback gobject-toggle-ref-toggled) (null-pointer)))
(let ((locks-were-present (not (null *gobject-gc-hooks*))))
(push pointer *gobject-gc-hooks*)
(unless locks-were-present
- (debugf "adding idle-gc-hook to main loop~%")
+ (log-for :gc "adding idle-gc-hook to main loop~%")
(g-idle-add (callback g-idle-gc-hook) (null-pointer))))))
(defun g-object-dispose-carefully (pointer)
(handler-case
(register-gobject-for-gc pointer)
- (error (e) (format t "Error in dispose: ~A~%" e))))
+ (error (e)
+ (log-for :gc "Error in dispose: ~A~%" e)
+ (format t "Error in dispose: ~A~%" e))))
(defun should-ref-sink-at-creation (object)
;;If object was not created from lisp-side, we should ref it
;;If an object is GInitiallyUnowned, then it is created with a floating reference, we should ref-sink it
;;A special case is GtkWindow: we should ref-sink it anyway
(let ((r (cond
- ((eq object *current-creating-object*) ;; g_object_new returns objects with ref = 1, we should save _this_ ref
- (g-object-is-floating (pointer object))) ;; but floating objects should be ref_sunk
+ ((equal *current-object-from-pointer* (pointer object))
+ (log-for :gc "*cur-obj-from-ptr* ")
+ t) ;; not new objects should be ref_sunk
+ ((eq object *current-creating-object*) ;; g_object_new returns objects with ref = 1, we should save _this_ ref
+ (typep object 'g-initially-unowned)) ;; but GInitiallyUnowned objects should be ref_sunk
(t t))))
- (debugf "(should-ref-sink-at-creation ~A) => ~A~%" object r)
+ (log-for :gc "(should-ref-sink-at-creation ~A) => ~A~%" object r)
r))
(defcallback gobject-toggle-ref-toggled :void
((data :pointer) (pointer :pointer) (is-last-ref :boolean))
(declare (ignore data))
- (debugf "~A is now ~A with ~A refs~%" pointer (if is-last-ref "weak pointer" "strong pointer") (ref-count pointer))
- (debugf "obj: ~A~%" (or (gethash (pointer-address pointer) *foreign-gobjects-strong*)
+ (log-for :gc "~A is now ~A with ~A refs~%" pointer (if is-last-ref "weak pointer" "strong pointer") (ref-count pointer))
+ (log-for :gc "obj: ~A~%" (or (gethash (pointer-address pointer) *foreign-gobjects-strong*)
(gethash (pointer-address pointer) *foreign-gobjects-weak*)))
(if is-last-ref
(let ((obj (gethash (pointer-address pointer) *foreign-gobjects-strong*)))
(progn
(remhash (pointer-address pointer) *foreign-gobjects-strong*)
(setf (gethash (pointer-address pointer) *foreign-gobjects-weak*) obj))
- (warn "GObject at ~A has no lisp-side (strong) reference" pointer)))
+ (progn
+ (log-for :gc "GObject at ~A has no lisp-side (strong) reference" pointer)
+ (warn "GObject at ~A has no lisp-side (strong) reference" pointer))))
(let ((obj (gethash (pointer-address pointer) *foreign-gobjects-weak*)))
- (unless obj (warn "GObject at ~A has no lisp-side (weak) reference" pointer))
+ (unless obj
+ (log-for :gc "GObject at ~A has no lisp-side (weak) reference" pointer)
+ (warn "GObject at ~A has no lisp-side (weak) reference" pointer))
(remhash (pointer-address pointer) *foreign-gobjects-weak*)
(setf (gethash (pointer-address pointer) *foreign-gobjects-strong*) obj))))
(defcallback gobject-weak-ref-finalized :void
((data :pointer) (pointer :pointer))
(declare (ignore data))
- (debugf "~A is weak-ref-finalized with ~A refs~%" pointer (ref-count pointer))
+ (log-for :gc "~A is weak-ref-finalized with ~A refs~%" pointer (ref-count pointer))
(remhash (pointer-address pointer) *foreign-gobjects-weak*)
(when (gethash (pointer-address pointer) *foreign-gobjects-strong*)
- (warn "GObject at ~A was weak-ref-finalized while still holding lisp-side strong reference to it" pointer))
+ (warn "GObject at ~A was weak-ref-finalized while still holding lisp-side strong reference to it" pointer)
+ (log-for :gc "GObject at ~A was weak-ref-finalized while still holding lisp-side strong reference to it" pointer))
(remhash (pointer-address pointer) *foreign-gobjects-strong*))
(defun register-g-object (obj)
- (debugf "registered GObject ~A with gobject ref-count ~A ~A~%" (pointer obj) (ref-count obj) (if (g-object-is-floating (pointer obj)) "(floating)" ""))
+ (log-for :gc "registered GObject ~A (~A) with initial ref-count ~A ~A~%"
+ (pointer obj) obj
+ (ref-count obj) (if (g-object-is-floating (pointer obj)) "(floating)" ""))
(when (should-ref-sink-at-creation obj)
- (debugf "g_object_ref_sink(~A)~%" (pointer obj))
+ (log-for :gc "g_object_ref_sink(~A)~%" (pointer obj))
(g-object-ref-sink (pointer obj)))
(setf (g-object-has-reference obj) t)
(setf (gethash (pointer-address (pointer obj)) *foreign-gobjects-strong*) obj)
(unless lisp-type
(error "Type ~A is not registered with REGISTER-OBJECT-TYPE"
(g-type-name g-type)))
- (g-object-ref pointer)
- (make-instance lisp-type :pointer pointer)))
+ (let ((*current-object-from-pointer* pointer))
+ (make-instance lisp-type :pointer pointer))))
(define-foreign-type foreign-g-object-type ()
((sub-type :reader sub-type :initarg :sub-type :initform 'g-object))
(unless (null-pointer-p pointer)
(or (gethash (pointer-address pointer) *foreign-gobjects-strong*)
(gethash (pointer-address pointer) *foreign-gobjects-weak*)
- (make-g-object-from-pointer pointer))))
+ (progn (log-for :gc "Now creating object for ~A~%" pointer)
+ (make-g-object-from-pointer pointer)))))
(defmethod translate-from-foreign (pointer (type foreign-g-object-type))
(get-g-object-for-pointer pointer))
(defmethod set-gvalue-for-type (gvalue-ptr (type-numeric (eql +g-type-interface+)) value)
(set-gvalue-object gvalue-ptr value))
-
-(defun g-signal-connect (object signal handler &key after)
- "Deprecated alias for @fun{connect-signal}"
- (connect-signal object signal handler :after after))
-
-(defun connect-signal (object signal handler &key after)
- "Connects the function to a signal for a particular object.
-If @code{after} is true, then the function will be called after the default handler of the signal.
-
-@arg[object]{an instance of @class{gobject}}
-@arg[signal]{a string; names the signal}
-@arg[handler]{a function; handles the signal. Number (and type) of arguments and return value type depends on the signal}
-@arg[after]{a boolean}"
- (g-signal-connect-closure (ensure-object-pointer object)
- signal
- (create-g-closure handler)
- after))
-
-(defun emit-signal (object signal-name &rest args)
- "Emits the signal.
-@arg[object]{an instance of @class{g-object}. Signal is emitted on this object}
-@arg[signal-name]{a string specifying the signal}
-@arg[args]{arguments for the signal}
-@return{none}"
- (let* ((object-type (g-type-from-object (pointer object)))
- (signal-info (parse-signal-name object-type signal-name)))
- (unless signal-info
- (error "Signal ~A not found on object ~A" signal-name object))
- (let ((params-count (length (signal-info-param-types signal-info))))
- (with-foreign-object (params 'g-value (1+ params-count))
- (set-g-value (mem-aref params 'g-value 0) object object-type :zero-g-value t)
- (iter (for i from 0 below params-count)
- (for arg in args)
- (for type in (signal-info-param-types signal-info))
- (set-g-value (mem-aref params 'g-value (1+ i)) arg type :zero-g-value t))
- (prog1
- (if (g-type= (signal-info-return-type signal-info) +g-type-void+)
- (g-signal-emitv params (signal-info-id signal-info) signal-name (null-pointer))
- (with-foreign-object (return-value 'g-value)
- (g-value-zero return-value)
- (g-value-init return-value (signal-info-return-type signal-info))
- (prog1 (parse-g-value return-value)
- (g-value-unset return-value))))
- (iter (for i from 0 below (1+ params-count))
- (g-value-unset (mem-aref params 'g-value i))))))))
(defvar *gobject-debug* nil)
-(defmacro debugf (&rest args)
- (when *gobject-debug*
- (apply 'format t args)))
\ No newline at end of file
+(defvar *debug-gc* nil)
+(defvar *debug-subclass* nil)
+
+(defvar *debug-stream* t)
+
+(defmacro log-for (categories control-string &rest args)
+ (let ((vars (iter (for sym in (if (listp categories) categories (list categories)))
+ (collect (intern (format nil "*DEBUG-~A*" (symbol-name sym)) (find-package :gobject))))))
+ `(progn
+ (when (or ,@vars)
+ (format *debug-stream* ,control-string ,@args))
+ nil)))
--- /dev/null
+(in-package :gobject)
+
+(defcstruct lisp-signal-handler-closure
+ (:parent-instance g-closure)
+ (:object :pointer)
+ (:function-id :int))
+
+(defun finalize-lisp-signal-handler-closure (closure)
+ (let* ((function-id (foreign-slot-value closure 'lisp-signal-handler-closure :function-id))
+ (addr (pointer-address (foreign-slot-value closure 'lisp-signal-handler-closure :object)))
+ (object (or (gethash addr *foreign-gobjects-strong*)
+ (gethash addr *foreign-gobjects-weak*))))
+ (when object
+ (delete-handler-from-object object function-id))))
+
+(defcallback lisp-signal-handler-closure-finalize :void
+ ((data :pointer) (closure (:pointer lisp-signal-handler-closure)))
+ (declare (ignore data))
+ (finalize-lisp-signal-handler-closure closure))
+
+(defun call-with-restarts (fn args)
+ (restart-case
+ (apply fn args)
+ (return-from-g-closure (&optional v) :report "Return value from closure" v)))
+
+(defcallback lisp-signal-handler-closure-marshal :void
+ ((closure (:pointer lisp-signal-handler-closure))
+ (return-value (:pointer g-value))
+ (count-of-args :uint)
+ (args (:pointer g-value))
+ (invocation-hint :pointer)
+ (marshal-data :pointer))
+ (declare (ignore invocation-hint marshal-data))
+ (let* ((args (parse-closure-arguments count-of-args args))
+ (function-id (foreign-slot-value closure 'lisp-signal-handler-closure :function-id))
+ (addr (pointer-address (foreign-slot-value closure 'lisp-signal-handler-closure :object)))
+ (object (or (gethash addr *foreign-gobjects-strong*)
+ (gethash addr *foreign-gobjects-weak*)))
+ (return-type (and (not (null-pointer-p return-value))
+ (g-value-type return-value)))
+ (fn (retrieve-handler-from-object object function-id))
+ (fn-result (call-with-restarts fn args)))
+ (when return-type
+ (set-g-value return-value fn-result return-type :g-value-init nil))))
+
+(defun parse-closure-arguments (count-of-args args)
+ (loop
+ for i from 0 below count-of-args
+ collect (parse-g-value (mem-aref args 'g-value i))))
+
+(defun create-signal-handler-closure (object fn)
+ (let ((function-id (save-handler-to-object object fn))
+ (closure (g-closure-new-simple (foreign-type-size 'lisp-signal-handler-closure) (null-pointer))))
+ (setf (foreign-slot-value closure 'lisp-signal-handler-closure :function-id) function-id
+ (foreign-slot-value closure 'lisp-signal-handler-closure :object) (pointer object))
+ (g-closure-add-finalize-notifier closure (null-pointer)
+ (callback lisp-signal-handler-closure-finalize))
+ (g-closure-set-marshal closure (callback lisp-signal-handler-closure-marshal))
+ closure))
+
+(defun find-free-signal-handler-id (object)
+ (iter (with handlers = (g-object-signal-handlers object))
+ (for i from 0 below (length handlers))
+ (finding i such-that (null (aref handlers i)))))
+
+(defun save-handler-to-object (object handler)
+ (assert handler)
+ (let ((id (find-free-signal-handler-id object))
+ (handlers (g-object-signal-handlers object)))
+ (if id
+ (setf (aref handlers id) handler)
+ (vector-push-extend handler handlers))))
+
+(defun retrieve-handler-from-object (object handler-id)
+ (aref (g-object-signal-handlers object) handler-id))
+
+(defun delete-handler-from-object (object handler-id)
+ (let ((handlers (g-object-signal-handlers object)))
+ (setf (aref handlers handler-id) nil)
+ (iter (while (plusp (length handlers)))
+ (while (null (aref handlers (1- (length handlers)))))
+ (vector-pop handlers))
+ nil))
+
+(defun connect-signal (object signal handler &key after)
+ "Connects the function to a signal for a particular object.
+If @code{after} is true, then the function will be called after the default handler of the signal.
+
+@arg[object]{an instance of @class{gobject}}
+@arg[signal]{a string; names the signal}
+@arg[handler]{a function; handles the signal. Number (and type) of arguments and return value type depends on the signal}
+@arg[after]{a boolean}"
+ (g-signal-connect-closure (pointer object)
+ signal
+ (create-signal-handler-closure object handler)
+ after))
+
+(defun g-signal-connect (object signal handler &key after)
+ "Deprecated alias for @fun{connect-signal}"
+ (connect-signal object signal handler :after after))
+
+(defun emit-signal (object signal-name &rest args)
+ "Emits the signal.
+@arg[object]{an instance of @class{g-object}. Signal is emitted on this object}
+@arg[signal-name]{a string specifying the signal}
+@arg[args]{arguments for the signal}
+@return{none}"
+ (let* ((object-type (g-type-from-object (pointer object)))
+ (signal-info (parse-signal-name object-type signal-name)))
+ (unless signal-info
+ (error "Signal ~A not found on object ~A" signal-name object))
+ (let ((params-count (length (signal-info-param-types signal-info))))
+ (with-foreign-object (params 'g-value (1+ params-count))
+ (set-g-value (mem-aref params 'g-value 0) object object-type :zero-g-value t)
+ (iter (for i from 0 below params-count)
+ (for arg in args)
+ (for type in (signal-info-param-types signal-info))
+ (set-g-value (mem-aref params 'g-value (1+ i)) arg type :zero-g-value t))
+ (prog1
+ (if (g-type= (signal-info-return-type signal-info) +g-type-void+)
+ (g-signal-emitv params (signal-info-id signal-info) signal-name (null-pointer))
+ (with-foreign-object (return-value 'g-value)
+ (g-value-zero return-value)
+ (g-value-init return-value (signal-info-return-type signal-info))
+ (prog1 (parse-g-value return-value)
+ (g-value-unset return-value))))
+ (iter (for i from 0 below (1+ params-count))
+ (g-value-unset (mem-aref params 'g-value i))))))))