1 ;; Common Lisp bindings for GTK+ v2.0
2 ;; Copyright (C) 2000-2001 Espen S. Johnsen <esj@stud.cs.uit.no>
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.
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.
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
18 ;; $Id: gtype.lisp,v 1.16 2002-03-24 12:56:03 espen Exp $
26 (deftype type-number () '(unsigned 32))
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)))
37 (defbinding %type-query () nil
41 (defun type-query (type)
42 (let ((query (make-instance 'type-query)))
43 (%type-query (find-type-number type t) query)
46 (defun type-instance-size (type)
47 (slot-value (type-query type) 'instance-size))
49 (defun type-class-size (type)
50 (slot-value (type-query type) 'class-size))
52 (defbinding type-class-ref (type) pointer
53 ((find-type-number type t) type-number))
55 (defbinding type-class-unref (type) nil
56 ((find-type-number type t) type-number))
58 (defbinding type-class-peek (type) pointer
59 ((find-type-number type t) type-number))
62 ;;;; Mapping between lisp types and glib types
64 (defvar *type-to-number-hash* (make-hash-table))
65 (defvar *number-to-type-hash* (make-hash-table))
67 (defun register-type (type 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)
76 (defbinding %type-from-name () type-number
79 (defun find-type-number (type &optional error)
83 (let ((type-number (%type-from-name type)))
85 ((and (zerop type-number) error)
86 (error "Invalid alien type name: ~A" type))
87 ((zerop type-number) nil)
90 (let ((type-number (gethash type *type-to-number-hash*)))
93 (and error (error "Type not registered: ~A" type)))))
94 (pcl::class (find-type-number (class-name type) error))))
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)))
102 (error "Type number not registered: ~A (~A)" type-number name)
103 (error "Invalid type number: ~A" type-number))))
106 (defun type-from-name (name)
108 (string (type-from-number (find-type-number name t)))))
110 (defbinding (find-type-name "g_type_name") (type) string
111 ((find-type-number type t) type-number))
113 (defun type-number-of (object)
114 (find-type-number (type-of object) t))
116 (defun init-type (init)
119 (funcall (mkbinding fname 'type-number)))
122 (defun %init-types-in-library (pathname ignore)
123 (let ((process (ext:run-program
124 "nm" (list "-D" (namestring (truename pathname)))
125 :output :stream :wait nil))
127 (labels ((read-symbols ()
128 (let ((line (read-line (ext:process-output process) nil)))
130 (let ((symbol (subseq line 11)))
132 (search "_get_type" symbol)
133 (not (member symbol ignore :test #'string=)))
134 (push symbol fnames)))
137 (ext:process-close process)
138 `(init-type ',fnames))))
140 (defmacro init-types-in-library (filename &key ignore)
141 (%init-types-in-library
142 (format nil "~A/~A" *gtk-library-path* filename) ignore))
146 ;;;; Superclass for wrapping types in the glib type system
148 (eval-when (:compile-toplevel :load-toplevel :execute)
149 (defclass ginstance (proxy)
150 ((class :allocation :alien :type pointer))
151 (:metaclass proxy-class)))
153 (defun %type-of-ginstance (location)
154 (let ((class (sap-ref-sap location 0)))
155 (type-from-number (sap-ref-unsigned class 0))))
157 (deftype-method translate-from-alien
158 ginstance (type-spec location &optional weak-ref)
159 (declare (ignore type-spec))
160 `(let ((location ,location))
161 (unless (null-pointer-p location)
162 (ensure-proxy-instance
163 (%type-of-ginstance location) location ,weak-ref))))
167 ;;;; Metaclass for subclasses of ginstance
169 (eval-when (:compile-toplevel :load-toplevel :execute)
170 (defclass ginstance-class (proxy-class)))
173 (defmethod shared-initialize ((class ginstance-class) names
174 &rest initargs &key name alien-name
176 (declare (ignore initargs names))
177 (let* ((class-name (or name (class-name class)))
180 (or (first alien-name) (default-alien-type-name class-name)) t)))
181 (register-type class-name type-number)
182 (let ((size (or size (type-instance-size type-number))))
183 (declare (special size))
187 (let ((ref (mkbinding (first ref) 'pointer 'pointer)))
189 (slot-value class 'copy)
190 #'(lambda (type location)
191 (declare (ignore type))
192 (funcall ref location)))))
194 (let ((unref (mkbinding (first unref) 'nil 'pointer)))
196 (slot-value class 'free)
197 #'(lambda (type location)
198 (declare (ignore type))
199 (funcall unref location))))))
202 (defmethod validate-superclass
203 ((class ginstance-class) (super pcl::standard-class))
204 (subtypep (class-name super) 'ginstance))
207 ;;;; Registering fundamental types
209 (register-type 'pointer "gpointer")
210 (register-type 'char "gchar")
211 (register-type 'unsigned-char "guchar")
212 (register-type 'boolean "gboolean")
213 (register-type 'fixnum "gint")
214 (register-type 'int "gint")
215 (register-type 'unsigned-int "guint")
216 (register-type 'long "glong")
217 (register-type 'unsigned-long "gulong")
218 (register-type 'single-float "gfloat")
219 (register-type 'double-float "gdouble")
220 (register-type 'string "gchararray")
225 (defvar *derivable-type-info* (make-hash-table))
227 (defun register-derivable-type (type id expander)
228 (register-type type id)
229 (let ((type-number (register-type type id)))
230 (setf (gethash type-number *derivable-type-info*) expander)))
232 (defun find-type-info (type)
233 (dolist (super (cdr (type-hierarchy type)))
234 (let ((info (gethash super *derivable-type-info*)))
237 (defun expand-type-definition (type options)
238 (let ((expander (find-type-info type)))
239 (funcall expander (find-type-number type t) options)))
241 (defbinding type-parent (type) type-number
242 ((find-type-number type t) type-number))
244 (defun supertype (type)
245 (type-from-number (type-parent type)))
247 (defbinding %type-interfaces (type) pointer
248 ((find-type-number type t) type-number)
249 (n-interfaces unsigned-int :out))
251 (defun type-interfaces (type)
252 (multiple-value-bind (array length) (%type-interfaces type)
254 (map-c-array 'list #'identity array 'type-number length)
255 (deallocate-memory array))))
257 (defun implements (type)
258 (mapcar #'type-from-number (type-interfaces type)))
260 (defun type-hierarchy (type)
261 (let ((type-number (find-type-number type t)))
262 (unless (= type-number 0)
263 (cons type-number (type-hierarchy (type-parent type-number))))))
265 (defbinding (type-is-p "g_type_is_a") (type super) boolean
266 ((find-type-number type) type-number)
267 ((find-type-number super) type-number))
269 (defbinding %type-children () pointer
270 (type-number type-number)
271 (num-children unsigned-int :out))
273 (defun map-subtypes (function type &optional prefix)
274 (let ((type-number (find-type-number type t)))
275 (multiple-value-bind (array length) (%type-children type-number)
279 #'(lambda (type-number)
282 (string-prefix-p prefix (find-type-name type-number)))
283 (funcall function type-number))
284 (map-subtypes function type-number prefix))
285 array 'type-number length)
286 (deallocate-memory array)))))
288 (defun find-types (prefix)
289 (let ((type-list nil))
291 #'(lambda (type-number expander)
292 (declare (ignore expander))
294 #'(lambda (type-number)
295 (pushnew type-number type-list))
297 *derivable-type-info*)
300 (defun %sort-types-topologicaly (unsorted)
302 (loop while unsorted do
303 (dolist (type unsorted)
305 (append (rest (type-hierarchy type)) (type-interfaces type))))
309 (setq unsorted (delete type unsorted)))
311 (unless (dolist (dep dependencies)
312 (when (find type (rest (type-hierarchy dep)))
313 (error "Cyclic type dependencie"))
314 (return-if (find dep unsorted)))
316 (setq unsorted (delete type unsorted))))))))
320 (defun expand-type-definitions (prefix &optional args)
321 (flet ((type-options (type-number)
322 (let ((name (find-type-name type-number)))
323 (cdr (assoc name args :test #'string=)))))
327 #'(lambda (type-number)
328 (let ((name (find-type-name type-number)))
330 (getf (type-options type-number) :ignore)
334 (string-prefix-p (first options) name)
335 (getf (cdr options) :ignore-prefix)
337 #'(lambda (exception)
338 (string= name exception))
339 (getf (cdr options) :except)))))
341 (find-types prefix))))
343 (dolist (type-number type-list)
344 (let ((name (find-type-name type-number)))
346 (getf (type-options type-number) :type (default-type-name name))
352 (expand-type-definition type (type-options type)))
353 (%sort-types-topologicaly type-list))))))
355 (defmacro define-types-by-introspection (prefix &rest args)
356 (expand-type-definitions prefix args))