src/joystick/android/SDL_sysjoystick.c
changeset 7041 eb0b541c9722
child 7191 75360622e65f
child 8460 f483e8bdae3c
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/joystick/android/SDL_sysjoystick.c	Mon Apr 01 13:21:23 2013 -0300
     1.3 @@ -0,0 +1,156 @@
     1.4 +/*
     1.5 +  Simple DirectMedia Layer
     1.6 +  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
     1.7 +
     1.8 +  This software is provided 'as-is', without any express or implied
     1.9 +  warranty.  In no event will the authors be held liable for any damages
    1.10 +  arising from the use of this software.
    1.11 +
    1.12 +  Permission is granted to anyone to use this software for any purpose,
    1.13 +  including commercial applications, and to alter it and redistribute it
    1.14 +  freely, subject to the following restrictions:
    1.15 +
    1.16 +  1. The origin of this software must not be misrepresented; you must not
    1.17 +     claim that you wrote the original software. If you use this software
    1.18 +     in a product, an acknowledgment in the product documentation would be
    1.19 +     appreciated but is not required.
    1.20 +  2. Altered source versions must be plainly marked as such, and must not be
    1.21 +     misrepresented as being the original software.
    1.22 +  3. This notice may not be removed or altered from any source distribution.
    1.23 +*/
    1.24 +
    1.25 +#include "SDL_config.h"
    1.26 +
    1.27 +#ifdef SDL_JOYSTICK_ANDROID
    1.28 +
    1.29 +/* This is the system specific header for the SDL joystick API */
    1.30 +#include <stdio.h>              /* For the definition of NULL */
    1.31 +
    1.32 +#include "SDL_error.h"
    1.33 +#include "SDL_events.h"
    1.34 +#include "SDL_joystick.h"
    1.35 +#include "../SDL_sysjoystick.h"
    1.36 +#include "../SDL_joystick_c.h"
    1.37 +#include "../../core/android/SDL_android.h"
    1.38 +
    1.39 +static const char *accelerometerName = "Android accelerometer";
    1.40 +
    1.41 +/* Function to scan the system for joysticks.
    1.42 + * This function should set SDL_numjoysticks to the number of available
    1.43 + * joysticks.  Joystick 0 should be the system default joystick.
    1.44 + * It should return 0, or -1 on an unrecoverable fatal error.
    1.45 + */
    1.46 +int
    1.47 +SDL_SYS_JoystickInit(void)
    1.48 +{
    1.49 +    return (1);
    1.50 +}
    1.51 +
    1.52 +int SDL_SYS_NumJoysticks()
    1.53 +{
    1.54 +    return 1;
    1.55 +}
    1.56 +
    1.57 +void SDL_SYS_JoystickDetect()
    1.58 +{
    1.59 +}
    1.60 +
    1.61 +SDL_bool SDL_SYS_JoystickNeedsPolling()
    1.62 +{
    1.63 +    return SDL_FALSE;
    1.64 +}
    1.65 +
    1.66 +/* Function to get the device-dependent name of a joystick */
    1.67 +const char *
    1.68 +SDL_SYS_JoystickNameForDeviceIndex(int device_index)
    1.69 +{
    1.70 +    return accelerometerName;
    1.71 +}
    1.72 +
    1.73 +/* Function to perform the mapping from device index to the instance id for this index */
    1.74 +SDL_JoystickID SDL_SYS_GetInstanceIdOfDeviceIndex(int device_index)
    1.75 +{
    1.76 +    return device_index;
    1.77 +}
    1.78 +
    1.79 +/* Function to open a joystick for use.
    1.80 +   The joystick to open is specified by the index field of the joystick.
    1.81 +   This should fill the nbuttons and naxes fields of the joystick structure.
    1.82 +   It returns 0, or -1 if there is an error.
    1.83 + */
    1.84 +int
    1.85 +SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index)
    1.86 +{
    1.87 +    if (device_index == 0) {
    1.88 +        joystick->nbuttons = 0;
    1.89 +        joystick->nhats = 0;
    1.90 +        joystick->nballs = 0;
    1.91 +        joystick->naxes = 3;
    1.92 +        return 0;
    1.93 +	} else {
    1.94 +		SDL_SetError("No joystick available with that index");
    1.95 +		return (-1);
    1.96 +	}
    1.97 +}
    1.98 +
    1.99 +/* Function to determine is this joystick is attached to the system right now */
   1.100 +SDL_bool SDL_SYS_JoystickAttached(SDL_Joystick *joystick)
   1.101 +{
   1.102 +    return SDL_TRUE;
   1.103 +}
   1.104 +
   1.105 +/* Function to update the state of a joystick - called as a device poll.
   1.106 + * This function shouldn't update the joystick structure directly,
   1.107 + * but instead should call SDL_PrivateJoystick*() to deliver events
   1.108 + * and update joystick device state.
   1.109 + */
   1.110 +void
   1.111 +SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
   1.112 +{
   1.113 +    int i;
   1.114 +    Sint16 value;
   1.115 +    float values[3];
   1.116 +
   1.117 +    if (Android_JNI_GetAccelerometerValues(values)) {
   1.118 +        for ( i = 0; i < 3; i++ ) {
   1.119 +            value = (Sint16)(values[i] * 32767.0f);
   1.120 +            SDL_PrivateJoystickAxis(joystick, i, value);
   1.121 +        }
   1.122 +    }
   1.123 +}
   1.124 +
   1.125 +/* Function to close a joystick after use */
   1.126 +void
   1.127 +SDL_SYS_JoystickClose(SDL_Joystick * joystick)
   1.128 +{
   1.129 +}
   1.130 +
   1.131 +/* Function to perform any system-specific joystick related cleanup */
   1.132 +void
   1.133 +SDL_SYS_JoystickQuit(void)
   1.134 +{
   1.135 +}
   1.136 +
   1.137 +SDL_JoystickGUID SDL_SYS_JoystickGetDeviceGUID( int device_index )
   1.138 +{
   1.139 +    SDL_JoystickGUID guid;
   1.140 +    // the GUID is just the first 16 chars of the name for now
   1.141 +    const char *name = SDL_SYS_JoystickNameForDeviceIndex( device_index );
   1.142 +    SDL_zero( guid );
   1.143 +    SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) );
   1.144 +    return guid;
   1.145 +}
   1.146 +
   1.147 +SDL_JoystickGUID SDL_SYS_JoystickGetGUID(SDL_Joystick * joystick)
   1.148 +{
   1.149 +    SDL_JoystickGUID guid;
   1.150 +    // the GUID is just the first 16 chars of the name for now
   1.151 +    const char *name = joystick->name;
   1.152 +    SDL_zero( guid );
   1.153 +    SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) );
   1.154 +    return guid;
   1.155 +}
   1.156 +
   1.157 +#endif /* SDL_JOYSTICK_ANDROID */
   1.158 +
   1.159 +/* vi: set ts=4 sw=4 expandtab: */