1 ;; Common Lisp bindings for GTK+ v2.0
2 ;; Copyright (C) 2000 Espen S. Johnsen <espejohn@online.no>
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.
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.
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
18 ;; $Id: gtype.lisp,v 1.1.1.1 2000/08/14 16:44:34 espen Exp $
27 (deftype type-number () '(unsigned 32))
29 (define-foreign ("g_type_name" alien-type-name) (type) (static string)
30 ((find-type-number type) type-number))
32 (define-foreign %type-from-name () type-number
35 ;(define-foreign type-parent () type-number
38 (define-foreign type-instance-size (type) int
39 ((find-type-number type) type-number))
41 (define-foreign type-create-instance (type) pointer
42 ((find-type-number type) type-number))
44 (define-foreign type-free-instance () nil
48 (defvar *type-to-number-hash* (make-hash-table))
49 (defvar *number-to-type-hash* (make-hash-table))
51 (defun type-number-from-alien-name (name &optional (error t))
52 (if (string= name "invalid")
54 (let ((type-number (%type-from-name name)))
56 ((and (zerop type-number) error)
57 (error "Invalid alien type name: ~A" name))
58 ((zerop type-number) nil)
61 (defun (setf alien-type-name) (alien-name type)
62 (let ((type-name (ensure-type-name type))
63 (type-number (type-number-from-alien-name alien-name)))
64 (setf (gethash type-number *number-to-type-hash*) type-name)
65 (setf (gethash type-name *type-to-number-hash*) type-number)))
67 (defun (setf find-type-number) (type-number type)
68 (setf (gethash (ensure-type-name type) *type-to-number-hash*) type-number))
70 (defun find-type-number (type)
73 (symbol (gethash type *type-to-number-hash*))
74 (pcl::class (gethash (class-name type) *type-to-number-hash*))))
76 (defun type-from-number (type-number)
77 (gethash type-number *number-to-type-hash*))
79 (defun type-number-of (object)
80 (find-type-number (type-of object)))
84 ;;;; Superclass for all metaclasses implementing some sort of virtual slots
86 (eval-when (:compile-toplevel :load-toplevel :execute)
87 (defclass virtual-class (pcl::standard-class))
89 (defclass direct-virtual-slot-definition (standard-direct-slot-definition)
91 :reader slot-definition-location
94 (defclass effective-virtual-slot-definition
95 (standard-effective-slot-definition)))
98 (defmethod direct-slot-definition-class ((class virtual-class) initargs)
99 (if (eq (getf initargs :allocation) :virtual)
100 (find-class 'direct-virtual-slot-definition)
104 (defmethod effective-slot-definition-class ((class virtual-class) initargs)
105 (if (eq (getf initargs :allocation) :virtual)
106 (find-class 'effective-virtual-slot-definition)
110 (defun %direct-slot-definitions-slot-value (slotds slot &optional default)
115 (slot-exists-p slotd slot)
116 (slot-boundp slotd slot)))
119 (slot-value slotd slot)
123 (defgeneric compute-virtual-slot-location (class slotd direct-slotds))
125 (defmethod compute-virtual-slot-location
126 ((class virtual-class)
127 (slotd effective-virtual-slot-definition)
130 (%direct-slot-definitions-slot-value direct-slotds 'location)))
131 (if (and location (symbolp location))
132 (list location `(setf ,location))
136 (defmethod compute-effective-slot-definition
137 ((class virtual-class) direct-slotds)
138 (let ((slotd (call-next-method)))
139 (when (typep slotd 'effective-virtual-slot-definition)
141 (slot-value slotd 'pcl::location)
142 (compute-virtual-slot-location class slotd direct-slotds)))
146 (defmethod slot-value-using-class
147 ((class virtual-class) (object standard-object)
148 (slotd effective-virtual-slot-definition))
149 (let ((reader (first (slot-definition-location slotd))))
151 (funcall reader object)
152 (slot-unbound class object (slot-definition-name slotd)))))
155 (defmethod slot-boundp-using-class
156 ((class virtual-class) (object standard-object)
157 (slotd effective-virtual-slot-definition))
158 (and (first (slot-definition-location slotd)) t))
162 (defmethod (setf slot-value-using-class)
163 (value (class virtual-class) (object standard-object)
164 (slotd effective-virtual-slot-definition))
165 (let ((writer (second (slot-definition-location slotd))))
169 "Can't set read-only slot ~A in ~A"
170 (slot-definition-name slotd)
172 ((or (functionp writer) (symbolp writer))
173 (funcall writer value object)
176 (funcall (fdefinition writer) value object)
180 (defmethod validate-superclass
181 ((class virtual-class) (super pcl::standard-class))
186 ;;;; Superclass for wrapping of C structures
188 (eval-when (:compile-toplevel :load-toplevel :execute)
189 (defclass alien-instance ()
191 :reader alien-instance-location
192 :type system-area-pointer)))
194 (defgeneric allocate-alien-storage (class))
195 (defgeneric reference-instance (object))
196 (defgeneric unreference-instance (object))
197 (defgeneric from-alien-initialize-instance (object &rest initargs))
198 (defgeneric instance-finalizer (object)))
201 (internal *instance-cache*)
202 (defvar *instance-cache* (make-hash-table :test #'eql))
204 (defun cache-instance (object)
206 (gethash (system:sap-int (alien-instance-location object)) *instance-cache*)
207 (ext:make-weak-pointer object)))
209 (defun find-cached-instance (location)
210 (let ((ref (gethash (system:sap-int location) *instance-cache*)))
212 (ext:weak-pointer-value ref))))
214 (defun remove-cached-instance (location)
215 (remhash (system:sap-int location) *instance-cache*))
218 (defmethod initialize-instance :before ((instance alien-instance)
220 (declare (ignore initargs))
222 (slot-value instance 'location)
223 (allocate-alien-storage (class-of instance)))
224 (cache-instance instance)
225 (ext:finalize instance (instance-finalizer instance)))
228 (defmethod from-alien-initialize-instance ((instance alien-instance)
229 &rest initargs &key location)
230 (declare (ignore initargs))
231 (setf (slot-value instance 'location) location)
232 (cache-instance instance))
235 (deftype-method translate-type-spec alien-instance (type-spec)
236 (declare (ignore type-spec))
237 'system-area-pointer)
241 ;;;; Metaclass used for subclasses of alien-instance
243 (eval-when (:compile-toplevel :load-toplevel :execute)
244 (defclass alien-class (virtual-class)
246 :reader alien-class-size)))
248 (defclass direct-alien-slot-definition (direct-virtual-slot-definition)
252 :reader slot-definition-offset
256 (defclass effective-alien-slot-definition (effective-virtual-slot-definition)
258 :reader slot-definition-offset)))
260 (defclass effective-virtual-alien-slot-definition
261 (effective-virtual-slot-definition))
264 (defmethod alien-class-superclass ((class alien-class))
267 (subtypep (class-name class) 'alien-instance))
268 (pcl::class-direct-superclasses class)))
271 (defmethod shared-initialize ((class alien-class) names
272 &rest initargs &key size alien-name name)
273 (declare (ignore initargs))
276 ;; For some reason I can't figure out, accessors for only the
277 ;; first direct slot in an alien class gets defined by
278 ;; PCL. Therefore it has to be done here.
279 (pcl::fix-slot-accessors class (class-direct-slots class) 'pcl::add)
282 (setf (alien-type-name (or name (class-name class))) (first alien-name)))
284 (setf (slot-value class 'size) (first size))))
287 (defmethod shared-initialize :after ((class alien-class) names
289 (declare (ignore initargs names))
290 (let* ((super (alien-class-superclass class))
292 (if (eq (class-name super) 'alien-instance)
294 (alien-class-size super))))
295 (dolist (slotd (class-slots class))
296 (when (eq (slot-definition-allocation slotd) :alien)
297 (with-slots (offset type) slotd
298 (setq actual-size (max actual-size (+ offset (size-of type)))))))
300 ((not (slot-boundp class 'size))
301 (setf (slot-value class 'size) actual-size))
302 ((> actual-size (slot-value class 'size))
303 (warn "The actual size of class ~A is lager than specified" class)))))
306 (defmethod direct-slot-definition-class ((class alien-class) initargs)
307 (case (getf initargs :allocation)
308 ((nil :alien) (find-class 'direct-alien-slot-definition))
309 ; (:instance (error "Allocation :instance not allowed in class ~A" class))
310 (t (call-next-method))))
313 (defmethod effective-slot-definition-class ((class alien-class) initargs)
314 (case (getf initargs :allocation)
315 (:alien (find-class 'effective-alien-slot-definition))
316 (:virtual (find-class 'effective-virtual-alien-slot-definition))
317 (t (call-next-method))))
320 (defmethod compute-virtual-slot-location
321 ((class alien-class) (slotd effective-alien-slot-definition)
323 (with-slots (offset type) slotd
324 (setf offset (%direct-slot-definitions-slot-value direct-slotds 'offset))
325 (let ((reader (get-reader-function type))
326 (writer (get-writer-function type))
327 (destroy (get-destroy-function type)))
330 (funcall reader (alien-instance-location object) offset))
331 #'(lambda (value object)
332 (let ((location (alien-instance-location object)))
333 (funcall destroy location offset)
334 (funcall writer value location offset)))))))
337 (defmethod compute-virtual-slot-location
339 (slotd effective-virtual-alien-slot-definition)
341 (let ((location (call-next-method)))
342 (if (or (stringp location) (consp location))
343 (destructuring-bind (reader &optional writer) (mklist location)
344 (with-slots (type) slotd
347 (let* ((alien-type (translate-type-spec type))
350 (alien::make-heap-alien-info
351 :type (alien::parse-alien-type
352 `(function ,alien-type system-area-pointer))
353 :sap-form (system:foreign-symbol-address reader))))
354 (from-alien (get-from-alien-function type)))
359 alien (alien-instance-location object)))))
362 (let* ((alien-type (translate-type-spec type))
365 (alien::make-heap-alien-info
366 :type (alien::parse-alien-type
368 void ,alien-type system-area-pointer))
369 :sap-form (system:foreign-symbol-address writer))))
370 (to-alien (get-to-alien-function type))
371 (cleanup (get-cleanup-function type)))
372 #'(lambda (value object)
373 (let ((alien-value (funcall to-alien value))
374 (location (alien-instance-location object)))
375 (alien-funcall alien location alien-value)
376 (funcall cleanup alien-value))))
381 (defmethod compute-slots ((class alien-class))
382 ;; Translating the user supplied relative (to previous slot) offsets
383 ;; to absolute offsets.
384 ;; This code is broken and have to be fixed for real use.
385 (with-slots (direct-slots) class
386 (let* ((super (alien-class-superclass class))
388 (if (eq (class-name super) 'alien-instance)
390 (alien-class-size super))))
391 (dolist (slotd direct-slots)
392 (when (eq (slot-definition-allocation slotd) :alien)
393 (with-slots (offset type) slotd
395 offset (+ slot-offset offset)
396 slot-offset (+ offset (size-of type)))))))
398 ;; Reverse the direct slot definitions so the effective slots
399 ;; will be in correct order.
400 (setf direct-slots (nreverse direct-slots)))
404 (defmethod validate-superclass ((class alien-class)
405 (super pcl::standard-class))
406 (subtypep (class-name super) 'alien-instance))
408 (defgeneric make-instance-from-alien (class location &rest initargs &key)))
410 (defmethod make-instance-from-alien ((class symbol) location
412 (apply #'make-instance-from-alien (find-class class) location initargs))
414 (defmethod make-instance-from-alien ((class alien-class) location
416 (let ((instance (allocate-instance class)))
418 #'from-alien-initialize-instance
419 instance :location location initargs)
422 (defun ensure-alien-instance (class location &rest initargs)
424 (find-cached-instance location)
425 (apply #'make-instance-from-alien class location initargs)))
427 (defmethod allocate-alien-storage ((class alien-class))
428 (allocate-memory (alien-class-size class)))
432 ;;;; Superclass for wrapping structures with reference counting
434 (eval-when (:compile-toplevel :load-toplevel :execute)
435 (defclass alien-object (alien-instance)
437 (:metaclass alien-class)
440 (define-type-method-fun alien-ref (type-spec))
441 (define-type-method-fun alien-unref (type-spec))
443 (defmethod from-alien-initialize-instance ((object alien-object)
445 (declare (ignore initargs))
447 (reference-instance object))
449 (defmethod instance-finalizer ((object alien-object))
450 (let ((location (alien-instance-location object))
451 (unref (fdefinition (alien-unref (class-of object)))))
452 (declare (type system-area-pointer location) (type function unref))
454 (remove-cached-instance location)
455 (funcall unref location))))
457 (defmethod reference-instance ((object alien-object))
458 (funcall (alien-ref (class-of object)) object)
461 (defmethod unreference-instance ((object alien-object))
462 (funcall (alien-unref (class-of object)) object)
465 (deftype-method translate-to-alien
466 alien-object (type-spec object &optional copy)
468 `(,(alien-ref type-spec) ,object)
469 `(alien-instance-location ,object)))
471 (deftype-method translate-from-alien
472 alien-object (type-spec location &optional alloc)
473 (declare (ignore alloc))
474 `(let ((location ,location))
475 (unless (null-pointer-p location)
476 (ensure-alien-instance ',type-spec location))))
479 cleanup-alien alien-object (type-spec sap &optional copied)
482 (unless (null-pointer-p sap)
483 (,(alien-unref type-spec) sap)))))
487 ;;;; Superclass for wrapping of non-refcounted structures
489 (eval-when (:compile-toplevel :load-toplevel :execute)
490 (defclass alien-structure (alien-instance)
492 :allocation :instance
493 :reader alien-structure-static-p
496 (:metaclass alien-class)
499 (define-type-method-fun alien-copier (type-spec))
500 (define-type-method-fun alien-deallocator (type-spec))
502 (defmethod from-alien-initialize-instance ((structure alien-structure)
503 &rest initargs &key static)
504 (declare (ignore initargs))
506 (setf (slot-value structure 'static) static))
508 (defmethod instance-finalizer ((structure alien-structure))
509 (let ((location (alien-instance-location structure)))
510 (declare (type system-area-pointer location))
511 (if (alien-structure-static-p structure)
513 (remove-cached-instance location))
515 (fdefinition (alien-deallocator (class-of structure)))))
516 (declare (type function deallocator))
518 (remove-cached-instance location)
519 (funcall deallocator location))))))
522 (deftype-method alien-copier alien-structure (type-spec)
523 (declare (ignore type-spec))
526 (deftype-method alien-deallocator alien-structure (type-spec)
527 (declare (ignore type-spec))
530 (deftype-method translate-to-alien
531 alien-structure (type-spec object &optional copy)
532 `(let ((object ,object))
533 (if (and ,copy (not (alien-structure-static-p object)))
534 (,(alien-copier type-spec)
535 `(alien-instance-location object)
536 ,(alien-class-size (find-class type-spec)))
537 (alien-instance-location object))))
539 (deftype-method translate-from-alien
540 alien-structure (type-spec location &optional (alloc :dynamic))
541 `(let ((location ,location))
542 (unless (null-pointer-p location)
544 (:dynamic `(ensure-alien-instance ',type-spec location))
545 (:static `(ensure-alien-instance ',type-spec location :static t))
546 (:copy `(ensure-alien-instance
548 `(,(alien-copier type-spec)
549 location ,(alien-class-size (find-class type-spec)))))))))
551 (deftype-method cleanup-alien alien-structure (type-spec sap &optional copied)
556 (alien-structure-static-p (find-cached-instance sap)))
557 (,(alien-deallocator type-spec) sap)))))
561 ;;;; Superclass for static structures such as gdk:visual
563 (defclass static-structure (alien-structure)
565 (:metaclass alien-class)
569 (defmethod from-alien-initialize-instance ((structure alien-structure)
571 (declare (ignore initargs))
573 (setf (slot-value structure 'static) t))
577 ;;;; Superclass wrapping types in the glib type system
579 (eval-when (:compile-toplevel :load-toplevel :execute)
580 (defclass gtype (alien-object)
582 (:metaclass alien-class)
583 (:size 4 #|(size-of 'pointer)|#)))
586 (defun %alien-instance-type-number (location)
587 (let ((class (sap-ref-sap location 0)))
588 (sap-ref-unsigned class 0)))
591 (deftype-method translate-from-alien gtype (type-spec location &optional alloc)
592 (declare (ignore type-spec alloc))
593 `(let ((location ,location))
594 (unless (null-pointer-p location)
595 (ensure-alien-instance
596 (type-from-number (%alien-instance-type-number location))
601 ;;;; Metaclass for subclasses of gtype-class
603 (eval-when (:compile-toplevel :load-toplevel :execute)
604 (defclass gtype-class (alien-class)))
607 (defmethod shared-initialize ((class gtype-class) names
608 &rest initargs &key name)
609 (declare (ignore initargs names))
612 (slot-value class 'size)
613 (type-instance-size (find-type-number (or name (class-name class))))))
616 (defmethod validate-superclass
617 ((class gtype-class) (super pcl::standard-class))
618 (subtypep (class-name super) 'gtype))
621 (defmethod allocate-alien-storage ((class gtype-class))
622 (type-create-instance (find-type-number class)))
625 ;;;; Initializing type numbers
627 (setf (alien-type-name 'invalid) "invalid")
628 (setf (alien-type-name 'char) "gchar")
629 (setf (alien-type-name 'unsigned-char) "guchar")
630 (setf (alien-type-name 'boolean) "gboolean")
631 (setf (alien-type-name 'int) "gint")
632 (setf (alien-type-name 'unsigned-int) "guint")
633 (setf (alien-type-name 'long) "glong")
634 (setf (alien-type-name 'unsigned-long) "gulong")
635 (setf (alien-type-name 'enum) "GEnum")
636 (setf (alien-type-name 'flags) "GFlags")
637 (setf (alien-type-name 'single-float) "gfloat")
638 (setf (alien-type-name 'double-float) "gdouble")
639 (setf (alien-type-name 'string) "gstring")
640 (setf (find-type-number 'fixnum) (find-type-number 'int))