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.27 2005/03/06 17:26:23 espen Exp $
24 ;; Initialize the glib type system
25 (defbinding type-init () nil)
28 (deftype type-number () '(unsigned 32))
30 (deftype gtype () 'symbol)
32 (defmethod alien-type ((type (eql 'gtype)) &rest args)
33 (declare (ignore type args))
34 (alien-type 'type-number))
36 (defmethod size-of ((type (eql 'gtype)) &rest args)
37 (declare (ignore type args))
38 (size-of 'type-number))
40 (defmethod to-alien-form (gtype (type (eql 'gtype)) &rest args)
41 (declare (ignore type args))
42 `(find-type-number ,gtype t))
44 (defmethod to-alien-function ((type (eql 'gtype)) &rest args)
45 (declare (ignore type args))
47 (find-type-number gtype t)))
49 (defmethod from-alien-form (type-number (type (eql 'gtype)) &rest args)
50 (declare (ignore type args))
51 `(type-from-number ,type-number))
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)))
58 (defmethod writer-function ((type (eql 'gtype)) &rest args)
59 (declare (ignore type args))
60 (let ((writer (writer-function 'type-number)))
61 #'(lambda (gtype location &optional (offset 0))
62 (funcall writer (find-type-number gtype t) location offset))))
64 (defmethod reader-function ((type (eql 'gtype)) &rest args)
65 (declare (ignore type args))
66 (let ((reader (reader-function 'type-number)))
67 #'(lambda (location &optional (offset 0))
68 (type-from-number (funcall reader location offset)))))
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)))
80 (defbinding type-query (type) nil
81 ((find-type-number type t) type-number)
82 ((make-instance 'type-query) type-query :return))
84 (defun type-instance-size (type)
85 (slot-value (type-query type) 'instance-size))
87 (defun type-class-size (type)
88 (slot-value (type-query type) 'class-size))
90 (defbinding type-class-ref (type) pointer
91 ((find-type-number type t) type-number))
93 (defbinding type-class-unref (type) nil
94 ((find-type-number type t) type-number))
96 (defbinding type-class-peek (type) pointer
97 ((find-type-number type t) type-number))
100 ;;;; Mapping between lisp types and glib types
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))
107 (defbinding %type-from-name () type-number
110 (defun type-number-from-glib-name (name &optional (error-p t))
111 (let ((type-number (%type-from-name name)))
113 ((not (zerop type-number)) type-number)
114 (error-p (error "Invalid gtype name: ~A" name)))))
116 (defun register-type (type id)
117 (pushnew (cons type id) *registered-types* :key #'car)
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)
126 (defun register-type-alias (type alias)
127 (pushnew (cons type alias) *registered-type-aliases* :key #'car)
129 (gethash type *lisp-type-to-type-number*)
130 (find-type-number alias t)))
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)))
139 (mapc #'(lambda (type)
140 (register-type-alias (car type) (cdr type)))
141 *registered-type-aliases*))
143 (pushnew 'reinitialize-all-types
144 #+cmu *after-save-initializations*
148 (pushnew 'system::reinitialize-global-table ; we shouldn't have to do this?
149 *after-save-initializations*)
152 (defun find-type-number (type &optional error-p)
155 (string (type-number-from-glib-name type error-p))
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))))
162 (defun type-from-number (type-number &optional error)
163 (multiple-value-bind (type found)
164 (gethash type-number *type-number-to-lisp-type*)
165 (when (and error (not found))
166 (let ((name (find-foreign-type-name type-number)))
168 (error "Type number not registered: ~A (~A)" type-number name)
169 (error "Invalid type number: ~A" type-number))))
172 (defbinding (find-foreign-type-name "g_type_name") (type) (copy-of string)
173 ((find-type-number type t) type-number))
175 (defun type-number-of (object)
176 (find-type-number (type-of object) t))
178 (eval-when (:compile-toplevel :load-toplevel :execute)
179 (defvar *type-initializers* ())
180 (defun %find-types-in-library (pathname prefixes ignore)
181 (let ((process (run-program
182 "/usr/bin/nm" (list "--defined-only" "-D" (namestring (truename pathname)))
183 :output :stream :wait nil)))
186 as symbol = (let ((line (read-line (process-output process) nil)))
187 (when line (subseq line 11)))
190 (> (length symbol) 9)
193 (some #'(lambda (prefix)
195 (> (length symbol) (length prefix))
196 (string= prefix symbol :end2 (length prefix))))
198 (string= "_get_type" symbol :start2 (- (length symbol) 9))
199 (not (member symbol ignore :test #'string=)))
201 (process-close process)))))
204 (defmacro init-types-in-library (filename &key (prefix "") ignore)
205 (let ((names (%find-types-in-library filename prefix ignore)))
207 ,@(mapcar #'(lambda (name)
209 (defbinding (,(intern name) ,name) () type-number)
211 (pushnew ',(intern name) *type-initializers*)))
214 (defun find-type-init-function (type-number)
217 for type-init in *type-initializers*
218 when (= type-number (funcall type-init))
219 do (return type-init))
220 (error "Can't find init function for type number ~D" type-number)))
222 (defun default-type-init-name (type)
223 (find-symbol (format nil "~A_~A_get_type"
224 (package-prefix *package*)
225 (substitute #\_ #\- (string-downcase type)))))
229 ;;;; Metaclass for subclasses of ginstance
231 (eval-when (:compile-toplevel :load-toplevel :execute)
232 (defclass ginstance-class (proxy-class)
236 (defmethod shared-initialize ((class ginstance-class) names &key name gtype)
237 (declare (ignore names))
238 (let* ((class-name (or name (class-name class)))
241 (find-type-number class-name)
242 (register-type class-name
243 (or (first gtype) (default-type-init-name class-name))))))
245 (when (slot-boundp class 'size)
246 (setf (slot-value class 'size) (type-instance-size type-number)))))
249 (defmethod validate-superclass ((class ginstance-class) (super standard-class))
250 (subtypep (class-name super) 'ginstance))
253 ;;;; Superclass for wrapping types in the glib type system
255 (eval-when (:compile-toplevel :load-toplevel :execute)
256 (defclass ginstance (proxy)
257 ((class :allocation :alien :type pointer))
258 (:metaclass proxy-class)))
260 (defun %type-of-ginstance (location)
261 (let ((class (sap-ref-sap location 0)))
262 (type-from-number (sap-ref-32 class 0))))
264 (defmethod ensure-proxy-instance ((class ginstance-class) location)
265 (declare (ignore class))
266 (let ((class (find-class (%type-of-ginstance location))))
268 (make-instance class :location (reference-foreign class location))
269 ;; TODO: (make-instance 'ginstance ...)
272 (defmethod copy-from-alien-form (location (class ginstance-class) &rest args)
273 (declare (ignore location class args))
274 (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."))
276 (defmethod copy-from-alien-function ((class ginstance-class) &rest args)
277 (declare (ignore class args))
278 (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."))
280 (defmethod reader-function ((class ginstance-class) &rest args)
281 (declare (ignore args))
282 #'(lambda (location &optional (offset 0))
283 (ensure-proxy-instance class (sap-ref-sap location offset))))
286 ;;;; Registering fundamental types
288 (register-type 'nil "void")
289 (register-type 'pointer "gpointer")
290 (register-type 'char "gchar")
291 (register-type 'unsigned-char "guchar")
292 (register-type 'boolean "gboolean")
293 (register-type 'int "gint")
294 (register-type-alias 'fixnum 'int)
295 (register-type 'unsigned-int "guint")
296 (register-type 'long "glong")
297 (register-type 'unsigned-long "gulong")
298 (register-type 'single-float "gfloat")
299 (register-type 'double-float "gdouble")
300 (register-type 'pathname "gchararray")
301 (register-type 'string "gchararray")
304 ;;;; Introspection of type information
306 (defvar *derivable-type-info* (make-hash-table))
308 (defun register-derivable-type (type id expander &optional dependencies)
309 (register-type type id)
310 (let ((type-number (register-type type id)))
312 (gethash type-number *derivable-type-info*)
313 (list expander dependencies))))
315 (defun find-type-info (type)
316 (dolist (super (cdr (type-hierarchy type)))
317 (let ((info (gethash super *derivable-type-info*)))
320 (defun expand-type-definition (type forward-p options)
321 (let ((expander (first (find-type-info type))))
322 (funcall expander (find-type-number type t) forward-p options)))
324 (defbinding type-parent (type) type-number
325 ((find-type-number type t) type-number))
327 (defun supertype (type)
328 (type-from-number (type-parent type)))
330 (defbinding %type-interfaces (type) pointer
331 ((find-type-number type t) type-number)
332 (n-interfaces unsigned-int :out))
334 (defun type-interfaces (type)
335 (multiple-value-bind (array length) (%type-interfaces type)
337 (map-c-vector 'list #'identity array 'type-number length)
338 (deallocate-memory array))))
340 (defun implements (type)
341 (mapcar #'type-from-number (type-interfaces type)))
343 (defun type-hierarchy (type)
344 (let ((type-number (find-type-number type t)))
345 (unless (= type-number 0)
346 (cons type-number (type-hierarchy (type-parent type-number))))))
348 (defbinding (type-is-p "g_type_is_a") (type super) boolean
349 ((find-type-number type) type-number)
350 ((find-type-number super) type-number))
352 (defbinding %type-children () pointer
353 (type-number type-number)
354 (num-children unsigned-int :out))
356 (defun map-subtypes (function type &optional prefix)
357 (let ((type-number (find-type-number type t)))
358 (multiple-value-bind (array length) (%type-children type-number)
362 #'(lambda (type-number)
365 (string-prefix-p prefix (find-foreign-type-name type-number)))
366 (funcall function type-number))
367 (map-subtypes function type-number prefix))
368 array 'type-number length)
369 (deallocate-memory array)))))
371 (defun find-types (prefix)
372 (let ((type-list nil))
374 #'(lambda (type-number expander)
375 (declare (ignore expander))
377 #'(lambda (type-number)
378 (pushnew type-number type-list))
380 *derivable-type-info*)
383 (defun find-type-dependencies (type)
384 (let ((list-dependencies (second (find-type-info type))))
385 (when list-dependencies
386 (funcall list-dependencies (find-type-number type t)))))
388 (defun %sort-types-topologicaly (types)
389 (let ((partial-sorted
393 (cons type (remove-if #'(lambda (dep)
394 (not (find dep types)))
395 (find-type-dependencies type))))
397 #'(lambda (type1 type2) (type-is-p type2 type1)) :key #'car))
401 as tmp = partial-sorted then (or (rest tmp) partial-sorted)
403 do (destructuring-bind (type . dependencies) (first tmp)
405 ((every #'(lambda (dep)
408 (push (cons type nil) sorted) ; no forward definition needed
409 (setq partial-sorted (delete type partial-sorted :key #'first)))
410 ((some #'(lambda (dep)
411 (find type (find-type-dependencies dep)))
413 (push (cons type t) sorted) ; forward definition needed
414 (setq partial-sorted (delete type partial-sorted :key #'first))))))
418 (defun expand-type-definitions (prefix &optional args)
419 (flet ((type-options (type-number)
420 (let ((name (find-foreign-type-name type-number)))
421 (cdr (assoc name args :test #'string=)))))
425 #'(lambda (type-number)
426 (let ((name (find-foreign-type-name type-number)))
428 (getf (type-options type-number) :ignore)
432 (string-prefix-p (first options) name)
433 (getf (cdr options) :ignore-prefix)
435 #'(lambda (exception)
436 (string= name exception))
437 (getf (cdr options) :except)))))
439 (find-types prefix))))
441 (dolist (type-number type-list)
442 (let ((name (find-foreign-type-name type-number)))
444 (getf (type-options type-number) :type (default-type-name name))
445 (find-type-init-function type-number))))
447 (let ((sorted-type-list (%sort-types-topologicaly type-list)))
451 (destructuring-bind (type . forward-p) pair
452 (expand-type-definition type forward-p (type-options type))))
456 (destructuring-bind (type . forward-p) pair
458 (expand-type-definition type nil (type-options type)))))
459 sorted-type-list))))))
461 (defmacro define-types-by-introspection (prefix &rest args)
462 (expand-type-definitions prefix args))
465 ;;;; Initialize all non static types in GObject
467 (init-types-in-library #.(concatenate 'string (pkg-config:pkg-variable "glib-2.0" "libdir") "/libgobject-2.0.so"))