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 release ((obj g-object
))
37 (cancel-finalization obj
)
38 (let ((p (pointer obj
)))
39 (setf (pointer obj
) nil
)
40 (g-object-dispose-carefully p
)))
42 (defmethod initialize-instance :around
((obj g-object
) &key
)
43 (when *currently-making-object-p
*
44 (setf *currently-making-object-p
* t
))
45 (let ((*current-creating-object
* obj
))
46 (log-for :subclass
"initialize-instance :around; *current-creating-object* = ~A~%" obj
)
49 (defmethod initialize-instance :after
((obj g-object
) &key
&allow-other-keys
)
50 (unless (slot-boundp obj
'pointer
)
51 (error "Pointer slot is not initialized for ~A" obj
))
52 (let* ((pointer (pointer obj
))
53 (s (format nil
"~A" obj
)))
56 (log-for :gc
"~A ~A is queued for GC (having ~A refs)~%"
57 (g-type-from-object pointer
) pointer
(ref-count pointer
))
59 (g-object-dispose-carefully pointer
)
61 (log-for :gc
"Error in finalizer for ~A: ~A~%" s e
)
62 (format t
"Error in finalizer for ~A: ~A~%" s e
))))))
63 (register-g-object obj
)
66 (defvar *gobject-gc-hooks-lock
* (make-recursive-lock "gobject-gc-hooks-lock"))
67 (defvar *gobject-gc-hooks
* nil
);;pointers to objects to be freed
69 (defun activate-gc-hooks ()
70 (with-recursive-lock-held (*gobject-gc-hooks-lock
*)
71 (when *gobject-gc-hooks
*
72 (log-for :gc
"activating gc hooks for objects: ~A~%" *gobject-gc-hooks
*)
74 for pointer in
*gobject-gc-hooks
*
75 do
(g-object-remove-toggle-ref pointer
(callback gobject-toggle-ref-toggled
) (null-pointer)))
76 (setf *gobject-gc-hooks
* nil
))))
78 (defcallback g-idle-gc-hook
:boolean
((data :pointer
))
79 (declare (ignore data
))
83 (defun register-gobject-for-gc (pointer)
84 (with-recursive-lock-held (*gobject-gc-hooks-lock
*)
85 (let ((locks-were-present (not (null *gobject-gc-hooks
*))))
86 (push pointer
*gobject-gc-hooks
*)
87 (unless locks-were-present
88 (log-for :gc
"adding idle-gc-hook to main loop~%")
89 (g-idle-add (callback g-idle-gc-hook
) (null-pointer))))))
91 (defun g-object-dispose-carefully (pointer)
93 (register-gobject-for-gc pointer
)
95 (log-for :gc
"Error in dispose: ~A~%" e
)
96 (format t
"Error in dispose: ~A~%" e
))))
98 (defun should-ref-sink-at-creation (object)
99 ;;If object was not created from lisp-side, we should ref it
100 ;;If an object is regular g-object, we should not ref-sink it
101 ;;If an object is GInitiallyUnowned, then it is created with a floating reference, we should ref-sink it
102 ;;A special case is GtkWindow: we should ref-sink it anyway
104 ((equal *current-object-from-pointer
* (pointer object
))
105 (log-for :gc
"*cur-obj-from-ptr* ")
106 t
) ;; not new objects should be ref_sunk
107 ((eq object
*current-creating-object
*) ;; g_object_new returns objects with ref = 1, we should save _this_ ref
108 (typep object
'g-initially-unowned
)) ;; but GInitiallyUnowned objects should be ref_sunk
110 (log-for :gc
"(should-ref-sink-at-creation ~A) => ~A~%" object r
)
113 (defcallback gobject-toggle-ref-toggled
:void
114 ((data :pointer
) (pointer :pointer
) (is-last-ref :boolean
))
115 (declare (ignore data
))
116 (log-for :gc
"~A is now ~A with ~A refs~%" pointer
(if is-last-ref
"weak pointer" "strong pointer") (ref-count pointer
))
117 (log-for :gc
"obj: ~A~%" (or (gethash (pointer-address pointer
) *foreign-gobjects-strong
*)
118 (gethash (pointer-address pointer
) *foreign-gobjects-weak
*)))
120 (let ((obj (gethash (pointer-address pointer
) *foreign-gobjects-strong
*)))
123 (remhash (pointer-address pointer
) *foreign-gobjects-strong
*)
124 (setf (gethash (pointer-address pointer
) *foreign-gobjects-weak
*) obj
))
126 (log-for :gc
"GObject at ~A has no lisp-side (strong) reference" pointer
)
127 (warn "GObject at ~A has no lisp-side (strong) reference" pointer
))))
128 (let ((obj (gethash (pointer-address pointer
) *foreign-gobjects-weak
*)))
130 (log-for :gc
"GObject at ~A has no lisp-side (weak) reference" pointer
)
131 (warn "GObject at ~A has no lisp-side (weak) reference" pointer
))
132 (remhash (pointer-address pointer
) *foreign-gobjects-weak
*)
133 (setf (gethash (pointer-address pointer
) *foreign-gobjects-strong
*) obj
))))
135 (defcallback gobject-weak-ref-finalized
:void
136 ((data :pointer
) (pointer :pointer
))
137 (declare (ignore data
))
138 (log-for :gc
"~A is weak-ref-finalized with ~A refs~%" pointer
(ref-count pointer
))
139 (remhash (pointer-address pointer
) *foreign-gobjects-weak
*)
140 (when (gethash (pointer-address pointer
) *foreign-gobjects-strong
*)
141 (warn "GObject at ~A was weak-ref-finalized while still holding lisp-side strong reference to it" pointer
)
142 (log-for :gc
"GObject at ~A was weak-ref-finalized while still holding lisp-side strong reference to it" pointer
))
143 (remhash (pointer-address pointer
) *foreign-gobjects-strong
*))
145 (defun register-g-object (obj)
146 (log-for :gc
"registered GObject ~A (~A) with initial ref-count ~A ~A~%"
148 (ref-count obj
) (if (g-object-is-floating (pointer obj
)) "(floating)" ""))
149 (when (should-ref-sink-at-creation obj
)
150 (log-for :gc
"g_object_ref_sink(~A)~%" (pointer obj
))
151 (g-object-ref-sink (pointer obj
)))
152 (setf (g-object-has-reference obj
) t
)
153 (setf (gethash (pointer-address (pointer obj
)) *foreign-gobjects-strong
*) obj
)
154 (g-object-add-toggle-ref (pointer obj
) (callback gobject-toggle-ref-toggled
) (null-pointer))
155 (g-object-unref (pointer obj
)))
157 (defvar *registered-object-types
* (make-hash-table :test
'equal
))
158 (defun register-object-type (name type
)
159 (setf (gethash name
*registered-object-types
*) type
))
160 (defun registered-object-type-by-name (name)
161 (gethash name
*registered-object-types
*))
162 (defun get-g-object-lisp-type (g-type)
163 (setf g-type
(ensure-g-type g-type
))
165 while
(not (zerop g-type
))
166 for lisp-type
= (gethash (g-type-name g-type
) *registered-object-types
*)
167 when lisp-type do
(return lisp-type
)
168 do
(setf g-type
(ensure-g-type (g-type-parent g-type
)))))
170 (defun make-g-object-from-pointer (pointer)
171 (let* ((g-type (g-type-from-instance pointer
))
172 (lisp-type (get-g-object-lisp-type g-type
)))
174 (error "Type ~A is not registered with REGISTER-OBJECT-TYPE"
175 (g-type-name g-type
)))
176 (let ((*current-object-from-pointer
* pointer
))
177 (make-instance lisp-type
:pointer pointer
))))
179 (define-foreign-type foreign-g-object-type
()
180 ((sub-type :reader sub-type
:initarg
:sub-type
:initform
'g-object
)
181 (already-referenced :reader foreign-g-object-type-already-referenced
:initarg
:already-referenced
:initform nil
))
182 (:actual-type
:pointer
))
184 (define-parse-method g-object
(&rest args
)
185 (let* ((sub-type (first (remove-if #'keywordp args
)))
186 (flags (remove-if-not #'keywordp args
))
187 (already-referenced (not (null (find :already-referenced flags
)))))
188 (make-instance 'foreign-g-object-type
:sub-type sub-type
:already-referenced already-referenced
)))
190 (defmethod translate-to-foreign (object (type foreign-g-object-type
))
194 ((pointerp object
) object
)
195 ((null (pointer object
))
196 (error "Object ~A has been disposed" object
))
197 ((typep object
'g-object
)
198 (when (sub-type type
)
199 (assert (typep object
(sub-type type
))
201 "Object ~A is not a subtype of ~A" object
(sub-type type
)))
203 (t (error "Object ~A is not translatable as GObject*" object
))))
205 (defun get-g-object-for-pointer (pointer)
206 (unless (null-pointer-p pointer
)
207 (or (gethash (pointer-address pointer
) *foreign-gobjects-strong
*)
208 (gethash (pointer-address pointer
) *foreign-gobjects-weak
*)
209 (progn (log-for :gc
"Now creating object for ~A~%" pointer
)
210 (make-g-object-from-pointer pointer
)))))
212 (defmethod translate-from-foreign (pointer (type foreign-g-object-type
))
213 (let ((object (get-g-object-for-pointer pointer
)))
214 (when (foreign-g-object-type-already-referenced type
)
215 (g-object-unref (pointer object
)))
218 (register-object-type "GObject" 'g-object
)
220 (defun ensure-g-type (type)
221 "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.
222 @arg[type]{a string or and integer}
223 @return{integer equal to GType of @code{type}}"
226 (string (or (g-type-from-name type
)
227 (error "Type ~A is invalid" type
)))))
229 (defun ensure-object-pointer (object)
230 (if (pointerp object
)
233 (g-object (pointer object
)))))
235 (defun parse-g-value-object (gvalue)
236 (get-g-object-for-pointer (g-value-get-object gvalue
)))
238 (defun set-gvalue-object (gvalue value
)
239 (g-value-set-object gvalue
(if value
(pointer value
) (null-pointer))))
241 (defmethod parse-g-value-for-type (gvalue-ptr (type-numeric (eql +g-type-object
+)) parse-kind
)
242 (declare (ignore parse-kind
))
243 (parse-g-value-object gvalue-ptr
))
245 (defmethod parse-g-value-for-type (gvalue-ptr (type-numeric (eql +g-type-interface
+)) parse-kind
)
246 (declare (ignore parse-kind
))
247 (parse-g-value-object gvalue-ptr
))
249 (defmethod set-gvalue-for-type (gvalue-ptr (type-numeric (eql +g-type-object
+)) value
)
250 (set-gvalue-object gvalue-ptr value
))
252 (defmethod set-gvalue-for-type (gvalue-ptr (type-numeric (eql +g-type-interface
+)) value
)
253 (set-gvalue-object gvalue-ptr value
))