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.42 2007-10-17 14:30:41 espen Exp $
35 (defmacro with-mutex ((mutex) &body body)
36 (declare (ignore mutex))
40 ;;;; Memory management
42 (defbinding (%allocate-memory "g_malloc0") () pointer
45 (defbinding (%deallocate-memory "g_free") () nil
49 ;; (symbol-function 'allocate-memory) #'%allocate-memory
50 ;; (symbol-function 'deallocate-memory) #'%deallocate-memory)
52 (setf *memory-allocator* #'%allocate-memory)
53 (setf *memory-deallocator* #'%deallocate-memory)
55 (defbinding (reallocate-memory "g_realloc") () pointer
59 (deftype gsize () 'unsigned-int)
61 (defbinding (slice-alloc "g_slice_alloc0") () pointer
64 ;;;; User data is a mechanism to store references to lisp objects in
67 (defvar *user-data-lock* (make-mutex))
68 (defvar *user-data* (make-hash-table))
69 (defvar *user-data-next-id* 1)
71 (defun register-user-data (object &optional destroy-function)
72 (check-type destroy-function (or null symbol function))
73 (with-mutex (*user-data-lock*)
75 (gethash *user-data-next-id* *user-data*)
76 (cons object destroy-function))
77 (1- (incf *user-data-next-id*))))
79 (defun find-user-data (id)
80 (check-type id fixnum)
81 (with-mutex (*user-data-lock*)
82 (multiple-value-bind (user-data p) (gethash id *user-data*)
83 (values (car user-data) p))))
85 (defun user-data-exists-p (id)
86 (nth-value 1 (find-user-data id)))
88 (defun update-user-data (id object)
89 (check-type id fixnum)
90 (with-mutex (*user-data-lock*)
91 (multiple-value-bind (user-data exists-p) (gethash id *user-data*)
93 ((not exists-p) (error "User data id ~A does not exist" id))
96 (funcall (cdr user-data) (car user-data)))
97 (setf (car user-data) object))))))
99 (defun destroy-user-data (id)
100 (check-type id fixnum)
101 (with-mutex (*user-data-lock*)
102 (multiple-value-bind (user-data exists-p) (gethash id *user-data*)
104 ; ((not exists-p) (error "User data id ~A does not exist" id))
106 (when (cdr user-data)
107 (funcall (cdr user-data) (car user-data)))
108 (remhash id *user-data*))))))
110 (defun take-user-data (id)
111 (check-type id fixnum)
112 (multiple-value-bind (user-data exists-p) (gethash id *user-data*)
114 ((not exists-p) (error "User data id ~A does not exist" id))
116 (when (cdr user-data)
117 (funcall (cdr user-data) (car user-data)))
118 (remhash id *user-data*)
121 (defmacro with-user-data ((var object) &body body)
122 `(let ((,var (register-user-data ,object)))
125 (destroy-user-data ,var))))
128 (deftype user-data-id () 'pointer-data)
133 (deftype quark () 'unsigned)
135 (defbinding %quark-from-string () quark
138 (defun quark-intern (object)
141 (string (%quark-from-string object))
142 (symbol (%quark-from-string (format nil "clg-~A:~A"
143 (package-name (symbol-package object))
146 (defbinding quark-to-string () (static string)
150 ;;;; Linked list (GList)
152 (deftype glist (type)
153 `(or null (cons ,type list)))
155 (defbinding (%glist-append "g_list_append") () pointer
156 (glist (or null pointer))
159 (defun make-glist (element-type list &optional temp-p)
160 (let ((writer (if (functionp element-type)
162 (writer-function element-type :temp temp-p))))
165 as glist = (%glist-append nil) then (%glist-append glist)
166 do (funcall writer element glist)
167 finally (return glist))))
169 (defun glist-next (glist)
170 (unless (null-pointer-p glist)
171 (ref-pointer glist #.(size-of 'pointer))))
173 ;; Also used for gslists
174 (defun map-glist (seqtype function glist element-type &optional (ref :read))
175 (let ((reader (if (functionp element-type)
177 (reader-function element-type :ref ref))))
181 as element = glist then (glist-next element)
182 until (null-pointer-p element)
183 do (funcall function (funcall reader element))))
186 as element = glist then (glist-next element)
187 until (null-pointer-p element)
188 collect (funcall function (funcall reader element))))
192 as element = glist then (glist-next element)
193 until (null-pointer-p element)
194 collect (funcall function (funcall reader element)))
197 (defbinding (glist-free "g_list_free") () nil
200 (defun destroy-glist (glist element-type &optional temp-p)
201 (let ((destroy (if (functionp element-type)
203 (destroy-function element-type :temp temp-p))))
205 as element = glist then (glist-next element)
206 until (null-pointer-p element)
207 do (funcall destroy element)))
210 (define-type-method alien-type ((type glist))
211 (declare (ignore type))
212 (alien-type 'pointer))
214 (define-type-method argument-type ((type glist))
217 (define-type-method return-type ((type glist))
220 (define-type-method size-of ((type glist) &key inlined)
221 (assert-not-inlined type inlined)
224 (define-type-method type-alignment ((type glist) &key inlined)
225 (assert-not-inlined type inlined)
226 (type-alignment 'pointer))
228 (define-type-method alien-arg-wrapper ((type glist) var list style form &optional copy-in-p)
229 (destructuring-bind (element-type) (rest (type-expand-to 'glist type))
231 ((and (in-arg-p style) (not (out-arg-p style)))
232 `(with-pointer (,var (make-glist ',element-type ,list ,(not copy-in-p)))
236 `(destroy-glist ,var ',element-type t)))))
237 ((and (in-arg-p style) (out-arg-p style))
238 (let ((glist (make-symbol "GLIST")))
239 `(with-pointer (,glist (make-glist ',element-type ,list ,(not copy-in-p)))
240 (with-pointer (,var ,glist)
244 `(destroy-glist ,glist ',element-type t)))))))
245 ((and (out-arg-p style) (not (in-arg-p style)))
246 `(with-pointer (,var)
249 (define-type-method to-alien-form ((type glist) list &optional copy-p)
250 (declare (ignore copy-p))
251 (destructuring-bind (element-type) (rest (type-expand-to 'glist type))
252 `(make-glist ',element-type ,list)))
254 (define-type-method to-alien-function ((type glist) &optional copy-p)
255 (destructuring-bind (element-type) (rest (type-expand-to 'glist type))
258 (make-glist element-type list (not copy-p)))
260 #'(lambda (list glist)
261 (declare (ignore list))
262 (destroy-glist glist element-type t))))))
264 (define-type-method from-alien-form ((type glist) form &key (ref :free))
265 (destructuring-bind (element-type) (rest (type-expand-to 'glist type))
266 `(let ((glist ,form))
268 (map-glist 'list #'identity glist ',element-type
269 ,(ecase ref (:free :get) ((:static :temp) :peek) (:copy :read)))
270 ,(when (eq ref :free)
271 `(destroy-glist glist ',element-type))))))
273 (define-type-method from-alien-function ((type glist) &key (ref :free))
274 (destructuring-bind (element-type) (rest (type-expand-to 'glist type))
279 (map-glist 'list #'identity glist element-type :get)
280 (glist-free glist))))
283 (map-glist 'list #'identity glist element-type :read)))
286 (map-glist 'list #'identity glist element-type :peek))))))
288 (define-type-method writer-function ((type glist) &key temp inlined)
289 (assert-not-inlined type inlined)
290 (let ((element-type (second (type-expand-to 'glist type))))
291 #'(lambda (list location &optional (offset 0))
293 (ref-pointer location offset)
294 (make-glist element-type list temp)))))
296 (define-type-method reader-function ((type glist) &key (ref :read) inlined)
297 (assert-not-inlined type inlined)
298 (let ((element-type (second (type-expand-to 'glist type))))
301 #'(lambda (location &optional (offset 0))
302 (unless (null-pointer-p (ref-pointer location offset))
303 (map-glist 'list #'identity (ref-pointer location offset) element-type ref))))
305 #'(lambda (location &optional (offset 0))
306 (unless (null-pointer-p (ref-pointer location offset))
308 (map-glist 'list #'identity (ref-pointer location offset) element-type :get)
309 (glist-free (ref-pointer location offset))
310 (setf (ref-pointer location offset) (make-pointer 0)))))))))
312 (define-type-method destroy-function ((type glist) &key temp inlined)
313 (assert-not-inlined type inlined)
314 (let ((element-type (second (type-expand-to 'glist type))))
315 #'(lambda (location &optional (offset 0))
316 (unless (null-pointer-p (ref-pointer location offset))
317 (destroy-glist (ref-pointer location offset) element-type temp)
318 (setf (ref-pointer location offset) (make-pointer 0))))))
320 (define-type-method copy-function ((type glist) &key inlined)
321 (assert-not-inlined type inlined)
322 (destructuring-bind (element-type) (rest (type-expand-to 'glist type))
323 (let ((copy-element (copy-function element-type)))
324 #'(lambda (from to &optional (offset 0))
325 (unless (null-pointer-p (ref-pointer from offset))
327 as from-list = (ref-pointer from offset)
328 then (glist-next from-list)
329 as to-list = (setf (ref-pointer to offset) (%glist-append nil))
330 then (%glist-append to-list)
331 do (funcall copy-element from-list to-list)
332 while (glist-next from-lisT)))))))
335 ;;;; Single linked list (GSList)
337 (deftype gslist (type) `(or null (cons ,type list)))
339 (defbinding (%gslist-prepend "g_slist_prepend") () pointer
343 (defbinding (%gslist-append "g_slist_append") () pointer
344 (glist (or null pointer))
348 (defun make-gslist (element-type list &optional temp-p)
349 (let ((writer (if (functionp element-type)
351 (writer-function element-type :temp temp-p))))
353 for element in (reverse list)
354 as gslist = (%gslist-prepend (make-pointer 0)) then (%gslist-prepend gslist)
355 do (funcall writer element gslist)
356 finally (return gslist))))
358 (defbinding (gslist-free "g_slist_free") () nil
361 (defun destroy-gslist (gslist element-type &optional temp-p)
363 with destroy = (destroy-function element-type :temp temp-p)
364 as element = gslist then (glist-next element)
365 until (null-pointer-p element)
366 do (funcall destroy element 0))
367 (gslist-free gslist))
369 (define-type-method alien-type ((type gslist))
370 (declare (ignore type))
371 (alien-type 'pointer))
373 (define-type-method argument-type ((type gslist))
376 (define-type-method return-type ((type gslist))
379 (define-type-method size-of ((type gslist) &key inlined)
380 (assert-not-inlined type inlined)
383 (define-type-method type-alignment ((type gslist) &key inlined)
384 (assert-not-inlined type inlined)
385 (type-alignment 'pointer))
387 (define-type-method alien-arg-wrapper ((type gslist) var list style form &optional copy-in-p)
388 (destructuring-bind (element-type) (rest (type-expand-to 'gslist type))
390 ((and (in-arg-p style) (not (out-arg-p style)))
391 `(with-pointer (,var (make-gslist ',element-type ,list ,(not copy-in-p)))
395 `(destroy-gslist ,var ',element-type t)))))
396 ((and (in-arg-p style) (out-arg-p style))
397 (let ((gslist (make-symbol "GSLIST")))
398 `(with-pointer (,gslist (make-gslist ',element-type ,list ,(not copy-in-p)))
399 (with-pointer (,var ,gslist)
403 `(destroy-gslist ,gslist ',element-type t)))))))
404 ((and (out-arg-p style) (not (in-arg-p style)))
405 `(with-pointer (,var)
408 (define-type-method to-alien-form ((type gslist) list &optional copy-p)
409 (declare (ignore copy-p))
410 (destructuring-bind (element-type) (rest (type-expand-to 'gslist type))
411 `(make-gslist ',element-type ,list)))
413 (define-type-method to-alien-function ((type gslist) &optional copy-p)
414 (destructuring-bind (element-type) (rest (type-expand-to 'gslist type))
417 (make-gslist element-type list (not copy-p)))
419 #'(lambda (list gslist)
420 (declare (ignore list))
421 (destroy-gslist gslist element-type t))))))
423 (define-type-method from-alien-form ((type gslist) form &key (ref :free))
424 (destructuring-bind (element-type) (rest (type-expand-to 'gslist type))
425 `(let ((gslist ,form))
427 (map-glist 'list #'identity gslist ',element-type
428 ,(ecase ref (:free :get) ((:static :temp) :peek) (:copy :read)))
429 ,(when (eq ref :free)
430 `(destroy-gslist gslist ',element-type))))))
432 (define-type-method from-alien-function ((type gslist) &key (ref :free))
433 (destructuring-bind (element-type) (rest (type-expand-to 'gslist type))
438 (map-glist 'list #'identity glist element-type :get)
439 (gslist-free glist))))
442 (map-glist 'list #'identity glist element-type :read)))
445 (map-glist 'list #'identity glist element-type :peek))))))
447 (define-type-method writer-function ((type gslist) &key temp inlined)
448 (assert-not-inlined type inlined)
449 (destructuring-bind (element-type) (rest (type-expand-to 'gslist type))
450 (let ((element-writer (writer-function element-type :temp temp)))
451 #'(lambda (list location &optional (offset 0))
453 (ref-pointer location offset)
454 (make-gslist element-writer list))))))
456 (define-type-method reader-function ((type gslist) &key (ref :read) inlined)
457 (assert-not-inlined type inlined)
458 (let ((element-type (second (type-expand-to 'gslist type))))
461 #'(lambda (location &optional (offset 0))
462 (unless (null-pointer-p (ref-pointer location offset))
463 (map-glist 'list #'identity (ref-pointer location offset) element-type ref))))
465 #'(lambda (location &optional (offset 0))
466 (unless (null-pointer-p (ref-pointer location offset))
468 (map-glist 'list #'identity (ref-pointer location offset) element-type :get)
469 (gslist-free (ref-pointer location offset))
470 (setf (ref-pointer location offset) (make-pointer 0)))))))))
472 (define-type-method destroy-function ((type gslist) &key temp inlined)
473 (assert-not-inlined type inlined)
474 (let ((element-type (second (type-expand-to 'gslist type))))
475 #'(lambda (location &optional (offset 0))
476 (unless (null-pointer-p (ref-pointer location offset))
477 (destroy-gslist (ref-pointer location offset) element-type temp)
478 (setf (ref-pointer location offset) (make-pointer 0))))))
480 (define-type-method copy-function ((type gslist) &key inlined)
481 (assert-not-inlined type inlined)
482 (destructuring-bind (element-type) (rest (type-expand-to 'gslist type))
483 (let ((copy-element (copy-function element-type)))
484 #'(lambda (from to &optional (offset 0))
485 (unless (null-pointer-p (ref-pointer from offset))
487 as from-list = (ref-pointer from offset)
488 then (glist-next from-list)
489 as to-list = (setf (ref-pointer to offset) (%gslist-append nil))
490 then (%gslist-append to-list)
491 do (funcall copy-element from-list to-list)
492 while (glist-next from-list)))))))