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