5 :type cffi:foreign-pointer
11 :accessor g-object-has-reference
14 "Base class for GObject classes hierarchy."))
16 (defvar *foreign-gobjects-weak* (make-weak-hash-table :test 'equal :weakness :value))
17 (defvar *foreign-gobjects-strong* (make-hash-table :test 'equal))
18 (defvar *current-creating-object* nil)
20 (defun ref-count (pointer)
21 (foreign-slot-value (if (pointerp pointer) pointer (pointer pointer)) 'g-object-struct :ref-count))
23 (defmethod initialize-instance :around ((obj g-object) &key)
24 (let ((*current-creating-object* obj))
27 (defmethod initialize-instance :after ((obj g-object) &key &allow-other-keys)
28 (unless (slot-boundp obj 'pointer)
29 (error "Pointer slot is not initialized for ~A" obj))
30 (let* ((pointer (pointer obj))
31 (s (format nil "~A" obj)))
34 (debugf "~A is queued for GC (having ~A refs)~%" pointer (ref-count pointer))
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-remove-toggle-ref pointer (callback gobject-toggle-ref-toggled) (null-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 (defun should-ref-sink-at-creation (object)
72 ;;If object was not created from lisp-side, we should ref it
73 ;;If an object is regular g-object, we should not ref-sink it
74 ;;If an object is GInitiallyUnowned, then it is created with a floating reference, we should ref-sink it
75 ;;A special case is GtkWindow: we should ref-sink it anyway
77 ((eq object *current-creating-object*) ;; g_object_new returns objects with ref = 1, we should save _this_ ref
78 (g-object-is-floating (pointer object))) ;; but floating objects should be ref_sunk
80 (debugf "(should-ref-sink-at-creation ~A) => ~A~%" object r)
83 (defcallback gobject-toggle-ref-toggled :void
84 ((data :pointer) (pointer :pointer) (is-last-ref :boolean))
85 (declare (ignore data))
86 (debugf "~A is now ~A with ~A refs~%" pointer (if is-last-ref "weak pointer" "strong pointer") (ref-count pointer))
87 (debugf "obj: ~A~%" (or (gethash (pointer-address pointer) *foreign-gobjects-strong*)
88 (gethash (pointer-address pointer) *foreign-gobjects-weak*)))
90 (let ((obj (gethash (pointer-address pointer) *foreign-gobjects-strong*)))
93 (remhash (pointer-address pointer) *foreign-gobjects-strong*)
94 (setf (gethash (pointer-address pointer) *foreign-gobjects-weak*) obj))
95 (warn "GObject at ~A has no lisp-side (strong) reference" pointer)))
96 (let ((obj (gethash (pointer-address pointer) *foreign-gobjects-weak*)))
97 (unless obj (warn "GObject at ~A has no lisp-side (weak) reference" pointer))
98 (remhash (pointer-address pointer) *foreign-gobjects-weak*)
99 (setf (gethash (pointer-address pointer) *foreign-gobjects-strong*) obj))))
101 (defcallback gobject-weak-ref-finalized :void
102 ((data :pointer) (pointer :pointer))
103 (declare (ignore data))
104 (debugf "~A is weak-ref-finalized with ~A refs~%" pointer (ref-count pointer))
105 (remhash (pointer-address pointer) *foreign-gobjects-weak*)
106 (when (gethash (pointer-address pointer) *foreign-gobjects-strong*)
107 (warn "GObject at ~A was weak-ref-finalized while still holding lisp-side strong reference to it" pointer))
108 (remhash (pointer-address pointer) *foreign-gobjects-strong*))
110 (defun register-g-object (obj)
111 (debugf "registered GObject ~A with gobject ref-count ~A ~A~%" (pointer obj) (ref-count obj) (if (g-object-is-floating (pointer obj)) "(floating)" ""))
112 (when (should-ref-sink-at-creation obj)
113 (debugf "g_object_ref_sink(~A)~%" (pointer obj))
114 (g-object-ref-sink (pointer obj)))
115 (setf (g-object-has-reference obj) t)
116 (setf (gethash (pointer-address (pointer obj)) *foreign-gobjects-strong*) obj)
117 (g-object-add-toggle-ref (pointer obj) (callback gobject-toggle-ref-toggled) (null-pointer))
118 (g-object-unref (pointer obj)))
120 (defvar *registered-object-types* (make-hash-table :test 'equal))
121 (defun register-object-type (name type)
122 (setf (gethash name *registered-object-types*) type))
123 (defun registered-object-type-by-name (name)
124 (gethash name *registered-object-types*))
125 (defun get-g-object-lisp-type (g-type)
126 (setf g-type (ensure-g-type g-type))
128 while (not (zerop g-type))
129 for lisp-type = (gethash (g-type-name g-type) *registered-object-types*)
130 when lisp-type do (return lisp-type)
131 do (setf g-type (ensure-g-type (g-type-parent g-type)))))
133 (defun make-g-object-from-pointer (pointer)
134 (let* ((g-type (g-type-from-instance pointer))
135 (lisp-type (get-g-object-lisp-type g-type)))
137 (error "Type ~A is not registered with REGISTER-OBJECT-TYPE"
138 (g-type-name g-type)))
139 (g-object-ref pointer)
140 (make-instance lisp-type :pointer pointer)))
142 (define-foreign-type foreign-g-object-type ()
143 ((sub-type :reader sub-type :initarg :sub-type :initform 'g-object))
144 (:actual-type :pointer))
146 (define-parse-method g-object (&optional (sub-type 'g-object))
147 (make-instance 'foreign-g-object-type :sub-type sub-type))
149 (defmethod translate-to-foreign (object (type foreign-g-object-type))
153 ((pointerp object) object)
154 ((null (pointer object))
155 (error "Object ~A has been disposed" object))
156 ((typep object 'g-object)
157 (assert (typep object (sub-type type))
159 "Object ~A is not a subtype of ~A" object (sub-type type))
161 (t (error "Object ~A is not translatable as GObject*" object))))
163 (defun get-g-object-for-pointer (pointer)
164 (unless (null-pointer-p pointer)
165 (or (gethash (pointer-address pointer) *foreign-gobjects-strong*)
166 (gethash (pointer-address pointer) *foreign-gobjects-weak*)
167 (make-g-object-from-pointer pointer))))
169 (defmethod translate-from-foreign (pointer (type foreign-g-object-type))
170 (get-g-object-for-pointer pointer))
172 (register-object-type "GObject" 'g-object)
174 (defun ensure-g-type (type)
175 "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.
176 @arg[type]{a string or and integer}
177 @return{integer equal to GType of @code{type}}"
180 (string (or (g-type-from-name type)
181 (error "Type ~A is invalid" type)))))
183 (defun ensure-object-pointer (object)
184 (if (pointerp object)
187 (g-object (pointer object)))))
189 (defun parse-g-value-object (gvalue)
190 (get-g-object-for-pointer (g-value-get-object gvalue)))
192 (defun set-gvalue-object (gvalue value)
193 (g-value-set-object gvalue (if value (pointer value) (null-pointer))))
195 (defmethod parse-g-value-for-type (gvalue-ptr (type-numeric (eql +g-type-object+)) parse-kind)
196 (declare (ignore parse-kind))
197 (parse-g-value-object gvalue-ptr))
199 (defmethod parse-g-value-for-type (gvalue-ptr (type-numeric (eql +g-type-interface+)) parse-kind)
200 (declare (ignore parse-kind))
201 (parse-g-value-object gvalue-ptr))
203 (defmethod set-gvalue-for-type (gvalue-ptr (type-numeric (eql +g-type-object+)) value)
204 (set-gvalue-object gvalue-ptr value))
206 (defmethod set-gvalue-for-type (gvalue-ptr (type-numeric (eql +g-type-interface+)) value)
207 (set-gvalue-object gvalue-ptr value))
209 (defun g-signal-connect (object signal handler &key after)
210 "Deprecated alias for @fun{connect-signal}"
211 (connect-signal object signal handler :after after))
213 (defun connect-signal (object signal handler &key after)
214 "Connects the function to a signal for a particular object.
215 If @code{after} is true, then the function will be called after the default handler of the signal.
217 @arg[object]{an instance of @class{gobject}}
218 @arg[signal]{a string; names the signal}
219 @arg[handler]{a function; handles the signal. Number (and type) of arguments and return value type depends on the signal}
220 @arg[after]{a boolean}"
221 (g-signal-connect-closure (ensure-object-pointer object)
223 (create-g-closure handler)
226 (defun emit-signal (object signal-name &rest args)
228 @arg[object]{an instance of @class{g-object}. Signal is emitted on this object}
229 @arg[signal-name]{a string specifying the signal}
230 @arg[args]{arguments for the signal}
232 (let* ((object-type (g-type-from-object (pointer object)))
233 (signal-info (parse-signal-name object-type signal-name)))
235 (error "Signal ~A not found on object ~A" signal-name object))
236 (let ((params-count (length (signal-info-param-types signal-info))))
237 (with-foreign-object (params 'g-value (1+ params-count))
238 (set-g-value (mem-aref params 'g-value 0) object object-type :zero-g-value t)
239 (iter (for i from 0 below params-count)
241 (for type in (signal-info-param-types signal-info))
242 (set-g-value (mem-aref params 'g-value (1+ i)) arg type :zero-g-value t))
244 (if (g-type= (signal-info-return-type signal-info) +g-type-void+)
245 (g-signal-emitv params (signal-info-id signal-info) signal-name (null-pointer))
246 (with-foreign-object (return-value 'g-value)
247 (g-value-zero return-value)
248 (g-value-init return-value (signal-info-return-type signal-info))
249 (prog1 (parse-g-value return-value)
250 (g-value-unset return-value))))
251 (iter (for i from 0 below (1+ params-count))
252 (g-value-unset (mem-aref params 'g-value i))))))))