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