| 1 | ;; Common Lisp bindings for GTK+ 2.x |
| 2 | ;; Copyright 1999-2005 Espen S. Johnsen <espen@users.sf.net> |
| 3 | ;; |
| 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: |
| 11 | ;; |
| 12 | ;; The above copyright notice and this permission notice shall be |
| 13 | ;; included in all copies or substantial portions of the Software. |
| 14 | ;; |
| 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 | |
| 23 | ;; $Id: glib.lisp,v 1.37 2006-04-25 21:51:32 espen Exp $ |
| 24 | |
| 25 | |
| 26 | (in-package "GLIB") |
| 27 | |
| 28 | (use-prefix "g") |
| 29 | |
| 30 | |
| 31 | ;;;; Memory management |
| 32 | |
| 33 | (defbinding (%allocate-memory "g_malloc0") () pointer |
| 34 | (size unsigned-long)) |
| 35 | |
| 36 | (defbinding (%deallocate-memory "g_free") () nil |
| 37 | (address pointer)) |
| 38 | |
| 39 | (setf |
| 40 | (symbol-function 'allocate-memory) #'%allocate-memory |
| 41 | (symbol-function 'deallocate-memory) #'%deallocate-memory) |
| 42 | |
| 43 | |
| 44 | ;;;; User data mechanism |
| 45 | |
| 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 | |
| 62 | (defun user-data-exists-p (id) |
| 63 | (nth-value 1 (find-user-data id))) |
| 64 | |
| 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 | |
| 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 | |
| 82 | |
| 83 | ;;;; Quarks |
| 84 | |
| 85 | (deftype quark () 'unsigned) |
| 86 | |
| 87 | (defbinding %quark-from-string () quark |
| 88 | (string string)) |
| 89 | |
| 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))))) |
| 97 | |
| 98 | (defbinding quark-to-string () (static string) |
| 99 | (quark quark)) |
| 100 | |
| 101 | |
| 102 | ;;;; Linked list (GList) |
| 103 | |
| 104 | (deftype glist (type) |
| 105 | `(or null (cons ,type list))) |
| 106 | |
| 107 | (defbinding (%glist-append "g_list_append") () pointer |
| 108 | (glist (or null pointer)) |
| 109 | (nil null)) |
| 110 | |
| 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)))) |
| 120 | |
| 121 | (defun glist-next (glist) |
| 122 | (unless (null-pointer-p glist) |
| 123 | (ref-pointer glist #.(size-of 'pointer)))) |
| 124 | |
| 125 | ;; Also used for gslists |
| 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)))) |
| 130 | (case seqtype |
| 131 | ((nil) |
| 132 | (loop |
| 133 | as element = glist then (glist-next element) |
| 134 | until (null-pointer-p element) |
| 135 | do (funcall function (funcall reader element)))) |
| 136 | (list |
| 137 | (loop |
| 138 | as element = glist then (glist-next element) |
| 139 | until (null-pointer-p element) |
| 140 | collect (funcall function (funcall reader element)))) |
| 141 | (t |
| 142 | (coerce |
| 143 | (loop |
| 144 | as element = glist then (glist-next element) |
| 145 | until (null-pointer-p element) |
| 146 | collect (funcall function (funcall reader element))) |
| 147 | seqtype))))) |
| 148 | |
| 149 | (defbinding (glist-free "g_list_free") () nil |
| 150 | (glist pointer)) |
| 151 | |
| 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))) |
| 160 | (glist-free glist)) |
| 161 | |
| 162 | (define-type-method alien-type ((type glist)) |
| 163 | (declare (ignore type)) |
| 164 | (alien-type 'pointer)) |
| 165 | |
| 166 | (define-type-method size-of ((type glist) &key inlined) |
| 167 | (assert-not-inlined type inlined) |
| 168 | (size-of 'pointer)) |
| 169 | |
| 170 | |
| 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))) |
| 196 | |
| 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) |
| 233 | (let ((element-type (second (type-expand-to 'glist type)))) |
| 234 | #'(lambda (list location &optional (offset 0)) |
| 235 | (setf |
| 236 | (ref-pointer location offset) |
| 237 | (make-glist element-type list temp))))) |
| 238 | |
| 239 | (define-type-method reader-function ((type glist) &key (ref :read) inlined) |
| 240 | (assert-not-inlined type inlined) |
| 241 | (let ((element-type (second (type-expand-to 'glist type)))) |
| 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) |
| 257 | (let ((element-type (second (type-expand-to 'glist type)))) |
| 258 | #'(lambda (location &optional (offset 0)) |
| 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))))))) |
| 276 | |
| 277 | |
| 278 | ;;;; Single linked list (GSList) |
| 279 | |
| 280 | (deftype gslist (type) `(or null (cons ,type list))) |
| 281 | |
| 282 | (defbinding (%gslist-prepend "g_slist_prepend") () pointer |
| 283 | (gslist pointer) |
| 284 | (nil null)) |
| 285 | |
| 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)))) |
| 300 | |
| 301 | (defbinding (gslist-free "g_slist_free") () nil |
| 302 | (gslist pointer)) |
| 303 | |
| 304 | (defun destroy-gslist (gslist element-type &optional temp-p) |
| 305 | (loop |
| 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)) |
| 310 | (gslist-free gslist)) |
| 311 | |
| 312 | (define-type-method alien-type ((type gslist)) |
| 313 | (declare (ignore type)) |
| 314 | (alien-type 'pointer)) |
| 315 | |
| 316 | (define-type-method size-of ((type gslist) &key inlined) |
| 317 | (assert-not-inlined type inlined) |
| 318 | (size-of 'pointer)) |
| 319 | |
| 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) |
| 391 | (let ((element-type (second (type-expand-to 'gslist type)))) |
| 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) |
| 407 | (let ((element-type (second (type-expand-to 'gslist type)))) |
| 408 | #'(lambda (location &optional (offset 0)) |
| 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))))))) |