chiark / gitweb /
Exporting new symbols
[clg] / glib / glib.lisp
... / ...
CommitLineData
1;; Common Lisp bindings for GTK+ 2.x
2;; Copyright 1999-2005 Espen S. Johnsen <espen@users.sf.net>
3;;
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:
11;;
12;; The above copyright notice and this permission notice shall be
13;; included in all copies or substantial portions of the Software.
14;;
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
23;; $Id: glib.lisp,v 1.41 2007-09-07 07:33:51 espen Exp $
24
25
26(in-package "GLIB")
27
28(use-prefix "g")
29
30#-sb-thread
31(progn
32 (defun make-mutex ()
33 nil)
34
35 (defmacro with-mutex ((mutex) &body body)
36 (declare (ignore mutex))
37 `(progn ,@body)))
38
39
40;;;; Memory management
41
42(defbinding (%allocate-memory "g_malloc0") () pointer
43 (size unsigned-long))
44
45(defbinding (%deallocate-memory "g_free") () nil
46 (address pointer))
47
48;; (setf
49;; (symbol-function 'allocate-memory) #'%allocate-memory
50;; (symbol-function 'deallocate-memory) #'%deallocate-memory)
51
52(setf *memory-allocator* #'%allocate-memory)
53(setf *memory-deallocator* #'%deallocate-memory)
54
55(defbinding (reallocate-memory "g_realloc") () pointer
56 (address pointer)
57 (size unsigned-long))
58
59
60;;;; User data mechanism
61
62(defvar *user-data-lock* (make-mutex))
63(defvar *user-data* (make-hash-table))
64(defvar *user-data-count* 0)
65
66(defun register-user-data (object &optional destroy-function)
67 (check-type destroy-function (or null symbol function))
68 (with-mutex (*user-data-lock*)
69 (incf *user-data-count*)
70 (setf
71 (gethash *user-data-count* *user-data*)
72 (cons object destroy-function))
73 *user-data-count*))
74
75(defun find-user-data (id)
76 (check-type id fixnum)
77 (with-mutex (*user-data-lock*)
78 (multiple-value-bind (user-data p) (gethash id *user-data*)
79 (values (car user-data) p))))
80
81(defun user-data-exists-p (id)
82 (nth-value 1 (find-user-data id)))
83
84(defun update-user-data (id object)
85 (check-type id fixnum)
86 (with-mutex (*user-data-lock*)
87 (multiple-value-bind (user-data exists-p) (gethash id *user-data*)
88 (cond
89 ((not exists-p) (error "User data id ~A does not exist" id))
90 (t
91 (when (cdr user-data)
92 (funcall (cdr user-data) (car user-data)))
93 (setf (car user-data) object))))))
94
95(defun destroy-user-data (id)
96 (check-type id fixnum)
97 (with-mutex (*user-data-lock*)
98 (let ((user-data (gethash id *user-data*)))
99 (when (cdr user-data)
100 (funcall (cdr user-data) (car user-data))))
101 (remhash id *user-data*)))
102
103
104;;;; Quarks
105
106(deftype quark () 'unsigned)
107
108(defbinding %quark-from-string () quark
109 (string string))
110
111(defun quark-intern (object)
112 (etypecase object
113 (quark object)
114 (string (%quark-from-string object))
115 (symbol (%quark-from-string (format nil "clg-~A:~A"
116 (package-name (symbol-package object))
117 object)))))
118
119(defbinding quark-to-string () (static string)
120 (quark quark))
121
122
123;;;; Linked list (GList)
124
125(deftype glist (type)
126 `(or null (cons ,type list)))
127
128(defbinding (%glist-append "g_list_append") () pointer
129 (glist (or null pointer))
130 (nil null))
131
132(defun make-glist (element-type list &optional temp-p)
133 (let ((writer (if (functionp element-type)
134 element-type
135 (writer-function element-type :temp temp-p))))
136 (loop
137 for element in list
138 as glist = (%glist-append nil) then (%glist-append glist)
139 do (funcall writer element glist)
140 finally (return glist))))
141
142(defun glist-next (glist)
143 (unless (null-pointer-p glist)
144 (ref-pointer glist #.(size-of 'pointer))))
145
146;; Also used for gslists
147(defun map-glist (seqtype function glist element-type &optional (ref :read))
148 (let ((reader (if (functionp element-type)
149 element-type
150 (reader-function element-type :ref ref))))
151 (case seqtype
152 ((nil)
153 (loop
154 as element = glist then (glist-next element)
155 until (null-pointer-p element)
156 do (funcall function (funcall reader element))))
157 (list
158 (loop
159 as element = glist then (glist-next element)
160 until (null-pointer-p element)
161 collect (funcall function (funcall reader element))))
162 (t
163 (coerce
164 (loop
165 as element = glist then (glist-next element)
166 until (null-pointer-p element)
167 collect (funcall function (funcall reader element)))
168 seqtype)))))
169
170(defbinding (glist-free "g_list_free") () nil
171 (glist pointer))
172
173(defun destroy-glist (glist element-type &optional temp-p)
174 (let ((destroy (if (functionp element-type)
175 element-type
176 (destroy-function element-type :temp temp-p))))
177 (loop
178 as element = glist then (glist-next element)
179 until (null-pointer-p element)
180 do (funcall destroy element)))
181 (glist-free glist))
182
183(define-type-method alien-type ((type glist))
184 (declare (ignore type))
185 (alien-type 'pointer))
186
187(define-type-method argument-type ((type glist))
188 'list)
189
190(define-type-method return-type ((type glist))
191 'list)
192
193(define-type-method size-of ((type glist) &key inlined)
194 (assert-not-inlined type inlined)
195 (size-of 'pointer))
196
197(define-type-method type-alignment ((type glist) &key inlined)
198 (assert-not-inlined type inlined)
199 (type-alignment 'pointer))
200
201(define-type-method alien-arg-wrapper ((type glist) var list style form &optional copy-in-p)
202 (destructuring-bind (element-type) (rest (type-expand-to 'glist type))
203 (cond
204 ((and (in-arg-p style) (not (out-arg-p style)))
205 `(with-pointer (,var (make-glist ',element-type ,list ,(not copy-in-p)))
206 (unwind-protect
207 ,form
208 ,(unless copy-in-p
209 `(destroy-glist ,var ',element-type t)))))
210 ((and (in-arg-p style) (out-arg-p style))
211 (let ((glist (make-symbol "GLIST")))
212 `(with-pointer (,glist (make-glist ',element-type ,list ,(not copy-in-p)))
213 (with-pointer (,var ,glist)
214 (unwind-protect
215 ,form
216 ,(unless copy-in-p
217 `(destroy-glist ,glist ',element-type t)))))))
218 ((and (out-arg-p style) (not (in-arg-p style)))
219 `(with-pointer (,var)
220 ,form)))))
221
222(define-type-method to-alien-form ((type glist) list &optional copy-p)
223 (declare (ignore copy-p))
224 (destructuring-bind (element-type) (rest (type-expand-to 'glist type))
225 `(make-glist ',element-type ,list)))
226
227(define-type-method to-alien-function ((type glist) &optional copy-p)
228 (destructuring-bind (element-type) (rest (type-expand-to 'glist type))
229 (values
230 #'(lambda (list)
231 (make-glist element-type list (not copy-p)))
232 (unless copy-p
233 #'(lambda (list glist)
234 (declare (ignore list))
235 (destroy-glist glist element-type t))))))
236
237(define-type-method from-alien-form ((type glist) form &key (ref :free))
238 (destructuring-bind (element-type) (rest (type-expand-to 'glist type))
239 `(let ((glist ,form))
240 (unwind-protect
241 (map-glist 'list #'identity glist ',element-type
242 ,(ecase ref (:free :get) ((:static :temp) :peek) (:copy :read)))
243 ,(when (eq ref :free)
244 `(destroy-glist glist ',element-type))))))
245
246(define-type-method from-alien-function ((type glist) &key (ref :free))
247 (destructuring-bind (element-type) (rest (type-expand-to 'glist type))
248 (ecase ref
249 (:free
250 #'(lambda (glist)
251 (prog1
252 (map-glist 'list #'identity glist element-type :get)
253 (glist-free glist))))
254 (:copy
255 #'(lambda (glist)
256 (map-glist 'list #'identity glist element-type :read)))
257 ((:static :temp)
258 #'(lambda (glist)
259 (map-glist 'list #'identity glist element-type :peek))))))
260
261(define-type-method writer-function ((type glist) &key temp inlined)
262 (assert-not-inlined type inlined)
263 (let ((element-type (second (type-expand-to 'glist type))))
264 #'(lambda (list location &optional (offset 0))
265 (setf
266 (ref-pointer location offset)
267 (make-glist element-type list temp)))))
268
269(define-type-method reader-function ((type glist) &key (ref :read) inlined)
270 (assert-not-inlined type inlined)
271 (let ((element-type (second (type-expand-to 'glist type))))
272 (ecase ref
273 ((:read :peek)
274 #'(lambda (location &optional (offset 0))
275 (unless (null-pointer-p (ref-pointer location offset))
276 (map-glist 'list #'identity (ref-pointer location offset) element-type ref))))
277 (:get
278 #'(lambda (location &optional (offset 0))
279 (unless (null-pointer-p (ref-pointer location offset))
280 (prog1
281 (map-glist 'list #'identity (ref-pointer location offset) element-type :get)
282 (glist-free (ref-pointer location offset))
283 (setf (ref-pointer location offset) (make-pointer 0)))))))))
284
285(define-type-method destroy-function ((type glist) &key temp inlined)
286 (assert-not-inlined type inlined)
287 (let ((element-type (second (type-expand-to 'glist type))))
288 #'(lambda (location &optional (offset 0))
289 (unless (null-pointer-p (ref-pointer location offset))
290 (destroy-glist (ref-pointer location offset) element-type temp)
291 (setf (ref-pointer location offset) (make-pointer 0))))))
292
293(define-type-method copy-function ((type glist) &key inlined)
294 (assert-not-inlined type inlined)
295 (destructuring-bind (element-type) (rest (type-expand-to 'glist type))
296 (let ((copy-element (copy-function element-type)))
297 #'(lambda (from to &optional (offset 0))
298 (unless (null-pointer-p (ref-pointer from offset))
299 (loop
300 as from-list = (ref-pointer from offset)
301 then (glist-next from-list)
302 as to-list = (setf (ref-pointer to offset) (%glist-append nil))
303 then (%glist-append to-list)
304 do (funcall copy-element from-list to-list)
305 while (glist-next from-lisT)))))))
306
307
308;;;; Single linked list (GSList)
309
310(deftype gslist (type) `(or null (cons ,type list)))
311
312(defbinding (%gslist-prepend "g_slist_prepend") () pointer
313 (gslist pointer)
314 (nil null))
315
316(defbinding (%gslist-append "g_slist_append") () pointer
317 (glist (or null pointer))
318 (nil null))
319
320
321(defun make-gslist (element-type list &optional temp-p)
322 (let ((writer (if (functionp element-type)
323 element-type
324 (writer-function element-type :temp temp-p))))
325 (loop
326 for element in (reverse list)
327 as gslist = (%gslist-prepend (make-pointer 0)) then (%gslist-prepend gslist)
328 do (funcall writer element gslist)
329 finally (return gslist))))
330
331(defbinding (gslist-free "g_slist_free") () nil
332 (gslist pointer))
333
334(defun destroy-gslist (gslist element-type &optional temp-p)
335 (loop
336 with destroy = (destroy-function element-type :temp temp-p)
337 as element = gslist then (glist-next element)
338 until (null-pointer-p element)
339 do (funcall destroy element 0))
340 (gslist-free gslist))
341
342(define-type-method alien-type ((type gslist))
343 (declare (ignore type))
344 (alien-type 'pointer))
345
346(define-type-method argument-type ((type gslist))
347 'list)
348
349(define-type-method return-type ((type gslist))
350 'list)
351
352(define-type-method size-of ((type gslist) &key inlined)
353 (assert-not-inlined type inlined)
354 (size-of 'pointer))
355
356(define-type-method type-alignment ((type gslist) &key inlined)
357 (assert-not-inlined type inlined)
358 (type-alignment 'pointer))
359
360(define-type-method alien-arg-wrapper ((type gslist) var list style form &optional copy-in-p)
361 (destructuring-bind (element-type) (rest (type-expand-to 'gslist type))
362 (cond
363 ((and (in-arg-p style) (not (out-arg-p style)))
364 `(with-pointer (,var (make-gslist ',element-type ,list ,(not copy-in-p)))
365 (unwind-protect
366 ,form
367 ,(unless copy-in-p
368 `(destroy-gslist ,var ',element-type t)))))
369 ((and (in-arg-p style) (out-arg-p style))
370 (let ((gslist (make-symbol "GSLIST")))
371 `(with-pointer (,gslist (make-gslist ',element-type ,list ,(not copy-in-p)))
372 (with-pointer (,var ,gslist)
373 (unwind-protect
374 ,form
375 ,(unless copy-in-p
376 `(destroy-gslist ,gslist ',element-type t)))))))
377 ((and (out-arg-p style) (not (in-arg-p style)))
378 `(with-pointer (,var)
379 ,form)))))
380
381(define-type-method to-alien-form ((type gslist) list &optional copy-p)
382 (declare (ignore copy-p))
383 (destructuring-bind (element-type) (rest (type-expand-to 'gslist type))
384 `(make-gslist ',element-type ,list)))
385
386(define-type-method to-alien-function ((type gslist) &optional copy-p)
387 (destructuring-bind (element-type) (rest (type-expand-to 'gslist type))
388 (values
389 #'(lambda (list)
390 (make-gslist element-type list (not copy-p)))
391 (unless copy-p
392 #'(lambda (list gslist)
393 (declare (ignore list))
394 (destroy-gslist gslist element-type t))))))
395
396(define-type-method from-alien-form ((type gslist) form &key (ref :free))
397 (destructuring-bind (element-type) (rest (type-expand-to 'gslist type))
398 `(let ((gslist ,form))
399 (unwind-protect
400 (map-glist 'list #'identity gslist ',element-type
401 ,(ecase ref (:free :get) ((:static :temp) :peek) (:copy :read)))
402 ,(when (eq ref :free)
403 `(destroy-gslist gslist ',element-type))))))
404
405(define-type-method from-alien-function ((type gslist) &key (ref :free))
406 (destructuring-bind (element-type) (rest (type-expand-to 'gslist type))
407 (ecase ref
408 (:free
409 #'(lambda (glist)
410 (prog1
411 (map-glist 'list #'identity glist element-type :get)
412 (gslist-free glist))))
413 (:copy
414 #'(lambda (glist)
415 (map-glist 'list #'identity glist element-type :read)))
416 ((:static :temp)
417 #'(lambda (glist)
418 (map-glist 'list #'identity glist element-type :peek))))))
419
420(define-type-method writer-function ((type gslist) &key temp inlined)
421 (assert-not-inlined type inlined)
422 (destructuring-bind (element-type) (rest (type-expand-to 'gslist type))
423 (let ((element-writer (writer-function element-type :temp temp)))
424 #'(lambda (list location &optional (offset 0))
425 (setf
426 (ref-pointer location offset)
427 (make-gslist element-writer list))))))
428
429(define-type-method reader-function ((type gslist) &key (ref :read) inlined)
430 (assert-not-inlined type inlined)
431 (let ((element-type (second (type-expand-to 'gslist type))))
432 (ecase ref
433 ((:read :peek)
434 #'(lambda (location &optional (offset 0))
435 (unless (null-pointer-p (ref-pointer location offset))
436 (map-glist 'list #'identity (ref-pointer location offset) element-type ref))))
437 (:get
438 #'(lambda (location &optional (offset 0))
439 (unless (null-pointer-p (ref-pointer location offset))
440 (prog1
441 (map-glist 'list #'identity (ref-pointer location offset) element-type :get)
442 (gslist-free (ref-pointer location offset))
443 (setf (ref-pointer location offset) (make-pointer 0)))))))))
444
445(define-type-method destroy-function ((type gslist) &key temp inlined)
446 (assert-not-inlined type inlined)
447 (let ((element-type (second (type-expand-to 'gslist type))))
448 #'(lambda (location &optional (offset 0))
449 (unless (null-pointer-p (ref-pointer location offset))
450 (destroy-gslist (ref-pointer location offset) element-type temp)
451 (setf (ref-pointer location offset) (make-pointer 0))))))
452
453(define-type-method copy-function ((type gslist) &key inlined)
454 (assert-not-inlined type inlined)
455 (destructuring-bind (element-type) (rest (type-expand-to 'gslist type))
456 (let ((copy-element (copy-function element-type)))
457 #'(lambda (from to &optional (offset 0))
458 (unless (null-pointer-p (ref-pointer from offset))
459 (loop
460 as from-list = (ref-pointer from offset)
461 then (glist-next from-list)
462 as to-list = (setf (ref-pointer to offset) (%gslist-append nil))
463 then (%gslist-append to-list)
464 do (funcall copy-element from-list to-list)
465 while (glist-next from-list)))))))