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