src/joystick/emscripten/SDL_sysjoystick.c
changeset 10226 cb13d22b7f09
parent 10224 d1c27f616999
child 10617 346c02ff71b6
equal deleted inserted replaced
10225:3134026517cb 10226:cb13d22b7f09
    25 
    25 
    26 #include <stdio.h>              /* For the definition of NULL */
    26 #include <stdio.h>              /* For the definition of NULL */
    27 #include "SDL_error.h"
    27 #include "SDL_error.h"
    28 #include "SDL_events.h"
    28 #include "SDL_events.h"
    29 
    29 
    30 #if !SDL_EVENTS_DISABLED
       
    31 #include "../../events/SDL_events_c.h"
       
    32 #endif
       
    33 
       
    34 #include "SDL_joystick.h"
    30 #include "SDL_joystick.h"
    35 #include "SDL_hints.h"
    31 #include "SDL_hints.h"
    36 #include "SDL_assert.h"
    32 #include "SDL_assert.h"
    37 #include "SDL_timer.h"
    33 #include "SDL_timer.h"
    38 #include "SDL_log.h"
    34 #include "SDL_log.h"
    55 
    51 
    56     if (JoystickByIndex(gamepadEvent->index) != NULL) {
    52     if (JoystickByIndex(gamepadEvent->index) != NULL) {
    57       return 1;
    53       return 1;
    58     }
    54     }
    59 
    55 
    60 #if !SDL_EVENTS_DISABLED
       
    61     SDL_Event event;
       
    62 #endif
       
    63 
       
    64     item = (SDL_joylist_item *) SDL_malloc(sizeof (SDL_joylist_item));
    56     item = (SDL_joylist_item *) SDL_malloc(sizeof (SDL_joylist_item));
    65     if (item == NULL) {
    57     if (item == NULL) {
    66         return 1;
    58         return 1;
    67     }
    59     }
    68 
    60 
   103         SDL_joylist_tail->next = item;
    95         SDL_joylist_tail->next = item;
   104         SDL_joylist_tail = item;
    96         SDL_joylist_tail = item;
   105     }
    97     }
   106 
    98 
   107     ++numjoysticks;
    99     ++numjoysticks;
       
   100 
       
   101     SDL_PrivateJoystickAdded(numjoysticks - 1);
       
   102 
   108 #ifdef DEBUG_JOYSTICK
   103 #ifdef DEBUG_JOYSTICK
   109     SDL_Log("Number of joysticks is %d", numjoysticks);
   104     SDL_Log("Number of joysticks is %d", numjoysticks);
   110 #endif
   105 #endif
   111 #if !SDL_EVENTS_DISABLED
       
   112     event.type = SDL_JOYDEVICEADDED;
       
   113 
       
   114     if (SDL_GetEventState(event.type) == SDL_ENABLE) {
       
   115         event.jdevice.which = numjoysticks - 1;
       
   116         if ( (SDL_EventOK == NULL) ||
       
   117              (*SDL_EventOK) (SDL_EventOKParam, &event) ) {
       
   118             SDL_PushEvent(&event);
       
   119         }
       
   120     }
       
   121 #endif /* !SDL_EVENTS_DISABLED */
       
   122 
   106 
   123 #ifdef DEBUG_JOYSTICK
   107 #ifdef DEBUG_JOYSTICK
   124     SDL_Log("Added joystick with index %d", item->index);
   108     SDL_Log("Added joystick with index %d", item->index);
   125 #endif
   109 #endif
   126 
   110 
   130 EM_BOOL
   114 EM_BOOL
   131 Emscripten_JoyStickDisconnected(int eventType, const EmscriptenGamepadEvent *gamepadEvent, void *userData)
   115 Emscripten_JoyStickDisconnected(int eventType, const EmscriptenGamepadEvent *gamepadEvent, void *userData)
   132 {
   116 {
   133     SDL_joylist_item *item = SDL_joylist;
   117     SDL_joylist_item *item = SDL_joylist;
   134     SDL_joylist_item *prev = NULL;
   118     SDL_joylist_item *prev = NULL;
   135 #if !SDL_EVENTS_DISABLED
       
   136     SDL_Event event;
       
   137 #endif
       
   138 
   119 
   139     while (item != NULL) {
   120     while (item != NULL) {
   140         if (item->index == gamepadEvent->index) {
   121         if (item->index == gamepadEvent->index) {
   141             break;
   122             break;
   142         }
   123         }
   163     }
   144     }
   164 
   145 
   165     /* Need to decrement the joystick count before we post the event */
   146     /* Need to decrement the joystick count before we post the event */
   166     --numjoysticks;
   147     --numjoysticks;
   167 
   148 
   168 #if !SDL_EVENTS_DISABLED
   149 	SDL_PrivateJoystickRemoved(item->device_instance);
   169     event.type = SDL_JOYDEVICEREMOVED;
       
   170 
       
   171     if (SDL_GetEventState(event.type) == SDL_ENABLE) {
       
   172         event.jdevice.which = item->device_instance;
       
   173         if ( (SDL_EventOK == NULL) ||
       
   174              (*SDL_EventOK) (SDL_EventOKParam, &event) ) {
       
   175             SDL_PushEvent(&event);
       
   176         }
       
   177     }
       
   178 #endif /* !SDL_EVENTS_DISABLED */
       
   179 
   150 
   180 #ifdef DEBUG_JOYSTICK
   151 #ifdef DEBUG_JOYSTICK
   181     SDL_Log("Removed joystick with id %d", item->device_instance);
   152     SDL_Log("Removed joystick with id %d", item->device_instance);
   182 #endif
   153 #endif
   183     SDL_free(item->name);
   154     SDL_free(item->name);