-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathc-support.lisp
185 lines (168 loc) · 7.42 KB
/
c-support.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
(in-package #:org.shirakumo.fraf.steamworks)
(eval-when (:compile-toplevel :load-toplevel :execute)
(defvar *this* #.(or *compile-file-pathname* *load-pathname*
(error "COMPILE-FILE or LOAD this file.")))
(defvar *static*
(make-pathname :name NIL :type NIL
:defaults (merge-pathnames "static/" *this*))))
(defparameter steam::*debug-calls* NIL)
(defvar *low-level-present* NIL)
(defvar steam::*callback-id-map* (make-hash-table :test 'eq))
(defvar steam::*function-callresult-map* (make-hash-table :test 'eq))
(defun callback-type-id (callback)
(or (gethash callback steam::*callback-id-map*)
(if *low-level-present*
(error "Not a callback: ~s" callback)
0)))
(define-compiler-macro callback-type-id (&whole whole callback &environment env)
(if (constantp callback env)
`(load-time-value (or (gethash ,callback steam::*callback-id-map*)
(if *low-level-present*
(error "Not a callback: ~s" ,callback)
0)))
whole))
(defun function-callresult (function)
(or (gethash function steam::*function-callresult-map*)
(if *low-level-present*
(error "Not a callresult function: ~s" function)
NIL)))
(define-compiler-macro function-callresult (&whole whole function &environment env)
(if (constantp function env)
`(load-time-value (or (gethash ,function steam::*function-callresult-map*)
(if *low-level-present*
(error "Not a callresult function: ~s" ,function)
NIL)))
whole))
(defun c-slot-value-extractor (struct slotdef)
(destructuring-bind (name type &key count offset) slotdef
(declare (ignore offset))
(cond ((= 1 count)
`(cffi:foreign-slot-value value '(:struct ,struct) ',name))
((eql type :char)
`(cffi:foreign-string-to-lisp
(cffi:foreign-slot-pointer value '(:struct ,struct) ',name)
:count ,count :encoding :utf-8))
(T
`(loop with ptr = (cffi:foreign-slot-pointer value '(:struct ,struct) ',name)
for i from 0 below ,count
collect (cffi:mem-aref ptr ',type i))))))
(defmacro steam::defcfun* ((lisp-name foreign-name &rest options) return-type &rest args)
(flet ((format-arg (arg)
(case (let ((type (second arg)))
(if (listp type) (first type) type))
(:pointer `(if (cffi:null-pointer-p ,(first arg))
"NULL"
(format NIL "0x~8,'0x" (cffi:pointer-address ,(first arg)))))
((:bool :boolean) `(if ,(first arg) 1 0))
(T (first arg)))))
(let* ((arg-names (mapcar #'first args))
(arg-types (mapcar #'second args))
(syms (cffi::make-gensym-list (length args))))
(multiple-value-bind (prelude caller)
(cffi::defcfun-helper-forms
foreign-name lisp-name (cffi::canonicalize-foreign-type return-type)
syms (mapcar #'cffi::canonicalize-foreign-type arg-types) options)
`(progn
,prelude
(defun ,lisp-name ,arg-names
,(when steam::*debug-calls*
`(format *debug-io* ,(format NIL "~~&~a(~{~~a~*~^, ~}) " foreign-name args)
,@(mapcar #'format-arg args)))
(let ((retval ,(cffi::translate-objects syms arg-names arg-types return-type caller)))
,(when (and steam::*debug-calls* (not (eql :void return-type)))
`(format *debug-io* "~a~%" ,(format-arg (list 'retval return-type))))
retval)))))))
(defmacro steam::defcstruct* (name &body slots)
(flet ((name (format &rest args)
(intern (format NIL "~?" format args) '#:org.shirakumo.fraf.steamworks.cffi)))
(let ((name-class (name "~a-TCLASS" name))
(constructor (name "MAKE-~a" name))
(handle (name "_HANDLE"))
(handle-accessor (name "~a-_HANDLE" name)))
`(progn
(cffi:defcstruct (,name :class ,name-class)
,@slots)
(defstruct (,name (:constructor ,constructor (,handle ,@(mapcar #'first slots))))
,handle ,@(mapcar #'first slots))
(defmethod cffi:translate-from-foreign (value (type ,name-class))
(,constructor
value
,@(loop for slot in slots
collect (c-slot-value-extractor name slot))))
(defmethod steam::_handle ((,name ,name))
(,handle-accessor ,name))))))
(cffi:define-foreign-library steam::steamworks
((:and :darwin)
"libsteam_api.dylib"
:search-path #.(merge-pathnames "osx/" *static*))
((:and :linux :x86)
"libsteam_api.so"
:search-path #.(merge-pathnames "linux32/" *static*))
((:and :linux :x86-64)
"libsteam_api.so"
:search-path #.(merge-pathnames "linux64/" *static*))
((:and :windows :x86)
"steam_api.dll"
:search-path #.(merge-pathnames "/" *static*))
((:and :windows :x86-64)
(:or "steam_api64.dll" "steam_api.dll")
:search-path #.(merge-pathnames "win64/" *static*)))
(cffi:define-foreign-library steam::steamworks-shim
((:and :darwin)
"steamworks_shim.dylib"
:search-path #.(merge-pathnames "osx/" *static*))
((:and :linux :x86)
"steamworks_shim.so"
:search-path #.(merge-pathnames "linux32/" *static*))
((:and :linux :x86-64)
"steamworks_shim.so"
:search-path #.(merge-pathnames "linux64/" *static*))
((:and :windows :x86)
"steamworks_shim.dll"
:search-path #.(merge-pathnames "/" *static*))
((:and :windows :x86-64)
"steamworks_shim.dll"
:search-path #.(merge-pathnames "win64/" *static*)))
(cffi:defctype steam::steam-id :unsigned-long)
(defun maybe-load-low-level (&optional file recompile)
(let ((file (or file (make-pathname :name "low-level" :type "lisp" :defaults *this*))))
(when (probe-file file)
(let ((fasl #+asdf (funcall asdf/driver:*output-translation-function* (compile-file-pathname file))
#-asdf (compile-file-pathname file)))
(when (or recompile (not (probe-file fasl)))
(ignore-errors (compile-file file :verbose NIL :print NIL :output-file fasl)))
(if (probe-file fasl)
(load fasl :verbose NIL :print NIL)
(load file :verbose NIL :print NIL)))
(setf *low-level-present* T))))
;; DEFCSTRUCT interns its accessors in *PACKAGE* rather than using the package
;; of either CONC-NAME or the slot name, so we have to switch packages here.
(in-package #:org.shirakumo.fraf.steamworks.cffi)
#+windows
(cffi:defcstruct (vtable :class vtable :conc-name vtable-)
(result-with-info :pointer)
(result :pointer)
(size :pointer))
#-windows
(cffi:defcstruct (vtable :class vtable :conc-name vtable-)
;; void (pointer this, pointer param)
(result :pointer)
;; void (pointer this, pointer param, bool failed, steam-apicall-t api-call)
(result-with-info :pointer)
;; int (pointer this)
(size :pointer))
(cffi:defcstruct (callback :class callback :conc-name callback-)
;; Pointer to vtable instance.
(vtable-ptr :pointer)
;; Should be 2 on a game server, 0 otherwise?
(flags :uint8)
;; Identifier of the type of callback to register.
(id :int)
;; Identifier for the callresult token.
(token :uint64)
;; Self pointer used when calling back.
(this :pointer)
;; Function pointer to call for callresult
(function :pointer)
;; vtable alloc
(vtable (:struct vtable)))