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