| 1 | ;; Common Lisp bindings for GTK+ v2.0 |
| 2 | ;; Copyright (C) 2000 Espen S. Johnsen <esj@stud.cs.uit.no> |
| 3 | ;; |
| 4 | ;; This library is free software; you can redistribute it and/or |
| 5 | ;; modify it under the terms of the GNU Lesser General Public |
| 6 | ;; License as published by the Free Software Foundation; either |
| 7 | ;; version 2 of the License, or (at your option) any later version. |
| 8 | ;; |
| 9 | ;; This library is distributed in the hope that it will be useful, |
| 10 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | ;; Lesser General Public License for more details. |
| 13 | ;; |
| 14 | ;; You should have received a copy of the GNU Lesser General Public |
| 15 | ;; License along with this library; if not, write to the Free Software |
| 16 | ;; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | |
| 18 | ;; $Id: proxy.lisp,v 1.15 2004/12/16 23:19:17 espen Exp $ |
| 19 | |
| 20 | (in-package "GLIB") |
| 21 | |
| 22 | ;;;; Superclass for all metaclasses implementing some sort of virtual slots |
| 23 | |
| 24 | (eval-when (:compile-toplevel :load-toplevel :execute) |
| 25 | (defclass virtual-slots-class (standard-class) |
| 26 | ()) |
| 27 | |
| 28 | (defclass direct-virtual-slot-definition (standard-direct-slot-definition) |
| 29 | ((setter :reader slot-definition-setter :initarg :setter) |
| 30 | (getter :reader slot-definition-getter :initarg :getter) |
| 31 | (unbound :reader slot-definition-unbound :initarg :unbound) |
| 32 | (boundp :reader slot-definition-boundp :initarg :boundp))) |
| 33 | |
| 34 | (defclass effective-virtual-slot-definition (standard-effective-slot-definition) |
| 35 | ((setter :reader slot-definition-setter :initarg :setter) |
| 36 | (getter :reader slot-definition-getter :initarg :getter) |
| 37 | (unbound :reader slot-definition-unbound :initarg :unbound) |
| 38 | (boundp :reader slot-definition-boundp :initarg :boundp))) |
| 39 | |
| 40 | (defvar *unbound-marker* (gensym "UNBOUND-MARKER-")) |
| 41 | |
| 42 | (defun most-specific-slot-value (instances slot &optional |
| 43 | (default *unbound-marker*)) |
| 44 | (let ((object (find-if |
| 45 | #'(lambda (ob) |
| 46 | (and (slot-exists-p ob slot) (slot-boundp ob slot))) |
| 47 | instances))) |
| 48 | (if object |
| 49 | (slot-value object slot) |
| 50 | default)))) |
| 51 | |
| 52 | |
| 53 | |
| 54 | (defmethod direct-slot-definition-class ((class virtual-slots-class) &rest initargs) |
| 55 | (if (eq (getf initargs :allocation) :virtual) |
| 56 | (find-class 'direct-virtual-slot-definition) |
| 57 | (call-next-method))) |
| 58 | |
| 59 | (defmethod effective-slot-definition-class ((class virtual-slots-class) &rest initargs) |
| 60 | (if (eq (getf initargs :allocation) :virtual) |
| 61 | (find-class 'effective-virtual-slot-definition) |
| 62 | (call-next-method))) |
| 63 | |
| 64 | |
| 65 | (defmethod initialize-internal-slot-functions ((slotd effective-virtual-slot-definition)) |
| 66 | (if (not (slot-boundp slotd 'getter)) |
| 67 | (setf |
| 68 | (slot-value slotd 'reader-function) |
| 69 | #'(lambda (object) |
| 70 | (declare (ignore object)) |
| 71 | (error "Can't read slot: ~A" (slot-definition-name slotd))) |
| 72 | (slot-value slotd 'boundp-function) |
| 73 | #'(lambda (object) (declare (ignore object)) nil)) |
| 74 | |
| 75 | (let ((getter-function |
| 76 | (let ((getter (slot-value slotd 'getter))) |
| 77 | (etypecase getter |
| 78 | (function getter) |
| 79 | (symbol |
| 80 | #'(lambda (object) |
| 81 | (funcall getter object))) |
| 82 | (string |
| 83 | (let ((reader nil)) |
| 84 | (setf (slot-value slotd 'reader-function) |
| 85 | #'(lambda (object) |
| 86 | (unless reader |
| 87 | (setq reader |
| 88 | (mkbinding getter |
| 89 | (slot-definition-type slotd) 'pointer))) |
| 90 | (funcall reader (proxy-location object)))))))))) |
| 91 | |
| 92 | (setf |
| 93 | (slot-value slotd 'boundp-function) |
| 94 | (cond |
| 95 | ((and |
| 96 | (not (slot-boundp slotd 'unbound)) |
| 97 | (not (slot-boundp slotd 'boundp))) |
| 98 | #'(lambda (object) (declare (ignore object)) t)) |
| 99 | ((slot-boundp slotd 'unbound) |
| 100 | (let ((unbound-value (slot-value slotd 'unbound))) |
| 101 | (lambda (object) |
| 102 | (not (eq (funcall getter-function object) unbound-value))))) |
| 103 | ((let ((boundp (slot-value slotd 'boundp))) |
| 104 | (etypecase boundp |
| 105 | (function boundp) |
| 106 | (symbol #'(lambda (object) |
| 107 | (funcall boundp object))) |
| 108 | (string (let ((reader ())) |
| 109 | #'(lambda (object) |
| 110 | (unless reader |
| 111 | (setq reader |
| 112 | (mkbinding boundp |
| 113 | (slot-definition-type slotd) 'pointer))) |
| 114 | (funcall reader (proxy-location object)))))))))) |
| 115 | |
| 116 | (setf |
| 117 | (slot-value slotd 'reader-function) |
| 118 | (cond |
| 119 | ((slot-boundp slotd 'unbound) |
| 120 | (let ((unbound (slot-value slotd 'unbound)) |
| 121 | (slot-name (slot-definition-name slotd))) |
| 122 | (lambda (object) |
| 123 | (let ((value (funcall getter-function object))) |
| 124 | (if (eq value unbound) |
| 125 | (slot-unbound (class-of object) object slot-name) |
| 126 | value))))) |
| 127 | ((slot-boundp slotd 'boundp) |
| 128 | (let ((boundp-function (slot-value slotd 'boundp-function))) |
| 129 | (lambda (object) |
| 130 | (and |
| 131 | (funcall boundp-function object) |
| 132 | (funcall getter-function object))))) |
| 133 | (getter-function))))) |
| 134 | |
| 135 | (setf |
| 136 | (slot-value slotd 'writer-function) |
| 137 | (if (not (slot-boundp slotd 'setter)) |
| 138 | #'(lambda (object) |
| 139 | (declare (ignore object)) |
| 140 | (error "Can't set slot: ~A" (slot-definition-name slotd))) |
| 141 | (with-slots (setter) slotd |
| 142 | (etypecase setter |
| 143 | (function setter) |
| 144 | ((or symbol cons) |
| 145 | #'(lambda (value object) |
| 146 | (funcall (fdefinition setter) value object))) |
| 147 | (string |
| 148 | (let ((writer ())) |
| 149 | (setf |
| 150 | (slot-value slotd 'writer-function) |
| 151 | #'(lambda (value object) |
| 152 | (unless writer |
| 153 | (setq writer |
| 154 | (mkbinding setter 'nil 'pointer |
| 155 | (slot-definition-type slotd)))) |
| 156 | (funcall writer (proxy-location object) value))))))))) |
| 157 | |
| 158 | (initialize-internal-slot-gfs (slot-definition-name slotd))) |
| 159 | |
| 160 | |
| 161 | |
| 162 | (defmethod compute-slot-accessor-info ((slotd effective-virtual-slot-definition) type gf) |
| 163 | nil) |
| 164 | |
| 165 | (defmethod compute-effective-slot-definition-initargs ((class virtual-slots-class) direct-slotds) |
| 166 | (if (typep (first direct-slotds) 'direct-virtual-slot-definition) |
| 167 | (let ((initargs ())) |
| 168 | (let ((getter (most-specific-slot-value direct-slotds 'getter))) |
| 169 | (unless (eq getter *unbound-marker*) |
| 170 | (setf (getf initargs :getter) getter))) |
| 171 | (let ((setter (most-specific-slot-value direct-slotds 'setter))) |
| 172 | (unless (eq setter *unbound-marker*) |
| 173 | (setf (getf initargs :setter) setter))) |
| 174 | (let ((unbound (most-specific-slot-value direct-slotds 'unbound))) |
| 175 | (unless (eq unbound *unbound-marker*) |
| 176 | (setf (getf initargs :unbound) unbound))) |
| 177 | (let ((boundp (most-specific-slot-value direct-slotds 'boundp))) |
| 178 | (unless (eq boundp *unbound-marker*) |
| 179 | (setf (getf initargs :boundp) boundp))) |
| 180 | (nconc initargs (call-next-method))) |
| 181 | (call-next-method))) |
| 182 | |
| 183 | |
| 184 | (defmethod slot-value-using-class |
| 185 | ((class virtual-slots-class) (object standard-object) |
| 186 | (slotd effective-virtual-slot-definition)) |
| 187 | (if (funcall (slot-value slotd 'boundp-function) object) |
| 188 | (funcall (slot-value slotd 'reader-function) object) |
| 189 | (slot-unbound class object (slot-definition-name slotd)))) |
| 190 | |
| 191 | (defmethod slot-boundp-using-class |
| 192 | ((class virtual-slots-class) (object standard-object) |
| 193 | (slotd effective-virtual-slot-definition)) |
| 194 | (funcall (slot-value slotd 'boundp-function) object)) |
| 195 | |
| 196 | (defmethod (setf slot-value-using-class) |
| 197 | (value (class virtual-slots-class) (object standard-object) |
| 198 | (slotd effective-virtual-slot-definition)) |
| 199 | (funcall (slot-value slotd 'writer-function) value object)) |
| 200 | |
| 201 | |
| 202 | (defmethod validate-superclass |
| 203 | ((class virtual-slots-class) (super standard-class)) |
| 204 | t) |
| 205 | |
| 206 | |
| 207 | ;;;; Proxy cache |
| 208 | |
| 209 | (internal *instance-cache*) |
| 210 | (defvar *instance-cache* (make-hash-table :test #'eql)) |
| 211 | |
| 212 | (defun cache-instance (instance) |
| 213 | (setf |
| 214 | (gethash (system:sap-int (proxy-location instance)) *instance-cache*) |
| 215 | (ext:make-weak-pointer instance))) |
| 216 | |
| 217 | (defun find-cached-instance (location) |
| 218 | (let ((ref (gethash (system:sap-int location) *instance-cache*))) |
| 219 | (when ref |
| 220 | (ext:weak-pointer-value ref)))) |
| 221 | |
| 222 | (defun instance-cached-p (location) |
| 223 | (gethash (system:sap-int location) *instance-cache*)) |
| 224 | |
| 225 | (defun remove-cached-instance (location) |
| 226 | (remhash (system:sap-int location) *instance-cache*)) |
| 227 | |
| 228 | ;; For debuging |
| 229 | (defun cached-instances () |
| 230 | (let ((instances ())) |
| 231 | (maphash #'(lambda (location ref) |
| 232 | (declare (ignore location)) |
| 233 | (push (ext:weak-pointer-value ref) instances)) |
| 234 | *instance-cache*) |
| 235 | instances)) |
| 236 | |
| 237 | |
| 238 | |
| 239 | ;;;; Proxy for alien instances |
| 240 | |
| 241 | (defclass proxy () |
| 242 | ((location :reader proxy-location :type system-area-pointer))) |
| 243 | |
| 244 | (defgeneric initialize-proxy (object &rest initargs)) |
| 245 | (defgeneric instance-finalizer (object)) |
| 246 | (defgeneric reference-foreign (class location)) |
| 247 | (defgeneric unreference-foreign (class location)) |
| 248 | |
| 249 | (defmethod unreference-foreign :around ((class class) location) |
| 250 | (unless (null-pointer-p location) |
| 251 | ;; (format t "Unreferencing ~A at ~A" (class-name class) location) |
| 252 | ;; (finish-output *standard-output*) |
| 253 | (call-next-method) |
| 254 | ;; (write-line " done") |
| 255 | ;; (finish-output *standard-output*) |
| 256 | )) |
| 257 | |
| 258 | (defmethod print-object ((instance proxy) stream) |
| 259 | (print-unreadable-object (instance stream :type t :identity nil) |
| 260 | (when (slot-boundp instance 'location) |
| 261 | (format stream "at 0x~X" (sap-int (proxy-location instance)))))) |
| 262 | |
| 263 | (defmethod initialize-instance :around ((instance proxy) &key location) |
| 264 | (if location |
| 265 | (setf (slot-value instance 'location) location) |
| 266 | (call-next-method)) |
| 267 | (cache-instance instance) |
| 268 | (ext:finalize instance (instance-finalizer instance)) |
| 269 | instance) |
| 270 | |
| 271 | (defmethod instance-finalizer ((instance proxy)) |
| 272 | (let ((location (proxy-location instance)) |
| 273 | (class (class-of instance))) |
| 274 | ;; (unless (find-method #'unreference-foreign nil (list (class-of class) t) nil) |
| 275 | ;; (error "No matching method for UNREFERENCE-INSTANCE when called with class ~A" class)) |
| 276 | #'(lambda () |
| 277 | (remove-cached-instance location) |
| 278 | (unreference-foreign class location)))) |
| 279 | |
| 280 | |
| 281 | ;;;; Metaclass used for subclasses of proxy |
| 282 | |
| 283 | (eval-when (:compile-toplevel :load-toplevel :execute) |
| 284 | (defclass proxy-class (virtual-slots-class) |
| 285 | ((size :reader proxy-instance-size))) |
| 286 | |
| 287 | (defclass direct-alien-slot-definition (direct-virtual-slot-definition) |
| 288 | ((allocation :initform :alien) |
| 289 | (offset :reader slot-definition-offset :initarg :offset))) |
| 290 | |
| 291 | (defclass effective-alien-slot-definition (effective-virtual-slot-definition) |
| 292 | ((offset :reader slot-definition-offset :initarg :offset))) |
| 293 | |
| 294 | |
| 295 | (defmethod most-specific-proxy-superclass ((class proxy-class)) |
| 296 | (find-if |
| 297 | #'(lambda (class) |
| 298 | (subtypep (class-name class) 'proxy)) |
| 299 | (cdr (compute-class-precedence-list class)))) |
| 300 | |
| 301 | (defmethod direct-proxy-superclass ((class proxy-class)) |
| 302 | (find-if |
| 303 | #'(lambda (class) |
| 304 | (subtypep (class-name class) 'proxy)) |
| 305 | (class-direct-superclasses class))) |
| 306 | |
| 307 | (defmethod shared-initialize ((class proxy-class) names &key size) |
| 308 | (call-next-method) |
| 309 | (cond |
| 310 | (size (setf (slot-value class 'size) (first size))) |
| 311 | ((slot-boundp class 'size) (slot-makunbound class 'size)))) |
| 312 | |
| 313 | (defmethod direct-slot-definition-class ((class proxy-class) &rest initargs) |
| 314 | (case (getf initargs :allocation) |
| 315 | ((nil :alien) (find-class 'direct-alien-slot-definition)) |
| 316 | (t (call-next-method)))) |
| 317 | |
| 318 | (defmethod effective-slot-definition-class ((class proxy-class) &rest initargs) |
| 319 | (case (getf initargs :allocation) |
| 320 | (:alien (find-class 'effective-alien-slot-definition)) |
| 321 | (t (call-next-method)))) |
| 322 | |
| 323 | |
| 324 | (defmethod compute-effective-slot-definition-initargs ((class proxy-class) direct-slotds) |
| 325 | (if (eq (most-specific-slot-value direct-slotds 'allocation) :alien) |
| 326 | (nconc |
| 327 | (list :offset (most-specific-slot-value direct-slotds 'offset)) |
| 328 | (call-next-method)) |
| 329 | (call-next-method))) |
| 330 | |
| 331 | |
| 332 | (defmethod initialize-internal-slot-functions ((slotd effective-alien-slot-definition)) |
| 333 | (with-slots (offset) slotd |
| 334 | (let ((type (slot-definition-type slotd))) |
| 335 | (unless (slot-boundp slotd 'getter) |
| 336 | (let ((reader (reader-function type))) |
| 337 | (setf |
| 338 | (slot-value slotd 'getter) |
| 339 | #'(lambda (object) |
| 340 | (funcall reader (proxy-location object) offset))))) |
| 341 | |
| 342 | (unless (slot-boundp slotd 'setter) |
| 343 | (let ((writer (writer-function type)) |
| 344 | (destroy (destroy-function type))) |
| 345 | (setf |
| 346 | (slot-value slotd 'setter) |
| 347 | #'(lambda (value object) |
| 348 | (let ((location (proxy-location object))) |
| 349 | (funcall destroy location offset) ; destroy old value |
| 350 | (funcall writer value location offset)))))))) |
| 351 | |
| 352 | (call-next-method)) |
| 353 | |
| 354 | |
| 355 | ;; TODO: call some C code to detect this a compile time |
| 356 | (defconstant +struct-alignmen+ 4) |
| 357 | |
| 358 | (defmethod compute-slots ((class proxy-class)) |
| 359 | (loop |
| 360 | with offset = (proxy-instance-size (most-specific-proxy-superclass class)) |
| 361 | with size = offset |
| 362 | for slotd in (class-direct-slots class) |
| 363 | when (eq (slot-definition-allocation slotd) :alien) |
| 364 | do (if (not (slot-boundp slotd 'offset)) |
| 365 | (setf (slot-value slotd 'offset) offset) |
| 366 | (setq offset (slot-value slotd 'offset))) |
| 367 | |
| 368 | (incf offset (size-of (slot-definition-type slotd))) |
| 369 | (incf offset (mod offset +struct-alignmen+)) |
| 370 | (setq size (max size offset)) |
| 371 | |
| 372 | finally (unless (slot-boundp class 'size) |
| 373 | (setf (slot-value class 'size) size))) |
| 374 | (call-next-method)) |
| 375 | |
| 376 | |
| 377 | (defmethod validate-superclass ((class proxy-class) (super standard-class)) |
| 378 | (subtypep (class-name super) 'proxy)) |
| 379 | |
| 380 | (defmethod proxy-instance-size (class) |
| 381 | (declare (ignore class)) |
| 382 | 0) |
| 383 | ) |
| 384 | |
| 385 | (defmethod alien-type ((class proxy-class) &rest args) |
| 386 | (declare (ignore class args)) |
| 387 | (alien-type 'pointer)) |
| 388 | |
| 389 | (defmethod size-of ((class proxy-class) &rest args) |
| 390 | (declare (ignore class args)) |
| 391 | (size-of 'pointer)) |
| 392 | |
| 393 | (defmethod from-alien-form (location (class proxy-class) &rest args) |
| 394 | (declare (ignore args)) |
| 395 | `(ensure-proxy-instance ',(class-name class) ,location)) |
| 396 | |
| 397 | (defmethod from-alien-function ((class proxy-class) &rest args) |
| 398 | (declare (ignore args)) |
| 399 | #'(lambda (location) |
| 400 | (ensure-proxy-instance class location))) |
| 401 | |
| 402 | (defmethod to-alien-form (instance (class proxy-class) &rest args) |
| 403 | (declare (ignore class args)) |
| 404 | `(proxy-location ,instance)) |
| 405 | |
| 406 | (defmethod to-alien-function ((class proxy-class) &rest args) |
| 407 | (declare (ignore class args)) |
| 408 | #'proxy-location) |
| 409 | |
| 410 | (defmethod copy-from-alien-form (location (class proxy-class) &rest args) |
| 411 | (declare (ignore args)) |
| 412 | (let ((class-name (class-name class))) |
| 413 | `(ensure-proxy-instance ',class-name |
| 414 | (reference-foreign ',class-name ,location)))) |
| 415 | |
| 416 | (defmethod copy-from-alien-function ((class proxy-class) &rest args) |
| 417 | (declare (ignore args)) |
| 418 | #'(lambda (location) |
| 419 | (ensure-proxy-instance class (reference-foreign class location)))) |
| 420 | |
| 421 | (defmethod copy-to-alien-form (instance (class proxy-class) &rest args) |
| 422 | (declare (ignore args)) |
| 423 | `(reference-foreign ',(class-name class) (proxy-location ,instance))) |
| 424 | |
| 425 | (defmethod copy-to-alien-function ((class proxy-class) &rest args) |
| 426 | (declare (ignore class args)) |
| 427 | #'(lambda (instance) |
| 428 | (reference-foreign class (proxy-location instance)))) |
| 429 | |
| 430 | (defmethod writer-function ((class proxy-class) &rest args) |
| 431 | (declare (ignore args)) |
| 432 | #'(lambda (instance location &optional (offset 0)) |
| 433 | (assert (null-pointer-p (sap-ref-sap location offset))) |
| 434 | (setf |
| 435 | (sap-ref-sap location offset) |
| 436 | (reference-foreign class (proxy-location instance))))) |
| 437 | |
| 438 | (defmethod reader-function ((class proxy-class) &rest args) |
| 439 | (declare (ignore args)) |
| 440 | #'(lambda (location &optional (offset 0)) |
| 441 | (let ((instance (sap-ref-sap location offset))) |
| 442 | (unless (null-pointer-p instance) |
| 443 | (ensure-proxy-instance class (reference-foreign class instance)))))) |
| 444 | |
| 445 | (defmethod destroy-function ((class proxy-class) &rest args) |
| 446 | (declare (ignore args)) |
| 447 | #'(lambda (location &optional (offset 0)) |
| 448 | (unreference-foreign class (sap-ref-sap location offset)))) |
| 449 | |
| 450 | |
| 451 | (defgeneric ensure-proxy-instance (class location) |
| 452 | (:documentation "Returns a proxy object representing the foreign object at the give location.")) |
| 453 | |
| 454 | (defmethod ensure-proxy-instance :around (class location) |
| 455 | (unless (null-pointer-p location) |
| 456 | (or |
| 457 | (find-cached-instance location) |
| 458 | (call-next-method)))) |
| 459 | |
| 460 | (defmethod ensure-proxy-instance ((class symbol) location) |
| 461 | (ensure-proxy-instance (find-class class) location)) |
| 462 | |
| 463 | (defmethod ensure-proxy-instance ((class proxy-class) location) |
| 464 | (make-instance class :location location)) |
| 465 | |
| 466 | |
| 467 | ;;;; Superclasses for wrapping of C structures |
| 468 | |
| 469 | (defclass struct (proxy) |
| 470 | () |
| 471 | (:metaclass proxy-class)) |
| 472 | |
| 473 | (defmethod initialize-instance ((struct struct) &rest initargs) |
| 474 | (declare (ignore initargs)) |
| 475 | (unless (slot-boundp struct 'location) |
| 476 | (let ((size (proxy-instance-size (class-of struct)))) |
| 477 | (if (zerop size) |
| 478 | (error "~A has zero size" (class-of struct)) |
| 479 | (setf (slot-value struct 'location) (allocate-memory size))))) |
| 480 | (call-next-method)) |
| 481 | |
| 482 | |
| 483 | ;;;; Metaclasses used for subclasses of struct |
| 484 | |
| 485 | (defclass struct-class (proxy-class) |
| 486 | ()) |
| 487 | |
| 488 | (defmethod reference-foreign ((class struct-class) location) |
| 489 | (copy-memory location (proxy-instance-size class))) |
| 490 | |
| 491 | (defmethod unreference-foreign ((class struct-class) location) |
| 492 | (deallocate-memory location)) |
| 493 | |
| 494 | |
| 495 | (defclass static-struct-class (struct-class) |
| 496 | ()) |
| 497 | |
| 498 | (defmethod reference-foreign ((class static-struct-class) location) |
| 499 | (declare (ignore class)) |
| 500 | location) |
| 501 | |
| 502 | (defmethod unreference-foreign ((class static-struct-class) location) |
| 503 | (declare (ignore class location)) |
| 504 | nil) |