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.44 2006-02-06 11:34:05 espen Exp $
28 ;;;; Metaclass used for subclasses of gobject
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")))
36 (defmethod validate-superclass ((class gobject-class) (super standard-class))
37 ; (subtypep (class-name super) 'gobject)
40 (defclass direct-property-slot-definition (direct-virtual-slot-definition)
41 ((pname :reader slot-definition-pname :initarg :pname)
42 (readable :initform t :reader slot-readable-p :initarg :readable)
43 (writable :initform t :reader slot-writable-p :initarg :writable)
44 (construct :initform nil :initarg :construct)))
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 :initarg :construct)))
52 (defclass direct-user-data-slot-definition (direct-virtual-slot-definition)
55 (defclass effective-user-data-slot-definition (effective-virtual-slot-definition)
59 (defbinding %object-ref () pointer
62 (defbinding %object-unref () nil
67 (defcallback toggle-ref-callback (nil (data pointer) (location pointer) (last-ref-p boolean))
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)))
73 (cache-instance (find-cached-instance location) t)
74 (cache-instance (find-cached-instance location) nil)))
76 (defbinding %object-add-toggle-ref () pointer
78 ((callback toggle-ref-callback) pointer)
81 (defbinding %object-remove-toggle-ref () pointer
83 ((callback toggle-ref-callback) pointer)
86 (defmethod reference-foreign ((class gobject-class) location)
87 (declare (ignore class))
88 (%object-ref location))
90 (defmethod unreference-foreign ((class gobject-class) location)
91 (declare (ignore class))
92 (%object-unref location))
96 (defcallback weak-ref-callback (nil (data pointer) (location pointer))
97 (format t "Object at 0x~8,'0X being finalized~%" (sap-int location)))
99 (defbinding %object-weak-ref () pointer
101 ((callback weak-ref-callback) pointer)
105 ; (defbinding object-class-install-param () nil
108 ; (parameter parameter))
110 ; (defbinding object-class-find-param-spec () parameter
114 (defun signal-name-to-string (name)
115 (substitute #\_ #\- (string-downcase (string name))))
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))))
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))))
130 (defmethod compute-effective-slot-definition-initargs ((class gobject-class) direct-slotds)
131 (if (typep (first direct-slotds) 'direct-property-slot-definition)
133 (list :pname (signal-name-to-string
134 (most-specific-slot-value direct-slotds 'pname))
135 :readable (most-specific-slot-value direct-slotds 'readable)
136 :writable (most-specific-slot-value direct-slotds 'writable)
137 :construct (most-specific-slot-value direct-slotds 'construct))
142 (defmethod initialize-internal-slot-functions ((slotd effective-property-slot-definition))
143 (let ((type (slot-definition-type slotd))
144 (pname (slot-definition-pname slotd)))
145 (when (and (not (slot-boundp slotd 'getter)) (slot-readable-p slotd))
147 (slot-value slotd 'getter)
151 (setq reader (reader-function type)))
152 (let ((gvalue (gvalue-new type)))
153 (%object-get-property object pname gvalue)
155 (funcall reader gvalue +gvalue-value-offset+)
156 (gvalue-free gvalue t)))))))
158 (when (and (not (slot-boundp slotd 'setter)) (slot-writable-p slotd))
160 (slot-value slotd 'setter)
162 #'(lambda (value object)
164 (setq writer (writer-function type)))
165 (let ((gvalue (gvalue-new type)))
166 (funcall writer value gvalue +gvalue-value-offset+)
167 (%object-set-property object pname gvalue)
168 (gvalue-free gvalue t)
173 (defmethod initialize-internal-slot-functions ((slotd effective-user-data-slot-definition))
174 (let ((slot-name (slot-definition-name slotd)))
175 (unless (slot-boundp slotd 'getter)
177 (slot-value slotd 'getter)
179 (prog1 (user-data object slot-name)))))
180 (unless (slot-boundp slotd 'setter)
182 (slot-value slotd 'setter)
183 #'(lambda (value object)
184 (setf (user-data object slot-name) value))))
185 (unless (slot-boundp slotd 'boundp)
187 (slot-value slotd 'boundp)
189 (user-data-p object slot-name)))))
192 (defmethod shared-initialize :after ((class gobject-class) names &rest initargs)
193 (declare (ignore initargs))
194 (when (some #'(lambda (slotd)
196 (eq (slot-definition-allocation slotd) :instance)
197 (not (typep slotd 'effective-special-slot-definition))))
199 (setf (slot-value class 'instance-slots-p) t)))
203 ;;;; Super class for all classes in the GObject type hierarchy
205 (eval-when (:compile-toplevel :load-toplevel :execute)
206 (defclass gobject (ginstance)
207 (#+debug-ref-counting
208 (ref-count :allocation :alien :type int :reader ref-count))
209 (:metaclass gobject-class)
213 (defmethod print-object ((instance gobject) stream)
214 (print-unreadable-object (instance stream :type t :identity nil)
215 (if (slot-boundp instance 'location)
216 (format stream "at 0x~X (~D)" (sap-int (foreign-location instance)) (ref-count instance))
217 (write-string "at \"unbound\"" stream))))
220 (defun initial-add (object function initargs key pkey)
222 as (initarg value . rest) = initargs then rest
224 ((eq initarg key) (funcall function object value))
225 ((eq initarg pkey) (mapc #'(lambda (value)
226 (funcall function object value))
230 (defun initial-apply-add (object function initargs key pkey)
231 (initial-add object #'(lambda (object value)
232 (apply function object (mklist value)))
236 (defmethod make-proxy-instance ((class gobject-class) location &rest initargs)
237 (declare (ignore location initargs))
238 (if (slot-value class 'instance-slots-p)
239 (error "An object of class ~A has instance slots and should only be created with MAKE-INSTANCE" class)
242 (defmethod initialize-instance :around ((object gobject) &rest initargs)
243 (declare (ignore initargs))
245 #+debug-ref-counting(%object-weak-ref (foreign-location object))
247 (when (slot-value (class-of object) 'instance-slots-p)
248 (with-slots (location) object
249 (%object-add-toggle-ref location)
250 (%object-unref location))))
253 (defmethod initialize-instance ((object gobject) &rest initargs)
254 (unless (slot-boundp object 'location)
255 ;; Extract initargs which we should pass directly to the GObject
257 (let* ((slotds (class-slots (class-of object)))
260 as (key value . rest) = initargs then rest
263 (member key (slot-definition-initargs slotd)))
265 when (and (typep slotd 'effective-property-slot-definition)
266 (slot-value slotd 'construct))
270 (slot-definition-pname slotd)
271 (slot-definition-type slotd)
275 (let* ((string-size (size-of 'string))
276 (string-writer (writer-function 'string))
277 (string-destroy (destroy-function 'string))
278 (params (allocate-memory
279 (* (length args) (+ string-size +gvalue-size+)))))
281 for (pname type value) in args
282 as tmp = params then (sap+ tmp (+ string-size +gvalue-size+))
283 do (funcall string-writer pname tmp)
284 (gvalue-init (sap+ tmp string-size) type value))
287 (slot-value object 'location)
288 (%gobject-newv (type-number-of object) (length args) params))
291 as tmp = params then (sap+ tmp (+ string-size +gvalue-size+))
292 do (funcall string-destroy tmp)
293 (gvalue-unset (sap+ tmp string-size)))
294 (deallocate-memory params)))
296 (slot-value object 'location)
297 (%gobject-new (type-number-of object))))))
299 (apply #'call-next-method object initargs))
302 (defmethod instance-finalizer ((instance gobject))
303 (let ((location (foreign-location instance)))
305 (if (slot-value (class-of instance) 'instance-slots-p)
308 (format t "Finalizing proxy for 0x~8,'0X~%" (sap-int location))
309 (remove-cached-instance location)
310 (%object-remove-toggle-ref location))
313 (format t "Finalizing proxy for 0x~8,'0X~%" (sap-int location))
314 (remove-cached-instance location)
315 (%object-unref location)))
318 (remove-cached-instance location)
319 (%object-unref location))))
322 (defbinding (%gobject-new "g_object_new") () pointer
326 (defbinding (%gobject-newv "g_object_newv") () pointer
328 (n-parameters unsigned-int)
335 (defbinding %object-set-property () nil
340 (defbinding %object-get-property () nil
345 (defbinding %object-notify () nil
349 (defbinding object-freeze-notify () nil
352 (defbinding object-thaw-notify () nil
358 (defbinding %object-set-qdata-full () nil
362 (destroy-marshal pointer))
364 (defcallback user-data-destroy-func (nil (id unsigned-int))
365 (destroy-user-data id))
367 (export 'user-data-destroy-func)
369 (defun (setf user-data) (data object key)
370 (%object-set-qdata-full object (quark-intern key)
371 (register-user-data data) (callback user-data-destroy-func))
375 (defun (setf object-data) (data object key &key (test #'eq))
376 (assert (eq test #'eq))
377 (setf (user-data object key) data))
379 (defbinding %object-get-qdata () unsigned-long
383 (defun user-data (object key)
384 (find-user-data (%object-get-qdata object (quark-intern key))))
387 (defun object-data (object key &key (test #'eq))
388 (assert (eq test #'eq))
389 (user-data object key))
391 (defun user-data-p (object key)
392 (user-data-exists-p (%object-get-qdata object (quark-intern key))))
394 (defbinding %object-steal-qdata () unsigned-long
398 (defun unset-user-data (object key)
399 (destroy-user-data (%object-steal-qdata object (quark-intern key))))
404 (defbinding %object-class-list-properties () pointer
406 (n-properties unsigned-int :out))
409 (defun %map-params (params length type inherited-p)
411 (map-c-vector 'list #'identity params 'param length)
412 (let ((properties ()))
415 (when (eql (param-owner-type param) type)
416 (push param properties)))
417 params 'param length)
418 (nreverse properties))))
420 (defun query-object-class-properties (type &optional inherited-p)
421 (let* ((type-number (find-type-number type t))
422 (class (type-class-ref type-number)))
424 (multiple-value-bind (array length)
425 (%object-class-list-properties class)
426 (unless (null-pointer-p array)
428 (%map-params array length type-number inherited-p)
429 (deallocate-memory array))))
430 ; (type-class-unref type-number)
434 (defun default-slot-name (name)
435 (intern (substitute #\- #\_ (string-upcase (string-upcase name)))))
437 (defun default-slot-accessor (class-name slot-name type)
440 nil "~A-~A~A" class-name slot-name
441 (if (eq type 'boolean) "-P" ""))))
444 (defun slot-definition-from-property (class property &optional slot-name args)
445 (with-slots (name flags value-type documentation) property
446 (let* ((slot-name (or slot-name (default-slot-name name)))
447 (slot-type (or (getf args :type) (type-from-number value-type) 'pointer))
448 (accessor (default-slot-accessor class slot-name slot-type)))
451 :allocation :property :pname ,name
453 ,@(when (find :unbound args) (list :unbound (getf args :unbound)))
454 ,@(when (find :getter args) (list :getter (getf args :getter)))
455 ,@(when (find :setter args) (list :setter (getf args :setter)))
460 (member :writable flags) (member :readable flags)
461 (not (member :construct-only flags)))
462 (list :accessor accessor))
463 ((and (member :writable flags) (not (member :construct-only flags)))
464 (list :writer `(setf ,accessor)))
465 ((member :readable flags)
466 (list :reader accessor)))
468 ;; readable/writable/construct
469 ,@(when (or (not (member :writable flags))
470 (member :construct-only flags))
472 ,@(when (not (member :readable flags))
474 ,@(when (or (member :construct flags)
475 (member :construct-only flags))
479 ,@(if (find :initarg args)
480 (let ((initarg (getf args :initarg)))
483 (symbol `(:initarg ,initarg))))
484 (when (or (member :construct flags)
485 (member :construct-only flags)
486 (member :writable flags))
487 (list :initarg (intern (string slot-name) "KEYWORD"))))
490 :documentation ,documentation))))
493 (defun slot-definitions (class properties slots)
495 for property in properties
497 (find (param-name property) slots
498 :key #'(lambda (slot) (getf (rest slot) :pname))
500 (find (param-name property) slots
501 :key #'first :test #'string-equal))
504 (push (slot-definition-from-property class property) slots))
505 ((getf (rest slot) :merge)
508 (rest (slot-definition-from-property class property (first slot) (rest slot)))))))
509 (delete-if #'(lambda (slot) (getf (rest slot) :ignore)) slots))
512 (defun expand-gobject-type (type forward-p options &optional (metaclass 'gobject-class))
513 (let ((supers (cons (supertype type) (implements type)))
514 (class (type-from-number type))
515 (slots (getf options :slots)))
516 `(defclass ,class ,supers
518 (slot-definitions class (query-object-class-properties type) slots))
519 (:metaclass ,metaclass)
520 (:gtype ,(register-type-as type)))))
522 (defun gobject-dependencies (type)
527 (type-interfaces type)
528 (mapcar #'param-value-type (query-object-class-properties type))))))
531 (register-derivable-type 'gobject "GObject" 'expand-gobject-type 'gobject-dependencies)
534 ;;; Pseudo type for gobject instances which have their reference count
535 ;;; increased by the returning function
537 (defmethod alien-type ((type (eql 'referenced)) &rest args)
538 (declare (ignore type args))
539 (alien-type 'gobject))
541 (defmethod from-alien-form (form (type (eql 'referenced)) &rest args)
542 (declare (ignore type))
543 (destructuring-bind (type) args
544 (if (subtypep type 'gobject)
545 (let ((instance (make-symbol "INSTANCE")))
546 `(let ((,instance ,(from-alien-form form type)))
548 (%object-unref (foreign-location ,instance)))
550 (error "~A is not a subclass of GOBJECT" type))))