chiark / gitweb /
Fixed broken reference counting
[clg] / glib / gobject.lisp
CommitLineData
112ac1d3 1;; Common Lisp bindings for GTK+ v2.x
2;; Copyright 2000-2005 Espen S. Johnsen <espen@users.sf.net>
560af5c5 3;;
112ac1d3 4;; Permission is hereby granted, free of charge, to any person obtaining
5;; a copy of this software and associated documentation files (the
6;; "Software"), to deal in the Software without restriction, including
7;; without limitation the rights to use, copy, modify, merge, publish,
8;; distribute, sublicense, and/or sell copies of the Software, and to
9;; permit persons to whom the Software is furnished to do so, subject to
10;; the following conditions:
560af5c5 11;;
112ac1d3 12;; The above copyright notice and this permission notice shall be
13;; included in all copies or substantial portions of the Software.
560af5c5 14;;
112ac1d3 15;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18;; IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19;; CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20;; TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21;; SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
560af5c5 22
008c81d9 23;; $Id: gobject.lisp,v 1.41 2006-02-03 12:44:32 espen Exp $
560af5c5 24
25(in-package "GLIB")
26
27
2176a9c7 28;;;; Metaclass used for subclasses of gobject
29
30(eval-when (:compile-toplevel :load-toplevel :execute)
31 (defclass gobject-class (ginstance-class)
b036d261 32 ((instance-slots-p :initform nil
33 :documentation "Non NIL if the class has slots with instance allocation")))
2176a9c7 34
73572c12 35 (defmethod validate-superclass ((class gobject-class) (super standard-class))
2176a9c7 36; (subtypep (class-name super) 'gobject)
37 t))
38
39(defclass direct-property-slot-definition (direct-virtual-slot-definition)
40 ((pname :reader slot-definition-pname :initarg :pname)
41 (readable :initform t :reader slot-readable-p :initarg :readable)
42 (writable :initform t :reader slot-writable-p :initarg :writable)
43 (construct :initform nil :initarg :construct)))
44
45(defclass effective-property-slot-definition (effective-virtual-slot-definition)
46 ((pname :reader slot-definition-pname :initarg :pname)
47 (readable :reader slot-readable-p :initarg :readable)
48 (writable :reader slot-writable-p :initarg :writable)
174e8a5c 49 (construct :initarg :construct)))
50
51(defclass direct-user-data-slot-definition (direct-virtual-slot-definition)
52 ())
53
54(defclass effective-user-data-slot-definition (effective-virtual-slot-definition)
55 ())
56
2176a9c7 57
58(defbinding %object-ref () pointer
59 (location pointer))
60
61(defbinding %object-unref () nil
62 (location pointer))
63
0090bb5e 64#+glib2.8
008c81d9 65(progn
66 (defcallback toggle-ref-callback (nil (data pointer) (location pointer) (last-ref-p boolean))
67 (if last-ref-p
68 (cache-instance (find-cached-instance location) t)
69 (cache-instance (find-cached-instance location) nil)))
70
71 (defbinding %object-add-toggle-ref () pointer
72 (location pointer)
73 ((callback toggle-ref-callback) pointer)
74 (nil null))
75
76 (defbinding %object-remove-toggle-ref () pointer
77 (location pointer)
78 ((callback toggle-ref-callback) pointer)
79 (nil null)))
b036d261 80
2176a9c7 81(defmethod reference-foreign ((class gobject-class) location)
82 (declare (ignore class))
5eb27832 83 (%object-ref location))
2176a9c7 84
85(defmethod unreference-foreign ((class gobject-class) location)
86 (declare (ignore class))
008c81d9 87 (%object-unref location))
b036d261 88
008c81d9 89#+debug-ref-counting
90(progn
91 (defcallback weak-ref-callback (nil (data pointer) (location pointer))
92 (format t "Object at 0x~8,'0X being finalized~%" (sap-int location)))
93
94 (defbinding %object-weak-ref () pointer
95 (location pointer)
96 ((callback weak-ref-callback) pointer)
97 (nil null)))
2176a9c7 98
99
100; (defbinding object-class-install-param () nil
101; (class pointer)
102; (id unsigned-int)
103; (parameter parameter))
104
105; (defbinding object-class-find-param-spec () parameter
106; (class pointer)
107; (name string))
108
109(defun signal-name-to-string (name)
110 (substitute #\_ #\- (string-downcase (string name))))
111
112
113(defmethod direct-slot-definition-class ((class gobject-class) &rest initargs)
114 (case (getf initargs :allocation)
115 (:property (find-class 'direct-property-slot-definition))
174e8a5c 116 (:user-data (find-class 'direct-user-data-slot-definition))
2176a9c7 117 (t (call-next-method))))
118
119(defmethod effective-slot-definition-class ((class gobject-class) &rest initargs)
120 (case (getf initargs :allocation)
121 (:property (find-class 'effective-property-slot-definition))
174e8a5c 122 (:user-data (find-class 'effective-user-data-slot-definition))
2176a9c7 123 (t (call-next-method))))
124
125(defmethod compute-effective-slot-definition-initargs ((class gobject-class) direct-slotds)
eeda1c2d 126 (if (typep (first direct-slotds) 'direct-property-slot-definition)
2176a9c7 127 (nconc
128 (list :pname (signal-name-to-string
129 (most-specific-slot-value direct-slotds 'pname))
130 :readable (most-specific-slot-value direct-slotds 'readable)
131 :writable (most-specific-slot-value direct-slotds 'writable)
132 :construct (most-specific-slot-value direct-slotds 'construct))
133 (call-next-method))
134 (call-next-method)))
135
136
137(defmethod initialize-internal-slot-functions ((slotd effective-property-slot-definition))
dfa4f314 138 (let ((type (slot-definition-type slotd))
139 (pname (slot-definition-pname slotd)))
eeda1c2d 140 (when (and (not (slot-boundp slotd 'getter)) (slot-readable-p slotd))
2176a9c7 141 (setf
eeda1c2d 142 (slot-value slotd 'getter)
dfa4f314 143 (let ((reader nil))
eeda1c2d 144 #'(lambda (object)
21299acf 145 (unless reader
146 (setq reader (reader-function type)))
dfa4f314 147 (let ((gvalue (gvalue-new type)))
eeda1c2d 148 (%object-get-property object pname gvalue)
149 (unwind-protect
150 (funcall reader gvalue +gvalue-value-offset+)
151 (gvalue-free gvalue t)))))))
2176a9c7 152
eeda1c2d 153 (when (and (not (slot-boundp slotd 'setter)) (slot-writable-p slotd))
2176a9c7 154 (setf
eeda1c2d 155 (slot-value slotd 'setter)
dfa4f314 156 (let ((writer nil))
eeda1c2d 157 #'(lambda (value object)
21299acf 158 (unless writer
159 (setq writer (writer-function type)))
dfa4f314 160 (let ((gvalue (gvalue-new type)))
eeda1c2d 161 (funcall writer value gvalue +gvalue-value-offset+)
162 (%object-set-property object pname gvalue)
163 (gvalue-free gvalue t)
164 value))))))
165
2176a9c7 166 (call-next-method))
167
174e8a5c 168(defmethod initialize-internal-slot-functions ((slotd effective-user-data-slot-definition))
169 (let ((slot-name (slot-definition-name slotd)))
56870989 170 (unless (slot-boundp slotd 'getter)
171 (setf
172 (slot-value slotd 'getter)
173 #'(lambda (object)
174 (prog1 (user-data object slot-name)))))
175 (unless (slot-boundp slotd 'setter)
176 (setf
177 (slot-value slotd 'setter)
178 #'(lambda (value object)
179 (setf (user-data object slot-name) value))))
180 (unless (slot-boundp slotd 'boundp)
181 (setf
182 (slot-value slotd 'boundp)
183 #'(lambda (object)
184 (user-data-p object slot-name)))))
174e8a5c 185 (call-next-method))
186
b036d261 187(defmethod shared-initialize :after ((class gobject-class) names &rest initargs)
188 (declare (ignore initargs))
189 (when (some #'(lambda (slotd)
190 (and
191 (eq (slot-definition-allocation slotd) :instance)
e2ebafb1 192 (not (typep slotd 'effective-special-slot-definition))))
b036d261 193 (class-slots class))
194 (setf (slot-value class 'instance-slots-p) t)))
195
196
2176a9c7 197
198;;;; Super class for all classes in the GObject type hierarchy
199
560af5c5 200(eval-when (:compile-toplevel :load-toplevel :execute)
c8c48a4c 201 (defclass gobject (ginstance)
560af5c5 202 ()
2176a9c7 203 (:metaclass gobject-class)
dfa4f314 204 (:gtype "GObject")))
9adccb27 205
eeda1c2d 206
207(defun initial-add (object function initargs key pkey)
208 (loop
209 as (initarg value . rest) = initargs then rest
210 do (cond
211 ((eq initarg key) (funcall function object value))
212 ((eq initarg pkey) (mapc #'(lambda (value)
213 (funcall function object value))
214 value)))
215 while rest))
216
217(defun initial-apply-add (object function initargs key pkey)
218 (initial-add object #'(lambda (object value)
219 (apply function object (mklist value)))
220 initargs key pkey))
221
222
008c81d9 223(defmethod initialize-instance :around ((object gobject) &rest initargs)
224 (declare (ignore initargs))
225 (call-next-method)
226 #+debug-ref-counting(%object-weak-ref (proxy-location object))
227 #+glib2.8
228 (when (slot-value (class-of object) 'instance-slots-p)
229 (with-slots (location) object
230 (%object-add-toggle-ref location)
231 (%object-unref location))))
232
233
82747bbd 234(defmethod initialize-instance ((object gobject) &rest initargs)
040579dd 235 (unless (slot-boundp object 'location)
b036d261 236 ;; Extract initargs which we should pass directly to the GObject
040579dd 237 ;; constructor
238 (let* ((slotds (class-slots (class-of object)))
239 (args (when initargs
240 (loop
241 as (key value . rest) = initargs then rest
242 as slotd = (find-if
243 #'(lambda (slotd)
244 (member key (slot-definition-initargs slotd)))
245 slotds)
246 when (and (typep slotd 'effective-property-slot-definition)
247 (slot-value slotd 'construct))
248 collect (progn
249 (remf initargs key)
250 (list
251 (slot-definition-pname slotd)
252 (slot-definition-type slotd)
253 value))
254 while rest))))
255 (if args
256 (let* ((string-size (size-of 'string))
257 (string-writer (writer-function 'string))
258 (string-destroy (destroy-function 'string))
259 (params (allocate-memory
260 (* (length args) (+ string-size +gvalue-size+)))))
9adccb27 261 (loop
040579dd 262 for (pname type value) in args
9adccb27 263 as tmp = params then (sap+ tmp (+ string-size +gvalue-size+))
040579dd 264 do (funcall string-writer pname tmp)
265 (gvalue-init (sap+ tmp string-size) type value))
266 (unwind-protect
267 (setf
268 (slot-value object 'location)
269 (%gobject-newv (type-number-of object) (length args) params))
270 (loop
271 repeat (length args)
272 as tmp = params then (sap+ tmp (+ string-size +gvalue-size+))
273 do (funcall string-destroy tmp)
274 (gvalue-unset (sap+ tmp string-size)))
275 (deallocate-memory params)))
8755b1a5 276 (setf
277 (slot-value object 'location)
040579dd 278 (%gobject-new (type-number-of object))))))
8755b1a5 279
4d83a8a6 280 (apply #'call-next-method object initargs))
281
282
1dbf4216 283(defmethod instance-finalizer ((instance gobject))
284 (let ((location (proxy-location instance)))
0090bb5e 285 #+glib2.8
b036d261 286 (if (slot-value (class-of instance) 'instance-slots-p)
287 #'(lambda ()
008c81d9 288 #+debug-ref-counting
289 (format t "Finalizing proxy for 0x~8,'0X~%" (sap-int location))
b036d261 290 (remove-cached-instance location)
291 (%object-remove-toggle-ref location))
292 #'(lambda ()
008c81d9 293 #+debug-ref-counting
294 (format t "Finalizing proxy for 0x~8,'0X~%" (sap-int location))
b036d261 295 (remove-cached-instance location)
5eb27832 296 (%object-unref location)))
0090bb5e 297 #-glib2.8
5eb27832 298 #'(lambda ()
299 (remove-cached-instance location)
300 (%object-unref location))))
1dbf4216 301
9adccb27 302
d4b21b08 303(defbinding (%gobject-new "g_object_new") () pointer
a9044181 304 (type type-number)
305 (nil null))
560af5c5 306
9adccb27 307(defbinding (%gobject-newv "g_object_newv") () pointer
4d83a8a6 308 (type type-number)
309 (n-parameters unsigned-int)
9adccb27 310 (params pointer))
de8516ca 311
312
d4b21b08 313
323d4265 314;;;; Property stuff
560af5c5 315
26b133ed 316(defbinding %object-set-property () nil
c8c48a4c 317 (object gobject)
318 (name string)
319 (value gvalue))
86d9d6ab 320
26b133ed 321(defbinding %object-get-property () nil
c8c48a4c 322 (object gobject)
323 (name string)
a9044181 324 (value gvalue))
86d9d6ab 325
26b133ed 326(defbinding %object-notify () nil
c8c48a4c 327 (object gobject)
328 (name string))
86d9d6ab 329
26b133ed 330(defbinding object-freeze-notify () nil
a9044181 331 (object gobject))
86d9d6ab 332
26b133ed 333(defbinding object-thaw-notify () nil
a9044181 334 (object gobject))
86d9d6ab 335
a00ba56a 336
337;;;; User data
338
26b133ed 339(defbinding %object-set-qdata-full () nil
86d9d6ab 340 (object gobject)
341 (id quark)
342 (data unsigned-long)
343 (destroy-marshal pointer))
344
73572c12 345(defcallback user-data-destroy-func (nil (id unsigned-int))
346 (destroy-user-data id))
347
348(export 'user-data-destroy-func)
349
174e8a5c 350(defun (setf user-data) (data object key)
a00ba56a 351 (%object-set-qdata-full object (quark-intern key)
73572c12 352 (register-user-data data) (callback user-data-destroy-func))
86d9d6ab 353 data)
354
a00ba56a 355;; deprecated
174e8a5c 356(defun (setf object-data) (data object key &key (test #'eq))
357 (assert (eq test #'eq))
358 (setf (user-data object key) data))
359
26b133ed 360(defbinding %object-get-qdata () unsigned-long
86d9d6ab 361 (object gobject)
362 (id quark))
363
174e8a5c 364(defun user-data (object key)
a00ba56a 365 (find-user-data (%object-get-qdata object (quark-intern key))))
174e8a5c 366
a00ba56a 367;; deprecated
86d9d6ab 368(defun object-data (object key &key (test #'eq))
174e8a5c 369 (assert (eq test #'eq))
370 (user-data object key))
371
372(defun user-data-p (object key)
a00ba56a 373 (user-data-exists-p (%object-get-qdata object (quark-intern key))))
374
375(defbinding %object-steal-qdata () unsigned-long
376 (object gobject)
377 (id quark))
378
379(defun unset-user-data (object key)
380 (destroy-user-data (%object-steal-qdata object (quark-intern key))))
86d9d6ab 381
382
d4b21b08 383;;;;
384
323d4265 385(defbinding %object-class-list-properties () pointer
d4b21b08 386 (class pointer)
387 (n-properties unsigned-int :out))
388
9c03a07c 389
390(defun %map-params (params length type inherited-p)
391 (if inherited-p
9adccb27 392 (map-c-vector 'list #'identity params 'param length)
9c03a07c 393 (let ((properties ()))
9adccb27 394 (map-c-vector 'list
9c03a07c 395 #'(lambda (param)
396 (when (eql (param-owner-type param) type)
397 (push param properties)))
398 params 'param length)
399 (nreverse properties))))
400
401(defun query-object-class-properties (type &optional inherited-p)
735a29da 402 (let* ((type-number (find-type-number type t))
9c03a07c 403 (class (type-class-ref type-number)))
404 (unwind-protect
405 (multiple-value-bind (array length)
406 (%object-class-list-properties class)
21299acf 407 (unless (null-pointer-p array)
408 (unwind-protect
409 (%map-params array length type-number inherited-p)
410 (deallocate-memory array))))
9c03a07c 411; (type-class-unref type-number)
412 )))
d4b21b08 413
414
415(defun default-slot-name (name)
416 (intern (substitute #\- #\_ (string-upcase (string-upcase name)))))
417
418(defun default-slot-accessor (class-name slot-name type)
419 (intern
420 (format
421 nil "~A-~A~A" class-name slot-name
4d83a8a6 422 (if (eq type 'boolean) "-P" ""))))
d4b21b08 423
323d4265 424
646466b0 425(defun slot-definition-from-property (class property &optional slot-name args)
9c03a07c 426 (with-slots (name flags value-type documentation) property
646466b0 427 (let* ((slot-name (or slot-name (default-slot-name name)))
62f12808 428 (slot-type (or (getf args :type) (type-from-number value-type) 'pointer))
9c03a07c 429 (accessor (default-slot-accessor class slot-name slot-type)))
430
431 `(,slot-name
432 :allocation :property :pname ,name
eeda1c2d 433
735a29da 434 ,@(when (find :unbound args) (list :unbound (getf args :unbound)))
435 ,@(when (find :getter args) (list :getter (getf args :getter)))
436 ,@(when (find :setter args) (list :setter (getf args :setter)))
9c03a07c 437
438 ;; accessors
439 ,@(cond
440 ((and
441 (member :writable flags) (member :readable flags)
442 (not (member :construct-only flags)))
443 (list :accessor accessor))
444 ((and (member :writable flags) (not (member :construct-only flags)))
445 (list :writer `(setf ,accessor)))
446 ((member :readable flags)
447 (list :reader accessor)))
448
449 ;; readable/writable/construct
450 ,@(when (or (not (member :writable flags))
451 (member :construct-only flags))
452 '(:writable nil))
453 ,@(when (not (member :readable flags))
454 '(:readable nil))
455 ,@(when (or (member :construct flags)
456 (member :construct-only flags))
457 '(:construct t))
458
459 ;; initargs
e5c7586b 460 ,@(if (find :initarg args)
461 (let ((initarg (getf args :initarg)))
462 (etypecase initarg
463 (null ())
464 (symbol `(:initarg ,initarg))))
465 (when (or (member :construct flags)
466 (member :construct-only flags)
467 (member :writable flags))
468 (list :initarg (intern (string slot-name) "KEYWORD"))))
9c03a07c 469
470 :type ,slot-type
471 :documentation ,documentation))))
472
473
474(defun slot-definitions (class properties slots)
475 (loop
9c03a07c 476 for property in properties
eeda1c2d 477 as slot = (or
478 (find (param-name property) slots
479 :key #'(lambda (slot) (getf (rest slot) :pname))
480 :test #'string=)
481 (find (param-name property) slots
482 :key #'first :test #'string-equal))
483 do (cond
484 ((not slot)
485 (push (slot-definition-from-property class property) slots))
486 ((getf (rest slot) :merge)
487 (setf
488 (rest slot)
646466b0 489 (rest (slot-definition-from-property class property (first slot) (rest slot)))))))
9c03a07c 490 (delete-if #'(lambda (slot) (getf (rest slot) :ignore)) slots))
491
492
62f12808 493(defun expand-gobject-type (type forward-p options &optional (metaclass 'gobject-class))
9c03a07c 494 (let ((supers (cons (supertype type) (implements type)))
495 (class (type-from-number type))
496 (slots (getf options :slots)))
497 `(defclass ,class ,supers
735a29da 498 ,(unless forward-p
499 (slot-definitions class (query-object-class-properties type) slots))
500 (:metaclass ,metaclass)
501 (:gtype ,(register-type-as type)))))
323d4265 502
62f12808 503(defun gobject-dependencies (type)
21299acf 504 (delete-duplicates
505 (cons
506 (supertype type)
507 (append
508 (type-interfaces type)
509 (mapcar #'param-value-type (query-object-class-properties type))))))
d4b21b08 510
62f12808 511
512(register-derivable-type 'gobject "GObject" 'expand-gobject-type 'gobject-dependencies)
ae37d096 513
514
515;;; Pseudo type for gobject instances which have their reference count
516;;; increased by the returning function
517
518(defmethod alien-type ((type (eql 'referenced)) &rest args)
519 (declare (ignore type args))
520 (alien-type 'gobject))
521
522(defmethod from-alien-form (form (type (eql 'referenced)) &rest args)
523 (declare (ignore type))
524 (destructuring-bind (type) args
525 (if (subtypep type 'gobject)
526 (let ((instance (make-symbol "INSTANCE")))
527 `(let ((,instance ,(from-alien-form form type)))
528 (when ,instance
529 (%object-unref (proxy-location ,instance)))
530 ,instance))
531 (error "~A is not a subclass of GOBJECT" type))))
532
533(export 'referenced)