1 ;; Common Lisp bindings for GTK+ 2.x
2 ;; Copyright 1999-2005 Espen S. Johnsen <espen@users.sf.net>
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:
12 ;; The above copyright notice and this permission notice shall be
13 ;; included in all copies or substantial portions of the Software.
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.
23 ;; $Id: glib.lisp,v 1.45 2008-12-14 21:37:25 espen Exp $
35 (defmacro with-mutex ((mutex) &body body)
36 (declare (ignore mutex))
40 ;;;; Memory management
42 (deftype gsize () 'unsigned-int)
43 (deftype gssize () 'int)
44 (deftype goffset () '(unsigned-int 64))
47 (defbinding malloc0 () pointer
50 (defbinding free () nil
53 (setf *memory-allocator* #'malloc0)
54 (setf *memory-deallocator* #'free)
56 (defbinding (reallocate-memory "g_realloc") () pointer
60 (defbinding (slice-alloc "g_slice_alloc0") () pointer
63 ;;;; User data is a mechanism to store references to lisp objects in
68 (defvar *user-data-lock* (make-mutex))
69 (defvar *user-data* (make-hash-table))
70 (defvar *user-data-next-id* 1)
72 (defun register-user-data (object &optional destroy-function)
73 (check-type destroy-function (or null symbol function))
74 (with-mutex (*user-data-lock*)
76 (gethash *user-data-next-id* *user-data*)
77 (cons object destroy-function))
78 (1- (incf *user-data-next-id*))))
80 (defun find-user-data (id)
81 (check-type id fixnum)
82 (with-mutex (*user-data-lock*)
83 (multiple-value-bind (user-data p) (gethash id *user-data*)
84 (values (car user-data) p))))
86 (defun user-data-exists-p (id)
87 (nth-value 1 (find-user-data id)))
89 (defun update-user-data (id object)
90 (check-type id fixnum)
91 (with-mutex (*user-data-lock*)
92 (multiple-value-bind (user-data exists-p) (gethash id *user-data*)
94 ((not exists-p) (error "User data id ~A does not exist" id))
97 (funcall (cdr user-data) (car user-data)))
98 (setf (car user-data) object))))))
100 (defun destroy-user-data (id)
101 (check-type id fixnum)
102 (with-mutex (*user-data-lock*)
103 (multiple-value-bind (user-data exists-p) (gethash id *user-data*)
105 ; ((not exists-p) (error "User data id ~A does not exist" id))
107 (when (cdr user-data)
108 (funcall (cdr user-data) (car user-data)))
109 (remhash id *user-data*))))))
111 (defun take-user-data (id)
112 (check-type id fixnum)
113 (multiple-value-bind (user-data exists-p) (gethash id *user-data*)
115 ((not exists-p) (error "User data id ~A does not exist" id))
117 (when (cdr user-data)
118 (funcall (cdr user-data) (car user-data)))
119 (remhash id *user-data*)
122 (defmacro with-user-data ((var object) &body body)
123 `(let ((,var (register-user-data ,object)))
126 (destroy-user-data ,var))))
129 (deftype user-data-id () 'pointer-data)
134 (deftype quark () 'unsigned)
136 (defbinding %quark-from-string () quark
139 (defun quark-intern (object)
142 (string (%quark-from-string object))
143 (symbol (%quark-from-string (format nil "clg-~A:~A"
144 (package-name (symbol-package object))
147 (defbinding quark-to-string () (static string)
151 ;;;; Linked list (GList)
153 (deftype glist (type)
154 `(or null (cons ,type list)))
156 (defbinding (%glist-prepend "g_list_prepend") () pointer
157 (glist (or null pointer))
160 (defbinding (%glist-reverse "g_list_reverse") () pointer
161 (glist (or null pointer)))
163 (defun make-glist (element-type list &optional temp-p)
164 (let ((writer (if (functionp element-type)
166 (writer-function element-type :temp temp-p))))
169 as glist = (%glist-prepend nil) then (%glist-prepend glist)
170 do (funcall writer element glist)
171 finally (return (%glist-reverse glist)))))
173 (defun glist-next (glist)
174 (unless (null-pointer-p glist)
175 (ref-pointer glist #.(size-of 'pointer))))
177 ;; Also used for gslists
178 (defun map-glist (seqtype function glist element-type &optional (ref :read))
179 (let ((reader (if (functionp element-type)
181 (reader-function element-type :ref ref))))
185 as element = glist then (glist-next element)
186 until (null-pointer-p element)
187 do (funcall function (funcall reader element))))
190 as element = glist then (glist-next element)
191 until (null-pointer-p element)
192 collect (funcall function (funcall reader element))))
196 as element = glist then (glist-next element)
197 until (null-pointer-p element)
198 collect (funcall function (funcall reader element)))
201 (defbinding (glist-free "g_list_free") () nil
204 (defun destroy-glist (glist element-type &optional temp-p)
205 (let ((destroy (if (functionp element-type)
207 (destroy-function element-type :temp temp-p))))
209 as element = glist then (glist-next element)
210 until (null-pointer-p element)
211 do (funcall destroy element)))
214 (define-type-method alien-type ((type glist))
215 (declare (ignore type))
216 (alien-type 'pointer))
218 (define-type-method argument-type ((type glist))
221 (define-type-method return-type ((type glist))
224 (define-type-method size-of ((type glist) &key inlined)
225 (assert-not-inlined type inlined)
228 (define-type-method type-alignment ((type glist) &key inlined)
229 (assert-not-inlined type inlined)
230 (type-alignment 'pointer))
232 (define-type-method alien-arg-wrapper ((type glist) var list style form &optional copy-in-p)
233 (destructuring-bind (element-type) (rest (type-expand-to 'glist type))
235 ((and (in-arg-p style) (not (out-arg-p style)))
236 `(with-pointer (,var (make-glist ',element-type ,list ,(not copy-in-p)))
240 `(destroy-glist ,var ',element-type t)))))
241 ((and (in-arg-p style) (out-arg-p style))
242 (let ((glist (make-symbol "GLIST")))
243 `(with-pointer (,glist (make-glist ',element-type ,list ,(not copy-in-p)))
244 (with-pointer (,var ,glist)
248 `(destroy-glist ,glist ',element-type t)))))))
249 ((and (out-arg-p style) (not (in-arg-p style)))
250 `(with-pointer (,var)
253 (define-type-method to-alien-form ((type glist) list &optional copy-p)
254 (declare (ignore copy-p))
255 (destructuring-bind (element-type) (rest (type-expand-to 'glist type))
256 `(make-glist ',element-type ,list)))
258 (define-type-method to-alien-function ((type glist) &optional copy-p)
259 (destructuring-bind (element-type) (rest (type-expand-to 'glist type))
262 (make-glist element-type list (not copy-p)))
264 #'(lambda (list glist)
265 (declare (ignore list))
266 (destroy-glist glist element-type t))))))
268 (define-type-method from-alien-form ((type glist) form &key (ref :free))
269 (destructuring-bind (element-type) (rest (type-expand-to 'glist type))
270 `(let ((glist ,form))
272 (map-glist 'list #'identity glist ',element-type
273 ,(ecase ref (:free :get) ((:static :temp) :peek) (:copy :read)))
274 ,(when (eq ref :free)
275 `(destroy-glist glist ',element-type))))))
277 (define-type-method from-alien-function ((type glist) &key (ref :free))
278 (destructuring-bind (element-type) (rest (type-expand-to 'glist type))
283 (map-glist 'list #'identity glist element-type :get)
284 (glist-free glist))))
287 (map-glist 'list #'identity glist element-type :read)))
290 (map-glist 'list #'identity glist element-type :peek))))))
292 (define-type-method writer-function ((type glist) &key temp inlined)
293 (assert-not-inlined type inlined)
294 (let ((element-type (second (type-expand-to 'glist type))))
295 #'(lambda (list location &optional (offset 0))
297 (ref-pointer location offset)
298 (make-glist element-type list temp)))))
300 (define-type-method reader-function ((type glist) &key (ref :read) inlined)
301 (assert-not-inlined type inlined)
302 (let ((element-type (second (type-expand-to 'glist type))))
305 #'(lambda (location &optional (offset 0))
306 (unless (null-pointer-p (ref-pointer location offset))
307 (map-glist 'list #'identity (ref-pointer location offset) element-type ref))))
309 #'(lambda (location &optional (offset 0))
310 (unless (null-pointer-p (ref-pointer location offset))
312 (map-glist 'list #'identity (ref-pointer location offset) element-type :get)
313 (glist-free (ref-pointer location offset))
314 (setf (ref-pointer location offset) (make-pointer 0)))))))))
316 (define-type-method destroy-function ((type glist) &key temp inlined)
317 (assert-not-inlined type inlined)
318 (let ((element-type (second (type-expand-to 'glist type))))
319 #'(lambda (location &optional (offset 0))
320 (unless (null-pointer-p (ref-pointer location offset))
321 (destroy-glist (ref-pointer location offset) element-type temp)
322 (setf (ref-pointer location offset) (make-pointer 0))))))
324 (define-type-method copy-function ((type glist) &key inlined)
325 (assert-not-inlined type inlined)
326 (destructuring-bind (element-type) (rest (type-expand-to 'glist type))
327 (let ((copy-element (copy-function element-type)))
328 #'(lambda (from to &optional (offset 0))
329 (unless (null-pointer-p (ref-pointer from offset))
331 as from-list = (ref-pointer from offset) then (glist-next from-list)
332 as to-list = (%glist-prepend nil) then (%glist-prepend to-list)
333 do (funcall copy-element from-list to-list)
334 while (glist-next from-list)
335 finally (setf (ref-pointer to offset) (%glist-reverse to-list))))))))
338 ;;;; Single linked list (GSList)
340 (deftype gslist (type) `(or null (cons ,type list)))
342 (defbinding (%gslist-prepend "g_slist_prepend") () pointer
346 (defbinding (%gslist-append "g_slist_append") () pointer
347 (glist (or null pointer))
351 (defun make-gslist (element-type list &optional temp-p)
352 (let ((writer (if (functionp element-type)
354 (writer-function element-type :temp temp-p))))
356 for element in (reverse list)
357 as gslist = (%gslist-prepend (make-pointer 0)) then (%gslist-prepend gslist)
358 do (funcall writer element gslist)
359 finally (return gslist))))
361 (defbinding (gslist-free "g_slist_free") () nil
364 (defun destroy-gslist (gslist element-type &optional temp-p)
366 with destroy = (destroy-function element-type :temp temp-p)
367 as element = gslist then (glist-next element)
368 until (null-pointer-p element)
369 do (funcall destroy element 0))
370 (gslist-free gslist))
372 (define-type-method alien-type ((type gslist))
373 (declare (ignore type))
374 (alien-type 'pointer))
376 (define-type-method argument-type ((type gslist))
379 (define-type-method return-type ((type gslist))
382 (define-type-method size-of ((type gslist) &key inlined)
383 (assert-not-inlined type inlined)
386 (define-type-method type-alignment ((type gslist) &key inlined)
387 (assert-not-inlined type inlined)
388 (type-alignment 'pointer))
390 (define-type-method alien-arg-wrapper ((type gslist) var list style form &optional copy-in-p)
391 (destructuring-bind (element-type) (rest (type-expand-to 'gslist type))
393 ((and (in-arg-p style) (not (out-arg-p style)))
394 `(with-pointer (,var (make-gslist ',element-type ,list ,(not copy-in-p)))
398 `(destroy-gslist ,var ',element-type t)))))
399 ((and (in-arg-p style) (out-arg-p style))
400 (let ((gslist (make-symbol "GSLIST")))
401 `(with-pointer (,gslist (make-gslist ',element-type ,list ,(not copy-in-p)))
402 (with-pointer (,var ,gslist)
406 `(destroy-gslist ,gslist ',element-type t)))))))
407 ((and (out-arg-p style) (not (in-arg-p style)))
408 `(with-pointer (,var)
411 (define-type-method to-alien-form ((type gslist) list &optional copy-p)
412 (declare (ignore copy-p))
413 (destructuring-bind (element-type) (rest (type-expand-to 'gslist type))
414 `(make-gslist ',element-type ,list)))
416 (define-type-method to-alien-function ((type gslist) &optional copy-p)
417 (destructuring-bind (element-type) (rest (type-expand-to 'gslist type))
420 (make-gslist element-type list (not copy-p)))
422 #'(lambda (list gslist)
423 (declare (ignore list))
424 (destroy-gslist gslist element-type t))))))
426 (define-type-method from-alien-form ((type gslist) form &key (ref :free))
427 (destructuring-bind (element-type) (rest (type-expand-to 'gslist type))
428 `(let ((gslist ,form))
430 (map-glist 'list #'identity gslist ',element-type
431 ,(ecase ref (:free :get) ((:static :temp) :peek) (:copy :read)))
432 ,(when (eq ref :free)
433 `(destroy-gslist gslist ',element-type))))))
435 (define-type-method from-alien-function ((type gslist) &key (ref :free))
436 (destructuring-bind (element-type) (rest (type-expand-to 'gslist type))
441 (map-glist 'list #'identity glist element-type :get)
442 (gslist-free glist))))
445 (map-glist 'list #'identity glist element-type :read)))
448 (map-glist 'list #'identity glist element-type :peek))))))
450 (define-type-method writer-function ((type gslist) &key temp inlined)
451 (assert-not-inlined type inlined)
452 (destructuring-bind (element-type) (rest (type-expand-to 'gslist type))
453 (let ((element-writer (writer-function element-type :temp temp)))
454 #'(lambda (list location &optional (offset 0))
456 (ref-pointer location offset)
457 (make-gslist element-writer list))))))
459 (define-type-method reader-function ((type gslist) &key (ref :read) inlined)
460 (assert-not-inlined type inlined)
461 (let ((element-type (second (type-expand-to 'gslist type))))
464 #'(lambda (location &optional (offset 0))
465 (unless (null-pointer-p (ref-pointer location offset))
466 (map-glist 'list #'identity (ref-pointer location offset) element-type ref))))
468 #'(lambda (location &optional (offset 0))
469 (unless (null-pointer-p (ref-pointer location offset))
471 (map-glist 'list #'identity (ref-pointer location offset) element-type :get)
472 (gslist-free (ref-pointer location offset))
473 (setf (ref-pointer location offset) (make-pointer 0)))))))))
475 (define-type-method destroy-function ((type gslist) &key temp inlined)
476 (assert-not-inlined type inlined)
477 (let ((element-type (second (type-expand-to 'gslist type))))
478 #'(lambda (location &optional (offset 0))
479 (unless (null-pointer-p (ref-pointer location offset))
480 (destroy-gslist (ref-pointer location offset) element-type temp)
481 (setf (ref-pointer location offset) (make-pointer 0))))))
483 (define-type-method copy-function ((type gslist) &key inlined)
484 (assert-not-inlined type inlined)
485 (destructuring-bind (element-type) (rest (type-expand-to 'gslist type))
486 (let ((copy-element (copy-function element-type)))
487 #'(lambda (from to &optional (offset 0))
488 (unless (null-pointer-p (ref-pointer from offset))
490 as from-list = (ref-pointer from offset)
491 then (glist-next from-list)
492 as to-list = (setf (ref-pointer to offset) (%gslist-append nil))
493 then (%gslist-append to-list)
494 do (funcall copy-element from-list to-list)
495 while (glist-next from-list)))))))