chiark / gitweb /
f41cb3239df746cc1151a0c384411dec162bef6f
[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.20 2004-11-09 10:10:59 espen Exp $
19
20 (in-package "GLIB")
21
22
23 ;;;; Metaclass used for subclasses of gobject
24
25 (eval-when (:compile-toplevel :load-toplevel :execute)
26   (defclass gobject-class (ginstance-class)
27     ())
28
29   (defmethod validate-superclass ((class gobject-class)
30                                 (super pcl::standard-class))
31 ;  (subtypep (class-name super) 'gobject)
32     t))
33
34 (defclass direct-property-slot-definition (direct-virtual-slot-definition)
35   ((pname :reader slot-definition-pname :initarg :pname)
36    (readable :initform t :reader slot-readable-p :initarg :readable)
37    (writable :initform t :reader slot-writable-p :initarg :writable)
38    (construct :initform nil :initarg :construct)))
39
40 (defclass effective-property-slot-definition (effective-virtual-slot-definition)
41   ((pname :reader slot-definition-pname :initarg :pname)
42    (readable :reader slot-readable-p :initarg :readable)
43    (writable :reader slot-writable-p :initarg :writable)
44    (construct :initarg :construct)));)
45
46 (defbinding %object-ref () pointer
47   (location pointer))
48
49 (defbinding %object-unref () nil
50   (location pointer))
51
52 (defmethod reference-foreign ((class gobject-class) location)
53   (declare (ignore class))
54   (%object-ref location))
55
56 (defmethod unreference-foreign ((class gobject-class) location)
57   (declare (ignore class))
58   (%object-unref location))
59
60
61 ; (defbinding object-class-install-param () nil
62 ;   (class pointer)
63 ;   (id unsigned-int)
64 ;   (parameter parameter))
65
66 ; (defbinding object-class-find-param-spec () parameter
67 ;   (class pointer)
68 ;   (name string))
69
70 (defun signal-name-to-string (name)
71   (substitute #\_ #\- (string-downcase (string name))))
72
73
74 (defmethod direct-slot-definition-class ((class gobject-class) &rest initargs)
75   (case (getf initargs :allocation)
76     (:property (find-class 'direct-property-slot-definition))
77     (t (call-next-method))))
78
79 (defmethod effective-slot-definition-class ((class gobject-class) &rest initargs)
80   (case (getf initargs :allocation)
81     (:property (find-class 'effective-property-slot-definition))
82     (t (call-next-method))))
83
84 (defmethod compute-effective-slot-definition-initargs ((class gobject-class) direct-slotds)
85   (if (eq (most-specific-slot-value direct-slotds 'allocation) :property)
86       (nconc 
87        (list :pname (signal-name-to-string 
88                      (most-specific-slot-value direct-slotds 'pname))
89              :readable (most-specific-slot-value direct-slotds 'readable)
90              :writable (most-specific-slot-value direct-slotds 'writable)
91              :construct (most-specific-slot-value direct-slotds 'construct))
92        (call-next-method))
93     (call-next-method)))
94
95
96 (defmethod initialize-internal-slot-functions ((slotd effective-property-slot-definition))
97   (let* ((type (slot-definition-type slotd))
98          (pname (slot-definition-pname slotd))
99          (type-number (find-type-number type)))
100     (unless (slot-boundp slotd 'reader-function)
101       (setf 
102        (slot-value slotd 'reader-function)
103        (if (slot-readable-p slotd)
104            (let () ;(reader (reader-function (type-from-number type-number))))
105              #'(lambda (object)
106                  (let ((gvalue (gvalue-new type-number)))
107                    (%object-get-property object pname gvalue)
108                    (unwind-protect
109                         (funcall #|reader|# (reader-function (type-from-number type-number))  gvalue +gvalue-value-offset+)
110                      (gvalue-free gvalue t)))))
111            #'(lambda (value object)
112                (error "Slot is not readable: ~A" (slot-definition-name slotd))))))
113     
114     (unless (slot-boundp slotd 'writer-function)
115       (setf 
116        (slot-value slotd 'writer-function)
117        (if (slot-writable-p slotd)
118            (let ();; (writer (writer-function (type-from-number type-number)))
119 ;;               (destroy (destroy-function (type-from-number type-number))))
120              #'(lambda (value object)
121                  (let ((gvalue (gvalue-new type-number)))
122                    (funcall #|writer|# (writer-function (type-from-number type-number)) value gvalue +gvalue-value-offset+)
123                    (%object-set-property object pname gvalue)
124 ;                  (funcall #|destroy|#(destroy-function (type-from-number type-number)) gvalue +gvalue-value-offset+)
125                    (gvalue-free gvalue t)
126                    value)))
127            #'(lambda (value object)
128                (error "Slot is not writable: ~A" (slot-definition-name slotd))))))
129     
130     (unless (slot-boundp slotd 'boundp-function)
131       (setf 
132        (slot-value slotd 'boundp-function)
133        #'(lambda (object)
134            (declare (ignore object))
135            t))))
136   (call-next-method))
137
138
139 ;;;; Super class for all classes in the GObject type hierarchy
140
141 (eval-when (:compile-toplevel :load-toplevel :execute)
142   (defclass gobject (ginstance)
143     ()
144     (:metaclass gobject-class)
145     (:alien-name "GObject")))
146
147 (defmethod print-object ((instance gobject) stream)
148   (print-unreadable-object (instance stream :type t :identity nil)
149     (if (slot-boundp instance 'location)
150         (format stream "at 0x~X" (sap-int (proxy-location instance)))
151       (write-string "(destroyed)" stream))))
152
153
154 (defmethod initialize-instance ((object gobject) &rest initargs)
155   ;; Extract initargs which we should pass directly to the GObeject
156   ;; constructor
157   (let* ((slotds (class-slots (class-of object)))
158          (args (loop 
159                 as tmp = initargs then (cddr tmp) while tmp
160                 as key = (first tmp)
161                 as value = (second tmp)
162                 as slotd = (find-if
163                             #'(lambda (slotd)
164                                 (member key (slot-definition-initargs slotd)))
165                             slotds)
166                 when (and (typep slotd 'effective-property-slot-definition)
167                           (slot-value slotd 'construct))
168                 collect (progn 
169                           (remf initargs key)
170                           (list 
171                            (slot-definition-pname slotd)
172                            (slot-definition-type slotd)
173                            value)))))
174     (if args
175         (let* ((string-size (size-of 'string))
176                (string-writer (writer-function 'string))
177                (string-destroy (destroy-function 'string))
178                (params (allocate-memory 
179                         (* (length args) (+ string-size +gvalue-size+)))))
180           (loop
181            for (pname type value) in args
182            as tmp = params then (sap+ tmp (+ string-size +gvalue-size+))
183            do (funcall string-writer pname tmp)
184            (gvalue-init (sap+ tmp string-size) type value))
185           (unwind-protect
186                (setf  
187                 (slot-value object 'location) 
188                 (%gobject-newv (type-number-of object) (length args) params))
189             (loop
190              repeat (length args)
191              as tmp = params then (sap+ tmp (+ string-size +gvalue-size+))
192              do (funcall string-destroy tmp)
193              (gvalue-unset (sap+ tmp string-size)))
194             (deallocate-memory params)))
195         (setf  
196          (slot-value object 'location) 
197          (%gobject-new (type-number-of object)))))
198
199   (%object-weak-ref object)
200   (apply #'call-next-method object initargs))
201
202
203 (defmethod initialize-instance :around ((object gobject) &rest initargs)
204   (declare (ignore initargs))
205   (call-next-method)
206   (%object-weak-ref object))
207
208 (defmethod instance-finalizer ((instance gobject))
209   (let ((location (proxy-location instance)))
210     #'(lambda ()
211         (remove-cached-instance location)
212         (%weak-object-unref location)
213         (%object-unref location))))
214
215
216 (defcallback weak-notify (nil (data int) (location pointer))
217   (let ((object (find-cached-instance location)))
218     (when object
219 ;;       (warn "~A being finalized by the GObject system while still in existence in lisp" object)
220       (slot-makunbound object 'location)
221       (remove-cached-instance location))))
222
223 (defbinding %object-weak-ref (object) nil
224   (object gobject)
225   ((callback weak-notify) pointer)
226   (0 unsigned-int))
227
228 (defbinding %object-weak-unref () nil
229   (location pointer)
230   ((callback weak-notify) pointer)
231   (0 unsigned-int))
232             
233
234 (defbinding (%gobject-new "g_object_new") () pointer
235   (type type-number)
236   (nil null))
237
238 (defbinding (%gobject-newv "g_object_newv") () pointer
239   (type type-number)
240   (n-parameters unsigned-int)
241   (params pointer))
242
243
244
245 ;;;; Property stuff
246
247 (defbinding %object-set-property () nil
248   (object gobject)
249   (name string)
250   (value gvalue))
251
252 (defbinding %object-get-property () nil
253   (object gobject)
254   (name string)
255   (value gvalue))
256
257 (defbinding %object-notify () nil
258   (object gobject)
259   (name string))
260
261 (defbinding object-freeze-notify () nil
262   (object gobject))
263
264 (defbinding object-thaw-notify () nil
265   (object gobject))
266
267 (defbinding %object-set-qdata-full () nil
268   (object gobject)
269   (id quark)
270   (data unsigned-long)
271   (destroy-marshal pointer))
272
273
274 ;;;; User data
275
276 (defun (setf object-data) (data object key &key (test #'eq))
277   (%object-set-qdata-full
278    object (quark-from-object key :test test)
279    (register-user-data data) (callback %destroy-user-data))
280   data)
281
282 (defbinding %object-get-qdata () unsigned-long
283   (object gobject)               
284   (id quark))
285
286 (defun object-data (object key &key (test #'eq))
287   (find-user-data
288    (%object-get-qdata object (quark-from-object key :test test))))
289
290
291 ;;;;
292
293 (defbinding %object-class-list-properties () pointer
294   (class pointer)
295   (n-properties unsigned-int :out))
296
297
298 (defun %map-params (params length type inherited-p)
299   (if inherited-p
300       (map-c-vector 'list #'identity params 'param length)
301     (let ((properties ()))
302       (map-c-vector 'list 
303        #'(lambda (param)
304            (when (eql (param-owner-type param) type)
305              (push param properties)))
306        params 'param length)
307       (nreverse properties))))
308
309 (defun query-object-class-properties (type &optional inherited-p)
310   (let* ((type-number (find-type-number type))
311          (class (type-class-ref type-number)))
312     (unwind-protect
313          (multiple-value-bind (array length)
314              (%object-class-list-properties class)
315            (unwind-protect
316                 (%map-params array length type-number inherited-p)
317              (deallocate-memory array)))
318 ;      (type-class-unref type-number)
319       )))
320
321
322 (defun default-slot-name (name)
323   (intern (substitute #\- #\_ (string-upcase (string-upcase name)))))
324
325 (defun default-slot-accessor (class-name slot-name type)
326   (intern
327    (format
328     nil "~A-~A~A" class-name slot-name
329     (if (eq type 'boolean) "-P" ""))))
330
331
332 (defun slot-definition-from-property (class property)
333   (with-slots (name flags value-type documentation) property
334     (let* ((slot-name (default-slot-name name))
335            (slot-type (or (type-from-number value-type) value-type))
336            (accessor (default-slot-accessor class slot-name slot-type)))
337       
338       `(,slot-name
339         :allocation :property :pname ,name
340         
341         ;; accessors
342         ,@(cond
343            ((and
344              (member :writable flags) (member :readable flags)
345              (not (member :construct-only flags)))
346             (list :accessor accessor))
347            ((and (member :writable flags) (not (member :construct-only flags)))
348             (list :writer `(setf ,accessor)))
349            ((member :readable flags)
350             (list :reader accessor)))
351
352         ;; readable/writable/construct
353         ,@(when (or (not (member :writable flags))
354                     (member :construct-only flags))
355             '(:writable nil))
356         ,@(when (not (member :readable flags))
357             '(:readable nil))
358         ,@(when (or (member :construct flags) 
359                     (member :construct-only flags))
360             '(:construct t))
361         
362         ;; initargs
363         ,@(when (or (member :construct flags)
364                     (member :construct-only flags)
365                     (member :writable flags))
366             (list :initarg (intern (string slot-name) "KEYWORD")))
367         
368         :type ,slot-type
369         :documentation ,documentation))))
370
371
372 (defun slot-definitions (class properties slots)
373   (loop 
374    with manual-slots = slots
375    for property in properties
376    unless (find-if 
377            #'(lambda (slot)
378                (destructuring-bind (name &rest args) slot
379                  (or 
380                   (equal (param-name property) (getf args :pname))
381                   (eq (default-slot-name (param-name property)) name))))
382            manual-slots)
383    do (push (slot-definition-from-property class property) slots))
384   (delete-if #'(lambda (slot) (getf (rest slot) :ignore)) slots))
385
386
387 (defun expand-gobject-type (type &optional options (metaclass 'gobject-class))
388   (let ((supers (cons (supertype type) (implements type)))
389         (class  (type-from-number type))
390         (slots (getf options :slots)))    
391     `(defclass ,class ,supers
392       ,(slot-definitions class (query-object-class-properties type) slots)
393       (:metaclass ,metaclass)
394       (:alien-name ,(find-type-name type)))))
395
396
397 (register-derivable-type 'gobject "GObject" 'expand-gobject-type)