chiark / gitweb /
Added dependency to the gtk system and a couple of bug fixes
[clg] / glib / gtype.lisp
CommitLineData
560af5c5 1;; Common Lisp bindings for GTK+ v2.0
d4b21b08 2;; Copyright (C) 2000-2001 Espen S. Johnsen <esj@stud.cs.uit.no>
560af5c5 3;;
4;; This library is free software; you can redistribute it and/or
5;; modify it under the terms of the GNU Lesser General Public
6;; License as published by the Free Software Foundation; either
7;; version 2 of the License, or (at your option) any later version.
8;;
9;; This library is distributed in the hope that it will be useful,
10;; but WITHOUT ANY WARRANTY; without even the implied warranty of
11;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12;; Lesser General Public License for more details.
13;;
14;; You should have received a copy of the GNU Lesser General Public
15;; License along with this library; if not, write to the Free Software
16;; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
73383a9e 18;; $Id: gtype.lisp,v 1.31 2005-04-03 17:14:38 espen Exp $
560af5c5 19
20(in-package "GLIB")
21
22(use-prefix "g")
23
e0349dbd 24;; Initialize the glib type system
25(defbinding type-init () nil)
26(type-init)
560af5c5 27
28(deftype type-number () '(unsigned 32))
29
e74cfcab 30(deftype gtype () 'symbol)
31
32(defmethod alien-type ((type (eql 'gtype)) &rest args)
33 (declare (ignore type args))
34 (alien-type 'type-number))
35
36(defmethod size-of ((type (eql 'gtype)) &rest args)
37 (declare (ignore type args))
38 (size-of 'type-number))
39
40(defmethod to-alien-form (gtype (type (eql 'gtype)) &rest args)
41 (declare (ignore type args))
42 `(find-type-number ,gtype t))
43
44(defmethod to-alien-function ((type (eql 'gtype)) &rest args)
45 (declare (ignore type args))
46 #'(lambda (gtype)
47 (find-type-number gtype t)))
48
49(defmethod from-alien-form (type-number (type (eql 'gtype)) &rest args)
50 (declare (ignore type args))
63752532 51 `(type-from-number ,type-number))
e74cfcab 52
53(defmethod from-alien-function ((type (eql 'gtype)) &rest args)
54 (declare (ignore type args))
55 #'(lambda (type-number)
63752532 56 (type-from-number type-number)))
e74cfcab 57
58(defmethod writer-function ((type (eql 'gtype)) &rest args)
73572c12 59 (declare (ignore type args))
e74cfcab 60 (let ((writer (writer-function 'type-number)))
61 #'(lambda (gtype location &optional (offset 0))
62 (funcall writer (find-type-number gtype t) location offset))))
63
64(defmethod reader-function ((type (eql 'gtype)) &rest args)
73572c12 65 (declare (ignore type args))
e74cfcab 66 (let ((reader (reader-function 'type-number)))
67 #'(lambda (location &optional (offset 0))
63752532 68 (type-from-number (funcall reader location offset)))))
e74cfcab 69
70
93aa67db 71(eval-when (:compile-toplevel :load-toplevel :execute)
d4b21b08 72 (defclass type-query (struct)
93aa67db 73 ((type-number :allocation :alien :type type-number)
74 (name :allocation :alien :type string)
75 (class-size :allocation :alien :type unsigned-int)
76 (instance-size :allocation :alien :type unsigned-int))
d168bafd 77 (:metaclass struct-class)))
93aa67db 78
79
e74cfcab 80(defbinding type-query (type) nil
81 ((find-type-number type t) type-number)
82 ((make-instance 'type-query) type-query :return))
93aa67db 83
84(defun type-instance-size (type)
85 (slot-value (type-query type) 'instance-size))
86
87(defun type-class-size (type)
88 (slot-value (type-query type) 'class-size))
560af5c5 89
d4b21b08 90(defbinding type-class-ref (type) pointer
91 ((find-type-number type t) type-number))
560af5c5 92
d4b21b08 93(defbinding type-class-unref (type) nil
94 ((find-type-number type t) type-number))
93aa67db 95
d4b21b08 96(defbinding type-class-peek (type) pointer
97 ((find-type-number type t) type-number))
93aa67db 98
560af5c5 99
d4b21b08 100;;;; Mapping between lisp types and glib types
560af5c5 101
dfa4f314 102(defvar *registered-types* ())
103(defvar *registered-type-aliases* ())
104(defvar *lisp-type-to-type-number* (make-hash-table))
105(defvar *type-number-to-lisp-type* (make-hash-table))
d4b21b08 106
107(defbinding %type-from-name () type-number
108 (name string))
109
dfa4f314 110(defun type-number-from-glib-name (name &optional (error-p t))
111 (let ((type-number (%type-from-name name)))
112 (cond
113 ((not (zerop type-number)) type-number)
114 (error-p (error "Invalid gtype name: ~A" name)))))
115
116(defun register-type (type id)
117 (pushnew (cons type id) *registered-types* :key #'car)
118 (let ((type-number
119 (typecase id
120 (string (type-number-from-glib-name id))
121 (symbol (funcall id)))))
122 (setf (gethash type *lisp-type-to-type-number*) type-number)
123 (setf (gethash type-number *type-number-to-lisp-type*) type)
124 type-number))
125
126(defun register-type-alias (type alias)
127 (pushnew (cons type alias) *registered-type-aliases* :key #'car)
128 (setf
129 (gethash type *lisp-type-to-type-number*)
130 (find-type-number alias t)))
131
132(defun reinitialize-all-types ()
133 (clrhash *lisp-type-to-type-number*)
134 (clrhash *type-number-to-lisp-type*)
135 (type-init) ; initialize the glib type system
136 (mapc #'(lambda (type)
137 (register-type (car type) (cdr type)))
138 *registered-types*)
139 (mapc #'(lambda (type)
140 (register-type-alias (car type) (cdr type)))
141 *registered-type-aliases*))
142
143(pushnew 'reinitialize-all-types
144 #+cmu *after-save-initializations*
145 #+sbcl *init-hooks*)
146
147#+cmu
148(pushnew 'system::reinitialize-global-table ; we shouldn't have to do this?
149 *after-save-initializations*)
150
151
152(defun find-type-number (type &optional error-p)
560af5c5 153 (etypecase type
154 (integer type)
dfa4f314 155 (string (type-number-from-glib-name type error-p))
d4b21b08 156 (symbol
dfa4f314 157 (or
158 (gethash type *lisp-type-to-type-number*)
159 (and error-p (error "Type not registered: ~A" type))))
160 (class (find-type-number (class-name type) error-p))))
560af5c5 161
4de90d10 162(defun type-from-number (type-number &optional error)
163 (multiple-value-bind (type found)
dfa4f314 164 (gethash type-number *type-number-to-lisp-type*)
4de90d10 165 (when (and error (not found))
dfa4f314 166 (let ((name (find-foreign-type-name type-number)))
4de90d10 167 (if name
168 (error "Type number not registered: ~A (~A)" type-number name)
169 (error "Invalid type number: ~A" type-number))))
170 type))
560af5c5 171
dfa4f314 172(defbinding (find-foreign-type-name "g_type_name") (type) (copy-of string)
d4b21b08 173 ((find-type-number type t) type-number))
174
175(defun type-number-of (object)
176 (find-type-number (type-of object) t))
177
21299acf 178(eval-when (:compile-toplevel :load-toplevel :execute)
dfa4f314 179 (defvar *type-initializers* ())
180 (defun %find-types-in-library (pathname prefixes ignore)
21299acf 181 (let ((process (run-program
182 "/usr/bin/nm" (list "--defined-only" "-D" (namestring (truename pathname)))
183 :output :stream :wait nil)))
184 (unwind-protect
185 (loop
186 as symbol = (let ((line (read-line (process-output process) nil)))
187 (when line (subseq line 11)))
188 while symbol
189 when (and
dfa4f314 190 (> (length symbol) 9)
191 (or
192 (not prefixes)
193 (some #'(lambda (prefix)
194 (and
195 (> (length symbol) (length prefix))
196 (string= prefix symbol :end2 (length prefix))))
197 (mklist prefixes)))
198 (string= "_get_type" symbol :start2 (- (length symbol) 9))
21299acf 199 (not (member symbol ignore :test #'string=)))
200 collect symbol)
201 (process-close process)))))
202
560af5c5 203
735a29da 204(defmacro init-types-in-library (filename &key prefix ignore)
21299acf 205 (let ((names (%find-types-in-library filename prefix ignore)))
206 `(progn
207 ,@(mapcar #'(lambda (name)
208 `(progn
209 (defbinding (,(intern name) ,name) () type-number)
dfa4f314 210 (,(intern name))
211 (pushnew ',(intern name) *type-initializers*)))
21299acf 212 names))))
213
dfa4f314 214(defun find-type-init-function (type-number)
735a29da 215 (loop
216 for type-init in *type-initializers*
217 when (= type-number (funcall type-init))
218 do (return type-init)))
219
220(defun register-type-as (type-number)
221 (or
222 (find-type-init-function type-number)
223 (find-foreign-type-name type-number)
224 (error "Unknown type-number: ~A" type-number)))
dfa4f314 225
226(defun default-type-init-name (type)
227 (find-symbol (format nil "~A_~A_get_type"
228 (package-prefix *package*)
229 (substitute #\_ #\- (string-downcase type)))))
230
21299acf 231
232
233;;;; Metaclass for subclasses of ginstance
234
235(eval-when (:compile-toplevel :load-toplevel :execute)
236 (defclass ginstance-class (proxy-class)
237 ()))
238
239
13d35622 240(defmethod shared-initialize ((class ginstance-class) names &rest initargs &key name gtype)
21299acf 241 (declare (ignore names))
242 (let* ((class-name (or name (class-name class)))
dfa4f314 243 (type-number
244 (or
245 (find-type-number class-name)
246 (register-type class-name
247 (or (first gtype) (default-type-init-name class-name))))))
13d35622 248 (if (getf initargs :size)
249 (call-next-method)
250 (let ((size (type-instance-size type-number)))
251 (apply #'call-next-method class names :size (list size) initargs)))))
4de90d10 252
253
21299acf 254(defmethod validate-superclass ((class ginstance-class) (super standard-class))
255 (subtypep (class-name super) 'ginstance))
256
560af5c5 257
93aa67db 258;;;; Superclass for wrapping types in the glib type system
560af5c5 259
260(eval-when (:compile-toplevel :load-toplevel :execute)
93aa67db 261 (defclass ginstance (proxy)
d4b21b08 262 ((class :allocation :alien :type pointer))
263 (:metaclass proxy-class)))
560af5c5 264
895c9a9e 265(defun %type-number-of-ginstance (location)
93aa67db 266 (let ((class (sap-ref-sap location 0)))
895c9a9e 267 (sap-ref-32 class 0)))
560af5c5 268
d168bafd 269(defmethod ensure-proxy-instance ((class ginstance-class) location)
270 (declare (ignore class))
895c9a9e 271 (let ((class (labels ((find-known-class (type-number)
272 (or
273 (find-class (type-from-number type-number) nil)
274 (unless (zerop type-number)
275 (find-known-class (type-parent type-number))))))
276 (find-known-class (%type-number-of-ginstance location)))))
d168bafd 277 (if class
278 (make-instance class :location (reference-foreign class location))
895c9a9e 279 (error "Object at ~A has an unkown type number: ~A"
280 location (%type-number-of-ginstance location)))))
560af5c5 281
9ca5565a 282(defmethod copy-from-alien-form (location (class ginstance-class) &rest args)
283 (declare (ignore location class args))
284 (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."))
285
286(defmethod copy-from-alien-function ((class ginstance-class) &rest args)
287 (declare (ignore class args))
288 (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."))
289
290(defmethod reader-function ((class ginstance-class) &rest args)
291 (declare (ignore args))
292 #'(lambda (location &optional (offset 0))
293 (ensure-proxy-instance class (sap-ref-sap location offset))))
294
560af5c5 295
d4b21b08 296;;;; Registering fundamental types
297
63752532 298(register-type 'nil "void")
d4b21b08 299(register-type 'pointer "gpointer")
300(register-type 'char "gchar")
301(register-type 'unsigned-char "guchar")
302(register-type 'boolean "gboolean")
d4b21b08 303(register-type 'int "gint")
73383a9e 304(register-type-alias 'integer 'int)
dfa4f314 305(register-type-alias 'fixnum 'int)
d4b21b08 306(register-type 'unsigned-int "guint")
307(register-type 'long "glong")
308(register-type 'unsigned-long "gulong")
309(register-type 'single-float "gfloat")
310(register-type 'double-float "gdouble")
a8cb9408 311(register-type 'pathname "gchararray")
4de90d10 312(register-type 'string "gchararray")
d4b21b08 313
314
62f12808 315;;;; Introspection of type information
d4b21b08 316
e77e7713 317(defvar *derivable-type-info* (make-hash-table))
d4b21b08 318
62f12808 319(defun register-derivable-type (type id expander &optional dependencies)
d4b21b08 320 (register-type type id)
e77e7713 321 (let ((type-number (register-type type id)))
62f12808 322 (setf
323 (gethash type-number *derivable-type-info*)
324 (list expander dependencies))))
d4b21b08 325
4de90d10 326(defun find-type-info (type)
327 (dolist (super (cdr (type-hierarchy type)))
e77e7713 328 (let ((info (gethash super *derivable-type-info*)))
4de90d10 329 (return-if info))))
330
62f12808 331(defun expand-type-definition (type forward-p options)
332 (let ((expander (first (find-type-info type))))
333 (funcall expander (find-type-number type t) forward-p options)))
d4b21b08 334
d4b21b08 335(defbinding type-parent (type) type-number
336 ((find-type-number type t) type-number))
337
338(defun supertype (type)
339 (type-from-number (type-parent type)))
340
337933d8 341(defbinding %type-interfaces (type) pointer
342 ((find-type-number type t) type-number)
343 (n-interfaces unsigned-int :out))
344
345(defun type-interfaces (type)
346 (multiple-value-bind (array length) (%type-interfaces type)
347 (unwind-protect
d168bafd 348 (map-c-vector 'list #'identity array 'type-number length)
337933d8 349 (deallocate-memory array))))
350
351(defun implements (type)
352 (mapcar #'type-from-number (type-interfaces type)))
353
d4b21b08 354(defun type-hierarchy (type)
355 (let ((type-number (find-type-number type t)))
356 (unless (= type-number 0)
357 (cons type-number (type-hierarchy (type-parent type-number))))))
358
359(defbinding (type-is-p "g_type_is_a") (type super) boolean
360 ((find-type-number type) type-number)
361 ((find-type-number super) type-number))
362
363(defbinding %type-children () pointer
364 (type-number type-number)
365 (num-children unsigned-int :out))
366
367(defun map-subtypes (function type &optional prefix)
368 (let ((type-number (find-type-number type t)))
369 (multiple-value-bind (array length) (%type-children type-number)
370 (unwind-protect
d168bafd 371 (map-c-vector
d4b21b08 372 'nil
373 #'(lambda (type-number)
374 (when (or
375 (not prefix)
dfa4f314 376 (string-prefix-p prefix (find-foreign-type-name type-number)))
d4b21b08 377 (funcall function type-number))
378 (map-subtypes function type-number prefix))
379 array 'type-number length)
380 (deallocate-memory array)))))
381
382(defun find-types (prefix)
383 (let ((type-list nil))
e77e7713 384 (maphash
385 #'(lambda (type-number expander)
386 (declare (ignore expander))
387 (map-subtypes
388 #'(lambda (type-number)
389 (pushnew type-number type-list))
390 type-number prefix))
391 *derivable-type-info*)
d4b21b08 392 type-list))
393
62f12808 394(defun find-type-dependencies (type)
395 (let ((list-dependencies (second (find-type-info type))))
396 (when list-dependencies
397 (funcall list-dependencies (find-type-number type t)))))
398
399(defun %sort-types-topologicaly (types)
21299acf 400 (let ((partial-sorted
401 (sort
402 (mapcar
403 #'(lambda (type)
404 (cons type (remove-if #'(lambda (dep)
405 (not (find dep types)))
406 (find-type-dependencies type))))
407 types)
408 #'(lambda (type1 type2) (type-is-p type2 type1)) :key #'car))
62f12808 409 (sorted ()))
410
411 (loop
21299acf 412 as tmp = partial-sorted then (or (rest tmp) partial-sorted)
62f12808 413 while tmp
414 do (destructuring-bind (type . dependencies) (first tmp)
d4b21b08 415 (cond
62f12808 416 ((every #'(lambda (dep)
21299acf 417 (assoc dep sorted))
62f12808 418 dependencies)
21299acf 419 (push (cons type nil) sorted) ; no forward definition needed
420 (setq partial-sorted (delete type partial-sorted :key #'first)))
62f12808 421 ((some #'(lambda (dep)
422 (find type (find-type-dependencies dep)))
423 dependencies)
21299acf 424 (push (cons type t) sorted) ; forward definition needed
425 (setq partial-sorted (delete type partial-sorted :key #'first))))))
426 (nreverse sorted)))
d4b21b08 427
428
429(defun expand-type-definitions (prefix &optional args)
dfa4f314 430 (flet ((type-options (type-number)
431 (let ((name (find-foreign-type-name type-number)))
4de90d10 432 (cdr (assoc name args :test #'string=)))))
d4b21b08 433
e77e7713 434 (let ((type-list
435 (delete-if
436 #'(lambda (type-number)
dfa4f314 437 (let ((name (find-foreign-type-name type-number)))
e77e7713 438 (or
439 (getf (type-options type-number) :ignore)
440 (find-if
441 #'(lambda (options)
442 (and
443 (string-prefix-p (first options) name)
80a09c29 444 (getf (cdr options) :ignore-prefix)
445 (not (some
446 #'(lambda (exception)
447 (string= name exception))
448 (getf (cdr options) :except)))))
e77e7713 449 args))))
450 (find-types prefix))))
21299acf 451
e77e7713 452 (dolist (type-number type-list)
dfa4f314 453 (let ((name (find-foreign-type-name type-number)))
e77e7713 454 (register-type
455 (getf (type-options type-number) :type (default-type-name name))
735a29da 456 (register-type-as type-number))))
21299acf 457
458 (let ((sorted-type-list (%sort-types-topologicaly type-list)))
62f12808 459 `(progn
460 ,@(mapcar
21299acf 461 #'(lambda (pair)
462 (destructuring-bind (type . forward-p) pair
463 (expand-type-definition type forward-p (type-options type))))
464 sorted-type-list)
62f12808 465 ,@(mapcar
21299acf 466 #'(lambda (pair)
467 (destructuring-bind (type . forward-p) pair
468 (when forward-p
469 (expand-type-definition type nil (type-options type)))))
62f12808 470 sorted-type-list))))))
e77e7713 471
d4b21b08 472(defmacro define-types-by-introspection (prefix &rest args)
4de90d10 473 (expand-type-definitions prefix args))
21299acf 474
475
476;;;; Initialize all non static types in GObject
477
478(init-types-in-library #.(concatenate 'string (pkg-config:pkg-variable "glib-2.0" "libdir") "/libgobject-2.0.so"))