This repository has been archived by the owner on Mar 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
/
gamepad.go
340 lines (279 loc) · 7.67 KB
/
gamepad.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
package uinput
import (
"errors"
"fmt"
"io"
"os"
)
const MaximumAxisValue = 32767
// HatDirection specifies the direction of hat movement
type HatDirection int
const (
HatUp HatDirection = iota + 1
HatDown
HatLeft
HatRight
)
type HatAction int
const (
Press HatAction = iota + 1
Release
)
// Gamepad is a hybrid key / absolute change event output device.
// It used to enable a program to simulate gamepad input events.
type Gamepad interface {
// ButtonPress will cause the button to be pressed and immediately released.
ButtonPress(key int) error
// ButtonDown will send a button-press event to an existing gamepad device.
// The key can be any of the predefined keycodes from keycodes.go.
// Note that the key will be "held down" until "KeyUp" is called.
ButtonDown(key int) error
// ButtonUp will send a button-release event to an existing gamepad device.
// The key can be any of the predefined keycodes from keycodes.go.
ButtonUp(key int) error
// LeftStickMoveX performs a movement of the left stick along the x-axis
LeftStickMoveX(value float32) error
// LeftStickMoveY performs a movement of the left stick along the y-axis
LeftStickMoveY(value float32) error
// RightStickMoveX performs a movement of the right stick along the x-axis
RightStickMoveX(value float32) error
// RightStickMoveY performs a movement of the right stick along the y-axis
RightStickMoveY(value float32) error
// LeftStickMove moves the left stick along the x and y-axis
LeftStickMove(x, y float32) error
// RightStickMove moves the right stick along the x and y-axis
RightStickMove(x, y float32) error
// HatPress will issue a hat-press event in the given direction
HatPress(direction HatDirection) error
// HatRelease will issue a hat-release event in the given direction
HatRelease(direction HatDirection) error
io.Closer
}
type vGamepad struct {
name []byte
deviceFile *os.File
}
// CreateGamepad will create a new gamepad using the given uinput
// device path of the uinput device.
func CreateGamepad(path string, name []byte, vendor uint16, product uint16) (Gamepad, error) { // TODO: Consider moving this to a generic function that works for all devices
err := validateDevicePath(path)
if err != nil {
return nil, err
}
err = validateUinputName(name)
if err != nil {
return nil, err
}
fd, err := createVGamepadDevice(path, name, vendor, product)
if err != nil {
return nil, err
}
return vGamepad{name: name, deviceFile: fd}, nil
}
func (vg vGamepad) ButtonPress(key int) error {
err := vg.ButtonDown(key)
if err != nil {
return err
}
err = vg.ButtonUp(key)
if err != nil {
return err
}
return nil
}
func (vg vGamepad) ButtonDown(key int) error {
return sendBtnEvent(vg.deviceFile, []int{key}, btnStatePressed)
}
func (vg vGamepad) ButtonUp(key int) error {
return sendBtnEvent(vg.deviceFile, []int{key}, btnStateReleased)
}
func (vg vGamepad) LeftStickMoveX(value float32) error {
return vg.sendStickAxisEvent(absX, value)
}
func (vg vGamepad) LeftStickMoveY(value float32) error {
return vg.sendStickAxisEvent(absY, value)
}
func (vg vGamepad) RightStickMoveX(value float32) error {
return vg.sendStickAxisEvent(absRX, value)
}
func (vg vGamepad) RightStickMoveY(value float32) error {
return vg.sendStickAxisEvent(absRY, value)
}
func (vg vGamepad) RightStickMove(x, y float32) error {
values := map[uint16]float32{}
values[absRX] = x
values[absRY] = y
return vg.sendStickEvent(values)
}
func (vg vGamepad) LeftStickMove(x, y float32) error {
values := map[uint16]float32{}
values[absX] = x
values[absY] = y
return vg.sendStickEvent(values)
}
func (vg vGamepad) HatPress(direction HatDirection) error {
return vg.sendHatEvent(direction, Press)
}
func (vg vGamepad) HatRelease(direction HatDirection) error {
return vg.sendHatEvent(direction, Release)
}
func (vg vGamepad) sendStickAxisEvent(absCode uint16, value float32) error {
ev := inputEvent{
Type: evAbs,
Code: absCode,
Value: denormalizeInput(value),
}
buf, err := inputEventToBuffer(ev)
if err != nil {
return fmt.Errorf("writing abs stick event failed: %v", err)
}
_, err = vg.deviceFile.Write(buf)
if err != nil {
return fmt.Errorf("failed to write abs stick event to device file: %v", err)
}
return syncEvents(vg.deviceFile)
}
func (vg vGamepad) sendStickEvent(values map[uint16]float32) error {
for code, value := range values {
ev := inputEvent{
Type: evAbs,
Code: code,
Value: denormalizeInput(value),
}
buf, err := inputEventToBuffer(ev)
if err != nil {
return fmt.Errorf("writing abs stick event failed: %v", err)
}
_, err = vg.deviceFile.Write(buf)
if err != nil {
return fmt.Errorf("failed to write abs stick event to device file: %v", err)
}
}
return syncEvents(vg.deviceFile)
}
func (vg vGamepad) sendHatEvent(direction HatDirection, action HatAction) error {
var event uint16
var value int32
switch direction {
case HatUp:
{
event = absHat0Y
value = -1
}
case HatDown:
{
event = absHat0Y
value = 1
}
case HatLeft:
{
event = absHat0X
value = -1
}
case HatRight:
{
event = absHat0X
value = 1
}
default:
{
return errors.New("failed to parse input direction")
}
}
if action == Release {
value = 0
}
ev := inputEvent{
Type: evAbs,
Code: event,
Value: value,
}
buf, err := inputEventToBuffer(ev)
if err != nil {
return fmt.Errorf("writing abs stick event failed: %v", err)
}
_, err = vg.deviceFile.Write(buf)
if err != nil {
return fmt.Errorf("failed to write abs stick event to device file: %v", err)
}
return syncEvents(vg.deviceFile)
}
func (vg vGamepad) Close() error {
return closeDevice(vg.deviceFile)
}
func createVGamepadDevice(path string, name []byte, vendor uint16, product uint16) (fd *os.File, err error) {
// This array is needed to register the event keys for the gamepad device.
keys := []uint16{
ButtonGamepad,
ButtonSouth,
ButtonEast,
ButtonNorth,
ButtonWest,
ButtonBumperLeft,
ButtonBumperRight,
ButtonTriggerLeft,
ButtonTriggerRight,
ButtonThumbLeft,
ButtonThumbRight,
ButtonSelect,
ButtonStart,
ButtonDpadUp, // * * *
ButtonDpadDown, // * These buttons can be used instead of the hat events.
ButtonDpadLeft, // *
ButtonDpadRight, // * * *
ButtonMode,
}
// absEvents is for the absolute events for the gamepad device.
absEvents := []uint16{
absX,
absY,
absZ,
absRX,
absRY,
absRZ,
absHat0X,
absHat0Y,
}
deviceFile, err := createDeviceFile(path)
if err != nil {
return nil, fmt.Errorf("failed to create virtual gamepad device: %v", err)
}
// register button events
err = registerDevice(deviceFile, uintptr(evKey))
if err != nil {
_ = deviceFile.Close()
return nil, fmt.Errorf("failed to register virtual gamepad device: %v", err)
}
for _, code := range keys {
err = ioctl(deviceFile, uiSetKeyBit, uintptr(code))
if err != nil {
_ = deviceFile.Close()
return nil, fmt.Errorf("failed to register key number %d: %v", code, err)
}
}
// register absolute events
err = registerDevice(deviceFile, uintptr(evAbs))
if err != nil {
_ = deviceFile.Close()
return nil, fmt.Errorf("failed to register absolute event input device: %v", err)
}
for _, event := range absEvents {
err = ioctl(deviceFile, uiSetAbsBit, uintptr(event))
if err != nil {
_ = deviceFile.Close()
return nil, fmt.Errorf("failed to register absolute event %v: %v", event, err)
}
}
return createUsbDevice(deviceFile,
uinputUserDev{
Name: toUinputName(name),
ID: inputID{
Bustype: busUsb,
Vendor: vendor,
Product: product,
Version: 1}})
}
// Takes in a normalized value (-1.0:1.0) and return an event value
func denormalizeInput(value float32) int32 {
return int32(value * MaximumAxisValue)
}