Changed to MIT license
[clg] / glib / gtype.lisp
CommitLineData
55212af1 1;; Common Lisp bindings for GTK+ v2.x
2;; Copyright 2000-2005 Espen S. Johnsen <espen@users.sf.net>
0d07716f 3;;
55212af1 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:
0d07716f 11;;
55212af1 12;; The above copyright notice and this permission notice shall be
13;; included in all copies or substantial portions of the Software.
0d07716f 14;;
55212af1 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.32 2005/04/23 16:48:51 espen Exp $
0d07716f 24
25(in-package "GLIB")
26
27(use-prefix "g")
28
d1266407 29;; Initialize the glib type system
30(defbinding type-init () nil)
31(type-init)
0d07716f 32
33(deftype type-number () '(unsigned 32))
34
b4a2c852 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))
40c346ec 56 `(type-from-number ,type-number))
b4a2c852 57
58(defmethod from-alien-function ((type (eql 'gtype)) &rest args)
59 (declare (ignore type args))
60 #'(lambda (type-number)
40c346ec 61 (type-from-number type-number)))
b4a2c852 62
63(defmethod writer-function ((type (eql 'gtype)) &rest args)
3d36c5d6 64 (declare (ignore type args))
b4a2c852 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)
3d36c5d6 70 (declare (ignore type args))
b4a2c852 71 (let ((reader (reader-function 'type-number)))
72 #'(lambda (location &optional (offset 0))
40c346ec 73 (type-from-number (funcall reader location offset)))))
b4a2c852 74
75
fc47a022 76(eval-when (:compile-toplevel :load-toplevel :execute)
3a935dfa 77 (defclass type-query (struct)
fc47a022 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))
4d1d3921 82 (:metaclass struct-class)))
fc47a022 83
84
b4a2c852 85(defbinding type-query (type) nil
86 ((find-type-number type t) type-number)
87 ((make-instance 'type-query) type-query :return))
fc47a022 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))
0d07716f 94
3a935dfa 95(defbinding type-class-ref (type) pointer
96 ((find-type-number type t) type-number))
0d07716f 97
3a935dfa 98(defbinding type-class-unref (type) nil
99 ((find-type-number type t) type-number))
fc47a022 100
3a935dfa 101(defbinding type-class-peek (type) pointer
102 ((find-type-number type t) type-number))
fc47a022 103
0d07716f 104
3a935dfa 105;;;; Mapping between lisp types and glib types
0d07716f 106
dcb31db6 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))
3a935dfa 111
112(defbinding %type-from-name () type-number
113 (name string))
114
dcb31db6 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)
0d07716f 158 (etypecase type
159 (integer type)
dcb31db6 160 (string (type-number-from-glib-name type error-p))
3a935dfa 161 (symbol
dcb31db6 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))))
0d07716f 166
b011356b 167(defun type-from-number (type-number &optional error)
168 (multiple-value-bind (type found)
dcb31db6 169 (gethash type-number *type-number-to-lisp-type*)
b011356b 170 (when (and error (not found))
dcb31db6 171 (let ((name (find-foreign-type-name type-number)))
b011356b 172 (if name
173 (error "Type number not registered: ~A (~A)" type-number name)
174 (error "Invalid type number: ~A" type-number))))
175 type))
0d07716f 176
dcb31db6 177(defbinding (find-foreign-type-name "g_type_name") (type) (copy-of string)
3a935dfa 178 ((find-type-number type t) type-number))
179
180(defun type-number-of (object)
181 (find-type-number (type-of object) t))
182
6556dccd 183(eval-when (:compile-toplevel :load-toplevel :execute)
dcb31db6 184 (defvar *type-initializers* ())
185 (defun %find-types-in-library (pathname prefixes ignore)
6556dccd 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
dcb31db6 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))
6556dccd 204 (not (member symbol ignore :test #'string=)))
205 collect symbol)
206 (process-close process)))))
207
0d07716f 208
80031aba 209(defmacro init-types-in-library (filename &key prefix ignore)
6556dccd 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)
dcb31db6 215 (,(intern name))
216 (pushnew ',(intern name) *type-initializers*)))
6556dccd 217 names))))
218
dcb31db6 219(defun find-type-init-function (type-number)
80031aba 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)))
dcb31db6 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
6556dccd 236
237
238;;;; Metaclass for subclasses of ginstance
239
240(eval-when (:compile-toplevel :load-toplevel :execute)
241 (defclass ginstance-class (proxy-class)
242 ()))
243
244
7554d66c 245(defmethod shared-initialize ((class ginstance-class) names &rest initargs &key name gtype)
6556dccd 246 (declare (ignore names))
247 (let* ((class-name (or name (class-name class)))
dcb31db6 248 (type-number
249 (or
250 (find-type-number class-name)
251 (register-type class-name
252 (or (first gtype) (default-type-init-name class-name))))))
7554d66c 253 (if (getf initargs :size)
254 (call-next-method)
255 (let ((size (type-instance-size type-number)))
256 (apply #'call-next-method class names :size (list size) initargs)))))
b011356b 257
258
6556dccd 259(defmethod validate-superclass ((class ginstance-class) (super standard-class))
260 (subtypep (class-name super) 'ginstance))
261
0d07716f 262
fc47a022 263;;;; Superclass for wrapping types in the glib type system
0d07716f 264
265(eval-when (:compile-toplevel :load-toplevel :execute)
fc47a022 266 (defclass ginstance (proxy)
3a935dfa 267 ((class :allocation :alien :type pointer))
268 (:metaclass proxy-class)))
0d07716f 269
609ba905 270(defun %type-number-of-ginstance (location)
fc47a022 271 (let ((class (sap-ref-sap location 0)))
609ba905 272 (sap-ref-32 class 0)))
0d07716f 273
4d1d3921 274(defmethod ensure-proxy-instance ((class ginstance-class) location)
275 (declare (ignore class))
609ba905 276 (let ((class (labels ((find-known-class (type-number)
277 (or
278 (find-class (type-from-number type-number) nil)
279 (unless (zerop type-number)
280 (find-known-class (type-parent type-number))))))
281 (find-known-class (%type-number-of-ginstance location)))))
4d1d3921 282 (if class
283 (make-instance class :location (reference-foreign class location))
609ba905 284 (error "Object at ~A has an unkown type number: ~A"
285 location (%type-number-of-ginstance location)))))
0d07716f 286
508d13a7 287(defmethod copy-from-alien-form (location (class ginstance-class) &rest args)
288 (declare (ignore location class args))
289 (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."))
290
291(defmethod copy-from-alien-function ((class ginstance-class) &rest args)
292 (declare (ignore class args))
293 (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."))
294
295(defmethod reader-function ((class ginstance-class) &rest args)
296 (declare (ignore args))
297 #'(lambda (location &optional (offset 0))
298 (ensure-proxy-instance class (sap-ref-sap location offset))))
299
0d07716f 300
3a935dfa 301;;;; Registering fundamental types
302
40c346ec 303(register-type 'nil "void")
3a935dfa 304(register-type 'pointer "gpointer")
305(register-type 'char "gchar")
306(register-type 'unsigned-char "guchar")
307(register-type 'boolean "gboolean")
3a935dfa 308(register-type 'int "gint")
0b392a0d 309(register-type-alias 'integer 'int)
dcb31db6 310(register-type-alias 'fixnum 'int)
3a935dfa 311(register-type 'unsigned-int "guint")
312(register-type 'long "glong")
313(register-type 'unsigned-long "gulong")
314(register-type 'single-float "gfloat")
315(register-type 'double-float "gdouble")
b77fe850 316(register-type 'pathname "gchararray")
b011356b 317(register-type 'string "gchararray")
3a935dfa 318
319
e9934f39 320;;;; Introspection of type information
3a935dfa 321
4812615b 322(defvar *derivable-type-info* (make-hash-table))
3a935dfa 323
e9934f39 324(defun register-derivable-type (type id expander &optional dependencies)
3a935dfa 325 (register-type type id)
4812615b 326 (let ((type-number (register-type type id)))
e9934f39 327 (setf
328 (gethash type-number *derivable-type-info*)
329 (list expander dependencies))))
3a935dfa 330
b011356b 331(defun find-type-info (type)
332 (dolist (super (cdr (type-hierarchy type)))
4812615b 333 (let ((info (gethash super *derivable-type-info*)))
b011356b 334 (return-if info))))
335
e9934f39 336(defun expand-type-definition (type forward-p options)
337 (let ((expander (first (find-type-info type))))
338 (funcall expander (find-type-number type t) forward-p options)))
3a935dfa 339
3a935dfa 340(defbinding type-parent (type) type-number
341 ((find-type-number type t) type-number))
342
343(defun supertype (type)
344 (type-from-number (type-parent type)))
345
7858d45e 346(defbinding %type-interfaces (type) pointer
347 ((find-type-number type t) type-number)
348 (n-interfaces unsigned-int :out))
349
350(defun type-interfaces (type)
351 (multiple-value-bind (array length) (%type-interfaces type)
352 (unwind-protect
4d1d3921 353 (map-c-vector 'list #'identity array 'type-number length)
7858d45e 354 (deallocate-memory array))))
355
356(defun implements (type)
357 (mapcar #'type-from-number (type-interfaces type)))
358
3a935dfa 359(defun type-hierarchy (type)
360 (let ((type-number (find-type-number type t)))
361 (unless (= type-number 0)
362 (cons type-number (type-hierarchy (type-parent type-number))))))
363
364(defbinding (type-is-p "g_type_is_a") (type super) boolean
365 ((find-type-number type) type-number)
366 ((find-type-number super) type-number))
367
368(defbinding %type-children () pointer
369 (type-number type-number)
370 (num-children unsigned-int :out))
371
372(defun map-subtypes (function type &optional prefix)
373 (let ((type-number (find-type-number type t)))
374 (multiple-value-bind (array length) (%type-children type-number)
375 (unwind-protect
4d1d3921 376 (map-c-vector
3a935dfa 377 'nil
378 #'(lambda (type-number)
379 (when (or
380 (not prefix)
dcb31db6 381 (string-prefix-p prefix (find-foreign-type-name type-number)))
3a935dfa 382 (funcall function type-number))
383 (map-subtypes function type-number prefix))
384 array 'type-number length)
385 (deallocate-memory array)))))
386
387(defun find-types (prefix)
388 (let ((type-list nil))
4812615b 389 (maphash
390 #'(lambda (type-number expander)
391 (declare (ignore expander))
392 (map-subtypes
393 #'(lambda (type-number)
394 (pushnew type-number type-list))
395 type-number prefix))
396 *derivable-type-info*)
3a935dfa 397 type-list))
398
e9934f39 399(defun find-type-dependencies (type)
400 (let ((list-dependencies (second (find-type-info type))))
401 (when list-dependencies
402 (funcall list-dependencies (find-type-number type t)))))
403
404(defun %sort-types-topologicaly (types)
6556dccd 405 (let ((partial-sorted
406 (sort
407 (mapcar
408 #'(lambda (type)
409 (cons type (remove-if #'(lambda (dep)
410 (not (find dep types)))
411 (find-type-dependencies type))))
412 types)
413 #'(lambda (type1 type2) (type-is-p type2 type1)) :key #'car))
e9934f39 414 (sorted ()))
415
416 (loop
6556dccd 417 as tmp = partial-sorted then (or (rest tmp) partial-sorted)
e9934f39 418 while tmp
419 do (destructuring-bind (type . dependencies) (first tmp)
3a935dfa 420 (cond
e9934f39 421 ((every #'(lambda (dep)
6556dccd 422 (assoc dep sorted))
e9934f39 423 dependencies)
6556dccd 424 (push (cons type nil) sorted) ; no forward definition needed
425 (setq partial-sorted (delete type partial-sorted :key #'first)))
e9934f39 426 ((some #'(lambda (dep)
427 (find type (find-type-dependencies dep)))
428 dependencies)
6556dccd 429 (push (cons type t) sorted) ; forward definition needed
430 (setq partial-sorted (delete type partial-sorted :key #'first))))))
431 (nreverse sorted)))
3a935dfa 432
433
434(defun expand-type-definitions (prefix &optional args)
dcb31db6 435 (flet ((type-options (type-number)
436 (let ((name (find-foreign-type-name type-number)))
b011356b 437 (cdr (assoc name args :test #'string=)))))
3a935dfa 438
4812615b 439 (let ((type-list
440 (delete-if
441 #'(lambda (type-number)
dcb31db6 442 (let ((name (find-foreign-type-name type-number)))
4812615b 443 (or
444 (getf (type-options type-number) :ignore)
445 (find-if
446 #'(lambda (options)
447 (and
448 (string-prefix-p (first options) name)
17c607d0 449 (getf (cdr options) :ignore-prefix)
450 (not (some
451 #'(lambda (exception)
452 (string= name exception))
453 (getf (cdr options) :except)))))
4812615b 454 args))))
455 (find-types prefix))))
6556dccd 456
4812615b 457 (dolist (type-number type-list)
dcb31db6 458 (let ((name (find-foreign-type-name type-number)))
4812615b 459 (register-type
460 (getf (type-options type-number) :type (default-type-name name))
80031aba 461 (register-type-as type-number))))
6556dccd 462
463 (let ((sorted-type-list (%sort-types-topologicaly type-list)))
e9934f39 464 `(progn
465 ,@(mapcar
6556dccd 466 #'(lambda (pair)
467 (destructuring-bind (type . forward-p) pair
468 (expand-type-definition type forward-p (type-options type))))
469 sorted-type-list)
e9934f39 470 ,@(mapcar
6556dccd 471 #'(lambda (pair)
472 (destructuring-bind (type . forward-p) pair
473 (when forward-p
474 (expand-type-definition type nil (type-options type)))))
e9934f39 475 sorted-type-list))))))
4812615b 476
3a935dfa 477(defmacro define-types-by-introspection (prefix &rest args)
b011356b 478 (expand-type-definitions prefix args))
6556dccd 479
480
481;;;; Initialize all non static types in GObject
482
483(init-types-in-library #.(concatenate 'string (pkg-config:pkg-variable "glib-2.0" "libdir") "/libgobject-2.0.so"))