chiark / gitweb /
Added simple list test
[clg] / glib / gtype.lisp
... / ...
CommitLineData
1;; Common Lisp bindings for GTK+ v2.0
2;; Copyright (C) 2000-2001 Espen S. Johnsen <esj@stud.cs.uit.no>
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
18;; $Id: gtype.lisp,v 1.20 2004-11-13 16:37:09 espen Exp $
19
20(in-package "GLIB")
21
22(use-prefix "g")
23
24;; Initialize the glib type system
25(defbinding type-init () nil)
26(type-init)
27
28(deftype type-number () '(unsigned 32))
29
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))
51 `(type-from-number ,type-number t))
52
53(defmethod from-alien-function ((type (eql 'gtype)) &rest args)
54 (declare (ignore type args))
55 #'(lambda (type-number)
56 (type-from-number type-number t)))
57
58(defmethod writer-function ((type (eql 'gtype)) &rest args)
59 (declare (ignore type))
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)
65 (declare (ignore type))
66 (let ((reader (reader-function 'type-number)))
67 #'(lambda (location &optional (offset 0))
68 (type-from-number (funcall reader location offset) t))))
69
70
71(eval-when (:compile-toplevel :load-toplevel :execute)
72 (defclass type-query (struct)
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))
77 (:metaclass struct-class)))
78
79
80(defbinding type-query (type) nil
81 ((find-type-number type t) type-number)
82 ((make-instance 'type-query) type-query :return))
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))
89
90(defbinding type-class-ref (type) pointer
91 ((find-type-number type t) type-number))
92
93(defbinding type-class-unref (type) nil
94 ((find-type-number type t) type-number))
95
96(defbinding type-class-peek (type) pointer
97 ((find-type-number type t) type-number))
98
99
100;;;; Mapping between lisp types and glib types
101
102(defvar *type-to-number-hash* (make-hash-table))
103(defvar *number-to-type-hash* (make-hash-table))
104
105(defun register-type (type id)
106 (let ((type-number
107 (etypecase id
108 (integer id)
109 (string (find-type-number id t))
110 (symbol (gethash id *type-to-number-hash*)))))
111 (setf (gethash type *type-to-number-hash*) type-number)
112 (unless (symbolp id)
113 (setf (gethash type-number *number-to-type-hash*) type))
114 type-number))
115
116(defbinding %type-from-name () type-number
117 (name string))
118
119(defun find-type-number (type &optional error)
120 (etypecase type
121 (integer type)
122 (string
123 (let ((type-number (%type-from-name type)))
124 (cond
125 ((and (zerop type-number) error)
126 (error "Invalid gtype name: ~A" type))
127 ((zerop type-number) nil)
128 (t type-number))))
129 (symbol
130 (let ((type-number (gethash type *type-to-number-hash*)))
131 (or
132 type-number
133 (and error (error "Type not registered: ~A" type)))))
134 (pcl::class (find-type-number (class-name type) error))))
135
136(defun type-from-number (type-number &optional error)
137 (multiple-value-bind (type found)
138 (gethash type-number *number-to-type-hash*)
139 (when (and error (not found))
140 (let ((name (find-type-name type-number)))
141 (if name
142 (error "Type number not registered: ~A (~A)" type-number name)
143 (error "Invalid type number: ~A" type-number))))
144 type))
145
146(defun type-from-name (name)
147 (etypecase name
148 (string (type-from-number (find-type-number name t)))))
149
150(defbinding (find-type-name "g_type_name") (type) string
151 ((find-type-number type t) type-number))
152
153(defun type-number-of (object)
154 (find-type-number (type-of object) t))
155
156(defun init-type (init)
157 (mapc
158 #'(lambda (fname)
159 (funcall (mkbinding fname 'type-number)))
160 (mklist init)))
161
162(defun %init-types-in-library (pathname prefix ignore)
163 (let ((process (ext:run-program
164 "nm" (list "-D" (namestring (truename pathname)))
165 :output :stream :wait nil))
166 (fnames ()))
167 (labels ((read-symbols ()
168 (let ((line (read-line (ext:process-output process) nil)))
169 (when line
170 (let ((symbol (subseq line 11)))
171 (when (and
172 (> (length symbol) (length prefix))
173 (string= prefix symbol :end2 (length prefix))
174 (search "_get_type" symbol)
175 (not (member symbol ignore :test #'string=)))
176 (push symbol fnames)))
177 (read-symbols)))))
178 (read-symbols)
179 (ext:process-close process)
180 `(init-type ',fnames))))
181
182(defmacro init-types-in-library (filename &key (prefix "") ignore)
183 (%init-types-in-library filename prefix ignore))
184
185
186
187;;;; Superclass for wrapping types in the glib type system
188
189(eval-when (:compile-toplevel :load-toplevel :execute)
190 (defclass ginstance (proxy)
191 ((class :allocation :alien :type pointer))
192 (:metaclass proxy-class)))
193
194(defun %type-of-ginstance (location)
195 (let ((class (sap-ref-sap location 0)))
196 (type-from-number (sap-ref-32 class 0))))
197
198(defmethod ensure-proxy-instance ((class ginstance-class) location)
199 (declare (ignore class))
200 (let ((class (find-class (%type-of-ginstance location))))
201 (if class
202 (make-instance class :location (reference-foreign class location))
203 ;; TODO: (make-instance 'ginstance ...)
204 location)))
205
206
207;;;; Metaclass for subclasses of ginstance
208
209(eval-when (:compile-toplevel :load-toplevel :execute)
210 (defclass ginstance-class (proxy-class)
211 ()))
212
213
214(defmethod shared-initialize ((class ginstance-class) names
215 &rest initargs &key name alien-name)
216 (declare (ignore names))
217 (let* ((class-name (or name (class-name class)))
218 (type-number
219 (find-type-number
220 (or (first alien-name) (default-alien-type-name class-name)) t)))
221 (register-type class-name type-number)
222 (if (getf initargs :size)
223 (call-next-method)
224 (let ((size (type-instance-size type-number)))
225 (apply #'call-next-method class names :size (list size) initargs)))))
226
227
228(defmethod validate-superclass ((class ginstance-class) (super standard-class))
229 (subtypep (class-name super) 'ginstance))
230
231
232;;;; Registering fundamental types
233
234(register-type 'pointer "gpointer")
235(register-type 'char "gchar")
236(register-type 'unsigned-char "guchar")
237(register-type 'boolean "gboolean")
238(register-type 'fixnum "gint")
239(register-type 'int "gint")
240(register-type 'unsigned-int "guint")
241(register-type 'long "glong")
242(register-type 'unsigned-long "gulong")
243(register-type 'single-float "gfloat")
244(register-type 'double-float "gdouble")
245(register-type 'string "gchararray")
246
247
248;;;;
249
250(defvar *derivable-type-info* (make-hash-table))
251
252(defun register-derivable-type (type id expander)
253 (register-type type id)
254 (let ((type-number (register-type type id)))
255 (setf (gethash type-number *derivable-type-info*) expander)))
256
257(defun find-type-info (type)
258 (dolist (super (cdr (type-hierarchy type)))
259 (let ((info (gethash super *derivable-type-info*)))
260 (return-if info))))
261
262(defun expand-type-definition (type options)
263 (let ((expander (find-type-info type)))
264 (funcall expander (find-type-number type t) options)))
265
266(defbinding type-parent (type) type-number
267 ((find-type-number type t) type-number))
268
269(defun supertype (type)
270 (type-from-number (type-parent type)))
271
272(defbinding %type-interfaces (type) pointer
273 ((find-type-number type t) type-number)
274 (n-interfaces unsigned-int :out))
275
276(defun type-interfaces (type)
277 (multiple-value-bind (array length) (%type-interfaces type)
278 (unwind-protect
279 (map-c-vector 'list #'identity array 'type-number length)
280 (deallocate-memory array))))
281
282(defun implements (type)
283 (mapcar #'type-from-number (type-interfaces type)))
284
285(defun type-hierarchy (type)
286 (let ((type-number (find-type-number type t)))
287 (unless (= type-number 0)
288 (cons type-number (type-hierarchy (type-parent type-number))))))
289
290(defbinding (type-is-p "g_type_is_a") (type super) boolean
291 ((find-type-number type) type-number)
292 ((find-type-number super) type-number))
293
294(defbinding %type-children () pointer
295 (type-number type-number)
296 (num-children unsigned-int :out))
297
298(defun map-subtypes (function type &optional prefix)
299 (let ((type-number (find-type-number type t)))
300 (multiple-value-bind (array length) (%type-children type-number)
301 (unwind-protect
302 (map-c-vector
303 'nil
304 #'(lambda (type-number)
305 (when (or
306 (not prefix)
307 (string-prefix-p prefix (find-type-name type-number)))
308 (funcall function type-number))
309 (map-subtypes function type-number prefix))
310 array 'type-number length)
311 (deallocate-memory array)))))
312
313(defun find-types (prefix)
314 (let ((type-list nil))
315 (maphash
316 #'(lambda (type-number expander)
317 (declare (ignore expander))
318 (map-subtypes
319 #'(lambda (type-number)
320 (pushnew type-number type-list))
321 type-number prefix))
322 *derivable-type-info*)
323 type-list))
324
325(defun %sort-types-topologicaly (unsorted)
326 (let ((sorted ()))
327 (loop while unsorted do
328 (dolist (type unsorted)
329 (let ((dependencies
330 (append (rest (type-hierarchy type)) (type-interfaces type))))
331 (cond
332 ((null dependencies)
333 (push type sorted)
334 (setq unsorted (delete type unsorted)))
335 (t
336 (unless (dolist (dep dependencies)
337 (when (find type (rest (type-hierarchy dep)))
338 (error "Cyclic type dependencie"))
339 (return-if (find dep unsorted)))
340 (push type sorted)
341 (setq unsorted (delete type unsorted))))))))
342 (nreverse sorted)))
343
344
345(defun expand-type-definitions (prefix &optional args)
346 (flet ((type-options (type-number)
347 (let ((name (find-type-name type-number)))
348 (cdr (assoc name args :test #'string=)))))
349
350 (let ((type-list
351 (delete-if
352 #'(lambda (type-number)
353 (let ((name (find-type-name type-number)))
354 (or
355 (getf (type-options type-number) :ignore)
356 (find-if
357 #'(lambda (options)
358 (and
359 (string-prefix-p (first options) name)
360 (getf (cdr options) :ignore-prefix)
361 (not (some
362 #'(lambda (exception)
363 (string= name exception))
364 (getf (cdr options) :except)))))
365 args))))
366 (find-types prefix))))
367
368 (dolist (type-number type-list)
369 (let ((name (find-type-name type-number)))
370 (register-type
371 (getf (type-options type-number) :type (default-type-name name))
372 type-number)))
373
374 `(progn
375 ,@(mapcar
376 #'(lambda (type)
377 (expand-type-definition type (type-options type)))
378 (%sort-types-topologicaly type-list))))))
379
380(defmacro define-types-by-introspection (prefix &rest args)
381 (expand-type-definitions prefix args))
382
383
384