1 ;; Common Lisp bindings for GTK+ v2.0
2 ;; Copyright (C) 2000-2001 Espen S. Johnsen <esj@stud.cs.uit.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: genums.lisp,v 1.8 2005-02-10 20:27:54 espen Exp $
23 (defun %map-enum (args op)
24 (let ((current-value 0))
27 (destructuring-bind (symbol &optional (value current-value))
29 (setf current-value (1+ value))
31 (:enum-int (list symbol value))
32 (:flags-int (list symbol value))
33 (:int-enum (list value symbol))
34 (:int-flags (list value symbol))
38 (defun %query-enum-or-flags-values (query-function class type)
39 (multiple-value-bind (sap length)
40 (funcall query-function (type-class-ref type))
42 (size (proxy-instance-size (find-class class)))
43 (proxy (make-instance class :location sap)))
45 (with-slots (location nickname value) proxy
47 (setq sap (sap+ sap size))
50 (intern (substitute #\- #\_ (string-upcase nickname)) "KEYWORD")
56 ;;;; Generic enum type
58 (deftype enum (&rest args)
59 `(member ,@(%map-enum args :symbols)))
61 (defmethod alien-type ((type (eql 'enum)) &rest args)
62 (declare (ignore type args))
65 (defmethod size-of ((type (eql 'enum)) &rest args)
66 (declare (ignore type args))
69 (defmethod to-alien-form (form (type (eql 'enum)) &rest args)
70 (declare (ignore type))
72 ,@(%map-enum args :enum-int)))
74 (defmethod from-alien-form (form (type (eql 'enum)) &rest args)
75 (declare (ignore type))
77 ,@(%map-enum args :int-enum)))
79 (defmethod to-alien-function ((type (eql 'enum)) &rest args)
80 (let ((mappings (%map-enum args :enum-int)))
83 (second (assoc enum mappings))
84 (error "~S is not of type ~S" enum (cons type args))))))
86 (defmethod from-alien-function ((type (eql 'enum)) &rest args)
87 (declare (ignore type))
88 (let ((mappings (%map-enum args :int-enum)))
90 (second (assoc int mappings)))))
92 (defmethod writer-function ((type (eql 'enum)) &rest args)
93 (declare (ignore type))
94 (let ((writer (writer-function 'signed))
95 (function (apply #'to-alien-function 'enum args)))
96 #'(lambda (enum location &optional (offset 0))
97 (funcall writer (funcall function enum) location offset))))
99 (defmethod reader-function ((type (eql 'enum)) &rest args)
100 (declare (ignore type))
101 (let ((reader (reader-function 'signed))
102 (function (apply #'from-alien-function 'enum args)))
103 #'(lambda (location &optional (offset 0))
104 (funcall function (funcall reader location offset)))))
108 (defclass %enum-value (struct)
109 ((value :allocation :alien :type int)
110 (name :allocation :alien :type string)
111 (nickname :allocation :alien :type string))
112 (:metaclass static-struct-class))
114 (defbinding %enum-class-values () pointer
116 (n-values unsigned-int :out))
118 (defun query-enum-values (type)
119 (%query-enum-or-flags-values #'%enum-class-values '%enum-value type))
121 (defun enum-int (enum type)
122 (funcall (to-alien-function type) enum))
124 (defun int-enum (int type)
125 (funcall (from-alien-function type) int))
127 (defun enum-mapping (type)
128 (rest (type-expand-to 'enum type)))
131 ;;;; Named enum types
133 (defmacro define-enum-type (name &rest args)
134 (let ((enum-int (intern (format nil "~A-TO-INT" name)))
135 (int-enum (intern (format nil "INT-TO-~A" name))))
137 (deftype ,name () '(enum ,@args))
138 (defun ,enum-int (value)
140 ,@(%map-enum args :enum-int)))
141 (defun ,int-enum (value)
143 ,@(%map-enum args :int-enum)))
144 (defmethod to-alien-form (form (type (eql ',name)) &rest args)
145 (declare (ignore type args))
146 (list ',enum-int form))
147 (defmethod from-alien-form (form (type (eql ',name)) &rest args)
148 (declare (ignore type args))
149 (list ',int-enum form))
150 (defmethod to-alien-function ((type (eql ',name)) &rest args)
151 (declare (ignore type args))
153 (defmethod from-alien-function ((type (eql ',name)) &rest args)
154 (declare (ignore type args))
156 (defmethod writer-function ((type (eql ',name)) &rest args)
157 (declare (ignore type args))
158 (let ((writer (writer-function 'signed)))
159 #'(lambda (enum location &optional (offset 0))
160 (funcall writer (,enum-int enum) location offset))))
161 (defmethod reader-function ((type (eql ',name)) &rest args)
162 (declare (ignore type args))
163 (let ((reader (reader-function 'signed)))
164 #'(lambda (location &optional (offset 0))
165 (,int-enum (funcall reader location offset))))))))
168 ;;;; Generic flags type
170 (deftype flags (&rest args)
171 `(or null (cons (member ,@(%map-enum args :symbols)) list)))
173 (defmethod alien-type ((type (eql 'flags)) &rest args)
174 (declare (ignore type args))
175 (alien-type 'unsigned))
177 (defmethod size-of ((type (eql 'flags)) &rest args)
178 (declare (ignore type args))
181 (defmethod to-alien-form (flags (type (eql 'flags)) &rest args)
185 for flag in (mklist flags)
188 (second (assoc flag ',(%map-enum args :flags-int)))
189 (error "~S is not of type ~S" flags '(,type ,@args)))))
190 (setq value (logior value flagval)))
191 finally (return value)))
193 (defmethod from-alien-form (int (type (eql 'flags)) &rest args)
194 (declare (ignore type))
196 for mapping in ',(%map-enum args :int-flags)
197 unless (zerop (logand ,int (first mapping)))
198 collect (second mapping)))
200 (defmethod to-alien-function ((type (eql 'flags)) &rest args)
201 (let ((mappings (%map-enum args :flags-int)))
205 for flag in (mklist flags)
206 do (let ((flagval (or
207 (second (assoc flag mappings))
208 (error "~S is not of type ~S" flags (cons type args)))))
209 (setq value (logior value flagval)))
210 finally (return value)))))
212 (defmethod from-alien-function ((type (eql 'flags)) &rest args)
213 (declare (ignore type))
214 (let ((mappings (%map-enum args :int-flags)))
217 for mapping in mappings
218 unless (zerop (logand int (first mapping)))
219 collect (second mapping)))))
221 (defmethod writer-function ((type (eql 'flags)) &rest args)
222 (declare (ignore type))
223 (let ((writer (writer-function 'unsigned))
224 (function (apply #'to-alien-function 'flags args)))
225 #'(lambda (flags location &optional (offset 0))
226 (funcall writer (funcall function flags) location offset))))
228 (defmethod reader-function ((type (eql 'flags)) &rest args)
229 (declare (ignore type))
230 (let ((reader (reader-function 'unsigned))
231 (function (apply #'from-alien-function 'flags args)))
232 #'(lambda (location &optional (offset 0))
233 (funcall function (funcall reader location offset)))))
237 (defclass %flags-value (struct)
238 ((value :allocation :alien :type unsigned-int)
239 (name :allocation :alien :type string)
240 (nickname :allocation :alien :type string))
241 (:metaclass static-struct-class))
243 (defbinding %flags-class-values () pointer
245 (n-values unsigned-int :out))
247 (defun query-flags-values (type)
248 (%query-enum-or-flags-values #'%flags-class-values '%flags-value type))
251 ;;;; Named flags types
253 (defmacro define-flags-type (name &rest args)
254 (let ((flags-int (intern (format nil "~A-TO-INT" name)))
255 (int-flags (intern (format nil "INT-TO-~A" name))))
257 (deftype ,name () '(flags ,@args))
258 (defun ,flags-int (value)
260 ,@(%map-enum args :flags-int)))
261 (defun ,int-flags (value)
263 ,@(%map-enum args :int-flags)))
264 (defmethod to-alien-form (form (type (eql ',name)) &rest args)
265 (declare (ignore type args))
266 (list ',flags-int form))
267 (defmethod from-alien-form (form (type (eql ',name)) &rest args)
268 (declare (ignore type args))
269 (list ',int-flags form))
270 (defmethod to-alien-function ((type (eql ',name)) &rest args)
271 (declare (ignore type args))
273 (defmethod from-alien-function ((type (eql ',name)) &rest args)
274 (declare (ignore type args))
276 (defmethod writer-function ((type (eql ',name)) &rest args)
277 (declare (ignore type args))
278 (let ((writer (writer-function 'signed)))
279 #'(lambda (flags location &optional (offset 0))
280 (funcall writer (,flags-int flags) location offset))))
281 (defmethod reader-function ((type (eql ',name)) &rest args)
282 (declare (ignore type args))
283 (let ((reader (reader-function 'signed)))
284 #'(lambda (location &optional (offset 0))
285 (,int-flags (funcall reader location offset))))))))
289 ;;;; Type definition by introspection
291 (defun expand-enum-type (type-number forward-p options)
292 (declare (ignore forward-p))
293 (let* ((super (supertype type-number))
294 (type (type-from-number type-number))
295 (mappings (getf options :mappings))
301 (assoc (first mapping) mappings)
302 (rassoc (cdr mapping) mappings :test #'equal)))
304 (query-enum-values type-number)
305 (query-flags-values type-number)))
307 #'(lambda (mapping) (eq (second mapping) nil)) mappings))))
309 (register-type ',type ,(find-type-name type-number))
311 (enum `(define-enum-type ,type ,@expanded-mappings))
312 (flags `(define-flags-type ,type ,@expanded-mappings))))))
315 (register-derivable-type 'enum "GEnum" 'expand-enum-type)
316 (register-derivable-type 'flags "GFlags" 'expand-enum-type)