chiark / gitweb /
f0b0ef1caf531ebc8b42389938e12e1ed3e9026b
[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.36 2005-04-23 16:48:51 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     ())
33
34   (defmethod validate-superclass ((class gobject-class) (super standard-class))
35 ;  (subtypep (class-name super) 'gobject)
36     t))
37
38 (defclass direct-property-slot-definition (direct-virtual-slot-definition)
39   ((pname :reader slot-definition-pname :initarg :pname)
40    (readable :initform t :reader slot-readable-p :initarg :readable)
41    (writable :initform t :reader slot-writable-p :initarg :writable)
42    (construct :initform nil :initarg :construct)))
43
44 (defclass effective-property-slot-definition (effective-virtual-slot-definition)
45   ((pname :reader slot-definition-pname :initarg :pname)
46    (readable :reader slot-readable-p :initarg :readable)
47    (writable :reader slot-writable-p :initarg :writable)
48    (construct :initarg :construct)))
49
50 (defclass direct-user-data-slot-definition (direct-virtual-slot-definition)
51   ())
52
53 (defclass effective-user-data-slot-definition (effective-virtual-slot-definition)
54   ())
55
56
57 (defbinding %object-ref () pointer
58   (location pointer))
59
60 (defbinding %object-unref () nil
61   (location pointer))
62
63 (defmethod reference-foreign ((class gobject-class) location)
64   (declare (ignore class))
65   (%object-ref location))
66
67 (defmethod unreference-foreign ((class gobject-class) location)
68   (declare (ignore class))
69   (%object-unref location))
70
71
72 ; (defbinding object-class-install-param () nil
73 ;   (class pointer)
74 ;   (id unsigned-int)
75 ;   (parameter parameter))
76
77 ; (defbinding object-class-find-param-spec () parameter
78 ;   (class pointer)
79 ;   (name string))
80
81 (defun signal-name-to-string (name)
82   (substitute #\_ #\- (string-downcase (string name))))
83
84
85 (defmethod direct-slot-definition-class ((class gobject-class) &rest initargs)
86   (case (getf initargs :allocation)
87     (:property (find-class 'direct-property-slot-definition))
88     (:user-data (find-class 'direct-user-data-slot-definition))
89     (t (call-next-method))))
90
91 (defmethod effective-slot-definition-class ((class gobject-class) &rest initargs)
92   (case (getf initargs :allocation)
93     (:property (find-class 'effective-property-slot-definition))
94     (:user-data (find-class 'effective-user-data-slot-definition))
95     (t (call-next-method))))
96
97 (defmethod compute-effective-slot-definition-initargs ((class gobject-class) direct-slotds)
98   (if (typep (first direct-slotds) 'direct-property-slot-definition)
99       (nconc 
100        (list :pname (signal-name-to-string 
101                      (most-specific-slot-value direct-slotds 'pname))
102              :readable (most-specific-slot-value direct-slotds 'readable)
103              :writable (most-specific-slot-value direct-slotds 'writable)
104              :construct (most-specific-slot-value direct-slotds 'construct))
105        (call-next-method))
106     (call-next-method)))
107
108
109 (defmethod initialize-internal-slot-functions ((slotd effective-property-slot-definition))
110   (let ((type (slot-definition-type slotd))
111         (pname (slot-definition-pname slotd)))
112     (when (and (not (slot-boundp slotd 'getter)) (slot-readable-p slotd))
113       (setf 
114        (slot-value slotd 'getter)
115        (let ((reader nil))
116          #'(lambda (object)
117              (unless reader
118                (setq reader (reader-function type)))
119              (let ((gvalue (gvalue-new type)))
120                (%object-get-property object pname gvalue)
121                (unwind-protect
122                  (funcall reader  gvalue +gvalue-value-offset+)
123                  (gvalue-free gvalue t)))))))
124     
125     (when (and (not (slot-boundp slotd 'setter)) (slot-writable-p slotd))
126       (setf 
127        (slot-value slotd 'setter)
128        (let ((writer nil))
129          #'(lambda (value object)
130              (unless writer
131                (setq writer (writer-function type)))
132              (let ((gvalue (gvalue-new type)))
133                (funcall writer value gvalue +gvalue-value-offset+)
134                (%object-set-property object pname gvalue)
135                (gvalue-free gvalue t)
136                value))))))
137
138   (call-next-method))
139
140 (defmethod initialize-internal-slot-functions ((slotd effective-user-data-slot-definition))
141   (let ((slot-name (slot-definition-name slotd)))
142     (unless (slot-boundp slotd 'getter)
143       (setf 
144        (slot-value slotd 'getter)
145        #'(lambda (object)
146            (prog1 (user-data object slot-name)))))
147     (unless (slot-boundp slotd 'setter)
148       (setf 
149        (slot-value slotd 'setter)
150        #'(lambda (value object)
151            (setf (user-data object slot-name) value))))
152     (unless (slot-boundp slotd 'boundp)
153       (setf 
154        (slot-value slotd 'boundp)
155        #'(lambda (object)
156            (user-data-p object slot-name)))))
157   (call-next-method))
158
159
160 ;;;; Super class for all classes in the GObject type hierarchy
161
162 (eval-when (:compile-toplevel :load-toplevel :execute)
163   (defclass gobject (ginstance)
164     ()
165     (:metaclass gobject-class)
166     (:gtype "GObject")))
167
168
169 (defun initial-add (object function initargs key pkey)
170   (loop 
171    as (initarg value . rest) = initargs then rest
172    do (cond
173        ((eq initarg key) (funcall function object value))
174        ((eq initarg pkey) (mapc #'(lambda (value)
175                                     (funcall function object value))
176                                 value)))
177        while rest))
178
179 (defun initial-apply-add (object function initargs key pkey)
180   (initial-add object #'(lambda (object value)
181                           (apply function object (mklist value)))
182                initargs key pkey))
183
184
185 (defmethod initialize-instance ((object gobject) &rest initargs)
186   (unless (slot-boundp object 'location)
187     ;; Extract initargs which we should pass directly to the GObeject
188     ;; constructor
189     (let* ((slotds (class-slots (class-of object)))
190            (args (when initargs
191                    (loop 
192                     as (key value . rest) = initargs then rest
193                     as slotd = (find-if
194                                 #'(lambda (slotd)
195                                     (member key (slot-definition-initargs slotd)))
196                                 slotds)
197                     when (and (typep slotd 'effective-property-slot-definition)
198                               (slot-value slotd 'construct))
199                     collect (progn 
200                               (remf initargs key)
201                               (list 
202                                (slot-definition-pname slotd)
203                                (slot-definition-type slotd)
204                                value))
205                     while rest))))
206       (if args
207           (let* ((string-size (size-of 'string))
208                  (string-writer (writer-function 'string))
209                  (string-destroy (destroy-function 'string))
210                  (params (allocate-memory 
211                           (* (length args) (+ string-size +gvalue-size+)))))
212             (loop
213              for (pname type value) in args
214              as tmp = params then (sap+ tmp (+ string-size +gvalue-size+))
215              do (funcall string-writer pname tmp)
216              (gvalue-init (sap+ tmp string-size) type value))
217             (unwind-protect
218                 (setf  
219                  (slot-value object 'location) 
220                  (%gobject-newv (type-number-of object) (length args) params))
221               (loop
222                repeat (length args)
223                as tmp = params then (sap+ tmp (+ string-size +gvalue-size+))
224                do (funcall string-destroy tmp)
225                (gvalue-unset (sap+ tmp string-size)))
226               (deallocate-memory params)))
227         (setf  
228          (slot-value object 'location) 
229          (%gobject-new (type-number-of object))))))
230
231   (apply #'call-next-method object initargs))
232
233
234 (defmethod instance-finalizer ((instance gobject))
235   (let ((location (proxy-location instance)))
236     #'(lambda ()
237         (remove-cached-instance location)
238         (%object-unref location))))
239
240
241 (defbinding (%gobject-new "g_object_new") () pointer
242   (type type-number)
243   (nil null))
244
245 (defbinding (%gobject-newv "g_object_newv") () pointer
246   (type type-number)
247   (n-parameters unsigned-int)
248   (params pointer))
249
250
251
252 ;;;; Property stuff
253
254 (defbinding %object-set-property () nil
255   (object gobject)
256   (name string)
257   (value gvalue))
258
259 (defbinding %object-get-property () nil
260   (object gobject)
261   (name string)
262   (value gvalue))
263
264 (defbinding %object-notify () nil
265   (object gobject)
266   (name string))
267
268 (defbinding object-freeze-notify () nil
269   (object gobject))
270
271 (defbinding object-thaw-notify () nil
272   (object gobject))
273
274
275 ;;;; User data
276
277 (defbinding %object-set-qdata-full () nil
278   (object gobject)
279   (id quark)
280   (data unsigned-long)
281   (destroy-marshal pointer))
282
283 (defcallback user-data-destroy-func (nil (id unsigned-int))
284   (destroy-user-data id))
285
286 (export 'user-data-destroy-func)
287
288 (defun (setf user-data) (data object key)
289   (%object-set-qdata-full object (quark-intern key)
290    (register-user-data data) (callback user-data-destroy-func))
291   data)
292
293 ;; deprecated
294 (defun (setf object-data) (data object key &key (test #'eq))
295   (assert (eq test #'eq))
296   (setf (user-data object key) data))
297
298 (defbinding %object-get-qdata () unsigned-long
299   (object gobject)               
300   (id quark))
301
302 (defun user-data (object key)
303   (find-user-data (%object-get-qdata object (quark-intern key))))
304
305 ;; deprecated
306 (defun object-data (object key &key (test #'eq))
307   (assert (eq test #'eq))
308   (user-data object key))
309
310 (defun user-data-p (object key)
311   (user-data-exists-p (%object-get-qdata object (quark-intern key))))
312
313 (defbinding %object-steal-qdata () unsigned-long
314   (object gobject)               
315   (id quark))
316
317 (defun unset-user-data (object key)
318   (destroy-user-data (%object-steal-qdata object (quark-intern key))))
319
320
321 ;;;;
322
323 (defbinding %object-class-list-properties () pointer
324   (class pointer)
325   (n-properties unsigned-int :out))
326
327
328 (defun %map-params (params length type inherited-p)
329   (if inherited-p
330       (map-c-vector 'list #'identity params 'param length)
331     (let ((properties ()))
332       (map-c-vector 'list 
333        #'(lambda (param)
334            (when (eql (param-owner-type param) type)
335              (push param properties)))
336        params 'param length)
337       (nreverse properties))))
338
339 (defun query-object-class-properties (type &optional inherited-p)
340   (let* ((type-number (find-type-number type t))
341          (class (type-class-ref type-number)))
342     (unwind-protect
343          (multiple-value-bind (array length)
344              (%object-class-list-properties class)
345            (unless (null-pointer-p array)
346              (unwind-protect
347                  (%map-params array length type-number inherited-p)
348                (deallocate-memory array))))
349 ;      (type-class-unref type-number)
350       )))
351
352
353 (defun default-slot-name (name)
354   (intern (substitute #\- #\_ (string-upcase (string-upcase name)))))
355
356 (defun default-slot-accessor (class-name slot-name type)
357   (intern
358    (format
359     nil "~A-~A~A" class-name slot-name
360     (if (eq type 'boolean) "-P" ""))))
361
362
363 (defun slot-definition-from-property (class property &optional slot-name args)
364   (with-slots (name flags value-type documentation) property
365     (let* ((slot-name (or slot-name (default-slot-name name)))
366            (slot-type (or (getf args :type) (type-from-number value-type) 'pointer))
367            (accessor (default-slot-accessor class slot-name slot-type)))
368       
369       `(,slot-name
370         :allocation :property :pname ,name
371
372         ,@(when (find :unbound args) (list :unbound (getf args :unbound)))
373         ,@(when (find :getter args) (list :getter (getf args :getter)))
374         ,@(when (find :setter args) (list :setter (getf args :setter)))
375         
376         ;; accessors
377         ,@(cond
378            ((and
379              (member :writable flags) (member :readable flags)
380              (not (member :construct-only flags)))
381             (list :accessor accessor))
382            ((and (member :writable flags) (not (member :construct-only flags)))
383             (list :writer `(setf ,accessor)))
384            ((member :readable flags)
385             (list :reader accessor)))
386
387         ;; readable/writable/construct
388         ,@(when (or (not (member :writable flags))
389                     (member :construct-only flags))
390             '(:writable nil))
391         ,@(when (not (member :readable flags))
392             '(:readable nil))
393         ,@(when (or (member :construct flags) 
394                     (member :construct-only flags))
395             '(:construct t))
396         
397         ;; initargs
398         ,@(if (find :initarg args)
399               (let ((initarg (getf args :initarg)))
400                 (etypecase initarg
401                   (null ())
402                   (symbol `(:initarg ,initarg))))
403             (when (or (member :construct flags)
404                       (member :construct-only flags)
405                       (member :writable flags))
406               (list :initarg (intern (string slot-name) "KEYWORD"))))
407         
408         :type ,slot-type
409         :documentation ,documentation))))
410
411
412 (defun slot-definitions (class properties slots)
413   (loop 
414    for property in properties
415    as slot = (or
416               (find (param-name property) slots 
417                :key #'(lambda (slot) (getf (rest slot) :pname)) 
418                :test #'string=)
419               (find (param-name property) slots 
420                :key #'first :test #'string-equal))
421    do (cond
422        ((not slot) 
423         (push (slot-definition-from-property class property) slots))
424        ((getf (rest slot) :merge)
425         (setf 
426          (rest slot) 
427          (rest (slot-definition-from-property class property (first slot) (rest slot)))))))
428   (delete-if #'(lambda (slot) (getf (rest slot) :ignore)) slots))
429
430
431 (defun expand-gobject-type (type forward-p options &optional (metaclass 'gobject-class))
432   (let ((supers (cons (supertype type) (implements type)))
433         (class  (type-from-number type))
434         (slots (getf options :slots)))    
435     `(defclass ,class ,supers
436          ,(unless forward-p
437             (slot-definitions class (query-object-class-properties type) slots))
438          (:metaclass ,metaclass)
439          (:gtype ,(register-type-as type)))))
440
441 (defun gobject-dependencies (type)
442   (delete-duplicates 
443    (cons
444     (supertype type)
445     (append 
446      (type-interfaces type)
447      (mapcar #'param-value-type (query-object-class-properties type))))))
448
449
450 (register-derivable-type 'gobject "GObject" 'expand-gobject-type 'gobject-dependencies)
451
452
453 ;;; Pseudo type for gobject instances which have their reference count
454 ;;; increased by the returning function
455
456 (defmethod alien-type ((type (eql 'referenced)) &rest args)
457   (declare (ignore type args))
458   (alien-type 'gobject))
459
460 (defmethod from-alien-form (form (type (eql 'referenced)) &rest args)
461   (declare (ignore type))
462   (destructuring-bind (type) args
463     (if (subtypep type 'gobject)
464         (let ((instance (make-symbol "INSTANCE")))
465           `(let ((,instance ,(from-alien-form form type)))
466              (when ,instance
467                (%object-unref (proxy-location ,instance)))
468              ,instance))
469       (error "~A is not a subclass of GOBJECT" type))))
470
471 (export 'referenced)