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.41 2006-02-03 12:44: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))
68 (cache-instance (find-cached-instance location) t)
69 (cache-instance (find-cached-instance location) nil)))
71 (defbinding %object-add-toggle-ref () pointer
73 ((callback toggle-ref-callback) pointer)
76 (defbinding %object-remove-toggle-ref () pointer
78 ((callback toggle-ref-callback) pointer)
81 (defmethod reference-foreign ((class gobject-class) location)
82 (declare (ignore class))
83 (%object-ref location))
85 (defmethod unreference-foreign ((class gobject-class) location)
86 (declare (ignore class))
87 (%object-unref location))
91 (defcallback weak-ref-callback (nil (data pointer) (location pointer))
92 (format t "Object at 0x~8,'0X being finalized~%" (sap-int location)))
94 (defbinding %object-weak-ref () pointer
96 ((callback weak-ref-callback) pointer)
100 ; (defbinding object-class-install-param () nil
103 ; (parameter parameter))
105 ; (defbinding object-class-find-param-spec () parameter
109 (defun signal-name-to-string (name)
110 (substitute #\_ #\- (string-downcase (string name))))
113 (defmethod direct-slot-definition-class ((class gobject-class) &rest initargs)
114 (case (getf initargs :allocation)
115 (:property (find-class 'direct-property-slot-definition))
116 (:user-data (find-class 'direct-user-data-slot-definition))
117 (t (call-next-method))))
119 (defmethod effective-slot-definition-class ((class gobject-class) &rest initargs)
120 (case (getf initargs :allocation)
121 (:property (find-class 'effective-property-slot-definition))
122 (:user-data (find-class 'effective-user-data-slot-definition))
123 (t (call-next-method))))
125 (defmethod compute-effective-slot-definition-initargs ((class gobject-class) direct-slotds)
126 (if (typep (first direct-slotds) 'direct-property-slot-definition)
128 (list :pname (signal-name-to-string
129 (most-specific-slot-value direct-slotds 'pname))
130 :readable (most-specific-slot-value direct-slotds 'readable)
131 :writable (most-specific-slot-value direct-slotds 'writable)
132 :construct (most-specific-slot-value direct-slotds 'construct))
137 (defmethod initialize-internal-slot-functions ((slotd effective-property-slot-definition))
138 (let ((type (slot-definition-type slotd))
139 (pname (slot-definition-pname slotd)))
140 (when (and (not (slot-boundp slotd 'getter)) (slot-readable-p slotd))
142 (slot-value slotd 'getter)
146 (setq reader (reader-function type)))
147 (let ((gvalue (gvalue-new type)))
148 (%object-get-property object pname gvalue)
150 (funcall reader gvalue +gvalue-value-offset+)
151 (gvalue-free gvalue t)))))))
153 (when (and (not (slot-boundp slotd 'setter)) (slot-writable-p slotd))
155 (slot-value slotd 'setter)
157 #'(lambda (value object)
159 (setq writer (writer-function type)))
160 (let ((gvalue (gvalue-new type)))
161 (funcall writer value gvalue +gvalue-value-offset+)
162 (%object-set-property object pname gvalue)
163 (gvalue-free gvalue t)
168 (defmethod initialize-internal-slot-functions ((slotd effective-user-data-slot-definition))
169 (let ((slot-name (slot-definition-name slotd)))
170 (unless (slot-boundp slotd 'getter)
172 (slot-value slotd 'getter)
174 (prog1 (user-data object slot-name)))))
175 (unless (slot-boundp slotd 'setter)
177 (slot-value slotd 'setter)
178 #'(lambda (value object)
179 (setf (user-data object slot-name) value))))
180 (unless (slot-boundp slotd 'boundp)
182 (slot-value slotd 'boundp)
184 (user-data-p object slot-name)))))
187 (defmethod shared-initialize :after ((class gobject-class) names &rest initargs)
188 (declare (ignore initargs))
189 (when (some #'(lambda (slotd)
191 (eq (slot-definition-allocation slotd) :instance)
192 (not (typep slotd 'effective-special-slot-definition))))
194 (setf (slot-value class 'instance-slots-p) t)))
198 ;;;; Super class for all classes in the GObject type hierarchy
200 (eval-when (:compile-toplevel :load-toplevel :execute)
201 (defclass gobject (ginstance)
203 (:metaclass gobject-class)
207 (defun initial-add (object function initargs key pkey)
209 as (initarg value . rest) = initargs then rest
211 ((eq initarg key) (funcall function object value))
212 ((eq initarg pkey) (mapc #'(lambda (value)
213 (funcall function object value))
217 (defun initial-apply-add (object function initargs key pkey)
218 (initial-add object #'(lambda (object value)
219 (apply function object (mklist value)))
223 (defmethod initialize-instance :around ((object gobject) &rest initargs)
224 (declare (ignore initargs))
226 #+debug-ref-counting(%object-weak-ref (proxy-location object))
228 (when (slot-value (class-of object) 'instance-slots-p)
229 (with-slots (location) object
230 (%object-add-toggle-ref location)
231 (%object-unref location))))
234 (defmethod initialize-instance ((object gobject) &rest initargs)
235 (unless (slot-boundp object 'location)
236 ;; Extract initargs which we should pass directly to the GObject
238 (let* ((slotds (class-slots (class-of object)))
241 as (key value . rest) = initargs then rest
244 (member key (slot-definition-initargs slotd)))
246 when (and (typep slotd 'effective-property-slot-definition)
247 (slot-value slotd 'construct))
251 (slot-definition-pname slotd)
252 (slot-definition-type slotd)
256 (let* ((string-size (size-of 'string))
257 (string-writer (writer-function 'string))
258 (string-destroy (destroy-function 'string))
259 (params (allocate-memory
260 (* (length args) (+ string-size +gvalue-size+)))))
262 for (pname type value) in args
263 as tmp = params then (sap+ tmp (+ string-size +gvalue-size+))
264 do (funcall string-writer pname tmp)
265 (gvalue-init (sap+ tmp string-size) type value))
268 (slot-value object 'location)
269 (%gobject-newv (type-number-of object) (length args) params))
272 as tmp = params then (sap+ tmp (+ string-size +gvalue-size+))
273 do (funcall string-destroy tmp)
274 (gvalue-unset (sap+ tmp string-size)))
275 (deallocate-memory params)))
277 (slot-value object 'location)
278 (%gobject-new (type-number-of object))))))
280 (apply #'call-next-method object initargs))
283 (defmethod instance-finalizer ((instance gobject))
284 (let ((location (proxy-location instance)))
286 (if (slot-value (class-of instance) 'instance-slots-p)
289 (format t "Finalizing proxy for 0x~8,'0X~%" (sap-int location))
290 (remove-cached-instance location)
291 (%object-remove-toggle-ref location))
294 (format t "Finalizing proxy for 0x~8,'0X~%" (sap-int location))
295 (remove-cached-instance location)
296 (%object-unref location)))
299 (remove-cached-instance location)
300 (%object-unref location))))
303 (defbinding (%gobject-new "g_object_new") () pointer
307 (defbinding (%gobject-newv "g_object_newv") () pointer
309 (n-parameters unsigned-int)
316 (defbinding %object-set-property () nil
321 (defbinding %object-get-property () nil
326 (defbinding %object-notify () nil
330 (defbinding object-freeze-notify () nil
333 (defbinding object-thaw-notify () nil
339 (defbinding %object-set-qdata-full () nil
343 (destroy-marshal pointer))
345 (defcallback user-data-destroy-func (nil (id unsigned-int))
346 (destroy-user-data id))
348 (export 'user-data-destroy-func)
350 (defun (setf user-data) (data object key)
351 (%object-set-qdata-full object (quark-intern key)
352 (register-user-data data) (callback user-data-destroy-func))
356 (defun (setf object-data) (data object key &key (test #'eq))
357 (assert (eq test #'eq))
358 (setf (user-data object key) data))
360 (defbinding %object-get-qdata () unsigned-long
364 (defun user-data (object key)
365 (find-user-data (%object-get-qdata object (quark-intern key))))
368 (defun object-data (object key &key (test #'eq))
369 (assert (eq test #'eq))
370 (user-data object key))
372 (defun user-data-p (object key)
373 (user-data-exists-p (%object-get-qdata object (quark-intern key))))
375 (defbinding %object-steal-qdata () unsigned-long
379 (defun unset-user-data (object key)
380 (destroy-user-data (%object-steal-qdata object (quark-intern key))))
385 (defbinding %object-class-list-properties () pointer
387 (n-properties unsigned-int :out))
390 (defun %map-params (params length type inherited-p)
392 (map-c-vector 'list #'identity params 'param length)
393 (let ((properties ()))
396 (when (eql (param-owner-type param) type)
397 (push param properties)))
398 params 'param length)
399 (nreverse properties))))
401 (defun query-object-class-properties (type &optional inherited-p)
402 (let* ((type-number (find-type-number type t))
403 (class (type-class-ref type-number)))
405 (multiple-value-bind (array length)
406 (%object-class-list-properties class)
407 (unless (null-pointer-p array)
409 (%map-params array length type-number inherited-p)
410 (deallocate-memory array))))
411 ; (type-class-unref type-number)
415 (defun default-slot-name (name)
416 (intern (substitute #\- #\_ (string-upcase (string-upcase name)))))
418 (defun default-slot-accessor (class-name slot-name type)
421 nil "~A-~A~A" class-name slot-name
422 (if (eq type 'boolean) "-P" ""))))
425 (defun slot-definition-from-property (class property &optional slot-name args)
426 (with-slots (name flags value-type documentation) property
427 (let* ((slot-name (or slot-name (default-slot-name name)))
428 (slot-type (or (getf args :type) (type-from-number value-type) 'pointer))
429 (accessor (default-slot-accessor class slot-name slot-type)))
432 :allocation :property :pname ,name
434 ,@(when (find :unbound args) (list :unbound (getf args :unbound)))
435 ,@(when (find :getter args) (list :getter (getf args :getter)))
436 ,@(when (find :setter args) (list :setter (getf args :setter)))
441 (member :writable flags) (member :readable flags)
442 (not (member :construct-only flags)))
443 (list :accessor accessor))
444 ((and (member :writable flags) (not (member :construct-only flags)))
445 (list :writer `(setf ,accessor)))
446 ((member :readable flags)
447 (list :reader accessor)))
449 ;; readable/writable/construct
450 ,@(when (or (not (member :writable flags))
451 (member :construct-only flags))
453 ,@(when (not (member :readable flags))
455 ,@(when (or (member :construct flags)
456 (member :construct-only flags))
460 ,@(if (find :initarg args)
461 (let ((initarg (getf args :initarg)))
464 (symbol `(:initarg ,initarg))))
465 (when (or (member :construct flags)
466 (member :construct-only flags)
467 (member :writable flags))
468 (list :initarg (intern (string slot-name) "KEYWORD"))))
471 :documentation ,documentation))))
474 (defun slot-definitions (class properties slots)
476 for property in properties
478 (find (param-name property) slots
479 :key #'(lambda (slot) (getf (rest slot) :pname))
481 (find (param-name property) slots
482 :key #'first :test #'string-equal))
485 (push (slot-definition-from-property class property) slots))
486 ((getf (rest slot) :merge)
489 (rest (slot-definition-from-property class property (first slot) (rest slot)))))))
490 (delete-if #'(lambda (slot) (getf (rest slot) :ignore)) slots))
493 (defun expand-gobject-type (type forward-p options &optional (metaclass 'gobject-class))
494 (let ((supers (cons (supertype type) (implements type)))
495 (class (type-from-number type))
496 (slots (getf options :slots)))
497 `(defclass ,class ,supers
499 (slot-definitions class (query-object-class-properties type) slots))
500 (:metaclass ,metaclass)
501 (:gtype ,(register-type-as type)))))
503 (defun gobject-dependencies (type)
508 (type-interfaces type)
509 (mapcar #'param-value-type (query-object-class-properties type))))))
512 (register-derivable-type 'gobject "GObject" 'expand-gobject-type 'gobject-dependencies)
515 ;;; Pseudo type for gobject instances which have their reference count
516 ;;; increased by the returning function
518 (defmethod alien-type ((type (eql 'referenced)) &rest args)
519 (declare (ignore type args))
520 (alien-type 'gobject))
522 (defmethod from-alien-form (form (type (eql 'referenced)) &rest args)
523 (declare (ignore type))
524 (destructuring-bind (type) args
525 (if (subtypep type 'gobject)
526 (let ((instance (make-symbol "INSTANCE")))
527 `(let ((,instance ,(from-alien-form form type)))
529 (%object-unref (proxy-location ,instance)))
531 (error "~A is not a subclass of GOBJECT" type))))