SDL  2.0
SDL_sysjoystick.c
Go to the documentation of this file.
1 /*
2  Simple DirectMedia Layer
3  Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
4 
5  This software is provided 'as-is', without any express or implied
6  warranty. In no event will the authors be held liable for any damages
7  arising from the use of this software.
8 
9  Permission is granted to anyone to use this software for any purpose,
10  including commercial applications, and to alter it and redistribute it
11  freely, subject to the following restrictions:
12 
13  1. The origin of this software must not be misrepresented; you must not
14  claim that you wrote the original software. If you use this software
15  in a product, an acknowledgment in the product documentation would be
16  appreciated but is not required.
17  2. Altered source versions must be plainly marked as such, and must not be
18  misrepresented as being the original software.
19  3. This notice may not be removed or altered from any source distribution.
20 */
21 
22 #include "../../SDL_internal.h"
23 
24 #ifdef SDL_JOYSTICK_EMSCRIPTEN
25 
26 #include <stdio.h> /* For the definition of NULL */
27 #include "SDL_error.h"
28 #include "SDL_events.h"
29 
30 #include "SDL_joystick.h"
31 #include "SDL_assert.h"
32 #include "SDL_timer.h"
33 #include "SDL_log.h"
34 #include "SDL_sysjoystick_c.h"
35 #include "../SDL_joystick_c.h"
36 
37 static SDL_joylist_item * JoystickByIndex(int index);
38 
39 static SDL_joylist_item *SDL_joylist = NULL;
40 static SDL_joylist_item *SDL_joylist_tail = NULL;
41 static int numjoysticks = 0;
42 static int instance_counter = 0;
43 
44 static EM_BOOL
45 Emscripten_JoyStickConnected(int eventType, const EmscriptenGamepadEvent *gamepadEvent, void *userData)
46 {
47  int i;
48 
49  SDL_joylist_item *item;
50 
51  if (JoystickByIndex(gamepadEvent->index) != NULL) {
52  return 1;
53  }
54 
55  item = (SDL_joylist_item *) SDL_malloc(sizeof (SDL_joylist_item));
56  if (item == NULL) {
57  return 1;
58  }
59 
60  SDL_zerop(item);
61  item->index = gamepadEvent->index;
62 
63  item->name = SDL_strdup(gamepadEvent->id);
64  if ( item->name == NULL ) {
65  SDL_free(item);
66  return 1;
67  }
68 
69  item->mapping = SDL_strdup(gamepadEvent->mapping);
70  if ( item->mapping == NULL ) {
71  SDL_free(item->name);
72  SDL_free(item);
73  return 1;
74  }
75 
76  item->naxes = gamepadEvent->numAxes;
77  item->nbuttons = gamepadEvent->numButtons;
78  item->device_instance = instance_counter++;
79 
80  item->timestamp = gamepadEvent->timestamp;
81 
82  for( i = 0; i < item->naxes; i++) {
83  item->axis[i] = gamepadEvent->axis[i];
84  }
85 
86  for( i = 0; i < item->nbuttons; i++) {
87  item->analogButton[i] = gamepadEvent->analogButton[i];
88  item->digitalButton[i] = gamepadEvent->digitalButton[i];
89  }
90 
91  if (SDL_joylist_tail == NULL) {
92  SDL_joylist = SDL_joylist_tail = item;
93  } else {
94  SDL_joylist_tail->next = item;
95  SDL_joylist_tail = item;
96  }
97 
98  ++numjoysticks;
99 
100  SDL_PrivateJoystickAdded(item->device_instance);
101 
102 #ifdef DEBUG_JOYSTICK
103  SDL_Log("Number of joysticks is %d", numjoysticks);
104 #endif
105 
106 #ifdef DEBUG_JOYSTICK
107  SDL_Log("Added joystick with index %d", item->index);
108 #endif
109 
110  return 1;
111 }
112 
113 static EM_BOOL
114 Emscripten_JoyStickDisconnected(int eventType, const EmscriptenGamepadEvent *gamepadEvent, void *userData)
115 {
116  SDL_joylist_item *item = SDL_joylist;
117  SDL_joylist_item *prev = NULL;
118 
119  while (item != NULL) {
120  if (item->index == gamepadEvent->index) {
121  break;
122  }
123  prev = item;
124  item = item->next;
125  }
126 
127  if (item == NULL) {
128  return 1;
129  }
130 
131  if (item->joystick) {
132  item->joystick->hwdata = NULL;
133  }
134 
135  if (prev != NULL) {
136  prev->next = item->next;
137  } else {
138  SDL_assert(SDL_joylist == item);
139  SDL_joylist = item->next;
140  }
141  if (item == SDL_joylist_tail) {
142  SDL_joylist_tail = prev;
143  }
144 
145  /* Need to decrement the joystick count before we post the event */
146  --numjoysticks;
147 
148  SDL_PrivateJoystickRemoved(item->device_instance);
149 
150 #ifdef DEBUG_JOYSTICK
151  SDL_Log("Removed joystick with id %d", item->device_instance);
152 #endif
153  SDL_free(item->name);
154  SDL_free(item->mapping);
155  SDL_free(item);
156  return 1;
157 }
158 
159 /* Function to perform any system-specific joystick related cleanup */
160 static void
161 EMSCRIPTEN_JoystickQuit(void)
162 {
163  SDL_joylist_item *item = NULL;
164  SDL_joylist_item *next = NULL;
165 
166  for (item = SDL_joylist; item; item = next) {
167  next = item->next;
168  SDL_free(item->mapping);
169  SDL_free(item->name);
170  SDL_free(item);
171  }
172 
173  SDL_joylist = SDL_joylist_tail = NULL;
174 
175  numjoysticks = 0;
176  instance_counter = 0;
177 
178  emscripten_set_gamepadconnected_callback(NULL, 0, NULL);
179  emscripten_set_gamepaddisconnected_callback(NULL, 0, NULL);
180 }
181 
182 /* Function to scan the system for joysticks.
183  * It should return 0, or -1 on an unrecoverable fatal error.
184  */
185 static int
186 EMSCRIPTEN_JoystickInit(void)
187 {
188  int retval, i, numjs;
189  EmscriptenGamepadEvent gamepadState;
190 
191  numjoysticks = 0;
192 
193  retval = emscripten_sample_gamepad_data();
194 
195  /* Check if gamepad is supported by browser */
196  if (retval == EMSCRIPTEN_RESULT_NOT_SUPPORTED) {
197  return SDL_SetError("Gamepads not supported");
198  }
199 
200  numjs = emscripten_get_num_gamepads();
201 
202  /* handle already connected gamepads */
203  if (numjs > 0) {
204  for(i = 0; i < numjs; i++) {
205  retval = emscripten_get_gamepad_status(i, &gamepadState);
206  if (retval == EMSCRIPTEN_RESULT_SUCCESS) {
207  Emscripten_JoyStickConnected(EMSCRIPTEN_EVENT_GAMEPADCONNECTED,
208  &gamepadState,
209  NULL);
210  }
211  }
212  }
213 
214  retval = emscripten_set_gamepadconnected_callback(NULL,
215  0,
216  Emscripten_JoyStickConnected);
217 
218  if(retval != EMSCRIPTEN_RESULT_SUCCESS) {
219  EMSCRIPTEN_JoystickQuit();
220  return SDL_SetError("Could not set gamepad connect callback");
221  }
222 
223  retval = emscripten_set_gamepaddisconnected_callback(NULL,
224  0,
225  Emscripten_JoyStickDisconnected);
226  if(retval != EMSCRIPTEN_RESULT_SUCCESS) {
227  EMSCRIPTEN_JoystickQuit();
228  return SDL_SetError("Could not set gamepad disconnect callback");
229  }
230 
231  return 0;
232 }
233 
234 /* Returns item matching given SDL device index. */
235 static SDL_joylist_item *
236 JoystickByDeviceIndex(int device_index)
237 {
238  SDL_joylist_item *item = SDL_joylist;
239 
240  while (0 < device_index) {
241  --device_index;
242  item = item->next;
243  }
244 
245  return item;
246 }
247 
248 /* Returns item matching given HTML gamepad index. */
249 static SDL_joylist_item *
250 JoystickByIndex(int index)
251 {
252  SDL_joylist_item *item = SDL_joylist;
253 
254  if (index < 0) {
255  return NULL;
256  }
257 
258  while (item != NULL) {
259  if (item->index == index) {
260  break;
261  }
262  item = item->next;
263  }
264 
265  return item;
266 }
267 
268 static int
269 EMSCRIPTEN_JoystickGetCount(void)
270 {
271  return numjoysticks;
272 }
273 
274 static void
275 EMSCRIPTEN_JoystickDetect(void)
276 {
277 }
278 
279 static const char *
280 EMSCRIPTEN_JoystickGetDeviceName(int device_index)
281 {
282  return JoystickByDeviceIndex(device_index)->name;
283 }
284 
285 static int
286 EMSCRIPTEN_JoystickGetDevicePlayerIndex(int device_index)
287 {
288  return -1;
289 }
290 
291 static void
292 EMSCRIPTEN_JoystickSetDevicePlayerIndex(int device_index, int player_index)
293 {
294 }
295 
296 static SDL_JoystickID
297 EMSCRIPTEN_JoystickGetDeviceInstanceID(int device_index)
298 {
299  return JoystickByDeviceIndex(device_index)->device_instance;
300 }
301 
302 /* Function to open a joystick for use.
303  The joystick to open is specified by the device index.
304  This should fill the nbuttons and naxes fields of the joystick structure.
305  It returns 0, or -1 if there is an error.
306  */
307 static int
308 EMSCRIPTEN_JoystickOpen(SDL_Joystick * joystick, int device_index)
309 {
310  SDL_joylist_item *item = JoystickByDeviceIndex(device_index);
311 
312  if (item == NULL ) {
313  return SDL_SetError("No such device");
314  }
315 
316  if (item->joystick != NULL) {
317  return SDL_SetError("Joystick already opened");
318  }
319 
320  joystick->instance_id = item->device_instance;
321  joystick->hwdata = (struct joystick_hwdata *) item;
322  item->joystick = joystick;
323 
324  /* HTML5 Gamepad API doesn't say anything about these */
325  joystick->nhats = 0;
326  joystick->nballs = 0;
327 
328  joystick->nbuttons = item->nbuttons;
329  joystick->naxes = item->naxes;
330 
331  return (0);
332 }
333 
334 /* Function to update the state of a joystick - called as a device poll.
335  * This function shouldn't update the joystick structure directly,
336  * but instead should call SDL_PrivateJoystick*() to deliver events
337  * and update joystick device state.
338  */
339 static void
340 EMSCRIPTEN_JoystickUpdate(SDL_Joystick * joystick)
341 {
342  EmscriptenGamepadEvent gamepadState;
343  SDL_joylist_item *item = (SDL_joylist_item *) joystick->hwdata;
344  int i, result, buttonState;
345 
346  emscripten_sample_gamepad_data();
347 
348  if (item) {
349  result = emscripten_get_gamepad_status(item->index, &gamepadState);
350  if( result == EMSCRIPTEN_RESULT_SUCCESS) {
351  if(gamepadState.timestamp == 0 || gamepadState.timestamp != item->timestamp) {
352  for(i = 0; i < item->nbuttons; i++) {
353  if(item->digitalButton[i] != gamepadState.digitalButton[i]) {
354  buttonState = gamepadState.digitalButton[i]? SDL_PRESSED: SDL_RELEASED;
355  SDL_PrivateJoystickButton(item->joystick, i, buttonState);
356  }
357 
358  /* store values to compare them in the next update */
359  item->analogButton[i] = gamepadState.analogButton[i];
360  item->digitalButton[i] = gamepadState.digitalButton[i];
361  }
362 
363  for(i = 0; i < item->naxes; i++) {
364  if(item->axis[i] != gamepadState.axis[i]) {
365  /* do we need to do conversion? */
366  SDL_PrivateJoystickAxis(item->joystick, i,
367  (Sint16) (32767.*gamepadState.axis[i]));
368  }
369 
370  /* store to compare in next update */
371  item->axis[i] = gamepadState.axis[i];
372  }
373 
374  item->timestamp = gamepadState.timestamp;
375  }
376  }
377  }
378 }
379 
380 /* Function to close a joystick after use */
381 static void
382 EMSCRIPTEN_JoystickClose(SDL_Joystick * joystick)
383 {
384  SDL_joylist_item *item = (SDL_joylist_item *) joystick->hwdata;
385  if (item) {
386  item->joystick = NULL;
387  }
388 }
389 
390 static SDL_JoystickGUID
391 EMSCRIPTEN_JoystickGetDeviceGUID(int device_index)
392 {
394  /* the GUID is just the first 16 chars of the name for now */
395  const char *name = EMSCRIPTEN_JoystickGetDeviceName(device_index);
396  SDL_zero(guid);
397  SDL_memcpy(&guid, name, SDL_min(sizeof(guid), SDL_strlen(name)));
398  return guid;
399 }
400 
401 static int
402 EMSCRIPTEN_JoystickRumble(SDL_Joystick * joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble)
403 {
404  return SDL_Unsupported();
405 }
406 
408 {
409  EMSCRIPTEN_JoystickInit,
410  EMSCRIPTEN_JoystickGetCount,
411  EMSCRIPTEN_JoystickDetect,
412  EMSCRIPTEN_JoystickGetDeviceName,
413  EMSCRIPTEN_JoystickGetDevicePlayerIndex,
414  EMSCRIPTEN_JoystickSetDevicePlayerIndex,
415  EMSCRIPTEN_JoystickGetDeviceGUID,
416  EMSCRIPTEN_JoystickGetDeviceInstanceID,
417  EMSCRIPTEN_JoystickOpen,
418  EMSCRIPTEN_JoystickRumble,
419  EMSCRIPTEN_JoystickUpdate,
420  EMSCRIPTEN_JoystickClose,
421  EMSCRIPTEN_JoystickQuit,
422 };
423 
424 #endif /* SDL_JOYSTICK_EMSCRIPTEN */
425 
426 /* vi: set ts=4 sw=4 expandtab: */
SDL_zero
#define SDL_zero(x)
Definition: SDL_stdinc.h:418
SDL_events.h
joystick_hwdata::guid
SDL_JoystickGUID guid
Definition: SDL_sysjoystick_c.h:71
Uint16
uint16_t Uint16
Definition: SDL_stdinc.h:191
if
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 &reg2 endm macro vzip8 reg2 vzip d d &reg2 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 skip1(dst_w_bpp<=(lowbit *8)) &&((lowbit *8)<(pixblock_size *dst_w_bpp)) .if lowbit< 16 tst DST_R
Definition: pixman-arm-neon-asm.h:469
numjoysticks
static int numjoysticks
Definition: SDL_sysjoystick.m:83
NULL
#define NULL
Definition: begin_code.h:167
SDL_timer.h
SDL_joystick.h
SDL_JoystickID
Sint32 SDL_JoystickID
Definition: SDL_joystick.h:81
SDL_error.h
SDL_zerop
#define SDL_zerop(x)
Definition: SDL_stdinc.h:419
SDL_log.h
SDL_PrivateJoystickAdded
void SDL_PrivateJoystickAdded(SDL_JoystickID device_instance)
Definition: SDL_joystick.c:921
joystick_hwdata::joystick
SDL_Joystick * joystick
Definition: SDL_sysjoystick_c.h:42
index
GLuint index
Definition: SDL_opengl_glext.h:663
SDL_RELEASED
#define SDL_RELEASED
Definition: SDL_events.h:49
result
GLuint64EXT * result
Definition: SDL_opengl_glext.h:9435
SDL_PrivateJoystickRemoved
void SDL_PrivateJoystickRemoved(SDL_JoystickID device_instance)
Definition: SDL_joystick.c:987
SDL_PrivateJoystickAxis
int SDL_PrivateJoystickAxis(SDL_Joystick *joystick, Uint8 axis, Sint16 value)
Definition: SDL_joystick.c:1023
SDL_PRESSED
#define SDL_PRESSED
Definition: SDL_events.h:50
Sint16
int16_t Sint16
Definition: SDL_stdinc.h:185
SDL_memcpy
#define SDL_memcpy
Definition: SDL_dynapi_overrides.h:387
retval
SDL_bool retval
Definition: testgamecontroller.c:65
SDL_Log
#define SDL_Log
Definition: SDL_dynapi_overrides.h:31
SDL_free
#define SDL_free
Definition: SDL_dynapi_overrides.h:377
name
GLuint const GLchar * name
Definition: SDL_opengl_glext.h:663
SDL_PrivateJoystickButton
int SDL_PrivateJoystickButton(SDL_Joystick *joystick, Uint8 button, Uint8 state)
Definition: SDL_joystick.c:1162
SDL_assert.h
SDL_min
#define SDL_min(x, y)
Definition: SDL_stdinc.h:406
SDL_assert
#define SDL_assert(condition)
Definition: SDL_assert.h:169
SDL_JoystickDriver
Definition: SDL_sysjoystick.h:89
SDL_SetError
#define SDL_SetError
Definition: SDL_dynapi_overrides.h:30
joystick_hwdata::item
struct SDL_joylist_item * item
Definition: SDL_sysjoystick_c.h:33
SDL_strdup
#define SDL_strdup
Definition: SDL_dynapi_overrides.h:397
SDL_strlen
#define SDL_strlen
Definition: SDL_dynapi_overrides.h:393
SDL_Unsupported
#define SDL_Unsupported()
Definition: SDL_error.h:53
SDL_malloc
#define SDL_malloc
Definition: SDL_dynapi_overrides.h:374
SDL_JoystickGUID
Definition: SDL_joystick.h:70
SDL_EMSCRIPTEN_JoystickDriver
SDL_JoystickDriver SDL_EMSCRIPTEN_JoystickDriver
i
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)
Definition: SDL_x11sym.h:50
joystick_hwdata
Definition: SDL_sysjoystick_c.h:47