-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjoystick.go
More file actions
247 lines (210 loc) · 6.84 KB
/
joystick.go
File metadata and controls
247 lines (210 loc) · 6.84 KB
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
package darwin
import (
"fmt"
"os"
"sync"
"unsafe"
"github.com/ebitengine/purego"
)
type joystick struct {
device IOHIDDeviceRef
name string
axes []ioHIDElement
buttons []ioHIDElement
hats []ioHIDElement
}
type ioHIDElement struct {
element uintptr
logicalMin int64
logicalMax int64
}
var (
joystickMtx sync.Mutex
joystickManager IOHIDManagerRef
joysticks []joystick
)
const (
kIOHIDPageGenericDesktop = 0x01
kIOHIDPageButton = 0x09
kIOHIDUsageJoystick = 0x04
kIOHIDUsageGamepad = 0x05
kIOHIDUsageMultiAxisController = 0x08
kIOHIDUsageHatSwitch = 0x39
kIOHIDElementTypeAxis = 2
kIOHIDElementTypeButton = 3
kIOHIDElementTypeHatswitch = 4
kCFStringEncodingUTF8 = 0x08000100
)
func SetupJoysticks() {
pool := NewAutoreleasePool()
defer pool.Drain()
joystickMtx.Lock()
defer joystickMtx.Unlock()
if joystickManager != 0 {
return
}
mgr, _, _ := purego.SyscallN(_IOHIDManagerCreate, 0, uintptr(0))
if mgr == 0 {
fmt.Fprintf(os.Stderr, "darwin: IOHIDManagerCreate failed\n")
return
}
joystickManager = IOHIDManagerRef(mgr)
match := createDeviceMatchingArray()
purego.SyscallN(_IOHIDManagerSetDeviceMatchingMultiple, uintptr(joystickManager), match)
purego.SyscallN(_IOHIDManagerRegisterDeviceMatchingCallback, uintptr(joystickManager), purego.NewCallback(deviceMatchingCallback), 0)
purego.SyscallN(_IOHIDManagerRegisterDeviceRemovalCallback, uintptr(joystickManager), purego.NewCallback(deviceRemovalCallback), 0)
runLoop := Objc_sendMsg[uintptr](Class_NSRunLoop, Sel_mainRunLoop)
purego.SyscallN(_IOHIDManagerScheduleWithRunLoop, uintptr(joystickManager), runLoop, NSDefaultRunLoopMode)
purego.SyscallN(_IOHIDManagerOpen, uintptr(joystickManager), uintptr(0))
}
func createDeviceMatchingArray() uintptr {
usagePageKey := NSString_WithUTF8String("UsagePage")
usageKey := NSString_WithUTF8String("Usage")
newNumber := func(v int) uintptr {
return Objc_sendMsg[uintptr](Class_NSNumber, Sel_numberWithInt, v)
}
newDict := func(usage int) uintptr {
keys := [2]uintptr{uintptr(usagePageKey.Ptr), uintptr(usageKey.Ptr)}
values := [2]uintptr{newNumber(kIOHIDPageGenericDesktop), newNumber(usage)}
return Objc_sendMsg[uintptr](Class_NSDictionary, Sel_dictionaryWithObjectsForKeysCount, &values[0], &keys[0], len(keys))
}
joystickDict := newDict(kIOHIDUsageJoystick)
gamepadDict := newDict(kIOHIDUsageGamepad)
multiAxisDict := newDict(kIOHIDUsageMultiAxisController)
return Objc_sendMsg[uintptr](Class_NSArray, Sel_arrayWithObjects, joystickDict, gamepadDict, multiAxisDict, 0)
}
func deviceMatchingCallback(ctx, result, sender, device uintptr) {
joystickMtx.Lock()
defer joystickMtx.Unlock()
addJoystick(IOHIDDeviceRef(device))
}
func deviceRemovalCallback(ctx, result, sender, device uintptr) {
joystickMtx.Lock()
defer joystickMtx.Unlock()
removeJoystick(IOHIDDeviceRef(device))
}
func addJoystick(devRef IOHIDDeviceRef) {
var j joystick
j.device = devRef
j.name = "Unknown Joystick"
productName, _, _ := purego.SyscallN(_IOHIDDeviceGetProperty, uintptr(devRef), uintptr(NSString_WithUTF8String("Product").Ptr))
if productName != 0 {
j.name = GoString(Objc_sendMsg[uintptr](productName, Sel_UTF8String))
}
elements, _, _ := purego.SyscallN(_IOHIDDeviceCopyMatchingElements, uintptr(devRef), 0, 0)
if elements == 0 {
return
}
defer purego.SyscallN(_CFRelease, elements)
count, _, _ := purego.SyscallN(_CFArrayGetCount, elements)
for i := uintptr(0); i < count; i++ {
elem, _, _ := purego.SyscallN(_CFArrayGetValueAtIndex, elements, i)
if elem == 0 {
continue
}
usagePage, _, _ := purego.SyscallN(_IOHIDElementGetUsagePage, elem)
usage, _, _ := purego.SyscallN(_IOHIDElementGetUsage, elem)
elemType, _, _ := purego.SyscallN(_IOHIDElementGetType, elem)
hidElem := ioHIDElement{element: elem}
min, _, _ := purego.SyscallN(_IOHIDElementGetLogicalMin, elem)
hidElem.logicalMin = int64(min)
max, _, _ := purego.SyscallN(_IOHIDElementGetLogicalMax, elem)
hidElem.logicalMax = int64(max)
if usagePage == kIOHIDPageGenericDesktop {
if elemType == kIOHIDElementTypeAxis {
j.axes = append(j.axes, hidElem)
} else if usage == kIOHIDUsageHatSwitch {
j.hats = append(j.hats, hidElem)
}
} else if usagePage == kIOHIDPageButton {
j.buttons = append(j.buttons, hidElem)
}
}
joysticks = append(joysticks, j)
fmt.Fprintf(os.Stderr, "Joystick connected: %s\n", j.name)
}
func removeJoystick(devRef IOHIDDeviceRef) {
for i, j := range joysticks {
if j.device == devRef {
name := j.name
joysticks = append(joysticks[:i], joysticks[i+1:]...)
fmt.Fprintf(os.Stderr, "Joystick disconnected: %s\n", name)
return
}
}
}
func IsJoystickPresent(joy int) bool {
joystickMtx.Lock()
defer joystickMtx.Unlock()
return joy >= 0 && joy < len(joysticks)
}
func GetJoystickName(joy int) string {
joystickMtx.Lock()
defer joystickMtx.Unlock()
if joy < 0 || joy >= len(joysticks) {
return ""
}
return joysticks[joy].name
}
func GetJoystickAxes(joy int) ([]float32, error) {
joystickMtx.Lock()
defer joystickMtx.Unlock()
if joy < 0 || joy >= len(joysticks) {
return nil, fmt.Errorf("joystick index %d is out of bounds", joy)
}
j := joysticks[joy]
axes := make([]float32, len(j.axes))
for i, elem := range j.axes {
var hidValue uintptr
purego.SyscallN(_IOHIDDeviceGetValue, uintptr(j.device), elem.element, uintptr(unsafe.Pointer(&hidValue)))
if hidValue == 0 {
continue
}
val, _, _ := purego.SyscallN(_IOHIDValueGetIntegerValue, hidValue)
if elem.logicalMax != elem.logicalMin {
normalized := 2.0*float32(int64(val)-elem.logicalMin)/float32(elem.logicalMax-elem.logicalMin) - 1.0
axes[i] = normalized
}
}
return axes, nil
}
func GetJoystickButtons(joy int) ([]byte, error) {
joystickMtx.Lock()
defer joystickMtx.Unlock()
if joy < 0 || joy >= len(joysticks) {
return nil, fmt.Errorf("joystick index %d is out of bounds", joy)
}
j := joysticks[joy]
buttons := make([]byte, len(j.buttons))
for i, elem := range j.buttons {
var hidValue uintptr
purego.SyscallN(_IOHIDDeviceGetValue, uintptr(j.device), elem.element, uintptr(unsafe.Pointer(&hidValue)))
if hidValue == 0 {
continue
}
val, _, _ := purego.SyscallN(_IOHIDValueGetIntegerValue, hidValue)
if val != 0 {
buttons[i] = 1
}
}
return buttons, nil
}
func GetJoystickHats(joy int) ([]byte, error) {
joystickMtx.Lock()
defer joystickMtx.Unlock()
if joy < 0 || joy >= len(joysticks) {
return nil, fmt.Errorf("joystick index %d is out of bounds", joy)
}
j := joysticks[joy]
hats := make([]byte, len(j.hats))
for i, elem := range j.hats {
var hidValue uintptr
purego.SyscallN(_IOHIDDeviceGetValue, uintptr(j.device), elem.element, uintptr(unsafe.Pointer(&hidValue)))
if hidValue == 0 {
continue
}
val, _, _ := purego.SyscallN(_IOHIDValueGetIntegerValue, hidValue)
hats[i] = byte(val)
}
return hats, nil
}