src/joystick/android/SDL_sysjoystick.c
author Edward Rudd <urkle@outoforder.cc>
Wed, 26 Sep 2012 14:08:46 -0400
changeset 6462 5e09ac1aba80
parent 6212 78d854de3a66
child 6695 ec21e81c62aa
permissions -rwxr-xr-x
allocate *Hint structures per Xlib docs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4721
7bb9d3a3f257 Added accelerometer 'joystick'
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
     1
/*
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
     2
  Simple DirectMedia Layer
6138
4c64952a58fb Happy New Year!
Sam Lantinga <slouken@libsdl.org>
parents: 5535
diff changeset
     3
  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
4721
7bb9d3a3f257 Added accelerometer 'joystick'
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
     4
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
     5
  This software is provided 'as-is', without any express or implied
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
     6
  warranty.  In no event will the authors be held liable for any damages
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
     7
  arising from the use of this software.
4721
7bb9d3a3f257 Added accelerometer 'joystick'
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
     8
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
     9
  Permission is granted to anyone to use this software for any purpose,
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    10
  including commercial applications, and to alter it and redistribute it
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    11
  freely, subject to the following restrictions:
4721
7bb9d3a3f257 Added accelerometer 'joystick'
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
    12
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    13
  1. The origin of this software must not be misrepresented; you must not
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    14
     claim that you wrote the original software. If you use this software
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    15
     in a product, an acknowledgment in the product documentation would be
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    16
     appreciated but is not required.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    17
  2. Altered source versions must be plainly marked as such, and must not be
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    18
     misrepresented as being the original software.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    19
  3. This notice may not be removed or altered from any source distribution.
4721
7bb9d3a3f257 Added accelerometer 'joystick'
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
    20
*/
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
#include "SDL_config.h"
7bb9d3a3f257 Added accelerometer 'joystick'
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
    23
7bb9d3a3f257 Added accelerometer 'joystick'
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
    24
#ifdef SDL_JOYSTICK_ANDROID
7bb9d3a3f257 Added accelerometer 'joystick'
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
    25
7bb9d3a3f257 Added accelerometer 'joystick'
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
    26
/* This is the system specific header for the SDL joystick API */
7bb9d3a3f257 Added accelerometer 'joystick'
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
    27
#include <stdio.h>              /* For the definition of NULL */
7bb9d3a3f257 Added accelerometer 'joystick'
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
    28
7bb9d3a3f257 Added accelerometer 'joystick'
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
    29
#include "SDL_error.h"
7bb9d3a3f257 Added accelerometer 'joystick'
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
    30
#include "SDL_events.h"
7bb9d3a3f257 Added accelerometer 'joystick'
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
    31
#include "SDL_joystick.h"
7bb9d3a3f257 Added accelerometer 'joystick'
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
    32
#include "../SDL_sysjoystick.h"
7bb9d3a3f257 Added accelerometer 'joystick'
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
    33
#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
    34
#include "../../core/android/SDL_android.h"
4721
7bb9d3a3f257 Added accelerometer 'joystick'
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
    35
5000
6a10693e66c3 Cleaned up internal accelerometer interface
Sam Lantinga <slouken@libsdl.org>
parents: 4721
diff changeset
    36
static const char *accelerometerName = "Android accelerometer";
4721
7bb9d3a3f257 Added accelerometer 'joystick'
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
    37
7bb9d3a3f257 Added accelerometer 'joystick'
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
    38
/* Function to scan the system for joysticks.
7bb9d3a3f257 Added accelerometer 'joystick'
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
    39
 * This function should set SDL_numjoysticks to the number of available
7bb9d3a3f257 Added accelerometer 'joystick'
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
    40
 * joysticks.  Joystick 0 should be the system default joystick.
7bb9d3a3f257 Added accelerometer 'joystick'
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
    41
 * It should return 0, or -1 on an unrecoverable fatal error.
7bb9d3a3f257 Added accelerometer 'joystick'
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
    42
 */
7bb9d3a3f257 Added accelerometer 'joystick'
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
    43
int
7bb9d3a3f257 Added accelerometer 'joystick'
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
    44
SDL_SYS_JoystickInit(void)
7bb9d3a3f257 Added accelerometer 'joystick'
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
    45
{
7bb9d3a3f257 Added accelerometer 'joystick'
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
    46
    SDL_numjoysticks = 1;
7bb9d3a3f257 Added accelerometer 'joystick'
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
    47
    
5000
6a10693e66c3 Cleaned up internal accelerometer interface
Sam Lantinga <slouken@libsdl.org>
parents: 4721
diff changeset
    48
    return (1);
4721
7bb9d3a3f257 Added accelerometer 'joystick'
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
    49
}
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
/* Function to get the device-dependent name of a joystick */
7bb9d3a3f257 Added accelerometer 'joystick'
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
    52
const char *
7bb9d3a3f257 Added accelerometer 'joystick'
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
    53
SDL_SYS_JoystickName(int index)
7bb9d3a3f257 Added accelerometer 'joystick'
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
    54
{
5000
6a10693e66c3 Cleaned up internal accelerometer interface
Sam Lantinga <slouken@libsdl.org>
parents: 4721
diff changeset
    55
    if (index == 0) {
4721
7bb9d3a3f257 Added accelerometer 'joystick'
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
    56
        return accelerometerName;
5000
6a10693e66c3 Cleaned up internal accelerometer interface
Sam Lantinga <slouken@libsdl.org>
parents: 4721
diff changeset
    57
    } else {
6a10693e66c3 Cleaned up internal accelerometer interface
Sam Lantinga <slouken@libsdl.org>
parents: 4721
diff changeset
    58
        SDL_SetError("No joystick available with that index");
6a10693e66c3 Cleaned up internal accelerometer interface
Sam Lantinga <slouken@libsdl.org>
parents: 4721
diff changeset
    59
        return (NULL);
6a10693e66c3 Cleaned up internal accelerometer interface
Sam Lantinga <slouken@libsdl.org>
parents: 4721
diff changeset
    60
    }
4721
7bb9d3a3f257 Added accelerometer 'joystick'
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
    61
}
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
/* Function to open a joystick for use.
7bb9d3a3f257 Added accelerometer 'joystick'
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
    64
   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
    65
   This should fill the nbuttons and naxes fields of the joystick structure.
7bb9d3a3f257 Added accelerometer 'joystick'
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
    66
   It returns 0, or -1 if there is an error.
7bb9d3a3f257 Added accelerometer 'joystick'
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
    67
 */
7bb9d3a3f257 Added accelerometer 'joystick'
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
    68
int
7bb9d3a3f257 Added accelerometer 'joystick'
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
    69
SDL_SYS_JoystickOpen(SDL_Joystick * joystick)
7bb9d3a3f257 Added accelerometer 'joystick'
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
    70
{
7bb9d3a3f257 Added accelerometer 'joystick'
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
    71
    joystick->nbuttons = 0;
7bb9d3a3f257 Added accelerometer 'joystick'
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
    72
    joystick->nhats = 0;
7bb9d3a3f257 Added accelerometer 'joystick'
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
    73
    joystick->nballs = 0;
7bb9d3a3f257 Added accelerometer 'joystick'
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
    74
    joystick->naxes = 3;
7bb9d3a3f257 Added accelerometer 'joystick'
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
    75
    joystick->name = accelerometerName;
7bb9d3a3f257 Added accelerometer 'joystick'
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
    76
    return 0;
7bb9d3a3f257 Added accelerometer 'joystick'
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
    77
}
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
/* 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
    81
 * This function shouldn't update the joystick structure directly,
7bb9d3a3f257 Added accelerometer 'joystick'
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
    82
 * but instead should call SDL_PrivateJoystick*() to deliver events
7bb9d3a3f257 Added accelerometer 'joystick'
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
    83
 * and update joystick device state.
7bb9d3a3f257 Added accelerometer 'joystick'
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
    84
 */
5000
6a10693e66c3 Cleaned up internal accelerometer interface
Sam Lantinga <slouken@libsdl.org>
parents: 4721
diff changeset
    85
void
4721
7bb9d3a3f257 Added accelerometer 'joystick'
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
    86
SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
7bb9d3a3f257 Added accelerometer 'joystick'
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
    87
{
5000
6a10693e66c3 Cleaned up internal accelerometer interface
Sam Lantinga <slouken@libsdl.org>
parents: 4721
diff changeset
    88
    int i;
6212
78d854de3a66 Fixed bug 1368 - Enabling joystick subsystem cause an infinite loop
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
    89
    Sint16 value;
5000
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
6212
78d854de3a66 Fixed bug 1368 - Enabling joystick subsystem cause an infinite loop
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
    92
    if (Android_JNI_GetAccelerometerValues(values)) {
78d854de3a66 Fixed bug 1368 - Enabling joystick subsystem cause an infinite loop
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
    93
        for ( i = 0; i < 3; i++ ) {
78d854de3a66 Fixed bug 1368 - Enabling joystick subsystem cause an infinite loop
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
    94
            value = (Sint16)(values[i] * 32767.0f);
78d854de3a66 Fixed bug 1368 - Enabling joystick subsystem cause an infinite loop
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
    95
            SDL_PrivateJoystickAxis(joystick, i, value);
78d854de3a66 Fixed bug 1368 - Enabling joystick subsystem cause an infinite loop
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
    96
        }
4721
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
7bb9d3a3f257 Added accelerometer 'joystick'
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
   100
/* Function to close a joystick after use */
7bb9d3a3f257 Added accelerometer 'joystick'
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
   101
void
7bb9d3a3f257 Added accelerometer 'joystick'
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
   102
SDL_SYS_JoystickClose(SDL_Joystick * joystick)
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
7bb9d3a3f257 Added accelerometer 'joystick'
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
   106
/* Function to perform any system-specific joystick related cleanup */
7bb9d3a3f257 Added accelerometer 'joystick'
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
   107
void
7bb9d3a3f257 Added accelerometer 'joystick'
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
   108
SDL_SYS_JoystickQuit(void)
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
7bb9d3a3f257 Added accelerometer 'joystick'
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
   112
#endif /* SDL_JOYSTICK_NDS */
5000
6a10693e66c3 Cleaned up internal accelerometer interface
Sam Lantinga <slouken@libsdl.org>
parents: 4721
diff changeset
   113
6a10693e66c3 Cleaned up internal accelerometer interface
Sam Lantinga <slouken@libsdl.org>
parents: 4721
diff changeset
   114
/* vi: set ts=4 sw=4 expandtab: */