chiark / gitweb /
Bug fixes
[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.44 2006-02-06 11:34:05 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 ;;   (push :debug-ref-counting *features*)
32   (defclass gobject-class (ginstance-class)
33     ((instance-slots-p :initform nil
34       :documentation "Non NIL if the class has slots with instance allocation")))
35
36   (defmethod validate-superclass ((class gobject-class) (super standard-class))
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)
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
58
59 (defbinding %object-ref () pointer
60   (location pointer))
61
62 (defbinding %object-unref () nil
63   (location pointer))
64
65 #+glib2.8
66 (progn
67   (defcallback toggle-ref-callback (nil (data pointer) (location pointer) (last-ref-p boolean))
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)))
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)))
85
86 (defmethod reference-foreign ((class gobject-class) location)
87   (declare (ignore class))
88   (%object-ref location))
89
90 (defmethod unreference-foreign ((class gobject-class) location)
91   (declare (ignore class))
92   (%object-unref location))
93
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)))
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))
121     (:user-data (find-class 'direct-user-data-slot-definition))
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))
127     (:user-data (find-class 'effective-user-data-slot-definition))
128     (t (call-next-method))))
129
130 (defmethod compute-effective-slot-definition-initargs ((class gobject-class) direct-slotds)
131   (if (typep (first direct-slotds) 'direct-property-slot-definition)
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))
143   (let ((type (slot-definition-type slotd))
144         (pname (slot-definition-pname slotd)))
145     (when (and (not (slot-boundp slotd 'getter)) (slot-readable-p slotd))
146       (setf 
147        (slot-value slotd 'getter)
148        (let ((reader nil))
149          #'(lambda (object)
150              (unless reader
151                (setq reader (reader-function type)))
152              (let ((gvalue (gvalue-new type)))
153                (%object-get-property object pname gvalue)
154                (unwind-protect
155                  (funcall reader  gvalue +gvalue-value-offset+)
156                  (gvalue-free gvalue t)))))))
157     
158     (when (and (not (slot-boundp slotd 'setter)) (slot-writable-p slotd))
159       (setf 
160        (slot-value slotd 'setter)
161        (let ((writer nil))
162          #'(lambda (value object)
163              (unless writer
164                (setq writer (writer-function type)))
165              (let ((gvalue (gvalue-new type)))
166                (funcall writer value gvalue +gvalue-value-offset+)
167                (%object-set-property object pname gvalue)
168                (gvalue-free gvalue t)
169                value))))))
170
171   (call-next-method))
172
173 (defmethod initialize-internal-slot-functions ((slotd effective-user-data-slot-definition))
174   (let ((slot-name (slot-definition-name slotd)))
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)))))
190   (call-next-method))
191
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)
197                    (not (typep slotd 'effective-special-slot-definition))))
198               (class-slots class))
199     (setf (slot-value class 'instance-slots-p) t)))
200
201
202
203 ;;;; Super class for all classes in the GObject type hierarchy
204
205 (eval-when (:compile-toplevel :load-toplevel :execute)
206   (defclass gobject (ginstance)
207     (#+debug-ref-counting
208      (ref-count :allocation :alien :type int :reader ref-count))
209     (:metaclass gobject-class)
210     (:gtype "GObject")))
211
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
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
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
242 (defmethod initialize-instance :around ((object gobject) &rest initargs)
243   (declare (ignore initargs))
244   (call-next-method)
245   #+debug-ref-counting(%object-weak-ref (foreign-location object))
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
253 (defmethod initialize-instance ((object gobject) &rest initargs)
254   (unless (slot-boundp object 'location)
255     ;; Extract initargs which we should pass directly to the GObject
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+)))))
280             (loop
281              for (pname type value) in args
282              as tmp = params then (sap+ tmp (+ string-size +gvalue-size+))
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)))
295         (setf  
296          (slot-value object 'location) 
297          (%gobject-new (type-number-of object))))))
298
299   (apply #'call-next-method object initargs))
300
301
302 (defmethod instance-finalizer ((instance gobject))
303   (let ((location (foreign-location instance)))
304     #+glib2.8
305     (if (slot-value (class-of instance) 'instance-slots-p)
306         #'(lambda ()
307             #+debug-ref-counting
308             (format t "Finalizing proxy for 0x~8,'0X~%" (sap-int location))
309             (remove-cached-instance location)
310             (%object-remove-toggle-ref location))
311       #'(lambda ()
312           #+debug-ref-counting
313           (format t "Finalizing proxy for 0x~8,'0X~%" (sap-int location))
314           (remove-cached-instance location)
315           (%object-unref location)))
316     #-glib2.8
317     #'(lambda ()
318         (remove-cached-instance location)
319           (%object-unref location))))
320
321
322 (defbinding (%gobject-new "g_object_new") () pointer
323   (type type-number)
324   (nil null))
325
326 (defbinding (%gobject-newv "g_object_newv") () pointer
327   (type type-number)
328   (n-parameters unsigned-int)
329   (params pointer))
330
331
332
333 ;;;; Property stuff
334
335 (defbinding %object-set-property () nil
336   (object gobject)
337   (name string)
338   (value gvalue))
339
340 (defbinding %object-get-property () nil
341   (object gobject)
342   (name string)
343   (value gvalue))
344
345 (defbinding %object-notify () nil
346   (object gobject)
347   (name string))
348
349 (defbinding object-freeze-notify () nil
350   (object gobject))
351
352 (defbinding object-thaw-notify () nil
353   (object gobject))
354
355
356 ;;;; User data
357
358 (defbinding %object-set-qdata-full () nil
359   (object gobject)
360   (id quark)
361   (data unsigned-long)
362   (destroy-marshal pointer))
363
364 (defcallback user-data-destroy-func (nil (id unsigned-int))
365   (destroy-user-data id))
366
367 (export 'user-data-destroy-func)
368
369 (defun (setf user-data) (data object key)
370   (%object-set-qdata-full object (quark-intern key)
371    (register-user-data data) (callback user-data-destroy-func))
372   data)
373
374 ;; deprecated
375 (defun (setf object-data) (data object key &key (test #'eq))
376   (assert (eq test #'eq))
377   (setf (user-data object key) data))
378
379 (defbinding %object-get-qdata () unsigned-long
380   (object gobject)               
381   (id quark))
382
383 (defun user-data (object key)
384   (find-user-data (%object-get-qdata object (quark-intern key))))
385
386 ;; deprecated
387 (defun object-data (object key &key (test #'eq))
388   (assert (eq test #'eq))
389   (user-data object key))
390
391 (defun user-data-p (object key)
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))))
400
401
402 ;;;;
403
404 (defbinding %object-class-list-properties () pointer
405   (class pointer)
406   (n-properties unsigned-int :out))
407
408
409 (defun %map-params (params length type inherited-p)
410   (if inherited-p
411       (map-c-vector 'list #'identity params 'param length)
412     (let ((properties ()))
413       (map-c-vector 'list 
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)
421   (let* ((type-number (find-type-number type t))
422          (class (type-class-ref type-number)))
423     (unwind-protect
424          (multiple-value-bind (array length)
425              (%object-class-list-properties class)
426            (unless (null-pointer-p array)
427              (unwind-protect
428                  (%map-params array length type-number inherited-p)
429                (deallocate-memory array))))
430 ;      (type-class-unref type-number)
431       )))
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
441     (if (eq type 'boolean) "-P" ""))))
442
443
444 (defun slot-definition-from-property (class property &optional slot-name args)
445   (with-slots (name flags value-type documentation) property
446     (let* ((slot-name (or slot-name (default-slot-name name)))
447            (slot-type (or (getf args :type) (type-from-number value-type) 'pointer))
448            (accessor (default-slot-accessor class slot-name slot-type)))
449       
450       `(,slot-name
451         :allocation :property :pname ,name
452
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)))
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
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"))))
488         
489         :type ,slot-type
490         :documentation ,documentation))))
491
492
493 (defun slot-definitions (class properties slots)
494   (loop 
495    for property in properties
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) 
508          (rest (slot-definition-from-property class property (first slot) (rest slot)))))))
509   (delete-if #'(lambda (slot) (getf (rest slot) :ignore)) slots))
510
511
512 (defun expand-gobject-type (type forward-p options &optional (metaclass 'gobject-class))
513   (let ((supers (cons (supertype type) (implements type)))
514         (class  (type-from-number type))
515         (slots (getf options :slots)))    
516     `(defclass ,class ,supers
517          ,(unless forward-p
518             (slot-definitions class (query-object-class-properties type) slots))
519          (:metaclass ,metaclass)
520          (:gtype ,(register-type-as type)))))
521
522 (defun gobject-dependencies (type)
523   (delete-duplicates 
524    (cons
525     (supertype type)
526     (append 
527      (type-interfaces type)
528      (mapcar #'param-value-type (query-object-class-properties type))))))
529
530
531 (register-derivable-type 'gobject "GObject" 'expand-gobject-type 'gobject-dependencies)
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
548                (%object-unref (foreign-location ,instance)))
549              ,instance))
550       (error "~A is not a subclass of GOBJECT" type))))
551
552 (export 'referenced)