src/joystick/android/SDL_sysjoystick.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 26 Nov 2012 22:02:01 -0800
changeset 6695 ec21e81c62aa
parent 6212 78d854de3a66
child 6696 73e2ad636f6e
permissions -rw-r--r--
Fixed building joystick code on Android
paul@4721
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@6138
     3
  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
paul@4721
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
paul@4721
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
paul@4721
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
paul@4721
    20
*/
paul@4721
    21
paul@4721
    22
#include "SDL_config.h"
paul@4721
    23
paul@4721
    24
#ifdef SDL_JOYSTICK_ANDROID
paul@4721
    25
paul@4721
    26
/* This is the system specific header for the SDL joystick API */
paul@4721
    27
#include <stdio.h>              /* For the definition of NULL */
paul@4721
    28
paul@4721
    29
#include "SDL_error.h"
paul@4721
    30
#include "SDL_events.h"
paul@4721
    31
#include "SDL_joystick.h"
paul@4721
    32
#include "../SDL_sysjoystick.h"
paul@4721
    33
#include "../SDL_joystick_c.h"
slouken@5090
    34
#include "../../core/android/SDL_android.h"
paul@4721
    35
slouken@5000
    36
static const char *accelerometerName = "Android accelerometer";
paul@4721
    37
paul@4721
    38
/* Function to scan the system for joysticks.
paul@4721
    39
 * This function should set SDL_numjoysticks to the number of available
paul@4721
    40
 * joysticks.  Joystick 0 should be the system default joystick.
paul@4721
    41
 * It should return 0, or -1 on an unrecoverable fatal error.
paul@4721
    42
 */
paul@4721
    43
int
paul@4721
    44
SDL_SYS_JoystickInit(void)
paul@4721
    45
{
slouken@5000
    46
    return (1);
paul@4721
    47
}
paul@4721
    48
paul@4721
    49
/* Function to get the device-dependent name of a joystick */
paul@4721
    50
const char *
slouken@6695
    51
SDL_SYS_JoystickNameForIndex(int index)
paul@4721
    52
{
slouken@5000
    53
    if (index == 0) {
paul@4721
    54
        return accelerometerName;
slouken@5000
    55
    } else {
slouken@5000
    56
        SDL_SetError("No joystick available with that index");
slouken@5000
    57
        return (NULL);
slouken@5000
    58
    }
paul@4721
    59
}
paul@4721
    60
paul@4721
    61
/* Function to open a joystick for use.
paul@4721
    62
   The joystick to open is specified by the index field of the joystick.
paul@4721
    63
   This should fill the nbuttons and naxes fields of the joystick structure.
paul@4721
    64
   It returns 0, or -1 if there is an error.
paul@4721
    65
 */
paul@4721
    66
int
slouken@6695
    67
SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int index)
paul@4721
    68
{
paul@4721
    69
    joystick->nbuttons = 0;
paul@4721
    70
    joystick->nhats = 0;
paul@4721
    71
    joystick->nballs = 0;
paul@4721
    72
    joystick->naxes = 3;
paul@4721
    73
    return 0;
paul@4721
    74
}
paul@4721
    75
paul@4721
    76
paul@4721
    77
/* Function to update the state of a joystick - called as a device poll.
paul@4721
    78
 * This function shouldn't update the joystick structure directly,
paul@4721
    79
 * but instead should call SDL_PrivateJoystick*() to deliver events
paul@4721
    80
 * and update joystick device state.
paul@4721
    81
 */
slouken@5000
    82
void
paul@4721
    83
SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
paul@4721
    84
{
slouken@5000
    85
    int i;
slouken@6212
    86
    Sint16 value;
slouken@5000
    87
    float values[3];
slouken@5000
    88
slouken@6212
    89
    if (Android_JNI_GetAccelerometerValues(values)) {
slouken@6212
    90
        for ( i = 0; i < 3; i++ ) {
slouken@6212
    91
            value = (Sint16)(values[i] * 32767.0f);
slouken@6212
    92
            SDL_PrivateJoystickAxis(joystick, i, value);
slouken@6212
    93
        }
paul@4721
    94
    }
paul@4721
    95
}
paul@4721
    96
paul@4721
    97
/* Function to close a joystick after use */
paul@4721
    98
void
paul@4721
    99
SDL_SYS_JoystickClose(SDL_Joystick * joystick)
paul@4721
   100
{
paul@4721
   101
}
paul@4721
   102
paul@4721
   103
/* Function to perform any system-specific joystick related cleanup */
paul@4721
   104
void
paul@4721
   105
SDL_SYS_JoystickQuit(void)
paul@4721
   106
{
paul@4721
   107
}
paul@4721
   108
slouken@6695
   109
/* Function to perform the mapping from device index to the instance id for this index */
slouken@6695
   110
SDL_JoystickID SDL_SYS_GetInstanceIdOfDeviceIndex(int index)
slouken@6695
   111
{
slouken@6695
   112
    return index;
slouken@6695
   113
}
slouken@6695
   114
slouken@6695
   115
/* Function to determine is this joystick is attached to the system right now */
slouken@6695
   116
int SDL_SYS_JoystickAttached(SDL_Joystick *joystick)
slouken@6695
   117
{
slouken@6695
   118
    return 1;
slouken@6695
   119
}
slouken@6695
   120
slouken@6695
   121
int SDL_SYS_NumJoysticks()
slouken@6695
   122
{
slouken@6695
   123
    return 1;
slouken@6695
   124
}
slouken@6695
   125
slouken@6695
   126
int SDL_SYS_JoystickNeedsPolling()
slouken@6695
   127
{
slouken@6695
   128
    return 0;
slouken@6695
   129
}
slouken@6695
   130
slouken@6695
   131
void SDL_SYS_JoystickDetect()
slouken@6695
   132
{
slouken@6695
   133
}
slouken@6695
   134
slouken@6695
   135
JoystickGUID SDL_SYS_PrivateJoystickGetDeviceGUID( int device_index )
slouken@6695
   136
{
slouken@6695
   137
    JoystickGUID guid;
slouken@6695
   138
    // the GUID is just the first 16 chars of the name for now
slouken@6695
   139
    const char *name = SDL_SYS_JoystickNameForIndex( device_index );
slouken@6695
   140
    SDL_zero( guid );
slouken@6695
   141
    SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) );
slouken@6695
   142
    return guid;
slouken@6695
   143
}
slouken@6695
   144
slouken@6695
   145
JoystickGUID SDL_SYS_PrivateJoystickGetGUID(SDL_Joystick * joystick)
slouken@6695
   146
{
slouken@6695
   147
    JoystickGUID guid;
slouken@6695
   148
    // the GUID is just the first 16 chars of the name for now
slouken@6695
   149
    const char *name = joystick->name;
slouken@6695
   150
    SDL_zero( guid );
slouken@6695
   151
    SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) );
slouken@6695
   152
    return guid;
slouken@6695
   153
}
slouken@6695
   154
paul@4721
   155
#endif /* SDL_JOYSTICK_NDS */
slouken@5000
   156
slouken@5000
   157
/* vi: set ts=4 sw=4 expandtab: */