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