1 ;; Common Lisp bindings for GTK+ v2.x
2 ;; Copyright 2000-2005 Espen S. Johnsen <espen@users.sf.net>
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:
12 ;; The above copyright notice and this permission notice shall be
13 ;; included in all copies or substantial portions of the Software.
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.
23 ;; $Id: gobject.lisp,v 1.42 2006/02/04 12:15:32 espen Exp $
28 ;;;; Metaclass used for subclasses of gobject
30 (eval-when (:compile-toplevel :load-toplevel :execute)
31 (defclass gobject-class (ginstance-class)
32 ((instance-slots-p :initform nil
33 :documentation "Non NIL if the class has slots with instance allocation")))
35 (defmethod validate-superclass ((class gobject-class) (super standard-class))
36 ; (subtypep (class-name super) 'gobject)
39 (defclass direct-property-slot-definition (direct-virtual-slot-definition)
40 ((pname :reader slot-definition-pname :initarg :pname)
41 (readable :initform t :reader slot-readable-p :initarg :readable)
42 (writable :initform t :reader slot-writable-p :initarg :writable)
43 (construct :initform nil :initarg :construct)))
45 (defclass effective-property-slot-definition (effective-virtual-slot-definition)
46 ((pname :reader slot-definition-pname :initarg :pname)
47 (readable :reader slot-readable-p :initarg :readable)
48 (writable :reader slot-writable-p :initarg :writable)
49 (construct :initarg :construct)))
51 (defclass direct-user-data-slot-definition (direct-virtual-slot-definition)
54 (defclass effective-user-data-slot-definition (effective-virtual-slot-definition)
58 (defbinding %object-ref () pointer
61 (defbinding %object-unref () nil
66 (defcallback toggle-ref-callback (nil (data pointer) (location pointer) (last-ref-p boolean))
69 (format t "Object at 0x~8,'0X has no foreign references~%" (sap-int location))
70 (format t "Foreign reference added to object at 0x~8,'0X~%" (sap-int location)))
72 (cache-instance (find-cached-instance location) t)
73 (cache-instance (find-cached-instance location) nil)))
75 (defbinding %object-add-toggle-ref () pointer
77 ((callback toggle-ref-callback) pointer)
80 (defbinding %object-remove-toggle-ref () pointer
82 ((callback toggle-ref-callback) pointer)
85 (defmethod reference-foreign ((class gobject-class) location)
86 (declare (ignore class))
87 (%object-ref location))
89 (defmethod unreference-foreign ((class gobject-class) location)
90 (declare (ignore class))
91 (%object-unref location))
95 (defcallback weak-ref-callback (nil (data pointer) (location pointer))
96 (format t "Object at 0x~8,'0X being finalized~%" (sap-int location)))
98 (defbinding %object-weak-ref () pointer
100 ((callback weak-ref-callback) pointer)
104 ; (defbinding object-class-install-param () nil
107 ; (parameter parameter))
109 ; (defbinding object-class-find-param-spec () parameter
113 (defun signal-name-to-string (name)
114 (substitute #\_ #\- (string-downcase (string name))))
117 (defmethod direct-slot-definition-class ((class gobject-class) &rest initargs)
118 (case (getf initargs :allocation)
119 (:property (find-class 'direct-property-slot-definition))
120 (:user-data (find-class 'direct-user-data-slot-definition))
121 (t (call-next-method))))
123 (defmethod effective-slot-definition-class ((class gobject-class) &rest initargs)
124 (case (getf initargs :allocation)
125 (:property (find-class 'effective-property-slot-definition))
126 (:user-data (find-class 'effective-user-data-slot-definition))
127 (t (call-next-method))))
129 (defmethod compute-effective-slot-definition-initargs ((class gobject-class) direct-slotds)
130 (if (typep (first direct-slotds) 'direct-property-slot-definition)
132 (list :pname (signal-name-to-string
133 (most-specific-slot-value direct-slotds 'pname))
134 :readable (most-specific-slot-value direct-slotds 'readable)
135 :writable (most-specific-slot-value direct-slotds 'writable)
136 :construct (most-specific-slot-value direct-slotds 'construct))
141 (defmethod initialize-internal-slot-functions ((slotd effective-property-slot-definition))
142 (let ((type (slot-definition-type slotd))
143 (pname (slot-definition-pname slotd)))
144 (when (and (not (slot-boundp slotd 'getter)) (slot-readable-p slotd))
146 (slot-value slotd 'getter)
150 (setq reader (reader-function type)))
151 (let ((gvalue (gvalue-new type)))
152 (%object-get-property object pname gvalue)
154 (funcall reader gvalue +gvalue-value-offset+)
155 (gvalue-free gvalue t)))))))
157 (when (and (not (slot-boundp slotd 'setter)) (slot-writable-p slotd))
159 (slot-value slotd 'setter)
161 #'(lambda (value object)
163 (setq writer (writer-function type)))
164 (let ((gvalue (gvalue-new type)))
165 (funcall writer value gvalue +gvalue-value-offset+)
166 (%object-set-property object pname gvalue)
167 (gvalue-free gvalue t)
172 (defmethod initialize-internal-slot-functions ((slotd effective-user-data-slot-definition))
173 (let ((slot-name (slot-definition-name slotd)))
174 (unless (slot-boundp slotd 'getter)
176 (slot-value slotd 'getter)
178 (prog1 (user-data object slot-name)))))
179 (unless (slot-boundp slotd 'setter)
181 (slot-value slotd 'setter)
182 #'(lambda (value object)
183 (setf (user-data object slot-name) value))))
184 (unless (slot-boundp slotd 'boundp)
186 (slot-value slotd 'boundp)
188 (user-data-p object slot-name)))))
191 (defmethod shared-initialize :after ((class gobject-class) names &rest initargs)
192 (declare (ignore initargs))
193 (when (some #'(lambda (slotd)
195 (eq (slot-definition-allocation slotd) :instance)
196 (not (typep slotd 'effective-special-slot-definition))))
198 (setf (slot-value class 'instance-slots-p) t)))
202 ;;;; Super class for all classes in the GObject type hierarchy
204 (eval-when (:compile-toplevel :load-toplevel :execute)
205 (defclass gobject (ginstance)
207 (:metaclass gobject-class)
211 (defun initial-add (object function initargs key pkey)
213 as (initarg value . rest) = initargs then rest
215 ((eq initarg key) (funcall function object value))
216 ((eq initarg pkey) (mapc #'(lambda (value)
217 (funcall function object value))
221 (defun initial-apply-add (object function initargs key pkey)
222 (initial-add object #'(lambda (object value)
223 (apply function object (mklist value)))
227 (defmethod initialize-instance :around ((object gobject) &rest initargs)
228 (declare (ignore initargs))
230 #+debug-ref-counting(%object-weak-ref (foreign-location object))
232 (when (slot-value (class-of object) 'instance-slots-p)
233 (with-slots (location) object
234 (%object-add-toggle-ref location)
235 (%object-unref location))))
238 (defmethod initialize-instance ((object gobject) &rest initargs)
239 (unless (slot-boundp object 'location)
240 ;; Extract initargs which we should pass directly to the GObject
242 (let* ((slotds (class-slots (class-of object)))
245 as (key value . rest) = initargs then rest
248 (member key (slot-definition-initargs slotd)))
250 when (and (typep slotd 'effective-property-slot-definition)
251 (slot-value slotd 'construct))
255 (slot-definition-pname slotd)
256 (slot-definition-type slotd)
260 (let* ((string-size (size-of 'string))
261 (string-writer (writer-function 'string))
262 (string-destroy (destroy-function 'string))
263 (params (allocate-memory
264 (* (length args) (+ string-size +gvalue-size+)))))
266 for (pname type value) in args
267 as tmp = params then (sap+ tmp (+ string-size +gvalue-size+))
268 do (funcall string-writer pname tmp)
269 (gvalue-init (sap+ tmp string-size) type value))
272 (slot-value object 'location)
273 (%gobject-newv (type-number-of object) (length args) params))
276 as tmp = params then (sap+ tmp (+ string-size +gvalue-size+))
277 do (funcall string-destroy tmp)
278 (gvalue-unset (sap+ tmp string-size)))
279 (deallocate-memory params)))
281 (slot-value object 'location)
282 (%gobject-new (type-number-of object))))))
284 (apply #'call-next-method object initargs))
287 (defmethod instance-finalizer ((instance gobject))
288 (let ((location (foreign-location instance)))
290 (if (slot-value (class-of instance) 'instance-slots-p)
293 (format t "Finalizing proxy for 0x~8,'0X~%" (sap-int location))
294 (remove-cached-instance location)
295 (%object-remove-toggle-ref location))
298 (format t "Finalizing proxy for 0x~8,'0X~%" (sap-int location))
299 (remove-cached-instance location)
300 (%object-unref location)))
303 (remove-cached-instance location)
304 (%object-unref location))))
307 (defbinding (%gobject-new "g_object_new") () pointer
311 (defbinding (%gobject-newv "g_object_newv") () pointer
313 (n-parameters unsigned-int)
320 (defbinding %object-set-property () nil
325 (defbinding %object-get-property () nil
330 (defbinding %object-notify () nil
334 (defbinding object-freeze-notify () nil
337 (defbinding object-thaw-notify () nil
343 (defbinding %object-set-qdata-full () nil
347 (destroy-marshal pointer))
349 (defcallback user-data-destroy-func (nil (id unsigned-int))
350 (destroy-user-data id))
352 (export 'user-data-destroy-func)
354 (defun (setf user-data) (data object key)
355 (%object-set-qdata-full object (quark-intern key)
356 (register-user-data data) (callback user-data-destroy-func))
360 (defun (setf object-data) (data object key &key (test #'eq))
361 (assert (eq test #'eq))
362 (setf (user-data object key) data))
364 (defbinding %object-get-qdata () unsigned-long
368 (defun user-data (object key)
369 (find-user-data (%object-get-qdata object (quark-intern key))))
372 (defun object-data (object key &key (test #'eq))
373 (assert (eq test #'eq))
374 (user-data object key))
376 (defun user-data-p (object key)
377 (user-data-exists-p (%object-get-qdata object (quark-intern key))))
379 (defbinding %object-steal-qdata () unsigned-long
383 (defun unset-user-data (object key)
384 (destroy-user-data (%object-steal-qdata object (quark-intern key))))
389 (defbinding %object-class-list-properties () pointer
391 (n-properties unsigned-int :out))
394 (defun %map-params (params length type inherited-p)
396 (map-c-vector 'list #'identity params 'param length)
397 (let ((properties ()))
400 (when (eql (param-owner-type param) type)
401 (push param properties)))
402 params 'param length)
403 (nreverse properties))))
405 (defun query-object-class-properties (type &optional inherited-p)
406 (let* ((type-number (find-type-number type t))
407 (class (type-class-ref type-number)))
409 (multiple-value-bind (array length)
410 (%object-class-list-properties class)
411 (unless (null-pointer-p array)
413 (%map-params array length type-number inherited-p)
414 (deallocate-memory array))))
415 ; (type-class-unref type-number)
419 (defun default-slot-name (name)
420 (intern (substitute #\- #\_ (string-upcase (string-upcase name)))))
422 (defun default-slot-accessor (class-name slot-name type)
425 nil "~A-~A~A" class-name slot-name
426 (if (eq type 'boolean) "-P" ""))))
429 (defun slot-definition-from-property (class property &optional slot-name args)
430 (with-slots (name flags value-type documentation) property
431 (let* ((slot-name (or slot-name (default-slot-name name)))
432 (slot-type (or (getf args :type) (type-from-number value-type) 'pointer))
433 (accessor (default-slot-accessor class slot-name slot-type)))
436 :allocation :property :pname ,name
438 ,@(when (find :unbound args) (list :unbound (getf args :unbound)))
439 ,@(when (find :getter args) (list :getter (getf args :getter)))
440 ,@(when (find :setter args) (list :setter (getf args :setter)))
445 (member :writable flags) (member :readable flags)
446 (not (member :construct-only flags)))
447 (list :accessor accessor))
448 ((and (member :writable flags) (not (member :construct-only flags)))
449 (list :writer `(setf ,accessor)))
450 ((member :readable flags)
451 (list :reader accessor)))
453 ;; readable/writable/construct
454 ,@(when (or (not (member :writable flags))
455 (member :construct-only flags))
457 ,@(when (not (member :readable flags))
459 ,@(when (or (member :construct flags)
460 (member :construct-only flags))
464 ,@(if (find :initarg args)
465 (let ((initarg (getf args :initarg)))
468 (symbol `(:initarg ,initarg))))
469 (when (or (member :construct flags)
470 (member :construct-only flags)
471 (member :writable flags))
472 (list :initarg (intern (string slot-name) "KEYWORD"))))
475 :documentation ,documentation))))
478 (defun slot-definitions (class properties slots)
480 for property in properties
482 (find (param-name property) slots
483 :key #'(lambda (slot) (getf (rest slot) :pname))
485 (find (param-name property) slots
486 :key #'first :test #'string-equal))
489 (push (slot-definition-from-property class property) slots))
490 ((getf (rest slot) :merge)
493 (rest (slot-definition-from-property class property (first slot) (rest slot)))))))
494 (delete-if #'(lambda (slot) (getf (rest slot) :ignore)) slots))
497 (defun expand-gobject-type (type forward-p options &optional (metaclass 'gobject-class))
498 (let ((supers (cons (supertype type) (implements type)))
499 (class (type-from-number type))
500 (slots (getf options :slots)))
501 `(defclass ,class ,supers
503 (slot-definitions class (query-object-class-properties type) slots))
504 (:metaclass ,metaclass)
505 (:gtype ,(register-type-as type)))))
507 (defun gobject-dependencies (type)
512 (type-interfaces type)
513 (mapcar #'param-value-type (query-object-class-properties type))))))
516 (register-derivable-type 'gobject "GObject" 'expand-gobject-type 'gobject-dependencies)
519 ;;; Pseudo type for gobject instances which have their reference count
520 ;;; increased by the returning function
522 (defmethod alien-type ((type (eql 'referenced)) &rest args)
523 (declare (ignore type args))
524 (alien-type 'gobject))
526 (defmethod from-alien-form (form (type (eql 'referenced)) &rest args)
527 (declare (ignore type))
528 (destructuring-bind (type) args
529 (if (subtypep type 'gobject)
530 (let ((instance (make-symbol "INSTANCE")))
531 `(let ((,instance ,(from-alien-form form type)))
533 (%object-unref (foreign-location ,instance)))
535 (error "~A is not a subclass of GOBJECT" type))))