chiark / gitweb /
Exporting CALL-NEXT-HANDLER
[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
15cbdefc 23;; $Id: gtype.lisp,v 1.33 2006-01-31 14:02:51 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*)
4de90d10 170 (when (and error (not found))
dfa4f314 171 (let ((name (find-foreign-type-name type-number)))
4de90d10 172 (if name
173 (error "Type number not registered: ~A (~A)" type-number name)
174 (error "Invalid type number: ~A" type-number))))
175 type))
560af5c5 176
dfa4f314 177(defbinding (find-foreign-type-name "g_type_name") (type) (copy-of string)
d4b21b08 178 ((find-type-number type t) type-number))
179
180(defun type-number-of (object)
181 (find-type-number (type-of object) t))
182
21299acf 183(eval-when (:compile-toplevel :load-toplevel :execute)
dfa4f314 184 (defvar *type-initializers* ())
185 (defun %find-types-in-library (pathname prefixes ignore)
21299acf 186 (let ((process (run-program
187 "/usr/bin/nm" (list "--defined-only" "-D" (namestring (truename pathname)))
188 :output :stream :wait nil)))
189 (unwind-protect
190 (loop
191 as symbol = (let ((line (read-line (process-output process) nil)))
192 (when line (subseq line 11)))
193 while symbol
194 when (and
dfa4f314 195 (> (length symbol) 9)
196 (or
197 (not prefixes)
198 (some #'(lambda (prefix)
199 (and
200 (> (length symbol) (length prefix))
201 (string= prefix symbol :end2 (length prefix))))
202 (mklist prefixes)))
203 (string= "_get_type" symbol :start2 (- (length symbol) 9))
21299acf 204 (not (member symbol ignore :test #'string=)))
205 collect symbol)
206 (process-close process)))))
207
560af5c5 208
735a29da 209(defmacro init-types-in-library (filename &key prefix ignore)
21299acf 210 (let ((names (%find-types-in-library filename prefix ignore)))
211 `(progn
212 ,@(mapcar #'(lambda (name)
213 `(progn
214 (defbinding (,(intern name) ,name) () type-number)
dfa4f314 215 (,(intern name))
216 (pushnew ',(intern name) *type-initializers*)))
21299acf 217 names))))
218
dfa4f314 219(defun find-type-init-function (type-number)
735a29da 220 (loop
221 for type-init in *type-initializers*
222 when (= type-number (funcall type-init))
223 do (return type-init)))
224
225(defun register-type-as (type-number)
226 (or
227 (find-type-init-function type-number)
228 (find-foreign-type-name type-number)
229 (error "Unknown type-number: ~A" type-number)))
dfa4f314 230
231(defun default-type-init-name (type)
232 (find-symbol (format nil "~A_~A_get_type"
233 (package-prefix *package*)
234 (substitute #\_ #\- (string-downcase type)))))
235
21299acf 236
15cbdefc 237(eval-when (:compile-toplevel :load-toplevel :execute)
238 (defclass type-info (struct)
239 ((class-size :allocation :alien :type (unsigned 16) :initarg :class-size)
240 (base-init :allocation :alien :type pointer)
241 (base-finalize :allocation :alien :type pointer)
242 (class-init :allocation :alien :type pointer)
243 (class-finalize :allocation :alien :type pointer)
244 (class-data :allocation :alien :type pointer)
245 (instance-size :allocation :alien :type (unsigned 16)
246 :initarg :instance-size)
247 (n-preallocs :allocation :alien :type (unsigned 16))
248 (instance-init :allocation :alien :type pointer)
249 (value-table :allocation :alien :type pointer))
250 (:metaclass struct-class)))
251
252(defbinding %type-register-static () type-number
253 (parent-type gtype)
254 (name string)
255 (info type-info)
256 (0 unsigned-int))
257
258(defun register-new-type (type parent)
259 (let ((parent-info (type-query parent)))
260 (with-slots ((parent-number type-number) class-size instance-size) parent-info
261 (let ((type-number
262 (%type-register-static
263 parent-number
264 (default-alien-type-name type)
265 (make-instance 'type-info :class-size class-size :instance-size instance-size))))
266 (setf (gethash type *lisp-type-to-type-number*) type-number)
267 (setf (gethash type-number *type-number-to-lisp-type*) type)
268 type-number))))
269
270
21299acf 271
272;;;; Metaclass for subclasses of ginstance
273
274(eval-when (:compile-toplevel :load-toplevel :execute)
275 (defclass ginstance-class (proxy-class)
276 ()))
277
278
13d35622 279(defmethod shared-initialize ((class ginstance-class) names &rest initargs &key name gtype)
21299acf 280 (declare (ignore names))
281 (let* ((class-name (or name (class-name class)))
dfa4f314 282 (type-number
283 (or
284 (find-type-number class-name)
285 (register-type class-name
286 (or (first gtype) (default-type-init-name class-name))))))
13d35622 287 (if (getf initargs :size)
288 (call-next-method)
289 (let ((size (type-instance-size type-number)))
290 (apply #'call-next-method class names :size (list size) initargs)))))
4de90d10 291
292
21299acf 293(defmethod validate-superclass ((class ginstance-class) (super standard-class))
294 (subtypep (class-name super) 'ginstance))
295
560af5c5 296
93aa67db 297;;;; Superclass for wrapping types in the glib type system
560af5c5 298
299(eval-when (:compile-toplevel :load-toplevel :execute)
93aa67db 300 (defclass ginstance (proxy)
d4b21b08 301 ((class :allocation :alien :type pointer))
302 (:metaclass proxy-class)))
560af5c5 303
895c9a9e 304(defun %type-number-of-ginstance (location)
93aa67db 305 (let ((class (sap-ref-sap location 0)))
895c9a9e 306 (sap-ref-32 class 0)))
560af5c5 307
d168bafd 308(defmethod ensure-proxy-instance ((class ginstance-class) location)
309 (declare (ignore class))
895c9a9e 310 (let ((class (labels ((find-known-class (type-number)
311 (or
312 (find-class (type-from-number type-number) nil)
313 (unless (zerop type-number)
314 (find-known-class (type-parent type-number))))))
315 (find-known-class (%type-number-of-ginstance location)))))
d168bafd 316 (if class
317 (make-instance class :location (reference-foreign class location))
895c9a9e 318 (error "Object at ~A has an unkown type number: ~A"
319 location (%type-number-of-ginstance location)))))
560af5c5 320
9ca5565a 321(defmethod copy-from-alien-form (location (class ginstance-class) &rest args)
322 (declare (ignore location class args))
323 (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."))
324
325(defmethod copy-from-alien-function ((class ginstance-class) &rest args)
326 (declare (ignore class args))
327 (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."))
328
329(defmethod reader-function ((class ginstance-class) &rest args)
330 (declare (ignore args))
331 #'(lambda (location &optional (offset 0))
332 (ensure-proxy-instance class (sap-ref-sap location offset))))
333
560af5c5 334
d4b21b08 335;;;; Registering fundamental types
336
63752532 337(register-type 'nil "void")
d4b21b08 338(register-type 'pointer "gpointer")
339(register-type 'char "gchar")
340(register-type 'unsigned-char "guchar")
341(register-type 'boolean "gboolean")
d4b21b08 342(register-type 'int "gint")
73383a9e 343(register-type-alias 'integer 'int)
dfa4f314 344(register-type-alias 'fixnum 'int)
d4b21b08 345(register-type 'unsigned-int "guint")
346(register-type 'long "glong")
347(register-type 'unsigned-long "gulong")
348(register-type 'single-float "gfloat")
349(register-type 'double-float "gdouble")
a8cb9408 350(register-type 'pathname "gchararray")
4de90d10 351(register-type 'string "gchararray")
d4b21b08 352
353
62f12808 354;;;; Introspection of type information
d4b21b08 355
e77e7713 356(defvar *derivable-type-info* (make-hash-table))
d4b21b08 357
62f12808 358(defun register-derivable-type (type id expander &optional dependencies)
d4b21b08 359 (register-type type id)
e77e7713 360 (let ((type-number (register-type type id)))
62f12808 361 (setf
362 (gethash type-number *derivable-type-info*)
363 (list expander dependencies))))
d4b21b08 364
4de90d10 365(defun find-type-info (type)
366 (dolist (super (cdr (type-hierarchy type)))
e77e7713 367 (let ((info (gethash super *derivable-type-info*)))
4de90d10 368 (return-if info))))
369
62f12808 370(defun expand-type-definition (type forward-p options)
371 (let ((expander (first (find-type-info type))))
372 (funcall expander (find-type-number type t) forward-p options)))
d4b21b08 373
d4b21b08 374(defbinding type-parent (type) type-number
375 ((find-type-number type t) type-number))
376
377(defun supertype (type)
378 (type-from-number (type-parent type)))
379
337933d8 380(defbinding %type-interfaces (type) pointer
381 ((find-type-number type t) type-number)
382 (n-interfaces unsigned-int :out))
383
384(defun type-interfaces (type)
385 (multiple-value-bind (array length) (%type-interfaces type)
386 (unwind-protect
d168bafd 387 (map-c-vector 'list #'identity array 'type-number length)
337933d8 388 (deallocate-memory array))))
389
390(defun implements (type)
391 (mapcar #'type-from-number (type-interfaces type)))
392
d4b21b08 393(defun type-hierarchy (type)
394 (let ((type-number (find-type-number type t)))
395 (unless (= type-number 0)
396 (cons type-number (type-hierarchy (type-parent type-number))))))
397
398(defbinding (type-is-p "g_type_is_a") (type super) boolean
399 ((find-type-number type) type-number)
400 ((find-type-number super) type-number))
401
402(defbinding %type-children () pointer
403 (type-number type-number)
404 (num-children unsigned-int :out))
405
406(defun map-subtypes (function type &optional prefix)
407 (let ((type-number (find-type-number type t)))
408 (multiple-value-bind (array length) (%type-children type-number)
409 (unwind-protect
d168bafd 410 (map-c-vector
d4b21b08 411 'nil
412 #'(lambda (type-number)
413 (when (or
414 (not prefix)
dfa4f314 415 (string-prefix-p prefix (find-foreign-type-name type-number)))
d4b21b08 416 (funcall function type-number))
417 (map-subtypes function type-number prefix))
418 array 'type-number length)
419 (deallocate-memory array)))))
420
421(defun find-types (prefix)
422 (let ((type-list nil))
e77e7713 423 (maphash
424 #'(lambda (type-number expander)
425 (declare (ignore expander))
426 (map-subtypes
427 #'(lambda (type-number)
428 (pushnew type-number type-list))
429 type-number prefix))
430 *derivable-type-info*)
d4b21b08 431 type-list))
432
62f12808 433(defun find-type-dependencies (type)
434 (let ((list-dependencies (second (find-type-info type))))
435 (when list-dependencies
436 (funcall list-dependencies (find-type-number type t)))))
437
438(defun %sort-types-topologicaly (types)
21299acf 439 (let ((partial-sorted
440 (sort
441 (mapcar
442 #'(lambda (type)
443 (cons type (remove-if #'(lambda (dep)
444 (not (find dep types)))
445 (find-type-dependencies type))))
446 types)
447 #'(lambda (type1 type2) (type-is-p type2 type1)) :key #'car))
62f12808 448 (sorted ()))
449
450 (loop
21299acf 451 as tmp = partial-sorted then (or (rest tmp) partial-sorted)
62f12808 452 while tmp
453 do (destructuring-bind (type . dependencies) (first tmp)
d4b21b08 454 (cond
62f12808 455 ((every #'(lambda (dep)
21299acf 456 (assoc dep sorted))
62f12808 457 dependencies)
21299acf 458 (push (cons type nil) sorted) ; no forward definition needed
459 (setq partial-sorted (delete type partial-sorted :key #'first)))
62f12808 460 ((some #'(lambda (dep)
461 (find type (find-type-dependencies dep)))
462 dependencies)
21299acf 463 (push (cons type t) sorted) ; forward definition needed
464 (setq partial-sorted (delete type partial-sorted :key #'first))))))
465 (nreverse sorted)))
d4b21b08 466
467
468(defun expand-type-definitions (prefix &optional args)
dfa4f314 469 (flet ((type-options (type-number)
470 (let ((name (find-foreign-type-name type-number)))
4de90d10 471 (cdr (assoc name args :test #'string=)))))
d4b21b08 472
e77e7713 473 (let ((type-list
474 (delete-if
475 #'(lambda (type-number)
dfa4f314 476 (let ((name (find-foreign-type-name type-number)))
e77e7713 477 (or
478 (getf (type-options type-number) :ignore)
479 (find-if
480 #'(lambda (options)
481 (and
482 (string-prefix-p (first options) name)
80a09c29 483 (getf (cdr options) :ignore-prefix)
484 (not (some
485 #'(lambda (exception)
486 (string= name exception))
487 (getf (cdr options) :except)))))
e77e7713 488 args))))
489 (find-types prefix))))
21299acf 490
e77e7713 491 (dolist (type-number type-list)
dfa4f314 492 (let ((name (find-foreign-type-name type-number)))
e77e7713 493 (register-type
494 (getf (type-options type-number) :type (default-type-name name))
735a29da 495 (register-type-as type-number))))
21299acf 496
497 (let ((sorted-type-list (%sort-types-topologicaly type-list)))
62f12808 498 `(progn
499 ,@(mapcar
21299acf 500 #'(lambda (pair)
501 (destructuring-bind (type . forward-p) pair
502 (expand-type-definition type forward-p (type-options type))))
503 sorted-type-list)
62f12808 504 ,@(mapcar
21299acf 505 #'(lambda (pair)
506 (destructuring-bind (type . forward-p) pair
507 (when forward-p
508 (expand-type-definition type nil (type-options type)))))
62f12808 509 sorted-type-list))))))
e77e7713 510
d4b21b08 511(defmacro define-types-by-introspection (prefix &rest args)
4de90d10 512 (expand-type-definitions prefix args))
21299acf 513
514
515;;;; Initialize all non static types in GObject
516
517(init-types-in-library #.(concatenate 'string (pkg-config:pkg-variable "glib-2.0" "libdir") "/libgobject-2.0.so"))