22 #include "../../SDL_internal.h" 24 #ifdef SDL_JOYSTICK_EMSCRIPTEN 34 #include "SDL_sysjoystick_c.h" 35 #include "../SDL_joystick_c.h" 37 static SDL_joylist_item * JoystickByIndex(
int index);
39 static SDL_joylist_item *SDL_joylist =
NULL;
40 static SDL_joylist_item *SDL_joylist_tail =
NULL;
42 static int instance_counter = 0;
45 Emscripten_JoyStickConnected(
int eventType,
const EmscriptenGamepadEvent *gamepadEvent,
void *userData)
49 SDL_joylist_item *item;
51 if (JoystickByIndex(gamepadEvent->index) !=
NULL) {
55 item = (SDL_joylist_item *)
SDL_malloc(
sizeof (SDL_joylist_item));
61 item->index = gamepadEvent->index;
64 if ( item->name ==
NULL ) {
69 item->mapping =
SDL_strdup(gamepadEvent->mapping);
70 if ( item->mapping ==
NULL ) {
76 item->naxes = gamepadEvent->numAxes;
77 item->nbuttons = gamepadEvent->numButtons;
78 item->device_instance = instance_counter++;
80 item->timestamp = gamepadEvent->timestamp;
82 for( i = 0; i < item->naxes; i++) {
83 item->axis[
i] = gamepadEvent->axis[
i];
86 for( i = 0; i < item->nbuttons; i++) {
87 item->analogButton[
i] = gamepadEvent->analogButton[
i];
88 item->digitalButton[
i] = gamepadEvent->digitalButton[
i];
91 if (SDL_joylist_tail ==
NULL) {
92 SDL_joylist = SDL_joylist_tail = item;
94 SDL_joylist_tail->next = item;
95 SDL_joylist_tail = item;
102 #ifdef DEBUG_JOYSTICK 106 #ifdef DEBUG_JOYSTICK 107 SDL_Log(
"Added joystick with index %d", item->index);
114 Emscripten_JoyStickDisconnected(
int eventType,
const EmscriptenGamepadEvent *gamepadEvent,
void *userData)
116 SDL_joylist_item *item = SDL_joylist;
117 SDL_joylist_item *prev =
NULL;
119 while (item !=
NULL) {
120 if (item->index == gamepadEvent->index) {
131 if (item->joystick) {
132 item->joystick->hwdata =
NULL;
136 prev->next = item->next;
139 SDL_joylist = item->next;
141 if (item == SDL_joylist_tail) {
142 SDL_joylist_tail = prev;
150 #ifdef DEBUG_JOYSTICK 151 SDL_Log(
"Removed joystick with id %d", item->device_instance);
161 EMSCRIPTEN_JoystickQuit(
void)
163 SDL_joylist_item *item =
NULL;
164 SDL_joylist_item *next =
NULL;
166 for (item = SDL_joylist; item; item = next) {
173 SDL_joylist = SDL_joylist_tail =
NULL;
176 instance_counter = 0;
178 emscripten_set_gamepadconnected_callback(
NULL, 0,
NULL);
179 emscripten_set_gamepaddisconnected_callback(
NULL, 0,
NULL);
186 EMSCRIPTEN_JoystickInit(
void)
189 EmscriptenGamepadEvent gamepadState;
192 numjs = emscripten_get_num_gamepads();
195 if (numjs == EMSCRIPTEN_RESULT_NOT_SUPPORTED) {
201 for(i = 0; i < numjs; i++) {
202 retval = emscripten_get_gamepad_status(i, &gamepadState);
203 if (retval == EMSCRIPTEN_RESULT_SUCCESS) {
204 Emscripten_JoyStickConnected(EMSCRIPTEN_EVENT_GAMEPADCONNECTED,
211 retval = emscripten_set_gamepadconnected_callback(
NULL,
213 Emscripten_JoyStickConnected);
215 if(retval != EMSCRIPTEN_RESULT_SUCCESS) {
216 EMSCRIPTEN_JoystickQuit();
217 return SDL_SetError(
"Could not set gamepad connect callback");
220 retval = emscripten_set_gamepaddisconnected_callback(
NULL,
222 Emscripten_JoyStickDisconnected);
223 if(retval != EMSCRIPTEN_RESULT_SUCCESS) {
224 EMSCRIPTEN_JoystickQuit();
225 return SDL_SetError(
"Could not set gamepad disconnect callback");
232 static SDL_joylist_item *
233 JoystickByDeviceIndex(
int device_index)
235 SDL_joylist_item *item = SDL_joylist;
237 while (0 < device_index) {
246 static SDL_joylist_item *
247 JoystickByIndex(
int index)
249 SDL_joylist_item *item = SDL_joylist;
255 while (item !=
NULL) {
256 if (item->index == index) {
266 EMSCRIPTEN_JoystickGetCount(
void)
272 EMSCRIPTEN_JoystickDetect(
void)
277 EMSCRIPTEN_JoystickGetDeviceName(
int device_index)
279 return JoystickByDeviceIndex(device_index)->name;
283 EMSCRIPTEN_JoystickGetDevicePlayerIndex(
int device_index)
289 EMSCRIPTEN_JoystickGetDeviceInstanceID(
int device_index)
291 return JoystickByDeviceIndex(device_index)->device_instance;
300 EMSCRIPTEN_JoystickOpen(SDL_Joystick * joystick,
int device_index)
302 SDL_joylist_item *item = JoystickByDeviceIndex(device_index);
308 if (item->joystick !=
NULL) {
312 joystick->instance_id = item->device_instance;
318 joystick->nballs = 0;
320 joystick->nbuttons = item->nbuttons;
321 joystick->naxes = item->naxes;
332 EMSCRIPTEN_JoystickUpdate(SDL_Joystick * joystick)
334 EmscriptenGamepadEvent gamepadState;
335 SDL_joylist_item *item = (SDL_joylist_item *) joystick->hwdata;
336 int i,
result, buttonState;
339 result = emscripten_get_gamepad_status(item->index, &gamepadState);
340 if(
result == EMSCRIPTEN_RESULT_SUCCESS) {
341 if(gamepadState.timestamp == 0 || gamepadState.timestamp != item->timestamp) {
342 for(i = 0; i < item->nbuttons; i++) {
343 if(item->digitalButton[i] != gamepadState.digitalButton[i]) {
349 item->analogButton[
i] = gamepadState.analogButton[
i];
350 item->digitalButton[
i] = gamepadState.digitalButton[
i];
353 for(i = 0; i < item->naxes; i++) {
354 if(item->axis[i] != gamepadState.axis[i]) {
357 (
Sint16) (32767.*gamepadState.axis[i]));
361 item->axis[
i] = gamepadState.axis[
i];
364 item->timestamp = gamepadState.timestamp;
372 EMSCRIPTEN_JoystickClose(SDL_Joystick * joystick)
374 SDL_joylist_item *item = (SDL_joylist_item *) joystick->hwdata;
376 item->joystick =
NULL;
381 EMSCRIPTEN_JoystickGetDeviceGUID(
int device_index)
385 const char *
name = EMSCRIPTEN_JoystickGetDeviceName(device_index);
392 EMSCRIPTEN_JoystickRumble(SDL_Joystick * joystick,
Uint16 low_frequency_rumble,
Uint16 high_frequency_rumble,
Uint32 duration_ms)
399 EMSCRIPTEN_JoystickInit,
400 EMSCRIPTEN_JoystickGetCount,
401 EMSCRIPTEN_JoystickDetect,
402 EMSCRIPTEN_JoystickGetDeviceName,
403 EMSCRIPTEN_JoystickGetDevicePlayerIndex,
404 EMSCRIPTEN_JoystickGetDeviceGUID,
405 EMSCRIPTEN_JoystickGetDeviceInstanceID,
406 EMSCRIPTEN_JoystickOpen,
407 EMSCRIPTEN_JoystickRumble,
408 EMSCRIPTEN_JoystickUpdate,
409 EMSCRIPTEN_JoystickClose,
410 EMSCRIPTEN_JoystickQuit,
void SDL_PrivateJoystickRemoved(SDL_JoystickID device_instance)
set set set set set set set macro pixldst1 abits if abits op else op endif endm macro pixldst2 abits if abits op else op endif endm macro pixldst4 abits if abits op else op endif endm macro pixldst0 abits op endm macro pixldst3 mem_operand op endm macro pixldst30 mem_operand op endm macro pixldst abits if abits elseif abits elseif abits elseif abits elseif abits pixldst0 abits else pixldst0 abits pixldst0 abits pixldst0 abits pixldst0 abits endif elseif abits else pixldst0 abits pixldst0 abits endif elseif abits else error unsupported bpp *numpix else pixst endif endm macro pixld1_s mem_operand if asr adds SRC_WIDTH_FIXED bpl add asl mov asr adds SRC_WIDTH_FIXED bpl add asl mov asr adds SRC_WIDTH_FIXED bpl add asl mov asr adds SRC_WIDTH_FIXED bpl add asl elseif asr adds SRC_WIDTH_FIXED bpl add asl mov asr adds SRC_WIDTH_FIXED bpl add asl else error unsupported endif endm macro pixld2_s mem_operand if mov asr add asl add asl mov asr sub UNIT_X add asl mov asr add asl add asl mov asr add UNIT_X add asl else pixld1_s mem_operand pixld1_s mem_operand endif endm macro pixld0_s mem_operand if asr adds SRC_WIDTH_FIXED bpl add asl elseif asr adds SRC_WIDTH_FIXED bpl add asl endif endm macro pixld_s_internal mem_operand if mem_operand pixld2_s mem_operand pixdeinterleave basereg elseif mem_operand elseif mem_operand elseif mem_operand elseif mem_operand pixld0_s mem_operand else pixld0_s mem_operand pixld0_s mem_operand pixld0_s mem_operand pixld0_s mem_operand endif elseif mem_operand else pixld0_s mem_operand pixld0_s mem_operand endif elseif mem_operand else error unsupported mem_operand if bpp mem_operand endif endm macro vuzp8 reg2 vuzp d d ®2 endm macro vzip8 reg2 vzip d d ®2 endm macro pixdeinterleave basereg basereg basereg basereg basereg endif endm macro pixinterleave basereg basereg basereg basereg basereg endif endm macro PF boost_increment endif if endif PF tst PF addne PF subne PF cmp ORIG_W if endif if endif if endif PF subge ORIG_W PF subges if endif if endif if endif endif endm macro cache_preload_simple endif if dst_r_bpp pld [DST_R, #(PREFETCH_DISTANCE_SIMPLE *dst_r_bpp/8)] endif if mask_bpp pld if[MASK, #(PREFETCH_DISTANCE_SIMPLE *mask_bpp/8)] endif endif endm macro fetch_mask_pixblock pixld mask_basereg pixblock_size MASK endm macro ensure_destination_ptr_alignment process_pixblock_tail_head if beq irp local skip1(dst_w_bpp<=(lowbit *8)) &&((lowbit *8)<(pixblock_size *dst_w_bpp)) .if lowbit< 16 tst DST_R
int SDL_PrivateJoystickButton(SDL_Joystick *joystick, Uint8 button, Uint8 state)
int SDL_PrivateJoystickAxis(SDL_Joystick *joystick, Uint8 axis, Sint16 value)
GLuint const GLchar * name
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
#define SDL_assert(condition)
void SDL_PrivateJoystickAdded(SDL_JoystickID device_instance)
SDL_JoystickDriver SDL_EMSCRIPTEN_JoystickDriver
#define SDL_Unsupported()