-
Notifications
You must be signed in to change notification settings - Fork 22
/
vigem.go
319 lines (258 loc) · 8.27 KB
/
vigem.go
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
package stadiacontroller
/*
#include <stdint.h>
typedef struct
{
uint16_t wButtons;
uint8_t bLeftTrigger;
uint8_t bRightTrigger;
int16_t sThumbLX;
int16_t sThumbLY;
int16_t sThumbRX;
int16_t sThumbRY;
} xusb_report;
*/
import "C"
import (
"errors"
"unsafe"
"golang.org/x/sys/windows"
)
const (
VIGEM_ERROR_NONE = 0x20000000
VIGEM_ERROR_BUS_NOT_FOUND = 0xE0000001
VIGEM_ERROR_NO_FREE_SLOT = 0xE0000002
VIGEM_ERROR_INVALID_TARGET = 0xE0000003
VIGEM_ERROR_REMOVAL_FAILED = 0xE0000004
VIGEM_ERROR_ALREADY_CONNECTED = 0xE0000005
VIGEM_ERROR_TARGET_UNINITIALIZED = 0xE0000006
VIGEM_ERROR_TARGET_NOT_PLUGGED_IN = 0xE0000007
VIGEM_ERROR_BUS_VERSION_MISMATCH = 0xE0000008
VIGEM_ERROR_BUS_ACCESS_FAILED = 0xE0000009
VIGEM_ERROR_CALLBACK_ALREADY_REGISTERED = 0xE0000010
VIGEM_ERROR_CALLBACK_NOT_FOUND = 0xE0000011
VIGEM_ERROR_BUS_ALREADY_CONNECTED = 0xE0000012
VIGEM_ERROR_BUS_INVALID_HANDLE = 0xE0000013
VIGEM_ERROR_XUSB_USERINDEX_OUT_OF_RANGE = 0xE0000014
VIGEM_ERROR_MAX = VIGEM_ERROR_XUSB_USERINDEX_OUT_OF_RANGE + 1
)
var (
client = windows.NewLazyDLL("ViGEmClient.dll")
procAlloc = client.NewProc("vigem_alloc")
procFree = client.NewProc("vigem_free")
procConnect = client.NewProc("vigem_connect")
procDisconnect = client.NewProc("vigem_disconnect")
procTargetAdd = client.NewProc("vigem_target_add")
procTargetFree = client.NewProc("vigem_target_free")
procTargetRemove = client.NewProc("vigem_target_remove")
procTargetX360Alloc = client.NewProc("vigem_target_x360_alloc")
procTargetX360RegisterNotification = client.NewProc("vigem_target_x360_register_notification")
procTargetX360UnregisterNotification = client.NewProc("vigem_target_x360_unregister_notification")
procTargetX360Update = client.NewProc("vigem_target_x360_update")
)
type VigemError struct {
code uint
}
func NewVigemError(rawCode uintptr) *VigemError {
code := uint(rawCode)
if code == VIGEM_ERROR_NONE {
return nil
}
return &VigemError{code}
}
func (err *VigemError) Error() string {
switch err.code {
case VIGEM_ERROR_BUS_NOT_FOUND:
return "bus not found"
case VIGEM_ERROR_NO_FREE_SLOT:
return "no free slot"
case VIGEM_ERROR_INVALID_TARGET:
return "invalid target"
case VIGEM_ERROR_REMOVAL_FAILED:
return "removal failed"
case VIGEM_ERROR_ALREADY_CONNECTED:
return "already connected"
case VIGEM_ERROR_TARGET_UNINITIALIZED:
return "target uninitialized"
case VIGEM_ERROR_TARGET_NOT_PLUGGED_IN:
return "target not plugged in"
case VIGEM_ERROR_BUS_VERSION_MISMATCH:
return "bus version mismatch"
case VIGEM_ERROR_BUS_ACCESS_FAILED:
return "bus access failed"
case VIGEM_ERROR_CALLBACK_ALREADY_REGISTERED:
return "callback already registered"
case VIGEM_ERROR_CALLBACK_NOT_FOUND:
return "callback not found"
case VIGEM_ERROR_BUS_ALREADY_CONNECTED:
return "bus already connected"
case VIGEM_ERROR_BUS_INVALID_HANDLE:
return "bus invalid handle"
case VIGEM_ERROR_XUSB_USERINDEX_OUT_OF_RANGE:
return "xusb userindex out of range"
default:
return "invalid code returned by ViGEm"
}
}
type Emulator struct {
handle uintptr
onVibration func(vibration Vibration)
}
type Vibration struct {
LargeMotor byte
SmallMotor byte
}
func NewEmulator(onVibration func(vibration Vibration)) (*Emulator, error) {
handle, _, err := procAlloc.Call()
if !errors.Is(err, windows.ERROR_SUCCESS) {
return nil, err
}
libErr, _, err := procConnect.Call(handle)
if !errors.Is(err, windows.ERROR_SUCCESS) {
return nil, err
}
if err := NewVigemError(libErr); err != nil {
return nil, err
}
return &Emulator{handle, onVibration}, nil
}
func (e *Emulator) Close() error {
procDisconnect.Call(e.handle)
_, _, err := procFree.Call(e.handle)
return err
}
func (e *Emulator) CreateXbox360Controller() (*Xbox360Controller, error) {
handle, _, err := procTargetX360Alloc.Call()
if !errors.Is(err, windows.ERROR_SUCCESS) {
return nil, err
}
notificationHandler := func(client, target uintptr, largeMotor, smallMotor, ledNumber byte) uintptr {
e.onVibration(Vibration{largeMotor, smallMotor})
return 0
}
callback := windows.NewCallback(notificationHandler)
return &Xbox360Controller{e, handle, false, callback}, nil
}
type x360NotificationHandler func(client, target uintptr, largeMotor, smallMotor, ledNumber byte) uintptr
type Xbox360Controller struct {
emulator *Emulator
handle uintptr
connected bool
notificationHandler uintptr
}
func (c *Xbox360Controller) Close() error {
_, _, err := procTargetFree.Call(c.handle)
return err
}
func (c *Xbox360Controller) Connect() error {
libErr, _, err := procTargetAdd.Call(c.emulator.handle, c.handle)
if !errors.Is(err, windows.ERROR_SUCCESS) {
return err
}
if err := NewVigemError(libErr); err != nil {
return err
}
libErr, _, err = procTargetX360RegisterNotification.Call(c.emulator.handle, c.handle, c.notificationHandler)
if !errors.Is(err, windows.ERROR_SUCCESS) {
return err
}
if err := NewVigemError(libErr); err != nil {
return err
}
c.connected = true
return nil
}
func (c *Xbox360Controller) Disconnect() error {
libErr, _, err := procTargetX360UnregisterNotification.Call(c.handle)
if !errors.Is(err, windows.ERROR_SUCCESS) {
return err
}
if err := NewVigemError(libErr); err != nil {
return err
}
libErr, _, err = procTargetRemove.Call(c.emulator.handle, c.handle)
if !errors.Is(err, windows.ERROR_SUCCESS) {
return err
}
if err := NewVigemError(libErr); err != nil {
return err
}
c.connected = false
return nil
}
func (c *Xbox360Controller) Send(report *Xbox360ControllerReport) error {
libErr, _, err := procTargetX360Update.Call(c.emulator.handle, c.handle, uintptr(unsafe.Pointer(&report.native)))
if !errors.Is(err, windows.ERROR_SUCCESS) {
return err
}
if err := NewVigemError(libErr); err != nil {
return err
}
return nil
}
type Xbox360ControllerReport struct {
native C.xusb_report
Capture bool
Assistant bool
}
// Bits that correspond to the Xbox 360 controller buttons.
const (
Xbox360ControllerButtonUp = 0
Xbox360ControllerButtonDown = 1
Xbox360ControllerButtonLeft = 2
Xbox360ControllerButtonRight = 3
Xbox360ControllerButtonStart = 4
Xbox360ControllerButtonBack = 5
Xbox360ControllerButtonLeftThumb = 6
Xbox360ControllerButtonRightThumb = 7
Xbox360ControllerButtonLeftShoulder = 8
Xbox360ControllerButtonRightShoulder = 9
Xbox360ControllerButtonGuide = 10
Xbox360ControllerButtonA = 12
Xbox360ControllerButtonB = 13
Xbox360ControllerButtonX = 14
Xbox360ControllerButtonY = 15
)
func NewXbox360ControllerReport() Xbox360ControllerReport {
return Xbox360ControllerReport{}
}
func (r *Xbox360ControllerReport) GetButtons() uint16 {
return uint16(r.native.wButtons)
}
func (r *Xbox360ControllerReport) SetButtons(buttons uint16) {
r.native.wButtons = C.uint16_t(buttons)
}
func (r *Xbox360ControllerReport) MaybeSetButton(shiftBy int, isSet bool) {
if isSet {
r.SetButton(shiftBy)
}
}
func (r *Xbox360ControllerReport) SetButton(shiftBy int) {
r.native.wButtons |= 1 << shiftBy
}
func (r *Xbox360ControllerReport) GetLeftTrigger() byte {
return byte(r.native.bLeftTrigger)
}
func (r *Xbox360ControllerReport) SetLeftTrigger(value byte) {
r.native.bLeftTrigger = C.uint8_t(value)
}
func (r *Xbox360ControllerReport) GetRightTrigger() byte {
return byte(r.native.bRightTrigger)
}
func (r *Xbox360ControllerReport) SetRightTrigger(value byte) {
r.native.bRightTrigger = C.uint8_t(value)
}
func (r *Xbox360ControllerReport) GetLeftThumb() (x, y int16) {
return int16(r.native.sThumbLX), int16(r.native.sThumbLY)
}
func (r *Xbox360ControllerReport) SetLeftThumb(x, y int16) {
r.native.sThumbLX = C.int16_t(x)
r.native.sThumbLY = C.int16_t(y)
}
func (r *Xbox360ControllerReport) GetRightThumb() (x, y int16) {
return int16(r.native.sThumbRX), int16(r.native.sThumbRY)
}
func (r *Xbox360ControllerReport) SetRightThumb(x, y int16) {
r.native.sThumbRX = C.int16_t(x)
r.native.sThumbRY = C.int16_t(y)
}