1 ;; Common Lisp bindings for GTK+ v2.x
2 ;; Copyright 2000-2005 Espen S. Johnsen <espen@users.sf.net>
4 ;; Permission is hereby granted, free of charge, to any person obtaining
5 ;; a copy of this software and associated documentation files (the
6 ;; "Software"), to deal in the Software without restriction, including
7 ;; without limitation the rights to use, copy, modify, merge, publish,
8 ;; distribute, sublicense, and/or sell copies of the Software, and to
9 ;; permit persons to whom the Software is furnished to do so, subject to
10 ;; the following conditions:
12 ;; The above copyright notice and this permission notice shall be
13 ;; included in all copies or substantial portions of the Software.
15 ;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 ;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 ;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 ;; IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 ;; CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 ;; TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 ;; SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 ;; $Id: gtype.lisp,v 1.34 2006/02/01 14:17:37 espen Exp $
29 ;; Initialize the glib type system
30 (defbinding type-init () nil)
33 (deftype type-number () '(unsigned 32))
35 (deftype gtype () 'symbol)
37 (defmethod alien-type ((type (eql 'gtype)) &rest args)
38 (declare (ignore type args))
39 (alien-type 'type-number))
41 (defmethod size-of ((type (eql 'gtype)) &rest args)
42 (declare (ignore type args))
43 (size-of 'type-number))
45 (defmethod to-alien-form (gtype (type (eql 'gtype)) &rest args)
46 (declare (ignore type args))
47 `(find-type-number ,gtype t))
49 (defmethod to-alien-function ((type (eql 'gtype)) &rest args)
50 (declare (ignore type args))
52 (find-type-number gtype t)))
54 (defmethod from-alien-form (type-number (type (eql 'gtype)) &rest args)
55 (declare (ignore type args))
56 `(type-from-number ,type-number))
58 (defmethod from-alien-function ((type (eql 'gtype)) &rest args)
59 (declare (ignore type args))
60 #'(lambda (type-number)
61 (type-from-number type-number)))
63 (defmethod writer-function ((type (eql 'gtype)) &rest args)
64 (declare (ignore type args))
65 (let ((writer (writer-function 'type-number)))
66 #'(lambda (gtype location &optional (offset 0))
67 (funcall writer (find-type-number gtype t) location offset))))
69 (defmethod reader-function ((type (eql 'gtype)) &rest args)
70 (declare (ignore type args))
71 (let ((reader (reader-function 'type-number)))
72 #'(lambda (location &optional (offset 0))
73 (type-from-number (funcall reader location offset)))))
76 (eval-when (:compile-toplevel :load-toplevel :execute)
77 (defclass type-query (struct)
78 ((type-number :allocation :alien :type type-number)
79 (name :allocation :alien :type string)
80 (class-size :allocation :alien :type unsigned-int)
81 (instance-size :allocation :alien :type unsigned-int))
82 (:metaclass struct-class)))
85 (defbinding type-query (type) nil
86 ((find-type-number type t) type-number)
87 ((make-instance 'type-query) type-query :return))
89 (defun type-instance-size (type)
90 (slot-value (type-query type) 'instance-size))
92 (defun type-class-size (type)
93 (slot-value (type-query type) 'class-size))
95 (defbinding type-class-ref (type) pointer
96 ((find-type-number type t) type-number))
98 (defbinding type-class-unref (type) nil
99 ((find-type-number type t) type-number))
101 (defbinding type-class-peek (type) pointer
102 ((find-type-number type t) type-number))
105 ;;;; Mapping between lisp types and glib types
107 (defvar *registered-types* ())
108 (defvar *registered-type-aliases* ())
109 (defvar *lisp-type-to-type-number* (make-hash-table))
110 (defvar *type-number-to-lisp-type* (make-hash-table))
112 (defbinding %type-from-name () type-number
115 (defun type-number-from-glib-name (name &optional (error-p t))
116 (let ((type-number (%type-from-name name)))
118 ((not (zerop type-number)) type-number)
119 (error-p (error "Invalid gtype name: ~A" name)))))
121 (defun register-type (type id)
122 (pushnew (cons type id) *registered-types* :key #'car)
125 (string (type-number-from-glib-name id))
126 (symbol (funcall id)))))
127 (setf (gethash type *lisp-type-to-type-number*) type-number)
128 (setf (gethash type-number *type-number-to-lisp-type*) type)
131 (defun register-type-alias (type alias)
132 (pushnew (cons type alias) *registered-type-aliases* :key #'car)
134 (gethash type *lisp-type-to-type-number*)
135 (find-type-number alias t)))
137 (defun reinitialize-all-types ()
138 (clrhash *lisp-type-to-type-number*)
139 (clrhash *type-number-to-lisp-type*)
140 (type-init) ; initialize the glib type system
141 (mapc #'(lambda (type)
142 (register-type (car type) (cdr type)))
144 (mapc #'(lambda (type)
145 (register-type-alias (car type) (cdr type)))
146 *registered-type-aliases*))
148 (pushnew 'reinitialize-all-types
149 #+cmu *after-save-initializations*
153 (pushnew 'system::reinitialize-global-table ; we shouldn't have to do this?
154 *after-save-initializations*)
157 (defun find-type-number (type &optional error-p)
160 (string (type-number-from-glib-name type error-p))
163 (gethash type *lisp-type-to-type-number*)
164 (and error-p (error "Type not registered: ~A" type))))
165 (class (find-type-number (class-name type) error-p))))
167 (defun type-from-number (type-number &optional error)
168 (multiple-value-bind (type found)
169 (gethash type-number *type-number-to-lisp-type*)
172 (let ((name (find-foreign-type-name type-number)))
174 ((and name (type-number-from-glib-name name nil))
175 ;; This is a hack because GdkEvent seems to be registered
177 (type-from-number (type-number-from-glib-name name)))
179 (error "Type number not registered: ~A (~A)" type-number name))
181 (error "Invalid type number: ~A" type-number)))))))
183 (defbinding (find-foreign-type-name "g_type_name") (type) (copy-of string)
184 ((find-type-number type t) type-number))
186 (defun type-number-of (object)
187 (find-type-number (type-of object) t))
189 (eval-when (:compile-toplevel :load-toplevel :execute)
190 (defvar *type-initializers* ())
191 (defun %find-types-in-library (pathname prefixes ignore)
192 (let ((process (run-program
193 "/usr/bin/nm" (list "--defined-only" "-D" (namestring (truename pathname)))
194 :output :stream :wait nil)))
197 as symbol = (let ((line (read-line (process-output process) nil)))
198 (when line (subseq line 11)))
201 (> (length symbol) 9)
204 (some #'(lambda (prefix)
206 (> (length symbol) (length prefix))
207 (string= prefix symbol :end2 (length prefix))))
209 (string= "_get_type" symbol :start2 (- (length symbol) 9))
210 (not (member symbol ignore :test #'string=)))
212 (process-close process)))))
215 (defmacro init-types-in-library (filename &key prefix ignore)
216 (let ((names (%find-types-in-library filename prefix ignore)))
218 ,@(mapcar #'(lambda (name)
220 (defbinding (,(intern name) ,name) () type-number)
222 (pushnew ',(intern name) *type-initializers*)))
225 (defun find-type-init-function (type-number)
227 for type-init in *type-initializers*
228 when (= type-number (funcall type-init))
229 do (return type-init)))
231 (defun register-type-as (type-number)
233 (find-type-init-function type-number)
234 (find-foreign-type-name type-number)
235 (error "Unknown type-number: ~A" type-number)))
237 (defun default-type-init-name (type)
238 (find-symbol (format nil "~A_~A_get_type"
239 (package-prefix *package*)
240 (substitute #\_ #\- (string-downcase type)))))
243 (eval-when (:compile-toplevel :load-toplevel :execute)
244 (defclass type-info (struct)
245 ((class-size :allocation :alien :type (unsigned 16) :initarg :class-size)
246 (base-init :allocation :alien :type pointer)
247 (base-finalize :allocation :alien :type pointer)
248 (class-init :allocation :alien :type pointer)
249 (class-finalize :allocation :alien :type pointer)
250 (class-data :allocation :alien :type pointer)
251 (instance-size :allocation :alien :type (unsigned 16)
252 :initarg :instance-size)
253 (n-preallocs :allocation :alien :type (unsigned 16))
254 (instance-init :allocation :alien :type pointer)
255 (value-table :allocation :alien :type pointer))
256 (:metaclass struct-class)))
258 (defbinding %type-register-static () type-number
259 (parent-type type-number)
264 (defun register-new-type (type parent)
265 (let ((parent-info (type-query parent)))
266 (with-slots ((parent-number type-number) class-size instance-size) parent-info
268 (%type-register-static
270 (default-alien-type-name type)
271 (make-instance 'type-info :class-size class-size :instance-size instance-size))))
272 (setf (gethash type *lisp-type-to-type-number*) type-number)
273 (setf (gethash type-number *type-number-to-lisp-type*) type)
278 ;;;; Metaclass for subclasses of ginstance
280 (eval-when (:compile-toplevel :load-toplevel :execute)
281 (defclass ginstance-class (proxy-class)
285 (defmethod shared-initialize ((class ginstance-class) names &rest initargs &key name gtype)
286 (declare (ignore names))
287 (let* ((class-name (or name (class-name class)))
290 (find-type-number class-name)
291 (register-type class-name
292 (or (first gtype) (default-type-init-name class-name))))))
293 (if (getf initargs :size)
295 (let ((size (type-instance-size type-number)))
296 (apply #'call-next-method class names :size (list size) initargs)))))
299 (defmethod validate-superclass ((class ginstance-class) (super standard-class))
300 (subtypep (class-name super) 'ginstance))
303 ;;;; Superclass for wrapping types in the glib type system
305 (eval-when (:compile-toplevel :load-toplevel :execute)
306 (defclass ginstance (proxy)
307 ((class :allocation :alien :type pointer))
308 (:metaclass proxy-class)))
310 (defun %type-number-of-ginstance (location)
311 (let ((class (sap-ref-sap location 0)))
312 (sap-ref-32 class 0)))
314 (defmethod ensure-proxy-instance ((class ginstance-class) location)
315 (declare (ignore class))
316 (let ((class (labels ((find-known-class (type-number)
318 (find-class (type-from-number type-number) nil)
319 (unless (zerop type-number)
320 (find-known-class (type-parent type-number))))))
321 (find-known-class (%type-number-of-ginstance location)))))
323 (make-instance class :location (reference-foreign class location))
324 (error "Object at ~A has an unkown type number: ~A"
325 location (%type-number-of-ginstance location)))))
327 (defmethod copy-from-alien-form (location (class ginstance-class) &rest args)
328 (declare (ignore location class args))
329 (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."))
331 (defmethod copy-from-alien-function ((class ginstance-class) &rest args)
332 (declare (ignore class args))
333 (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."))
335 (defmethod reader-function ((class ginstance-class) &rest args)
336 (declare (ignore args))
337 #'(lambda (location &optional (offset 0))
338 (ensure-proxy-instance class (sap-ref-sap location offset))))
341 ;;;; Registering fundamental types
343 (register-type 'nil "void")
344 (register-type 'pointer "gpointer")
345 (register-type 'char "gchar")
346 (register-type 'unsigned-char "guchar")
347 (register-type 'boolean "gboolean")
348 (register-type 'int "gint")
349 (register-type-alias 'integer 'int)
350 (register-type-alias 'fixnum 'int)
351 (register-type 'unsigned-int "guint")
352 (register-type 'long "glong")
353 (register-type 'unsigned-long "gulong")
354 (register-type 'single-float "gfloat")
355 (register-type 'double-float "gdouble")
356 (register-type 'pathname "gchararray")
357 (register-type 'string "gchararray")
360 ;;;; Introspection of type information
362 (defvar *derivable-type-info* (make-hash-table))
364 (defun register-derivable-type (type id expander &optional dependencies)
365 (register-type type id)
366 (let ((type-number (register-type type id)))
368 (gethash type-number *derivable-type-info*)
369 (list expander dependencies))))
371 (defun find-type-info (type)
372 (dolist (super (cdr (type-hierarchy type)))
373 (let ((info (gethash super *derivable-type-info*)))
376 (defun expand-type-definition (type forward-p options)
377 (let ((expander (first (find-type-info type))))
378 (funcall expander (find-type-number type t) forward-p options)))
380 (defbinding type-parent (type) type-number
381 ((find-type-number type t) type-number))
383 (defun supertype (type)
384 (type-from-number (type-parent type)))
386 (defbinding %type-interfaces (type) pointer
387 ((find-type-number type t) type-number)
388 (n-interfaces unsigned-int :out))
390 (defun type-interfaces (type)
391 (multiple-value-bind (array length) (%type-interfaces type)
393 (map-c-vector 'list #'identity array 'type-number length)
394 (deallocate-memory array))))
396 (defun implements (type)
397 (mapcar #'type-from-number (type-interfaces type)))
399 (defun type-hierarchy (type)
400 (let ((type-number (find-type-number type t)))
401 (unless (= type-number 0)
402 (cons type-number (type-hierarchy (type-parent type-number))))))
404 (defbinding (type-is-p "g_type_is_a") (type super) boolean
405 ((find-type-number type) type-number)
406 ((find-type-number super) type-number))
408 (defbinding %type-children () pointer
409 (type-number type-number)
410 (num-children unsigned-int :out))
412 (defun map-subtypes (function type &optional prefix)
413 (let ((type-number (find-type-number type t)))
414 (multiple-value-bind (array length) (%type-children type-number)
418 #'(lambda (type-number)
421 (string-prefix-p prefix (find-foreign-type-name type-number)))
422 (funcall function type-number))
423 (map-subtypes function type-number prefix))
424 array 'type-number length)
425 (deallocate-memory array)))))
427 (defun find-types (prefix)
428 (let ((type-list nil))
430 #'(lambda (type-number expander)
431 (declare (ignore expander))
433 #'(lambda (type-number)
434 (pushnew type-number type-list))
436 *derivable-type-info*)
439 (defun find-type-dependencies (type)
440 (let ((list-dependencies (second (find-type-info type))))
441 (when list-dependencies
442 (funcall list-dependencies (find-type-number type t)))))
444 (defun %sort-types-topologicaly (types)
445 (let ((partial-sorted
449 (cons type (remove-if #'(lambda (dep)
450 (not (find dep types)))
451 (find-type-dependencies type))))
453 #'(lambda (type1 type2) (type-is-p type2 type1)) :key #'car))
457 as tmp = partial-sorted then (or (rest tmp) partial-sorted)
459 do (destructuring-bind (type . dependencies) (first tmp)
461 ((every #'(lambda (dep)
464 (push (cons type nil) sorted) ; no forward definition needed
465 (setq partial-sorted (delete type partial-sorted :key #'first)))
466 ((some #'(lambda (dep)
467 (find type (find-type-dependencies dep)))
469 (push (cons type t) sorted) ; forward definition needed
470 (setq partial-sorted (delete type partial-sorted :key #'first))))))
474 (defun expand-type-definitions (prefix &optional args)
475 (flet ((type-options (type-number)
476 (let ((name (find-foreign-type-name type-number)))
477 (cdr (assoc name args :test #'string=)))))
481 #'(lambda (type-number)
482 (let ((name (find-foreign-type-name type-number)))
484 (getf (type-options type-number) :ignore)
488 (string-prefix-p (first options) name)
489 (getf (cdr options) :ignore-prefix)
491 #'(lambda (exception)
492 (string= name exception))
493 (getf (cdr options) :except)))))
495 (find-types prefix))))
497 (dolist (type-number type-list)
498 (let ((name (find-foreign-type-name type-number)))
500 (getf (type-options type-number) :type (default-type-name name))
501 (register-type-as type-number))))
503 (let ((sorted-type-list (%sort-types-topologicaly type-list)))
507 (destructuring-bind (type . forward-p) pair
508 (expand-type-definition type forward-p (type-options type))))
512 (destructuring-bind (type . forward-p) pair
514 (expand-type-definition type nil (type-options type)))))
515 sorted-type-list))))))
517 (defmacro define-types-by-introspection (prefix &rest args)
518 (expand-type-definitions prefix args))
521 ;;;; Initialize all non static types in GObject
523 (init-types-in-library #.(concatenate 'string (pkg-config:pkg-variable "glib-2.0" "libdir") "/libgobject-2.0.so"))