chiark / gitweb /
Build instructions updated for SBCL with native C callback support
[clg] / glib / gtype.lisp
CommitLineData
55212af1 1;; Common Lisp bindings for GTK+ v2.x
2;; Copyright 2000-2005 Espen S. Johnsen <espen@users.sf.net>
0d07716f 3;;
55212af1 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:
0d07716f 11;;
55212af1 12;; The above copyright notice and this permission notice shall be
13;; included in all copies or substantial portions of the Software.
0d07716f 14;;
55212af1 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
0f68f696 23;; $Id: gtype.lisp,v 1.45 2006/02/19 19:27:32 espen Exp $
0d07716f 24
25(in-package "GLIB")
26
27(use-prefix "g")
28
d1266407 29;; Initialize the glib type system
30(defbinding type-init () nil)
31(type-init)
0d07716f 32
33(deftype type-number () '(unsigned 32))
34
b4a2c852 35(deftype gtype () 'symbol)
36
37(defmethod alien-type ((type (eql 'gtype)) &rest args)
38 (declare (ignore type args))
39 (alien-type 'type-number))
40
41(defmethod size-of ((type (eql 'gtype)) &rest args)
42 (declare (ignore type args))
43 (size-of 'type-number))
44
45(defmethod to-alien-form (gtype (type (eql 'gtype)) &rest args)
46 (declare (ignore type args))
47 `(find-type-number ,gtype t))
48
49(defmethod to-alien-function ((type (eql 'gtype)) &rest args)
50 (declare (ignore type args))
51 #'(lambda (gtype)
52 (find-type-number gtype t)))
53
54(defmethod from-alien-form (type-number (type (eql 'gtype)) &rest args)
55 (declare (ignore type args))
40c346ec 56 `(type-from-number ,type-number))
b4a2c852 57
58(defmethod from-alien-function ((type (eql 'gtype)) &rest args)
59 (declare (ignore type args))
60 #'(lambda (type-number)
40c346ec 61 (type-from-number type-number)))
b4a2c852 62
63(defmethod writer-function ((type (eql 'gtype)) &rest args)
3d36c5d6 64 (declare (ignore type args))
b4a2c852 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(defmethod reader-function ((type (eql 'gtype)) &rest args)
3d36c5d6 70 (declare (ignore type args))
b4a2c852 71 (let ((reader (reader-function 'type-number)))
0739b019 72 #'(lambda (location &optional (offset 0) weak-p)
73 (declare (ignore weak-p))
40c346ec 74 (type-from-number (funcall reader location offset)))))
b4a2c852 75
76
fc47a022 77(eval-when (:compile-toplevel :load-toplevel :execute)
3a935dfa 78 (defclass type-query (struct)
fc47a022 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))
4d1d3921 83 (:metaclass struct-class)))
fc47a022 84
85
b4a2c852 86(defbinding type-query (type) nil
87 ((find-type-number type t) type-number)
88 ((make-instance 'type-query) type-query :return))
fc47a022 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))
0d07716f 95
3a935dfa 96(defbinding type-class-ref (type) pointer
97 ((find-type-number type t) type-number))
0d07716f 98
3a935dfa 99(defbinding type-class-unref (type) nil
100 ((find-type-number type t) type-number))
fc47a022 101
3a935dfa 102(defbinding type-class-peek (type) pointer
103 ((find-type-number type t) type-number))
fc47a022 104
0d07716f 105
3a935dfa 106;;;; Mapping between lisp types and glib types
0d07716f 107
dcb31db6 108(defvar *registered-types* ())
109(defvar *registered-type-aliases* ())
0f68f696 110(defvar *registered-static-types* ())
dcb31db6 111(defvar *lisp-type-to-type-number* (make-hash-table))
112(defvar *type-number-to-lisp-type* (make-hash-table))
3a935dfa 113
114(defbinding %type-from-name () type-number
115 (name string))
116
dcb31db6 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)
0d07716f 160 (etypecase type
161 (integer type)
dcb31db6 162 (string (type-number-from-glib-name type error-p))
3a935dfa 163 (symbol
dcb31db6 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))))
0d07716f 168
b011356b 169(defun type-from-number (type-number &optional error)
170 (multiple-value-bind (type found)
dcb31db6 171 (gethash type-number *type-number-to-lisp-type*)
e40a19fb 172 (if found
173 type
dcb31db6 174 (let ((name (find-foreign-type-name type-number)))
e40a19fb 175 (cond
f53fad52 176 ((and name (not (= (type-number-from-glib-name name nil) type-number)))
e40a19fb 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)))))))
0d07716f 184
dcb31db6 185(defbinding (find-foreign-type-name "g_type_name") (type) (copy-of string)
3a935dfa 186 ((find-type-number type t) type-number))
187
188(defun type-number-of (object)
189 (find-type-number (type-of object) t))
190
6556dccd 191(eval-when (:compile-toplevel :load-toplevel :execute)
dcb31db6 192 (defvar *type-initializers* ())
193 (defun %find-types-in-library (pathname prefixes ignore)
6556dccd 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
dcb31db6 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))
6556dccd 212 (not (member symbol ignore :test #'string=)))
213 collect symbol)
214 (process-close process)))))
215
0d07716f 216
80031aba 217(defmacro init-types-in-library (filename &key prefix ignore)
6556dccd 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)
dcb31db6 223 (,(intern name))
224 (pushnew ',(intern name) *type-initializers*)))
6556dccd 225 names))))
226
dcb31db6 227(defun find-type-init-function (type-number)
80031aba 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)))
dcb31db6 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
6556dccd 244
0a77b51f 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
e40a19fb 261 (parent-type type-number)
0a77b51f 262 (name string)
263 (info type-info)
264 (0 unsigned-int))
265
8fbfa684 266(defun register-new-type (type parent &optional foreign-name)
0a77b51f 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
8fbfa684 272 (or foreign-name (default-alien-type-name type))
0a77b51f 273 (make-instance 'type-info :class-size class-size :instance-size instance-size))))
0f68f696 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))))
0a77b51f 278
279
6556dccd 280
281;;;; Metaclass for subclasses of ginstance
282
283(eval-when (:compile-toplevel :load-toplevel :execute)
284 (defclass ginstance-class (proxy-class)
7bab08b9 285 ((gtype :initarg :gtype :initform nil :reader ginstance-class-gtype))))
f53fad52 286
287
d905d6ef 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
7ce0497d 298
f53fad52 299(defmethod finalize-inheritance ((class ginstance-class))
300 (call-next-method)
301 (let* ((class-name (class-name class))
8fbfa684 302 (super (most-specific-proxy-superclass class))
7bab08b9 303 (gtype (or
304 (first (ginstance-class-gtype class))
305 (default-alien-type-name class-name)))
f53fad52 306 (type-number
dcb31db6 307 (or
308 (find-type-number class-name)
a0b64f21 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))))
8fbfa684 320 (warn "~A is the super type for ~A in the gobject type system."
d905d6ef 321 (supertype type-number) 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
b011356b 332
6556dccd 333(defmethod validate-superclass ((class ginstance-class) (super standard-class))
334 (subtypep (class-name super) 'ginstance))
335
0d07716f 336
fc47a022 337;;;; Superclass for wrapping types in the glib type system
0d07716f 338
339(eval-when (:compile-toplevel :load-toplevel :execute)
fc47a022 340 (defclass ginstance (proxy)
7ce0497d 341 (;(class :allocation :alien :type pointer :offset 0)
342 )
343 (:metaclass proxy-class)
344 (:size #.(size-of 'pointer))))
0d07716f 345
609ba905 346(defun %type-number-of-ginstance (location)
fc47a022 347 (let ((class (sap-ref-sap location 0)))
609ba905 348 (sap-ref-32 class 0)))
0d07716f 349
1d06a422 350(defmethod make-proxy-instance :around ((class ginstance-class) location &rest initargs)
4d1d3921 351 (declare (ignore class))
609ba905 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)))))
1d06a422 358 ;; Note that chancing the class argument must not alter "the
359 ;; ordered set of applicable methods" as specified in the
360 ;; Hyperspec
4d1d3921 361 (if class
1d06a422 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
253c1339 374(defmethod invalidate-instance ((instance ginstance))
375 (declare (ignore instance))
376 ;; A ginstance should never be invalidated since it is ref counted
377 nil)
0d07716f 378
32f0fe2b 379(defmethod callback-from-alien-form (form (type t) &rest args)
380 (apply #'from-alien-form form type args))
381
508d13a7 382(defmethod copy-from-alien-form (location (class ginstance-class) &rest args)
383 (declare (ignore location class args))
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(defmethod copy-from-alien-function ((class ginstance-class) &rest args)
387 (declare (ignore class args))
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(defmethod reader-function ((class ginstance-class) &rest args)
391 (declare (ignore args))
0739b019 392 #'(lambda (location &optional (offset 0) weak-p)
393 (declare (ignore weak-p))
508d13a7 394 (ensure-proxy-instance class (sap-ref-sap location offset))))
395
0d07716f 396
3a935dfa 397;;;; Registering fundamental types
398
40c346ec 399(register-type 'nil "void")
3a935dfa 400(register-type 'pointer "gpointer")
401(register-type 'char "gchar")
402(register-type 'unsigned-char "guchar")
403(register-type 'boolean "gboolean")
3a935dfa 404(register-type 'int "gint")
0b392a0d 405(register-type-alias 'integer 'int)
dcb31db6 406(register-type-alias 'fixnum 'int)
3a935dfa 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")
b77fe850 412(register-type 'pathname "gchararray")
b011356b 413(register-type 'string "gchararray")
3a935dfa 414
415
e9934f39 416;;;; Introspection of type information
3a935dfa 417
4812615b 418(defvar *derivable-type-info* (make-hash-table))
3a935dfa 419
e9934f39 420(defun register-derivable-type (type id expander &optional dependencies)
3a935dfa 421 (register-type type id)
4812615b 422 (let ((type-number (register-type type id)))
e9934f39 423 (setf
424 (gethash type-number *derivable-type-info*)
425 (list expander dependencies))))
3a935dfa 426
b011356b 427(defun find-type-info (type)
428 (dolist (super (cdr (type-hierarchy type)))
4812615b 429 (let ((info (gethash super *derivable-type-info*)))
b011356b 430 (return-if info))))
431
e9934f39 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)))
3a935dfa 435
3a935dfa 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
7858d45e 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
4d1d3921 449 (map-c-vector 'list #'identity array 'type-number length)
7858d45e 450 (deallocate-memory array))))
451
452(defun implements (type)
453 (mapcar #'type-from-number (type-interfaces type)))
454
3a935dfa 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
4d1d3921 472 (map-c-vector
3a935dfa 473 'nil
474 #'(lambda (type-number)
475 (when (or
476 (not prefix)
dcb31db6 477 (string-prefix-p prefix (find-foreign-type-name type-number)))
3a935dfa 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))
4812615b 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*)
3a935dfa 493 type-list))
494
e9934f39 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)
6556dccd 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))
e9934f39 510 (sorted ()))
511
512 (loop
6556dccd 513 as tmp = partial-sorted then (or (rest tmp) partial-sorted)
e9934f39 514 while tmp
515 do (destructuring-bind (type . dependencies) (first tmp)
3a935dfa 516 (cond
e9934f39 517 ((every #'(lambda (dep)
6556dccd 518 (assoc dep sorted))
e9934f39 519 dependencies)
6556dccd 520 (push (cons type nil) sorted) ; no forward definition needed
521 (setq partial-sorted (delete type partial-sorted :key #'first)))
e9934f39 522 ((some #'(lambda (dep)
523 (find type (find-type-dependencies dep)))
524 dependencies)
6556dccd 525 (push (cons type t) sorted) ; forward definition needed
526 (setq partial-sorted (delete type partial-sorted :key #'first))))))
527 (nreverse sorted)))
3a935dfa 528
529
530(defun expand-type-definitions (prefix &optional args)
dcb31db6 531 (flet ((type-options (type-number)
532 (let ((name (find-foreign-type-name type-number)))
b011356b 533 (cdr (assoc name args :test #'string=)))))
3a935dfa 534
4812615b 535 (let ((type-list
536 (delete-if
537 #'(lambda (type-number)
dcb31db6 538 (let ((name (find-foreign-type-name type-number)))
4812615b 539 (or
540 (getf (type-options type-number) :ignore)
541 (find-if
542 #'(lambda (options)
543 (and
544 (string-prefix-p (first options) name)
17c607d0 545 (getf (cdr options) :ignore-prefix)
546 (not (some
547 #'(lambda (exception)
548 (string= name exception))
549 (getf (cdr options) :except)))))
4812615b 550 args))))
551 (find-types prefix))))
6556dccd 552
4812615b 553 (dolist (type-number type-list)
dcb31db6 554 (let ((name (find-foreign-type-name type-number)))
4812615b 555 (register-type
556 (getf (type-options type-number) :type (default-type-name name))
80031aba 557 (register-type-as type-number))))
6556dccd 558
559 (let ((sorted-type-list (%sort-types-topologicaly type-list)))
e9934f39 560 `(progn
561 ,@(mapcar
6556dccd 562 #'(lambda (pair)
563 (destructuring-bind (type . forward-p) pair
564 (expand-type-definition type forward-p (type-options type))))
565 sorted-type-list)
e9934f39 566 ,@(mapcar
6556dccd 567 #'(lambda (pair)
568 (destructuring-bind (type . forward-p) pair
569 (when forward-p
570 (expand-type-definition type nil (type-options type)))))
e9934f39 571 sorted-type-list))))))
4812615b 572
3a935dfa 573(defmacro define-types-by-introspection (prefix &rest args)
b011356b 574 (expand-type-definitions prefix args))
6556dccd 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"))