560af5c5 |
1 | ;; Common Lisp bindings for GTK+ v2.0 |
c8c48a4c |
2 | ;; Copyright (C) 2000 Espen S. Johnsen <esj@stud.cs.uit.no> |
560af5c5 |
3 | ;; |
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. |
8 | ;; |
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. |
13 | ;; |
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 |
17 | |
7479d92c |
18 | ;; $Id: gtype.lisp,v 1.9 2001-04-30 11:25:25 espen Exp $ |
560af5c5 |
19 | |
20 | (in-package "GLIB") |
21 | |
22 | (use-prefix "g") |
23 | |
24 | |
25 | ;;;; |
26 | |
27 | (deftype type-number () '(unsigned 32)) |
28 | |
93aa67db |
29 | (eval-when (:compile-toplevel :load-toplevel :execute) |
30 | (defclass type-query (alien-structure) |
31 | ((type-number :allocation :alien :type type-number) |
32 | (name :allocation :alien :type string) |
33 | (class-size :allocation :alien :type unsigned-int) |
34 | (instance-size :allocation :alien :type unsigned-int)) |
35 | (:metaclass proxy-class))) |
36 | |
37 | |
38 | (defbinding ("g_type_name" alien-type-name) (type) (static string) |
560af5c5 |
39 | ((find-type-number type) type-number)) |
40 | |
93aa67db |
41 | (defbinding %type-from-name () type-number |
560af5c5 |
42 | (name string)) |
43 | |
93aa67db |
44 | (defbinding type-parent () type-number |
45 | (type type-number)) |
560af5c5 |
46 | |
93aa67db |
47 | (defbinding %type-query () nil |
48 | (type type-number) |
49 | (query type-query)) |
50 | |
51 | (defun type-query (type) |
52 | (let ((query (make-instance 'type-query))) |
53 | (%type-query (find-type-number type) query) |
54 | query)) |
55 | |
56 | (defun type-instance-size (type) |
57 | (slot-value (type-query type) 'instance-size)) |
58 | |
59 | (defun type-class-size (type) |
60 | (slot-value (type-query type) 'class-size)) |
560af5c5 |
61 | |
93aa67db |
62 | (defbinding type-class-ref () pointer |
63 | (type type-number)) |
560af5c5 |
64 | |
93aa67db |
65 | (defbinding type-class-unref () nil |
66 | (type type-number)) |
67 | |
68 | (defbinding type-class-peek () pointer |
69 | (type type-number)) |
70 | |
71 | (defbinding type-create-instance (type) pointer |
72 | ((find-type-number type) type-number)) |
73 | |
74 | (defbinding type-free-instance () nil |
560af5c5 |
75 | (instance pointer)) |
76 | |
77 | |
78 | (defvar *type-to-number-hash* (make-hash-table)) |
79 | (defvar *number-to-type-hash* (make-hash-table)) |
80 | |
81 | (defun type-number-from-alien-name (name &optional (error t)) |
82 | (if (string= name "invalid") |
83 | 0 |
84 | (let ((type-number (%type-from-name name))) |
85 | (cond |
86 | ((and (zerop type-number) error) |
87 | (error "Invalid alien type name: ~A" name)) |
88 | ((zerop type-number) nil) |
89 | (t type-number))))) |
90 | |
91 | (defun (setf alien-type-name) (alien-name type) |
92 | (let ((type-name (ensure-type-name type)) |
93 | (type-number (type-number-from-alien-name alien-name))) |
94 | (setf (gethash type-number *number-to-type-hash*) type-name) |
95 | (setf (gethash type-name *type-to-number-hash*) type-number))) |
96 | |
97 | (defun (setf find-type-number) (type-number type) |
98 | (setf (gethash (ensure-type-name type) *type-to-number-hash*) type-number)) |
99 | |
100 | (defun find-type-number (type) |
101 | (etypecase type |
102 | (integer type) |
103 | (symbol (gethash type *type-to-number-hash*)) |
104 | (pcl::class (gethash (class-name type) *type-to-number-hash*)))) |
105 | |
106 | (defun type-from-number (type-number) |
107 | (gethash type-number *number-to-type-hash*)) |
108 | |
109 | (defun type-number-of (object) |
110 | (find-type-number (type-of object))) |
111 | |
93aa67db |
112 | (defun type-init (name &optional init-fname) |
113 | (funcall |
7479d92c |
114 | (mkbinding |
115 | (or init-fname (default-alien-fname (format nil "~A_get_type" name))) |
116 | 'type-number))) |
560af5c5 |
117 | |
118 | |
93aa67db |
119 | ;;;; Superclass for wrapping types in the glib type system |
560af5c5 |
120 | |
121 | (eval-when (:compile-toplevel :load-toplevel :execute) |
93aa67db |
122 | (defclass ginstance (proxy) |
560af5c5 |
123 | () |
93aa67db |
124 | (:metaclass proxy-class) |
125 | (:size 4 #|(size-of 'pointer|#))) |
560af5c5 |
126 | |
93aa67db |
127 | (defmethod initialize-proxy ((instance ginstance) &rest initargs &key location) |
560af5c5 |
128 | (declare (ignore initargs)) |
93aa67db |
129 | (setf |
130 | (slot-value instance 'location) |
131 | (funcall (ginstance-class-ref (class-of instance)) location)) |
132 | (call-next-method)) |
560af5c5 |
133 | |
93aa67db |
134 | (defmethod instance-finalizer ((instance ginstance)) |
135 | (let ((location (proxy-location instance)) |
136 | (unref (ginstance-class-unref (class-of instance)))) |
137 | (declare (type system-area-pointer location)) |
560af5c5 |
138 | #'(lambda () |
93aa67db |
139 | (funcall unref location) |
140 | (remove-cached-instance location)))) |
560af5c5 |
141 | |
93aa67db |
142 | (defun %type-of-ginstance (location) |
143 | (let ((class (sap-ref-sap location 0))) |
144 | (type-from-number (sap-ref-unsigned class 0)))) |
560af5c5 |
145 | |
146 | (deftype-method translate-from-alien |
93aa67db |
147 | ginstance (type-spec location &optional weak-ref) |
148 | (declare (ignore type-spec)) |
560af5c5 |
149 | `(let ((location ,location)) |
150 | (unless (null-pointer-p location) |
93aa67db |
151 | (ensure-proxy-instance |
152 | (%type-of-ginstance location) location ,weak-ref)))) |
560af5c5 |
153 | |
93aa67db |
154 | (deftype-method translate-to-alien |
155 | ginstance (type-spec object &optional weak-ref) |
560af5c5 |
156 | (declare (ignore type-spec)) |
93aa67db |
157 | (if weak-ref |
158 | `(proxy-location ,object) |
159 | `(let ((object ,object)) |
160 | (funcall |
161 | (ginstance-class-ref (class-of object)) (proxy-location object))))) |
560af5c5 |
162 | |
93aa67db |
163 | (deftype-method unreference-alien ginstance (type-spec location) |
560af5c5 |
164 | (declare (ignore type-spec)) |
93aa67db |
165 | `(let* ((location ,location) |
166 | (class (find-class (%type-of-ginstance location)))) |
167 | (funcall (ginstance-class-unref class) location))) |
560af5c5 |
168 | |
560af5c5 |
169 | |
170 | |
93aa67db |
171 | ;;;; Metaclass for subclasses of ginstance |
560af5c5 |
172 | |
173 | (eval-when (:compile-toplevel :load-toplevel :execute) |
93aa67db |
174 | (defclass ginstance-class (proxy-class) |
175 | ((ref :reader ginstance-class-ref) |
176 | (unref :reader ginstance-class-unref)))) |
560af5c5 |
177 | |
178 | |
c8c48a4c |
179 | (defmethod shared-initialize ((class ginstance-class) names |
93aa67db |
180 | &rest initargs |
181 | &key name alien-name size |
182 | ref unref type-init) |
560af5c5 |
183 | (declare (ignore initargs names)) |
184 | (call-next-method) |
93aa67db |
185 | |
186 | (let* ((class-name (or name (class-name class))) |
187 | (type-number |
188 | (cond |
189 | ((and alien-name type-init) |
190 | (error |
191 | "Specify either :type-init or :alien-name for class ~A" |
192 | class-name)) |
193 | (alien-name (type-number-from-alien-name (first alien-name))) |
194 | (type-init (type-init class-name (first type-init))) |
195 | (t |
196 | (or |
197 | (type-number-from-alien-name |
198 | (default-alien-type-name class-name) nil) |
199 | (type-init class-name)))))) |
200 | (setf (find-type-number class) type-number) |
201 | (unless size |
202 | (setf |
203 | (slot-value class 'size) |
204 | (type-instance-size (find-type-number class-name)))) |
205 | (when ref |
206 | (setf |
207 | (slot-value class 'ref) |
7479d92c |
208 | (mkbinding (first ref) 'pointer 'pointer))) |
93aa67db |
209 | (when unref |
210 | (setf |
211 | (slot-value class 'unref) |
7479d92c |
212 | (mkbinding (first unref) 'nil 'pointer))))) |
93aa67db |
213 | |
214 | (defmethod shared-initialize :after ((class ginstance-class) names |
215 | &rest initargs) |
216 | (declare (ignore names initargs)) |
217 | (unless (slot-boundp class 'ref) |
218 | (setf |
219 | (slot-value class 'ref) |
220 | (ginstance-class-ref (most-specific-proxy-superclass class)))) |
221 | (unless (slot-boundp class 'unref) |
222 | (setf |
223 | (slot-value class 'unref) |
224 | (ginstance-class-unref (most-specific-proxy-superclass class))))) |
560af5c5 |
225 | |
226 | |
227 | (defmethod validate-superclass |
c8c48a4c |
228 | ((class ginstance-class) (super pcl::standard-class)) |
229 | (subtypep (class-name super) 'ginstance)) |
560af5c5 |
230 | |
231 | |
560af5c5 |
232 | ;;;; Initializing type numbers |
233 | |
234 | (setf (alien-type-name 'invalid) "invalid") |
235 | (setf (alien-type-name 'char) "gchar") |
236 | (setf (alien-type-name 'unsigned-char) "guchar") |
237 | (setf (alien-type-name 'boolean) "gboolean") |
238 | (setf (alien-type-name 'int) "gint") |
239 | (setf (alien-type-name 'unsigned-int) "guint") |
240 | (setf (alien-type-name 'long) "glong") |
241 | (setf (alien-type-name 'unsigned-long) "gulong") |
560af5c5 |
242 | (setf (alien-type-name 'single-float) "gfloat") |
243 | (setf (alien-type-name 'double-float) "gdouble") |
93aa67db |
244 | (setf (alien-type-name 'string) "GString") |
560af5c5 |
245 | (setf (find-type-number 'fixnum) (find-type-number 'int)) |