3
#include <SDL/SDL_ttf.h>
6
#include <emscripten.h>
10
void assertJoystickEvent(int expectedGamepad, int expectedType, int expectedIndex, int expectedValue) {
13
// Loop ends either when assertion fails (we run out of events), or we find
14
// the event we're looking for.
15
assert(SDL_PollEvent(&event) == 1);
16
if (event.type != expectedType) {
20
case SDL_JOYAXISMOTION: {
21
assert(event.jaxis.which == expectedGamepad);
22
assert(event.jaxis.axis == expectedIndex);
23
assert(event.jaxis.value == expectedValue);
26
case SDL_JOYBUTTONUP: case SDL_JOYBUTTONDOWN: {
27
assert(event.jbutton.which == expectedGamepad);
28
assert(event.jbutton.button == expectedIndex);
29
assert(event.jbutton.state == expectedValue);
33
// Break out of while loop.
38
void assertNoJoystickEvent() {
40
while(SDL_PollEvent(&event)) {
42
case SDL_JOYBUTTONDOWN: case SDL_JOYBUTTONUP: case SDL_JOYAXISMOTION: {
50
void main_2(void* arg);
53
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK);
54
SDL_Surface *screen = SDL_SetVideoMode(600, 450, 32, SDL_HWSURFACE);
55
emscripten_async_call(main_2, NULL, 3000); // avoid startup delays and intermittent errors
59
void main_2(void* arg) {
60
// TODO: At the moment, we only support joystick support through polling.
61
emscripten_run_script("window.addNewGamepad('Pad Thai', 4, 16)");
62
emscripten_run_script("window.addNewGamepad('Pad Kee Mao', 0, 4)");
63
// Check that the joysticks exist properly.
64
assert(SDL_NumJoysticks() == 2);
65
assert(!SDL_JoystickOpened(0));
66
assert(!SDL_JoystickOpened(1));
67
SDL_Joystick* pad1 = SDL_JoystickOpen(0);
68
assert(SDL_JoystickOpened(0));
69
assert(SDL_JoystickIndex(pad1) == 0);
70
assert(strncmp(SDL_JoystickName(0), "Pad Thai", 9) == 0);
71
assert(strncmp(SDL_JoystickName(1), "Pad Kee Mao", 12) == 0);
72
assert(SDL_JoystickNumAxes(pad1) == 4);
73
assert(SDL_JoystickNumButtons(pad1) == 16);
75
// By default, SDL will automatically process events. Test this behavior, and then disable it.
76
assert(SDL_JoystickEventState(SDL_QUERY) == SDL_ENABLE);
77
SDL_JoystickEventState(SDL_DISABLE);
78
assert(SDL_JoystickEventState(SDL_QUERY) == SDL_DISABLE);
80
emscripten_run_script("window.simulateGamepadButtonDown(0, 1)");
81
// We didn't tell SDL to automatically update this joystick's state.
82
assertNoJoystickEvent();
84
assertJoystickEvent(0, SDL_JOYBUTTONDOWN, 1, SDL_PRESSED);
85
assert(SDL_JoystickGetButton(pad1, 1) == 1);
86
// Enable automatic updates.
87
SDL_JoystickEventState(SDL_ENABLE);
88
assert(SDL_JoystickEventState(SDL_QUERY) == SDL_ENABLE);
89
emscripten_run_script("window.simulateGamepadButtonUp(0, 1)");
90
assertJoystickEvent(0, SDL_JOYBUTTONUP, 1, SDL_RELEASED);
91
assert(SDL_JoystickGetButton(pad1, 1) == 0);
92
// No button change: Should not result in a new event.
93
emscripten_run_script("window.simulateGamepadButtonUp(0, 1)");
94
assertNoJoystickEvent();
95
// Joystick 1 is not opened; should not result in a new event.
96
emscripten_run_script("window.simulateGamepadButtonDown(1, 1)");
97
assertNoJoystickEvent();
100
emscripten_run_script("window.simulateAxisMotion(0, 0, 1)");
101
assertJoystickEvent(0, SDL_JOYAXISMOTION, 0, 32767);
102
assert(SDL_JoystickGetAxis(pad1, 0) == 32767);
103
emscripten_run_script("window.simulateAxisMotion(0, 0, 0)");
104
assertJoystickEvent(0, SDL_JOYAXISMOTION, 0, 0);
105
assert(SDL_JoystickGetAxis(pad1, 0) == 0);
106
emscripten_run_script("window.simulateAxisMotion(0, 1, -1)");
107
assertJoystickEvent(0, SDL_JOYAXISMOTION, 1, -32768);
108
assert(SDL_JoystickGetAxis(pad1, 1) == -32768);
109
emscripten_run_script("window.simulateAxisMotion(0, 1, -1)");
110
// No joystick change: Should not result in a new event.
111
assertNoJoystickEvent();
112
// Joystick 1 is not opened; should not result in a new event.
113
emscripten_run_script("window.simulateAxisMotion(1, 1, -1)");
114
assertNoJoystickEvent();
116
SDL_JoystickClose(pad1);
117
assert(!SDL_JoystickOpened(0));
119
// Joystick 0 is closed; we should not process any new gamepad events from it.
120
emscripten_run_script("window.simulateGamepadButtonDown(0, 1)");
121
assertNoJoystickEvent();
125
printf("Test passed!\n");