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