Removed "possibly unknown" comment.
[trivial-gtk.git] / gir-loader.lisp
blob03c559c7cec01ef1d60be8ebf65ebc427b8c8f30
1 ;; Copyright (c) 2008 John Connors (johnc@yagc.ndo.remove.this.please.co.uk).
2 ;;
3 ;; Permission is hereby granted, free of charge, to any person obtaining a
4 ;; copy of this software and associated documentation files (the "Software"), to
5 ;; deal in the Software without restriction, including without limitation the
6 ;; rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7 ;; sell copies of the Software, and to permit persons to whom the Software is
8 ;; furnished to do so, subject to the following conditions:
9 ;;
10 ;; The above copyright notice and this permission notice shall be included in all
11 ;; copies or substantial portions of the Software.
13 ;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 ;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 ;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 ;; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 ;; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 ;; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19 ;; SOFTWARE.
21 (in-package :gir)
23 (defun lispize (fn-name)
24 "Turn a c-gtk name into a lisp one"
25 (intern (substitute #\- #\_
26 (if (symbolp fn-name)
27 (string fn-name)
28 fn-name))))
30 (defparameter *type-resolver-table* (make-hash-table :test 'equal))
32 (defun add-type (newtype basetype)
33 (setf (gethash newtype *type-resolver-table*) basetype))
35 (defun is-pointer (type)
36 (let ((type-string (string type)))
37 (char= (char type-string (1- (length type-string))) #\*)))
39 (defun dump-resolver-table ()
40 (iterate
41 (for (key val) in-hashtable *type-resolver-table*)
42 (format t "Type ~A~TResolution ~S~%" key val)))
44 (defun is-func-pointer (type)
45 (let* ((type-string (string type))
46 (type-string-len (length type-string)))
47 (or
48 (and (> type-string-len 4)
49 (string= "FUNC" (string-upcase (subseq type-string (- type-string-len 4)))))
50 (and (> type-string-len (length "Function"))
51 (string= "FUNCTION" (string-upcase (subseq type-string (- type-string-len (length "Function")))))))))
53 (defparameter *unknown-types* nil)
55 (defun resolve-type (type)
56 (cond
57 ((null type) ":void")
58 ((is-pointer type) ":pointer")
59 ((is-func-pointer type) ":pointer")
61 (let ((result (gethash type *type-resolver-table* nil)))
62 (if (or (null result) (symbolp result))
63 (if (and result (not (eql result :struct)))
64 (format nil ":~A" result)
65 (let ((typestring (string (intern type))))
66 (unless (eql result :struct)
67 (pushnew typestring *unknown-types* :test 'equal)
68 ;; (format t ";; ~S possibly unknown~%" typestring)
70 (format nil "~A"
71 typestring)))
72 (resolve-type result))))))
74 ;; instead of this, why not use defctype?
76 (add-type "gchar" :char)
77 (add-type "gint8" :int8)
78 (add-type "guint8" :uint8)
79 (add-type "gshort" :short)
80 (add-type "gint16" :int16)
81 (add-type "guint16" :uint16)
82 (add-type "glong" :long)
83 (add-type "gint" :int)
84 (add-type "int" :int)
85 (add-type "guint32" :uint32)
86 (add-type "gint32" :int32)
87 (add-type "gboolean" :int)
88 (add-type "guchar" :unsigned-char)
89 (add-type "gushort" :unsigned-short)
90 (add-type "gulong" :unsigned-long)
91 (add-type "guint" :unsigned-int)
92 (add-type "gfloat" :float)
93 (add-type "float" :float)
94 (add-type "gdouble" :double)
95 (add-type "double" :double)
96 (add-type "gpointer" :pointer)
97 (add-type "none" :void)
98 (add-type "void" :void)
99 (add-type "GType" :uint32) ;; might be a problem on 64-bit platforms
100 (add-type "GQuark" :uint32)
101 (add-type "GdkAtom" :pointer)
102 (add-type "Atom" :pointer)
103 (add-type "GDestroyNotify" :pointer)
104 (add-type "GCallback" :pointer)
106 (defun parse-gir (gir-in)
107 (s-xml:parse-xml gir-in :output-type :xml-struct))
109 (defun parse-gir-file (pathname)
110 (with-open-file (gir-in pathname)
111 (parse-gir gir-in)))
113 (defparameter *repository-names* '(
114 ;; "gtk-x11-2.0.gir"
115 ;; "gdk-x11-2.0.gir"
116 ;; "atk-1.0.gir"
117 ;; "pango-1.0.gir"
118 "cairo.gir"
123 (defparameter *gir-files* nil)
125 (mapcar
126 #'(lambda (gir-name)
127 (setf *gir-files*
128 (cons (parse-gir-file (merge-pathnames (cl-fad::pathname-as-file (concatenate 'string "gir-repository/gir/" gir-name)))) *gir-files*)))
129 *repository-names*)