chiark / gitweb /
Added GET-ALL and PLIST-REMOVE to manipulate plists
[clg] / glib / gtype.lisp
1 ;; Common Lisp bindings for GTK+ v2.0
2 ;; Copyright (C) 2000-2001 Espen S. Johnsen <esj@stud.cs.uit.no>
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
18 ;; $Id: gtype.lisp,v 1.11 2001-05-29 15:49:23 espen Exp $
19
20 (in-package "GLIB")
21
22 (use-prefix "g")
23
24 ;;;; 
25
26 (deftype type-number () '(unsigned 32))
27
28 (eval-when (:compile-toplevel :load-toplevel :execute)
29   (defclass type-query (struct)
30     ((type-number :allocation :alien :type type-number)
31      (name :allocation :alien :type string)
32      (class-size :allocation :alien :type unsigned-int)
33      (instance-size :allocation :alien :type unsigned-int))
34     (:metaclass proxy-class)))
35
36
37 (defbinding %type-query () nil
38   (type type-number)
39   (query type-query))
40
41 (defun type-query (type)
42   (let ((query (make-instance 'type-query)))
43     (%type-query (find-type-number type t) query)
44     query))
45
46 (defun type-instance-size (type)
47   (slot-value (type-query type) 'instance-size))
48
49 (defun type-class-size (type)
50   (slot-value (type-query type) 'class-size))
51
52 (defbinding type-class-ref (type) pointer
53   ((find-type-number type t) type-number))
54
55 (defbinding type-class-unref (type) nil
56   ((find-type-number type t) type-number))
57
58 (defbinding type-class-peek (type) pointer
59   ((find-type-number type t) type-number))
60
61
62 ;;;; Mapping between lisp types and glib types
63
64 (defvar *type-to-number-hash* (make-hash-table))
65 (defvar *number-to-type-hash* (make-hash-table))
66
67 (defun register-type (type id)
68   (let ((type-number
69          (etypecase id
70            (integer id)
71            (string (find-type-number id t)))))
72     (setf (gethash type *type-to-number-hash*) type-number)
73     (setf (gethash type-number *number-to-type-hash*) type)
74     type-number))
75
76 (defbinding %type-from-name () type-number
77   (name string))
78
79 (defun find-type-number (type &optional error)
80   (etypecase type
81     (integer type)
82     (string
83      (let ((type-number (%type-from-name type)))
84        (cond
85         ((and (zerop type-number) error)
86          (error "Invalid alien type name: ~A" type))
87         ((zerop type-number) nil)
88         (t type-number))))
89     (symbol
90      (let ((type-number (gethash type *type-to-number-hash*)))
91        (or
92         type-number
93         (and error (error "Type not registered: ~A" type)))))
94     (pcl::class (find-type-number (class-name type) error))))
95  
96 (defun type-from-number (type-number &optional error)
97   (multiple-value-bind (type found)
98       (gethash type-number *number-to-type-hash*)
99     (when (and error (not found))
100       (let ((name (find-type-name type-number)))
101         (if name
102             (error "Type number not registered: ~A (~A)" type-number name)
103           (error "Invalid type number: ~A" type-number))))
104     type))
105
106 (defun type-from-name (name)
107   (etypecase name
108     (string (type-from-number (find-type-number name t)))))
109
110 (defbinding (find-type-name "g_type_name") (type) string
111   ((find-type-number type t) type-number))
112
113 (defun type-number-of (object)
114   (find-type-number (type-of object) t))
115
116 (defun init-type (init)
117   (mapc
118    #'(lambda (fname)
119        (funcall (mkbinding fname 'type-number)))
120    (mklist init)))
121
122 (defun %init-types-in-library (pathname ignore)
123   (let ((process (ext:run-program
124                   "nm" (list (namestring (truename pathname)))
125                   :output :stream :wait nil))
126         (fnames ()))
127     (labels ((read-symbols ()
128                (let ((line (read-line (ext:process-output process) nil)))
129                  (when line
130                    (let ((symbol (subseq line 11)))
131                      (when (and
132                             (search "_get_type" symbol)
133                             (not (member symbol ignore :test #'string=)))
134                        (push symbol fnames)))
135                    (read-symbols)))))
136       (read-symbols)
137       (ext:process-close process)
138       `(init-type ',fnames))))
139
140 (defmacro init-types-in-library (pathname &key ignore)
141   (%init-types-in-library pathname ignore))
142
143
144
145 ;;;; Superclass for wrapping types in the glib type system
146
147 (eval-when (:compile-toplevel :load-toplevel :execute)
148   (defclass ginstance (proxy)
149     ((class :allocation :alien :type pointer))
150     (:metaclass proxy-class)))
151
152 (defun %type-of-ginstance (location)
153   (let ((class (sap-ref-sap location 0)))
154     (type-from-number (sap-ref-unsigned class 0))))
155
156 (deftype-method translate-from-alien
157     ginstance (type-spec location &optional weak-ref)
158   (declare (ignore type-spec))
159   `(let ((location ,location))
160      (unless (null-pointer-p location)
161        (ensure-proxy-instance
162         (%type-of-ginstance location) location ,weak-ref))))
163
164
165
166 ;;;; Metaclass for subclasses of ginstance
167
168 (eval-when (:compile-toplevel :load-toplevel :execute)
169   (defclass ginstance-class (proxy-class)))
170
171
172 (defmethod shared-initialize ((class ginstance-class) names
173                               &rest initargs &key name alien-name
174                               size ref unref)
175   (declare (ignore initargs names))
176   (let* ((class-name (or name (class-name class)))
177          (type-number
178           (find-type-number
179            (or (first alien-name) (default-alien-type-name class-name)) t)))
180     (register-type class-name type-number)
181     (let ((size (or size (type-instance-size type-number))))
182       (declare (special size))
183       (call-next-method)))
184
185   (when ref
186     (let ((ref (mkbinding (first ref) 'pointer 'pointer)))
187       (setf
188        (slot-value class 'copy)
189        #'(lambda (type location)
190            (declare (ignore type))
191            (funcall ref location)))))     
192   (when unref
193     (let ((unref (mkbinding (first unref) 'nil 'pointer)))
194       (setf
195        (slot-value class 'free)
196        #'(lambda (type location)
197            (declare (ignore type))
198            (funcall unref location))))))
199
200
201 (defmethod validate-superclass
202     ((class ginstance-class) (super pcl::standard-class))
203   (subtypep (class-name super) 'ginstance))
204
205
206 ;;;; Registering fundamental types
207
208 (register-type 'pointer "gpointer")
209 (register-type 'char "gchar")
210 (register-type 'unsigned-char "guchar")
211 (register-type 'boolean "gboolean")
212 (register-type 'fixnum "gint")
213 (register-type 'int "gint")
214 (register-type 'unsigned-int "guint")
215 (register-type 'long "glong")
216 (register-type 'unsigned-long "gulong")
217 (register-type 'single-float "gfloat")
218 (register-type 'double-float "gdouble")
219 (register-type 'string "gchararray")
220
221
222 ;;;; 
223
224 (defvar *derivable-type-info* ())
225
226 (defun register-derivable-type (type id &key query expand)
227   (register-type type id)
228   (let* ((type-number (register-type type id))
229          (info (assoc type-number *derivable-type-info*)))
230     (if info
231         (setf (cdr info) (list query expand))
232       (push
233        (list type-number query expand)
234        *derivable-type-info*))))
235
236 (defun find-type-info (type)
237   (dolist (super (cdr (type-hierarchy type)))
238     (let ((info (assoc super *derivable-type-info*)))
239       (return-if info))))
240
241 (defun type-dependencies (type)
242   (let ((query (second (find-type-info type))))
243     (when query
244       (funcall query (find-type-number type t)))))
245
246 (defun expand-type-definition (type)
247   (let ((expander (third (find-type-info type))))
248     (funcall expander (find-type-number type t))))
249
250 (defbinding type-parent (type) type-number
251   ((find-type-number type t) type-number))
252
253 (defun supertype (type)
254   (type-from-number (type-parent type)))
255
256 (defun type-hierarchy (type)
257   (let ((type-number (find-type-number type t)))
258     (unless (= type-number 0)
259       (cons type-number (type-hierarchy (type-parent type-number))))))
260   
261 (defbinding (type-is-p "g_type_is_a") (type super) boolean
262   ((find-type-number type) type-number)
263   ((find-type-number super) type-number))
264
265 (defbinding %type-children () pointer
266   (type-number type-number)
267   (num-children unsigned-int :out))
268
269 (defun map-subtypes (function type &optional prefix)
270   (let ((type-number (find-type-number type t)))
271     (multiple-value-bind (array length) (%type-children type-number)
272       (unwind-protect
273           (map-c-array
274            'nil
275            #'(lambda (type-number)
276                (when (or
277                       (not prefix)
278                       (string-prefix-p prefix (find-type-name type-number)))
279                  (funcall function type-number))
280                (map-subtypes function type-number prefix))
281            array 'type-number length)
282         (deallocate-memory array)))))
283
284 (defun find-types (prefix)
285   (let ((type-list nil))
286     (dolist (type-info *derivable-type-info*)
287       (map-subtypes
288        #'(lambda (type-number)
289            (pushnew type-number type-list))
290        (first type-info) prefix))
291     type-list))
292
293 (defun %sort-types-topologicaly (unsorted)
294   (let ((sorted ()))
295     (loop while unsorted do
296       (dolist (type unsorted)
297         (let ((dependencies (type-dependencies type)))
298           (cond
299            ((null dependencies)
300             (push type sorted)
301             (setq unsorted (delete type unsorted)))
302            (t
303             (unless (dolist (dep dependencies)
304                       (when (find type (type-dependencies dep))
305                         (error "Cyclic type dependencies not yet supported"))
306                       (return-if (find dep unsorted)))
307               (push type sorted)
308               (setq unsorted (delete type unsorted))))))))
309     (nreverse sorted)))
310
311
312 (defun expand-type-definitions (prefix &optional args)
313   (flet ((type-options (type-number)
314            (let ((name (find-type-name type-number)))
315              (cdr (assoc name args :test #'string=)))))
316
317     (let ((type-list
318            (delete-if
319             #'(lambda (type-number)
320                 (let ((name (find-type-name type-number)))
321                   (or
322                    (getf (type-options type-number) :ignore)
323                    (find-if
324                     #'(lambda (options)
325                         (and
326                          (string-prefix-p (first options) name)
327                          (getf (cdr options) :ignore-prefix)))
328                     args))))
329             (find-types prefix))))
330              
331       (dolist (type-number type-list)
332         (let ((name (find-type-name type-number)))
333           (register-type
334            (getf (type-options type-number) :type (default-type-name name))
335            type-number)))
336
337       `(progn
338          ,@(mapcar
339             #'expand-type-definition
340             (%sort-types-topologicaly type-list))))))
341             
342 (defmacro define-types-by-introspection (prefix &rest args)
343   (expand-type-definitions prefix args))