src/joystick/android/SDL_sysjoystick.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 15 Feb 2013 08:47:44 -0800
changeset 6885 700f1b25f77f
parent 6744 4ac2bb6dc80c
child 8460 f483e8bdae3c
permissions -rw-r--r--
Happy New Year!
paul@4721
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@6885
     3
  Copyright (C) 1997-2013 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
{
slouken@5000
    46
    return (1);
paul@4721
    47
}
paul@4721
    48
slouken@6707
    49
int SDL_SYS_NumJoysticks()
slouken@6707
    50
{
slouken@6707
    51
    return 1;
slouken@6707
    52
}
slouken@6707
    53
slouken@6707
    54
void SDL_SYS_JoystickDetect()
slouken@6707
    55
{
slouken@6707
    56
}
slouken@6707
    57
slouken@6707
    58
SDL_bool SDL_SYS_JoystickNeedsPolling()
slouken@6707
    59
{
slouken@6707
    60
    return SDL_FALSE;
slouken@6707
    61
}
slouken@6707
    62
paul@4721
    63
/* Function to get the device-dependent name of a joystick */
paul@4721
    64
const char *
slouken@6707
    65
SDL_SYS_JoystickNameForDeviceIndex(int device_index)
paul@4721
    66
{
slouken@6707
    67
    return accelerometerName;
slouken@6707
    68
}
slouken@6707
    69
slouken@6707
    70
/* Function to perform the mapping from device index to the instance id for this index */
slouken@6707
    71
SDL_JoystickID SDL_SYS_GetInstanceIdOfDeviceIndex(int device_index)
slouken@6707
    72
{
slouken@6707
    73
    return device_index;
paul@4721
    74
}
paul@4721
    75
paul@4721
    76
/* Function to open a joystick for use.
paul@4721
    77
   The joystick to open is specified by the index field of the joystick.
paul@4721
    78
   This should fill the nbuttons and naxes fields of the joystick structure.
paul@4721
    79
   It returns 0, or -1 if there is an error.
paul@4721
    80
 */
paul@4721
    81
int
slouken@6696
    82
SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index)
paul@4721
    83
{
slouken@6696
    84
    if (device_index == 0) {
slouken@6696
    85
        joystick->nbuttons = 0;
slouken@6696
    86
        joystick->nhats = 0;
slouken@6696
    87
        joystick->nballs = 0;
slouken@6696
    88
        joystick->naxes = 3;
slouken@6696
    89
        return 0;
slouken@6696
    90
	} else {
slouken@6696
    91
		SDL_SetError("No joystick available with that index");
slouken@6696
    92
		return (-1);
slouken@6696
    93
	}
paul@4721
    94
}
paul@4721
    95
slouken@6707
    96
/* Function to determine is this joystick is attached to the system right now */
slouken@6707
    97
SDL_bool SDL_SYS_JoystickAttached(SDL_Joystick *joystick)
slouken@6707
    98
{
slouken@6707
    99
    return SDL_TRUE;
slouken@6707
   100
}
paul@4721
   101
paul@4721
   102
/* Function to update the state of a joystick - called as a device poll.
paul@4721
   103
 * This function shouldn't update the joystick structure directly,
paul@4721
   104
 * but instead should call SDL_PrivateJoystick*() to deliver events
paul@4721
   105
 * and update joystick device state.
paul@4721
   106
 */
slouken@5000
   107
void
paul@4721
   108
SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
paul@4721
   109
{
slouken@5000
   110
    int i;
slouken@6212
   111
    Sint16 value;
slouken@5000
   112
    float values[3];
slouken@5000
   113
slouken@6212
   114
    if (Android_JNI_GetAccelerometerValues(values)) {
slouken@6212
   115
        for ( i = 0; i < 3; i++ ) {
slouken@6212
   116
            value = (Sint16)(values[i] * 32767.0f);
slouken@6212
   117
            SDL_PrivateJoystickAxis(joystick, i, value);
slouken@6212
   118
        }
paul@4721
   119
    }
paul@4721
   120
}
paul@4721
   121
paul@4721
   122
/* Function to close a joystick after use */
paul@4721
   123
void
paul@4721
   124
SDL_SYS_JoystickClose(SDL_Joystick * joystick)
paul@4721
   125
{
paul@4721
   126
}
paul@4721
   127
paul@4721
   128
/* Function to perform any system-specific joystick related cleanup */
paul@4721
   129
void
paul@4721
   130
SDL_SYS_JoystickQuit(void)
paul@4721
   131
{
paul@4721
   132
}
paul@4721
   133
slouken@6744
   134
SDL_JoystickGUID SDL_SYS_JoystickGetDeviceGUID( int device_index )
slouken@6695
   135
{
slouken@6744
   136
    SDL_JoystickGUID guid;
slouken@6695
   137
    // the GUID is just the first 16 chars of the name for now
slouken@6707
   138
    const char *name = SDL_SYS_JoystickNameForDeviceIndex( device_index );
slouken@6695
   139
    SDL_zero( guid );
slouken@6695
   140
    SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) );
slouken@6695
   141
    return guid;
slouken@6695
   142
}
slouken@6695
   143
slouken@6744
   144
SDL_JoystickGUID SDL_SYS_JoystickGetGUID(SDL_Joystick * joystick)
slouken@6695
   145
{
slouken@6744
   146
    SDL_JoystickGUID guid;
slouken@6695
   147
    // the GUID is just the first 16 chars of the name for now
slouken@6695
   148
    const char *name = joystick->name;
slouken@6695
   149
    SDL_zero( guid );
slouken@6695
   150
    SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) );
slouken@6695
   151
    return guid;
slouken@6695
   152
}
slouken@6695
   153
slouken@6707
   154
#endif /* SDL_JOYSTICK_ANDROID */
slouken@5000
   155
slouken@5000
   156
/* vi: set ts=4 sw=4 expandtab: */