Workaround for missing proxy classes
[clg] / glib / gtype.lisp
CommitLineData
0d07716f 1;; Common Lisp bindings for GTK+ v2.0
3a935dfa 2;; Copyright (C) 2000-2001 Espen S. Johnsen <esj@stud.cs.uit.no>
0d07716f 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
609ba905 18;; $Id: gtype.lisp,v 1.29 2005/03/11 20:16:07 espen Exp $
0d07716f 19
20(in-package "GLIB")
21
22(use-prefix "g")
23
d1266407 24;; Initialize the glib type system
25(defbinding type-init () nil)
26(type-init)
0d07716f 27
28(deftype type-number () '(unsigned 32))
29
b4a2c852 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))
40c346ec 51 `(type-from-number ,type-number))
b4a2c852 52
53(defmethod from-alien-function ((type (eql 'gtype)) &rest args)
54 (declare (ignore type args))
55 #'(lambda (type-number)
40c346ec 56 (type-from-number type-number)))
b4a2c852 57
58(defmethod writer-function ((type (eql 'gtype)) &rest args)
3d36c5d6 59 (declare (ignore type args))
b4a2c852 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)
3d36c5d6 65 (declare (ignore type args))
b4a2c852 66 (let ((reader (reader-function 'type-number)))
67 #'(lambda (location &optional (offset 0))
40c346ec 68 (type-from-number (funcall reader location offset)))))
b4a2c852 69
70
fc47a022 71(eval-when (:compile-toplevel :load-toplevel :execute)
3a935dfa 72 (defclass type-query (struct)
fc47a022 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))
4d1d3921 77 (:metaclass struct-class)))
fc47a022 78
79
b4a2c852 80(defbinding type-query (type) nil
81 ((find-type-number type t) type-number)
82 ((make-instance 'type-query) type-query :return))
fc47a022 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))
0d07716f 89
3a935dfa 90(defbinding type-class-ref (type) pointer
91 ((find-type-number type t) type-number))
0d07716f 92
3a935dfa 93(defbinding type-class-unref (type) nil
94 ((find-type-number type t) type-number))
fc47a022 95
3a935dfa 96(defbinding type-class-peek (type) pointer
97 ((find-type-number type t) type-number))
fc47a022 98
0d07716f 99
3a935dfa 100;;;; Mapping between lisp types and glib types
0d07716f 101
dcb31db6 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))
3a935dfa 106
107(defbinding %type-from-name () type-number
108 (name string))
109
dcb31db6 110(defun type-number-from-glib-name (name &optional (error-p t))
111 (let ((type-number (%type-from-name name)))
112 (cond
113 ((not (zerop type-number)) type-number)
114 (error-p (error "Invalid gtype name: ~A" name)))))
115
116(defun register-type (type id)
117 (pushnew (cons type id) *registered-types* :key #'car)
118 (let ((type-number
119 (typecase id
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)
124 type-number))
125
126(defun register-type-alias (type alias)
127 (pushnew (cons type alias) *registered-type-aliases* :key #'car)
128 (setf
129 (gethash type *lisp-type-to-type-number*)
130 (find-type-number alias t)))
131
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)))
138 *registered-types*)
139 (mapc #'(lambda (type)
140 (register-type-alias (car type) (cdr type)))
141 *registered-type-aliases*))
142
143(pushnew 'reinitialize-all-types
144 #+cmu *after-save-initializations*
145 #+sbcl *init-hooks*)
146
147#+cmu
148(pushnew 'system::reinitialize-global-table ; we shouldn't have to do this?
149 *after-save-initializations*)
150
151
152(defun find-type-number (type &optional error-p)
0d07716f 153 (etypecase type
154 (integer type)
dcb31db6 155 (string (type-number-from-glib-name type error-p))
3a935dfa 156 (symbol
dcb31db6 157 (or
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))))
0d07716f 161
b011356b 162(defun type-from-number (type-number &optional error)
163 (multiple-value-bind (type found)
dcb31db6 164 (gethash type-number *type-number-to-lisp-type*)
b011356b 165 (when (and error (not found))
dcb31db6 166 (let ((name (find-foreign-type-name type-number)))
b011356b 167 (if name
168 (error "Type number not registered: ~A (~A)" type-number name)
169 (error "Invalid type number: ~A" type-number))))
170 type))
0d07716f 171
dcb31db6 172(defbinding (find-foreign-type-name "g_type_name") (type) (copy-of string)
3a935dfa 173 ((find-type-number type t) type-number))
174
175(defun type-number-of (object)
176 (find-type-number (type-of object) t))
177
6556dccd 178(eval-when (:compile-toplevel :load-toplevel :execute)
dcb31db6 179 (defvar *type-initializers* ())
180 (defun %find-types-in-library (pathname prefixes ignore)
6556dccd 181 (let ((process (run-program
182 "/usr/bin/nm" (list "--defined-only" "-D" (namestring (truename pathname)))
183 :output :stream :wait nil)))
184 (unwind-protect
185 (loop
186 as symbol = (let ((line (read-line (process-output process) nil)))
187 (when line (subseq line 11)))
188 while symbol
189 when (and
dcb31db6 190 (> (length symbol) 9)
191 (or
192 (not prefixes)
193 (some #'(lambda (prefix)
194 (and
195 (> (length symbol) (length prefix))
196 (string= prefix symbol :end2 (length prefix))))
197 (mklist prefixes)))
198 (string= "_get_type" symbol :start2 (- (length symbol) 9))
6556dccd 199 (not (member symbol ignore :test #'string=)))
200 collect symbol)
201 (process-close process)))))
202
0d07716f 203
80031aba 204(defmacro init-types-in-library (filename &key prefix ignore)
6556dccd 205 (let ((names (%find-types-in-library filename prefix ignore)))
206 `(progn
207 ,@(mapcar #'(lambda (name)
208 `(progn
209 (defbinding (,(intern name) ,name) () type-number)
dcb31db6 210 (,(intern name))
211 (pushnew ',(intern name) *type-initializers*)))
6556dccd 212 names))))
213
dcb31db6 214(defun find-type-init-function (type-number)
80031aba 215 (loop
216 for type-init in *type-initializers*
217 when (= type-number (funcall type-init))
218 do (return type-init)))
219
220(defun register-type-as (type-number)
221 (or
222 (find-type-init-function type-number)
223 (find-foreign-type-name type-number)
224 (error "Unknown type-number: ~A" type-number)))
dcb31db6 225
226(defun default-type-init-name (type)
227 (find-symbol (format nil "~A_~A_get_type"
228 (package-prefix *package*)
229 (substitute #\_ #\- (string-downcase type)))))
230
6556dccd 231
232
233;;;; Metaclass for subclasses of ginstance
234
235(eval-when (:compile-toplevel :load-toplevel :execute)
236 (defclass ginstance-class (proxy-class)
237 ()))
238
239
dcb31db6 240(defmethod shared-initialize ((class ginstance-class) names &key name gtype)
6556dccd 241 (declare (ignore names))
242 (let* ((class-name (or name (class-name class)))
dcb31db6 243 (type-number
244 (or
245 (find-type-number class-name)
246 (register-type class-name
247 (or (first gtype) (default-type-init-name class-name))))))
248 (call-next-method)
249 (when (slot-boundp class 'size)
250 (setf (slot-value class 'size) (type-instance-size type-number)))))
b011356b 251
252
6556dccd 253(defmethod validate-superclass ((class ginstance-class) (super standard-class))
254 (subtypep (class-name super) 'ginstance))
255
0d07716f 256
fc47a022 257;;;; Superclass for wrapping types in the glib type system
0d07716f 258
259(eval-when (:compile-toplevel :load-toplevel :execute)
fc47a022 260 (defclass ginstance (proxy)
3a935dfa 261 ((class :allocation :alien :type pointer))
262 (:metaclass proxy-class)))
0d07716f 263
609ba905 264(defun %type-number-of-ginstance (location)
fc47a022 265 (let ((class (sap-ref-sap location 0)))
609ba905 266 (sap-ref-32 class 0)))
0d07716f 267
4d1d3921 268(defmethod ensure-proxy-instance ((class ginstance-class) location)
269 (declare (ignore class))
609ba905 270 (let ((class (labels ((find-known-class (type-number)
271 (or
272 (find-class (type-from-number type-number) nil)
273 (unless (zerop type-number)
274 (find-known-class (type-parent type-number))))))
275 (find-known-class (%type-number-of-ginstance location)))))
4d1d3921 276 (if class
277 (make-instance class :location (reference-foreign class location))
609ba905 278 (error "Object at ~A has an unkown type number: ~A"
279 location (%type-number-of-ginstance location)))))
0d07716f 280
508d13a7 281(defmethod copy-from-alien-form (location (class ginstance-class) &rest args)
282 (declare (ignore location class args))
283 (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."))
284
285(defmethod copy-from-alien-function ((class ginstance-class) &rest args)
286 (declare (ignore class args))
287 (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."))
288
289(defmethod reader-function ((class ginstance-class) &rest args)
290 (declare (ignore args))
291 #'(lambda (location &optional (offset 0))
292 (ensure-proxy-instance class (sap-ref-sap location offset))))
293
0d07716f 294
3a935dfa 295;;;; Registering fundamental types
296
40c346ec 297(register-type 'nil "void")
3a935dfa 298(register-type 'pointer "gpointer")
299(register-type 'char "gchar")
300(register-type 'unsigned-char "guchar")
301(register-type 'boolean "gboolean")
3a935dfa 302(register-type 'int "gint")
dcb31db6 303(register-type-alias 'fixnum 'int)
3a935dfa 304(register-type 'unsigned-int "guint")
305(register-type 'long "glong")
306(register-type 'unsigned-long "gulong")
307(register-type 'single-float "gfloat")
308(register-type 'double-float "gdouble")
b77fe850 309(register-type 'pathname "gchararray")
b011356b 310(register-type 'string "gchararray")
3a935dfa 311
312
e9934f39 313;;;; Introspection of type information
3a935dfa 314
4812615b 315(defvar *derivable-type-info* (make-hash-table))
3a935dfa 316
e9934f39 317(defun register-derivable-type (type id expander &optional dependencies)
3a935dfa 318 (register-type type id)
4812615b 319 (let ((type-number (register-type type id)))
e9934f39 320 (setf
321 (gethash type-number *derivable-type-info*)
322 (list expander dependencies))))
3a935dfa 323
b011356b 324(defun find-type-info (type)
325 (dolist (super (cdr (type-hierarchy type)))
4812615b 326 (let ((info (gethash super *derivable-type-info*)))
b011356b 327 (return-if info))))
328
e9934f39 329(defun expand-type-definition (type forward-p options)
330 (let ((expander (first (find-type-info type))))
331 (funcall expander (find-type-number type t) forward-p options)))
3a935dfa 332
3a935dfa 333(defbinding type-parent (type) type-number
334 ((find-type-number type t) type-number))
335
336(defun supertype (type)
337 (type-from-number (type-parent type)))
338
7858d45e 339(defbinding %type-interfaces (type) pointer
340 ((find-type-number type t) type-number)
341 (n-interfaces unsigned-int :out))
342
343(defun type-interfaces (type)
344 (multiple-value-bind (array length) (%type-interfaces type)
345 (unwind-protect
4d1d3921 346 (map-c-vector 'list #'identity array 'type-number length)
7858d45e 347 (deallocate-memory array))))
348
349(defun implements (type)
350 (mapcar #'type-from-number (type-interfaces type)))
351
3a935dfa 352(defun type-hierarchy (type)
353 (let ((type-number (find-type-number type t)))
354 (unless (= type-number 0)
355 (cons type-number (type-hierarchy (type-parent type-number))))))
356
357(defbinding (type-is-p "g_type_is_a") (type super) boolean
358 ((find-type-number type) type-number)
359 ((find-type-number super) type-number))
360
361(defbinding %type-children () pointer
362 (type-number type-number)
363 (num-children unsigned-int :out))
364
365(defun map-subtypes (function type &optional prefix)
366 (let ((type-number (find-type-number type t)))
367 (multiple-value-bind (array length) (%type-children type-number)
368 (unwind-protect
4d1d3921 369 (map-c-vector
3a935dfa 370 'nil
371 #'(lambda (type-number)
372 (when (or
373 (not prefix)
dcb31db6 374 (string-prefix-p prefix (find-foreign-type-name type-number)))
3a935dfa 375 (funcall function type-number))
376 (map-subtypes function type-number prefix))
377 array 'type-number length)
378 (deallocate-memory array)))))
379
380(defun find-types (prefix)
381 (let ((type-list nil))
4812615b 382 (maphash
383 #'(lambda (type-number expander)
384 (declare (ignore expander))
385 (map-subtypes
386 #'(lambda (type-number)
387 (pushnew type-number type-list))
388 type-number prefix))
389 *derivable-type-info*)
3a935dfa 390 type-list))
391
e9934f39 392(defun find-type-dependencies (type)
393 (let ((list-dependencies (second (find-type-info type))))
394 (when list-dependencies
395 (funcall list-dependencies (find-type-number type t)))))
396
397(defun %sort-types-topologicaly (types)
6556dccd 398 (let ((partial-sorted
399 (sort
400 (mapcar
401 #'(lambda (type)
402 (cons type (remove-if #'(lambda (dep)
403 (not (find dep types)))
404 (find-type-dependencies type))))
405 types)
406 #'(lambda (type1 type2) (type-is-p type2 type1)) :key #'car))
e9934f39 407 (sorted ()))
408
409 (loop
6556dccd 410 as tmp = partial-sorted then (or (rest tmp) partial-sorted)
e9934f39 411 while tmp
412 do (destructuring-bind (type . dependencies) (first tmp)
3a935dfa 413 (cond
e9934f39 414 ((every #'(lambda (dep)
6556dccd 415 (assoc dep sorted))
e9934f39 416 dependencies)
6556dccd 417 (push (cons type nil) sorted) ; no forward definition needed
418 (setq partial-sorted (delete type partial-sorted :key #'first)))
e9934f39 419 ((some #'(lambda (dep)
420 (find type (find-type-dependencies dep)))
421 dependencies)
6556dccd 422 (push (cons type t) sorted) ; forward definition needed
423 (setq partial-sorted (delete type partial-sorted :key #'first))))))
424 (nreverse sorted)))
3a935dfa 425
426
427(defun expand-type-definitions (prefix &optional args)
dcb31db6 428 (flet ((type-options (type-number)
429 (let ((name (find-foreign-type-name type-number)))
b011356b 430 (cdr (assoc name args :test #'string=)))))
3a935dfa 431
4812615b 432 (let ((type-list
433 (delete-if
434 #'(lambda (type-number)
dcb31db6 435 (let ((name (find-foreign-type-name type-number)))
4812615b 436 (or
437 (getf (type-options type-number) :ignore)
438 (find-if
439 #'(lambda (options)
440 (and
441 (string-prefix-p (first options) name)
17c607d0 442 (getf (cdr options) :ignore-prefix)
443 (not (some
444 #'(lambda (exception)
445 (string= name exception))
446 (getf (cdr options) :except)))))
4812615b 447 args))))
448 (find-types prefix))))
6556dccd 449
4812615b 450 (dolist (type-number type-list)
dcb31db6 451 (let ((name (find-foreign-type-name type-number)))
4812615b 452 (register-type
453 (getf (type-options type-number) :type (default-type-name name))
80031aba 454 (register-type-as type-number))))
6556dccd 455
456 (let ((sorted-type-list (%sort-types-topologicaly type-list)))
e9934f39 457 `(progn
458 ,@(mapcar
6556dccd 459 #'(lambda (pair)
460 (destructuring-bind (type . forward-p) pair
461 (expand-type-definition type forward-p (type-options type))))
462 sorted-type-list)
e9934f39 463 ,@(mapcar
6556dccd 464 #'(lambda (pair)
465 (destructuring-bind (type . forward-p) pair
466 (when forward-p
467 (expand-type-definition type nil (type-options type)))))
e9934f39 468 sorted-type-list))))))
4812615b 469
3a935dfa 470(defmacro define-types-by-introspection (prefix &rest args)
b011356b 471 (expand-type-definitions prefix args))
6556dccd 472
473
474;;;; Initialize all non static types in GObject
475
476(init-types-in-library #.(concatenate 'string (pkg-config:pkg-variable "glib-2.0" "libdir") "/libgobject-2.0.so"))