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