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.41 2007-09-07 07:33:51 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
60 ;;;; User data mechanism
62 (defvar *user-data-lock* (make-mutex))
63 (defvar *user-data* (make-hash-table))
64 (defvar *user-data-count* 0)
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*)
71 (gethash *user-data-count* *user-data*)
72 (cons object destroy-function))
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))))
81 (defun user-data-exists-p (id)
82 (nth-value 1 (find-user-data id)))
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*)
89 ((not exists-p) (error "User data id ~A does not exist" id))
92 (funcall (cdr user-data) (car user-data)))
93 (setf (car user-data) object))))))
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*)))
100 (funcall (cdr user-data) (car user-data))))
101 (remhash id *user-data*)))
106 (deftype quark () 'unsigned)
108 (defbinding %quark-from-string () quark
111 (defun quark-intern (object)
114 (string (%quark-from-string object))
115 (symbol (%quark-from-string (format nil "clg-~A:~A"
116 (package-name (symbol-package object))
119 (defbinding quark-to-string () (static string)
123 ;;;; Linked list (GList)
125 (deftype glist (type)
126 `(or null (cons ,type list)))
128 (defbinding (%glist-append "g_list_append") () pointer
129 (glist (or null pointer))
132 (defun make-glist (element-type list &optional temp-p)
133 (let ((writer (if (functionp element-type)
135 (writer-function element-type :temp temp-p))))
138 as glist = (%glist-append nil) then (%glist-append glist)
139 do (funcall writer element glist)
140 finally (return glist))))
142 (defun glist-next (glist)
143 (unless (null-pointer-p glist)
144 (ref-pointer glist #.(size-of 'pointer))))
146 ;; Also used for gslists
147 (defun map-glist (seqtype function glist element-type &optional (ref :read))
148 (let ((reader (if (functionp element-type)
150 (reader-function element-type :ref ref))))
154 as element = glist then (glist-next element)
155 until (null-pointer-p element)
156 do (funcall function (funcall reader element))))
159 as element = glist then (glist-next element)
160 until (null-pointer-p element)
161 collect (funcall function (funcall reader element))))
165 as element = glist then (glist-next element)
166 until (null-pointer-p element)
167 collect (funcall function (funcall reader element)))
170 (defbinding (glist-free "g_list_free") () nil
173 (defun destroy-glist (glist element-type &optional temp-p)
174 (let ((destroy (if (functionp element-type)
176 (destroy-function element-type :temp temp-p))))
178 as element = glist then (glist-next element)
179 until (null-pointer-p element)
180 do (funcall destroy element)))
183 (define-type-method alien-type ((type glist))
184 (declare (ignore type))
185 (alien-type 'pointer))
187 (define-type-method argument-type ((type glist))
190 (define-type-method return-type ((type glist))
193 (define-type-method size-of ((type glist) &key inlined)
194 (assert-not-inlined type inlined)
197 (define-type-method type-alignment ((type glist) &key inlined)
198 (assert-not-inlined type inlined)
199 (type-alignment 'pointer))
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))
204 ((and (in-arg-p style) (not (out-arg-p style)))
205 `(with-pointer (,var (make-glist ',element-type ,list ,(not 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)
217 `(destroy-glist ,glist ',element-type t)))))))
218 ((and (out-arg-p style) (not (in-arg-p style)))
219 `(with-pointer (,var)
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)))
227 (define-type-method to-alien-function ((type glist) &optional copy-p)
228 (destructuring-bind (element-type) (rest (type-expand-to 'glist type))
231 (make-glist element-type list (not copy-p)))
233 #'(lambda (list glist)
234 (declare (ignore list))
235 (destroy-glist glist element-type t))))))
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))
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))))))
246 (define-type-method from-alien-function ((type glist) &key (ref :free))
247 (destructuring-bind (element-type) (rest (type-expand-to 'glist type))
252 (map-glist 'list #'identity glist element-type :get)
253 (glist-free glist))))
256 (map-glist 'list #'identity glist element-type :read)))
259 (map-glist 'list #'identity glist element-type :peek))))))
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))
266 (ref-pointer location offset)
267 (make-glist element-type list temp)))))
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))))
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))))
278 #'(lambda (location &optional (offset 0))
279 (unless (null-pointer-p (ref-pointer location offset))
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)))))))))
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))))))
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))
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)))))))
308 ;;;; Single linked list (GSList)
310 (deftype gslist (type) `(or null (cons ,type list)))
312 (defbinding (%gslist-prepend "g_slist_prepend") () pointer
316 (defbinding (%gslist-append "g_slist_append") () pointer
317 (glist (or null pointer))
321 (defun make-gslist (element-type list &optional temp-p)
322 (let ((writer (if (functionp element-type)
324 (writer-function element-type :temp temp-p))))
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))))
331 (defbinding (gslist-free "g_slist_free") () nil
334 (defun destroy-gslist (gslist element-type &optional temp-p)
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))
342 (define-type-method alien-type ((type gslist))
343 (declare (ignore type))
344 (alien-type 'pointer))
346 (define-type-method argument-type ((type gslist))
349 (define-type-method return-type ((type gslist))
352 (define-type-method size-of ((type gslist) &key inlined)
353 (assert-not-inlined type inlined)
356 (define-type-method type-alignment ((type gslist) &key inlined)
357 (assert-not-inlined type inlined)
358 (type-alignment 'pointer))
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))
363 ((and (in-arg-p style) (not (out-arg-p style)))
364 `(with-pointer (,var (make-gslist ',element-type ,list ,(not 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)
376 `(destroy-gslist ,gslist ',element-type t)))))))
377 ((and (out-arg-p style) (not (in-arg-p style)))
378 `(with-pointer (,var)
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)))
386 (define-type-method to-alien-function ((type gslist) &optional copy-p)
387 (destructuring-bind (element-type) (rest (type-expand-to 'gslist type))
390 (make-gslist element-type list (not copy-p)))
392 #'(lambda (list gslist)
393 (declare (ignore list))
394 (destroy-gslist gslist element-type t))))))
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))
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))))))
405 (define-type-method from-alien-function ((type gslist) &key (ref :free))
406 (destructuring-bind (element-type) (rest (type-expand-to 'gslist type))
411 (map-glist 'list #'identity glist element-type :get)
412 (gslist-free glist))))
415 (map-glist 'list #'identity glist element-type :read)))
418 (map-glist 'list #'identity glist element-type :peek))))))
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))
426 (ref-pointer location offset)
427 (make-gslist element-writer list))))))
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))))
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))))
438 #'(lambda (location &optional (offset 0))
439 (unless (null-pointer-p (ref-pointer location offset))
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)))))))))
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))))))
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))
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)))))))