src/joystick/android/SDL_sysjoystick.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 13 Jan 2012 20:57:35 -0500
changeset 6212 78d854de3a66
parent 6138 4c64952a58fb
child 6695 ec21e81c62aa
permissions -rwxr-xr-x
Fixed bug 1368 - Enabling joystick subsystem cause an infinite loop

morgan.devel@gmail.com 2012-01-13 00:32:23 PST

The android version of SDL_SYS_JoystickUpdate doesn't check if there is
actually new data and always generate the SDL_JOYAXISMOTION event.
Consequently, doing a while(SDL_PollEvent()) will result in an endless loop.

The attached patch fix this issue.

It also scale the incoming values properly in the Sint16 range. The scale from
[-gravity;+gravity] is done directly in the java part because one may want to
map the sensor values with a non-linear method for example.
     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     SDL_numjoysticks = 1;
    47     
    48     return (1);
    49 }
    50 
    51 /* Function to get the device-dependent name of a joystick */
    52 const char *
    53 SDL_SYS_JoystickName(int index)
    54 {
    55     if (index == 0) {
    56         return accelerometerName;
    57     } else {
    58         SDL_SetError("No joystick available with that index");
    59         return (NULL);
    60     }
    61 }
    62 
    63 /* Function to open a joystick for use.
    64    The joystick to open is specified by the index field of the joystick.
    65    This should fill the nbuttons and naxes fields of the joystick structure.
    66    It returns 0, or -1 if there is an error.
    67  */
    68 int
    69 SDL_SYS_JoystickOpen(SDL_Joystick * joystick)
    70 {
    71     joystick->nbuttons = 0;
    72     joystick->nhats = 0;
    73     joystick->nballs = 0;
    74     joystick->naxes = 3;
    75     joystick->name = accelerometerName;
    76     return 0;
    77 }
    78 
    79 
    80 /* Function to update the state of a joystick - called as a device poll.
    81  * This function shouldn't update the joystick structure directly,
    82  * but instead should call SDL_PrivateJoystick*() to deliver events
    83  * and update joystick device state.
    84  */
    85 void
    86 SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
    87 {
    88     int i;
    89     Sint16 value;
    90     float values[3];
    91 
    92     if (Android_JNI_GetAccelerometerValues(values)) {
    93         for ( i = 0; i < 3; i++ ) {
    94             value = (Sint16)(values[i] * 32767.0f);
    95             SDL_PrivateJoystickAxis(joystick, i, value);
    96         }
    97     }
    98 }
    99 
   100 /* Function to close a joystick after use */
   101 void
   102 SDL_SYS_JoystickClose(SDL_Joystick * joystick)
   103 {
   104 }
   105 
   106 /* Function to perform any system-specific joystick related cleanup */
   107 void
   108 SDL_SYS_JoystickQuit(void)
   109 {
   110 }
   111 
   112 #endif /* SDL_JOYSTICK_NDS */
   113 
   114 /* vi: set ts=4 sw=4 expandtab: */