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.13 2001/11/12 22:24:29 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" user::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 (defun type-hierarchy (type)
248 (let ((type-number (find-type-number type t)))
249 (unless (= type-number 0)
250 (cons type-number (type-hierarchy (type-parent type-number))))))
252 (defbinding (type-is-p "g_type_is_a") (type super) boolean
253 ((find-type-number type) type-number)
254 ((find-type-number super) type-number))
256 (defbinding %type-children () pointer
257 (type-number type-number)
258 (num-children unsigned-int :out))
260 (defun map-subtypes (function type &optional prefix)
261 (let ((type-number (find-type-number type t)))
262 (multiple-value-bind (array length) (%type-children type-number)
266 #'(lambda (type-number)
269 (string-prefix-p prefix (find-type-name type-number)))
270 (funcall function type-number))
271 (map-subtypes function type-number prefix))
272 array 'type-number length)
273 (deallocate-memory array)))))
275 (defun find-types (prefix)
276 (let ((type-list nil))
278 #'(lambda (type-number expander)
279 (declare (ignore expander))
281 #'(lambda (type-number)
282 (pushnew type-number type-list))
284 *derivable-type-info*)
287 (defun %sort-types-topologicaly (unsorted)
289 (loop while unsorted do
290 (dolist (type unsorted)
291 (let ((dependencies (rest (type-hierarchy type))))
295 (setq unsorted (delete type unsorted)))
297 (unless (dolist (dep dependencies)
298 (when (find type (rest (type-hierarchy dep)))
299 (error "Cyclic type dependencie"))
300 (return-if (find dep unsorted)))
302 (setq unsorted (delete type unsorted))))))))
306 (defun expand-type-definitions (prefix &optional args)
307 (flet ((type-options (type-number)
308 (let ((name (find-type-name type-number)))
309 (cdr (assoc name args :test #'string=)))))
313 #'(lambda (type-number)
314 (let ((name (find-type-name type-number)))
316 (getf (type-options type-number) :ignore)
320 (string-prefix-p (first options) name)
321 (getf (cdr options) :ignore-prefix)))
323 (find-types prefix))))
325 (dolist (type-number type-list)
326 (let ((name (find-type-name type-number)))
328 (getf (type-options type-number) :type (default-type-name name))
334 (expand-type-definition type (type-options type)))
335 (%sort-types-topologicaly type-list))))))
337 (defmacro define-types-by-introspection (prefix &rest args)
338 (expand-type-definitions prefix args))