summaryrefslogtreecommitdiff
path: root/sdl2/gamecontroller.ha
blob: 4cab9cee2928ff45db6a8df0c2f06fda429fb5bc (plain)
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
// TODO: Flesh me out

// The SDL_GameController structure used to identify an SDL game controller.
// (Opaque)
export type SDL_GameController = opaque;

// The list of axes available from a controller
//
// Thumbstick axis values range from [[SDL_JOYSTICK_AXIS_MIN]] to
// [[SDL_JOYSTICK_AXIS_MAX]], and are centered within ~8000 of zero, though advanced
// UI will allow users to set or autodetect the dead zone, which varies between
// controllers.
//
// Trigger axis values range from 0 to [[SDL_JOYSTICK_AXIS_MAX]].
export type SDL_GameControllerAxis = enum u8 {
	LEFTX,
	LEFTY,
	RIGHTX,
	RIGHTY,
	TRIGGERLEFT,
	TRIGGERRIGHT,
	INVALID = 255,
};

// The list of buttons available from a controller
export type SDL_GameControllerButton = enum u8 {
	INVALID = 255,
	A = 0,
	B,
	X,
	Y,
	BACK,
	GUIDE,
	START,
	LEFTSTICK,
	RIGHTSTICK,
	LEFTSHOULDER,
	RIGHTSHOULDER,
	DPAD_UP,
	DPAD_DOWN,
	DPAD_LEFT,
	DPAD_RIGHT,
	MISC1,
	PADDLE1,
	PADDLE2,
	PADDLE3,
	PADDLE4,
	TOUCHPAD,
};

// Check if the given joystick is supported by the game controller interface.
//
// 'joystick_index' is the same as the 'device_index' passed to
// [[joystick_open]].
//
// Returns true if the given joystick is supported by the game controller
// interface, false if it isn't or it's an invalid index.
export @symbol("SDL_IsGameController") fn SDL_IsGameController(
	joystick_index: int) bool;

@symbol("SDL_GameControllerOpen") fn _game_controller_open(
	joystick_index: int) nullable *SDL_GameController;

// Get the SDL_GameController associated with an instance id.
export fn SDL_GameControllerOpen(
	joystick_index: int,
) (*SDL_GameController | error) = {
	return wrapptr(_game_controller_open(joystick_index))?: *SDL_GameController;
};

// Close a game controller previously opened with [[game_controller_open]].
export @symbol("SDL_GameControllerClose") fn SDL_GameControllerClose(
	gamecontroller: *SDL_GameController) void;

@symbol("SDL_GameControllerRumble") fn _SDL_GameControllerRumble(
	gamecontroller: *SDL_GameController,
	low_frequency_rumble: u16,
	high_frequency_rumble: u16,
	duration_ms: u32) int;

// Start a rumble effect on a game controller.
//
// Each call to this function cancels any previous rumble effect, and calling
// it with 0 intensity stops any rumbling.
//
// The low-frequency motor is generally on the left, and the high-frequency
// motor is generally on the right.
export fn SDL_GameControllerRumble(
	gamecontroller: *SDL_GameController,
	low_frequency_rumble: u16,
	high_frequency_rumble: u16,
	duration_ms: u32,
) (void | error) = {
	return wrapvoid(_SDL_GameControllerRumble(
		gamecontroller,
		low_frequency_rumble,
		high_frequency_rumble,
		duration_ms));
};