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