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