chiark / gitweb /
Added declaration to get rid of a couple of warnings.
[clg] / glib / gtype.lisp
CommitLineData
560af5c5 1;; Common Lisp bindings for GTK+ v2.0
d4b21b08 2;; Copyright (C) 2000-2001 Espen S. Johnsen <esj@stud.cs.uit.no>
560af5c5 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
21299acf 18;; $Id: gtype.lisp,v 1.26 2005-02-10 00:20:02 espen Exp $
560af5c5 19
20(in-package "GLIB")
21
22(use-prefix "g")
23
e0349dbd 24;; Initialize the glib type system
25(defbinding type-init () nil)
26(type-init)
560af5c5 27
28(deftype type-number () '(unsigned 32))
29
e74cfcab 30(deftype gtype () 'symbol)
31
32(defmethod alien-type ((type (eql 'gtype)) &rest args)
33 (declare (ignore type args))
34 (alien-type 'type-number))
35
36(defmethod size-of ((type (eql 'gtype)) &rest args)
37 (declare (ignore type args))
38 (size-of 'type-number))
39
40(defmethod to-alien-form (gtype (type (eql 'gtype)) &rest args)
41 (declare (ignore type args))
42 `(find-type-number ,gtype t))
43
44(defmethod to-alien-function ((type (eql 'gtype)) &rest args)
45 (declare (ignore type args))
46 #'(lambda (gtype)
47 (find-type-number gtype t)))
48
49(defmethod from-alien-form (type-number (type (eql 'gtype)) &rest args)
50 (declare (ignore type args))
63752532 51 `(type-from-number ,type-number))
e74cfcab 52
53(defmethod from-alien-function ((type (eql 'gtype)) &rest args)
54 (declare (ignore type args))
55 #'(lambda (type-number)
63752532 56 (type-from-number type-number)))
e74cfcab 57
58(defmethod writer-function ((type (eql 'gtype)) &rest args)
73572c12 59 (declare (ignore type args))
e74cfcab 60 (let ((writer (writer-function 'type-number)))
61 #'(lambda (gtype location &optional (offset 0))
62 (funcall writer (find-type-number gtype t) location offset))))
63
64(defmethod reader-function ((type (eql 'gtype)) &rest args)
73572c12 65 (declare (ignore type args))
e74cfcab 66 (let ((reader (reader-function 'type-number)))
67 #'(lambda (location &optional (offset 0))
63752532 68 (type-from-number (funcall reader location offset)))))
e74cfcab 69
70
93aa67db 71(eval-when (:compile-toplevel :load-toplevel :execute)
d4b21b08 72 (defclass type-query (struct)
93aa67db 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))
d168bafd 77 (:metaclass struct-class)))
93aa67db 78
79
e74cfcab 80(defbinding type-query (type) nil
81 ((find-type-number type t) type-number)
82 ((make-instance 'type-query) type-query :return))
93aa67db 83
84(defun type-instance-size (type)
85 (slot-value (type-query type) 'instance-size))
86
87(defun type-class-size (type)
88 (slot-value (type-query type) 'class-size))
560af5c5 89
d4b21b08 90(defbinding type-class-ref (type) pointer
91 ((find-type-number type t) type-number))
560af5c5 92
d4b21b08 93(defbinding type-class-unref (type) nil
94 ((find-type-number type t) type-number))
93aa67db 95
d4b21b08 96(defbinding type-class-peek (type) pointer
97 ((find-type-number type t) type-number))
93aa67db 98
560af5c5 99
d4b21b08 100;;;; Mapping between lisp types and glib types
560af5c5 101
102(defvar *type-to-number-hash* (make-hash-table))
103(defvar *number-to-type-hash* (make-hash-table))
104
d4b21b08 105(defun register-type (type id)
106 (let ((type-number
107 (etypecase id
108 (integer id)
4d83a8a6 109 (string (find-type-number id t))
110 (symbol (gethash id *type-to-number-hash*)))))
d4b21b08 111 (setf (gethash type *type-to-number-hash*) type-number)
4d83a8a6 112 (unless (symbolp id)
113 (setf (gethash type-number *number-to-type-hash*) type))
d4b21b08 114 type-number))
115
116(defbinding %type-from-name () type-number
117 (name string))
118
119(defun find-type-number (type &optional error)
560af5c5 120 (etypecase type
121 (integer type)
d4b21b08 122 (string
123 (let ((type-number (%type-from-name type)))
124 (cond
125 ((and (zerop type-number) error)
d168bafd 126 (error "Invalid gtype name: ~A" type))
d4b21b08 127 ((zerop type-number) nil)
128 (t type-number))))
129 (symbol
130 (let ((type-number (gethash type *type-to-number-hash*)))
131 (or
132 type-number
133 (and error (error "Type not registered: ~A" type)))))
73572c12 134 (class (find-type-number (class-name type) error))))
560af5c5 135
4de90d10 136(defun type-from-number (type-number &optional error)
137 (multiple-value-bind (type found)
138 (gethash type-number *number-to-type-hash*)
139 (when (and error (not found))
140 (let ((name (find-type-name type-number)))
141 (if name
142 (error "Type number not registered: ~A (~A)" type-number name)
143 (error "Invalid type number: ~A" type-number))))
144 type))
560af5c5 145
d4b21b08 146(defun type-from-name (name)
147 (etypecase name
148 (string (type-from-number (find-type-number name t)))))
560af5c5 149
9ca5565a 150(defbinding (find-type-name "g_type_name") (type) (copy-of string)
d4b21b08 151 ((find-type-number type t) type-number))
152
153(defun type-number-of (object)
154 (find-type-number (type-of object) t))
155
21299acf 156(eval-when (:compile-toplevel :load-toplevel :execute)
157 (defun %find-types-in-library (pathname prefix ignore)
158 (let ((process (run-program
159 "/usr/bin/nm" (list "--defined-only" "-D" (namestring (truename pathname)))
160 :output :stream :wait nil)))
161 (unwind-protect
162 (loop
163 as symbol = (let ((line (read-line (process-output process) nil)))
164 (when line (subseq line 11)))
165 while symbol
166 when (and
167 (> (length symbol) (length prefix))
168 (string= prefix symbol :end2 (length prefix))
169 (search "_get_type" symbol)
170 (not (member symbol ignore :test #'string=)))
171 collect symbol)
172 (process-close process)))))
173
560af5c5 174
d168bafd 175(defmacro init-types-in-library (filename &key (prefix "") ignore)
21299acf 176 (let ((names (%find-types-in-library filename prefix ignore)))
177 `(progn
178 ,@(mapcar #'(lambda (name)
179 `(progn
180 (defbinding (,(intern name) ,name) () type-number)
181 (,(intern name))))
182 names))))
183
184
185
186;;;; Metaclass for subclasses of ginstance
187
188(eval-when (:compile-toplevel :load-toplevel :execute)
189 (defclass ginstance-class (proxy-class)
190 ()))
191
192
193(defmethod shared-initialize ((class ginstance-class) names
194 &rest initargs &key name alien-name)
195 (declare (ignore names))
196 (let* ((class-name (or name (class-name class)))
197 (type-number
198 (find-type-number
199 (or (first alien-name) (default-alien-type-name class-name)) t)))
200 (register-type class-name type-number)
201 (if (getf initargs :size)
202 (call-next-method)
203 (let ((size (type-instance-size type-number)))
204 (apply #'call-next-method class names :size (list size) initargs)))))
4de90d10 205
206
21299acf 207(defmethod validate-superclass ((class ginstance-class) (super standard-class))
208 (subtypep (class-name super) 'ginstance))
209
560af5c5 210
93aa67db 211;;;; Superclass for wrapping types in the glib type system
560af5c5 212
213(eval-when (:compile-toplevel :load-toplevel :execute)
93aa67db 214 (defclass ginstance (proxy)
d4b21b08 215 ((class :allocation :alien :type pointer))
216 (:metaclass proxy-class)))
560af5c5 217
93aa67db 218(defun %type-of-ginstance (location)
219 (let ((class (sap-ref-sap location 0)))
d168bafd 220 (type-from-number (sap-ref-32 class 0))))
560af5c5 221
d168bafd 222(defmethod ensure-proxy-instance ((class ginstance-class) location)
223 (declare (ignore class))
224 (let ((class (find-class (%type-of-ginstance location))))
225 (if class
226 (make-instance class :location (reference-foreign class location))
227 ;; TODO: (make-instance 'ginstance ...)
228 location)))
560af5c5 229
9ca5565a 230(defmethod copy-from-alien-form (location (class ginstance-class) &rest args)
231 (declare (ignore location class args))
232 (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."))
233
234(defmethod copy-from-alien-function ((class ginstance-class) &rest args)
235 (declare (ignore class args))
236 (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."))
237
238(defmethod reader-function ((class ginstance-class) &rest args)
239 (declare (ignore args))
240 #'(lambda (location &optional (offset 0))
241 (ensure-proxy-instance class (sap-ref-sap location offset))))
242
560af5c5 243
d4b21b08 244;;;; Registering fundamental types
245
63752532 246(register-type 'nil "void")
d4b21b08 247(register-type 'pointer "gpointer")
248(register-type 'char "gchar")
249(register-type 'unsigned-char "guchar")
250(register-type 'boolean "gboolean")
251(register-type 'fixnum "gint")
252(register-type 'int "gint")
253(register-type 'unsigned-int "guint")
254(register-type 'long "glong")
255(register-type 'unsigned-long "gulong")
256(register-type 'single-float "gfloat")
257(register-type 'double-float "gdouble")
a8cb9408 258(register-type 'pathname "gchararray")
4de90d10 259(register-type 'string "gchararray")
d4b21b08 260
261
62f12808 262;;;; Introspection of type information
d4b21b08 263
e77e7713 264(defvar *derivable-type-info* (make-hash-table))
d4b21b08 265
62f12808 266(defun register-derivable-type (type id expander &optional dependencies)
d4b21b08 267 (register-type type id)
e77e7713 268 (let ((type-number (register-type type id)))
62f12808 269 (setf
270 (gethash type-number *derivable-type-info*)
271 (list expander dependencies))))
d4b21b08 272
4de90d10 273(defun find-type-info (type)
274 (dolist (super (cdr (type-hierarchy type)))
e77e7713 275 (let ((info (gethash super *derivable-type-info*)))
4de90d10 276 (return-if info))))
277
62f12808 278(defun expand-type-definition (type forward-p options)
279 (let ((expander (first (find-type-info type))))
280 (funcall expander (find-type-number type t) forward-p options)))
d4b21b08 281
d4b21b08 282(defbinding type-parent (type) type-number
283 ((find-type-number type t) type-number))
284
285(defun supertype (type)
286 (type-from-number (type-parent type)))
287
337933d8 288(defbinding %type-interfaces (type) pointer
289 ((find-type-number type t) type-number)
290 (n-interfaces unsigned-int :out))
291
292(defun type-interfaces (type)
293 (multiple-value-bind (array length) (%type-interfaces type)
294 (unwind-protect
d168bafd 295 (map-c-vector 'list #'identity array 'type-number length)
337933d8 296 (deallocate-memory array))))
297
298(defun implements (type)
299 (mapcar #'type-from-number (type-interfaces type)))
300
d4b21b08 301(defun type-hierarchy (type)
302 (let ((type-number (find-type-number type t)))
303 (unless (= type-number 0)
304 (cons type-number (type-hierarchy (type-parent type-number))))))
305
306(defbinding (type-is-p "g_type_is_a") (type super) boolean
307 ((find-type-number type) type-number)
308 ((find-type-number super) type-number))
309
310(defbinding %type-children () pointer
311 (type-number type-number)
312 (num-children unsigned-int :out))
313
314(defun map-subtypes (function type &optional prefix)
315 (let ((type-number (find-type-number type t)))
316 (multiple-value-bind (array length) (%type-children type-number)
317 (unwind-protect
d168bafd 318 (map-c-vector
d4b21b08 319 'nil
320 #'(lambda (type-number)
321 (when (or
322 (not prefix)
323 (string-prefix-p prefix (find-type-name type-number)))
324 (funcall function type-number))
325 (map-subtypes function type-number prefix))
326 array 'type-number length)
327 (deallocate-memory array)))))
328
329(defun find-types (prefix)
330 (let ((type-list nil))
e77e7713 331 (maphash
332 #'(lambda (type-number expander)
333 (declare (ignore expander))
334 (map-subtypes
335 #'(lambda (type-number)
336 (pushnew type-number type-list))
337 type-number prefix))
338 *derivable-type-info*)
d4b21b08 339 type-list))
340
62f12808 341(defun find-type-dependencies (type)
342 (let ((list-dependencies (second (find-type-info type))))
343 (when list-dependencies
344 (funcall list-dependencies (find-type-number type t)))))
345
346(defun %sort-types-topologicaly (types)
21299acf 347 (let ((partial-sorted
348 (sort
349 (mapcar
350 #'(lambda (type)
351 (cons type (remove-if #'(lambda (dep)
352 (not (find dep types)))
353 (find-type-dependencies type))))
354 types)
355 #'(lambda (type1 type2) (type-is-p type2 type1)) :key #'car))
62f12808 356 (sorted ()))
357
358 (loop
21299acf 359 as tmp = partial-sorted then (or (rest tmp) partial-sorted)
62f12808 360 while tmp
361 do (destructuring-bind (type . dependencies) (first tmp)
d4b21b08 362 (cond
62f12808 363 ((every #'(lambda (dep)
21299acf 364 (assoc dep sorted))
62f12808 365 dependencies)
21299acf 366 (push (cons type nil) sorted) ; no forward definition needed
367 (setq partial-sorted (delete type partial-sorted :key #'first)))
62f12808 368 ((some #'(lambda (dep)
369 (find type (find-type-dependencies dep)))
370 dependencies)
21299acf 371 (push (cons type t) sorted) ; forward definition needed
372 (setq partial-sorted (delete type partial-sorted :key #'first))))))
373 (nreverse sorted)))
d4b21b08 374
375
376(defun expand-type-definitions (prefix &optional args)
e77e7713 377 (flet ((type-options (type-number)
d4b21b08 378 (let ((name (find-type-name type-number)))
4de90d10 379 (cdr (assoc name args :test #'string=)))))
d4b21b08 380
e77e7713 381 (let ((type-list
382 (delete-if
383 #'(lambda (type-number)
384 (let ((name (find-type-name type-number)))
385 (or
386 (getf (type-options type-number) :ignore)
387 (find-if
388 #'(lambda (options)
389 (and
390 (string-prefix-p (first options) name)
80a09c29 391 (getf (cdr options) :ignore-prefix)
392 (not (some
393 #'(lambda (exception)
394 (string= name exception))
395 (getf (cdr options) :except)))))
e77e7713 396 args))))
397 (find-types prefix))))
21299acf 398
e77e7713 399 (dolist (type-number type-list)
400 (let ((name (find-type-name type-number)))
401 (register-type
402 (getf (type-options type-number) :type (default-type-name name))
403 type-number)))
21299acf 404
405 (let ((sorted-type-list (%sort-types-topologicaly type-list)))
62f12808 406 `(progn
407 ,@(mapcar
21299acf 408 #'(lambda (pair)
409 (destructuring-bind (type . forward-p) pair
410 (expand-type-definition type forward-p (type-options type))))
411 sorted-type-list)
62f12808 412 ,@(mapcar
21299acf 413 #'(lambda (pair)
414 (destructuring-bind (type . forward-p) pair
415 (when forward-p
416 (expand-type-definition type nil (type-options type)))))
62f12808 417 sorted-type-list))))))
e77e7713 418
d4b21b08 419(defmacro define-types-by-introspection (prefix &rest args)
4de90d10 420 (expand-type-definitions prefix args))
21299acf 421
422
423;;;; Initialize all non static types in GObject
424
425(init-types-in-library #.(concatenate 'string (pkg-config:pkg-variable "glib-2.0" "libdir") "/libgobject-2.0.so"))