chiark / gitweb /
Fixed broken reference counting
[clg] / glib / gobject.lisp
1 ;; Common Lisp bindings for GTK+ v2.x
2 ;; Copyright 2000-2005 Espen S. Johnsen <espen@users.sf.net>
3 ;;
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:
11 ;;
12 ;; The above copyright notice and this permission notice shall be
13 ;; included in all copies or substantial portions of the Software.
14 ;;
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.
22
23 ;; $Id: gobject.lisp,v 1.41 2006-02-03 12:44:32 espen Exp $
24
25 (in-package "GLIB")
26
27
28 ;;;; Metaclass used for subclasses of gobject
29
30 (eval-when (:compile-toplevel :load-toplevel :execute)
31   (defclass gobject-class (ginstance-class)
32     ((instance-slots-p :initform nil
33       :documentation "Non NIL if the class has slots with instance allocation")))
34
35   (defmethod validate-superclass ((class gobject-class) (super standard-class))
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)
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
57
58 (defbinding %object-ref () pointer
59   (location pointer))
60
61 (defbinding %object-unref () nil
62   (location pointer))
63
64 #+glib2.8
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)))
80
81 (defmethod reference-foreign ((class gobject-class) location)
82   (declare (ignore class))
83   (%object-ref location))
84
85 (defmethod unreference-foreign ((class gobject-class) location)
86   (declare (ignore class))
87   (%object-unref location))
88
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)))
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))
116     (:user-data (find-class 'direct-user-data-slot-definition))
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))
122     (:user-data (find-class 'effective-user-data-slot-definition))
123     (t (call-next-method))))
124
125 (defmethod compute-effective-slot-definition-initargs ((class gobject-class) direct-slotds)
126   (if (typep (first direct-slotds) 'direct-property-slot-definition)
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))
138   (let ((type (slot-definition-type slotd))
139         (pname (slot-definition-pname slotd)))
140     (when (and (not (slot-boundp slotd 'getter)) (slot-readable-p slotd))
141       (setf 
142        (slot-value slotd 'getter)
143        (let ((reader nil))
144          #'(lambda (object)
145              (unless reader
146                (setq reader (reader-function type)))
147              (let ((gvalue (gvalue-new type)))
148                (%object-get-property object pname gvalue)
149                (unwind-protect
150                  (funcall reader  gvalue +gvalue-value-offset+)
151                  (gvalue-free gvalue t)))))))
152     
153     (when (and (not (slot-boundp slotd 'setter)) (slot-writable-p slotd))
154       (setf 
155        (slot-value slotd 'setter)
156        (let ((writer nil))
157          #'(lambda (value object)
158              (unless writer
159                (setq writer (writer-function type)))
160              (let ((gvalue (gvalue-new type)))
161                (funcall writer value gvalue +gvalue-value-offset+)
162                (%object-set-property object pname gvalue)
163                (gvalue-free gvalue t)
164                value))))))
165
166   (call-next-method))
167
168 (defmethod initialize-internal-slot-functions ((slotd effective-user-data-slot-definition))
169   (let ((slot-name (slot-definition-name slotd)))
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)))))
185   (call-next-method))
186
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)
192                    (not (typep slotd 'effective-special-slot-definition))))
193               (class-slots class))
194     (setf (slot-value class 'instance-slots-p) t)))
195
196
197
198 ;;;; Super class for all classes in the GObject type hierarchy
199
200 (eval-when (:compile-toplevel :load-toplevel :execute)
201   (defclass gobject (ginstance)
202     ()
203     (:metaclass gobject-class)
204     (:gtype "GObject")))
205
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
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
234 (defmethod initialize-instance ((object gobject) &rest initargs)
235   (unless (slot-boundp object 'location)
236     ;; Extract initargs which we should pass directly to the GObject
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+)))))
261             (loop
262              for (pname type value) in args
263              as tmp = params then (sap+ tmp (+ string-size +gvalue-size+))
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)))
276         (setf  
277          (slot-value object 'location) 
278          (%gobject-new (type-number-of object))))))
279
280   (apply #'call-next-method object initargs))
281
282
283 (defmethod instance-finalizer ((instance gobject))
284   (let ((location (proxy-location instance)))
285     #+glib2.8
286     (if (slot-value (class-of instance) 'instance-slots-p)
287         #'(lambda ()
288             #+debug-ref-counting
289             (format t "Finalizing proxy for 0x~8,'0X~%" (sap-int location))
290             (remove-cached-instance location)
291             (%object-remove-toggle-ref location))
292       #'(lambda ()
293           #+debug-ref-counting
294           (format t "Finalizing proxy for 0x~8,'0X~%" (sap-int location))
295           (remove-cached-instance location)
296           (%object-unref location)))
297     #-glib2.8
298     #'(lambda ()
299         (remove-cached-instance location)
300           (%object-unref location))))
301
302
303 (defbinding (%gobject-new "g_object_new") () pointer
304   (type type-number)
305   (nil null))
306
307 (defbinding (%gobject-newv "g_object_newv") () pointer
308   (type type-number)
309   (n-parameters unsigned-int)
310   (params pointer))
311
312
313
314 ;;;; Property stuff
315
316 (defbinding %object-set-property () nil
317   (object gobject)
318   (name string)
319   (value gvalue))
320
321 (defbinding %object-get-property () nil
322   (object gobject)
323   (name string)
324   (value gvalue))
325
326 (defbinding %object-notify () nil
327   (object gobject)
328   (name string))
329
330 (defbinding object-freeze-notify () nil
331   (object gobject))
332
333 (defbinding object-thaw-notify () nil
334   (object gobject))
335
336
337 ;;;; User data
338
339 (defbinding %object-set-qdata-full () nil
340   (object gobject)
341   (id quark)
342   (data unsigned-long)
343   (destroy-marshal pointer))
344
345 (defcallback user-data-destroy-func (nil (id unsigned-int))
346   (destroy-user-data id))
347
348 (export 'user-data-destroy-func)
349
350 (defun (setf user-data) (data object key)
351   (%object-set-qdata-full object (quark-intern key)
352    (register-user-data data) (callback user-data-destroy-func))
353   data)
354
355 ;; deprecated
356 (defun (setf object-data) (data object key &key (test #'eq))
357   (assert (eq test #'eq))
358   (setf (user-data object key) data))
359
360 (defbinding %object-get-qdata () unsigned-long
361   (object gobject)               
362   (id quark))
363
364 (defun user-data (object key)
365   (find-user-data (%object-get-qdata object (quark-intern key))))
366
367 ;; deprecated
368 (defun object-data (object key &key (test #'eq))
369   (assert (eq test #'eq))
370   (user-data object key))
371
372 (defun user-data-p (object key)
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))))
381
382
383 ;;;;
384
385 (defbinding %object-class-list-properties () pointer
386   (class pointer)
387   (n-properties unsigned-int :out))
388
389
390 (defun %map-params (params length type inherited-p)
391   (if inherited-p
392       (map-c-vector 'list #'identity params 'param length)
393     (let ((properties ()))
394       (map-c-vector 'list 
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)
402   (let* ((type-number (find-type-number type t))
403          (class (type-class-ref type-number)))
404     (unwind-protect
405          (multiple-value-bind (array length)
406              (%object-class-list-properties class)
407            (unless (null-pointer-p array)
408              (unwind-protect
409                  (%map-params array length type-number inherited-p)
410                (deallocate-memory array))))
411 ;      (type-class-unref type-number)
412       )))
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
422     (if (eq type 'boolean) "-P" ""))))
423
424
425 (defun slot-definition-from-property (class property &optional slot-name args)
426   (with-slots (name flags value-type documentation) property
427     (let* ((slot-name (or slot-name (default-slot-name name)))
428            (slot-type (or (getf args :type) (type-from-number value-type) 'pointer))
429            (accessor (default-slot-accessor class slot-name slot-type)))
430       
431       `(,slot-name
432         :allocation :property :pname ,name
433
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)))
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
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"))))
469         
470         :type ,slot-type
471         :documentation ,documentation))))
472
473
474 (defun slot-definitions (class properties slots)
475   (loop 
476    for property in properties
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) 
489          (rest (slot-definition-from-property class property (first slot) (rest slot)))))))
490   (delete-if #'(lambda (slot) (getf (rest slot) :ignore)) slots))
491
492
493 (defun expand-gobject-type (type forward-p options &optional (metaclass 'gobject-class))
494   (let ((supers (cons (supertype type) (implements type)))
495         (class  (type-from-number type))
496         (slots (getf options :slots)))    
497     `(defclass ,class ,supers
498          ,(unless forward-p
499             (slot-definitions class (query-object-class-properties type) slots))
500          (:metaclass ,metaclass)
501          (:gtype ,(register-type-as type)))))
502
503 (defun gobject-dependencies (type)
504   (delete-duplicates 
505    (cons
506     (supertype type)
507     (append 
508      (type-interfaces type)
509      (mapcar #'param-value-type (query-object-class-properties type))))))
510
511
512 (register-derivable-type 'gobject "GObject" 'expand-gobject-type 'gobject-dependencies)
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)