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.40 2007/06/15 12:25:16 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 ;;;; User data mechanism
57 (defvar *user-data-lock* (make-mutex))
58 (defvar *user-data* (make-hash-table))
59 (defvar *user-data-count* 0)
61 (defun register-user-data (object &optional destroy-function)
62 (check-type destroy-function (or null symbol function))
63 (with-mutex (*user-data-lock*)
64 (incf *user-data-count*)
66 (gethash *user-data-count* *user-data*)
67 (cons object destroy-function))
70 (defun find-user-data (id)
71 (check-type id fixnum)
72 (with-mutex (*user-data-lock*)
73 (multiple-value-bind (user-data p) (gethash id *user-data*)
74 (values (car user-data) p))))
76 (defun user-data-exists-p (id)
77 (nth-value 1 (find-user-data id)))
79 (defun update-user-data (id object)
80 (check-type id fixnum)
81 (with-mutex (*user-data-lock*)
82 (multiple-value-bind (user-data exists-p) (gethash id *user-data*)
84 ((not exists-p) (error "User data id ~A does not exist" id))
87 (funcall (cdr user-data) (car user-data)))
88 (setf (car user-data) object))))))
90 (defun destroy-user-data (id)
91 (check-type id fixnum)
92 (with-mutex (*user-data-lock*)
93 (let ((user-data (gethash id *user-data*)))
95 (funcall (cdr user-data) (car user-data))))
96 (remhash id *user-data*)))
101 (deftype quark () 'unsigned)
103 (defbinding %quark-from-string () quark
106 (defun quark-intern (object)
109 (string (%quark-from-string object))
110 (symbol (%quark-from-string (format nil "clg-~A:~A"
111 (package-name (symbol-package object))
114 (defbinding quark-to-string () (static string)
118 ;;;; Linked list (GList)
120 (deftype glist (type)
121 `(or null (cons ,type list)))
123 (defbinding (%glist-append "g_list_append") () pointer
124 (glist (or null pointer))
127 (defun make-glist (element-type list &optional temp-p)
128 (let ((writer (if (functionp element-type)
130 (writer-function element-type :temp temp-p))))
133 as glist = (%glist-append nil) then (%glist-append glist)
134 do (funcall writer element glist)
135 finally (return glist))))
137 (defun glist-next (glist)
138 (unless (null-pointer-p glist)
139 (ref-pointer glist #.(size-of 'pointer))))
141 ;; Also used for gslists
142 (defun map-glist (seqtype function glist element-type &optional (ref :read))
143 (let ((reader (if (functionp element-type)
145 (reader-function element-type :ref ref))))
149 as element = glist then (glist-next element)
150 until (null-pointer-p element)
151 do (funcall function (funcall reader element))))
154 as element = glist then (glist-next element)
155 until (null-pointer-p element)
156 collect (funcall function (funcall reader element))))
160 as element = glist then (glist-next element)
161 until (null-pointer-p element)
162 collect (funcall function (funcall reader element)))
165 (defbinding (glist-free "g_list_free") () nil
168 (defun destroy-glist (glist element-type &optional temp-p)
169 (let ((destroy (if (functionp element-type)
171 (destroy-function element-type :temp temp-p))))
173 as element = glist then (glist-next element)
174 until (null-pointer-p element)
175 do (funcall destroy element)))
178 (define-type-method alien-type ((type glist))
179 (declare (ignore type))
180 (alien-type 'pointer))
182 (define-type-method size-of ((type glist) &key inlined)
183 (assert-not-inlined type inlined)
186 (define-type-method type-alignment ((type glist) &key inlined)
187 (assert-not-inlined type inlined)
188 (type-alignment 'pointer))
190 (define-type-method alien-arg-wrapper ((type glist) var list style form &optional copy-in-p)
191 (destructuring-bind (element-type) (rest (type-expand-to 'glist type))
193 ((and (in-arg-p style) (not (out-arg-p style)))
194 `(with-pointer (,var (make-glist ',element-type ,list ,(not copy-in-p)))
198 `(destroy-glist ,var ',element-type t)))))
199 ((and (in-arg-p style) (out-arg-p style))
200 (let ((glist (make-symbol "GLIST")))
201 `(with-pointer (,glist (make-glist ',element-type ,list ,(not copy-in-p)))
202 (with-pointer (,var ,glist)
206 `(destroy-glist ,glist ',element-type t)))))))
207 ((and (out-arg-p style) (not (in-arg-p style)))
208 `(with-pointer (,var)
211 (define-type-method to-alien-form ((type glist) list &optional copy-p)
212 (declare (ignore copy-p))
213 (destructuring-bind (element-type) (rest (type-expand-to 'glist type))
214 `(make-glist ',element-type ,list)))
216 (define-type-method to-alien-function ((type glist) &optional copy-p)
217 (destructuring-bind (element-type) (rest (type-expand-to 'glist type))
220 (make-glist element-type list (not copy-p)))
222 #'(lambda (list glist)
223 (declare (ignore list))
224 (destroy-glist glist element-type t))))))
226 (define-type-method from-alien-form ((type glist) form &key (ref :free))
227 (destructuring-bind (element-type) (rest (type-expand-to 'glist type))
228 `(let ((glist ,form))
230 (map-glist 'list #'identity glist ',element-type
231 ,(ecase ref (:free :get) ((:static :temp) :peek) (:copy :read)))
232 ,(when (eq ref :free)
233 `(destroy-glist glist ',element-type))))))
235 (define-type-method from-alien-function ((type glist) &key (ref :free))
236 (destructuring-bind (element-type) (rest (type-expand-to 'glist type))
241 (map-glist 'list #'identity glist element-type :get)
242 (glist-free glist))))
245 (map-glist 'list #'identity glist element-type :read)))
248 (map-glist 'list #'identity glist element-type :peek))))))
250 (define-type-method writer-function ((type glist) &key temp inlined)
251 (assert-not-inlined type inlined)
252 (let ((element-type (second (type-expand-to 'glist type))))
253 #'(lambda (list location &optional (offset 0))
255 (ref-pointer location offset)
256 (make-glist element-type list temp)))))
258 (define-type-method reader-function ((type glist) &key (ref :read) inlined)
259 (assert-not-inlined type inlined)
260 (let ((element-type (second (type-expand-to 'glist type))))
263 #'(lambda (location &optional (offset 0))
264 (unless (null-pointer-p (ref-pointer location offset))
265 (map-glist 'list #'identity (ref-pointer location offset) element-type ref))))
267 #'(lambda (location &optional (offset 0))
268 (unless (null-pointer-p (ref-pointer location offset))
270 (map-glist 'list #'identity (ref-pointer location offset) element-type :get)
271 (glist-free (ref-pointer location offset))
272 (setf (ref-pointer location offset) (make-pointer 0)))))))))
274 (define-type-method destroy-function ((type glist) &key temp inlined)
275 (assert-not-inlined type inlined)
276 (let ((element-type (second (type-expand-to 'glist type))))
277 #'(lambda (location &optional (offset 0))
278 (unless (null-pointer-p (ref-pointer location offset))
279 (destroy-glist (ref-pointer location offset) element-type temp)
280 (setf (ref-pointer location offset) (make-pointer 0))))))
282 (define-type-method copy-function ((type glist) &key inlined)
283 (assert-not-inlined type inlined)
284 (destructuring-bind (element-type) (rest (type-expand-to 'glist type))
285 (let ((copy-element (copy-function element-type)))
286 #'(lambda (from to &optional (offset 0))
287 (unless (null-pointer-p (ref-pointer from offset))
289 as from-list = (ref-pointer from offset)
290 then (glist-next from-list)
291 as to-list = (setf (ref-pointer to offset) (%glist-append nil))
292 then (%glist-append to-list)
293 do (funcall copy-element from-list to-list)
294 while (glist-next from-lisT)))))))
297 ;;;; Single linked list (GSList)
299 (deftype gslist (type) `(or null (cons ,type list)))
301 (defbinding (%gslist-prepend "g_slist_prepend") () pointer
305 (defbinding (%gslist-append "g_slist_append") () pointer
306 (glist (or null pointer))
310 (defun make-gslist (element-type list &optional temp-p)
311 (let ((writer (if (functionp element-type)
313 (writer-function element-type :temp temp-p))))
315 for element in (reverse list)
316 as gslist = (%gslist-prepend (make-pointer 0)) then (%gslist-prepend gslist)
317 do (funcall writer element gslist)
318 finally (return gslist))))
320 (defbinding (gslist-free "g_slist_free") () nil
323 (defun destroy-gslist (gslist element-type &optional temp-p)
325 with destroy = (destroy-function element-type :temp temp-p)
326 as element = gslist then (glist-next element)
327 until (null-pointer-p element)
328 do (funcall destroy element 0))
329 (gslist-free gslist))
331 (define-type-method alien-type ((type gslist))
332 (declare (ignore type))
333 (alien-type 'pointer))
335 (define-type-method size-of ((type gslist) &key inlined)
336 (assert-not-inlined type inlined)
339 (define-type-method type-alignment ((type gslist) &key inlined)
340 (assert-not-inlined type inlined)
341 (type-alignment 'pointer))
343 (define-type-method alien-arg-wrapper ((type gslist) var list style form &optional copy-in-p)
344 (destructuring-bind (element-type) (rest (type-expand-to 'gslist type))
346 ((and (in-arg-p style) (not (out-arg-p style)))
347 `(with-pointer (,var (make-gslist ',element-type ,list ,(not copy-in-p)))
351 `(destroy-gslist ,var ',element-type t)))))
352 ((and (in-arg-p style) (out-arg-p style))
353 (let ((gslist (make-symbol "GSLIST")))
354 `(with-pointer (,gslist (make-gslist ',element-type ,list ,(not copy-in-p)))
355 (with-pointer (,var ,gslist)
359 `(destroy-gslist ,gslist ',element-type t)))))))
360 ((and (out-arg-p style) (not (in-arg-p style)))
361 `(with-pointer (,var)
364 (define-type-method to-alien-form ((type gslist) list &optional copy-p)
365 (declare (ignore copy-p))
366 (destructuring-bind (element-type) (rest (type-expand-to 'gslist type))
367 `(make-gslist ',element-type ,list)))
369 (define-type-method to-alien-function ((type gslist) &optional copy-p)
370 (destructuring-bind (element-type) (rest (type-expand-to 'gslist type))
373 (make-gslist element-type list (not copy-p)))
375 #'(lambda (list gslist)
376 (declare (ignore list))
377 (destroy-gslist gslist element-type t))))))
379 (define-type-method from-alien-form ((type gslist) form &key (ref :free))
380 (destructuring-bind (element-type) (rest (type-expand-to 'gslist type))
381 `(let ((gslist ,form))
383 (map-glist 'list #'identity gslist ',element-type
384 ,(ecase ref (:free :get) ((:static :temp) :peek) (:copy :read)))
385 ,(when (eq ref :free)
386 `(destroy-gslist gslist ',element-type))))))
388 (define-type-method from-alien-function ((type gslist) &key (ref :free))
389 (destructuring-bind (element-type) (rest (type-expand-to 'gslist type))
394 (map-glist 'list #'identity glist element-type :get)
395 (gslist-free glist))))
398 (map-glist 'list #'identity glist element-type :read)))
401 (map-glist 'list #'identity glist element-type :peek))))))
403 (define-type-method writer-function ((type gslist) &key temp inlined)
404 (assert-not-inlined type inlined)
405 (destructuring-bind (element-type) (rest (type-expand-to 'gslist type))
406 (let ((element-writer (writer-function element-type :temp temp)))
407 #'(lambda (list location &optional (offset 0))
409 (ref-pointer location offset)
410 (make-gslist element-writer list))))))
412 (define-type-method reader-function ((type gslist) &key (ref :read) inlined)
413 (assert-not-inlined type inlined)
414 (let ((element-type (second (type-expand-to 'gslist type))))
417 #'(lambda (location &optional (offset 0))
418 (unless (null-pointer-p (ref-pointer location offset))
419 (map-glist 'list #'identity (ref-pointer location offset) element-type ref))))
421 #'(lambda (location &optional (offset 0))
422 (unless (null-pointer-p (ref-pointer location offset))
424 (map-glist 'list #'identity (ref-pointer location offset) element-type :get)
425 (gslist-free (ref-pointer location offset))
426 (setf (ref-pointer location offset) (make-pointer 0)))))))))
428 (define-type-method destroy-function ((type gslist) &key temp inlined)
429 (assert-not-inlined type inlined)
430 (let ((element-type (second (type-expand-to 'gslist type))))
431 #'(lambda (location &optional (offset 0))
432 (unless (null-pointer-p (ref-pointer location offset))
433 (destroy-gslist (ref-pointer location offset) element-type temp)
434 (setf (ref-pointer location offset) (make-pointer 0))))))
436 (define-type-method copy-function ((type gslist) &key inlined)
437 (assert-not-inlined type inlined)
438 (destructuring-bind (element-type) (rest (type-expand-to 'gslist type))
439 (let ((copy-element (copy-function element-type)))
440 #'(lambda (from to &optional (offset 0))
441 (unless (null-pointer-p (ref-pointer from offset))
443 as from-list = (ref-pointer from offset)
444 then (glist-next from-list)
445 as to-list = (setf (ref-pointer to offset) (%gslist-append nil))
446 then (%gslist-append to-list)
447 do (funcall copy-element from-list to-list)
448 while (glist-next from-list)))))))