-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
341 lines (294 loc) · 7.74 KB
/
main.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
341
package main
// #cgo CFLAGS: -x objective-c
// #cgo LDFLAGS: -framework Cocoa
// #import <Foundation/Foundation.h>
// #import <CoreGraphics/CoreGraphics.h>
import "C"
import (
"encoding/json"
"fmt"
"log"
"os"
"strconv"
"github.com/veandco/go-sdl2/sdl"
)
type config struct {
Axes map[string][3]int `json:"axes"`
Buttons map[string]int `json:"buttons"`
Triggers map[string][2]int `json:"triggers"`
}
func newConfig() config {
return config{
Axes: make(map[string][3]int),
Buttons: make(map[string]int),
Triggers: make(map[string][2]int),
}
}
func createKey(val int) (C.CGEventRef, C.CGEventRef, error) {
up := C.CGEventCreateKeyboardEvent(nil, C.CGKeyCode(val), false)
if up == nil {
return nil, nil, fmt.Errorf("unable to create key %d", val)
}
down := C.CGEventCreateKeyboardEvent(nil, C.CGKeyCode(val), true)
if down == nil {
return nil, nil, fmt.Errorf("unable to create key %d", val)
}
return up, down, nil
}
/**************************************************
Axis
**************************************************/
type axis struct {
threshold int
negative bool
positive bool
nUp C.CGEventRef
nDown C.CGEventRef
pUp C.CGEventRef
pDown C.CGEventRef
}
func openAxis(threshold int, neg int, pos int) (*axis, error) {
nUp, nDown, err := createKey(neg)
if err != nil {
return nil, err
}
pUp, pDown, err := createKey(pos)
if err != nil {
return nil, err
}
a := &axis{
threshold: threshold,
negative: false,
positive: false,
nUp: nUp,
nDown: nDown,
pUp: pUp,
pDown: pDown,
}
return a, nil
}
func (a *axis) close() {
C.CFRelease(C.CFTypeRef(a.nUp))
C.CFRelease(C.CFTypeRef(a.nDown))
C.CFRelease(C.CFTypeRef(a.pUp))
C.CFRelease(C.CFTypeRef(a.pDown))
}
func (a *axis) handleEvent(val int) {
if val <= -a.threshold && !a.negative {
C.CGEventPost(C.kCGAnnotatedSessionEventTap, a.nDown)
a.negative = true
} else if val > -a.threshold && a.negative {
C.CGEventPost(C.kCGAnnotatedSessionEventTap, a.nUp)
a.negative = false
}
if val >= a.threshold && !a.positive {
C.CGEventPost(C.kCGAnnotatedSessionEventTap, a.pDown)
a.positive = true
} else if val < a.threshold && a.positive {
C.CGEventPost(C.kCGAnnotatedSessionEventTap, a.pUp)
a.positive = false
}
}
/***********************************************
Button
***********************************************/
type button struct {
up C.CGEventRef
down C.CGEventRef
}
func openButton(val int) (*button, error) {
up, down, err := createKey(val)
if err != nil {
return nil, err
}
return &button{up: up, down: down}, nil
}
func (b *button) close() {
C.CFRelease(C.CFTypeRef(b.up))
C.CFRelease(C.CFTypeRef(b.down))
}
func (b *button) handleUp() {
C.CGEventPost(C.kCGAnnotatedSessionEventTap, b.up)
}
func (b *button) handleDown() {
C.CGEventPost(C.kCGAnnotatedSessionEventTap, b.down)
}
/****************************************
Trigger
****************************************/
type trigger struct {
threshold int
active bool
up C.CGEventRef
down C.CGEventRef
}
func openTrigger(threshold int, val int) (*trigger, error) {
up, down, err := createKey(val)
if err != nil {
return nil, err
}
t := &trigger{
threshold: threshold,
active: false,
up: up,
down: down,
}
return t, nil
}
func (t *trigger) handleEvent(val int) {
if val >= t.threshold && !t.active {
C.CGEventPost(C.kCGAnnotatedSessionEventTap, t.down)
t.active = true
} else if val < t.threshold && t.active {
C.CGEventPost(C.kCGAnnotatedSessionEventTap, t.up)
t.active = false
}
}
func (t *trigger) close() {
C.CFRelease(C.CFTypeRef(t.up))
C.CFRelease(C.CFTypeRef(t.down))
}
/************************************************
Controller
************************************************/
type controller struct {
Axes map[int]*axis
Buttons map[int]*button
Triggers map[int]*trigger
}
func newController(conf config) (*controller, error) {
axes := make(map[int]*axis)
for k, v := range conf.Axes {
a, err := openAxis(v[0], v[1], v[2])
if err != nil {
return nil, err
}
i, err := strconv.Atoi(k)
if err != nil {
return nil, err
}
axes[i] = a
}
buttons := make(map[int]*button)
for k, v := range conf.Buttons {
b, err := openButton(v)
if err != nil {
return nil, err
}
i, err := strconv.Atoi(k)
if err != nil {
return nil, err
}
buttons[i] = b
}
triggers := make(map[int]*trigger)
for k, v := range conf.Triggers {
t, err := openTrigger(v[0], v[1])
if err != nil {
return nil, err
}
i, err := strconv.Atoi(k)
if err != nil {
return nil, err
}
triggers[i] = t
}
return &controller{Axes: axes, Buttons: buttons, Triggers: triggers}, nil
}
func (c *controller) close() {
for _, v := range c.Axes {
v.close()
}
for _, v := range c.Buttons {
v.close()
}
for _, v := range c.Triggers {
v.close()
}
}
func main() {
defaultConfig := config{
Axes: map[string][3]int{
strconv.Itoa(sdl.CONTROLLER_AXIS_LEFTX): {0x4000, 0x7B, 0x7C}, // Arrow Left + Right
strconv.Itoa(sdl.CONTROLLER_AXIS_LEFTY): {0x4000, 0x7E, 0x7D}, // Arrow Up + Down
strconv.Itoa(sdl.CONTROLLER_AXIS_RIGHTX): {0x4000, 0x00, 0x07}, // A + X
strconv.Itoa(sdl.CONTROLLER_AXIS_RIGHTY): {0x4000, 0x01, 0x06}, // S + Z
},
Buttons: map[string]int{
strconv.Itoa(sdl.CONTROLLER_BUTTON_DPAD_UP): 0X7E, // Up
strconv.Itoa(sdl.CONTROLLER_BUTTON_DPAD_DOWN): 0X7D, // Down
strconv.Itoa(sdl.CONTROLLER_BUTTON_DPAD_LEFT): 0x7B, // Left
strconv.Itoa(sdl.CONTROLLER_BUTTON_DPAD_RIGHT): 0x7C, // Right
strconv.Itoa(sdl.CONTROLLER_BUTTON_A): 0X06, // Z
strconv.Itoa(sdl.CONTROLLER_BUTTON_B): 0x07, // X
strconv.Itoa(sdl.CONTROLLER_BUTTON_X): 0x00, // A
strconv.Itoa(sdl.CONTROLLER_BUTTON_Y): 0x01, // S
strconv.Itoa(sdl.CONTROLLER_BUTTON_LEFTSHOULDER): 0x0C, // Q
strconv.Itoa(sdl.CONTROLLER_BUTTON_RIGHTSHOULDER): 0x0D, // W
strconv.Itoa(sdl.CONTROLLER_BUTTON_LEFTSTICK): 0x38, // Shift
strconv.Itoa(sdl.CONTROLLER_BUTTON_RIGHTSTICK): 0x3B, // Ctrl
strconv.Itoa(sdl.CONTROLLER_BUTTON_BACK): 0x33, // Delete
strconv.Itoa(sdl.CONTROLLER_BUTTON_GUIDE): 0x30, // Tab
strconv.Itoa(sdl.CONTROLLER_BUTTON_START): 0x24, // Return
},
Triggers: map[string][2]int{
strconv.Itoa(sdl.CONTROLLER_AXIS_TRIGGERLEFT): {0x4000, 0x0E}, // E
strconv.Itoa(sdl.CONTROLLER_AXIS_TRIGGERRIGHT): {0x4000, 0x0F}, // R
},
}
conf := newConfig()
if file, err := os.Open("config.json"); err != nil {
f, err := os.Create("config.json")
if err != nil {
log.Fatal(err)
}
defer f.Close()
conf = defaultConfig
buf, err := json.MarshalIndent(conf, "", " ")
if err != nil {
log.Fatal(err)
}
f.Write(buf)
} else {
err := json.NewDecoder(file).Decode(&conf)
if err != nil {
os.Remove("config.json")
log.Fatal(err)
}
file.Close()
}
controller, err := newController(conf)
if err != nil {
log.Fatal(err)
}
defer controller.close()
err = sdl.Init((1 << 14) | sdl.INIT_GAMECONTROLLER)
if err != nil {
log.Fatal(err)
}
defer sdl.Quit()
sdlController := sdl.GameControllerOpen(0)
if sdlController == nil {
log.Fatal(sdl.GetError())
}
defer sdlController.Close()
for {
for e := sdl.PollEvent(); e != nil; e = sdl.PollEvent() {
switch e := e.(type) {
case *sdl.ControllerButtonEvent:
if e.State == 0 {
controller.Buttons[int(e.Button)].handleUp()
} else {
controller.Buttons[int(e.Button)].handleDown()
}
case *sdl.ControllerAxisEvent:
if e.Axis == sdl.CONTROLLER_AXIS_TRIGGERLEFT || e.Axis == sdl.CONTROLLER_AXIS_TRIGGERRIGHT {
controller.Triggers[int(e.Axis)].handleEvent(int(e.Value))
} else {
controller.Axes[int(e.Axis)].handleEvent(int(e.Value))
}
}
}
sdl.Delay(10)
}
}