1 ;; Common Lisp bindings for GTK+ v2.0
2 ;; Copyright (C) 1999-2001 Espen S. Johnsen <esj@stud.cs.uit.no>
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.
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.
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
18 ;; $Id: gforeign.lisp,v 1.8 2001/05/04 17:00:37 espen Exp $
24 (defvar *type-methods* (make-hash-table))
26 (defun ensure-type-method-fun (fname)
27 (unless (fboundp fname)
29 (symbol-function fname)
30 #'(lambda (type-spec &rest args)
32 (find-applicable-type-method type-spec fname) type-spec args)))))
34 (defmacro define-type-method-fun (fname lambda-list)
35 (declare (ignore lambda-list))
36 `(defun ,fname (type-spec &rest args)
38 (find-applicable-type-method type-spec ',fname) type-spec args)))
41 (defun ensure-type-name (type)
44 (pcl::class (class-name type))))
46 (defun add-type-method (type fname function)
49 (gethash (ensure-type-name type) *type-methods*)))
51 (defun find-type-method (type fname)
52 (cdr (assoc fname (gethash (ensure-type-name type) *type-methods*))))
54 (defun find-applicable-type-method (type-spec fname &optional (error t))
55 (flet ((find-superclass-method (class)
57 (dolist (super (cdr (pcl::class-precedence-list class)))
58 (return-if (find-type-method super fname)))))
59 (find-expanded-type-method (type-spec)
60 (multiple-value-bind (expanded-type-spec expanded-p)
61 (type-expand-1 type-spec)
64 (find-applicable-type-method expanded-type-spec fname nil))
66 (find-applicable-type-method t fname nil))))))
72 (find-type-method type-spec fname)
73 (find-superclass-method type-spec)))
76 (find-type-method type-spec fname)
77 (find-expanded-type-method type-spec)
78 (find-superclass-method (find-class type-spec nil))))
81 (find-type-method (first type-spec) fname)
82 (find-expanded-type-method type-spec)))
84 (error "Invalid type specifier ~A" type-spec)))
88 "No applicable method for ~A when called with type specifier ~A"
91 (defmacro deftype-method (fname type lambda-list &body body)
93 (ensure-type-method-fun ',fname)
94 (add-type-method ',type ',fname #'(lambda ,lambda-list ,@body))
97 ;; To make the compiler happy
98 (eval-when (:compile-toplevel :load-toplevel :execute)
99 (define-type-method-fun translate-type-spec (type-spec))
100 (define-type-method-fun size-of (type-spec))
101 (define-type-method-fun translate-to-alien (type-spec expr &optional weak-ref))
102 (define-type-method-fun translate-from-alien (type-spec expr &optional weak-ref))
103 (define-type-method-fun cleanup-alien (type-spec sap &otional weak-ref))
104 (define-type-method-fun unreference-alien (type-spec sap)))
109 (defvar *type-function-cache* (make-hash-table :test #'equal))
111 (defun get-cached-function (type-spec fname)
112 (cdr (assoc fname (gethash type-spec *type-function-cache*))))
114 (defun set-cached-function (type-spec fname function)
115 (push (cons fname function) (gethash type-spec *type-function-cache*))
119 (defun intern-argument-translator (type-spec)
121 (get-cached-function type-spec 'argument-translator)
122 (set-cached-function type-spec 'argument-translator
126 (declare (ignorable object))
127 ,(translate-to-alien type-spec 'object t))))))
129 (defun intern-return-value-translator (type-spec)
131 (get-cached-function type-spec 'return-value-translator)
132 (set-cached-function type-spec 'return-value-translator
136 (declare (ignorable alien))
137 ,(translate-from-alien type-spec 'alien nil))))))
139 (defun intern-cleanup-function (type-spec)
141 (get-cached-function type-spec 'cleanup-function)
142 (set-cached-function type-spec 'cleanup-function
146 (declare (ignorable alien))
147 ,(cleanup-alien type-spec 'alien t))))))
151 ;; Returns a function to write an object of the specified type
152 ;; to a memory location
153 (defun intern-writer-function (type-spec)
155 (get-cached-function type-spec 'writer-function)
156 (set-cached-function type-spec 'writer-function
159 `(lambda (value sap offset)
160 (declare (ignorable value sap offset))
162 (,(sap-ref-fname type-spec) sap offset)
163 ,(translate-to-alien type-spec 'value nil)))))))
165 ;; Returns a function to read an object of the specified type
166 ;; from a memory location
167 (defun intern-reader-function (type-spec)
169 (get-cached-function type-spec 'reader-function)
170 (set-cached-function type-spec 'reader-function
173 `(lambda (sap offset)
174 (declare (ignorable sap offset))
175 ,(translate-from-alien
176 type-spec `(,(sap-ref-fname type-spec) sap offset) t))))))
178 (defun intern-destroy-function (type-spec)
179 (if (atomic-type-p type-spec)
180 #'(lambda (sap offset)
181 (declare (ignore sap offset)))
183 (get-cached-function type-spec 'destroy-function)
184 (set-cached-function type-spec 'destroy-function
187 `(lambda (sap offset)
188 (declare (ignorable sap offset))
190 type-spec `(,(sap-ref-fname type-spec) sap offset))))))))
196 (defconstant +bits-per-unit+ 8
197 "Number of bits in an addressable unit (byte)")
199 ;; Sizes of fundamental C types in addressable units
200 (defconstant +size-of-short+ 2)
201 (defconstant +size-of-int+ 4)
202 (defconstant +size-of-long+ 4)
203 (defconstant +size-of-sap+ 4)
204 (defconstant +size-of-float+ 4)
205 (defconstant +size-of-double+ 8)
207 (defun sap-ref-unsigned (sap offset)
208 (sap-ref-32 sap offset))
210 (defun sap-ref-signed (sap offset)
211 (signed-sap-ref-32 sap offset))
213 (defun sap-ref-fname (type-spec)
214 (let ((alien-type-spec (mklist (translate-type-spec type-spec))))
215 (ecase (first alien-type-spec)
217 (ecase (second alien-type-spec)
223 (ecase (second alien-type-spec)
224 (8 'signed-sap-ref-8)
225 (16 'signed-sap-ref-16)
226 (32 'signed-sap-ref-32)
227 (64 'signed-sap-ref-64)))
228 (system-area-pointer 'sap-ref-sap)
229 (single-float 'sap-ref-single)
230 (double-float 'sap-ref-double))))
233 ;;;; Foreign function call interface
235 (defvar *package-prefix* nil)
237 (defun set-package-prefix (prefix &optional (package *package*))
238 (let ((package (find-package package)))
239 (delete-if #'(lambda (assoc) (eq (car assoc) package)) *package-prefix*)
240 (push (cons package prefix) *package-prefix*))
243 (defun package-prefix (&optional (package *package*))
244 (let ((package (find-package package)))
246 (cdr (assoc package *package-prefix*))
247 (substitute #\_ #\- (string-downcase (package-name package))))))
249 (defun find-prefix-package (prefix)
251 (car (rassoc (string-downcase prefix) *package-prefix* :test #'string=))
252 (find-package (string-upcase prefix))))
254 (defmacro use-prefix (prefix &optional (package *package*))
255 `(eval-when (:compile-toplevel :load-toplevel :execute)
256 (set-package-prefix ,prefix ,package)))
259 (defun default-alien-fname (lisp-name)
260 (let* ((lisp-name-string
261 (if (char= (char (the simple-string (string lisp-name)) 0) #\%)
262 (subseq (the simple-string (string lisp-name)) 1)
264 (prefix (package-prefix *package*))
265 (name (substitute #\_ #\- (string-downcase lisp-name-string))))
266 (if (or (not prefix) (string= prefix ""))
268 (format nil "~A_~A" prefix name))))
270 (defun default-alien-type-name (type-name)
271 (let ((prefix (package-prefix *package*)))
277 (cons prefix (split-string (symbol-name type-name) #\-))))))
279 (defun default-type-name (alien-name)
283 (split-string-if alien-name #'upper-case-p))))
286 (rest parts) #\-) (find-prefix-package (first parts)))))
289 (defmacro defbinding (name lambda-list return-type-spec &rest docs/args)
290 (multiple-value-bind (c-name lisp-name)
292 (values (default-alien-fname name) name)
294 (let ((supplied-lambda-list lambda-list)
297 (dolist (doc/arg docs/args)
298 (if (stringp doc/arg)
301 (destructuring-bind (expr type &optional (style :in)) doc/arg
302 (unless (member style '(:in :out :in-out))
303 (error "Bogus argument style ~S in ~S." style doc/arg))
305 (not supplied-lambda-list)
306 (namep expr) (member style '(:in :in-out)))
307 (push expr lambda-list))
309 (list (if (namep expr) expr (gensym)) expr type style) args)))))
312 c-name lisp-name (or supplied-lambda-list (nreverse lambda-list))
313 return-type-spec (reverse docs) (reverse args)))))
315 ;; For backward compatibility
316 (defmacro define-foreign (&rest args)
317 `(defbinding ,@args))
321 (defun %defbinding (foreign-name lisp-name lambda-list
322 return-type-spec docs args)
323 (ext:collect ((alien-types) (alien-bindings) (alien-parameters)
324 (alien-values) (alien-deallocators))
326 (destructuring-bind (var expr type-spec style) arg
327 (let ((declaration (translate-type-spec type-spec))
328 (deallocation (cleanup-alien type-spec expr t)))
330 ((member style '(:out :in-out))
331 (alien-types `(* ,declaration))
332 (alien-parameters `(addr ,var))
335 ,@(when (eq style :in-out)
336 (list (translate-to-alien type-spec expr t)))))
337 (alien-values (translate-from-alien type-spec var nil)))
339 (alien-types declaration)
341 `(,var ,declaration ,(translate-to-alien type-spec expr t)))
342 (alien-parameters var)
343 (alien-deallocators deallocation))
345 (alien-types declaration)
346 (alien-parameters (translate-to-alien type-spec expr t)))))))
348 (let ((alien-funcall `(alien-funcall ,lisp-name ,@(alien-parameters))))
349 `(defun ,lisp-name ,lambda-list
351 (with-alien ((,lisp-name
353 ,(translate-type-spec return-type-spec)
355 :extern ,foreign-name)
357 ,(if return-type-spec
359 ,(translate-from-alien return-type-spec alien-funcall nil)))
360 ,@(alien-deallocators)
361 (values result ,@(alien-values)))
364 ,@(alien-deallocators)
365 (values ,@(alien-values)))))))))
368 (defun mkbinding (name rettype &rest types)
369 (declare (optimize (ext:inhibit-warnings 3)))
371 `(function ,@(mapcar #'translate-type-spec (cons rettype types))))
374 (alien::make-heap-alien-info
375 :type (alien::parse-alien-type ftype)
376 :sap-form (system:foreign-symbol-address name))))
377 (translate-arguments (mapcar #'intern-return-value-translator types))
378 (translate-return-value (intern-return-value-translator rettype))
379 (cleanup-arguments (mapcar #'intern-cleanup-function types)))
381 #'(lambda (&rest args)
382 (map-into args #'funcall translate-arguments args)
385 translate-return-value (apply #'alien:alien-funcall alien args))
386 (mapc #'funcall cleanup-arguments args)))))
389 ;;;; Definitons and translations of fundamental types
391 (deftype long (&optional (min '*) (max '*)) `(integer ,min ,max))
392 (deftype unsigned-long (&optional (min '*) (max '*)) `(integer ,min ,max))
393 (deftype int (&optional (min '*) (max '*)) `(long ,min ,max))
394 (deftype unsigned-int (&optional (min '*) (max '*)) `(unsigned-long ,min ,max))
395 (deftype short (&optional (min '*) (max '*)) `(int ,min ,max))
396 (deftype unsigned-short (&optional (min '*) (max '*)) `(unsigned-int ,min ,max))
397 (deftype signed (&optional (size '*)) `(signed-byte ,size))
398 (deftype unsigned (&optional (size '*)) `(signed-byte ,size))
399 (deftype char () 'base-char)
400 (deftype pointer () 'system-area-pointer)
401 (deftype boolean (&optional (size '*))
402 (declare (ignore size))
404 (deftype invalid () nil)
406 (defun atomic-type-p (type-spec)
408 (eq type-spec 'pointer)
409 (not (eq (translate-type-spec type-spec) 'system-area-pointer))))
412 (deftype-method cleanup-alien t (type-spec sap &optional weak-ref)
413 (declare (ignore type-spec sap weak-ref))
417 (deftype-method translate-to-alien integer (type-spec number &optional weak-ref)
418 (declare (ignore type-spec weak-ref))
421 (deftype-method translate-from-alien integer (type-spec number &optional weak-ref)
422 (declare (ignore type-spec weak-ref))
426 (deftype-method translate-type-spec fixnum (type-spec)
427 (declare (ignore type-spec))
428 (translate-type-spec 'signed))
430 (deftype-method size-of fixnum (type-spec)
431 (declare (ignore type-spec))
434 (deftype-method translate-to-alien fixnum (type-spec number &optional weak-ref)
435 (declare (ignore type-spec weak-ref))
438 (deftype-method translate-from-alien fixnum (type-spec number &optional weak-ref)
439 (declare (ignore type-spec weak-ref))
443 (deftype-method translate-type-spec long (type-spec)
444 (declare (ignore type-spec))
445 `(signed ,(* +bits-per-unit+ +size-of-long+)))
447 (deftype-method size-of long (type-spec)
448 (declare (ignore type-spec))
452 (deftype-method translate-type-spec unsigned-long (type-spec)
453 (declare (ignore type-spec))
454 `(unsigned ,(* +bits-per-unit+ +size-of-long+)))
456 (deftype-method size-of unsigned-long (type-spec)
457 (declare (ignore type-spec))
461 (deftype-method translate-type-spec int (type-spec)
462 (declare (ignore type-spec))
463 `(signed ,(* +bits-per-unit+ +size-of-int+)))
465 (deftype-method size-of int (type-spec)
466 (declare (ignore type-spec))
470 (deftype-method translate-type-spec unsigned-int (type-spec)
471 (declare (ignore type-spec))
472 `(unsigned ,(* +bits-per-unit+ +size-of-int+)))
474 (deftype-method size-of unsigned-int (type-spec)
475 (declare (ignore type-spec))
479 (deftype-method translate-type-spec short (type-spec)
480 (declare (ignore type-spec))
481 `(signed ,(* +bits-per-unit+ +size-of-short+)))
483 (deftype-method size-of short (type-spec)
484 (declare (ignore type-spec))
488 (deftype-method translate-type-spec unsigned-short (type-spec)
489 (declare (ignore type-spec))
490 `(unsigned ,(* +bits-per-unit+ +size-of-short+)))
492 (deftype-method size-of unsigned-short (type-spec)
493 (declare (ignore type-spec))
497 (deftype-method translate-type-spec signed-byte (type-spec)
498 (let ((size (second (mklist (type-expand-to 'signed-byte type-spec)))))
501 ((member size '(nil *)) (* +bits-per-unit+ +size-of-int+))
504 (deftype-method size-of signed-byte (type-spec)
505 (let ((size (second (mklist (type-expand-to 'signed-byte type-spec)))))
507 ((member size '(nil *)) +size-of-int+)
508 (t (/ size +bits-per-unit+)))))
510 (deftype-method translate-to-alien signed-byte (type-spec number &optional weak-ref)
511 (declare (ignore type-spec weak-ref))
514 (deftype-method translate-from-alien signed-byte
515 (type-spec number &optional weak-ref)
516 (declare (ignore type-spec weak-ref))
520 (deftype-method translate-type-spec unsigned-byte (type-spec)
521 (let ((size (second (mklist (type-expand-to 'unsigned-byte type-spec)))))
524 ((member size '(nil *)) (* +bits-per-unit+ +size-of-int+))
527 (deftype-method size-of unsigned-byte (type-spec)
528 (let ((size (second (mklist (type-expand-to 'unsigned-byte type-spec)))))
530 ((member size '(nil *)) +size-of-int+)
531 (t (/ size +bits-per-unit+)))))
533 (deftype-method translate-to-alien unsigned-byte (type-spec number &optional weak-ref)
534 (declare (ignore type-spec weak-ref))
537 (deftype-method translate-from-alien unsigned-byte
538 (type-spec number &optional weak-ref)
539 (declare (ignore type-spec weak-ref))
543 (deftype-method translate-type-spec single-float (type-spec)
544 (declare (ignore type-spec))
547 (deftype-method size-of single-float (type-spec)
548 (declare (ignore type-spec))
551 (deftype-method translate-to-alien single-float (type-spec number &optional weak-ref)
552 (declare (ignore type-spec weak-ref))
555 (deftype-method translate-from-alien single-float
556 (type-spec number &optional weak-ref)
557 (declare (ignore type-spec weak-ref))
561 (deftype-method translate-type-spec double-float (type-spec)
562 (declare (ignore type-spec))
565 (deftype-method size-of double-float (type-spec)
566 (declare (ignore type-spec))
569 (deftype-method translate-to-alien double-float (type-spec number &optional weak-ref)
570 (declare (ignore type-spec weak-ref))
573 (deftype-method translate-from-alien double-float
574 (type-spec number &optional weak-ref)
575 (declare (ignore type-spec weak-ref))
579 (deftype-method translate-type-spec base-char (type-spec)
580 (declare (ignore type-spec))
581 `(unsigned ,+bits-per-unit+))
583 (deftype-method size-of base-char (type-spec)
584 (declare (ignore type-spec))
587 (deftype-method translate-to-alien base-char (type-spec char &optional weak-ref)
588 (declare (ignore type-spec weak-ref))
591 (deftype-method translate-from-alien base-char (type-spec code &optional weak-ref)
592 (declare (ignore type-spec weak-ref))
596 (deftype-method translate-type-spec string (type-spec)
597 (declare (ignore type-spec))
598 'system-area-pointer)
600 (deftype-method size-of string (type-spec)
601 (declare (ignore type-spec))
604 (deftype-method translate-to-alien string (type-spec string &optional weak-ref)
605 (declare (ignore type-spec weak-ref))
606 `(let ((string ,string))
607 ;; Always copy strings to prevent seg fault due to GC
609 (make-pointer (1+ (kernel:get-lisp-obj-address string)))
610 (1+ (length string)))))
612 (deftype-method translate-from-alien string
613 (type-spec c-string &optional weak-ref)
614 (declare (ignore type-spec))
615 `(let ((c-string ,c-string))
616 (unless (null-pointer-p c-string)
618 (c-call::%naturalize-c-string c-string)
619 ;,(unless weak-ref `(deallocate-memory c-string))
622 (deftype-method cleanup-alien string (type-spec c-string &optional weak-ref)
623 (declare (ignore type-spec))
625 (unreference-alien type-spec c-string)))
627 (deftype-method unreference-alien string (type-spec c-string)
628 (declare (ignore type-spec))
629 `(let ((c-string ,c-string))
630 (unless (null-pointer-p c-string)
631 (deallocate-memory c-string))))
634 (deftype-method translate-type-spec boolean (type-spec)
636 (cons 'unsigned (cdr (mklist (type-expand-to 'boolean type-spec))))))
638 (deftype-method size-of boolean (type-spec)
640 (cons 'unsigned (cdr (mklist (type-expand-to 'boolean type-spec))))))
642 (deftype-method translate-to-alien boolean (type-spec boolean &optional weak-ref)
643 (declare (ignore type-spec weak-ref))
646 (deftype-method translate-from-alien boolean (type-spec int &optional weak-ref)
647 (declare (ignore type-spec weak-ref))
651 (deftype-method translate-type-spec or (union-type)
652 (let* ((member-types (cdr (type-expand-to 'or union-type)))
653 (alien-type (translate-type-spec (first member-types))))
654 (dolist (type (cdr member-types))
655 (unless (eq alien-type (translate-type-spec type))
656 (error "No common alien type specifier for union type: ~A" union-type)))
659 (deftype-method size-of or (union-type)
660 (size-of (first (cdr (type-expand-to 'or union-type)))))
662 (deftype-method translate-to-alien or (union-type-spec expr &optional weak-ref)
663 (destructuring-bind (name &rest type-specs)
664 (type-expand-to 'or union-type-spec)
665 (declare (ignore name))
666 `(let ((value ,expr))
670 #'(lambda (type-spec)
671 (list type-spec (translate-to-alien type-spec 'value weak-ref)))
675 (deftype-method translate-type-spec system-area-pointer (type-spec)
676 (declare (ignore type-spec))
677 'system-area-pointer)
679 (deftype-method size-of system-area-pointer (type-spec)
680 (declare (ignore type-spec))
683 (deftype-method translate-to-alien system-area-pointer (type-spec sap &optional weak-ref)
684 (declare (ignore type-spec weak-ref))
687 (deftype-method translate-from-alien system-area-pointer
688 (type-spec sap &optional weak-ref)
689 (declare (ignore type-spec weak-ref))
693 (deftype-method translate-type-spec null (type-spec)
694 (declare (ignore type-spec))
695 'system-area-pointer)
697 (deftype-method translate-to-alien null (type-spec expr &optional weak-ref)
698 (declare (ignore type-spec expr weak-ref))
702 (deftype-method translate-type-spec nil (type-spec)
703 (declare (ignore type-spec))
706 (deftype-method translate-from-alien nil (type-spec expr &optional weak-ref)
707 (declare (ignore type-spec weak-ref))