src/joystick/android/SDL_sysjoystick.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 11 Feb 2011 22:37:15 -0800
changeset 5262 b530ef003506
parent 5090 327f181542f1
child 5535 96594ac5fd1a
permissions -rw-r--r--
Happy 2011! :)
paul@4721
     1
/*
paul@4721
     2
    SDL - Simple DirectMedia Layer
slouken@5262
     3
    Copyright (C) 1997-2011 Sam Lantinga
paul@4721
     4
paul@4721
     5
    This library is free software; you can redistribute it and/or
paul@4721
     6
    modify it under the terms of the GNU Lesser General Public
paul@4721
     7
    License as published by the Free Software Foundation; either
paul@4721
     8
    version 2.1 of the License, or (at your option) any later version.
paul@4721
     9
paul@4721
    10
    This library is distributed in the hope that it will be useful,
paul@4721
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
paul@4721
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
paul@4721
    13
    Lesser General Public License for more details.
paul@4721
    14
paul@4721
    15
    You should have received a copy of the GNU Lesser General Public
paul@4721
    16
    License along with this library; if not, write to the Free Software
paul@4721
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
paul@4721
    18
paul@4721
    19
    Sam Lantinga
paul@4721
    20
    slouken@libsdl.org
paul@4721
    21
*/
paul@4721
    22
paul@4721
    23
#include "SDL_config.h"
paul@4721
    24
paul@4721
    25
#ifdef SDL_JOYSTICK_ANDROID
paul@4721
    26
paul@4721
    27
/* This is the system specific header for the SDL joystick API */
paul@4721
    28
#include <stdio.h>              /* For the definition of NULL */
paul@4721
    29
paul@4721
    30
#include "SDL_error.h"
paul@4721
    31
#include "SDL_events.h"
paul@4721
    32
#include "SDL_joystick.h"
paul@4721
    33
#include "../SDL_sysjoystick.h"
paul@4721
    34
#include "../SDL_joystick_c.h"
slouken@5090
    35
#include "../../core/android/SDL_android.h"
paul@4721
    36
slouken@5000
    37
static const char *accelerometerName = "Android accelerometer";
paul@4721
    38
paul@4721
    39
/* Function to scan the system for joysticks.
paul@4721
    40
 * This function should set SDL_numjoysticks to the number of available
paul@4721
    41
 * joysticks.  Joystick 0 should be the system default joystick.
paul@4721
    42
 * It should return 0, or -1 on an unrecoverable fatal error.
paul@4721
    43
 */
paul@4721
    44
int
paul@4721
    45
SDL_SYS_JoystickInit(void)
paul@4721
    46
{
paul@4721
    47
    SDL_numjoysticks = 1;
paul@4721
    48
    
slouken@5000
    49
    return (1);
paul@4721
    50
}
paul@4721
    51
paul@4721
    52
/* Function to get the device-dependent name of a joystick */
paul@4721
    53
const char *
paul@4721
    54
SDL_SYS_JoystickName(int index)
paul@4721
    55
{
slouken@5000
    56
    if (index == 0) {
paul@4721
    57
        return accelerometerName;
slouken@5000
    58
    } else {
slouken@5000
    59
        SDL_SetError("No joystick available with that index");
slouken@5000
    60
        return (NULL);
slouken@5000
    61
    }
paul@4721
    62
}
paul@4721
    63
paul@4721
    64
/* Function to open a joystick for use.
paul@4721
    65
   The joystick to open is specified by the index field of the joystick.
paul@4721
    66
   This should fill the nbuttons and naxes fields of the joystick structure.
paul@4721
    67
   It returns 0, or -1 if there is an error.
paul@4721
    68
 */
paul@4721
    69
int
paul@4721
    70
SDL_SYS_JoystickOpen(SDL_Joystick * joystick)
paul@4721
    71
{
paul@4721
    72
    joystick->nbuttons = 0;
paul@4721
    73
    joystick->nhats = 0;
paul@4721
    74
    joystick->nballs = 0;
paul@4721
    75
    joystick->naxes = 3;
paul@4721
    76
    joystick->name = accelerometerName;
paul@4721
    77
    return 0;
paul@4721
    78
}
paul@4721
    79
paul@4721
    80
paul@4721
    81
/* Function to update the state of a joystick - called as a device poll.
paul@4721
    82
 * This function shouldn't update the joystick structure directly,
paul@4721
    83
 * but instead should call SDL_PrivateJoystick*() to deliver events
paul@4721
    84
 * and update joystick device state.
paul@4721
    85
 */
slouken@5000
    86
void
paul@4721
    87
SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
paul@4721
    88
{
slouken@5000
    89
    int i;
slouken@5000
    90
    float values[3];
slouken@5000
    91
slouken@5000
    92
    Android_JNI_GetAccelerometerValues(values);
slouken@5000
    93
slouken@5000
    94
    for ( i = 0; i < 3; i++ ) {
slouken@5000
    95
        SDL_PrivateJoystickAxis(joystick, i, values[i]);
paul@4721
    96
    }
paul@4721
    97
}
paul@4721
    98
paul@4721
    99
/* Function to close a joystick after use */
paul@4721
   100
void
paul@4721
   101
SDL_SYS_JoystickClose(SDL_Joystick * joystick)
paul@4721
   102
{
paul@4721
   103
}
paul@4721
   104
paul@4721
   105
/* Function to perform any system-specific joystick related cleanup */
paul@4721
   106
void
paul@4721
   107
SDL_SYS_JoystickQuit(void)
paul@4721
   108
{
paul@4721
   109
}
paul@4721
   110
paul@4721
   111
#endif /* SDL_JOYSTICK_NDS */
slouken@5000
   112
slouken@5000
   113
/* vi: set ts=4 sw=4 expandtab: */