(defpackage :glib
(:use :cl :cffi :iter)
- (:export #:gsize
+ (:export #:at-init
+ #:gsize
#:gssize
#:goffset
#:*glib-major-version*
(in-package :glib)
(eval-when (:compile-toplevel :load-toplevel :execute)
+ (defvar *initializers* nil)
+ (defun register-initializer (fn)
+ (setf *initializers* (nconc *initializers* (list fn)))))
+
+(defun run-initializers ()
+ (iter (for fn in *initializers*)
+ (funcall fn)))
+
+(defmacro at-init (&body body)
+ `(progn (register-initializer (lambda () ,@body))
+ ,@body))
+
+(eval-when (:compile-toplevel :load-toplevel :execute)
(define-foreign-library glib
(:unix (:or "libglib-2.0.so.0" "libglib-2.0.so"))
(t "libglib-2.0")))
(defvar *threads-initialized-p* nil)
-(unless *threads-initialized-p*
- (g-thread-init (null-pointer))
- (setf *threads-initialized-p* t))
+(at-init
+ (unless *threads-initialized-p*
+ (g-thread-init (null-pointer))
+ (setf *threads-initialized-p* t)))
(defcenum g-thread-priority
:g-thread-priority-low
(setf parent (g-type-name (ensure-g-type parent))))
`(progn
(setf (gethash ,name *registered-types*) (make-object-type :name ,name :class ',class :parent ,parent :interfaces ',interfaces :properties ',properties))
- (with-foreign-object (query 'g-type-query)
- (g-type-query (g-type-from-name ,parent) query)
- (with-foreign-slots ((class-size instance-size) query g-type-query)
- (g-type-register-static-simple (g-type-from-name ,parent) ,name class-size (callback c-class-init) instance-size (callback c-instance-init) nil)))
- (add-interfaces ,name)
+ (at-init
+ (format t "Registering GObject type implementation ~A for type ~A~%" ',class ,name)
+ (with-foreign-object (query 'g-type-query)
+ (g-type-query (g-type-from-name ,parent) query)
+ (with-foreign-slots ((class-size instance-size) query g-type-query)
+ (g-type-register-static-simple (g-type-from-name ,parent) ,name class-size (callback c-class-init) instance-size (callback c-instance-init) nil)))
+ (add-interfaces ,name))
(defmethod initialize-instance :before ((object ,class) &key pointer)
(unless (or pointer (and (slot-boundp object 'gobject::pointer)
(gobject::pointer object)))
(defmethod initialize-instance :after ((object gobject-class) &key &allow-other-keys)
(register-object-type (gobject-class-g-type-name object) (class-name object))
- (initialize-gobject-class-g-type object))
+ (at-init (initialize-gobject-class-g-type object)))
(defclass gobject-direct-slot-definition (standard-direct-slot-definition)
((g-property-type :initform nil
(defcfun (%g-type-init "g_type_init") :void)
-(%g-type-init)
+(at-init (%g-type-init))
(defcfun (g-type-name "g_type_name") :string
(type g-type))
(progn
(defun glib-stop-thread ()
(setf *threads-initialized-p* nil))
- (defun glib-start-thread ()
- (g-thread-init (null-pointer))
- (setf *threads-initialized-p* t))
- (pushnew 'glib-stop-thread sb-ext:*save-hooks*)
- (pushnew 'glib-start-thread sb-ext:*init-hooks*))
+ (pushnew 'glib-stop-thread sb-ext:*save-hooks*))
(defun map-inherited-classes (class fn)
(when (symbolp class) (setf class (find-class class)))
(iter (for subclass in (closer-mop:class-direct-subclasses class))
(map-inherited-classes subclass fn))))
-(defun initialize-all-gobject-types ()
- (map-inherited-classes 'gobject::g-object
- (lambda (class)
- (when (typep class 'gobject::gobject-class)
- (gobject::initialize-gobject-class-g-type class)))))
-
-(pushnew 'initialize-all-gobject-types sb-ext:*init-hooks*)
+(pushnew 'run-initializers sb-ext:*init-hooks*)