src/joystick/android/SDL_sysjoystick.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 31 Dec 2011 09:28:07 -0500
changeset 6138 4c64952a58fb
parent 5535 96594ac5fd1a
child 6212 78d854de3a66
permissions -rwxr-xr-x
Happy New Year!
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
{
paul@4721
    46
    SDL_numjoysticks = 1;
paul@4721
    47
    
slouken@5000
    48
    return (1);
paul@4721
    49
}
paul@4721
    50
paul@4721
    51
/* Function to get the device-dependent name of a joystick */
paul@4721
    52
const char *
paul@4721
    53
SDL_SYS_JoystickName(int index)
paul@4721
    54
{
slouken@5000
    55
    if (index == 0) {
paul@4721
    56
        return accelerometerName;
slouken@5000
    57
    } else {
slouken@5000
    58
        SDL_SetError("No joystick available with that index");
slouken@5000
    59
        return (NULL);
slouken@5000
    60
    }
paul@4721
    61
}
paul@4721
    62
paul@4721
    63
/* Function to open a joystick for use.
paul@4721
    64
   The joystick to open is specified by the index field of the joystick.
paul@4721
    65
   This should fill the nbuttons and naxes fields of the joystick structure.
paul@4721
    66
   It returns 0, or -1 if there is an error.
paul@4721
    67
 */
paul@4721
    68
int
paul@4721
    69
SDL_SYS_JoystickOpen(SDL_Joystick * joystick)
paul@4721
    70
{
paul@4721
    71
    joystick->nbuttons = 0;
paul@4721
    72
    joystick->nhats = 0;
paul@4721
    73
    joystick->nballs = 0;
paul@4721
    74
    joystick->naxes = 3;
paul@4721
    75
    joystick->name = accelerometerName;
paul@4721
    76
    return 0;
paul@4721
    77
}
paul@4721
    78
paul@4721
    79
paul@4721
    80
/* Function to update the state of a joystick - called as a device poll.
paul@4721
    81
 * This function shouldn't update the joystick structure directly,
paul@4721
    82
 * but instead should call SDL_PrivateJoystick*() to deliver events
paul@4721
    83
 * and update joystick device state.
paul@4721
    84
 */
slouken@5000
    85
void
paul@4721
    86
SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
paul@4721
    87
{
slouken@5000
    88
    int i;
slouken@5000
    89
    float values[3];
slouken@5000
    90
slouken@5000
    91
    Android_JNI_GetAccelerometerValues(values);
slouken@5000
    92
slouken@5000
    93
    for ( i = 0; i < 3; i++ ) {
slouken@5000
    94
        SDL_PrivateJoystickAxis(joystick, i, values[i]);
paul@4721
    95
    }
paul@4721
    96
}
paul@4721
    97
paul@4721
    98
/* Function to close a joystick after use */
paul@4721
    99
void
paul@4721
   100
SDL_SYS_JoystickClose(SDL_Joystick * joystick)
paul@4721
   101
{
paul@4721
   102
}
paul@4721
   103
paul@4721
   104
/* Function to perform any system-specific joystick related cleanup */
paul@4721
   105
void
paul@4721
   106
SDL_SYS_JoystickQuit(void)
paul@4721
   107
{
paul@4721
   108
}
paul@4721
   109
paul@4721
   110
#endif /* SDL_JOYSTICK_NDS */
slouken@5000
   111
slouken@5000
   112
/* vi: set ts=4 sw=4 expandtab: */