-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathffi.lisp
322 lines (275 loc) · 9.69 KB
/
ffi.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
;;; -*- show-trailing-whitespace: t; indent-tabs-mode: nil -*-
;;; Copyright (c) 2009 David Lichteblau. All rights reserved.
;;; Redistribution and use in source and binary forms, with or without
;;; modification, are permitted provided that the following conditions
;;; are met:
;;;
;;; * Redistributions of source code must retain the above copyright
;;; notice, this list of conditions and the following disclaimer.
;;;
;;; * Redistributions in binary form must reproduce the above
;;; copyright notice, this list of conditions and the following
;;; disclaimer in the documentation and/or other materials
;;; provided with the distribution.
;;;
;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED
;;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
;;; ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
;;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
;;; GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
(in-package :qt)
(defvar *loaded* nil)
(defvar *library-loaded-p* nil)
(defun load-libcommonqt ()
;; Workaround for Qt+SBCL SIGCHLD handler conflict.
;; In its SIGCHLD handler Qt invokes the old handler with
;; signal argument only, so in case when SA_SIGINFO is used
;; it receives bad values as its siginfo and context
;; arguments. In case of SBCL this causes memory fault
;; e.g. when using SB-EXT:RUN-PROGRAM. Here we disable
;; the handler that causes that fault (SB-EXT:RUN-PROGRAM
;; still works after that). Thanks to nyef on #lisp.
;; See also: src/corelib/io/qprocess_unix.cpp in Qt
#+sbcl
(sb-sys:enable-interrupt sb-unix:sigchld :default)
(cffi:load-foreign-library
#-(or windows mswindows win32)
(namestring (merge-pathnames "libcommonqt.so"
(asdf::component-relative-pathname
(asdf:find-system :qt))))
#+(or windows mswindows win32)
(namestring (merge-pathnames "debug/commonqt.dll"
(asdf::component-relative-pathname
(asdf:find-system :qt)))))
(setf *library-loaded-p* t))
#-(or ccl
(and sbcl
;; On SBCL/win32, :LINKAGE-TABLE is on *FEATURES*, but that's
;; a lie, it can't actually process FFI definitions before
;; the library has been loaded.
(and linkage-table (not windows))))
(load-libcommonqt)
(defmacro defcfun (name ret &rest args)
`(cffi:defcfun (,name ,(intern (string-upcase name) :qt)) ,ret ,@args))
(cffi:defcvar ("qt_Smoke" qt_Smoke) :pointer)
(cffi:defcvar ("qtwebkit_Smoke" qtwebkit_Smoke) :pointer)
(defcfun "sw_smoke" :void
(smoke :pointer)
(data :pointer)
(deletion-callback :pointer)
(method-callback :pointer)
(child-callback :pointer))
(defcfun "sw_windows_version" :int)
(defcfun "sw_make_qbytearray" :pointer
(str :string))
(defcfun "sw_delete_qbytearray" :void
(str :string))
(defcfun "sw_make_qstring" :pointer
(str :string))
(defcfun "sw_delete_qstring" :void
(qstring :pointer))
(defcfun "sw_qstringlist_new" :pointer)
(defcfun "sw_qstringlist_delete" :void
(qstringlist :pointer))
(defcfun "sw_qstringlist_append" :void
(qstringlist :pointer)
(str :string))
(defcfun "sw_qstringlist_size" :int
(qstringlist :pointer))
(defcfun "sw_qstringlist_at" :pointer
(qstringlist :pointer)
(index :int))
(defcfun "sw_make_metaobject" :pointer
(parent :pointer)
(str :pointer)
(data :pointer))
(defcfun "sw_delete" :void
(stack :pointer))
(defcfun "sw_qstring_to_utf8" :pointer
(obj :pointer))
(defcfun "sw_qstring_to_utf16" :pointer
(obj :pointer))
(declaim (inline convert-qstring-data))
(defun convert-qstring-data (data)
(declare (optimize speed))
#+nil
;; fixme: babel doesn't get endianness right in utf-16.
(cffi:foreign-string-to-lisp (sw_qstring_to_utf16 raw-ptr)
:encoding :utf-16)
;; handcode instead:
(let* ((nbytes (cffi::foreign-string-length data :encoding :utf-16))
(res (make-string (truncate nbytes 2))))
(iter (for i from 0 by 2 below nbytes)
(for j from 0)
(let ((code (cffi:mem-ref data :unsigned-short i)))
(until (zerop code))
(setf (char res j) (code-char code))))
res))
(defun qstring-pointer-to-lisp (raw-ptr)
(declare (optimize speed))
(convert-qstring-data (sw_qstring_to_utf16 raw-ptr)))
(defcfun "sw_find_name" :short
(smoke :pointer)
(str :string))
(defcfun "sw_find_class" :void
(name :string)
(smoke** :pointer)
(index** :pointer))
(defcfun "sw_id_method" :short
(smoke :pointer)
(class :short)
(name :short))
(defcfun "sw_id_type" :short
(smoke :pointer)
(name :string))
(defcfun "sw_id_class" :short
(smoke :pointer)
(name :string)
(external :char)) ;bool
(defcfun "sw_id_instance_class" :short
(instance :pointer)
(smoke** :pointer)
(index** :pointer))
(defun qlist-function-name (type-name name)
(alexandria:symbolicate "SW_QLIST_" (string-upcase type-name) "_" (string-upcase name)))
(macrolet ((define-qlist-marshaller-funcs (type-name)
(flet ((func-name (name)
(concatenate 'string "sw_qlist_" (string-downcase type-name)
"_" (string-downcase name))))
`(progn
(defcfun ,(func-name "new") :pointer)
(defcfun ,(func-name "delete") :void (qlist :pointer))
(defcfun ,(func-name "size") :int (qlist :pointer))
(defcfun ,(func-name "at") :pointer (qlist :pointer) (index :int))
(defcfun ,(func-name "append") :void (qlist :pointer) (var :pointer))))))
(define-qlist-marshaller-funcs void)
(define-qlist-marshaller-funcs int)
(define-qlist-marshaller-funcs qvariant)
(define-qlist-marshaller-funcs qbytearray)
(define-qlist-marshaller-funcs qmodelindex)
(define-qlist-marshaller-funcs qkeysequence))
(cffi:defcstruct |struct SmokeData|
(name :string)
(classes :pointer)
(nclasses :short)
(methods :pointer)
(nmethods :short)
(methodmaps :pointer)
(nmethodmaps :short)
(methodnames :pointer)
(nmethodnames :short)
(types :pointer)
(ntypes :short)
(inheritanceList :pointer)
(argumentList :pointer)
(ambiguousMethodList :pointer)
(castFn :pointer)
(thin :pointer)
(fat :pointer))
(macrolet ((% (fun slot)
`(progn
(declaim (inline ,fun))
(defun ,fun (data)
(cffi:foreign-slot-value data '|struct SmokeData| ',slot)))))
(% data-name name)
(% data-classes classes)
(% data-nclasses nclasses)
(% data-methods methods)
(% data-nmethods nmethods)
(% data-methodmaps methodmaps)
(% data-nmethodmaps nmethodmaps)
(% data-methodnames methodnames)
(% data-nmethodnames nmethodnames)
(% data-types types)
(% data-ntypes ntypes)
(% data-inheritanceList inheritanceList)
(% data-argumentList argumentList)
(% data-ambiguousMethodList ambiguousMethodList)
(% data-castFn castFn)
(% data-thin thin)
(% data-fat fat))
(cffi:defcunion |union StackItem|
(ptr :pointer)
(bool :char)
(char :char)
(uchar :unsigned-char)
(short :short)
(ushort :unsigned-short)
(int :int)
(uint :uint)
(long :long)
(ulong :ulong)
(float :float)
(double :double)
(enum :int)
(class :pointer))
(cffi:defcstruct |struct Method|
(classid :short)
(name :short)
(args :short)
(numargs :unsigned-char)
(flags :unsigned-short)
(ret :short)
(methodForClassFun :short))
(cffi:defcstruct |struct MethodMap|
(classid :short)
(name :short)
(methodid :short))
(cffi:defcstruct |struct Class|
(className :string)
(external :char)
(parents :short)
(classfn :pointer) ;; void (*classfn)(Index method, void *obj, Stack args)
(enumfn :pointer)
(flags :short)
(size :unsigned-int))
(cffi:defcstruct |struct Type|
(name :string)
(classid :short)
(flags :short))
(defvar *callbacks* (make-hash-table :test 'equal))
(defmacro defcallback (name ret (&rest args) &body body)
`(cffi:defcallback (,name #+commonqt-use-stdcall :calling-convention
#+commonqt-use-stdcall :stdcall)
,ret ,args ,@body))
(defcallback deletion-callback
:void
((smoke :pointer)
(obj :pointer))
(declare (ignore smoke))
;; Just dispatch to an ordinary function for debugging purposes.
;; Redefinition of a callback wouldn't affect the existing C++ code,
;; redefinition of the function does.
(%deletion-callback obj))
(defcallback method-invocation-callback
:char
((smoke :pointer)
(method :short)
(obj :pointer)
(args :pointer)
(abstractp :char))
;; Just dispatch to an ordinary function for debugging purposes.
;; Redefinition of a callback wouldn't affect the existing C++ code,
;; redefinition of the function does.
(%method-invocation-callback smoke method obj args abstractp))
(defcallback child-callback
:void
((smoke :pointer)
(added :char) ;bool
(obj :pointer))
(declare (ignore smoke))
;; Just dispatch to an ordinary function for debugging purposes.
;; Redefinition of a callback wouldn't affect the existing C++ code,
;; redefinition of the function does.
(%child-callback added obj))
(defvar *ptr-callback*)
(defcallback ptr-callback
:void
((obj :pointer))
(funcall *ptr-callback* obj))