Type method system redesigned
[clg] / glib / gtype.lisp
1 ;; Common Lisp bindings for GTK+ v2.x
2 ;; Copyright 2000-2005 Espen S. Johnsen <espen@users.sf.net>
3 ;;
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:
11 ;;
12 ;; The above copyright notice and this permission notice shall be
13 ;; included in all copies or substantial portions of the Software.
14 ;;
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.
22
23 ;; $Id: gtype.lisp,v 1.47 2006-02-26 15:30:01 espen Exp $
24
25 (in-package "GLIB")
26
27 (use-prefix "g")
28
29 ;; Initialize the glib type system
30 (defbinding type-init () nil)
31 (type-init)
32
33 (deftype type-number () '(unsigned 32))
34
35 (deftype gtype () 'symbol)
36
37 (define-type-method alien-type ((type gtype))
38 (declare (ignore type))
39 (alien-type 'type-number))
40
41 (define-type-method size-of ((type gtype))
42 (declare (ignore type))
43 (size-of 'type-number))
44
45 (define-type-method to-alien-form ((type gtype) gtype)
46 (declare (ignore type))
47 `(find-type-number ,gtype t))
48
49 (define-type-method to-alien-function ((type gtype))
50 (declare (ignore type))
51 #'(lambda (gtype)
52 (find-type-number gtype t)))
53
54 (define-type-method from-alien-form ((type gtype) type-number)
55 (declare (ignore type))
56 `(type-from-number ,type-number))
57
58 (define-type-method from-alien-function ((type gtype))
59 (declare (ignore type))
60 #'(lambda (type-number)
61 (type-from-number type-number)))
62
63 (define-type-method writer-function ((type gtype))
64 (declare (ignore type))
65 (let ((writer (writer-function 'type-number)))
66 #'(lambda (gtype location &optional (offset 0))
67 (funcall writer (find-type-number gtype t) location offset))))
68
69 (define-type-method reader-function ((type gtype))
70 (declare (ignore type))
71 (let ((reader (reader-function 'type-number)))
72 #'(lambda (location &optional (offset 0) weak-p)
73 (declare (ignore weak-p))
74 (type-from-number (funcall reader location offset)))))
75
76
77 (eval-when (:compile-toplevel :load-toplevel :execute)
78 (defclass type-query (struct)
79 ((type-number :allocation :alien :type type-number)
80 (name :allocation :alien :type string)
81 (class-size :allocation :alien :type unsigned-int)
82 (instance-size :allocation :alien :type unsigned-int))
83 (:metaclass struct-class)))
84
85
86 (defbinding type-query (type) nil
87 ((find-type-number type t) type-number)
88 ((make-instance 'type-query) type-query :return))
89
90 (defun type-instance-size (type)
91 (slot-value (type-query type) 'instance-size))
92
93 (defun type-class-size (type)
94 (slot-value (type-query type) 'class-size))
95
96 (defbinding type-class-ref (type) pointer
97 ((find-type-number type t) type-number))
98
99 (defbinding type-class-unref (type) nil
100 ((find-type-number type t) type-number))
101
102 (defbinding type-class-peek (type) pointer
103 ((find-type-number type t) type-number))
104
105
106 ;;;; Mapping between lisp types and glib types
107
108 (defvar *registered-types* ())
109 (defvar *registered-type-aliases* ())
110 (defvar *registered-static-types* ())
111 (defvar *lisp-type-to-type-number* (make-hash-table))
112 (defvar *type-number-to-lisp-type* (make-hash-table))
113
114 (defbinding %type-from-name () type-number
115 (name string))
116
117 (defun type-number-from-glib-name (name &optional (error-p t))
118 (let ((type-number (%type-from-name name)))
119 (cond
120 ((not (zerop type-number)) type-number)
121 (error-p (error "Invalid gtype name: ~A" name)))))
122
123 (defun register-type (type id)
124 (pushnew (cons type id) *registered-types* :key #'car)
125 (let ((type-number
126 (typecase id
127 (string (type-number-from-glib-name id))
128 (symbol (funcall id)))))
129 (setf (gethash type *lisp-type-to-type-number*) type-number)
130 (setf (gethash type-number *type-number-to-lisp-type*) type)
131 type-number))
132
133 (defun register-type-alias (type alias)
134 (pushnew (cons type alias) *registered-type-aliases* :key #'car)
135 (setf
136 (gethash type *lisp-type-to-type-number*)
137 (find-type-number alias t)))
138
139 (defun reinitialize-all-types ()
140 (clrhash *lisp-type-to-type-number*)
141 (clrhash *type-number-to-lisp-type*)
142 (type-init) ; initialize the glib type system
143 (mapc #'(lambda (type)
144 (register-type (car type) (cdr type)))
145 *registered-types*)
146 (mapc #'(lambda (type)
147 (register-type-alias (car type) (cdr type)))
148 *registered-type-aliases*))
149
150 (pushnew 'reinitialize-all-types
151 #+cmu *after-save-initializations*
152 #+sbcl *init-hooks*)
153
154 #+cmu
155 (pushnew 'system::reinitialize-global-table ; we shouldn't have to do this?
156 *after-save-initializations*)
157
158
159 (defun find-type-number (type &optional error-p)
160 (etypecase type
161 (integer type)
162 (string (type-number-from-glib-name type error-p))
163 (symbol
164 (or
165 (gethash type *lisp-type-to-type-number*)
166 (and error-p (error "Type not registered: ~A" type))))
167 (class (find-type-number (class-name type) error-p))))
168
169 (defun type-from-number (type-number &optional error)
170 (multiple-value-bind (type found)
171 (gethash type-number *type-number-to-lisp-type*)
172 (if found
173 type
174 (let ((name (find-foreign-type-name type-number)))
175 (cond
176 ((and name (not (= (type-number-from-glib-name name nil) type-number)))
177 ;; This is a hack because GdkEvent seems to be registered
178 ;; multiple times
179 (type-from-number (type-number-from-glib-name name)))
180 ((and error name)
181 (error "Type number not registered: ~A (~A)" type-number name))
182 ((and error)
183 (error "Invalid type number: ~A" type-number)))))))
184
185 (defbinding (find-foreign-type-name "g_type_name") (type) (copy-of string)
186 ((find-type-number type t) type-number))
187
188 (defun type-number-of (object)
189 (find-type-number (type-of object) t))
190
191 (eval-when (:compile-toplevel :load-toplevel :execute)
192 (defvar *type-initializers* ())
193 (defun %find-types-in-library (pathname prefixes ignore)
194 (let ((process (run-program
195 "/usr/bin/nm" (list "--defined-only" "-D" (namestring (truename pathname)))
196 :output :stream :wait nil)))
197 (unwind-protect
198 (loop
199 as symbol = (let ((line (read-line (process-output process) nil)))
200 (when line (subseq line 11)))
201 while symbol
202 when (and
203 (> (length symbol) 9)
204 (or
205 (not prefixes)
206 (some #'(lambda (prefix)
207 (and
208 (> (length symbol) (length prefix))
209 (string= prefix symbol :end2 (length prefix))))
210 (mklist prefixes)))
211 (string= "_get_type" symbol :start2 (- (length symbol) 9))
212 (not (member symbol ignore :test #'string=)))
213 collect symbol)
214 (process-close process)))))
215
216
217 (defmacro init-types-in-library (filename &key prefix ignore)
218 (let ((names (%find-types-in-library filename prefix ignore)))
219 `(progn
220 ,@(mapcar #'(lambda (name)
221 `(progn
222 (defbinding (,(intern name) ,name) () type-number)
223 (,(intern name))
224 (pushnew ',(intern name) *type-initializers*)))
225 names))))
226
227 (defun find-type-init-function (type-number)
228 (loop
229 for type-init in *type-initializers*
230 when (= type-number (funcall type-init))
231 do (return type-init)))
232
233 (defun register-type-as (type-number)
234 (or
235 (find-type-init-function type-number)
236 (find-foreign-type-name type-number)
237 (error "Unknown type-number: ~A" type-number)))
238
239 (defun default-type-init-name (type)
240 (find-symbol (format nil "~A_~A_get_type"
241 (package-prefix *package*)
242 (substitute #\_ #\- (string-downcase type)))))
243
244
245 (eval-when (:compile-toplevel :load-toplevel :execute)
246 (defclass type-info (struct)
247 ((class-size :allocation :alien :type (unsigned 16) :initarg :class-size)
248 (base-init :allocation :alien :type pointer)
249 (base-finalize :allocation :alien :type pointer)
250 (class-init :allocation :alien :type pointer)
251 (class-finalize :allocation :alien :type pointer)
252 (class-data :allocation :alien :type pointer)
253 (instance-size :allocation :alien :type (unsigned 16)
254 :initarg :instance-size)
255 (n-preallocs :allocation :alien :type (unsigned 16))
256 (instance-init :allocation :alien :type pointer)
257 (value-table :allocation :alien :type pointer))
258 (:metaclass struct-class)))
259
260 (defbinding %type-register-static () type-number
261 (parent-type type-number)
262 (name string)
263 (info type-info)
264 (0 unsigned-int))
265
266 (defun register-new-type (type parent &optional foreign-name)
267 (let ((parent-info (type-query parent)))
268 (with-slots ((parent-number type-number) class-size instance-size) parent-info
269 (let ((type-number
270 (%type-register-static
271 parent-number
272 (or foreign-name (default-alien-type-name type))
273 (make-instance 'type-info :class-size class-size :instance-size instance-size))))
274 (pushnew (list type parent foreign-name) *registered-static-types* :key #'car)
275 (setf (gethash type *lisp-type-to-type-number*) type-number)
276 (setf (gethash type-number *type-number-to-lisp-type*) type)
277 type-number))))
278
279
280
281 ;;;; Metaclass for subclasses of ginstance
282
283 (eval-when (:compile-toplevel :load-toplevel :execute)
284 (defclass ginstance-class (proxy-class)
285 ((gtype :initarg :gtype :initform nil :reader ginstance-class-gtype))))
286
287
288 (defun update-size (class)
289 (let ((type-number (find-type-number class)))
290 (cond
291 ((not (slot-boundp class 'size))
292 (setf (slot-value class 'size) (type-instance-size type-number)))
293 ((and
294 (slot-boundp class 'size)
295 (not (= (type-instance-size type-number) (slot-value class 'size))))
296 (warn "Size mismatch for class ~A" class)))))
297
298
299 (defmethod finalize-inheritance ((class ginstance-class))
300 (call-next-method)
301 (let* ((class-name (class-name class))
302 (super (most-specific-proxy-superclass class))
303 (gtype (or
304 (first (ginstance-class-gtype class))
305 (default-alien-type-name class-name)))
306 (type-number
307 (or
308 (find-type-number class-name)
309 (let ((type-number
310 (if (or
311 (symbolp gtype)
312 (type-number-from-glib-name gtype nil))
313 (register-type class-name gtype)
314 (register-new-type class-name (class-name super) gtype))))
315 (type-class-ref type-number)
316 type-number))))
317 (when (and
318 (supertype type-number)
319 (not (eq (class-name super) (supertype type-number))))
320 (warn "Super class mismatch between CLOS and GObject for ~A"
321 class-name)))
322
323 (update-size class))
324
325
326 (defmethod shared-initialize ((class ginstance-class) names &rest initargs)
327 (declare (ignore initargs))
328 (call-next-method)
329 (when (class-finalized-p class)
330 (update-size class)))
331
332
333 (defmethod validate-superclass ((class ginstance-class) (super standard-class))
334 (subtypep (class-name super) 'ginstance))
335
336
337 ;;;; Superclass for wrapping types in the glib type system
338
339 (eval-when (:compile-toplevel :load-toplevel :execute)
340 (defclass ginstance (proxy)
341 (;(class :allocation :alien :type pointer :offset 0)
342 )
343 (:metaclass proxy-class)
344 (:size #.(size-of 'pointer))))
345
346 (defun %type-number-of-ginstance (location)
347 (let ((class (sap-ref-sap location 0)))
348 (sap-ref-32 class 0)))
349
350 (defmethod make-proxy-instance :around ((class ginstance-class) location &rest initargs)
351 (declare (ignore class))
352 (let ((class (labels ((find-known-class (type-number)
353 (or
354 (find-class (type-from-number type-number) nil)
355 (unless (zerop type-number)
356 (find-known-class (type-parent type-number))))))
357 (find-known-class (%type-number-of-ginstance location)))))
358 ;; Note that chancing the class argument must not alter "the
359 ;; ordered set of applicable methods" as specified in the
360 ;; Hyperspec
361 (if class
362 (apply #'call-next-method class location initargs)
363 (error "Object at ~A has an unkown type number: ~A"
364 location (%type-number-of-ginstance location)))))
365
366 (defmethod make-proxy-instance ((class ginstance-class) location &rest initargs)
367 (declare (ignore initargs))
368 (reference-foreign class location)
369 ;; Since we make an explicit reference to the foreign object, we
370 ;; always have to release it when the proxy is garbage collected
371 ;; and therefor ignore the weak-p argument.
372 (call-next-method class location :weak nil))
373
374 (defmethod invalidate-instance ((instance ginstance))
375 (declare (ignore instance))
376 ;; A ginstance should never be invalidated since it is ref counted
377 nil)
378
379 (define-type-method copy-from-alien-form ((type ginstance) location)
380 (declare (ignore location type))
381 (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."))
382
383 (define-type-method copy-from-alien-function ((type ginstance))
384 (declare (ignore type))
385 (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."))
386
387 (define-type-method reader-function ((type ginstance))
388 #'(lambda (location &optional (offset 0) weak-p)
389 (declare (ignore weak-p))
390 (ensure-proxy-instance type (sap-ref-sap location offset))))
391
392
393 ;;;; Registering fundamental types
394
395 (register-type 'nil "void")
396 (register-type 'pointer "gpointer")
397 (register-type 'char "gchar")
398 (register-type 'unsigned-char "guchar")
399 (register-type 'boolean "gboolean")
400 (register-type 'int "gint")
401 (register-type-alias 'integer 'int)
402 (register-type-alias 'fixnum 'int)
403 (register-type 'unsigned-int "guint")
404 (register-type 'long "glong")
405 (register-type 'unsigned-long "gulong")
406 (register-type 'single-float "gfloat")
407 (register-type 'double-float "gdouble")
408 (register-type 'pathname "gchararray")
409 (register-type 'string "gchararray")
410
411
412 ;;;; Introspection of type information
413
414 (defvar *derivable-type-info* (make-hash-table))
415
416 (defun register-derivable-type (type id expander &optional dependencies)
417 (register-type type id)
418 (let ((type-number (register-type type id)))
419 (setf
420 (gethash type-number *derivable-type-info*)
421 (list expander dependencies))))
422
423 (defun find-type-info (type)
424 (dolist (super (cdr (type-hierarchy type)))
425 (let ((info (gethash super *derivable-type-info*)))
426 (return-if info))))
427
428 (defun expand-type-definition (type forward-p options)
429 (let ((expander (first (find-type-info type))))
430 (funcall expander (find-type-number type t) forward-p options)))
431
432 (defbinding type-parent (type) type-number
433 ((find-type-number type t) type-number))
434
435 (defun supertype (type)
436 (type-from-number (type-parent type)))
437
438 (defbinding %type-interfaces (type) pointer
439 ((find-type-number type t) type-number)
440 (n-interfaces unsigned-int :out))
441
442 (defun type-interfaces (type)
443 (multiple-value-bind (array length) (%type-interfaces type)
444 (unwind-protect
445 (map-c-vector 'list #'identity array 'type-number length)
446 (deallocate-memory array))))
447
448 (defun implements (type)
449 (mapcar #'type-from-number (type-interfaces type)))
450
451 (defun type-hierarchy (type)
452 (let ((type-number (find-type-number type t)))
453 (unless (= type-number 0)
454 (cons type-number (type-hierarchy (type-parent type-number))))))
455
456 (defbinding (type-is-p "g_type_is_a") (type super) boolean
457 ((find-type-number type) type-number)
458 ((find-type-number super) type-number))
459
460 (defbinding %type-children () pointer
461 (type-number type-number)
462 (num-children unsigned-int :out))
463
464 (defun map-subtypes (function type &optional prefix)
465 (let ((type-number (find-type-number type t)))
466 (multiple-value-bind (array length) (%type-children type-number)
467 (unwind-protect
468 (map-c-vector
469 'nil
470 #'(lambda (type-number)
471 (when (or
472 (not prefix)
473 (string-prefix-p prefix (find-foreign-type-name type-number)))
474 (funcall function type-number))
475 (map-subtypes function type-number prefix))
476 array 'type-number length)
477 (deallocate-memory array)))))
478
479 (defun find-types (prefix)
480 (let ((type-list nil))
481 (maphash
482 #'(lambda (type-number expander)
483 (declare (ignore expander))
484 (map-subtypes
485 #'(lambda (type-number)
486 (pushnew type-number type-list))
487 type-number prefix))
488 *derivable-type-info*)
489 type-list))
490
491 (defun find-type-dependencies (type)
492 (let ((list-dependencies (second (find-type-info type))))
493 (when list-dependencies
494 (funcall list-dependencies (find-type-number type t)))))
495
496 (defun %sort-types-topologicaly (types)
497 (let ((partial-sorted
498 (sort
499 (mapcar
500 #'(lambda (type)
501 (cons type (remove-if #'(lambda (dep)
502 (not (find dep types)))
503 (find-type-dependencies type))))
504 types)
505 #'(lambda (type1 type2) (type-is-p type2 type1)) :key #'car))
506 (sorted ()))
507
508 (loop
509 as tmp = partial-sorted then (or (rest tmp) partial-sorted)
510 while tmp
511 do (destructuring-bind (type . dependencies) (first tmp)
512 (cond
513 ((every #'(lambda (dep)
514 (assoc dep sorted))
515 dependencies)
516 (push (cons type nil) sorted) ; no forward definition needed
517 (setq partial-sorted (delete type partial-sorted :key #'first)))
518 ((some #'(lambda (dep)
519 (find type (find-type-dependencies dep)))
520 dependencies)
521 (push (cons type t) sorted) ; forward definition needed
522 (setq partial-sorted (delete type partial-sorted :key #'first))))))
523 (nreverse sorted)))
524
525
526 (defun expand-type-definitions (prefix &optional args)
527 (flet ((type-options (type-number)
528 (let ((name (find-foreign-type-name type-number)))
529 (cdr (assoc name args :test #'string=)))))
530
531 (let ((type-list
532 (delete-if
533 #'(lambda (type-number)
534 (let ((name (find-foreign-type-name type-number)))
535 (or
536 (getf (type-options type-number) :ignore)
537 (find-if
538 #'(lambda (options)
539 (and
540 (string-prefix-p (first options) name)
541 (getf (cdr options) :ignore-prefix)
542 (not (some
543 #'(lambda (exception)
544 (string= name exception))
545 (getf (cdr options) :except)))))
546 args))))
547 (find-types prefix))))
548
549 (dolist (type-number type-list)
550 (let ((name (find-foreign-type-name type-number)))
551 (register-type
552 (getf (type-options type-number) :type (default-type-name name))
553 (register-type-as type-number))))
554
555 (let ((sorted-type-list (%sort-types-topologicaly type-list)))
556 `(progn
557 ,@(mapcar
558 #'(lambda (pair)
559 (destructuring-bind (type . forward-p) pair
560 (expand-type-definition type forward-p (type-options type))))
561 sorted-type-list)
562 ,@(mapcar
563 #'(lambda (pair)
564 (destructuring-bind (type . forward-p) pair
565 (when forward-p
566 (expand-type-definition type nil (type-options type)))))
567 sorted-type-list))))))
568
569 (defmacro define-types-by-introspection (prefix &rest args)
570 (expand-type-definitions prefix args))
571
572
573 ;;;; Initialize all non static types in GObject
574
575 (init-types-in-library #.(concatenate 'string (pkg-config:pkg-variable "glib-2.0" "libdir") "/libgobject-2.0.so"))