chiark / gitweb /
Various necessary changes
[clg] / glib / glib.lisp
CommitLineData
55212af1 1;; Common Lisp bindings for GTK+ 2.x
2;; Copyright 1999-2005 Espen S. Johnsen <espen@users.sf.net>
0d07716f 3;;
55212af1 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:
0d07716f 11;;
55212af1 12;; The above copyright notice and this permission notice shall be
13;; included in all copies or substantial portions of the Software.
0d07716f 14;;
55212af1 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
7fa3e612 23;; $Id: glib.lisp,v 1.37 2006/04/25 21:51:32 espen Exp $
0d07716f 24
25
26(in-package "GLIB")
b467f3d0 27
0d07716f 28(use-prefix "g")
29
30
31;;;; Memory management
32
7fa3e612 33(defbinding (%allocate-memory "g_malloc0") () pointer
0d07716f 34 (size unsigned-long))
35
7fa3e612 36(defbinding (%deallocate-memory "g_free") () nil
3fa4f6bd 37 (address pointer))
3d23e952 38
7fa3e612 39(setf
40 (symbol-function 'allocate-memory) #'%allocate-memory
41 (symbol-function 'deallocate-memory) #'%deallocate-memory)
0d07716f 42
b467f3d0 43
7fa3e612 44;;;; User data mechanism
b467f3d0 45
b467f3d0 46(defvar *user-data* (make-hash-table))
47(defvar *user-data-count* 0)
48
49(defun register-user-data (object &optional destroy-function)
50 (check-type destroy-function (or null symbol function))
51 (incf *user-data-count*)
52 (setf
53 (gethash *user-data-count* *user-data*)
54 (cons object destroy-function))
55 *user-data-count*)
56
57(defun find-user-data (id)
58 (check-type id fixnum)
59 (multiple-value-bind (user-data p) (gethash id *user-data*)
60 (values (car user-data) p)))
61
3e033db9 62(defun user-data-exists-p (id)
63 (nth-value 1 (find-user-data id)))
64
29c05201 65(defun update-user-data (id object)
66 (check-type id fixnum)
67 (multiple-value-bind (user-data exists-p) (gethash id *user-data*)
68 (cond
69 ((not exists-p) (error "User data id ~A does not exist" id))
70 (t
71 (when (cdr user-data)
72 (funcall (cdr user-data) (car user-data)))
73 (setf (car user-data) object)))))
74
b467f3d0 75(defun destroy-user-data (id)
76 (check-type id fixnum)
77 (let ((user-data (gethash id *user-data*)))
78 (when (cdr user-data)
79 (funcall (cdr user-data) (car user-data))))
80 (remhash id *user-data*))
81
0d07716f 82
6755fdad 83;;;; Quarks
84
85(deftype quark () 'unsigned)
86
cb816364 87(defbinding %quark-from-string () quark
e5b6173a 88 (string string))
89
3e033db9 90(defun quark-intern (object)
91 (etypecase object
92 (quark object)
93 (string (%quark-from-string object))
94 (symbol (%quark-from-string (format nil "clg-~A:~A"
95 (package-name (symbol-package object))
96 object)))))
6755fdad 97
7fa3e612 98(defbinding quark-to-string () (static string)
3e033db9 99 (quark quark))
6755fdad 100
101
597999f9 102;;;; Linked list (GList)
0d07716f 103
5f2222a9 104(deftype glist (type)
4d1fea77 105 `(or null (cons ,type list)))
0d07716f 106
5f2222a9 107(defbinding (%glist-append "g_list_append") () pointer
7fa3e612 108 (glist (or null pointer))
5f2222a9 109 (nil null))
597999f9 110
7fa3e612 111(defun make-glist (element-type list &optional temp-p)
112 (let ((writer (if (functionp element-type)
113 element-type
114 (writer-function element-type :temp temp-p))))
115 (loop
116 for element in list
117 as glist = (%glist-append nil) then (%glist-append glist)
118 do (funcall writer element glist)
119 finally (return glist))))
0d07716f 120
0d07716f 121(defun glist-next (glist)
122 (unless (null-pointer-p glist)
7fa3e612 123 (ref-pointer glist #.(size-of 'pointer))))
0d07716f 124
6baf860c 125;; Also used for gslists
7fa3e612 126(defun map-glist (seqtype function glist element-type &optional (ref :read))
127 (let ((reader (if (functionp element-type)
128 element-type
129 (reader-function element-type :ref ref))))
6baf860c 130 (case seqtype
131 ((nil)
132 (loop
7fa3e612 133 as element = glist then (glist-next element)
134 until (null-pointer-p element)
135 do (funcall function (funcall reader element))))
6baf860c 136 (list
137 (loop
7fa3e612 138 as element = glist then (glist-next element)
139 until (null-pointer-p element)
140 collect (funcall function (funcall reader element))))
6baf860c 141 (t
142 (coerce
143 (loop
7fa3e612 144 as element = glist then (glist-next element)
145 until (null-pointer-p element)
146 collect (funcall function (funcall reader element)))
6baf860c 147 seqtype)))))
148
1c99696e 149(defbinding (glist-free "g_list_free") () nil
0d07716f 150 (glist pointer))
151
7fa3e612 152(defun destroy-glist (glist element-type &optional temp-p)
153 (let ((destroy (if (functionp element-type)
154 element-type
155 (destroy-function element-type :temp temp-p))))
156 (loop
157 as element = glist then (glist-next element)
158 until (null-pointer-p element)
159 do (funcall destroy element)))
5f2222a9 160 (glist-free glist))
e5b6173a 161
4d1fea77 162(define-type-method alien-type ((type glist))
163 (declare (ignore type))
6baf860c 164 (alien-type 'pointer))
165
7fa3e612 166(define-type-method size-of ((type glist) &key inlined)
167 (assert-not-inlined type inlined)
e5b6173a 168 (size-of 'pointer))
0d07716f 169
5f2222a9 170
7fa3e612 171(define-type-method alien-arg-wrapper ((type glist) var list style form &optional copy-in-p)
172 (destructuring-bind (element-type) (rest (type-expand-to 'glist type))
173 (cond
174 ((and (in-arg-p style) (not (out-arg-p style)))
175 `(with-pointer (,var (make-glist ',element-type ,list ,(not copy-in-p)))
176 (unwind-protect
177 ,form
178 ,(unless copy-in-p
179 `(destroy-glist ,var ',element-type t)))))
180 ((and (in-arg-p style) (out-arg-p style))
181 (let ((glist (make-symbol "GLIST")))
182 `(with-pointer (,glist (make-glist ',element-type ,list ,(not copy-in-p)))
183 (with-pointer (,var ,glist)
184 (unwind-protect
185 ,form
186 ,(unless copy-in-p
187 `(destroy-glist ,glist ',element-type t)))))))
188 ((and (out-arg-p style) (not (in-arg-p style)))
189 `(with-pointer (,var)
190 ,form)))))
191
192(define-type-method to-alien-form ((type glist) list &optional copy-p)
193 (declare (ignore copy-p))
194 (destructuring-bind (element-type) (rest (type-expand-to 'glist type))
195 `(make-glist ',element-type ,list)))
0d07716f 196
7fa3e612 197(define-type-method to-alien-function ((type glist) &optional copy-p)
198 (destructuring-bind (element-type) (rest (type-expand-to 'glist type))
199 (values
200 #'(lambda (list)
201 (make-glist element-type list (not copy-p)))
202 (unless copy-p
203 #'(lambda (list glist)
204 (declare (ignore list))
205 (destroy-glist glist element-type t))))))
206
207(define-type-method from-alien-form ((type glist) form &key (ref :free))
208 (destructuring-bind (element-type) (rest (type-expand-to 'glist type))
209 `(let ((glist ,form))
210 (unwind-protect
211 (map-glist 'list #'identity glist ',element-type
212 ,(ecase ref (:free :get) ((:static :temp) :peek) (:copy :read)))
213 ,(when (eq ref :free)
214 `(destroy-glist glist ',element-type))))))
215
216(define-type-method from-alien-function ((type glist) &key (ref :free))
217 (destructuring-bind (element-type) (rest (type-expand-to 'glist type))
218 (ecase ref
219 (:free
220 #'(lambda (glist)
221 (prog1
222 (map-glist 'list #'identity glist element-type :get)
223 (glist-free glist))))
224 (:copy
225 #'(lambda (glist)
226 (map-glist 'list #'identity glist element-type :read)))
227 ((:static :temp)
228 #'(lambda (glist)
229 (map-glist 'list #'identity glist element-type :peek))))))
230
231(define-type-method writer-function ((type glist) &key temp inlined)
232 (assert-not-inlined type inlined)
4d1fea77 233 (let ((element-type (second (type-expand-to 'glist type))))
02b6647e 234 #'(lambda (list location &optional (offset 0))
235 (setf
7fa3e612 236 (ref-pointer location offset)
237 (make-glist element-type list temp)))))
02b6647e 238
7fa3e612 239(define-type-method reader-function ((type glist) &key (ref :read) inlined)
240 (assert-not-inlined type inlined)
4d1fea77 241 (let ((element-type (second (type-expand-to 'glist type))))
7fa3e612 242 (ecase ref
243 ((:read :peek)
244 #'(lambda (location &optional (offset 0))
245 (unless (null-pointer-p (ref-pointer location offset))
246 (map-glist 'list #'identity (ref-pointer location offset) element-type ref))))
247 (:get
248 #'(lambda (location &optional (offset 0))
249 (unless (null-pointer-p (ref-pointer location offset))
250 (prog1
251 (map-glist 'list #'identity (ref-pointer location offset) element-type :get)
252 (glist-free (ref-pointer location offset))
253 (setf (ref-pointer location offset) (make-pointer 0)))))))))
254
255(define-type-method destroy-function ((type glist) &key temp inlined)
256 (assert-not-inlined type inlined)
4d1fea77 257 (let ((element-type (second (type-expand-to 'glist type))))
02b6647e 258 #'(lambda (location &optional (offset 0))
7fa3e612 259 (unless (null-pointer-p (ref-pointer location offset))
260 (destroy-glist (ref-pointer location offset) element-type temp)
261 (setf (ref-pointer location offset) (make-pointer 0))))))
262
263(define-type-method copy-function ((type glist) &key inlined)
264 (assert-not-inlined type inlined)
265 (destructuring-bind (element-type) (rest (type-expand-to 'glist type))
266 (let ((copy-element (copy-function element-type)))
267 #'(lambda (from to &optional (offset 0))
268 (unless (null-pointer-p (ref-pointer from offset))
269 (loop
270 as from-list = (ref-pointer from offset)
271 then (glist-next from-list)
272 as to-list = (setf (ref-pointer to offset) (%glist-append nil))
273 then (%glist-append to-list)
274 do (funcall copy-element from-list to-list)
275 while (glist-next from-lisT)))))))
02b6647e 276
0d07716f 277
597999f9 278;;;; Single linked list (GSList)
279
4d1fea77 280(deftype gslist (type) `(or null (cons ,type list)))
597999f9 281
5f2222a9 282(defbinding (%gslist-prepend "g_slist_prepend") () pointer
597999f9 283 (gslist pointer)
5f2222a9 284 (nil null))
597999f9 285
7fa3e612 286(defbinding (%gslist-append "g_slist_append") () pointer
287 (glist (or null pointer))
288 (nil null))
289
290
291(defun make-gslist (element-type list &optional temp-p)
292 (let ((writer (if (functionp element-type)
293 element-type
294 (writer-function element-type :temp temp-p))))
295 (loop
296 for element in (reverse list)
297 as gslist = (%gslist-prepend (make-pointer 0)) then (%gslist-prepend gslist)
298 do (funcall writer element gslist)
299 finally (return gslist))))
6baf860c 300
1c99696e 301(defbinding (gslist-free "g_slist_free") () nil
597999f9 302 (gslist pointer))
303
7fa3e612 304(defun destroy-gslist (gslist element-type &optional temp-p)
5f2222a9 305 (loop
7fa3e612 306 with destroy = (destroy-function element-type :temp temp-p)
307 as element = gslist then (glist-next element)
308 until (null-pointer-p element)
309 do (funcall destroy element 0))
5f2222a9 310 (gslist-free gslist))
597999f9 311
4d1fea77 312(define-type-method alien-type ((type gslist))
313 (declare (ignore type))
6baf860c 314 (alien-type 'pointer))
315
7fa3e612 316(define-type-method size-of ((type gslist) &key inlined)
317 (assert-not-inlined type inlined)
597999f9 318 (size-of 'pointer))
319
7fa3e612 320(define-type-method alien-arg-wrapper ((type gslist) var list style form &optional copy-in-p)
321 (destructuring-bind (element-type) (rest (type-expand-to 'gslist type))
322 (cond
323 ((and (in-arg-p style) (not (out-arg-p style)))
324 `(with-pointer (,var (make-gslist ',element-type ,list ,(not copy-in-p)))
325 (unwind-protect
326 ,form
327 ,(unless copy-in-p
328 `(destroy-gslist ,var ',element-type t)))))
329 ((and (in-arg-p style) (out-arg-p style))
330 (let ((gslist (make-symbol "GSLIST")))
331 `(with-pointer (,gslist (make-gslist ',element-type ,list ,(not copy-in-p)))
332 (with-pointer (,var ,gslist)
333 (unwind-protect
334 ,form
335 ,(unless copy-in-p
336 `(destroy-gslist ,gslist ',element-type t)))))))
337 ((and (out-arg-p style) (not (in-arg-p style)))
338 `(with-pointer (,var)
339 ,form)))))
340
341(define-type-method to-alien-form ((type gslist) list &optional copy-p)
342 (declare (ignore copy-p))
343 (destructuring-bind (element-type) (rest (type-expand-to 'gslist type))
344 `(make-gslist ',element-type ,list)))
345
346(define-type-method to-alien-function ((type gslist) &optional copy-p)
347 (destructuring-bind (element-type) (rest (type-expand-to 'gslist type))
348 (values
349 #'(lambda (list)
350 (make-gslist element-type list (not copy-p)))
351 (unless copy-p
352 #'(lambda (list gslist)
353 (declare (ignore list))
354 (destroy-gslist gslist element-type t))))))
355
356(define-type-method from-alien-form ((type gslist) form &key (ref :free))
357 (destructuring-bind (element-type) (rest (type-expand-to 'gslist type))
358 `(let ((gslist ,form))
359 (unwind-protect
360 (map-glist 'list #'identity gslist ',element-type
361 ,(ecase ref (:free :get) ((:static :temp) :peek) (:copy :read)))
362 ,(when (eq ref :free)
363 `(destroy-gslist gslist ',element-type))))))
364
365(define-type-method from-alien-function ((type gslist) &key (ref :free))
366 (destructuring-bind (element-type) (rest (type-expand-to 'gslist type))
367 (ecase ref
368 (:free
369 #'(lambda (glist)
370 (prog1
371 (map-glist 'list #'identity glist element-type :get)
372 (gslist-free glist))))
373 (:copy
374 #'(lambda (glist)
375 (map-glist 'list #'identity glist element-type :read)))
376 ((:static :temp)
377 #'(lambda (glist)
378 (map-glist 'list #'identity glist element-type :peek))))))
379
380(define-type-method writer-function ((type gslist) &key temp inlined)
381 (assert-not-inlined type inlined)
382 (destructuring-bind (element-type) (rest (type-expand-to 'gslist type))
383 (let ((element-writer (writer-function element-type :temp temp)))
384 #'(lambda (list location &optional (offset 0))
385 (setf
386 (ref-pointer location offset)
387 (make-gslist element-writer list))))))
388
389(define-type-method reader-function ((type gslist) &key (ref :read) inlined)
390 (assert-not-inlined type inlined)
4d1fea77 391 (let ((element-type (second (type-expand-to 'gslist type))))
7fa3e612 392 (ecase ref
393 ((:read :peek)
394 #'(lambda (location &optional (offset 0))
395 (unless (null-pointer-p (ref-pointer location offset))
396 (map-glist 'list #'identity (ref-pointer location offset) element-type ref))))
397 (:get
398 #'(lambda (location &optional (offset 0))
399 (unless (null-pointer-p (ref-pointer location offset))
400 (prog1
401 (map-glist 'list #'identity (ref-pointer location offset) element-type :get)
402 (gslist-free (ref-pointer location offset))
403 (setf (ref-pointer location offset) (make-pointer 0)))))))))
404
405(define-type-method destroy-function ((type gslist) &key temp inlined)
406 (assert-not-inlined type inlined)
4d1fea77 407 (let ((element-type (second (type-expand-to 'gslist type))))
02b6647e 408 #'(lambda (location &optional (offset 0))
7fa3e612 409 (unless (null-pointer-p (ref-pointer location offset))
410 (destroy-gslist (ref-pointer location offset) element-type temp)
411 (setf (ref-pointer location offset) (make-pointer 0))))))
412
413(define-type-method copy-function ((type gslist) &key inlined)
414 (assert-not-inlined type inlined)
415 (destructuring-bind (element-type) (rest (type-expand-to 'gslist type))
416 (let ((copy-element (copy-function element-type)))
417 #'(lambda (from to &optional (offset 0))
418 (unless (null-pointer-p (ref-pointer from offset))
419 (loop
420 as from-list = (ref-pointer from offset)
421 then (glist-next from-list)
422 as to-list = (setf (ref-pointer to offset) (%gslist-append nil))
423 then (%gslist-append to-list)
424 do (funcall copy-element from-list to-list)
425 while (glist-next from-list)))))))