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.47 2006/02/15 09:45:41 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 :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)))
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)))
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 (eq (slot-definition-allocation (first direct-slotds)) :property)
133 (list :pname (signal-name-to-string
134 (most-specific-slot-value direct-slotds 'pname
135 (slot-definition-name (first direct-slotds))))
136 :readable (most-specific-slot-value direct-slotds 'readable t)
137 :writable (most-specific-slot-value direct-slotds 'writable t)
138 :construct-only (most-specific-slot-value direct-slotds
139 'construct-only nil))
144 (defvar *ignore-setting-construct-only-property* nil)
145 (declaim (special *ignore-setting-construct-only-property*))
147 (defmethod initialize-internal-slot-functions ((slotd effective-property-slot-definition))
148 (let ((type (slot-definition-type slotd))
149 (pname (slot-definition-pname slotd)))
150 (when (and (not (slot-boundp slotd 'getter)) (slot-readable-p slotd))
152 (slot-value slotd 'getter)
156 (setq reader (reader-function type)))
157 (let ((gvalue (gvalue-new type)))
158 (%object-get-property object pname gvalue)
160 (funcall reader gvalue +gvalue-value-offset+)
161 (gvalue-free gvalue t)))))))
163 (when (not (slot-boundp slotd 'setter))
165 ((slot-writable-p slotd)
167 (slot-value slotd 'setter)
169 #'(lambda (value object)
171 (setq writer (writer-function type)))
172 (let ((gvalue (gvalue-new type)))
173 (funcall writer value gvalue +gvalue-value-offset+)
174 (%object-set-property object pname gvalue)
175 (gvalue-free gvalue t)
178 ((construct-only-property-p slotd)
180 (slot-value slotd 'setter)
181 #'(lambda (value object)
182 (declare (ignore value object))
183 (unless *ignore-setting-construct-only-property*
184 (error "Slot is not writable: ~A" (slot-definition-name slotd)))))))))
188 (defmethod initialize-internal-slot-functions ((slotd effective-user-data-slot-definition))
189 (let ((slot-name (slot-definition-name slotd)))
190 (unless (slot-boundp slotd 'getter)
192 (slot-value slotd 'getter)
194 (prog1 (user-data object slot-name)))))
195 (unless (slot-boundp slotd 'setter)
197 (slot-value slotd 'setter)
198 #'(lambda (value object)
199 (setf (user-data object slot-name) value))))
200 (unless (slot-boundp slotd 'boundp)
202 (slot-value slotd 'boundp)
204 (user-data-p object slot-name)))))
207 (defmethod shared-initialize :after ((class gobject-class) names &rest initargs)
208 (declare (ignore initargs))
209 (when (some #'(lambda (slotd)
211 (eq (slot-definition-allocation slotd) :instance)
212 (not (typep slotd 'effective-special-slot-definition))))
214 (setf (slot-value class 'instance-slots-p) t)))
218 ;;;; Super class for all classes in the GObject type hierarchy
220 (eval-when (:compile-toplevel :load-toplevel :execute)
221 (defclass gobject (ginstance)
222 (#+debug-ref-counting
223 (ref-count :allocation :alien :type int :reader ref-count))
224 (:metaclass gobject-class)
228 (defmethod print-object ((instance gobject) stream)
229 (print-unreadable-object (instance stream :type t :identity nil)
230 (if (proxy-valid-p instance)
231 (format stream "at 0x~X (~D)" (sap-int (foreign-location instance)) (ref-count instance))
232 (write-string "at \"unbound\"" stream))))
235 (defun initial-add (object function initargs key pkey)
237 as (initarg value . rest) = initargs then rest
239 ((eq initarg key) (funcall function object value))
240 ((eq initarg pkey) (mapc #'(lambda (value)
241 (funcall function object value))
245 (defun initial-apply-add (object function initargs key pkey)
246 (initial-add object #'(lambda (object value)
247 (apply function object (mklist value)))
251 (defmethod make-proxy-instance ((class gobject-class) location &rest initargs)
252 (declare (ignore location initargs))
253 (if (slot-value class 'instance-slots-p)
254 (error "An object of class ~A has instance slots and should only be created with MAKE-INSTANCE" class)
258 (defmethod allocate-foreign ((object gobject) &rest initargs)
259 (let ((init-slots ()))
260 (flet ((value-from-initargs (slotd)
262 with slot-initargs = (slot-definition-initargs slotd)
263 for (initarg value) on initargs by #'cddr
264 when (find initarg slot-initargs)
265 do (return (values value t)))))
268 for slotd in (class-slots (class-of object))
270 (eq (slot-definition-allocation slotd) :property)
271 (construct-only-property-p slotd))
272 do (multiple-value-bind (value initarg-p) (value-from-initargs slotd)
274 (initarg-p (push (cons slotd value) init-slots))
275 ((slot-definition-initfunction slotd)
277 (cons slotd (funcall (slot-definition-initfunction slotd)))
282 (let ((element-size (+ +gvalue-size+ +size-of-pointer+))
283 (num-slots (length init-slots)))
284 (with-allocated-memory (params (* num-slots element-size))
286 with string-writer = (writer-function 'string)
287 for (slotd . value) in init-slots
288 as offset = params then (sap+ offset element-size)
289 as type = (slot-definition-type slotd)
290 as pname = (slot-definition-pname slotd)
291 do (funcall string-writer pname offset)
292 (gvalue-init (sap+ offset +size-of-pointer+) type value))
295 (%gobject-newv (type-number-of object) num-slots params)
298 with string-destroy = (destroy-function 'string)
300 as offset = params then (sap+ offset element-size)
301 do (funcall string-destroy offset)
302 (gvalue-unset (sap+ offset +size-of-pointer+)))))))
304 (t (%gobject-new (type-number-of object))))))
307 (defmethod shared-initialize ((object gobject) names &rest initargs)
308 (declare (ignore names initargs))
309 (let ((*ignore-setting-construct-only-property* t))
312 (defmethod initialize-instance :around ((object gobject) &rest initargs)
313 (declare (ignore initargs))
316 #+debug-ref-counting(%object-weak-ref (foreign-location object))
318 (when (slot-value (class-of object) 'instance-slots-p)
319 (with-slots (location) object
320 (%object-add-toggle-ref location)
321 (%object-unref location)))))
324 (defmethod instance-finalizer ((instance gobject))
325 (let ((location (foreign-location instance)))
327 (if (slot-value (class-of instance) 'instance-slots-p)
330 (format t "Finalizing proxy for 0x~8,'0X~%" (sap-int location))
331 (remove-cached-instance location)
332 (%object-remove-toggle-ref location))
335 (format t "Finalizing proxy for 0x~8,'0X~%" (sap-int location))
336 (remove-cached-instance location)
337 (%object-unref location)))
340 (remove-cached-instance location)
341 (%object-unref location))))
344 (defbinding (%gobject-new "g_object_new") () pointer
348 (defbinding (%gobject-newv "g_object_newv") () pointer
350 (n-parameters unsigned-int)
357 (defbinding %object-set-property () nil
362 (defbinding %object-get-property () nil
367 (defbinding %object-notify () nil
371 (defbinding object-freeze-notify () nil
374 (defbinding object-thaw-notify () nil
380 (defbinding %object-set-qdata-full () nil
384 (destroy-marshal pointer))
386 (defcallback user-data-destroy-func (nil (id unsigned-int))
387 (destroy-user-data id))
389 (export 'user-data-destroy-func)
391 (defun (setf user-data) (data object key)
392 (%object-set-qdata-full object (quark-intern key)
393 (register-user-data data) (callback user-data-destroy-func))
397 (defun (setf object-data) (data object key &key (test #'eq))
398 (assert (eq test #'eq))
399 (setf (user-data object key) data))
401 (defbinding %object-get-qdata () unsigned-long
405 (defun user-data (object key)
406 (find-user-data (%object-get-qdata object (quark-intern key))))
409 (defun object-data (object key &key (test #'eq))
410 (assert (eq test #'eq))
411 (user-data object key))
413 (defun user-data-p (object key)
414 (user-data-exists-p (%object-get-qdata object (quark-intern key))))
416 (defbinding %object-steal-qdata () unsigned-long
420 (defun unset-user-data (object key)
421 (destroy-user-data (%object-steal-qdata object (quark-intern key))))
426 (defbinding %object-class-list-properties () pointer
428 (n-properties unsigned-int :out))
431 (defun %map-params (params length type inherited-p)
433 (map-c-vector 'list #'identity params 'param length)
434 (let ((properties ()))
437 (when (eql (param-owner-type param) type)
438 (push param properties)))
439 params 'param length)
440 (nreverse properties))))
442 (defun query-object-class-properties (type &optional inherited-p)
443 (let* ((type-number (find-type-number type t))
444 (class (type-class-ref type-number)))
446 (multiple-value-bind (array length)
447 (%object-class-list-properties class)
448 (unless (null-pointer-p array)
450 (%map-params array length type-number inherited-p)
451 (deallocate-memory array))))
452 ; (type-class-unref type-number)
456 (defun default-slot-name (name)
457 (intern (substitute #\- #\_ (string-upcase (string-upcase name)))))
459 (defun default-slot-accessor (class-name slot-name type)
462 nil "~A-~A~A" class-name slot-name
463 (if (eq type 'boolean) "-P" ""))))
466 (defun slot-definition-from-property (class property &optional slot-name args)
467 (with-slots (name flags value-type documentation) property
468 (let* ((slot-name (or slot-name (default-slot-name name)))
469 (slot-type (or (getf args :type) (type-from-number value-type) 'pointer))
470 (accessor (default-slot-accessor class slot-name slot-type)))
473 :allocation :property :pname ,name
475 ,@(when (find :unbound args) (list :unbound (getf args :unbound)))
476 ,@(when (find :getter args) (list :getter (getf args :getter)))
477 ,@(when (find :setter args) (list :setter (getf args :setter)))
482 (member :writable flags) (member :readable flags)
483 (not (member :construct-only flags)))
484 (list :accessor accessor))
485 ((and (member :writable flags) (not (member :construct-only flags)))
486 (list :writer `(setf ,accessor)))
487 ((member :readable flags)
488 (list :reader accessor)))
490 ;; readable/writable/construct
491 ,@(when (or (not (member :writable flags))
492 (member :construct-only flags))
494 ,@(when (not (member :readable flags))
496 ,@(when (member :construct-only flags)
497 '(:construct-only t))
500 ,@(if (find :initarg args)
501 (let ((initarg (getf args :initarg)))
504 (symbol `(:initarg ,initarg))))
505 (when (or (member :construct flags)
506 (member :construct-only flags)
507 (member :writable flags))
508 (list :initarg (intern (string slot-name) "KEYWORD"))))
511 :documentation ,documentation))))
514 (defun slot-definitions (class properties slots)
516 for property in properties
518 (find (param-name property) slots
519 :key #'(lambda (slot) (getf (rest slot) :pname))
521 (find (param-name property) slots
522 :key #'first :test #'string-equal))
525 (push (slot-definition-from-property class property) slots))
526 ((getf (rest slot) :merge)
529 (rest (slot-definition-from-property class property (first slot) (rest slot)))))))
530 (delete-if #'(lambda (slot) (getf (rest slot) :ignore)) slots))
533 (defun expand-gobject-type (type forward-p options &optional (metaclass 'gobject-class))
534 (let ((supers (cons (supertype type) (implements type)))
535 (class (type-from-number type))
536 (slots (getf options :slots)))
537 `(defclass ,class ,supers
539 (slot-definitions class (query-object-class-properties type) slots))
540 (:metaclass ,metaclass)
541 (:gtype ,(register-type-as type)))))
543 (defun gobject-dependencies (type)
548 (type-interfaces type)
549 (mapcar #'param-value-type (query-object-class-properties type))))))
552 (register-derivable-type 'gobject "GObject" 'expand-gobject-type 'gobject-dependencies)
555 ;;; Pseudo type for gobject instances which have their reference count
556 ;;; increased by the returning function
558 (defmethod alien-type ((type (eql 'referenced)) &rest args)
559 (declare (ignore type args))
560 (alien-type 'gobject))
562 (defmethod from-alien-form (form (type (eql 'referenced)) &rest args)
563 (declare (ignore type))
564 (destructuring-bind (type) args
565 (if (subtypep type 'gobject)
566 (let ((instance (make-symbol "INSTANCE")))
567 `(let ((,instance ,(from-alien-form form type)))
569 (%object-unref (foreign-location ,instance)))
571 (error "~A is not a subclass of GOBJECT" type))))