| 1 | ;; Common Lisp bindings for GTK+ v2.x |
| 2 | ;; Copyright 2000-2005 Espen S. Johnsen <espen@users.sf.net> |
| 3 | ;; |
| 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: |
| 11 | ;; |
| 12 | ;; The above copyright notice and this permission notice shall be |
| 13 | ;; included in all copies or substantial portions of the Software. |
| 14 | ;; |
| 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. |
| 22 | |
| 23 | ;; $Id: gtype.lisp,v 1.48 2006/02/26 16:12:25 espen Exp $ |
| 24 | |
| 25 | (in-package "GLIB") |
| 26 | |
| 27 | (use-prefix "g") |
| 28 | |
| 29 | ;; Initialize the glib type system |
| 30 | (defbinding type-init () nil) |
| 31 | (type-init) |
| 32 | |
| 33 | (deftype type-number () 'unsigned-long) |
| 34 | |
| 35 | (deftype gtype () 'symbol) |
| 36 | |
| 37 | (define-type-method alien-type ((type gtype)) |
| 38 | (declare (ignore type)) |
| 39 | (alien-type 'type-number)) |
| 40 | |
| 41 | (define-type-method size-of ((type gtype)) |
| 42 | (declare (ignore type)) |
| 43 | (size-of 'type-number)) |
| 44 | |
| 45 | (define-type-method to-alien-form ((type gtype) gtype) |
| 46 | (declare (ignore type)) |
| 47 | `(find-type-number ,gtype t)) |
| 48 | |
| 49 | (define-type-method to-alien-function ((type gtype)) |
| 50 | (declare (ignore type)) |
| 51 | #'(lambda (gtype) |
| 52 | (find-type-number gtype t))) |
| 53 | |
| 54 | (define-type-method from-alien-form ((type gtype) type-number) |
| 55 | (declare (ignore type)) |
| 56 | `(type-from-number ,type-number)) |
| 57 | |
| 58 | (define-type-method from-alien-function ((type gtype)) |
| 59 | (declare (ignore type)) |
| 60 | #'(lambda (type-number) |
| 61 | (type-from-number type-number))) |
| 62 | |
| 63 | (define-type-method writer-function ((type gtype)) |
| 64 | (declare (ignore type)) |
| 65 | (let ((writer (writer-function 'type-number))) |
| 66 | #'(lambda (gtype location &optional (offset 0)) |
| 67 | (funcall writer (find-type-number gtype t) location offset)))) |
| 68 | |
| 69 | (define-type-method reader-function ((type gtype)) |
| 70 | (declare (ignore type)) |
| 71 | (let ((reader (reader-function 'type-number))) |
| 72 | #'(lambda (location &optional (offset 0) weak-p) |
| 73 | (declare (ignore weak-p)) |
| 74 | (type-from-number (funcall reader location offset))))) |
| 75 | |
| 76 | |
| 77 | (eval-when (:compile-toplevel :load-toplevel :execute) |
| 78 | (defclass type-query (struct) |
| 79 | ((type-number :allocation :alien :type type-number) |
| 80 | (name :allocation :alien :type string) |
| 81 | (class-size :allocation :alien :type unsigned-int) |
| 82 | (instance-size :allocation :alien :type unsigned-int)) |
| 83 | (:metaclass struct-class))) |
| 84 | |
| 85 | |
| 86 | (defbinding type-query (type) nil |
| 87 | ((find-type-number type t) type-number) |
| 88 | ((make-instance 'type-query) type-query :return)) |
| 89 | |
| 90 | (defun type-instance-size (type) |
| 91 | (slot-value (type-query type) 'instance-size)) |
| 92 | |
| 93 | (defun type-class-size (type) |
| 94 | (slot-value (type-query type) 'class-size)) |
| 95 | |
| 96 | (defbinding type-class-ref (type) pointer |
| 97 | ((find-type-number type t) type-number)) |
| 98 | |
| 99 | (defbinding type-class-unref (type) nil |
| 100 | ((find-type-number type t) type-number)) |
| 101 | |
| 102 | (defbinding type-class-peek (type) pointer |
| 103 | ((find-type-number type t) type-number)) |
| 104 | |
| 105 | |
| 106 | ;;;; Mapping between lisp types and glib types |
| 107 | |
| 108 | (defvar *registered-types* ()) |
| 109 | (defvar *registered-type-aliases* ()) |
| 110 | (defvar *registered-static-types* ()) |
| 111 | (defvar *lisp-type-to-type-number* (make-hash-table)) |
| 112 | (defvar *type-number-to-lisp-type* (make-hash-table)) |
| 113 | |
| 114 | (defbinding %type-from-name () type-number |
| 115 | (name string)) |
| 116 | |
| 117 | (defun type-number-from-glib-name (name &optional (error-p t)) |
| 118 | (let ((type-number (%type-from-name name))) |
| 119 | (cond |
| 120 | ((not (zerop type-number)) type-number) |
| 121 | (error-p (error "Invalid gtype name: ~A" name))))) |
| 122 | |
| 123 | (defun register-type (type id) |
| 124 | (pushnew (cons type id) *registered-types* :key #'car) |
| 125 | (let ((type-number |
| 126 | (typecase id |
| 127 | (string (type-number-from-glib-name id)) |
| 128 | (symbol (funcall id))))) |
| 129 | (setf (gethash type *lisp-type-to-type-number*) type-number) |
| 130 | (setf (gethash type-number *type-number-to-lisp-type*) type) |
| 131 | type-number)) |
| 132 | |
| 133 | (defun register-type-alias (type alias) |
| 134 | (pushnew (cons type alias) *registered-type-aliases* :key #'car) |
| 135 | (setf |
| 136 | (gethash type *lisp-type-to-type-number*) |
| 137 | (find-type-number alias t))) |
| 138 | |
| 139 | (defun reinitialize-all-types () |
| 140 | (clrhash *lisp-type-to-type-number*) |
| 141 | (clrhash *type-number-to-lisp-type*) |
| 142 | (type-init) ; initialize the glib type system |
| 143 | (mapc #'(lambda (type) |
| 144 | (register-type (car type) (cdr type))) |
| 145 | *registered-types*) |
| 146 | (mapc #'(lambda (type) |
| 147 | (register-type-alias (car type) (cdr type))) |
| 148 | *registered-type-aliases*)) |
| 149 | |
| 150 | (pushnew 'reinitialize-all-types |
| 151 | #+cmu *after-save-initializations* |
| 152 | #+sbcl *init-hooks*) |
| 153 | |
| 154 | #+cmu |
| 155 | (pushnew 'system::reinitialize-global-table ; we shouldn't have to do this? |
| 156 | *after-save-initializations*) |
| 157 | |
| 158 | |
| 159 | (defun find-type-number (type &optional error-p) |
| 160 | (etypecase type |
| 161 | (integer type) |
| 162 | (string (type-number-from-glib-name type error-p)) |
| 163 | (symbol |
| 164 | (or |
| 165 | (gethash type *lisp-type-to-type-number*) |
| 166 | (and error-p (error "Type not registered: ~A" type)))) |
| 167 | (class (find-type-number (class-name type) error-p)))) |
| 168 | |
| 169 | (defun type-from-number (type-number &optional error) |
| 170 | (multiple-value-bind (type found) |
| 171 | (gethash type-number *type-number-to-lisp-type*) |
| 172 | (if found |
| 173 | type |
| 174 | (let ((name (find-foreign-type-name type-number))) |
| 175 | (cond |
| 176 | ((and name (not (= (type-number-from-glib-name name nil) type-number))) |
| 177 | ;; This is a hack because GdkEvent seems to be registered |
| 178 | ;; multiple times |
| 179 | (type-from-number (type-number-from-glib-name name))) |
| 180 | ((and error name) |
| 181 | (error "Type number not registered: ~A (~A)" type-number name)) |
| 182 | ((and error) |
| 183 | (error "Invalid type number: ~A" type-number))))))) |
| 184 | |
| 185 | (defbinding (find-foreign-type-name "g_type_name") (type) (copy-of string) |
| 186 | ((find-type-number type t) type-number)) |
| 187 | |
| 188 | (defun type-number-of (object) |
| 189 | (find-type-number (type-of object) t)) |
| 190 | |
| 191 | (eval-when (:compile-toplevel :load-toplevel :execute) |
| 192 | (defvar *type-initializers* ()) |
| 193 | (defun %find-types-in-library (pathname prefixes ignore) |
| 194 | (let ((process (run-program |
| 195 | "/usr/bin/nm" (list "--defined-only" "-D" (namestring (truename pathname))) |
| 196 | :output :stream :wait nil))) |
| 197 | (unwind-protect |
| 198 | (loop |
| 199 | as symbol = (let ((line (read-line (process-output process) nil))) |
| 200 | (when line |
| 201 | (subseq line (1+ (position #\Space line :from-end t))))) |
| 202 | while symbol |
| 203 | when (and |
| 204 | (> (length symbol) 9) |
| 205 | (or |
| 206 | (not prefixes) |
| 207 | (some #'(lambda (prefix) |
| 208 | (and |
| 209 | (> (length symbol) (length prefix)) |
| 210 | (string= prefix symbol :end2 (length prefix)))) |
| 211 | (mklist prefixes))) |
| 212 | (string= "_get_type" symbol :start2 (- (length symbol) 9)) |
| 213 | (not (member symbol ignore :test #'string=))) |
| 214 | collect symbol) |
| 215 | (process-close process))))) |
| 216 | |
| 217 | |
| 218 | (defmacro init-types-in-library (filename &key prefix ignore) |
| 219 | (let ((names (%find-types-in-library filename prefix ignore))) |
| 220 | `(progn |
| 221 | ,@(mapcar #'(lambda (name) |
| 222 | `(progn |
| 223 | (defbinding (,(intern name) ,name) () type-number) |
| 224 | (,(intern name)) |
| 225 | (pushnew ',(intern name) *type-initializers*))) |
| 226 | names)))) |
| 227 | |
| 228 | (defun find-type-init-function (type-number) |
| 229 | (loop |
| 230 | for type-init in *type-initializers* |
| 231 | when (= type-number (funcall type-init)) |
| 232 | do (return type-init))) |
| 233 | |
| 234 | (defun register-type-as (type-number) |
| 235 | (or |
| 236 | (find-type-init-function type-number) |
| 237 | (find-foreign-type-name type-number) |
| 238 | (error "Unknown type-number: ~A" type-number))) |
| 239 | |
| 240 | (defun default-type-init-name (type) |
| 241 | (find-symbol (format nil "~A_~A_get_type" |
| 242 | (package-prefix *package*) |
| 243 | (substitute #\_ #\- (string-downcase type))))) |
| 244 | |
| 245 | |
| 246 | (eval-when (:compile-toplevel :load-toplevel :execute) |
| 247 | (defclass type-info (struct) |
| 248 | ((class-size :allocation :alien :type (unsigned 16) :initarg :class-size) |
| 249 | (base-init :allocation :alien :type pointer) |
| 250 | (base-finalize :allocation :alien :type pointer) |
| 251 | (class-init :allocation :alien :type pointer) |
| 252 | (class-finalize :allocation :alien :type pointer) |
| 253 | (class-data :allocation :alien :type pointer) |
| 254 | (instance-size :allocation :alien :type (unsigned 16) |
| 255 | :initarg :instance-size) |
| 256 | (n-preallocs :allocation :alien :type (unsigned 16)) |
| 257 | (instance-init :allocation :alien :type pointer) |
| 258 | (value-table :allocation :alien :type pointer)) |
| 259 | (:metaclass struct-class))) |
| 260 | |
| 261 | (defbinding %type-register-static () type-number |
| 262 | (parent-type type-number) |
| 263 | (name string) |
| 264 | (info type-info) |
| 265 | (0 unsigned-int)) |
| 266 | |
| 267 | (defun register-new-type (type parent &optional foreign-name) |
| 268 | (let ((parent-info (type-query parent))) |
| 269 | (with-slots ((parent-number type-number) class-size instance-size) parent-info |
| 270 | (let ((type-number |
| 271 | (%type-register-static |
| 272 | parent-number |
| 273 | (or foreign-name (default-alien-type-name type)) |
| 274 | (make-instance 'type-info :class-size class-size :instance-size instance-size)))) |
| 275 | (pushnew (list type parent foreign-name) *registered-static-types* :key #'car) |
| 276 | (setf (gethash type *lisp-type-to-type-number*) type-number) |
| 277 | (setf (gethash type-number *type-number-to-lisp-type*) type) |
| 278 | type-number)))) |
| 279 | |
| 280 | |
| 281 | |
| 282 | ;;;; Metaclass for subclasses of ginstance |
| 283 | |
| 284 | (eval-when (:compile-toplevel :load-toplevel :execute) |
| 285 | (defclass ginstance-class (proxy-class) |
| 286 | ((gtype :initarg :gtype :initform nil :reader ginstance-class-gtype)))) |
| 287 | |
| 288 | |
| 289 | (defun update-size (class) |
| 290 | (let ((type-number (find-type-number class))) |
| 291 | (cond |
| 292 | ((not (slot-boundp class 'size)) |
| 293 | (setf (slot-value class 'size) (type-instance-size type-number))) |
| 294 | ((and |
| 295 | (slot-boundp class 'size) |
| 296 | (not (= (type-instance-size type-number) (slot-value class 'size)))) |
| 297 | (warn "Size mismatch for class ~A" class))))) |
| 298 | |
| 299 | |
| 300 | (defmethod finalize-inheritance ((class ginstance-class)) |
| 301 | (call-next-method) |
| 302 | (let* ((class-name (class-name class)) |
| 303 | (super (most-specific-proxy-superclass class)) |
| 304 | (gtype (or |
| 305 | (first (ginstance-class-gtype class)) |
| 306 | (default-alien-type-name class-name))) |
| 307 | (type-number |
| 308 | (or |
| 309 | (find-type-number class-name) |
| 310 | (let ((type-number |
| 311 | (if (or |
| 312 | (symbolp gtype) |
| 313 | (type-number-from-glib-name gtype nil)) |
| 314 | (register-type class-name gtype) |
| 315 | (register-new-type class-name (class-name super) gtype)))) |
| 316 | (type-class-ref type-number) |
| 317 | type-number)))) |
| 318 | (when (and |
| 319 | (supertype type-number) |
| 320 | (not (eq (class-name super) (supertype type-number)))) |
| 321 | (warn "Super class mismatch between CLOS and GObject for ~A" |
| 322 | class-name))) |
| 323 | |
| 324 | (update-size class)) |
| 325 | |
| 326 | |
| 327 | (defmethod shared-initialize ((class ginstance-class) names &rest initargs) |
| 328 | (declare (ignore initargs)) |
| 329 | (call-next-method) |
| 330 | (when (class-finalized-p class) |
| 331 | (update-size class))) |
| 332 | |
| 333 | |
| 334 | (defmethod validate-superclass ((class ginstance-class) (super standard-class)) |
| 335 | (subtypep (class-name super) 'ginstance)) |
| 336 | |
| 337 | |
| 338 | ;;;; Superclass for wrapping types in the glib type system |
| 339 | |
| 340 | (eval-when (:compile-toplevel :load-toplevel :execute) |
| 341 | (defclass ginstance (proxy) |
| 342 | (;(class :allocation :alien :type pointer :offset 0) |
| 343 | ) |
| 344 | (:metaclass proxy-class) |
| 345 | (:size #.(size-of 'pointer)))) |
| 346 | |
| 347 | (defun %type-number-of-ginstance (location) |
| 348 | (let ((class (sap-ref-sap location 0))) |
| 349 | (sap-ref-32 class 0))) |
| 350 | |
| 351 | (defmethod make-proxy-instance :around ((class ginstance-class) location &rest initargs) |
| 352 | (declare (ignore class)) |
| 353 | (let ((class (labels ((find-known-class (type-number) |
| 354 | (or |
| 355 | (find-class (type-from-number type-number) nil) |
| 356 | (unless (zerop type-number) |
| 357 | (find-known-class (type-parent type-number)))))) |
| 358 | (find-known-class (%type-number-of-ginstance location))))) |
| 359 | ;; Note that chancing the class argument must not alter "the |
| 360 | ;; ordered set of applicable methods" as specified in the |
| 361 | ;; Hyperspec |
| 362 | (if class |
| 363 | (apply #'call-next-method class location initargs) |
| 364 | (error "Object at ~A has an unkown type number: ~A" |
| 365 | location (%type-number-of-ginstance location))))) |
| 366 | |
| 367 | (defmethod make-proxy-instance ((class ginstance-class) location &rest initargs) |
| 368 | (declare (ignore initargs)) |
| 369 | (reference-foreign class location) |
| 370 | ;; Since we make an explicit reference to the foreign object, we |
| 371 | ;; always have to release it when the proxy is garbage collected |
| 372 | ;; and therefor ignore the weak-p argument. |
| 373 | (call-next-method class location :weak nil)) |
| 374 | |
| 375 | (defmethod invalidate-instance ((instance ginstance)) |
| 376 | (declare (ignore instance)) |
| 377 | ;; A ginstance should never be invalidated since it is ref counted |
| 378 | nil) |
| 379 | |
| 380 | (define-type-method copy-from-alien-form ((type ginstance) location) |
| 381 | (declare (ignore location type)) |
| 382 | (error "Doing copy-from-alien on a ref. counted class is most certainly an error, but if it really is what you want you should use REFERENCE-FOREIGN on the returned instance instead.")) |
| 383 | |
| 384 | (define-type-method copy-from-alien-function ((type ginstance)) |
| 385 | (declare (ignore type)) |
| 386 | (error "Doing copy-from-alien on a ref. counted class is most certainly an error, but if it really is what you want you should use REFERENCE-FOREIGN on the returned instance instead.")) |
| 387 | |
| 388 | (define-type-method reader-function ((type ginstance)) |
| 389 | #'(lambda (location &optional (offset 0) weak-p) |
| 390 | (declare (ignore weak-p)) |
| 391 | (ensure-proxy-instance type (sap-ref-sap location offset)))) |
| 392 | |
| 393 | |
| 394 | ;;;; Registering fundamental types |
| 395 | |
| 396 | (register-type 'nil "void") |
| 397 | (register-type 'pointer "gpointer") |
| 398 | (register-type 'char "gchar") |
| 399 | (register-type 'unsigned-char "guchar") |
| 400 | (register-type 'boolean "gboolean") |
| 401 | (register-type 'int "gint") |
| 402 | (register-type-alias 'integer 'int) |
| 403 | (register-type-alias 'fixnum 'int) |
| 404 | (register-type 'unsigned-int "guint") |
| 405 | (register-type 'long "glong") |
| 406 | (register-type 'unsigned-long "gulong") |
| 407 | (register-type 'single-float "gfloat") |
| 408 | (register-type 'double-float "gdouble") |
| 409 | (register-type 'pathname "gchararray") |
| 410 | (register-type 'string "gchararray") |
| 411 | |
| 412 | |
| 413 | ;;;; Introspection of type information |
| 414 | |
| 415 | (defvar *derivable-type-info* (make-hash-table)) |
| 416 | |
| 417 | (defun register-derivable-type (type id expander &optional dependencies) |
| 418 | (register-type type id) |
| 419 | (let ((type-number (register-type type id))) |
| 420 | (setf |
| 421 | (gethash type-number *derivable-type-info*) |
| 422 | (list expander dependencies)))) |
| 423 | |
| 424 | (defun find-type-info (type) |
| 425 | (dolist (super (cdr (type-hierarchy type))) |
| 426 | (let ((info (gethash super *derivable-type-info*))) |
| 427 | (return-if info)))) |
| 428 | |
| 429 | (defun expand-type-definition (type forward-p options) |
| 430 | (let ((expander (first (find-type-info type)))) |
| 431 | (funcall expander (find-type-number type t) forward-p options))) |
| 432 | |
| 433 | (defbinding type-parent (type) type-number |
| 434 | ((find-type-number type t) type-number)) |
| 435 | |
| 436 | (defun supertype (type) |
| 437 | (type-from-number (type-parent type))) |
| 438 | |
| 439 | (defbinding %type-interfaces (type) pointer |
| 440 | ((find-type-number type t) type-number) |
| 441 | (n-interfaces unsigned-int :out)) |
| 442 | |
| 443 | (defun type-interfaces (type) |
| 444 | (multiple-value-bind (array length) (%type-interfaces type) |
| 445 | (unwind-protect |
| 446 | (map-c-vector 'list #'identity array 'type-number length) |
| 447 | (deallocate-memory array)))) |
| 448 | |
| 449 | (defun implements (type) |
| 450 | (mapcar #'type-from-number (type-interfaces type))) |
| 451 | |
| 452 | (defun type-hierarchy (type) |
| 453 | (let ((type-number (find-type-number type t))) |
| 454 | (unless (= type-number 0) |
| 455 | (cons type-number (type-hierarchy (type-parent type-number)))))) |
| 456 | |
| 457 | (defbinding (type-is-p "g_type_is_a") (type super) boolean |
| 458 | ((find-type-number type) type-number) |
| 459 | ((find-type-number super) type-number)) |
| 460 | |
| 461 | (defbinding %type-children () pointer |
| 462 | (type-number type-number) |
| 463 | (num-children unsigned-int :out)) |
| 464 | |
| 465 | (defun map-subtypes (function type &optional prefix) |
| 466 | (let ((type-number (find-type-number type t))) |
| 467 | (multiple-value-bind (array length) (%type-children type-number) |
| 468 | (unwind-protect |
| 469 | (map-c-vector |
| 470 | 'nil |
| 471 | #'(lambda (type-number) |
| 472 | (when (or |
| 473 | (not prefix) |
| 474 | (string-prefix-p prefix (find-foreign-type-name type-number))) |
| 475 | (funcall function type-number)) |
| 476 | (map-subtypes function type-number prefix)) |
| 477 | array 'type-number length) |
| 478 | (deallocate-memory array))))) |
| 479 | |
| 480 | (defun find-types (prefix) |
| 481 | (let ((type-list nil)) |
| 482 | (maphash |
| 483 | #'(lambda (type-number expander) |
| 484 | (declare (ignore expander)) |
| 485 | (map-subtypes |
| 486 | #'(lambda (type-number) |
| 487 | (pushnew type-number type-list)) |
| 488 | type-number prefix)) |
| 489 | *derivable-type-info*) |
| 490 | type-list)) |
| 491 | |
| 492 | (defun find-type-dependencies (type) |
| 493 | (let ((list-dependencies (second (find-type-info type)))) |
| 494 | (when list-dependencies |
| 495 | (funcall list-dependencies (find-type-number type t))))) |
| 496 | |
| 497 | (defun %sort-types-topologicaly (types) |
| 498 | (let ((partial-sorted |
| 499 | (sort |
| 500 | (mapcar |
| 501 | #'(lambda (type) |
| 502 | (cons type (remove-if #'(lambda (dep) |
| 503 | (not (find dep types))) |
| 504 | (find-type-dependencies type)))) |
| 505 | types) |
| 506 | #'(lambda (type1 type2) (type-is-p type2 type1)) :key #'car)) |
| 507 | (sorted ())) |
| 508 | |
| 509 | (loop |
| 510 | as tmp = partial-sorted then (or (rest tmp) partial-sorted) |
| 511 | while tmp |
| 512 | do (destructuring-bind (type . dependencies) (first tmp) |
| 513 | (cond |
| 514 | ((every #'(lambda (dep) |
| 515 | (assoc dep sorted)) |
| 516 | dependencies) |
| 517 | (push (cons type nil) sorted) ; no forward definition needed |
| 518 | (setq partial-sorted (delete type partial-sorted :key #'first))) |
| 519 | ((some #'(lambda (dep) |
| 520 | (find type (find-type-dependencies dep))) |
| 521 | dependencies) |
| 522 | (push (cons type t) sorted) ; forward definition needed |
| 523 | (setq partial-sorted (delete type partial-sorted :key #'first)))))) |
| 524 | (nreverse sorted))) |
| 525 | |
| 526 | |
| 527 | (defun expand-type-definitions (prefix &optional args) |
| 528 | (flet ((type-options (type-number) |
| 529 | (let ((name (find-foreign-type-name type-number))) |
| 530 | (cdr (assoc name args :test #'string=))))) |
| 531 | |
| 532 | (let ((type-list |
| 533 | (delete-if |
| 534 | #'(lambda (type-number) |
| 535 | (let ((name (find-foreign-type-name type-number))) |
| 536 | (or |
| 537 | (getf (type-options type-number) :ignore) |
| 538 | (find-if |
| 539 | #'(lambda (options) |
| 540 | (and |
| 541 | (string-prefix-p (first options) name) |
| 542 | (getf (cdr options) :ignore-prefix) |
| 543 | (not (some |
| 544 | #'(lambda (exception) |
| 545 | (string= name exception)) |
| 546 | (getf (cdr options) :except))))) |
| 547 | args)))) |
| 548 | (find-types prefix)))) |
| 549 | |
| 550 | (dolist (type-number type-list) |
| 551 | (let ((name (find-foreign-type-name type-number))) |
| 552 | (register-type |
| 553 | (getf (type-options type-number) :type (default-type-name name)) |
| 554 | (register-type-as type-number)))) |
| 555 | |
| 556 | (let ((sorted-type-list (%sort-types-topologicaly type-list))) |
| 557 | `(progn |
| 558 | ,@(mapcar |
| 559 | #'(lambda (pair) |
| 560 | (destructuring-bind (type . forward-p) pair |
| 561 | (expand-type-definition type forward-p (type-options type)))) |
| 562 | sorted-type-list) |
| 563 | ,@(mapcar |
| 564 | #'(lambda (pair) |
| 565 | (destructuring-bind (type . forward-p) pair |
| 566 | (when forward-p |
| 567 | (expand-type-definition type nil (type-options type))))) |
| 568 | sorted-type-list)))))) |
| 569 | |
| 570 | (defmacro define-types-by-introspection (prefix &rest args) |
| 571 | (expand-type-definitions prefix args)) |
| 572 | |
| 573 | |
| 574 | ;;;; Initialize all non static types in GObject |
| 575 | |
| 576 | (init-types-in-library #.(concatenate 'string (pkg-config:pkg-variable "glib-2.0" "libdir") "/libgobject-2.0.so")) |