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