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)
24 (defvar *currently-making-object-p* nil)
27 (clrhash *foreign-gobjects-weak*)
28 (clrhash *foreign-gobjects-strong*)
29 (setf *current-creating-object* nil
30 *current-object-from-pointer* nil
31 *currently-making-object-p* nil))
33 (defun ref-count (pointer)
34 (foreign-slot-value (if (pointerp pointer) pointer (pointer pointer)) 'g-object-struct :ref-count))
36 (defmethod initialize-instance :around ((obj g-object) &key)
37 (when *currently-making-object-p*
38 (setf *currently-making-object-p* t))
39 (let ((*current-creating-object* obj))
40 (log-for :subclass "initialize-instance :around; *current-creating-object* = ~A~%" obj)
43 (defmethod initialize-instance :after ((obj g-object) &key &allow-other-keys)
44 (unless (slot-boundp obj 'pointer)
45 (error "Pointer slot is not initialized for ~A" obj))
46 (let* ((pointer (pointer obj))
47 (s (format nil "~A" obj)))
50 (log-for :gc "~A ~A is queued for GC (having ~A refs)~%"
51 (g-type-from-object pointer) pointer (ref-count pointer))
53 (g-object-dispose-carefully pointer)
55 (log-for :gc "Error in finalizer for ~A: ~A~%" s e)
56 (format t "Error in finalizer for ~A: ~A~%" s e))))))
57 (register-g-object obj)
60 (defvar *gobject-gc-hooks-lock* (make-recursive-lock "gobject-gc-hooks-lock"))
61 (defvar *gobject-gc-hooks* nil);;pointers to objects to be freed
63 (defun activate-gc-hooks ()
64 (with-recursive-lock-held (*gobject-gc-hooks-lock*)
65 (when *gobject-gc-hooks*
66 (log-for :gc "activating gc hooks for objects: ~A~%" *gobject-gc-hooks*)
68 for pointer in *gobject-gc-hooks*
69 do (g-object-remove-toggle-ref pointer (callback gobject-toggle-ref-toggled) (null-pointer)))
70 (setf *gobject-gc-hooks* nil))))
72 (defcallback g-idle-gc-hook :boolean ((data :pointer))
73 (declare (ignore data))
77 (defun register-gobject-for-gc (pointer)
78 (with-recursive-lock-held (*gobject-gc-hooks-lock*)
79 (let ((locks-were-present (not (null *gobject-gc-hooks*))))
80 (push pointer *gobject-gc-hooks*)
81 (unless locks-were-present
82 (log-for :gc "adding idle-gc-hook to main loop~%")
83 (g-idle-add (callback g-idle-gc-hook) (null-pointer))))))
85 (defun g-object-dispose-carefully (pointer)
87 (register-gobject-for-gc pointer)
89 (log-for :gc "Error in dispose: ~A~%" e)
90 (format t "Error in dispose: ~A~%" e))))
92 (defun should-ref-sink-at-creation (object)
93 ;;If object was not created from lisp-side, we should ref it
94 ;;If an object is regular g-object, we should not ref-sink it
95 ;;If an object is GInitiallyUnowned, then it is created with a floating reference, we should ref-sink it
96 ;;A special case is GtkWindow: we should ref-sink it anyway
98 ((equal *current-object-from-pointer* (pointer object))
99 (log-for :gc "*cur-obj-from-ptr* ")
100 t) ;; not new objects should be ref_sunk
101 ((eq object *current-creating-object*) ;; g_object_new returns objects with ref = 1, we should save _this_ ref
102 (typep object 'g-initially-unowned)) ;; but GInitiallyUnowned objects should be ref_sunk
104 (log-for :gc "(should-ref-sink-at-creation ~A) => ~A~%" object r)
107 (defcallback gobject-toggle-ref-toggled :void
108 ((data :pointer) (pointer :pointer) (is-last-ref :boolean))
109 (declare (ignore data))
110 (log-for :gc "~A is now ~A with ~A refs~%" pointer (if is-last-ref "weak pointer" "strong pointer") (ref-count pointer))
111 (log-for :gc "obj: ~A~%" (or (gethash (pointer-address pointer) *foreign-gobjects-strong*)
112 (gethash (pointer-address pointer) *foreign-gobjects-weak*)))
114 (let ((obj (gethash (pointer-address pointer) *foreign-gobjects-strong*)))
117 (remhash (pointer-address pointer) *foreign-gobjects-strong*)
118 (setf (gethash (pointer-address pointer) *foreign-gobjects-weak*) obj))
120 (log-for :gc "GObject at ~A has no lisp-side (strong) reference" pointer)
121 (warn "GObject at ~A has no lisp-side (strong) reference" pointer))))
122 (let ((obj (gethash (pointer-address pointer) *foreign-gobjects-weak*)))
124 (log-for :gc "GObject at ~A has no lisp-side (weak) reference" pointer)
125 (warn "GObject at ~A has no lisp-side (weak) reference" pointer))
126 (remhash (pointer-address pointer) *foreign-gobjects-weak*)
127 (setf (gethash (pointer-address pointer) *foreign-gobjects-strong*) obj))))
129 (defcallback gobject-weak-ref-finalized :void
130 ((data :pointer) (pointer :pointer))
131 (declare (ignore data))
132 (log-for :gc "~A is weak-ref-finalized with ~A refs~%" pointer (ref-count pointer))
133 (remhash (pointer-address pointer) *foreign-gobjects-weak*)
134 (when (gethash (pointer-address pointer) *foreign-gobjects-strong*)
135 (warn "GObject at ~A was weak-ref-finalized while still holding lisp-side strong reference to it" pointer)
136 (log-for :gc "GObject at ~A was weak-ref-finalized while still holding lisp-side strong reference to it" pointer))
137 (remhash (pointer-address pointer) *foreign-gobjects-strong*))
139 (defun register-g-object (obj)
140 (log-for :gc "registered GObject ~A (~A) with initial ref-count ~A ~A~%"
142 (ref-count obj) (if (g-object-is-floating (pointer obj)) "(floating)" ""))
143 (when (should-ref-sink-at-creation obj)
144 (log-for :gc "g_object_ref_sink(~A)~%" (pointer obj))
145 (g-object-ref-sink (pointer obj)))
146 (setf (g-object-has-reference obj) t)
147 (setf (gethash (pointer-address (pointer obj)) *foreign-gobjects-strong*) obj)
148 (g-object-add-toggle-ref (pointer obj) (callback gobject-toggle-ref-toggled) (null-pointer))
149 (g-object-unref (pointer obj)))
151 (defvar *registered-object-types* (make-hash-table :test 'equal))
152 (defun register-object-type (name type)
153 (setf (gethash name *registered-object-types*) type))
154 (defun registered-object-type-by-name (name)
155 (gethash name *registered-object-types*))
156 (defun get-g-object-lisp-type (g-type)
157 (setf g-type (ensure-g-type g-type))
159 while (not (zerop g-type))
160 for lisp-type = (gethash (g-type-name g-type) *registered-object-types*)
161 when lisp-type do (return lisp-type)
162 do (setf g-type (ensure-g-type (g-type-parent g-type)))))
164 (defun make-g-object-from-pointer (pointer)
165 (let* ((g-type (g-type-from-instance pointer))
166 (lisp-type (get-g-object-lisp-type g-type)))
168 (error "Type ~A is not registered with REGISTER-OBJECT-TYPE"
169 (g-type-name g-type)))
170 (let ((*current-object-from-pointer* pointer))
171 (make-instance lisp-type :pointer pointer))))
173 (define-foreign-type foreign-g-object-type ()
174 ((sub-type :reader sub-type :initarg :sub-type :initform 'g-object)
175 (already-referenced :reader foreign-g-object-type-already-referenced :initarg :already-referenced :initform nil))
176 (:actual-type :pointer))
178 (define-parse-method g-object (&rest args)
179 (let* ((sub-type (first (remove-if #'keywordp args)))
180 (flags (remove-if-not #'keywordp args))
181 (already-referenced (not (null (find :already-referenced flags)))))
182 (make-instance 'foreign-g-object-type :sub-type sub-type :already-referenced already-referenced)))
184 (defmethod translate-to-foreign (object (type foreign-g-object-type))
188 ((pointerp object) object)
189 ((null (pointer object))
190 (error "Object ~A has been disposed" object))
191 ((typep object 'g-object)
192 (when (sub-type type)
193 (assert (typep object (sub-type type))
195 "Object ~A is not a subtype of ~A" object (sub-type type)))
197 (t (error "Object ~A is not translatable as GObject*" object))))
199 (defun get-g-object-for-pointer (pointer)
200 (unless (null-pointer-p pointer)
201 (or (gethash (pointer-address pointer) *foreign-gobjects-strong*)
202 (gethash (pointer-address pointer) *foreign-gobjects-weak*)
203 (progn (log-for :gc "Now creating object for ~A~%" pointer)
204 (make-g-object-from-pointer pointer)))))
206 (defmethod translate-from-foreign (pointer (type foreign-g-object-type))
207 (let ((object (get-g-object-for-pointer pointer)))
208 (when (foreign-g-object-type-already-referenced type)
209 (g-object-unref (pointer object)))
212 (register-object-type "GObject" 'g-object)
214 (defun ensure-g-type (type)
215 "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.
216 @arg[type]{a string or and integer}
217 @return{integer equal to GType of @code{type}}"
220 (string (or (g-type-from-name type)
221 (error "Type ~A is invalid" type)))))
223 (defun ensure-object-pointer (object)
224 (if (pointerp object)
227 (g-object (pointer object)))))
229 (defun parse-g-value-object (gvalue)
230 (get-g-object-for-pointer (g-value-get-object gvalue)))
232 (defun set-gvalue-object (gvalue value)
233 (g-value-set-object gvalue (if value (pointer value) (null-pointer))))
235 (defmethod parse-g-value-for-type (gvalue-ptr (type-numeric (eql +g-type-object+)) parse-kind)
236 (declare (ignore parse-kind))
237 (parse-g-value-object gvalue-ptr))
239 (defmethod parse-g-value-for-type (gvalue-ptr (type-numeric (eql +g-type-interface+)) parse-kind)
240 (declare (ignore parse-kind))
241 (parse-g-value-object gvalue-ptr))
243 (defmethod set-gvalue-for-type (gvalue-ptr (type-numeric (eql +g-type-object+)) value)
244 (set-gvalue-object gvalue-ptr value))
246 (defmethod set-gvalue-for-type (gvalue-ptr (type-numeric (eql +g-type-interface+)) value)
247 (set-gvalue-object gvalue-ptr value))