chiark / gitweb /
Correct reference counting of gobjects
[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.16 2004-11-03 16:18:16 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-property-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 ;    (map 'nil #'gvalue-free values)
59     )
60   
61   (%object-weak-ref object)
62   (apply #'call-next-method object initargs))
63
64
65 (defmethod initialize-proxy ((object gobject) &rest initargs &key weak-ref)
66   (declare (ignore initargs))
67   (call-next-method)
68   (%object-weak-ref object)
69   (unless weak-ref
70     (object-ref object)))
71
72 (def-callback weak-notify (void (data int) (location system-area-pointer))
73   (when (instance-cached-p location)
74     (warn "~A being finalized by the GObject system while still in existence in lisp" (find-cached-instance location))
75     (remove-cached-instance location)))
76
77 (defbinding %object-weak-ref (object) nil
78   (object gobject)
79   ((callback weak-notify) pointer)
80   (0 unsigned-int))
81
82
83 (defbinding (%gobject-new "g_object_new") () pointer
84   (type type-number)
85   (nil null))
86
87 (defbinding (%gobject-newvv "g_object_newvv") () pointer
88   (type type-number)
89   (n-parameters unsigned-int)
90   (names (vector string))
91   (values (vector gvalue)))
92
93
94 (defbinding %object-ref (type location) pointer
95   (location pointer))
96
97  (defbinding %object-unref (type location) nil
98    (location pointer))
99
100 (defun object-ref (object)
101   (%object-ref nil (proxy-location object)))
102
103 (defun object-unref (object)
104   (%object-unref nil (proxy-location object)))
105
106
107
108 ;;;; Property stuff
109
110 (defbinding %object-set-property () nil
111   (object gobject)
112   (name string)
113   (value gvalue))
114
115 (defbinding %object-get-property () nil
116   (object gobject)
117   (name string)
118   (value gvalue))
119
120 (defbinding %object-notify () nil
121   (object gobject)
122   (name string))
123
124 (defbinding object-freeze-notify () nil
125   (object gobject))
126
127 (defbinding object-thaw-notify () nil
128   (object gobject))
129
130 (defbinding %object-set-qdata-full () nil
131   (object gobject)
132   (id quark)
133   (data unsigned-long)
134   (destroy-marshal pointer))
135
136
137 ;;;; User data
138
139 (defun (setf object-data) (data object key &key (test #'eq))
140   (%object-set-qdata-full
141    object (quark-from-object key :test test)
142    (register-user-data data) (callback %destroy-user-data))
143   data)
144
145 (defbinding %object-get-qdata () unsigned-long
146   (object gobject)               
147   (id quark))
148
149 (defun object-data (object key &key (test #'eq))
150   (find-user-data
151    (%object-get-qdata object (quark-from-object key :test test))))
152
153
154
155 ;;;; Metaclass used for subclasses of gobject
156
157 (eval-when (:compile-toplevel :load-toplevel :execute)
158   (defclass gobject-class (ginstance-class)
159     ())
160
161   (defclass direct-property-slot-definition (direct-virtual-slot-definition)
162     ((pname :reader slot-definition-pname :initarg :pname)
163      (readable :initform t :reader slot-readable-p :initarg :readable)
164      (writable :initform t :reader slot-writable-p :initarg :writable)
165      (construct :initform nil :initarg :construct)))
166
167   (defclass effective-property-slot-definition (effective-virtual-slot-definition)
168     ((pname :reader slot-definition-pname :initarg :pname)
169      (readable :reader slot-readable-p :initarg :readable)
170      (writable :reader slot-writable-p :initarg :writable)
171      (construct :initarg :construct))))
172
173
174
175 ; (defbinding object-class-install-param () nil
176 ;   (class pointer)
177 ;   (id unsigned-int)
178 ;   (parameter parameter))
179
180 ; (defbinding object-class-find-param-spec () parameter
181 ;   (class pointer)
182 ;   (name string))
183
184 (defun signal-name-to-string (name)
185   (substitute #\_ #\- (string-downcase (string name))))
186
187
188 (defmethod direct-slot-definition-class ((class gobject-class) &rest initargs)
189   (case (getf initargs :allocation)
190     (:property (find-class 'direct-property-slot-definition))
191     (t (call-next-method))))
192
193 (defmethod effective-slot-definition-class ((class gobject-class) &rest initargs)
194   (case (getf initargs :allocation)
195     (:property (find-class 'effective-property-slot-definition))
196     (t (call-next-method))))
197
198 (defmethod compute-effective-slot-definition-initargs ((class gobject-class) direct-slotds)
199   (if (eq (most-specific-slot-value direct-slotds 'allocation) :property)
200       (nconc 
201        (list :pname (signal-name-to-string 
202                      (most-specific-slot-value direct-slotds 'pname))
203              :readable (most-specific-slot-value direct-slotds 'readable)
204              :writable (most-specific-slot-value direct-slotds 'writable)
205              :construct (most-specific-slot-value direct-slotds 'construct))
206        (call-next-method))
207     (call-next-method)))
208
209
210 (defmethod initialize-internal-slot-functions ((slotd effective-property-slot-definition))
211   (let* ((type (slot-definition-type slotd))
212          (pname (slot-definition-pname slotd))
213          (type-number (find-type-number type)))
214     (unless (slot-boundp slotd 'reader-function)
215       (setf 
216        (slot-value slotd 'reader-function)
217        (if (slot-readable-p slotd)
218            #'(lambda (object)
219                (with-gc-disabled
220                    (let ((gvalue (gvalue-new type-number)))
221                      (%object-get-property object pname gvalue)
222                      (unwind-protect
223                           (funcall
224                            (intern-reader-function (type-from-number type-number)) gvalue +gvalue-value-offset+) ; temporary workaround for wrong topological sorting of types
225                        (gvalue-free gvalue t)))))
226            #'(lambda (value object)
227                (error "Slot is not readable: ~A" (slot-definition-name slotd))))))
228     
229     (unless (slot-boundp slotd 'writer-function)
230       (setf 
231        (slot-value slotd 'writer-function)
232        (if (slot-writable-p slotd)
233            #'(lambda (value object)
234                (with-gc-disabled
235                    (let ((gvalue (gvalue-new type-number)))
236                      (funcall
237                       (intern-writer-function (type-from-number type-number)) ; temporary
238                       value gvalue +gvalue-value-offset+)
239                      (%object-set-property object pname gvalue)
240                      (funcall
241                       (intern-destroy-function (type-from-number type-number)) ; temporary
242                       gvalue +gvalue-value-offset+)
243                      (gvalue-free gvalue nil)
244                      value)))
245            #'(lambda (value object)
246                (error "Slot is not writable: ~A" (slot-definition-name slotd))))))
247     
248     (unless (slot-boundp slotd 'boundp-function)
249       (setf 
250        (slot-value slotd 'boundp-function)
251        #'(lambda (object)
252            (declare (ignore object))
253            t))))
254   (call-next-method))
255
256
257 (defmethod validate-superclass ((class gobject-class)
258                                 (super pcl::standard-class))
259 ;  (subtypep (class-name super) 'gobject)
260   t)
261
262
263
264 ;;;;
265
266 (defbinding %object-class-list-properties () pointer
267   (class pointer)
268   (n-properties unsigned-int :out))
269
270
271 (defun %map-params (params length type inherited-p)
272   (if inherited-p
273       (map-c-array 'list #'identity params 'param length)
274     (let ((properties ()))
275       (map-c-array 'list 
276        #'(lambda (param)
277            (when (eql (param-owner-type param) type)
278              (push param properties)))
279        params 'param length)
280       (nreverse properties))))
281
282 (defun query-object-class-properties (type &optional inherited-p)
283   (let* ((type-number (find-type-number type))
284          (class (type-class-ref type-number)))
285     (unwind-protect
286          (multiple-value-bind (array length)
287              (%object-class-list-properties class)
288            (unwind-protect
289                 (%map-params array length type-number inherited-p)
290              (deallocate-memory array)))
291 ;      (type-class-unref type-number)
292       )))
293
294
295 (defun default-slot-name (name)
296   (intern (substitute #\- #\_ (string-upcase (string-upcase name)))))
297
298 (defun default-slot-accessor (class-name slot-name type)
299   (intern
300    (format
301     nil "~A-~A~A" class-name slot-name
302     (if (eq type 'boolean) "-P" ""))))
303
304
305 (defun slot-definition-from-property (class property)
306   (with-slots (name flags value-type documentation) property
307     (let* ((slot-name (default-slot-name name))
308            (slot-type (or (type-from-number value-type) value-type))
309            (accessor (default-slot-accessor class slot-name slot-type)))
310       
311       `(,slot-name
312         :allocation :property :pname ,name
313         
314         ;; accessors
315         ,@(cond
316            ((and
317              (member :writable flags) (member :readable flags)
318              (not (member :construct-only flags)))
319             (list :accessor accessor))
320            ((and (member :writable flags) (not (member :construct-only flags)))
321             (list :writer `(setf ,accessor)))
322            ((member :readable flags)
323             (list :reader accessor)))
324
325         ;; readable/writable/construct
326         ,@(when (or (not (member :writable flags))
327                     (member :construct-only flags))
328             '(:writable nil))
329         ,@(when (not (member :readable flags))
330             '(:readable nil))
331         ,@(when (or (member :construct flags) 
332                     (member :construct-only flags))
333             '(:construct t))
334         
335         ;; initargs
336         ,@(when (or (member :construct flags)
337                     (member :construct-only flags)
338                     (member :writable flags))
339             (list :initarg (intern (string slot-name) "KEYWORD")))
340         
341         :type ,slot-type
342         :documentation ,documentation))))
343
344
345 (defun slot-definitions (class properties slots)
346   (loop 
347    with manual-slots = slots
348    for property in properties
349    unless (find-if 
350            #'(lambda (slot)
351                (destructuring-bind (name &rest args) slot
352                  (or 
353                   (equal (param-name property) (getf args :pname))
354                   (eq (default-slot-name (param-name property)) name))))
355            manual-slots)
356    do (push (slot-definition-from-property class property) slots))
357   (delete-if #'(lambda (slot) (getf (rest slot) :ignore)) slots))
358
359
360 (defun expand-gobject-type (type &optional options (metaclass 'gobject-class))
361   (let ((supers (cons (supertype type) (implements type)))
362         (class  (type-from-number type))
363         (slots (getf options :slots)))    
364     `(defclass ,class ,supers
365       ,(slot-definitions class (query-object-class-properties type) slots)
366       (:metaclass ,metaclass)
367       (:alien-name ,(find-type-name type)))))
368
369
370 (register-derivable-type 'gobject "GObject" 'expand-gobject-type)