5 :type cffi:foreign-pointer
11 :accessor g-object-has-reference
14 "Base class for GObject classes hierarchy."))
16 (defvar *foreign-gobjects* (make-weak-hash-table :test 'equal :weakness :value))
17 (defvar *foreign-gobjects-ref-count* (make-hash-table :test 'equal))
18 (defvar *lisp-objects-pointers* (make-hash-table :test 'equal))
19 (defvar *current-creating-object* nil)
21 (defun ref-count (pointer)
22 (foreign-slot-value (if (pointerp pointer) pointer (pointer pointer)) 'g-object-struct :ref-count))
24 (defmethod initialize-instance :around ((obj g-object) &key)
25 (let ((*current-creating-object* obj))
28 (defmethod initialize-instance :after ((obj g-object) &key &allow-other-keys)
29 (unless (slot-boundp obj 'pointer)
30 (error "Pointer slot is not initialized for ~A" obj))
31 (let* ((pointer (pointer obj))
32 (s (format nil "~A" obj)))
36 (g-object-dispose-carefully pointer)
37 (error (e) (format t "Error in finalizer for ~A: ~A~%" s e))))))
38 (register-g-object obj)
41 (defvar *gobject-gc-hooks-lock* (make-recursive-lock "gobject-gc-hooks-lock"))
42 (defvar *gobject-gc-hooks* nil);;pointers to objects to be freed
44 (defun activate-gc-hooks ()
45 (with-recursive-lock-held (*gobject-gc-hooks-lock*)
46 (when *gobject-gc-hooks*
47 (debugf "activating gc hooks for objects: ~A~%" *gobject-gc-hooks*)
49 for pointer in *gobject-gc-hooks*
50 do (g-object-unref pointer))
51 (setf *gobject-gc-hooks* nil))))
53 (defcallback g-idle-gc-hook :boolean ((data :pointer))
54 (declare (ignore data))
58 (defun register-gobject-for-gc (pointer)
59 (with-recursive-lock-held (*gobject-gc-hooks-lock*)
60 (let ((locks-were-present (not (null *gobject-gc-hooks*))))
61 (push pointer *gobject-gc-hooks*)
62 (unless locks-were-present
63 (debugf "adding idle-gc-hook to main loop~%")
64 (g-idle-add (callback g-idle-gc-hook) (null-pointer))))))
66 (defun g-object-dispose-carefully (pointer)
68 (register-gobject-for-gc pointer)
69 (error (e) (format t "Error in dispose: ~A~%" e))))
71 (defcallback weak-notify-print :void ((data :pointer) (object-pointer :pointer))
72 (declare (ignore data))
73 (debugf "g-object has finalized ~A ~A~%" (g-type-name (g-type-from-object object-pointer)) object-pointer))
75 (defun erase-pointer (data object-pointer)
76 (declare (ignore data))
77 (remhash (pointer-address object-pointer) *lisp-objects-pointers*))
79 (defcallback weak-notify-erase-pointer :void ((data :pointer) (object-pointer :pointer))
80 (erase-pointer data object-pointer))
82 (defun should-ref-sink-at-creation (object)
83 ;;If object was not created from lisp-side, we should ref it
84 ;;If an object is regular g-object, we should not ref-sink it
85 ;;If an object is GInitiallyUnowned, then it is created with a floating reference, we should ref-sink it
86 ;;A special case is GtkWindow: we should ref-sink it anyway
87 (if (g-object-has-reference object)
88 (let ((object-type (g-type-from-object (pointer object)))
89 (initially-unowned-type (g-type-from-name "GInitiallyUnowned")))
90 (g-type-is-a object-type initially-unowned-type))
93 (defun register-g-object (obj)
94 (debugf "registered GObject ~A with gobject ref-count ~A ~A~%" (pointer obj) (ref-count obj) (if (g-object-is-floating (pointer obj)) "(floating)" ""))
95 (when (should-ref-sink-at-creation obj)
96 (debugf "g_object_ref_sink(~A)~%" (pointer obj))
97 (g-object-ref-sink (pointer obj)))
98 (g-object-weak-ref (pointer obj) (callback weak-notify-print) (null-pointer))
99 (g-object-weak-ref (pointer obj) (callback weak-notify-erase-pointer) (null-pointer))
100 (setf (g-object-has-reference obj) t)
101 (setf (gethash (pointer-address (pointer obj)) *foreign-gobjects*)
103 (setf (gethash (pointer-address (pointer obj)) *foreign-gobjects-ref-count*) 1))
105 (defun g-object-dispose (pointer)
106 (unless (gethash (pointer-address pointer) *foreign-gobjects-ref-count*)
107 (debugf "GObject ~A is already disposed, signalling error~%" pointer)
108 (error "GObject ~A is already disposed" pointer))
109 (debugf "g_object_unref(~A) (of type ~A, lisp-value ~A) (lisp ref-count ~A, gobject ref-count ~A)~%"
111 (g-type-name (g-type-from-object pointer))
112 (gethash (pointer-address pointer) *foreign-gobjects*)
113 (gethash (pointer-address pointer) *foreign-gobjects-ref-count*)
115 (let ((object (gethash (pointer-address pointer) *foreign-gobjects*)))
117 (setf (pointer object) nil)
118 (cancel-finalization object)))
119 (remhash (pointer-address pointer) *foreign-gobjects*)
120 (remhash (pointer-address pointer) *foreign-gobjects-ref-count*)
121 (g-object-unref pointer))
123 (defmethod release ((object g-object))
124 (debugf "Releasing object ~A (type ~A, lisp-value ~A)~%" (pointer object) (when (pointer object) (g-type-name (g-type-from-object (pointer object)))) object)
125 (unless (and (pointer object) (gethash (pointer-address (pointer object)) *foreign-gobjects-ref-count*))
126 (error "Object ~A already disposed of from lisp side" object))
127 (decf (gethash (pointer-address (pointer object)) *foreign-gobjects-ref-count*))
128 (when (zerop (gethash (pointer-address (pointer object)) *foreign-gobjects-ref-count*))
129 (g-object-dispose (pointer object)))
132 (defvar *registered-object-types* (make-hash-table :test 'equal))
133 (defun register-object-type (name type)
134 (setf (gethash name *registered-object-types*) type))
135 (defun registered-object-type-by-name (name)
136 (gethash name *registered-object-types*))
137 (defun get-g-object-lisp-type (g-type)
138 (setf g-type (ensure-g-type g-type))
140 while (not (zerop g-type))
141 for lisp-type = (gethash (g-type-name g-type) *registered-object-types*)
142 when lisp-type do (return lisp-type)
143 do (setf g-type (ensure-g-type (g-type-parent g-type)))))
145 (defun make-g-object-from-pointer (pointer)
146 (let* ((g-type (g-type-from-instance pointer))
147 (lisp-type (get-g-object-lisp-type g-type)))
149 (error "Type ~A is not registered with REGISTER-OBJECT-TYPE"
150 (g-type-name g-type)))
151 (g-object-ref pointer)
152 (make-instance lisp-type :pointer pointer)))
154 (define-foreign-type foreign-g-object-type ()
155 ((sub-type :reader sub-type :initarg :sub-type :initform 'g-object))
156 (:actual-type :pointer))
158 (define-parse-method g-object (&optional (sub-type 'g-object))
159 (make-instance 'foreign-g-object-type :sub-type sub-type))
161 (defmethod translate-to-foreign (object (type foreign-g-object-type))
165 ((pointerp object) object)
166 ((null (pointer object))
167 (error "Object ~A has been disposed" object))
168 ((typep object 'g-object)
169 (assert (typep object (sub-type type))
171 "Object ~A is not a subtype of ~A" object (sub-type type))
173 (t (error "Object ~A is not translatable as GObject*" object))))
175 (defun get-g-object-for-pointer (pointer)
176 (unless (null-pointer-p pointer)
177 (let ((object (gethash (pointer-address pointer) *foreign-gobjects*)))
180 (incf (gethash (pointer-address pointer) *foreign-gobjects-ref-count*))
181 (debugf "increfering object ~A~%" pointer))
182 (make-g-object-from-pointer pointer)))))
184 (defmethod translate-from-foreign (pointer (type foreign-g-object-type))
185 (get-g-object-for-pointer pointer))
187 (register-object-type "GObject" 'g-object)
189 (defun ensure-g-type (type)
190 "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.
191 @arg[type]{a string or and integer}
192 @return{integer equal to GType of @code{type}}"
195 (string (or (g-type-from-name type)
196 (error "Type ~A is invalid" type)))))
198 (defun ensure-object-pointer (object)
199 (if (pointerp object)
202 (g-object (pointer object)))))
204 (defun parse-g-value-object (gvalue)
205 (get-g-object-for-pointer (g-value-get-object gvalue)))
207 (defun set-gvalue-object (gvalue value)
208 (g-value-set-object gvalue (if value (pointer value) (null-pointer))))
210 (defmethod parse-g-value-for-type (gvalue-ptr (type-numeric (eql +g-type-object+)) parse-kind)
211 (declare (ignore parse-kind))
212 (parse-g-value-object gvalue-ptr))
214 (defmethod parse-g-value-for-type (gvalue-ptr (type-numeric (eql +g-type-interface+)) parse-kind)
215 (declare (ignore parse-kind))
216 (parse-g-value-object gvalue-ptr))
218 (defmethod set-gvalue-for-type (gvalue-ptr (type-numeric (eql +g-type-object+)) value)
219 (set-gvalue-object gvalue-ptr value))
221 (defmethod set-gvalue-for-type (gvalue-ptr (type-numeric (eql +g-type-interface+)) value)
222 (set-gvalue-object gvalue-ptr value))
224 (defun g-signal-connect (object signal handler &key after)
225 "Deprecated alias for @fun{connect-signal}"
226 (connect-signal object signal handler :after after))
228 (defun connect-signal (object signal handler &key after)
229 "Connects the function to a signal for a particular object.
230 If @code{after} is true, then the function will be called after the default handler of the signal.
232 @arg[object]{an instance of @class{gobject}}
233 @arg[signal]{a string; names the signal}
234 @arg[handler]{a function; handles the signal. Number (and type) of arguments and return value type depends on the signal}
235 @arg[after]{a boolean}"
236 (g-signal-connect-closure (ensure-object-pointer object)
238 (create-g-closure handler)
241 (defun emit-signal (object signal-name &rest args)
243 @arg[object]{an instance of @class{g-object}. Signal is emitted on this object}
244 @arg[signal-name]{a string specifying the signal}
245 @arg[args]{arguments for the signal}
247 (let* ((object-type (g-type-from-object (pointer object)))
248 (signal-info (parse-signal-name object-type signal-name)))
250 (error "Signal ~A not found on object ~A" signal-name object))
251 (let ((params-count (length (signal-info-param-types signal-info))))
252 (with-foreign-object (params 'g-value (1+ params-count))
253 (set-g-value (mem-aref params 'g-value 0) object object-type :zero-g-value t)
254 (iter (for i from 0 below params-count)
256 (for type in (signal-info-param-types signal-info))
257 (set-g-value (mem-aref params 'g-value (1+ i)) arg type :zero-g-value t))
259 (if (g-type= (signal-info-return-type signal-info) +g-type-void+)
260 (g-signal-emitv params (signal-info-id signal-info) signal-name (null-pointer))
261 (with-foreign-object (return-value 'g-value)
262 (g-value-zero return-value)
263 (g-value-init return-value (signal-info-return-type signal-info))
264 (prog1 (parse-g-value return-value)
265 (g-value-unset return-value))))
266 (iter (for i from 0 below (1+ params-count))
267 (g-value-unset (mem-aref params 'g-value i))))))))