+(defun signal-name-to-string (name)
+ (substitute #\_ #\- (string-downcase (string name))))
+
+
+(defmethod direct-slot-definition-class ((class gobject-class) &rest initargs)
+ (case (getf initargs :allocation)
+ (:property (find-class 'direct-property-slot-definition))
+ (:user-data (find-class 'direct-user-data-slot-definition))
+ (t (call-next-method))))
+
+(defmethod effective-slot-definition-class ((class gobject-class) &rest initargs)
+ (case (getf initargs :allocation)
+ (:property (find-class 'effective-property-slot-definition))
+ (:user-data (find-class 'effective-user-data-slot-definition))
+ (t (call-next-method))))
+
+(defmethod compute-effective-slot-definition-initargs ((class gobject-class) direct-slotds)
+ (if (eq (slot-definition-allocation (first direct-slotds)) :property)
+ (nconc
+ (compute-most-specific-initargs direct-slotds
+ '(pname construct-only readable writable))
+ (call-next-method))
+ (call-next-method)))
+
+
+(defvar *ignore-setting-construct-only-property* nil)
+(declaim (special *ignore-setting-construct-only-property*))
+
+(defmethod compute-slot-reader-function ((slotd effective-property-slot-definition) &optional signal-unbound-p)
+ (declare (ignore signal-unbound-p))
+ (let* ((type (slot-definition-type slotd))
+ (pname (slot-definition-pname slotd))
+ (reader (reader-function type :ref :get)))
+ #'(lambda (object)
+ (with-memory (gvalue +gvalue-size+)
+ (%gvalue-init gvalue (find-type-number type))
+ (%object-get-property object pname gvalue)
+ (funcall reader gvalue +gvalue-value-offset+)))))
+
+(defmethod compute-slot-writer-function :around ((slotd effective-property-slot-definition))
+ (if (construct-only-property-p slotd)
+ #'(lambda (value object)
+ (declare (ignore value object))
+ (unless *ignore-setting-construct-only-property*
+ (error 'unwritable-slot :name (slot-definition-name slotd) :instance object)))
+ (call-next-method)))
+
+(defmethod compute-slot-writer-function ((slotd effective-property-slot-definition))
+ (let* ((type (slot-definition-type slotd))
+ (pname (slot-definition-pname slotd))
+ (writer (writer-function type :temp t))
+ (destroy (destroy-function type :temp t)))
+ #'(lambda (value object)
+ (with-memory (gvalue +gvalue-size+)
+ (%gvalue-init gvalue (find-type-number type))
+ (funcall writer value gvalue +gvalue-value-offset+)
+ (%object-set-property object pname gvalue)
+ (funcall destroy gvalue +gvalue-value-offset+))
+ value)))
+
+(defmethod slot-readable-p ((slotd effective-user-data-slot-definition))
+ (declare (ignore slotd))
+ t)
+
+(defmethod compute-slot-reader-function ((slotd effective-user-data-slot-definition) &optional signal-unbound-p)
+ (declare (ignore signal-unbound-p))
+ (let ((slot-name (slot-definition-name slotd)))
+ #'(lambda (object)
+ (user-data object slot-name))))
+
+(defmethod compute-slot-boundp-function ((slotd effective-user-data-slot-definition))
+ (let ((slot-name (slot-definition-name slotd)))
+ #'(lambda (object)
+ (user-data-p object slot-name))))
+
+(defmethod slot-writable-p ((slotd effective-user-data-slot-definition))
+ (declare (ignore slotd))
+ t)
+
+(defmethod compute-slot-writer-function ((slotd effective-user-data-slot-definition))
+ (let ((slot-name (slot-definition-name slotd)))
+ #'(lambda (value object)
+ (setf (user-data object slot-name) value))))
+
+(defmethod compute-slot-makunbound-function ((slotd effective-user-data-slot-definition))
+ (let ((slot-name (slot-definition-name slotd)))
+ #'(lambda (object)
+ (unset-user-data object slot-name))))
+
+(defmethod compute-slots :around ((class gobject-class))
+ (let ((slots (call-next-method)))
+ (when (some #'(lambda (slotd)
+ (and
+ (eq (slot-definition-allocation slotd) :instance)
+ (not (typep slotd 'effective-special-slot-definition))))
+ slots)
+ (setf (slot-value class 'instance-slots-p) t))
+ slots))
+
+
+;;;; Super class for all classes in the GObject type hierarchy
+
+(eval-when (:compile-toplevel :load-toplevel :execute)
+ (defclass gobject (ginstance)
+ (#+debug-ref-counting
+ (ref-count :allocation :alien :type int :reader ref-count))
+ (:metaclass gobject-class)
+ (:gtype "GObject")))
+
+#+debug-ref-counting
+(defmethod print-object ((instance gobject) stream)
+ (print-unreadable-object (instance stream :type t :identity nil)
+ (if (proxy-valid-p instance)
+ (format stream "at 0x~X (~D)" (pointer-address (foreign-location instance)) (ref-count instance))
+ (write-string "at \"unbound\"" stream))))
+
+
+(define-type-method reader-function ((type gobject) &key (ref :read) inlined)
+ (assert-not-inlined type inlined)
+ (ecase ref
+ ((:read :peek) (call-next-method type :ref :read))
+ (:get
+ #'(lambda (location &optional (offset 0))
+ (let ((instance (ref-pointer location offset)))
+ (unless (null-pointer-p instance)
+ (multiple-value-bind (gobject new-p)
+ (ensure-proxy-instance 'gobject instance :reference nil)
+ (unless new-p
+ (%object-unref instance))
+ (setf (ref-pointer location offset) (make-pointer 0))
+ gobject)))))))
+
+(define-type-method callback-wrapper ((type gobject) var arg form)
+ (let ((class (type-expand type)))
+ `(let ((,var (ensure-proxy-instance ',class ,arg)))
+ ,form)))
+
+(defun initial-add (object function initargs key pkey)
+ (loop
+ as (initarg value . rest) = initargs then rest
+ do (cond
+ ((eq initarg key) (funcall function object value))
+ ((eq initarg pkey) (mapc #'(lambda (value)
+ (funcall function object value))
+ value)))
+ while rest))
+
+(defun initial-apply-add (object function initargs key pkey)
+ (initial-add object #'(lambda (object value)
+ (apply function object (mklist value)))
+ initargs key pkey))
+
+
+(defmethod make-proxy-instance ((class gobject-class) location &rest initargs)
+ (declare (ignore location initargs))
+ (if (slot-value class 'instance-slots-p)
+ (error "Objects of class ~A has instance slots and should only be created with MAKE-INSTANCE" class)
+ (call-next-method)))
+
+
+(defmethod allocate-foreign ((object gobject) &rest initargs)
+ (let ((init-slots ()))
+ (flet ((value-from-initargs (slotd)
+ (loop
+ with slot-initargs = (slot-definition-initargs slotd)
+ for (initarg value) on initargs by #'cddr
+ when (find initarg slot-initargs)
+ do (return (values value t)))))
+
+ (loop
+ for slotd in (class-slots (class-of object))
+ when (and
+ (eq (slot-definition-allocation slotd) :property)
+ (construct-only-property-p slotd))
+ do (multiple-value-bind (value initarg-p) (value-from-initargs slotd)
+ (cond
+ (initarg-p (push (cons slotd value) init-slots))
+ ((slot-definition-initfunction slotd)
+ (push
+ (cons slotd (funcall (slot-definition-initfunction slotd)))
+ init-slots))))))
+
+ (cond
+ (init-slots
+ (let* ((pointer-size (size-of 'pointer))
+ (element-size (+ +gvalue-size+ pointer-size))
+ (num-slots (length init-slots)))
+ (with-memory (params (* num-slots element-size))
+ (loop
+ with string-writer = (writer-function 'string)
+ for (slotd . value) in init-slots
+ as param = params then (pointer+ param element-size)
+ as type = (slot-definition-type slotd)
+ as pname = (slot-definition-pname slotd)
+ do (funcall string-writer pname param)
+ (gvalue-init (pointer+ param pointer-size) type value))
+
+ (unwind-protect
+ (%gobject-newv (type-number-of object) num-slots params)
+
+ (loop
+ with string-destroy = (destroy-function 'string)
+ repeat num-slots
+ as param = params then (pointer+ param element-size)
+ do (funcall string-destroy param)
+ (gvalue-unset (pointer+ param pointer-size)))))))
+
+ (t (%gobject-new (type-number-of object))))))
+
+
+(defmethod shared-initialize ((object gobject) names &rest initargs)
+ (declare (ignore names initargs))
+ (let ((*ignore-setting-construct-only-property* t))
+ (call-next-method)))
+
+(defmethod initialize-instance :around ((object gobject) &rest initargs)
+ (declare (ignore initargs))
+ (prog1
+ (call-next-method)
+ #+debug-ref-counting(%object-weak-ref (foreign-location object))
+ #?(pkg-exists-p "glib-2.0" :atleast-version "2.8.0")
+ (when (slot-value (class-of object) 'instance-slots-p)
+ (%object-add-toggle-ref (foreign-location object))
+ (%object-unref (foreign-location object)))))
+
+
+(defmethod instance-finalizer ((instance gobject))
+ (let ((location (foreign-location instance)))
+ #?(pkg-exists-p "glib-2.0" :atleast-version "2.8.0")
+ (if (slot-value (class-of instance) 'instance-slots-p)
+ #'(lambda ()
+ #+debug-ref-counting
+ (format t "Finalizing proxy for 0x~8,'0X~%" (pointer-address location))
+ (%object-remove-toggle-ref location))
+ #'(lambda ()
+ #+debug-ref-counting
+ (format t "Finalizing proxy for 0x~8,'0X~%" (pointer-address location))
+ (%object-unref location)))
+ #?-(pkg-exists-p "glib-2.0" :atleast-version "2.8.0")
+ #'(lambda ()
+ (%object-unref location))))
+
+
+(defbinding (%gobject-new "g_object_new") () pointer
+ (type type-number)
+ (nil null))
+
+(defbinding (%gobject-newv "g_object_newv") () pointer
+ (type type-number)
+ (n-parameters unsigned-int)
+ (params pointer))