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