Fixed gvalue leakage
[clg] / glib / gobject.lisp
1 ;; Common Lisp bindings for GTK+ v2.0
2 ;; Copyright (C) 2000-2001 Espen S. Johnsen <esj@stud.cs.uit.no>
3 ;;
4 ;; This library is free software; you can redistribute it and/or
5 ;; modify it under the terms of the GNU Lesser General Public
6 ;; License as published by the Free Software Foundation; either
7 ;; version 2 of the License, or (at your option) any later version.
8 ;;
9 ;; This library is distributed in the hope that it will be useful,
10 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 ;; Lesser General Public License for more details.
13 ;;
14 ;; You should have received a copy of the GNU Lesser General Public
15 ;; License along with this library; if not, write to the Free Software
16 ;; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
18 ;; $Id: gobject.lisp,v 1.14 2004-10-28 09:34:35 espen Exp $
19
20 (in-package "GLIB")
21
22
23 (eval-when (:compile-toplevel :load-toplevel :execute)
24 (defclass gobject (ginstance)
25 ()
26 (:metaclass ginstance-class)
27 (:alien-name "GObject")
28 (:copy %object-ref)
29 (:free %object-unref)))
30
31
32 (defmethod initialize-instance ((object gobject) &rest initargs)
33 (let ((slotds (class-slots (class-of object)))
34 (names (make-array 0 :adjustable t :fill-pointer t))
35 (values (make-array 0 :adjustable t :fill-pointer t)))
36
37 (loop
38 as tmp = initargs then (cddr tmp) while tmp
39 as key = (first tmp)
40 as value = (second tmp)
41 as slotd = (find-if
42 #'(lambda (slotd)
43 (member key (slot-definition-initargs slotd)))
44 slotds)
45 when (and (typep slotd 'effective-gobject-slot-definition)
46 (slot-value slotd 'construct))
47 do (let ((type (find-type-number (slot-definition-type slotd))))
48 (vector-push-extend (slot-definition-pname slotd) names)
49 (vector-push-extend (gvalue-new type value) values)
50 (remf initargs key)))
51
52 (setf
53 (slot-value object 'location)
54 (if (zerop (length names))
55 (%gobject-new (type-number-of object))
56 (%gobject-newvv (type-number-of object) (length names) names values)))
57
58 (mapc #'gvalue-free values))
59
60 (apply #'call-next-method object initargs))
61
62
63
64 (defbinding (%gobject-new "g_object_new") () pointer
65 (type type-number)
66 (nil null))
67
68 (defbinding (%gobject-newvv "g_object_newvv") () pointer
69 (type type-number)
70 (n-parameters unsigned-int)
71 (names (vector string))
72 (values (vector gvalue)))
73
74
75 (defbinding %object-ref (type location) pointer
76 (location pointer))
77
78 (defbinding %object-unref (type location) nil
79 (location pointer))
80
81 (defun object-ref (object)
82 (%object-ref nil (proxy-location object)))
83
84 (defun object-unref (object)
85 (%object-unref nil (proxy-location object)))
86
87
88
89 ;;;; Property stuff
90
91 (defbinding %object-set-property () nil
92 (object gobject)
93 (name string)
94 (value gvalue))
95
96 (defbinding %object-get-property () nil
97 (object gobject)
98 (name string)
99 (value gvalue))
100
101 (defbinding %object-notify () nil
102 (object gobject)
103 (name string))
104
105 (defbinding object-freeze-notify () nil
106 (object gobject))
107
108 (defbinding object-thaw-notify () nil
109 (object gobject))
110
111 (defbinding %object-set-qdata-full () nil
112 (object gobject)
113 (id quark)
114 (data unsigned-long)
115 (destroy-marshal pointer))
116
117
118 ;;;; User data
119
120 (defun (setf object-data) (data object key &key (test #'eq))
121 (%object-set-qdata-full
122 object (quark-from-object key :test test)
123 (register-user-data data) *destroy-notify*)
124 data)
125
126 (defbinding %object-get-qdata () unsigned-long
127 (object gobject)
128 (id quark))
129
130 (defun object-data (object key &key (test #'eq))
131 (find-user-data
132 (%object-get-qdata object (quark-from-object key :test test))))
133
134
135
136 ;;;; Metaclass used for subclasses of gobject
137
138 (eval-when (:compile-toplevel :load-toplevel :execute)
139 (defclass gobject-class (ginstance-class)
140 ())
141
142 (defclass direct-gobject-slot-definition (direct-virtual-slot-definition)
143 ((pname :reader slot-definition-pname :initarg :pname)
144 (readable :initform t :reader slot-readable-p :initarg :readable)
145 (writable :initform t :reader slot-writable-p :initarg :writable)
146 (construct :initform nil :initarg :construct)))
147
148 (defclass effective-gobject-slot-definition (effective-virtual-slot-definition)
149 ((pname :reader slot-definition-pname :initarg :pname)
150 (readable :reader slot-readable-p :initarg :readable)
151 (writable :reader slot-writable-p :initarg :writable)
152 (construct :initarg :construct))))
153
154
155
156 ; (defbinding object-class-install-param () nil
157 ; (class pointer)
158 ; (id unsigned-int)
159 ; (parameter parameter))
160
161 ; (defbinding object-class-find-param-spec () parameter
162 ; (class pointer)
163 ; (name string))
164
165 (defun signal-name-to-string (name)
166 (substitute #\_ #\- (string-downcase (string name))))
167
168
169 (defmethod direct-slot-definition-class ((class gobject-class) &rest initargs)
170 (case (getf initargs :allocation)
171 (:property (find-class 'direct-gobject-slot-definition))
172 (t (call-next-method))))
173
174 (defmethod effective-slot-definition-class ((class gobject-class) &rest initargs)
175 (case (getf initargs :allocation)
176 (:property (find-class 'effective-gobject-slot-definition))
177 (t (call-next-method))))
178
179 (defmethod compute-effective-slot-definition-initargs ((class gobject-class) direct-slotds)
180 (if (eq (most-specific-slot-value direct-slotds 'allocation) :property)
181 (nconc
182 (list :pname (signal-name-to-string
183 (most-specific-slot-value direct-slotds 'pname))
184 :readable (most-specific-slot-value direct-slotds 'readable)
185 :writable (most-specific-slot-value direct-slotds 'writable)
186 :construct (most-specific-slot-value direct-slotds 'construct))
187 (call-next-method))
188 (call-next-method)))
189
190
191 (defmethod initialize-internal-slot-functions ((slotd effective-gobject-slot-definition))
192 (let* ((type (slot-definition-type slotd))
193 (pname (slot-definition-pname slotd))
194 (type-number (find-type-number type)))
195 (unless (slot-boundp slotd 'reader-function)
196 (setf
197 (slot-value slotd 'reader-function)
198 (if (slot-readable-p slotd)
199 #'(lambda (object)
200 (with-gc-disabled
201 (let ((gvalue (gvalue-new type-number)))
202 (%object-get-property object pname gvalue)
203 (unwind-protect
204 (funcall
205 (intern-reader-function (type-from-number type-number)) gvalue +gvalue-value-offset+) ; temporary workaround for wrong topological sorting of types
206 (gvalue-free gvalue t)))))
207 #'(lambda (value object)
208 (error "Slot is not readable: ~A" (slot-definition-name slotd))))))
209
210 (unless (slot-boundp slotd 'writer-function)
211 (setf
212 (slot-value slotd 'writer-function)
213 (if (slot-writable-p slotd)
214 #'(lambda (value object)
215 (with-gc-disabled
216 (let ((gvalue (gvalue-new type-number)))
217 (funcall
218 (intern-writer-function (type-from-number type-number)) ; temporary
219 value gvalue +gvalue-value-offset+)
220 (%object-set-property object pname gvalue)
221 (funcall
222 (intern-destroy-function (type-from-number type-number)) ; temporary
223 gvalue +gvalue-value-offset+)
224 (gvalue-free gvalue nil)
225 value)))
226 #'(lambda (value object)
227 (error "Slot is not writable: ~A" (slot-definition-name slotd))))))
228
229 (unless (slot-boundp slotd 'boundp-function)
230 (setf
231 (slot-value slotd 'boundp-function)
232 #'(lambda (object)
233 (declare (ignore object))
234 t))))
235 (call-next-method))
236
237
238 (defmethod validate-superclass ((class gobject-class)
239 (super pcl::standard-class))
240 ; (subtypep (class-name super) 'gobject)
241 t)
242
243
244
245 ;;;;
246
247 (defbinding %object-class-list-properties () pointer
248 (class pointer)
249 (n-properties unsigned-int :out))
250
251 (defun query-object-class-properties (type-number &optional
252 inherited-properties)
253 (let ((class (type-class-ref type-number)))
254 (multiple-value-bind (array length)
255 (%object-class-list-properties class)
256 (unwind-protect
257 (let ((all-properties
258 (map-c-array 'list #'identity array 'param length)))
259 (if (not inherited-properties)
260 (delete-if
261 #'(lambda (param)
262 (not (eql type-number (param-owner-type param))))
263 all-properties)
264 all-properties))
265 (deallocate-memory array)))))
266
267
268 (defun default-slot-name (name)
269 (intern (substitute #\- #\_ (string-upcase (string-upcase name)))))
270
271 (defun default-slot-accessor (class-name slot-name type)
272 (intern
273 (format
274 nil "~A-~A~A" class-name slot-name
275 (if (eq type 'boolean) "-P" ""))))
276
277 (defun expand-gobject-type (type-number &optional options
278 (metaclass 'gobject-class))
279 (let* ((supers (cons (supertype type-number) (implements type-number)))
280 (class (type-from-number type-number))
281 (manual-slots (getf options :slots))
282 (expanded-slots
283 (mapcar
284 #'(lambda (param)
285 (with-slots (name flags value-type documentation) param
286 (let* ((slot-name (default-slot-name name))
287 ; (slot-type value-type) ;(type-from-number value-type t))
288 (slot-type (or (type-from-number value-type) value-type))
289 (accessor
290 (default-slot-accessor class slot-name slot-type)));(type-from-number slot-type)))) ; temporary workaround for wrong topological sorting of types
291
292 `(,slot-name
293 :allocation :property
294 :pname ,name
295 ,@(cond
296 ((and
297 (member :writable flags)
298 (member :readable flags)
299 (not (member :construct-only flags)))
300 (list :accessor accessor))
301 ((and (member :writable flags)
302 (not (member :construct-only flags)))
303 (list :writer `(setf ,accessor)))
304 ((member :readable flags)
305 (list :reader accessor)))
306 ,@(when (or
307 (not (member :writable flags))
308 (member :construct-only flags))
309 (list :writable nil))
310 ,@(when (not (member :readable flags))
311 (list :readable nil))
312 ,@(when (or
313 (member :construct flags)
314 (member :construct-only flags))
315 (list :construct t))
316 ,@(when (or
317 (member :construct flags)
318 (member :construct-only flags)
319 (member :writable flags))
320 (list :initarg (intern (string slot-name) "KEYWORD")))
321 :type ,slot-type
322 ,@(when documentation
323 (list :documentation documentation))))))
324 (query-object-class-properties type-number))))
325
326 (dolist (slot-def (reverse manual-slots))
327 (let ((name (car slot-def))
328 (pname (getf (cdr slot-def) :pname)))
329 (setq
330 expanded-slots
331 (delete-if
332 #'(lambda (expanded-slot-def)
333 (or
334 (eq name (car expanded-slot-def))
335 (and
336 pname
337 (string= pname (getf (cdr expanded-slot-def) :pname)))))
338 expanded-slots))
339
340 (unless (getf (cdr slot-def) :ignore)
341 (push slot-def expanded-slots))))
342
343 `(progn
344 (defclass ,class ,supers
345 ,expanded-slots
346 (:metaclass ,metaclass)
347 (:alien-name ,(find-type-name type-number))))))
348
349
350 (register-derivable-type 'gobject "GObject" 'expand-gobject-type)
351