Changed the topological sorting (which is not really needed anymore) to sort on hiera...
[clg] / glib / gtype.lisp
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.12 2001-10-21 21:50:18 espen Exp $
19
20 (in-package "GLIB")
21
22 (use-prefix "g")
23
24 ;;;;
25
26 (deftype type-number () '(unsigned 32))
27
28 (eval-when (:compile-toplevel :load-toplevel :execute)
29 (defclass type-query (struct)
30 ((type-number :allocation :alien :type type-number)
31 (name :allocation :alien :type string)
32 (class-size :allocation :alien :type unsigned-int)
33 (instance-size :allocation :alien :type unsigned-int))
34 (:metaclass proxy-class)))
35
36
37 (defbinding %type-query () nil
38 (type type-number)
39 (query type-query))
40
41 (defun type-query (type)
42 (let ((query (make-instance 'type-query)))
43 (%type-query (find-type-number type t) query)
44 query))
45
46 (defun type-instance-size (type)
47 (slot-value (type-query type) 'instance-size))
48
49 (defun type-class-size (type)
50 (slot-value (type-query type) 'class-size))
51
52 (defbinding type-class-ref (type) pointer
53 ((find-type-number type t) type-number))
54
55 (defbinding type-class-unref (type) nil
56 ((find-type-number type t) type-number))
57
58 (defbinding type-class-peek (type) pointer
59 ((find-type-number type t) type-number))
60
61
62 ;;;; Mapping between lisp types and glib types
63
64 (defvar *type-to-number-hash* (make-hash-table))
65 (defvar *number-to-type-hash* (make-hash-table))
66
67 (defun register-type (type id)
68 (let ((type-number
69 (etypecase id
70 (integer id)
71 (string (find-type-number id t)))))
72 (setf (gethash type *type-to-number-hash*) type-number)
73 (setf (gethash type-number *number-to-type-hash*) type)
74 type-number))
75
76 (defbinding %type-from-name () type-number
77 (name string))
78
79 (defun find-type-number (type &optional error)
80 (etypecase type
81 (integer type)
82 (string
83 (let ((type-number (%type-from-name type)))
84 (cond
85 ((and (zerop type-number) error)
86 (error "Invalid alien type name: ~A" type))
87 ((zerop type-number) nil)
88 (t type-number))))
89 (symbol
90 (let ((type-number (gethash type *type-to-number-hash*)))
91 (or
92 type-number
93 (and error (error "Type not registered: ~A" type)))))
94 (pcl::class (find-type-number (class-name type) error))))
95
96 (defun type-from-number (type-number &optional error)
97 (multiple-value-bind (type found)
98 (gethash type-number *number-to-type-hash*)
99 (when (and error (not found))
100 (let ((name (find-type-name type-number)))
101 (if name
102 (error "Type number not registered: ~A (~A)" type-number name)
103 (error "Invalid type number: ~A" type-number))))
104 type))
105
106 (defun type-from-name (name)
107 (etypecase name
108 (string (type-from-number (find-type-number name t)))))
109
110 (defbinding (find-type-name "g_type_name") (type) string
111 ((find-type-number type t) type-number))
112
113 (defun type-number-of (object)
114 (find-type-number (type-of object) t))
115
116 (defun init-type (init)
117 (mapc
118 #'(lambda (fname)
119 (funcall (mkbinding fname 'type-number)))
120 (mklist init)))
121
122 (defun %init-types-in-library (pathname ignore)
123 (let ((process (ext:run-program
124 "nm" (list (namestring (truename pathname)))
125 :output :stream :wait nil))
126 (fnames ()))
127 (labels ((read-symbols ()
128 (let ((line (read-line (ext:process-output process) nil)))
129 (when line
130 (let ((symbol (subseq line 11)))
131 (when (and
132 (search "_get_type" symbol)
133 (not (member symbol ignore :test #'string=)))
134 (push symbol fnames)))
135 (read-symbols)))))
136 (read-symbols)
137 (ext:process-close process)
138 `(init-type ',fnames))))
139
140 (defmacro init-types-in-library (pathname &key ignore)
141 (%init-types-in-library pathname ignore))
142
143
144
145 ;;;; Superclass for wrapping types in the glib type system
146
147 (eval-when (:compile-toplevel :load-toplevel :execute)
148 (defclass ginstance (proxy)
149 ((class :allocation :alien :type pointer))
150 (:metaclass proxy-class)))
151
152 (defun %type-of-ginstance (location)
153 (let ((class (sap-ref-sap location 0)))
154 (type-from-number (sap-ref-unsigned class 0))))
155
156 (deftype-method translate-from-alien
157 ginstance (type-spec location &optional weak-ref)
158 (declare (ignore type-spec))
159 `(let ((location ,location))
160 (unless (null-pointer-p location)
161 (ensure-proxy-instance
162 (%type-of-ginstance location) location ,weak-ref))))
163
164
165
166 ;;;; Metaclass for subclasses of ginstance
167
168 (eval-when (:compile-toplevel :load-toplevel :execute)
169 (defclass ginstance-class (proxy-class)))
170
171
172 (defmethod shared-initialize ((class ginstance-class) names
173 &rest initargs &key name alien-name
174 size ref unref)
175 (declare (ignore initargs names))
176 (let* ((class-name (or name (class-name class)))
177 (type-number
178 (find-type-number
179 (or (first alien-name) (default-alien-type-name class-name)) t)))
180 (register-type class-name type-number)
181 (let ((size (or size (type-instance-size type-number))))
182 (declare (special size))
183 (call-next-method)))
184
185 (when ref
186 (let ((ref (mkbinding (first ref) 'pointer 'pointer)))
187 (setf
188 (slot-value class 'copy)
189 #'(lambda (type location)
190 (declare (ignore type))
191 (funcall ref location)))))
192 (when unref
193 (let ((unref (mkbinding (first unref) 'nil 'pointer)))
194 (setf
195 (slot-value class 'free)
196 #'(lambda (type location)
197 (declare (ignore type))
198 (funcall unref location))))))
199
200
201 (defmethod validate-superclass
202 ((class ginstance-class) (super pcl::standard-class))
203 (subtypep (class-name super) 'ginstance))
204
205
206 ;;;; Registering fundamental types
207
208 (register-type 'pointer "gpointer")
209 (register-type 'char "gchar")
210 (register-type 'unsigned-char "guchar")
211 (register-type 'boolean "gboolean")
212 (register-type 'fixnum "gint")
213 (register-type 'int "gint")
214 (register-type 'unsigned-int "guint")
215 (register-type 'long "glong")
216 (register-type 'unsigned-long "gulong")
217 (register-type 'single-float "gfloat")
218 (register-type 'double-float "gdouble")
219 (register-type 'string "gchararray")
220
221
222 ;;;;
223
224 (defvar *derivable-type-info* (make-hash-table))
225
226 (defun register-derivable-type (type id expander)
227 (register-type type id)
228 (let ((type-number (register-type type id)))
229 (setf (gethash type-number *derivable-type-info*) expander)))
230
231 (defun find-type-info (type)
232 (dolist (super (cdr (type-hierarchy type)))
233 (let ((info (gethash super *derivable-type-info*)))
234 (return-if info))))
235
236 (defun expand-type-definition (type options)
237 (let ((expander (find-type-info type)))
238 (funcall expander (find-type-number type t) options)))
239
240 (defbinding type-parent (type) type-number
241 ((find-type-number type t) type-number))
242
243 (defun supertype (type)
244 (type-from-number (type-parent type)))
245
246 (defun type-hierarchy (type)
247 (let ((type-number (find-type-number type t)))
248 (unless (= type-number 0)
249 (cons type-number (type-hierarchy (type-parent type-number))))))
250
251 (defbinding (type-is-p "g_type_is_a") (type super) boolean
252 ((find-type-number type) type-number)
253 ((find-type-number super) type-number))
254
255 (defbinding %type-children () pointer
256 (type-number type-number)
257 (num-children unsigned-int :out))
258
259 (defun map-subtypes (function type &optional prefix)
260 (let ((type-number (find-type-number type t)))
261 (multiple-value-bind (array length) (%type-children type-number)
262 (unwind-protect
263 (map-c-array
264 'nil
265 #'(lambda (type-number)
266 (when (or
267 (not prefix)
268 (string-prefix-p prefix (find-type-name type-number)))
269 (funcall function type-number))
270 (map-subtypes function type-number prefix))
271 array 'type-number length)
272 (deallocate-memory array)))))
273
274 (defun find-types (prefix)
275 (let ((type-list nil))
276 (maphash
277 #'(lambda (type-number expander)
278 (declare (ignore expander))
279 (map-subtypes
280 #'(lambda (type-number)
281 (pushnew type-number type-list))
282 type-number prefix))
283 *derivable-type-info*)
284 type-list))
285
286 (defun %sort-types-topologicaly (unsorted)
287 (let ((sorted ()))
288 (loop while unsorted do
289 (dolist (type unsorted)
290 (let ((dependencies (rest (type-hierarchy type))))
291 (cond
292 ((null dependencies)
293 (push type sorted)
294 (setq unsorted (delete type unsorted)))
295 (t
296 (unless (dolist (dep dependencies)
297 (when (find type (rest (type-hierarchy dep)))
298 (error "Cyclic type dependencie"))
299 (return-if (find dep unsorted)))
300 (push type sorted)
301 (setq unsorted (delete type unsorted))))))))
302 (nreverse sorted)))
303
304
305 (defun expand-type-definitions (prefix &optional args)
306 (flet ((type-options (type-number)
307 (let ((name (find-type-name type-number)))
308 (cdr (assoc name args :test #'string=)))))
309
310 (let ((type-list
311 (delete-if
312 #'(lambda (type-number)
313 (let ((name (find-type-name type-number)))
314 (or
315 (getf (type-options type-number) :ignore)
316 (find-if
317 #'(lambda (options)
318 (and
319 (string-prefix-p (first options) name)
320 (getf (cdr options) :ignore-prefix)))
321 args))))
322 (find-types prefix))))
323
324 (dolist (type-number type-list)
325 (let ((name (find-type-name type-number)))
326 (register-type
327 (getf (type-options type-number) :type (default-type-name name))
328 type-number)))
329
330 `(progn
331 ,@(mapcar
332 #'(lambda (type)
333 (expand-type-definition type (type-options type)))
334 (%sort-types-topologicaly type-list))))))
335
336 (defmacro define-types-by-introspection (prefix &rest args)
337 (expand-type-definitions prefix args))
338
339
340