chiark / gitweb /
Got rid of a warning about an unused variable
[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.49 2006-02-19 22:24:37 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 :reader slot-readable-p :initarg :readable)
43    (writable :reader slot-writable-p :initarg :writable)
44    (construct-only :initarg :construct-only :reader construct-only-property-p)))
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-only :initarg :construct-only :reader construct-only-property-p)))
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   (define-callback toggle-ref-callback nil
68       ((data pointer) (location pointer) (last-ref-p boolean))
69     #+debug-ref-counting
70     (if last-ref-p
71         (format t "Object at 0x~8,'0X has no foreign references~%" (sap-int location))
72       (format t "Foreign reference added to object at 0x~8,'0X~%" (sap-int location)))
73     (if last-ref-p
74         (cache-instance (find-cached-instance location) t)
75       (cache-instance (find-cached-instance location) nil)))
76
77   (defbinding %object-add-toggle-ref (location) pointer
78     (location pointer)
79     (toggle-ref-callback callback)
80     (nil null))
81
82   (defbinding %object-remove-toggle-ref (location) pointer
83     (location pointer)
84     (toggle-ref-callback callback)
85     (nil null)))
86
87 (defmethod reference-foreign ((class gobject-class) location)
88   (declare (ignore class))
89   (%object-ref location))
90
91 (defmethod unreference-foreign ((class gobject-class) location)
92   (declare (ignore class))
93   (%object-unref location))
94
95 #+debug-ref-counting
96 (progn
97   (define-callback weak-ref-callback nil ((data pointer) (location pointer))
98     (format t "Object at 0x~8,'0X being finalized~%" (sap-int location)))
99   
100   (defbinding %object-weak-ref (location) pointer
101     (location pointer)
102     (weak-ref-callback callback)
103     (nil null)))
104
105
106 ; (defbinding object-class-install-param () nil
107 ;   (class pointer)
108 ;   (id unsigned-int)
109 ;   (parameter parameter))
110
111 ; (defbinding object-class-find-param-spec () parameter
112 ;   (class pointer)
113 ;   (name string))
114
115 (defun signal-name-to-string (name)
116   (substitute #\_ #\- (string-downcase (string name))))
117
118
119 (defmethod direct-slot-definition-class ((class gobject-class) &rest initargs)
120   (case (getf initargs :allocation)
121     (:property (find-class 'direct-property-slot-definition))
122     (:user-data (find-class 'direct-user-data-slot-definition))
123     (t (call-next-method))))
124
125 (defmethod effective-slot-definition-class ((class gobject-class) &rest initargs)
126   (case (getf initargs :allocation)
127     (:property (find-class 'effective-property-slot-definition))
128     (:user-data (find-class 'effective-user-data-slot-definition))
129     (t (call-next-method))))
130
131 (defmethod compute-effective-slot-definition-initargs ((class gobject-class) direct-slotds)
132   (if (eq (slot-definition-allocation (first direct-slotds)) :property)
133       (nconc 
134        (list :pname (signal-name-to-string 
135                      (most-specific-slot-value direct-slotds 'pname
136                       (slot-definition-name (first direct-slotds))))
137              :readable (most-specific-slot-value direct-slotds 'readable t)
138              :writable (most-specific-slot-value direct-slotds 'writable t)
139              :construct-only (most-specific-slot-value direct-slotds 
140                               'construct-only nil))
141        (call-next-method))
142     (call-next-method)))
143
144
145 (defvar *ignore-setting-construct-only-property* nil)
146 (declaim (special *ignore-setting-construct-only-property*))
147
148 (defmethod initialize-internal-slot-functions ((slotd effective-property-slot-definition))
149   (let ((type (slot-definition-type slotd))
150         (pname (slot-definition-pname slotd)))
151     (when (and (not (slot-boundp slotd 'getter)) (slot-readable-p slotd))
152       (setf 
153        (slot-value slotd 'getter)
154        (let ((reader nil))
155          #'(lambda (object)
156              (unless reader
157                (setq reader (reader-function type)))
158              (let ((gvalue (gvalue-new type)))
159                (%object-get-property object pname gvalue)
160                (unwind-protect
161                  (funcall reader  gvalue +gvalue-value-offset+)
162                  (gvalue-free gvalue t)))))))
163     
164     (when (not (slot-boundp slotd 'setter))
165       (cond
166        ((slot-writable-p slotd)
167         (setf 
168          (slot-value slotd 'setter)
169          (let ((writer nil))
170            #'(lambda (value object)
171                (unless writer
172                  (setq writer (writer-function type)))
173                (let ((gvalue (gvalue-new type)))
174                  (funcall writer value gvalue +gvalue-value-offset+)
175                  (%object-set-property object pname gvalue)
176                  (gvalue-free gvalue t)
177                  value)))))
178
179        ((construct-only-property-p slotd)
180         (setf 
181          (slot-value slotd 'setter)
182          #'(lambda (value object)
183              (declare (ignore value object))
184              (unless *ignore-setting-construct-only-property*
185                (error "Slot is not writable: ~A" (slot-definition-name slotd)))))))))
186
187   (call-next-method))
188
189 (defmethod initialize-internal-slot-functions ((slotd effective-user-data-slot-definition))
190   (let ((slot-name (slot-definition-name slotd)))
191     (unless (slot-boundp slotd 'getter)
192       (setf 
193        (slot-value slotd 'getter)
194        #'(lambda (object)
195            (prog1 (user-data object slot-name)))))
196     (unless (slot-boundp slotd 'setter)
197       (setf 
198        (slot-value slotd 'setter)
199        #'(lambda (value object)
200            (setf (user-data object slot-name) value))))
201     (unless (slot-boundp slotd 'boundp)
202       (setf 
203        (slot-value slotd 'boundp)
204        #'(lambda (object)
205            (user-data-p object slot-name)))))
206   (call-next-method))
207
208 (defmethod shared-initialize :after ((class gobject-class) names &rest initargs)
209   (declare (ignore initargs))
210   (when (some #'(lambda (slotd)
211                   (and
212                    (eq (slot-definition-allocation slotd) :instance)
213                    (not (typep slotd 'effective-special-slot-definition))))
214               (class-slots class))
215     (setf (slot-value class 'instance-slots-p) t)))
216
217
218
219 ;;;; Super class for all classes in the GObject type hierarchy
220
221 (eval-when (:compile-toplevel :load-toplevel :execute)
222   (defclass gobject (ginstance)
223     (#+debug-ref-counting
224      (ref-count :allocation :alien :type int :reader ref-count))
225     (:metaclass gobject-class)
226     (:gtype "GObject")))
227
228 #+debug-ref-counting
229 (defmethod print-object ((instance gobject) stream)
230   (print-unreadable-object (instance stream :type t :identity nil)
231     (if (proxy-valid-p instance)
232         (format stream "at 0x~X (~D)" (sap-int (foreign-location instance)) (ref-count instance))
233       (write-string "at \"unbound\"" stream))))
234
235
236 (defun initial-add (object function initargs key pkey)
237   (loop 
238    as (initarg value . rest) = initargs then rest
239    do (cond
240        ((eq initarg key) (funcall function object value))
241        ((eq initarg pkey) (mapc #'(lambda (value)
242                                     (funcall function object value))
243                                 value)))
244        while rest))
245
246 (defun initial-apply-add (object function initargs key pkey)
247   (initial-add object #'(lambda (object value)
248                           (apply function object (mklist value)))
249                initargs key pkey))
250
251
252 (defmethod make-proxy-instance ((class gobject-class) location &rest initargs)
253   (declare (ignore location initargs))
254   (if (slot-value class 'instance-slots-p)
255       (error "An object of class ~A has instance slots and should only be created with MAKE-INSTANCE" class)
256     (call-next-method)))
257
258
259 (defmethod allocate-foreign ((object gobject) &rest initargs)
260   (let ((init-slots ())) 
261     (flet ((value-from-initargs (slotd)
262              (loop
263               with slot-initargs = (slot-definition-initargs slotd)
264               for (initarg value) on initargs by #'cddr
265               when (find initarg slot-initargs)
266               do (return (values value t)))))
267
268     (loop 
269      for slotd in (class-slots (class-of object))
270      when (and 
271            (eq (slot-definition-allocation slotd) :property)
272            (construct-only-property-p slotd))
273      do (multiple-value-bind (value initarg-p) (value-from-initargs slotd)
274           (cond
275            (initarg-p (push (cons slotd value) init-slots))
276            ((slot-definition-initfunction slotd)
277             (push 
278              (cons slotd (funcall (slot-definition-initfunction slotd)))
279              init-slots))))))
280
281     (cond
282      (init-slots
283       (let ((element-size (+ +gvalue-size+ +size-of-pointer+))
284             (num-slots (length init-slots)))
285         (with-allocated-memory (params (* num-slots element-size))
286           (loop
287            with string-writer = (writer-function 'string)
288            for (slotd . value) in init-slots
289            as offset = params then (sap+ offset element-size)
290            as type = (slot-definition-type slotd)
291            as pname = (slot-definition-pname slotd)
292            do (funcall string-writer pname offset)
293               (gvalue-init (sap+ offset +size-of-pointer+) type value))
294
295           (unwind-protect
296               (%gobject-newv (type-number-of object) num-slots params)
297         
298             (loop
299              with string-destroy = (destroy-function 'string)
300              repeat num-slots
301              as offset = params then (sap+ offset element-size)
302              do (funcall string-destroy offset)
303                 (gvalue-unset (sap+ offset +size-of-pointer+)))))))
304
305      (t (%gobject-new (type-number-of object))))))
306
307
308 (defmethod shared-initialize ((object gobject) names &rest initargs)
309   (declare (ignore names initargs))
310   (let ((*ignore-setting-construct-only-property* t))
311     (call-next-method)))
312
313 (defmethod initialize-instance :around ((object gobject) &rest initargs)
314   (declare (ignore initargs))
315   (prog1
316       (call-next-method)
317     #+debug-ref-counting(%object-weak-ref (foreign-location object))
318     #+glib2.8
319     (when (slot-value (class-of object) 'instance-slots-p)
320       (with-slots (location) object
321         (%object-add-toggle-ref location)
322         (%object-unref location)))))
323
324
325 (defmethod instance-finalizer ((instance gobject))
326   (let ((location (foreign-location instance)))
327     #+glib2.8
328     (if (slot-value (class-of instance) 'instance-slots-p)
329         #'(lambda ()
330             #+debug-ref-counting
331             (format t "Finalizing proxy for 0x~8,'0X~%" (sap-int location))
332             (remove-cached-instance location)
333             (%object-remove-toggle-ref location))
334       #'(lambda ()
335           #+debug-ref-counting
336           (format t "Finalizing proxy for 0x~8,'0X~%" (sap-int location))
337           (remove-cached-instance location)
338           (%object-unref location)))
339     #-glib2.8
340     #'(lambda ()
341         (remove-cached-instance location)
342           (%object-unref location))))
343
344
345 (defbinding (%gobject-new "g_object_new") () pointer
346   (type type-number)
347   (nil null))
348
349 (defbinding (%gobject-newv "g_object_newv") () pointer
350   (type type-number)
351   (n-parameters unsigned-int)
352   (params pointer))
353
354
355
356 ;;;; Property stuff
357
358 (defbinding %object-set-property () nil
359   (object gobject)
360   (name string)
361   (value gvalue))
362
363 (defbinding %object-get-property () nil
364   (object gobject)
365   (name string)
366   (value gvalue))
367
368 (defbinding %object-notify () nil
369   (object gobject)
370   (name string))
371
372 (defbinding object-freeze-notify () nil
373   (object gobject))
374
375 (defbinding object-thaw-notify () nil
376   (object gobject))
377
378
379 ;;;; User data
380
381 (defbinding %object-set-qdata-full () nil
382   (object gobject)
383   (id quark)
384   (data unsigned-long)
385   (destroy-marshal callback))
386
387 (define-callback user-data-destroy-callback nil ((id unsigned-int))
388   (destroy-user-data id))
389
390 (defun (setf user-data) (data object key)
391   (%object-set-qdata-full object (quark-intern key)
392    (register-user-data data) user-data-destroy-callback)
393   data)
394
395 ;; deprecated
396 (defun (setf object-data) (data object key &key (test #'eq))
397   (assert (eq test #'eq))
398   (setf (user-data object key) data))
399
400 (defbinding %object-get-qdata () unsigned-long
401   (object gobject)               
402   (id quark))
403
404 (defun user-data (object key)
405   (find-user-data (%object-get-qdata object (quark-intern key))))
406
407 ;; deprecated
408 (defun object-data (object key &key (test #'eq))
409   (assert (eq test #'eq))
410   (user-data object key))
411
412 (defun user-data-p (object key)
413   (user-data-exists-p (%object-get-qdata object (quark-intern key))))
414
415 (defbinding %object-steal-qdata () unsigned-long
416   (object gobject)               
417   (id quark))
418
419 (defun unset-user-data (object key)
420   (destroy-user-data (%object-steal-qdata object (quark-intern key))))
421
422
423 ;;;;
424
425 (defbinding %object-class-list-properties () pointer
426   (class pointer)
427   (n-properties unsigned-int :out))
428
429
430 (defun %map-params (params length type inherited-p)
431   (if inherited-p
432       (map-c-vector 'list #'identity params 'param length)
433     (let ((properties ()))
434       (map-c-vector 'list 
435        #'(lambda (param)
436            (when (eql (param-owner-type param) type)
437              (push param properties)))
438        params 'param length)
439       (nreverse properties))))
440
441 (defun query-object-class-properties (type &optional inherited-p)
442   (let* ((type-number (find-type-number type t))
443          (class (type-class-ref type-number)))
444     (unwind-protect
445          (multiple-value-bind (array length)
446              (%object-class-list-properties class)
447            (unless (null-pointer-p array)
448              (unwind-protect
449                  (%map-params array length type-number inherited-p)
450                (deallocate-memory array))))
451 ;      (type-class-unref type-number)
452       )))
453
454
455 (defun default-slot-name (name)
456   (intern (substitute #\- #\_ (string-upcase (string-upcase name)))))
457
458 (defun default-slot-accessor (class-name slot-name type)
459   (intern
460    (format
461     nil "~A-~A~A" class-name slot-name
462     (if (eq type 'boolean) "-P" ""))))
463
464
465 (defun slot-definition-from-property (class property &optional slot-name args)
466   (with-slots (name flags value-type documentation) property
467     (let* ((slot-name (or slot-name (default-slot-name name)))
468            (slot-type (or (getf args :type) (type-from-number value-type) 'pointer))
469            (accessor (default-slot-accessor class slot-name slot-type)))
470       
471       `(,slot-name
472         :allocation :property :pname ,name
473
474         ,@(when (find :unbound args) (list :unbound (getf args :unbound)))
475         ,@(when (find :getter args) (list :getter (getf args :getter)))
476         ,@(when (find :setter args) (list :setter (getf args :setter)))
477         
478         ;; accessors
479         ,@(cond
480            ((and
481              (member :writable flags) (member :readable flags)
482              (not (member :construct-only flags)))
483             (list :accessor accessor))
484            ((and (member :writable flags) (not (member :construct-only flags)))
485             (list :writer `(setf ,accessor)))
486            ((member :readable flags)
487             (list :reader accessor)))
488
489         ;; readable/writable/construct
490         ,@(when (or (not (member :writable flags))
491                     (member :construct-only flags))
492             '(:writable nil))
493         ,@(when (not (member :readable flags))
494             '(:readable nil))
495         ,@(when (member :construct-only flags)
496             '(:construct-only t))
497         
498         ;; initargs
499         ,@(if (find :initarg args)
500               (let ((initarg (getf args :initarg)))
501                 (etypecase initarg
502                   (null ())
503                   (symbol `(:initarg ,initarg))))
504             (when (or (member :construct flags)
505                       (member :construct-only flags)
506                       (member :writable flags))
507               (list :initarg (intern (string slot-name) "KEYWORD"))))
508         
509         :type ,slot-type
510         :documentation ,documentation))))
511
512
513 (defun slot-definitions (class properties slots)
514   (loop 
515    for property in properties
516    as slot = (or
517               (find (param-name property) slots 
518                :key #'(lambda (slot) (getf (rest slot) :pname)) 
519                :test #'string=)
520               (find (param-name property) slots 
521                :key #'first :test #'string-equal))
522    do (cond
523        ((not slot) 
524         (push (slot-definition-from-property class property) slots))
525        ((getf (rest slot) :merge)
526         (setf 
527          (rest slot) 
528          (rest (slot-definition-from-property class property (first slot) (rest slot)))))))
529   (delete-if #'(lambda (slot) (getf (rest slot) :ignore)) slots))
530
531
532 (defun expand-gobject-type (type forward-p options &optional (metaclass 'gobject-class))
533   (let ((supers (cons (supertype type) (implements type)))
534         (class  (type-from-number type))
535         (slots (getf options :slots)))    
536     `(defclass ,class ,supers
537          ,(unless forward-p
538             (slot-definitions class (query-object-class-properties type) slots))
539          (:metaclass ,metaclass)
540          (:gtype ,(register-type-as type)))))
541
542 (defun gobject-dependencies (type)
543   (delete-duplicates 
544    (cons
545     (supertype type)
546     (append 
547      (type-interfaces type)
548      (mapcar #'param-value-type (query-object-class-properties type))))))
549
550
551 (register-derivable-type 'gobject "GObject" 'expand-gobject-type 'gobject-dependencies)
552
553
554 ;;; Pseudo type for gobject instances which have their reference count
555 ;;; increased by the returning function
556
557 (defmethod alien-type ((type (eql 'referenced)) &rest args)
558   (declare (ignore type args))
559   (alien-type 'gobject))
560
561 (defmethod from-alien-form (form (type (eql 'referenced)) &rest args)
562   (declare (ignore type))
563   (destructuring-bind (type) args
564     (if (subtypep type 'gobject)
565         (let ((instance (make-symbol "INSTANCE")))
566           `(let ((,instance ,(from-alien-form form type)))
567              (when ,instance
568                (%object-unref (foreign-location ,instance)))
569              ,instance))
570       (error "~A is not a subclass of GOBJECT" type))))
571
572 (export 'referenced)