/
gamecontroller.go
312 lines (263 loc) · 12.5 KB
/
gamecontroller.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
package sdl
/*
#include "sdl_wrapper.h"
#if !(SDL_VERSION_ATLEAST(2,0,4))
#pragma message("SDL_GameControllerFromInstanceID is not supported before SDL 2.0.4")
static SDL_GameController* SDL_GameControllerFromInstanceID(SDL_JoystickID joyid)
{
return NULL;
}
#endif
#if !(SDL_VERSION_ATLEAST(2,0,6))
#pragma message("SDL_GameControllerGetVendor is not supported before SDL 2.0.6")
static Uint16 SDL_GameControllerGetVendor(SDL_GameController* gamecontroller)
{
return 0;
}
#pragma message("SDL_GameControllerGetProduct is not supported before SDL 2.0.6")
static Uint16 SDL_GameControllerGetProduct(SDL_GameController* gamecontroller)
{
return 0;
}
#pragma message("SDL_GameControllerGetProductVersion is not supported before SDL 2.0.6")
static Uint16 SDL_GameControllerGetProductVersion(SDL_GameController* gamecontroller)
{
return 0;
}
#pragma message("SDL_GameControllerNumMappings is not supported before SDL 2.0.6")
static int SDL_GameControllerNumMappings(void)
{
return 0;
}
#pragma message("SDL_GameControllerMappingForIndex is not supported before SDL 2.0.6")
static char* SDL_GameControllerMappingForIndex(int mapping_index)
{
return NULL;
}
#endif
*/
import "C"
import "unsafe"
import "encoding/binary"
// Types of game controller inputs.
const (
CONTROLLER_BINDTYPE_NONE = C.SDL_CONTROLLER_BINDTYPE_NONE
CONTROLLER_BINDTYPE_BUTTON = C.SDL_CONTROLLER_BINDTYPE_BUTTON
CONTROLLER_BINDTYPE_AXIS = C.SDL_CONTROLLER_BINDTYPE_AXIS
CONTROLLER_BINDTYPE_HAT = C.SDL_CONTROLLER_BINDTYPE_HAT
)
// An enumeration of axes available from a controller.
// (https://wiki.libsdl.org/SDL_GameControllerAxis)
const (
CONTROLLER_AXIS_INVALID = C.SDL_CONTROLLER_AXIS_INVALID
CONTROLLER_AXIS_LEFTX = C.SDL_CONTROLLER_AXIS_LEFTX
CONTROLLER_AXIS_LEFTY = C.SDL_CONTROLLER_AXIS_LEFTY
CONTROLLER_AXIS_RIGHTX = C.SDL_CONTROLLER_AXIS_RIGHTX
CONTROLLER_AXIS_RIGHTY = C.SDL_CONTROLLER_AXIS_RIGHTY
CONTROLLER_AXIS_TRIGGERLEFT = C.SDL_CONTROLLER_AXIS_TRIGGERLEFT
CONTROLLER_AXIS_TRIGGERRIGHT = C.SDL_CONTROLLER_AXIS_TRIGGERRIGHT
CONTROLLER_AXIS_MAX = C.SDL_CONTROLLER_AXIS_MAX
)
// An enumeration of buttons available from a controller.
// (https://wiki.libsdl.org/SDL_GameControllerButton)
const (
CONTROLLER_BUTTON_INVALID = C.SDL_CONTROLLER_BUTTON_INVALID
CONTROLLER_BUTTON_A = C.SDL_CONTROLLER_BUTTON_A
CONTROLLER_BUTTON_B = C.SDL_CONTROLLER_BUTTON_B
CONTROLLER_BUTTON_X = C.SDL_CONTROLLER_BUTTON_X
CONTROLLER_BUTTON_Y = C.SDL_CONTROLLER_BUTTON_Y
CONTROLLER_BUTTON_BACK = C.SDL_CONTROLLER_BUTTON_BACK
CONTROLLER_BUTTON_GUIDE = C.SDL_CONTROLLER_BUTTON_GUIDE
CONTROLLER_BUTTON_START = C.SDL_CONTROLLER_BUTTON_START
CONTROLLER_BUTTON_LEFTSTICK = C.SDL_CONTROLLER_BUTTON_LEFTSTICK
CONTROLLER_BUTTON_RIGHTSTICK = C.SDL_CONTROLLER_BUTTON_RIGHTSTICK
CONTROLLER_BUTTON_LEFTSHOULDER = C.SDL_CONTROLLER_BUTTON_LEFTSHOULDER
CONTROLLER_BUTTON_RIGHTSHOULDER = C.SDL_CONTROLLER_BUTTON_RIGHTSHOULDER
CONTROLLER_BUTTON_DPAD_UP = C.SDL_CONTROLLER_BUTTON_DPAD_UP
CONTROLLER_BUTTON_DPAD_DOWN = C.SDL_CONTROLLER_BUTTON_DPAD_DOWN
CONTROLLER_BUTTON_DPAD_LEFT = C.SDL_CONTROLLER_BUTTON_DPAD_LEFT
CONTROLLER_BUTTON_DPAD_RIGHT = C.SDL_CONTROLLER_BUTTON_DPAD_RIGHT
CONTROLLER_BUTTON_MAX = C.SDL_CONTROLLER_BUTTON_MAX
)
// GameControllerBindType is a type of game controller input.
type GameControllerBindType C.SDL_GameControllerBindType
// GameControllerAxis is an axis on a game controller.
// (https://wiki.libsdl.org/SDL_GameControllerAxis)
type GameControllerAxis C.SDL_GameControllerAxis
// GameControllerButton is a button on a game controller.
// (https://wiki.libsdl.org/SDL_GameControllerButton)
type GameControllerButton C.SDL_GameControllerButton
// GameController used to identify an SDL game controller.
type GameController C.SDL_GameController
// GameControllerButtonBind SDL joystick layer binding for controller button/axis mapping.
type GameControllerButtonBind C.SDL_GameControllerButtonBind
func (ctrl *GameController) cptr() *C.SDL_GameController {
return (*C.SDL_GameController)(unsafe.Pointer(ctrl))
}
func (axis GameControllerAxis) c() C.SDL_GameControllerAxis {
return C.SDL_GameControllerAxis(axis)
}
func (btn GameControllerButton) c() C.SDL_GameControllerButton {
return C.SDL_GameControllerButton(btn)
}
// GameControllerAddMapping adds support for controllers that SDL is unaware of or to cause an existing controller to have a different binding.
// (https://wiki.libsdl.org/SDL_GameControllerAddMapping)
func GameControllerAddMapping(mappingString string) int {
_mappingString := C.CString(mappingString)
defer C.free(unsafe.Pointer(_mappingString))
return int(C.SDL_GameControllerAddMapping(_mappingString))
}
// GameControllerNumMappings returns the number of mappings installed.
func GameControllerNumMappings() int {
return int(C.SDL_GameControllerNumMappings())
}
// GameControllerMappingForIndex returns the game controller mapping string at a particular index.
func GameControllerMappingForIndex(index int) string {
mappingString := C.SDL_GameControllerMappingForIndex(C.int(index))
defer C.free(unsafe.Pointer(mappingString))
return C.GoString(mappingString)
}
// GameControllerMappingForGUID returns the game controller mapping string for a given GUID.
// (https://wiki.libsdl.org/SDL_GameControllerMappingForGUID)
func GameControllerMappingForGUID(guid JoystickGUID) string {
mappingString := C.SDL_GameControllerMappingForGUID(guid.c())
defer C.free(unsafe.Pointer(mappingString))
return C.GoString(mappingString)
}
// IsGameController reports whether the given joystick is supported by the game controller interface.
// (https://wiki.libsdl.org/SDL_IsGameController)
func IsGameController(index int) bool {
return C.SDL_IsGameController(C.int(index)) == C.SDL_TRUE
}
// GameControllerNameForIndex returns the implementation dependent name for the game controller.
// (https://wiki.libsdl.org/SDL_GameControllerNameForIndex)
func GameControllerNameForIndex(index int) string {
return C.GoString(C.SDL_GameControllerNameForIndex(C.int(index)))
}
// GameControllerOpen opens a gamecontroller for use.
// (https://wiki.libsdl.org/SDL_GameControllerOpen)
func GameControllerOpen(index int) *GameController {
return (*GameController)(C.SDL_GameControllerOpen(C.int(index)))
}
// GameControllerFromInstanceID returns the GameController associated with an instance id.
// (https://wiki.libsdl.org/SDL_GameControllerFromInstanceID)
func GameControllerFromInstanceID(joyid JoystickID) *GameController {
return (*GameController)(C.SDL_GameControllerFromInstanceID(joyid.c()))
}
// Name returns the implementation dependent name for an opened game controller.
// (https://wiki.libsdl.org/SDL_GameControllerName)
func (ctrl *GameController) Name() string {
return C.GoString(C.SDL_GameControllerName(ctrl.cptr()))
}
// Vendor returns the USB vendor ID of an opened controller, if available, 0 otherwise.
func (ctrl *GameController) Vendor() int {
return int(C.SDL_GameControllerGetVendor(ctrl.cptr()))
}
// Product returns the USB product ID of an opened controller, if available, 0 otherwise.
func (ctrl *GameController) Product() int {
return int(C.SDL_GameControllerGetProduct(ctrl.cptr()))
}
// ProductVersion returns the product version of an opened controller, if available, 0 otherwise.
func (ctrl *GameController) ProductVersion() int {
return int(C.SDL_GameControllerGetProductVersion(ctrl.cptr()))
}
// Attached reports whether a controller has been opened and is currently connected.
// (https://wiki.libsdl.org/SDL_GameControllerGetAttached)
func (ctrl *GameController) Attached() bool {
return C.SDL_GameControllerGetAttached(ctrl.cptr()) == C.SDL_TRUE
}
// Mapping returns the current mapping of a Game Controller.
// (https://wiki.libsdl.org/SDL_GameControllerMapping)
func (ctrl *GameController) Mapping() string {
mappingString := C.SDL_GameControllerMapping(ctrl.cptr())
defer C.free(unsafe.Pointer(mappingString))
return C.GoString(mappingString)
}
// Joystick returns the Joystick ID from a Game Controller. The game controller builds on the Joystick API, but to be able to use the Joystick's functions with a gamepad, you need to use this first to get the joystick object.
// (https://wiki.libsdl.org/SDL_GameControllerGetJoystick)
func (ctrl *GameController) Joystick() *Joystick {
return (*Joystick)(unsafe.Pointer(C.SDL_GameControllerGetJoystick(ctrl.cptr())))
}
// GameControllerEventState returns the current state of, enable, or disable events dealing with Game Controllers. This will not disable Joystick events, which can also be fired by a controller (see https://wiki.libsdl.org/SDL_JoystickEventState).
// (https://wiki.libsdl.org/SDL_GameControllerEventState)
func GameControllerEventState(state int) int {
return int(C.SDL_GameControllerEventState(C.int(state)))
}
// GameControllerUpdate manually pumps game controller updates if not using the loop.
// (https://wiki.libsdl.org/SDL_GameControllerUpdate)
func GameControllerUpdate() {
C.SDL_GameControllerUpdate()
}
// GameControllerGetAxisFromString converts a string into an enum representation for a GameControllerAxis.
// (https://wiki.libsdl.org/SDL_GameControllerGetAxisFromString)
func GameControllerGetAxisFromString(pchString string) GameControllerAxis {
_pchString := C.CString(pchString)
defer C.free(unsafe.Pointer(_pchString))
return GameControllerAxis(C.SDL_GameControllerGetAxisFromString(_pchString))
}
// GameControllerGetStringForAxis converts from an axis enum to a string.
// (https://wiki.libsdl.org/SDL_GameControllerGetStringForAxis)
func GameControllerGetStringForAxis(axis GameControllerAxis) string {
return C.GoString(C.SDL_GameControllerGetStringForAxis(axis.c()))
}
// BindForAxis returns the SDL joystick layer binding for a controller button mapping.
// (https://wiki.libsdl.org/SDL_GameControllerGetBindForAxis)
func (ctrl *GameController) BindForAxis(axis GameControllerAxis) GameControllerButtonBind {
return GameControllerButtonBind(C.SDL_GameControllerGetBindForAxis(ctrl.cptr(), axis.c()))
}
// Axis returns the current state of an axis control on a game controller.
// (https://wiki.libsdl.org/SDL_GameControllerGetAxis)
func (ctrl *GameController) Axis(axis GameControllerAxis) int16 {
return int16(C.SDL_GameControllerGetAxis(ctrl.cptr(), axis.c()))
}
// GameControllerGetButtonFromString turns a string into a button mapping.
// (https://wiki.libsdl.org/SDL_GameControllerGetButtonFromString)
func GameControllerGetButtonFromString(pchString string) GameControllerButton {
_pchString := C.CString(pchString)
defer C.free(unsafe.Pointer(_pchString))
return GameControllerButton(C.SDL_GameControllerGetButtonFromString(_pchString))
}
// GameControllerGetStringForButton turns a button enum into a string mapping.
// (https://wiki.libsdl.org/SDL_GameControllerGetStringForButton)
func GameControllerGetStringForButton(btn GameControllerButton) string {
return C.GoString(C.SDL_GameControllerGetStringForButton(btn.c()))
}
// BindForButton returns the SDL joystick layer binding for this controller button mapping.
// (https://wiki.libsdl.org/SDL_GameControllerGetBindForButton)
func (ctrl *GameController) BindForButton(btn GameControllerButton) GameControllerButtonBind {
return GameControllerButtonBind(C.SDL_GameControllerGetBindForButton(ctrl.cptr(), btn.c()))
}
// Button returns the current state of a button on a game controller.
// (https://wiki.libsdl.org/SDL_GameControllerGetButton)
func (ctrl *GameController) Button(btn GameControllerButton) byte {
return byte(C.SDL_GameControllerGetButton(ctrl.cptr(), btn.c()))
}
// Close closes a game controller previously opened with GameControllerOpen().
// (https://wiki.libsdl.org/SDL_GameControllerClose)
func (ctrl *GameController) Close() {
C.SDL_GameControllerClose(ctrl.cptr())
}
// Type returns the type of game controller input for this SDL joystick layer binding.
func (bind *GameControllerButtonBind) Type() int {
return int(bind.bindType)
}
// Button returns button mapped for this SDL joystick layer binding.
func (bind *GameControllerButtonBind) Button() int {
val, _ := binary.Varint(bind.value[:4])
return int(val)
}
// Axis returns axis mapped for this SDL joystick layer binding.
func (bind *GameControllerButtonBind) Axis() int {
val, _ := binary.Varint(bind.value[:4])
return int(val)
}
// Hat returns hat mapped for this SDL joystick layer binding.
func (bind *GameControllerButtonBind) Hat() int {
val, _ := binary.Varint(bind.value[:4])
return int(val)
}
// HatMask returns hat mask for this SDL joystick layer binding.
func (bind *GameControllerButtonBind) HatMask() int {
val, _ := binary.Varint(bind.value[4:8])
return int(val)
}