chiark / gitweb /
Force asdf to call the C source file blah.c, rather than just blah. I'm
[clg] / glib / gobject.lisp
1 ;; Common Lisp bindings for GTK+ v2.x
2 ;; Copyright 2000-2006 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.59 2008-11-04 03:22:23 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 :reader instance-slots-p
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 (defmethod slot-unbound (metaclass (class gobject-class) (slot (eql 'ref)))
41   (assert (class-direct-superclasses class))
42   (setf (slot-value class 'ref) 
43    #?-(pkg-exists-p "glib-2.0" :atleast-version "2.10.0") '%object-ref
44    #?(pkg-exists-p "glib-2.0" :atleast-version "2.10.0")
45    ;; We do this hack instead of creating a new metaclass to avoid
46    ;; breaking backward compatibility
47    (if (subtypep (class-name class) 'initially-unowned)
48        '%object-ref-sink
49      '%object-ref)))
50
51 (defmethod slot-unbound (metaclass (class gobject-class) (slot (eql 'unref)))
52   (setf (slot-value class 'unref) '%object-unref))
53
54
55 (defclass direct-property-slot-definition (direct-virtual-slot-definition)
56   ((pname :reader slot-definition-pname :initarg :pname)
57    (readable :reader slot-readable-p :initarg :readable)
58    (writable :reader slot-writable-p :initarg :writable)
59    (construct-only :initarg :construct-only :reader construct-only-property-p)))
60
61 (defclass effective-property-slot-definition (effective-virtual-slot-definition)
62   ((pname :reader slot-definition-pname :initarg :pname)
63    (readable :initform t :reader slot-readable-p :initarg :readable)
64    (writable :initform t :reader slot-writable-p :initarg :writable)
65    (construct-only :initform nil :initarg :construct-only :reader construct-only-property-p)))
66
67 (defclass direct-user-data-slot-definition (direct-virtual-slot-definition)
68   ())
69
70 (defclass effective-user-data-slot-definition (effective-virtual-slot-definition)
71   ())
72
73
74 (defbinding %object-ref () pointer
75   (location pointer))
76
77 (defbinding %object-unref () nil
78   (location pointer))
79
80 #?(pkg-exists-p "glib-2.0" :atleast-version "2.8.0")
81 (progn
82   (define-callback toggle-ref-callback nil
83       ((data pointer) (location pointer) (last-ref-p boolean))
84     (declare (ignore data))
85     #+debug-ref-counting
86     (if last-ref-p
87         (format t "Object at 0x~8,'0X has no foreign references~%" (pointer-address location))
88       (format t "Foreign reference added to object at 0x~8,'0X~%" (pointer-address location)))
89     (if last-ref-p
90         (cache-instance (find-cached-instance location) t)
91       (cache-instance (find-cached-instance location) nil)))
92
93   (defbinding %object-add-toggle-ref (location) pointer
94     (location pointer)
95     (toggle-ref-callback callback)
96     (nil null))
97
98   (defbinding %object-remove-toggle-ref (location) pointer
99     (location pointer)
100     (toggle-ref-callback callback)
101     (nil null)))
102
103 #+debug-ref-counting
104 (progn
105   (define-callback weak-ref-callback nil ((data pointer) (location pointer))
106     (format t "Object at 0x~8,'0X (~A) being finalized~%" (pointer-address location) (type-from-number (%type-number-of-ginstance location))))
107   
108   (defbinding %object-weak-ref (location) pointer
109     (location pointer)
110     (weak-ref-callback callback)
111     (nil null)))
112
113
114 ; (defbinding object-class-install-param () nil
115 ;   (class pointer)
116 ;   (id unsigned-int)
117 ;   (parameter parameter))
118
119 ; (defbinding object-class-find-param-spec () parameter
120 ;   (class pointer)
121 ;   (name string))
122
123 (defun signal-name-to-string (name)
124   (substitute #\_ #\- (string-downcase (string name))))
125
126
127 (defmethod direct-slot-definition-class ((class gobject-class) &rest initargs)
128   (case (getf initargs :allocation)
129     (:property (find-class 'direct-property-slot-definition))
130     (:user-data (find-class 'direct-user-data-slot-definition))
131     (t (call-next-method))))
132
133 (defmethod effective-slot-definition-class ((class gobject-class) &rest initargs)
134   (case (getf initargs :allocation)
135     (:property (find-class 'effective-property-slot-definition))
136     (:user-data (find-class 'effective-user-data-slot-definition))
137     (t (call-next-method))))
138
139 (defmethod compute-effective-slot-definition-initargs ((class gobject-class) direct-slotds)
140   (if (eq (slot-definition-allocation (first direct-slotds)) :property)
141       (nconc
142        (compute-most-specific-initargs direct-slotds
143         '(pname construct-only readable writable))
144        (call-next-method))
145     (call-next-method)))
146
147
148 (defvar *ignore-setting-construct-only-property* nil)
149 (declaim (special *ignore-setting-construct-only-property*))
150
151 (defmethod compute-slot-reader-function ((slotd effective-property-slot-definition) &optional signal-unbound-p)
152   (declare (ignore signal-unbound-p))
153   (let* ((type (slot-definition-type slotd))
154          (pname (slot-definition-pname slotd))
155          (get-reader (reader-function type :ref :get))
156          (peek-reader (reader-function type :ref :peek)))
157     #'(lambda (object)
158         (with-memory (gvalue +gvalue-size+)
159           (%gvalue-init gvalue (find-type-number type))
160           (%object-get-property object pname gvalue)
161           (if (gvalue-static-p gvalue)
162               (funcall peek-reader gvalue +gvalue-value-offset+)
163             (funcall get-reader gvalue +gvalue-value-offset+))))))
164
165 (defmethod compute-slot-writer-function :around ((slotd effective-property-slot-definition))
166   (if (construct-only-property-p slotd)
167       #'(lambda (value object)
168           (declare (ignore value))
169           (unless *ignore-setting-construct-only-property*
170             (error 'unwritable-slot :name (slot-definition-name slotd) :instance object)))
171     (call-next-method)))
172
173 (defmethod compute-slot-writer-function ((slotd effective-property-slot-definition))
174   (let* ((type (slot-definition-type slotd))
175          (pname (slot-definition-pname slotd))
176          (writer (writer-function type :temp t))
177          (destroy (destroy-function type :temp t)))
178     #'(lambda (value object)
179         (with-memory (gvalue +gvalue-size+)
180           (%gvalue-init gvalue (find-type-number type))
181           (funcall writer value gvalue +gvalue-value-offset+)
182           (%object-set-property object pname gvalue)
183           (funcall destroy gvalue +gvalue-value-offset+))
184         value)))
185
186 (defmethod slot-readable-p ((slotd effective-user-data-slot-definition))
187   (declare (ignore slotd))
188   t)
189
190 (defmethod compute-slot-reader-function ((slotd effective-user-data-slot-definition) &optional signal-unbound-p)
191   (declare (ignore signal-unbound-p))
192   (let ((slot-name (slot-definition-name slotd)))
193     #'(lambda (object)
194         (user-data object slot-name))))
195
196 (defmethod compute-slot-boundp-function ((slotd effective-user-data-slot-definition))
197   (let ((slot-name (slot-definition-name slotd)))
198     #'(lambda (object)
199         (user-data-p object slot-name))))
200
201 (defmethod slot-writable-p ((slotd effective-user-data-slot-definition))
202   (declare (ignore slotd))
203   t)
204
205 (defmethod compute-slot-writer-function ((slotd effective-user-data-slot-definition))
206   (let ((slot-name (slot-definition-name slotd)))
207     #'(lambda (value object)
208         (setf (user-data object slot-name) value))))
209
210 (defmethod compute-slot-makunbound-function ((slotd effective-user-data-slot-definition))
211   (let ((slot-name (slot-definition-name slotd)))
212     #'(lambda (object)
213         (unset-user-data object slot-name))))
214
215 (defmethod compute-slots :around ((class gobject-class))
216   (let ((slots (call-next-method)))
217     (when (some #'(lambda (slotd)
218                     (and
219                      (eq (slot-definition-allocation slotd) :instance)
220                      (not (typep slotd 'effective-special-slot-definition))))
221                 slots)
222       (setf (slot-value class 'instance-slots-p) t))
223     slots))
224
225
226 ;;;; Super class for all classes in the GObject type hierarchy
227
228 (eval-when (:compile-toplevel :load-toplevel :execute)
229   (defclass gobject (ginstance)
230     (#+debug-ref-counting
231      (ref-count :allocation :alien :type int :reader ref-count))
232     (:metaclass gobject-class)
233     (:gtype "GObject")))
234
235 #+debug-ref-counting
236 (defmethod print-object ((instance gobject) stream)
237   (print-unreadable-object (instance stream :type t :identity nil)
238     (if (proxy-valid-p instance)
239         (format stream "at 0x~X (~D)" (pointer-address (foreign-location instance)) (ref-count instance))
240       (write-string "at \"unbound\"" stream))))
241
242
243 (define-type-method reader-function ((type gobject) &key (ref :read) inlined)
244   (assert-not-inlined type inlined)
245   (ecase ref
246     ((:read :peek) (call-next-method type :ref :read))
247     (:get
248      #'(lambda (location &optional (offset 0))
249          (let ((instance (ref-pointer location offset)))
250            (unless (null-pointer-p instance)
251              (multiple-value-bind (gobject new-p)
252                  (ensure-proxy-instance 'gobject instance :reference nil)
253                (unless new-p
254                  (%object-unref instance))
255                (setf (ref-pointer location offset) (make-pointer 0))
256                gobject)))))))
257
258 (define-type-method callback-wrapper ((type gobject) var arg form)
259   (let ((class (type-expand type)))
260     `(let ((,var (ensure-proxy-instance ',class ,arg)))
261        ,form)))
262
263 (defun initial-add (object function initargs key pkey)
264   (loop 
265    as (initarg value . rest) = initargs then rest
266    do (cond
267        ((eq initarg key) (funcall function object value))
268        ((eq initarg pkey) (mapc #'(lambda (value)
269                                     (funcall function object value))
270                                 value)))
271        while rest))
272
273 (defun initial-apply-add (object function initargs key pkey)
274   (initial-add object #'(lambda (object value)
275                           (apply function object (mklist value)))
276                initargs key pkey))
277
278
279 (defmethod make-proxy-instance ((class gobject-class) location &rest initargs)
280   (declare (ignore location initargs))
281   (if (slot-value class 'instance-slots-p)
282       (error "Objects of class ~A has instance slots and should only be created with MAKE-INSTANCE" class)
283     (call-next-method)))
284
285 (defparameter +gparameter-gvalue-offset+
286   (max (size-of 'pointer) (type-alignment '(unsigned-byte 64))))
287 (defparameter +gparameter-size+
288   (+ +gparameter-gvalue-offset+ +gvalue-size+))
289
290 (defmethod allocate-foreign ((object gobject) &rest initargs)
291   (let ((init-slots ())) 
292     (flet ((value-from-initargs (slotd)
293              (loop
294               with slot-initargs = (slot-definition-initargs slotd)
295               for (initarg value) on initargs by #'cddr
296               when (find initarg slot-initargs)
297               do (return (values value t)))))
298
299     (loop 
300      for slotd in (class-slots (class-of object))
301      when (and 
302            (eq (slot-definition-allocation slotd) :property)
303            (construct-only-property-p slotd))
304      do (multiple-value-bind (value initarg-p) (value-from-initargs slotd)
305           (cond
306            (initarg-p (push (cons slotd value) init-slots))
307            ((slot-definition-initfunction slotd)
308             (push 
309              (cons slotd (funcall (slot-definition-initfunction slotd)))
310              init-slots))))))
311
312     (cond
313      (init-slots
314       (let* ((num-slots (length init-slots)))
315         (with-memory (params (* num-slots +gparameter-size+))
316           (loop
317            with string-writer = (writer-function 'string)
318            for (slotd . value) in init-slots
319            as param = params then (pointer+ param +gparameter-size+)
320            as type = (slot-definition-type slotd)
321            as pname = (slot-definition-pname slotd)
322            do (funcall string-writer pname param)
323               (gvalue-init 
324                (pointer+ param +gparameter-gvalue-offset+) type value))
325
326           (unwind-protect
327               (%gobject-newv (type-number-of object) num-slots params)
328         
329             (loop
330              with string-destroy = (destroy-function 'string)
331              repeat num-slots
332              as param = params then (pointer+ param +gparameter-size+)
333              do (funcall string-destroy param)
334                 (gvalue-unset (pointer+ param +gparameter-gvalue-offset+)))))))
335      
336      (t (%gobject-new (type-number-of object))))))
337
338
339 (defmethod shared-initialize ((object gobject) names &rest initargs)
340   (declare (ignore names initargs))
341   (let ((*ignore-setting-construct-only-property* t))
342     (call-next-method)))
343
344 (defmethod initialize-instance :around ((object gobject) &rest initargs)
345   (declare (ignore initargs))
346   (prog1
347       (call-next-method)
348     (let ((location (foreign-location object)))
349       #+debug-ref-counting(%object-weak-ref location)
350       #?(pkg-exists-p "glib-2.0" :atleast-version "2.8.0")
351       (when (slot-value (class-of object) 'instance-slots-p)
352         (%object-add-toggle-ref location)
353         (%object-unref location)))))
354
355
356 (defmethod instance-finalizer ((instance gobject))
357   (let ((location (foreign-location instance)))
358     #?(pkg-exists-p "glib-2.0" :atleast-version "2.8.0")
359     (if (slot-value (class-of instance) 'instance-slots-p)
360         #'(lambda ()
361             #+debug-ref-counting
362             (format t "Finalizing proxy for 0x~8,'0X (~A)~%" 
363              (pointer-address location) 
364              (find-foreign-type-name (%type-number-of-ginstance location)))
365             (%object-remove-toggle-ref location))
366       #'(lambda ()
367           #+debug-ref-counting
368           (format t "Finalizing proxy for 0x~8,'0X (~A)~%" 
369            (pointer-address location)
370            (find-foreign-type-name (%type-number-of-ginstance location)))
371           (%object-unref location)))
372     #?-(pkg-exists-p "glib-2.0" :atleast-version "2.8.0")
373     #'(lambda ()
374         (%object-unref location))))
375
376
377 (defbinding (%gobject-new "g_object_new") () pointer
378   (type type-number)
379   (nil null))
380
381 (defbinding (%gobject-newv "g_object_newv") () pointer
382   (type type-number)
383   (n-parameters unsigned-int)
384   (params pointer))
385
386
387 ;;;; Floating references
388
389 #?(pkg-exists-p "glib-2.0" :atleast-version "2.10.0")
390 (progn
391   (defclass initially-unowned (gobject)
392     ()
393     (:metaclass gobject-class)
394     (:gtype "GInitiallyUnowned"))
395
396   (defbinding %object-ref-sink () pointer
397     (location pointer))
398
399   (defbinding %object-is-floating () boolean
400     (location pointer))
401
402   (defmethod initialize-instance :before ((object initially-unowned) &rest initargs)
403     (declare (ignore initargs))
404     (%object-ref-sink (foreign-location object))))
405
406
407 ;;;; Property stuff
408
409 (defbinding %object-set-property () nil
410   (object gobject)
411   (name string)
412   (value gvalue))
413
414 (defbinding %object-get-property () nil
415   (object gobject)
416   (name string)
417   (value gvalue))
418
419 (defbinding %object-notify () nil
420   (object gobject)
421   (name string))
422
423 (defbinding object-freeze-notify () nil
424   (object gobject))
425
426 (defbinding object-thaw-notify () nil
427   (object gobject))
428
429
430 ;;;; User data
431
432 (defgeneric (setf user-data) (data object key))
433 (defgeneric user-data (object key))
434 (defgeneric user-data-p (object key))
435 (defgeneric unset-user-data (object key))
436
437 (defbinding %object-set-qdata-full () nil
438   (object gobject)
439   (id quark)
440   (data pointer-data)
441   (destroy-marshal callback))
442
443 (define-callback user-data-destroy-callback nil ((id pointer-data))
444   (destroy-user-data id))
445
446 (defmethod (setf user-data) (data (object gobject) key)
447   (%object-set-qdata-full object (quark-intern key)
448    (register-user-data data) user-data-destroy-callback)
449   data)
450
451 (defbinding %object-get-qdata () pointer-data
452   (object gobject)               
453   (id quark))
454
455 (defmethod user-data ((object gobject) key)
456   (find-user-data (%object-get-qdata object (quark-intern key))))
457
458 (defmethod user-data-p ((object gobject) key)
459   (user-data-exists-p (%object-get-qdata object (quark-intern key))))
460
461 (defbinding %object-steal-qdata () pointer-data
462   (object gobject)               
463   (id quark))
464
465 (defmethod unset-user-data ((object gobject) key)
466   (destroy-user-data (%object-steal-qdata object (quark-intern key))))
467
468
469 ;;;;
470
471 (defbinding %object-class-list-properties () pointer
472   (class pointer)
473   (n-properties unsigned-int :out))
474
475
476 (defun %map-params (params length type inherited-p)
477   (if inherited-p
478       (map-c-vector 'list #'identity params 'param length)
479     (let ((properties ()))
480       (map-c-vector 'list 
481        #'(lambda (param)
482            (when (eql (param-owner-type param) type)
483              (push param properties)))
484        params 'param length)
485       (nreverse properties))))
486
487 (defun query-object-class-properties (type &optional inherited-p)
488   (let* ((type-number (find-type-number type t))
489          (class (type-class-ref type-number)))
490     (unwind-protect
491          (multiple-value-bind (array length)
492              (%object-class-list-properties class)
493            (unless (null-pointer-p array)
494              (unwind-protect
495                  (%map-params array length type-number inherited-p)
496                (deallocate-memory array))))
497 ;      (type-class-unref type-number)
498       )))
499
500
501 (defun default-slot-name (name)
502   (let ((prefix-len (length (package-prefix))))
503     (intern (substitute #\- #\_
504              (string-upcase
505               (if (and
506                    (string-prefix-p (package-prefix) name)
507                    (char= #\- (char name prefix-len)))
508                   (subseq name (1+ prefix-len))
509                 name))))))
510
511 (defun default-slot-accessor (class-name slot-name type)
512   (intern
513    (format
514     nil "~A-~A~A" class-name slot-name
515     (if (eq type 'boolean) "-P" ""))))
516
517
518 (defun slot-definition-from-property (class property &optional slot-name args)
519   (with-slots (name flags value-type documentation) property
520     (let* ((slot-name (or slot-name (default-slot-name name)))
521            (slot-type (or (getf args :type) (type-from-number value-type) 'pointer))
522            (accessor (default-slot-accessor class slot-name slot-type)))
523       
524       `(,slot-name
525         :allocation :property :pname ,name
526
527         ,@(when (find :unbound args) (list :unbound (getf args :unbound)))
528         ,@(when (find :getter args) (list :getter (getf args :getter)))
529         ,@(when (find :setter args) (list :setter (getf args :setter)))
530         
531         ;; accessors
532         ,@(cond
533            ((and
534              (member :writable flags) (member :readable flags)
535              (not (member :construct-only flags)))
536             (list :accessor accessor))
537            ((and (member :writable flags) (not (member :construct-only flags)))
538             (list :writer `(setf ,accessor)))
539            ((member :readable flags)
540             (list :reader accessor)))
541
542         ;; readable/writable/construct
543         ,@(when (or (not (member :writable flags))
544                     (member :construct-only flags))
545             '(:writable nil))
546         ,@(when (not (member :readable flags))
547             '(:readable nil))
548         ,@(when (member :construct-only flags)
549             '(:construct-only t))
550         
551         ;; initargs
552         ,@(if (find :initarg args)
553               (let ((initarg (getf args :initarg)))
554                 (etypecase initarg
555                   (null ())
556                   (symbol `(:initarg ,initarg))))
557             (when (or (member :construct flags)
558                       (member :construct-only flags)
559                       (member :writable flags))
560               (list :initarg (intern (string slot-name) "KEYWORD"))))
561         
562         :type ,slot-type
563         :documentation ,documentation))))
564
565
566 (defun slot-definitions (class properties slots)
567   (loop 
568    for property in properties
569    as slot = (or
570               (find (param-name property) slots 
571                :key #'(lambda (slot) (getf (rest slot) :pname)) 
572                :test #'string=)
573               (find (param-name property) slots 
574                :key #'first :test #'string-equal))
575    do (cond
576        ((not slot) 
577         (push (slot-definition-from-property class property) slots))
578        ((getf (rest slot) :merge)
579         (setf 
580          (rest slot) 
581          (rest (slot-definition-from-property class property (first slot) (rest slot)))))))
582   (delete-if #'(lambda (slot) (getf (rest slot) :ignore)) slots))
583
584
585 (defun expand-gobject-type (type forward-p options &optional (metaclass 'gobject-class))
586   (let ((supers (cons (supertype type) (implements type)))
587         (class  (type-from-number type))
588         (slots (getf options :slots)))
589     `(defclass ,class ,supers
590          ,(unless forward-p
591             (slot-definitions class (query-object-class-properties type) slots))
592          (:metaclass ,metaclass)
593          (:gtype ,(register-type-as type)))))
594
595 (defun gobject-dependencies (type options)
596   (delete-duplicates 
597    (cons
598     (supertype type)
599     (append 
600      (type-interfaces type)
601      (mapcar #'param-value-type (query-object-class-properties type))
602      (getf options :dependencies)
603      (loop
604       for slot in (getf options :slots)
605       as type = (getf (rest slot) :type)
606       when (and type (symbolp type) (find-type-number type))
607       collect (find-type-number type))))))
608
609
610 (register-derivable-type 'gobject "GObject" 'expand-gobject-type 'gobject-dependencies)
611
612
613 ;;; Pseudo type for gobject instances which have their reference count
614 ;;; increased by the returning function
615
616 (deftype referenced (type) type)
617
618 (define-type-method from-alien-form ((type referenced) form &key (ref :free))
619   (cond
620    ((not (eq ref :free))
621     (error "Keyword arg :REF to FROM-ALIEN-FORM should be :FREE for type ~A. It was give ~A" type ref))
622    ((subtypep type 'gobject)
623     (from-alien-form (second (type-expand-to 'referenced type)) form :ref ref))))
624
625 (define-type-method from-alien-function ((type referenced) &key (ref :free))
626   (cond
627    ((not (eq ref :free))
628     (error "Keyword arg :REF to FROM-ALIEN-FUNCTION should be :FREE for type ~A. It was give ~A" type ref))
629 ;   ((subtypep type 'gobject) (call-next-method type ref :free))))
630    ((subtypep type 'gobject) 
631     (from-alien-function (second (type-expand-to 'referenced type)) :ref ref))))