Sub-classing of gobjects can now be done without explicit registering the new class...
[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
8fbfa684 23;; $Id: gtype.lisp,v 1.35 2006/02/01 17:46:11 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)))
72 #'(lambda (location &optional (offset 0))
40c346ec 73 (type-from-number (funcall reader location offset)))))
b4a2c852 74
75
fc47a022 76(eval-when (:compile-toplevel :load-toplevel :execute)
3a935dfa 77 (defclass type-query (struct)
fc47a022 78 ((type-number :allocation :alien :type type-number)
79 (name :allocation :alien :type string)
80 (class-size :allocation :alien :type unsigned-int)
81 (instance-size :allocation :alien :type unsigned-int))
4d1d3921 82 (:metaclass struct-class)))
fc47a022 83
84
b4a2c852 85(defbinding type-query (type) nil
86 ((find-type-number type t) type-number)
87 ((make-instance 'type-query) type-query :return))
fc47a022 88
89(defun type-instance-size (type)
90 (slot-value (type-query type) 'instance-size))
91
92(defun type-class-size (type)
93 (slot-value (type-query type) 'class-size))
0d07716f 94
3a935dfa 95(defbinding type-class-ref (type) pointer
96 ((find-type-number type t) type-number))
0d07716f 97
3a935dfa 98(defbinding type-class-unref (type) nil
99 ((find-type-number type t) type-number))
fc47a022 100
3a935dfa 101(defbinding type-class-peek (type) pointer
102 ((find-type-number type t) type-number))
fc47a022 103
0d07716f 104
3a935dfa 105;;;; Mapping between lisp types and glib types
0d07716f 106
dcb31db6 107(defvar *registered-types* ())
108(defvar *registered-type-aliases* ())
109(defvar *lisp-type-to-type-number* (make-hash-table))
110(defvar *type-number-to-lisp-type* (make-hash-table))
3a935dfa 111
112(defbinding %type-from-name () type-number
113 (name string))
114
dcb31db6 115(defun type-number-from-glib-name (name &optional (error-p t))
116 (let ((type-number (%type-from-name name)))
117 (cond
118 ((not (zerop type-number)) type-number)
119 (error-p (error "Invalid gtype name: ~A" name)))))
120
121(defun register-type (type id)
122 (pushnew (cons type id) *registered-types* :key #'car)
123 (let ((type-number
124 (typecase id
125 (string (type-number-from-glib-name id))
126 (symbol (funcall id)))))
127 (setf (gethash type *lisp-type-to-type-number*) type-number)
128 (setf (gethash type-number *type-number-to-lisp-type*) type)
129 type-number))
130
131(defun register-type-alias (type alias)
132 (pushnew (cons type alias) *registered-type-aliases* :key #'car)
133 (setf
134 (gethash type *lisp-type-to-type-number*)
135 (find-type-number alias t)))
136
137(defun reinitialize-all-types ()
138 (clrhash *lisp-type-to-type-number*)
139 (clrhash *type-number-to-lisp-type*)
140 (type-init) ; initialize the glib type system
141 (mapc #'(lambda (type)
142 (register-type (car type) (cdr type)))
143 *registered-types*)
144 (mapc #'(lambda (type)
145 (register-type-alias (car type) (cdr type)))
146 *registered-type-aliases*))
147
148(pushnew 'reinitialize-all-types
149 #+cmu *after-save-initializations*
150 #+sbcl *init-hooks*)
151
152#+cmu
153(pushnew 'system::reinitialize-global-table ; we shouldn't have to do this?
154 *after-save-initializations*)
155
156
157(defun find-type-number (type &optional error-p)
0d07716f 158 (etypecase type
159 (integer type)
dcb31db6 160 (string (type-number-from-glib-name type error-p))
3a935dfa 161 (symbol
dcb31db6 162 (or
163 (gethash type *lisp-type-to-type-number*)
164 (and error-p (error "Type not registered: ~A" type))))
165 (class (find-type-number (class-name type) error-p))))
0d07716f 166
b011356b 167(defun type-from-number (type-number &optional error)
168 (multiple-value-bind (type found)
dcb31db6 169 (gethash type-number *type-number-to-lisp-type*)
e40a19fb 170 (if found
171 type
dcb31db6 172 (let ((name (find-foreign-type-name type-number)))
e40a19fb 173 (cond
174 ((and name (type-number-from-glib-name name nil))
175 ;; This is a hack because GdkEvent seems to be registered
176 ;; multiple times
177 (type-from-number (type-number-from-glib-name name)))
178 ((and error name)
179 (error "Type number not registered: ~A (~A)" type-number name))
180 ((and error)
181 (error "Invalid type number: ~A" type-number)))))))
0d07716f 182
dcb31db6 183(defbinding (find-foreign-type-name "g_type_name") (type) (copy-of string)
3a935dfa 184 ((find-type-number type t) type-number))
185
186(defun type-number-of (object)
187 (find-type-number (type-of object) t))
188
6556dccd 189(eval-when (:compile-toplevel :load-toplevel :execute)
dcb31db6 190 (defvar *type-initializers* ())
191 (defun %find-types-in-library (pathname prefixes ignore)
6556dccd 192 (let ((process (run-program
193 "/usr/bin/nm" (list "--defined-only" "-D" (namestring (truename pathname)))
194 :output :stream :wait nil)))
195 (unwind-protect
196 (loop
197 as symbol = (let ((line (read-line (process-output process) nil)))
198 (when line (subseq line 11)))
199 while symbol
200 when (and
dcb31db6 201 (> (length symbol) 9)
202 (or
203 (not prefixes)
204 (some #'(lambda (prefix)
205 (and
206 (> (length symbol) (length prefix))
207 (string= prefix symbol :end2 (length prefix))))
208 (mklist prefixes)))
209 (string= "_get_type" symbol :start2 (- (length symbol) 9))
6556dccd 210 (not (member symbol ignore :test #'string=)))
211 collect symbol)
212 (process-close process)))))
213
0d07716f 214
80031aba 215(defmacro init-types-in-library (filename &key prefix ignore)
6556dccd 216 (let ((names (%find-types-in-library filename prefix ignore)))
217 `(progn
218 ,@(mapcar #'(lambda (name)
219 `(progn
220 (defbinding (,(intern name) ,name) () type-number)
dcb31db6 221 (,(intern name))
222 (pushnew ',(intern name) *type-initializers*)))
6556dccd 223 names))))
224
dcb31db6 225(defun find-type-init-function (type-number)
80031aba 226 (loop
227 for type-init in *type-initializers*
228 when (= type-number (funcall type-init))
229 do (return type-init)))
230
231(defun register-type-as (type-number)
232 (or
233 (find-type-init-function type-number)
234 (find-foreign-type-name type-number)
235 (error "Unknown type-number: ~A" type-number)))
dcb31db6 236
237(defun default-type-init-name (type)
238 (find-symbol (format nil "~A_~A_get_type"
239 (package-prefix *package*)
240 (substitute #\_ #\- (string-downcase type)))))
241
6556dccd 242
0a77b51f 243(eval-when (:compile-toplevel :load-toplevel :execute)
244 (defclass type-info (struct)
245 ((class-size :allocation :alien :type (unsigned 16) :initarg :class-size)
246 (base-init :allocation :alien :type pointer)
247 (base-finalize :allocation :alien :type pointer)
248 (class-init :allocation :alien :type pointer)
249 (class-finalize :allocation :alien :type pointer)
250 (class-data :allocation :alien :type pointer)
251 (instance-size :allocation :alien :type (unsigned 16)
252 :initarg :instance-size)
253 (n-preallocs :allocation :alien :type (unsigned 16))
254 (instance-init :allocation :alien :type pointer)
255 (value-table :allocation :alien :type pointer))
256 (:metaclass struct-class)))
257
258(defbinding %type-register-static () type-number
e40a19fb 259 (parent-type type-number)
0a77b51f 260 (name string)
261 (info type-info)
262 (0 unsigned-int))
263
8fbfa684 264(defun register-new-type (type parent &optional foreign-name)
0a77b51f 265 (let ((parent-info (type-query parent)))
266 (with-slots ((parent-number type-number) class-size instance-size) parent-info
267 (let ((type-number
268 (%type-register-static
269 parent-number
8fbfa684 270 (or foreign-name (default-alien-type-name type))
0a77b51f 271 (make-instance 'type-info :class-size class-size :instance-size instance-size))))
272 (setf (gethash type *lisp-type-to-type-number*) type-number)
273 (setf (gethash type-number *type-number-to-lisp-type*) type)
274 type-number))))
275
276
6556dccd 277
278;;;; Metaclass for subclasses of ginstance
279
280(eval-when (:compile-toplevel :load-toplevel :execute)
281 (defclass ginstance-class (proxy-class)
282 ()))
283
284
8fbfa684 285(defmethod shared-initialize :after ((class ginstance-class) names &key name gtype)
6556dccd 286 (let* ((class-name (or name (class-name class)))
8fbfa684 287 (super (most-specific-proxy-superclass class))
288 (foreign-name (or (first gtype) (default-type-init-name class-name)))
dcb31db6 289 (type-number
290 (or
291 (find-type-number class-name)
8fbfa684 292 (if (type-number-from-glib-name foreign-name nil)
293 (register-type class-name foreign-name)
294 (register-new-type class-name (class-name super) foreign-name)))))
295 (unless (eq (class-name super) (supertype type-number))
296 (warn "~A is the super type for ~A in the gobject type system."
297 (supertype type-number) class-name))
298
299 (unless (slot-boundp class 'size)
300 (setf (slot-value class 'size) (type-instance-size type-number)))))
b011356b 301
302
6556dccd 303(defmethod validate-superclass ((class ginstance-class) (super standard-class))
304 (subtypep (class-name super) 'ginstance))
305
0d07716f 306
fc47a022 307;;;; Superclass for wrapping types in the glib type system
0d07716f 308
309(eval-when (:compile-toplevel :load-toplevel :execute)
fc47a022 310 (defclass ginstance (proxy)
3a935dfa 311 ((class :allocation :alien :type pointer))
312 (:metaclass proxy-class)))
0d07716f 313
609ba905 314(defun %type-number-of-ginstance (location)
fc47a022 315 (let ((class (sap-ref-sap location 0)))
609ba905 316 (sap-ref-32 class 0)))
0d07716f 317
4d1d3921 318(defmethod ensure-proxy-instance ((class ginstance-class) location)
319 (declare (ignore class))
609ba905 320 (let ((class (labels ((find-known-class (type-number)
321 (or
322 (find-class (type-from-number type-number) nil)
323 (unless (zerop type-number)
324 (find-known-class (type-parent type-number))))))
325 (find-known-class (%type-number-of-ginstance location)))))
4d1d3921 326 (if class
327 (make-instance class :location (reference-foreign class location))
609ba905 328 (error "Object at ~A has an unkown type number: ~A"
329 location (%type-number-of-ginstance location)))))
0d07716f 330
508d13a7 331(defmethod copy-from-alien-form (location (class ginstance-class) &rest args)
332 (declare (ignore location class args))
333 (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."))
334
335(defmethod copy-from-alien-function ((class ginstance-class) &rest args)
336 (declare (ignore class args))
337 (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."))
338
339(defmethod reader-function ((class ginstance-class) &rest args)
340 (declare (ignore args))
341 #'(lambda (location &optional (offset 0))
342 (ensure-proxy-instance class (sap-ref-sap location offset))))
343
0d07716f 344
3a935dfa 345;;;; Registering fundamental types
346
40c346ec 347(register-type 'nil "void")
3a935dfa 348(register-type 'pointer "gpointer")
349(register-type 'char "gchar")
350(register-type 'unsigned-char "guchar")
351(register-type 'boolean "gboolean")
3a935dfa 352(register-type 'int "gint")
0b392a0d 353(register-type-alias 'integer 'int)
dcb31db6 354(register-type-alias 'fixnum 'int)
3a935dfa 355(register-type 'unsigned-int "guint")
356(register-type 'long "glong")
357(register-type 'unsigned-long "gulong")
358(register-type 'single-float "gfloat")
359(register-type 'double-float "gdouble")
b77fe850 360(register-type 'pathname "gchararray")
b011356b 361(register-type 'string "gchararray")
3a935dfa 362
363
e9934f39 364;;;; Introspection of type information
3a935dfa 365
4812615b 366(defvar *derivable-type-info* (make-hash-table))
3a935dfa 367
e9934f39 368(defun register-derivable-type (type id expander &optional dependencies)
3a935dfa 369 (register-type type id)
4812615b 370 (let ((type-number (register-type type id)))
e9934f39 371 (setf
372 (gethash type-number *derivable-type-info*)
373 (list expander dependencies))))
3a935dfa 374
b011356b 375(defun find-type-info (type)
376 (dolist (super (cdr (type-hierarchy type)))
4812615b 377 (let ((info (gethash super *derivable-type-info*)))
b011356b 378 (return-if info))))
379
e9934f39 380(defun expand-type-definition (type forward-p options)
381 (let ((expander (first (find-type-info type))))
382 (funcall expander (find-type-number type t) forward-p options)))
3a935dfa 383
3a935dfa 384(defbinding type-parent (type) type-number
385 ((find-type-number type t) type-number))
386
387(defun supertype (type)
388 (type-from-number (type-parent type)))
389
7858d45e 390(defbinding %type-interfaces (type) pointer
391 ((find-type-number type t) type-number)
392 (n-interfaces unsigned-int :out))
393
394(defun type-interfaces (type)
395 (multiple-value-bind (array length) (%type-interfaces type)
396 (unwind-protect
4d1d3921 397 (map-c-vector 'list #'identity array 'type-number length)
7858d45e 398 (deallocate-memory array))))
399
400(defun implements (type)
401 (mapcar #'type-from-number (type-interfaces type)))
402
3a935dfa 403(defun type-hierarchy (type)
404 (let ((type-number (find-type-number type t)))
405 (unless (= type-number 0)
406 (cons type-number (type-hierarchy (type-parent type-number))))))
407
408(defbinding (type-is-p "g_type_is_a") (type super) boolean
409 ((find-type-number type) type-number)
410 ((find-type-number super) type-number))
411
412(defbinding %type-children () pointer
413 (type-number type-number)
414 (num-children unsigned-int :out))
415
416(defun map-subtypes (function type &optional prefix)
417 (let ((type-number (find-type-number type t)))
418 (multiple-value-bind (array length) (%type-children type-number)
419 (unwind-protect
4d1d3921 420 (map-c-vector
3a935dfa 421 'nil
422 #'(lambda (type-number)
423 (when (or
424 (not prefix)
dcb31db6 425 (string-prefix-p prefix (find-foreign-type-name type-number)))
3a935dfa 426 (funcall function type-number))
427 (map-subtypes function type-number prefix))
428 array 'type-number length)
429 (deallocate-memory array)))))
430
431(defun find-types (prefix)
432 (let ((type-list nil))
4812615b 433 (maphash
434 #'(lambda (type-number expander)
435 (declare (ignore expander))
436 (map-subtypes
437 #'(lambda (type-number)
438 (pushnew type-number type-list))
439 type-number prefix))
440 *derivable-type-info*)
3a935dfa 441 type-list))
442
e9934f39 443(defun find-type-dependencies (type)
444 (let ((list-dependencies (second (find-type-info type))))
445 (when list-dependencies
446 (funcall list-dependencies (find-type-number type t)))))
447
448(defun %sort-types-topologicaly (types)
6556dccd 449 (let ((partial-sorted
450 (sort
451 (mapcar
452 #'(lambda (type)
453 (cons type (remove-if #'(lambda (dep)
454 (not (find dep types)))
455 (find-type-dependencies type))))
456 types)
457 #'(lambda (type1 type2) (type-is-p type2 type1)) :key #'car))
e9934f39 458 (sorted ()))
459
460 (loop
6556dccd 461 as tmp = partial-sorted then (or (rest tmp) partial-sorted)
e9934f39 462 while tmp
463 do (destructuring-bind (type . dependencies) (first tmp)
3a935dfa 464 (cond
e9934f39 465 ((every #'(lambda (dep)
6556dccd 466 (assoc dep sorted))
e9934f39 467 dependencies)
6556dccd 468 (push (cons type nil) sorted) ; no forward definition needed
469 (setq partial-sorted (delete type partial-sorted :key #'first)))
e9934f39 470 ((some #'(lambda (dep)
471 (find type (find-type-dependencies dep)))
472 dependencies)
6556dccd 473 (push (cons type t) sorted) ; forward definition needed
474 (setq partial-sorted (delete type partial-sorted :key #'first))))))
475 (nreverse sorted)))
3a935dfa 476
477
478(defun expand-type-definitions (prefix &optional args)
dcb31db6 479 (flet ((type-options (type-number)
480 (let ((name (find-foreign-type-name type-number)))
b011356b 481 (cdr (assoc name args :test #'string=)))))
3a935dfa 482
4812615b 483 (let ((type-list
484 (delete-if
485 #'(lambda (type-number)
dcb31db6 486 (let ((name (find-foreign-type-name type-number)))
4812615b 487 (or
488 (getf (type-options type-number) :ignore)
489 (find-if
490 #'(lambda (options)
491 (and
492 (string-prefix-p (first options) name)
17c607d0 493 (getf (cdr options) :ignore-prefix)
494 (not (some
495 #'(lambda (exception)
496 (string= name exception))
497 (getf (cdr options) :except)))))
4812615b 498 args))))
499 (find-types prefix))))
6556dccd 500
4812615b 501 (dolist (type-number type-list)
dcb31db6 502 (let ((name (find-foreign-type-name type-number)))
4812615b 503 (register-type
504 (getf (type-options type-number) :type (default-type-name name))
80031aba 505 (register-type-as type-number))))
6556dccd 506
507 (let ((sorted-type-list (%sort-types-topologicaly type-list)))
e9934f39 508 `(progn
509 ,@(mapcar
6556dccd 510 #'(lambda (pair)
511 (destructuring-bind (type . forward-p) pair
512 (expand-type-definition type forward-p (type-options type))))
513 sorted-type-list)
e9934f39 514 ,@(mapcar
6556dccd 515 #'(lambda (pair)
516 (destructuring-bind (type . forward-p) pair
517 (when forward-p
518 (expand-type-definition type nil (type-options type)))))
e9934f39 519 sorted-type-list))))))
4812615b 520
3a935dfa 521(defmacro define-types-by-introspection (prefix &rest args)
b011356b 522 (expand-type-definitions prefix args))
6556dccd 523
524
525;;;; Initialize all non static types in GObject
526
527(init-types-in-library #.(concatenate 'string (pkg-config:pkg-variable "glib-2.0" "libdir") "/libgobject-2.0.so"))