5 :type cffi:foreign-pointer
11 :accessor g-object-has-reference
15 :initform (make-array 0 :adjustable t :fill-pointer t)
16 :reader g-object-signal-handlers))
18 "Base class for GObject classes hierarchy."))
20 (defvar *foreign-gobjects-weak* (make-weak-hash-table :test 'equal :weakness :value))
21 (defvar *foreign-gobjects-strong* (make-hash-table :test 'equal))
22 (defvar *current-creating-object* nil)
23 (defvar *current-object-from-pointer* nil)
25 (defun ref-count (pointer)
26 (foreign-slot-value (if (pointerp pointer) pointer (pointer pointer)) 'g-object-struct :ref-count))
28 (defmethod initialize-instance :around ((obj g-object) &key)
29 (let ((*current-creating-object* obj))
32 (defmethod initialize-instance :after ((obj g-object) &key &allow-other-keys)
33 (unless (slot-boundp obj 'pointer)
34 (error "Pointer slot is not initialized for ~A" obj))
35 (let* ((pointer (pointer obj))
36 (s (format nil "~A" obj)))
39 (log-for :gc "~A ~A is queued for GC (having ~A refs)~%"
40 (g-type-from-object pointer) pointer (ref-count pointer))
42 (g-object-dispose-carefully pointer)
44 (log-for :gc "Error in finalizer for ~A: ~A~%" s e)
45 (format t "Error in finalizer for ~A: ~A~%" s e))))))
46 (register-g-object obj)
49 (defvar *gobject-gc-hooks-lock* (make-recursive-lock "gobject-gc-hooks-lock"))
50 (defvar *gobject-gc-hooks* nil);;pointers to objects to be freed
52 (defun activate-gc-hooks ()
53 (with-recursive-lock-held (*gobject-gc-hooks-lock*)
54 (when *gobject-gc-hooks*
55 (log-for :gc "activating gc hooks for objects: ~A~%" *gobject-gc-hooks*)
57 for pointer in *gobject-gc-hooks*
58 do (g-object-remove-toggle-ref pointer (callback gobject-toggle-ref-toggled) (null-pointer)))
59 (setf *gobject-gc-hooks* nil))))
61 (defcallback g-idle-gc-hook :boolean ((data :pointer))
62 (declare (ignore data))
66 (defun register-gobject-for-gc (pointer)
67 (with-recursive-lock-held (*gobject-gc-hooks-lock*)
68 (let ((locks-were-present (not (null *gobject-gc-hooks*))))
69 (push pointer *gobject-gc-hooks*)
70 (unless locks-were-present
71 (log-for :gc "adding idle-gc-hook to main loop~%")
72 (g-idle-add (callback g-idle-gc-hook) (null-pointer))))))
74 (defun g-object-dispose-carefully (pointer)
76 (register-gobject-for-gc pointer)
78 (log-for :gc "Error in dispose: ~A~%" e)
79 (format t "Error in dispose: ~A~%" e))))
81 (defun should-ref-sink-at-creation (object)
82 ;;If object was not created from lisp-side, we should ref it
83 ;;If an object is regular g-object, we should not ref-sink it
84 ;;If an object is GInitiallyUnowned, then it is created with a floating reference, we should ref-sink it
85 ;;A special case is GtkWindow: we should ref-sink it anyway
87 ((equal *current-object-from-pointer* (pointer object))
88 (log-for :gc "*cur-obj-from-ptr* ")
89 t) ;; not new objects should be ref_sunk
90 ((eq object *current-creating-object*) ;; g_object_new returns objects with ref = 1, we should save _this_ ref
91 (typep object 'g-initially-unowned)) ;; but GInitiallyUnowned objects should be ref_sunk
93 (log-for :gc "(should-ref-sink-at-creation ~A) => ~A~%" object r)
96 (defcallback gobject-toggle-ref-toggled :void
97 ((data :pointer) (pointer :pointer) (is-last-ref :boolean))
98 (declare (ignore data))
99 (log-for :gc "~A is now ~A with ~A refs~%" pointer (if is-last-ref "weak pointer" "strong pointer") (ref-count pointer))
100 (log-for :gc "obj: ~A~%" (or (gethash (pointer-address pointer) *foreign-gobjects-strong*)
101 (gethash (pointer-address pointer) *foreign-gobjects-weak*)))
103 (let ((obj (gethash (pointer-address pointer) *foreign-gobjects-strong*)))
106 (remhash (pointer-address pointer) *foreign-gobjects-strong*)
107 (setf (gethash (pointer-address pointer) *foreign-gobjects-weak*) obj))
109 (log-for :gc "GObject at ~A has no lisp-side (strong) reference" pointer)
110 (warn "GObject at ~A has no lisp-side (strong) reference" pointer))))
111 (let ((obj (gethash (pointer-address pointer) *foreign-gobjects-weak*)))
113 (log-for :gc "GObject at ~A has no lisp-side (weak) reference" pointer)
114 (warn "GObject at ~A has no lisp-side (weak) reference" pointer))
115 (remhash (pointer-address pointer) *foreign-gobjects-weak*)
116 (setf (gethash (pointer-address pointer) *foreign-gobjects-strong*) obj))))
118 (defcallback gobject-weak-ref-finalized :void
119 ((data :pointer) (pointer :pointer))
120 (declare (ignore data))
121 (log-for :gc "~A is weak-ref-finalized with ~A refs~%" pointer (ref-count pointer))
122 (remhash (pointer-address pointer) *foreign-gobjects-weak*)
123 (when (gethash (pointer-address pointer) *foreign-gobjects-strong*)
124 (warn "GObject at ~A was weak-ref-finalized while still holding lisp-side strong reference to it" pointer)
125 (log-for :gc "GObject at ~A was weak-ref-finalized while still holding lisp-side strong reference to it" pointer))
126 (remhash (pointer-address pointer) *foreign-gobjects-strong*))
128 (defun register-g-object (obj)
129 (log-for :gc "registered GObject ~A (~A) with initial ref-count ~A ~A~%"
131 (ref-count obj) (if (g-object-is-floating (pointer obj)) "(floating)" ""))
132 (when (should-ref-sink-at-creation obj)
133 (log-for :gc "g_object_ref_sink(~A)~%" (pointer obj))
134 (g-object-ref-sink (pointer obj)))
135 (setf (g-object-has-reference obj) t)
136 (setf (gethash (pointer-address (pointer obj)) *foreign-gobjects-strong*) obj)
137 (g-object-add-toggle-ref (pointer obj) (callback gobject-toggle-ref-toggled) (null-pointer))
138 (g-object-unref (pointer obj)))
140 (defvar *registered-object-types* (make-hash-table :test 'equal))
141 (defun register-object-type (name type)
142 (setf (gethash name *registered-object-types*) type))
143 (defun registered-object-type-by-name (name)
144 (gethash name *registered-object-types*))
145 (defun get-g-object-lisp-type (g-type)
146 (setf g-type (ensure-g-type g-type))
148 while (not (zerop g-type))
149 for lisp-type = (gethash (g-type-name g-type) *registered-object-types*)
150 when lisp-type do (return lisp-type)
151 do (setf g-type (ensure-g-type (g-type-parent g-type)))))
153 (defun make-g-object-from-pointer (pointer)
154 (let* ((g-type (g-type-from-instance pointer))
155 (lisp-type (get-g-object-lisp-type g-type)))
157 (error "Type ~A is not registered with REGISTER-OBJECT-TYPE"
158 (g-type-name g-type)))
159 (let ((*current-object-from-pointer* pointer))
160 (make-instance lisp-type :pointer pointer))))
162 (define-foreign-type foreign-g-object-type ()
163 ((sub-type :reader sub-type :initarg :sub-type :initform 'g-object))
164 (:actual-type :pointer))
166 (define-parse-method g-object (&optional (sub-type 'g-object))
167 (make-instance 'foreign-g-object-type :sub-type sub-type))
169 (defmethod translate-to-foreign (object (type foreign-g-object-type))
173 ((pointerp object) object)
174 ((null (pointer object))
175 (error "Object ~A has been disposed" object))
176 ((typep object 'g-object)
177 (assert (typep object (sub-type type))
179 "Object ~A is not a subtype of ~A" object (sub-type type))
181 (t (error "Object ~A is not translatable as GObject*" object))))
183 (defun get-g-object-for-pointer (pointer)
184 (unless (null-pointer-p pointer)
185 (or (gethash (pointer-address pointer) *foreign-gobjects-strong*)
186 (gethash (pointer-address pointer) *foreign-gobjects-weak*)
187 (progn (log-for :gc "Now creating object for ~A~%" pointer)
188 (make-g-object-from-pointer pointer)))))
190 (defmethod translate-from-foreign (pointer (type foreign-g-object-type))
191 (get-g-object-for-pointer pointer))
193 (register-object-type "GObject" 'g-object)
195 (defun ensure-g-type (type)
196 "Returns the GType value for a given type. If type is an integer, it is returned. If type is a string, GType corresponding to this type name is looked up and returned.
197 @arg[type]{a string or and integer}
198 @return{integer equal to GType of @code{type}}"
201 (string (or (g-type-from-name type)
202 (error "Type ~A is invalid" type)))))
204 (defun ensure-object-pointer (object)
205 (if (pointerp object)
208 (g-object (pointer object)))))
210 (defun parse-g-value-object (gvalue)
211 (get-g-object-for-pointer (g-value-get-object gvalue)))
213 (defun set-gvalue-object (gvalue value)
214 (g-value-set-object gvalue (if value (pointer value) (null-pointer))))
216 (defmethod parse-g-value-for-type (gvalue-ptr (type-numeric (eql +g-type-object+)) parse-kind)
217 (declare (ignore parse-kind))
218 (parse-g-value-object gvalue-ptr))
220 (defmethod parse-g-value-for-type (gvalue-ptr (type-numeric (eql +g-type-interface+)) parse-kind)
221 (declare (ignore parse-kind))
222 (parse-g-value-object gvalue-ptr))
224 (defmethod set-gvalue-for-type (gvalue-ptr (type-numeric (eql +g-type-object+)) value)
225 (set-gvalue-object gvalue-ptr value))
227 (defmethod set-gvalue-for-type (gvalue-ptr (type-numeric (eql +g-type-interface+)) value)
228 (set-gvalue-object gvalue-ptr value))