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