22 #include "../../SDL_internal.h" 24 #ifdef SDL_JOYSTICK_EMSCRIPTEN 35 #include "SDL_sysjoystick_c.h" 36 #include "../SDL_joystick_c.h" 38 static SDL_joylist_item * JoystickByIndex(
int index);
40 static SDL_joylist_item *SDL_joylist =
NULL;
41 static SDL_joylist_item *SDL_joylist_tail =
NULL;
43 static int instance_counter = 0;
46 Emscripten_JoyStickConnected(
int eventType,
const EmscriptenGamepadEvent *gamepadEvent,
void *userData)
50 SDL_joylist_item *item;
52 if (JoystickByIndex(gamepadEvent->index) !=
NULL) {
56 item = (SDL_joylist_item *)
SDL_malloc(
sizeof (SDL_joylist_item));
62 item->index = gamepadEvent->index;
65 if ( item->name ==
NULL ) {
70 item->mapping =
SDL_strdup(gamepadEvent->mapping);
71 if ( item->mapping ==
NULL ) {
77 item->naxes = gamepadEvent->numAxes;
78 item->nbuttons = gamepadEvent->numButtons;
79 item->device_instance = instance_counter++;
81 item->timestamp = gamepadEvent->timestamp;
83 for( i = 0; i < item->naxes; i++) {
84 item->axis[
i] = gamepadEvent->axis[
i];
87 for( i = 0; i < item->nbuttons; i++) {
88 item->analogButton[
i] = gamepadEvent->analogButton[
i];
89 item->digitalButton[
i] = gamepadEvent->digitalButton[
i];
92 if (SDL_joylist_tail ==
NULL) {
93 SDL_joylist = SDL_joylist_tail = item;
95 SDL_joylist_tail->next = item;
96 SDL_joylist_tail = item;
103 #ifdef DEBUG_JOYSTICK 107 #ifdef DEBUG_JOYSTICK 108 SDL_Log(
"Added joystick with index %d", item->index);
115 Emscripten_JoyStickDisconnected(
int eventType,
const EmscriptenGamepadEvent *gamepadEvent,
void *userData)
117 SDL_joylist_item *item = SDL_joylist;
118 SDL_joylist_item *prev =
NULL;
120 while (item !=
NULL) {
121 if (item->index == gamepadEvent->index) {
132 if (item->joystick) {
133 item->joystick->hwdata =
NULL;
137 prev->next = item->next;
140 SDL_joylist = item->next;
142 if (item == SDL_joylist_tail) {
143 SDL_joylist_tail = prev;
151 #ifdef DEBUG_JOYSTICK 152 SDL_Log(
"Removed joystick with id %d", item->device_instance);
167 EmscriptenGamepadEvent gamepadState;
170 numjs = emscripten_get_num_gamepads();
173 if (numjs == EMSCRIPTEN_RESULT_NOT_SUPPORTED) {
179 for(i = 0; i < numjs; i++) {
180 retval = emscripten_get_gamepad_status(i, &gamepadState);
181 if (retval == EMSCRIPTEN_RESULT_SUCCESS) {
182 Emscripten_JoyStickConnected(EMSCRIPTEN_EVENT_GAMEPADCONNECTED,
189 retval = emscripten_set_gamepadconnected_callback(
NULL,
191 Emscripten_JoyStickConnected);
193 if(retval != EMSCRIPTEN_RESULT_SUCCESS) {
195 return SDL_SetError(
"Could not set gamepad connect callback");
198 retval = emscripten_set_gamepaddisconnected_callback(
NULL,
200 Emscripten_JoyStickDisconnected);
201 if(retval != EMSCRIPTEN_RESULT_SUCCESS) {
203 return SDL_SetError(
"Could not set gamepad disconnect callback");
210 static SDL_joylist_item *
211 JoystickByDeviceIndex(
int device_index)
213 SDL_joylist_item *item = SDL_joylist;
215 while (0 < device_index) {
224 static SDL_joylist_item *
225 JoystickByIndex(
int index)
227 SDL_joylist_item *item = SDL_joylist;
233 while (item !=
NULL) {
234 if (item->index == index) {
256 return JoystickByDeviceIndex(device_index)->name;
262 return JoystickByDeviceIndex(device_index)->device_instance;
273 SDL_joylist_item *item = JoystickByDeviceIndex(device_index);
279 if (item->joystick !=
NULL) {
283 joystick->instance_id = item->device_instance;
289 joystick->nballs = 0;
291 joystick->nbuttons = item->nbuttons;
292 joystick->naxes = item->naxes;
300 return joystick->hwdata !=
NULL;
311 EmscriptenGamepadEvent gamepadState;
312 SDL_joylist_item *item = (SDL_joylist_item *) joystick->hwdata;
313 int i,
result, buttonState;
316 result = emscripten_get_gamepad_status(item->index, &gamepadState);
317 if(
result == EMSCRIPTEN_RESULT_SUCCESS) {
318 if(gamepadState.timestamp == 0 || gamepadState.timestamp != item->timestamp) {
319 for(i = 0; i < item->nbuttons; i++) {
320 if(item->digitalButton[i] != gamepadState.digitalButton[i]) {
326 item->analogButton[
i] = gamepadState.analogButton[
i];
327 item->digitalButton[
i] = gamepadState.digitalButton[
i];
330 for(i = 0; i < item->naxes; i++) {
331 if(item->axis[i] != gamepadState.axis[i]) {
334 (
Sint16) (32767.*gamepadState.axis[i]));
338 item->axis[
i] = gamepadState.axis[
i];
341 item->timestamp = gamepadState.timestamp;
351 SDL_joylist_item *item = (SDL_joylist_item *) joystick->hwdata;
353 item->joystick =
NULL;
361 SDL_joylist_item *item =
NULL;
364 for (item = SDL_joylist;
item; item =
next) {
371 SDL_joylist = SDL_joylist_tail =
NULL;
374 instance_counter = 0;
376 emscripten_set_gamepadconnected_callback(
NULL, 0,
NULL);
377 emscripten_set_gamepaddisconnected_callback(
NULL, 0,
NULL);
396 const char *name = joystick->name;
void SDL_PrivateJoystickRemoved(SDL_JoystickID device_instance)
int SDL_SYS_NumJoysticks()
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)
struct joystick_hwdata * next
GLuint const GLchar * name
void SDL_SYS_JoystickQuit(void)
int SDL_PrivateJoystickAxis(SDL_Joystick *joystick, Uint8 axis, Sint16 value)
SDL_bool SDL_SYS_JoystickAttached(SDL_Joystick *joystick)
void SDL_SYS_JoystickDetect()
void SDL_PrivateJoystickAdded(int device_index)
const char * SDL_SYS_JoystickNameForDeviceIndex(int device_index)
void SDL_SYS_JoystickUpdate(SDL_Joystick *joystick)
SDL_JoystickGUID SDL_SYS_JoystickGetGUID(SDL_Joystick *joystick)
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)
int SDL_SYS_JoystickInit(void)
struct SDL_joylist_item * item
SDL_JoystickID SDL_SYS_GetInstanceIdOfDeviceIndex(int device_index)
int SDL_SYS_JoystickOpen(SDL_Joystick *joystick, int device_index)
void SDL_SYS_JoystickClose(SDL_Joystick *joystick)
SDL_JoystickGUID SDL_SYS_JoystickGetDeviceGUID(int device_index)
int16_t Sint16
A signed 16-bit integer type.