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