[Android] Hotplugging support for joysticks
authorGabriel Jacobo <gabomdq@gmail.com>
Tue, 10 Dec 2013 16:24:11 -0300
changeset 8057801d84e26f91
parent 8056 abd9434c5d3e
child 8058 8e4512b74223
[Android] Hotplugging support for joysticks
WhatsNew.txt
android-project/src/org/libsdl/app/SDLActivity.java
src/core/android/SDL_android.c
src/core/android/SDL_android.h
src/joystick/android/SDL_sysjoystick.c
src/joystick/android/SDL_sysjoystick.h
src/joystick/android/SDL_sysjoystick_c.h
     1.1 --- a/WhatsNew.txt	Mon Dec 09 15:17:20 2013 -0500
     1.2 +++ b/WhatsNew.txt	Tue Dec 10 16:24:11 2013 -0300
     1.3 @@ -16,6 +16,7 @@
     1.4  * Joystick support (minimum SDK version required to build SDL is now 12, 
     1.5    the required runtime version remains at 10, but on such devices joystick 
     1.6    support won't be available).
     1.7 +* Hotplugging support for joysticks
     1.8  
     1.9  Linux:
    1.10  * Fixed fullscreen and focused behavior when receiving NotifyGrab events
     2.1 --- a/android-project/src/org/libsdl/app/SDLActivity.java	Mon Dec 09 15:17:20 2013 -0500
     2.2 +++ b/android-project/src/org/libsdl/app/SDLActivity.java	Tue Dec 10 16:24:11 2013 -0300
     2.3 @@ -256,9 +256,9 @@
     2.4      public static native void nativePause();
     2.5      public static native void nativeResume();
     2.6      public static native void onNativeResize(int x, int y, int format);
     2.7 -    public static native int onNativePadDown(int padId, int keycode);
     2.8 -    public static native int onNativePadUp(int padId, int keycode);
     2.9 -    public static native void onNativeJoy(int joyId, int axis,
    2.10 +    public static native int onNativePadDown(int device_id, int keycode);
    2.11 +    public static native int onNativePadUp(int device_id, int keycode);
    2.12 +    public static native void onNativeJoy(int device_id, int axis,
    2.13                                            float value);
    2.14      public static native void onNativeKeyDown(int keycode);
    2.15      public static native void onNativeKeyUp(int keycode);
    2.16 @@ -270,6 +270,10 @@
    2.17      public static native void onNativeSurfaceChanged();
    2.18      public static native void onNativeSurfaceDestroyed();
    2.19      public static native void nativeFlipBuffers();
    2.20 +    public static native int nativeAddJoystick(int device_id, String name, 
    2.21 +                                               int is_accelerometer, int nbuttons, 
    2.22 +                                               int naxes, int nhats, int nballs);
    2.23 +    public static native int nativeRemoveJoystick(int device_id);
    2.24  
    2.25      public static void flipBuffers() {
    2.26          SDLActivity.nativeFlipBuffers();
    2.27 @@ -460,29 +464,16 @@
    2.28      }
    2.29              
    2.30      // Joystick glue code, just a series of stubs that redirect to the SDLJoystickHandler instance
    2.31 -    public static int getNumJoysticks() {
    2.32 -        return mJoystickHandler.getNumJoysticks();
    2.33 -    }
    2.34 -    
    2.35 -    public static String getJoystickName(int joy) {
    2.36 -        return mJoystickHandler.getJoystickName(joy);
    2.37 -    }
    2.38 -    
    2.39 -    public static int getJoystickAxes(int joy) {
    2.40 -        return mJoystickHandler.getJoystickAxes(joy);
    2.41 -    }
    2.42 -    
    2.43      public static boolean handleJoystickMotionEvent(MotionEvent event) {
    2.44          return mJoystickHandler.handleMotionEvent(event);
    2.45      }
    2.46      
    2.47 -    /**
    2.48 -     * @param devId the device id to get opened joystick id for.
    2.49 -     * @return joystick id for device id or -1 if there is none.
    2.50 -     */
    2.51 -    public static int getJoyId(int devId) {
    2.52 -        return mJoystickHandler.getJoyId(devId);
    2.53 +    public static void pollInputDevices() {
    2.54 +        if (SDLActivity.mSDLThread != null) {
    2.55 +            mJoystickHandler.pollInputDevices();
    2.56 +        }
    2.57      }
    2.58 +    
    2.59  }
    2.60  
    2.61  /**
    2.62 @@ -660,16 +651,13 @@
    2.63          
    2.64          if ( (event.getSource() & 0x00000401) != 0 || /* API 12: SOURCE_GAMEPAD */
    2.65                     (event.getSource() & InputDevice.SOURCE_DPAD) != 0 ) {
    2.66 -            int id = SDLActivity.getJoyId( event.getDeviceId() );
    2.67 -            if (id != -1) {
    2.68 -                if (event.getAction() == KeyEvent.ACTION_DOWN) {
    2.69 -                    if (SDLActivity.onNativePadDown(id, keyCode) == 0) {
    2.70 -                        return true;
    2.71 -                    }
    2.72 -                } else if (event.getAction() == KeyEvent.ACTION_UP) {
    2.73 -                    if (SDLActivity.onNativePadUp(id, keyCode) == 0) {
    2.74 -                        return true;
    2.75 -                    }
    2.76 +            if (event.getAction() == KeyEvent.ACTION_DOWN) {
    2.77 +                if (SDLActivity.onNativePadDown(event.getDeviceId(), keyCode) == 0) {
    2.78 +                    return true;
    2.79 +                }
    2.80 +            } else if (event.getAction() == KeyEvent.ACTION_UP) {
    2.81 +                if (SDLActivity.onNativePadUp(event.getDeviceId(), keyCode) == 0) {
    2.82 +                    return true;
    2.83                  }
    2.84              }
    2.85          }
    2.86 @@ -916,36 +904,20 @@
    2.87  
    2.88  /* A null joystick handler for API level < 12 devices (the accelerometer is handled separately) */
    2.89  class SDLJoystickHandler {
    2.90 -    public int getNumJoysticks() {
    2.91 -        return 0;
    2.92 -    }
    2.93 -    
    2.94 -    public String getJoystickName(int joy) {
    2.95 -        return "";
    2.96 -    }
    2.97 -    
    2.98 -    public int getJoystickAxes(int joy) {
    2.99 -        return 0;
   2.100 -    }
   2.101 -    
   2.102 -    /**
   2.103 -     * @param devId the device id to get opened joystick id for.
   2.104 -     * @return joystick id for device id or -1 if there is none.
   2.105 -     */
   2.106 -    public int getJoyId(int devId) {
   2.107 -        return -1;
   2.108 -    }
   2.109      
   2.110      public boolean handleMotionEvent(MotionEvent event) {
   2.111          return false;
   2.112      }
   2.113 +    
   2.114 +    public void pollInputDevices() {
   2.115 +    }
   2.116  }
   2.117  
   2.118  /* Actual joystick functionality available for API >= 12 devices */
   2.119  class SDLJoystickHandler_API12 extends SDLJoystickHandler {
   2.120    
   2.121      class SDLJoystick {
   2.122 -        public int id;
   2.123 +        public int device_id;
   2.124          public String name;
   2.125          public ArrayList<InputDevice.MotionRange> axes;
   2.126      }
   2.127 @@ -953,54 +925,72 @@
   2.128      private ArrayList<SDLJoystick> mJoysticks;
   2.129      
   2.130      public SDLJoystickHandler_API12() {
   2.131 -        /* FIXME: Move the joystick initialization code to its own function and support hotplugging of devices */
   2.132         
   2.133          mJoysticks = new ArrayList<SDLJoystick>();
   2.134 +    }
   2.135 +
   2.136 +    @Override
   2.137 +    public void pollInputDevices() {
   2.138 +        int[] deviceIds = InputDevice.getDeviceIds();
   2.139 +        // It helps processing the device ids in reverse order
   2.140 +        // For example, in the case of the XBox 360 wireless dongle,
   2.141 +        // so the first controller seen by SDL matches what the receiver
   2.142 +        // considers to be the first controller
   2.143          
   2.144 -        int[] deviceIds = InputDevice.getDeviceIds();
   2.145 -        for(int i=0; i<deviceIds.length; i++) {
   2.146 -            SDLJoystick joystick = new SDLJoystick();
   2.147 -            InputDevice joystickDevice = InputDevice.getDevice(deviceIds[i]);
   2.148 -            
   2.149 -            if( (joystickDevice.getSources() & InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
   2.150 -                joystick.id = deviceIds[i];
   2.151 -                joystick.name = joystickDevice.getName();
   2.152 -                joystick.axes = new ArrayList<InputDevice.MotionRange>();
   2.153 -                
   2.154 -                for (InputDevice.MotionRange range : joystickDevice.getMotionRanges()) {
   2.155 -                     if ( (range.getSource() & InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
   2.156 -                        joystick.axes.add(range);
   2.157 -                     }
   2.158 +        for(int i=deviceIds.length-1; i>-1; i--) {
   2.159 +            SDLJoystick joystick = getJoystick(deviceIds[i]);
   2.160 +            if (joystick == null) {
   2.161 +                joystick = new SDLJoystick();
   2.162 +                InputDevice joystickDevice = InputDevice.getDevice(deviceIds[i]);
   2.163 +                if( (joystickDevice.getSources() & InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
   2.164 +                    joystick.device_id = deviceIds[i];
   2.165 +                    joystick.name = joystickDevice.getName();
   2.166 +                    joystick.axes = new ArrayList<InputDevice.MotionRange>();
   2.167 +                    
   2.168 +                    for (InputDevice.MotionRange range : joystickDevice.getMotionRanges()) {
   2.169 +                         if ( (range.getSource() & InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
   2.170 +                            joystick.axes.add(range);
   2.171 +                         }
   2.172 +                    }
   2.173 +                    
   2.174 +                    mJoysticks.add(joystick);
   2.175 +                    SDLActivity.nativeAddJoystick(joystick.device_id, joystick.name, 0, -1, joystick.axes.size(), 0, 0);
   2.176                  }
   2.177 -                
   2.178 -                mJoysticks.add(joystick);
   2.179              }
   2.180          }
   2.181 +        
   2.182 +        /* Check removed devices */
   2.183 +        ArrayList<Integer> removedDevices = new ArrayList<Integer>();
   2.184 +        for(int i=0; i < mJoysticks.size(); i++) {
   2.185 +            int device_id = mJoysticks.get(i).device_id;
   2.186 +            int j;
   2.187 +            for (j=0; j < deviceIds.length; j++) {
   2.188 +                if (device_id == deviceIds[j]) break;
   2.189 +            }
   2.190 +            if (j == deviceIds.length) {
   2.191 +                removedDevices.add(device_id);
   2.192 +            }
   2.193 +        }
   2.194 +            
   2.195 +        for(int i=0; i < removedDevices.size(); i++) {
   2.196 +            int device_id = removedDevices.get(i);
   2.197 +            SDLActivity.nativeRemoveJoystick(device_id);
   2.198 +            for (int j=0; j < mJoysticks.size(); j++) {
   2.199 +                if (mJoysticks.get(j).device_id == device_id) {
   2.200 +                    mJoysticks.remove(j);
   2.201 +                    break;
   2.202 +                }
   2.203 +            }
   2.204 +        }        
   2.205      }
   2.206      
   2.207 -    @Override
   2.208 -    public int getNumJoysticks() {
   2.209 -        return mJoysticks.size();
   2.210 -    }
   2.211 -    
   2.212 -    @Override
   2.213 -    public String getJoystickName(int joy) {
   2.214 -        return mJoysticks.get(joy).name;
   2.215 -    }
   2.216 -    
   2.217 -    @Override
   2.218 -    public int getJoystickAxes(int joy) {
   2.219 -        return mJoysticks.get(joy).axes.size();
   2.220 -    }
   2.221 -    
   2.222 -    @Override
   2.223 -    public int getJoyId(int devId) {
   2.224 +    protected SDLJoystick getJoystick(int device_id) {
   2.225          for(int i=0; i < mJoysticks.size(); i++) {
   2.226 -            if (mJoysticks.get(i).id == devId) {
   2.227 -                return i;
   2.228 +            if (mJoysticks.get(i).device_id == device_id) {
   2.229 +                return mJoysticks.get(i);
   2.230              }
   2.231          }
   2.232 -        return -1;
   2.233 +        return null;
   2.234      }   
   2.235      
   2.236      @Override        
   2.237 @@ -1010,14 +1000,13 @@
   2.238              int action = event.getActionMasked();
   2.239              switch(action) {
   2.240                  case MotionEvent.ACTION_MOVE:
   2.241 -                    int id = getJoyId( event.getDeviceId() );
   2.242 -                    if ( id != -1 ) {
   2.243 -                        SDLJoystick joystick = mJoysticks.get(id);
   2.244 +                    SDLJoystick joystick = getJoystick(event.getDeviceId());
   2.245 +                    if ( joystick != null ) {
   2.246                          for (int i = 0; i < joystick.axes.size(); i++) {
   2.247                              InputDevice.MotionRange range = joystick.axes.get(i);
   2.248                              /* Normalize the value to -1...1 */
   2.249                              float value = ( event.getAxisValue( range.getAxis(), actionPointerIndex) - range.getMin() ) / range.getRange() * 2.0f - 1.0f;
   2.250 -                            SDLActivity.onNativeJoy(id, i, value );
   2.251 +                            SDLActivity.onNativeJoy(joystick.device_id, i, value );
   2.252                          }                       
   2.253                      }
   2.254                      break;
     3.1 --- a/src/core/android/SDL_android.c	Mon Dec 09 15:17:20 2013 -0500
     3.2 +++ b/src/core/android/SDL_android.c	Tue Dec 10 16:24:11 2013 -0300
     3.3 @@ -34,7 +34,7 @@
     3.4  #include "../../video/android/SDL_androidtouch.h"
     3.5  #include "../../video/android/SDL_androidvideo.h"
     3.6  #include "../../video/android/SDL_androidwindow.h"
     3.7 -#include "../../joystick/android/SDL_sysjoystick.h"
     3.8 +#include "../../joystick/android/SDL_sysjoystick_c.h"
     3.9  
    3.10  #include <android/log.h>
    3.11  #include <pthread.h>
    3.12 @@ -75,6 +75,7 @@
    3.13  static jmethodID midAudioWriteShortBuffer;
    3.14  static jmethodID midAudioWriteByteBuffer;
    3.15  static jmethodID midAudioQuit;
    3.16 +static jmethodID midPollInputDevices;
    3.17  
    3.18  /* Accelerometer data storage */
    3.19  static float fLastAccelerometer[3];
    3.20 @@ -127,11 +128,13 @@
    3.21                                  "audioWriteByteBuffer", "([B)V");
    3.22      midAudioQuit = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,
    3.23                                  "audioQuit", "()V");
    3.24 +    midPollInputDevices = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,
    3.25 +                                "pollInputDevices", "()V");
    3.26  
    3.27      bHasNewData = false;
    3.28  
    3.29      if(!midGetNativeSurface || !midFlipBuffers || !midAudioInit ||
    3.30 -       !midAudioWriteShortBuffer || !midAudioWriteByteBuffer || !midAudioQuit) {
    3.31 +       !midAudioWriteShortBuffer || !midAudioWriteByteBuffer || !midAudioQuit || !midPollInputDevices) {
    3.32          __android_log_print(ANDROID_LOG_WARN, "SDL", "SDL: Couldn't locate Java callbacks, check that they're named and typed correctly");
    3.33      }
    3.34      __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL_Android_Init() finished!");
    3.35 @@ -148,25 +151,47 @@
    3.36  // Paddown
    3.37  int Java_org_libsdl_app_SDLActivity_onNativePadDown(
    3.38                                      JNIEnv* env, jclass jcls,
    3.39 -                                    jint padId, jint keycode)
    3.40 +                                    jint device_id, jint keycode)
    3.41  {
    3.42 -    return Android_OnPadDown(padId, keycode);
    3.43 +    return Android_OnPadDown(device_id, keycode);
    3.44  }
    3.45  
    3.46  // Padup
    3.47  int Java_org_libsdl_app_SDLActivity_onNativePadUp(
    3.48                                     JNIEnv* env, jclass jcls,
    3.49 -                                   jint padId, jint keycode)
    3.50 +                                   jint device_id, jint keycode)
    3.51  {
    3.52 -    return Android_OnPadUp(padId, keycode);
    3.53 +    return Android_OnPadUp(device_id, keycode);
    3.54  }
    3.55  
    3.56  /* Joy */
    3.57  void Java_org_libsdl_app_SDLActivity_onNativeJoy(
    3.58                                      JNIEnv* env, jclass jcls,
    3.59 -                                    jint joyId, jint axis, jfloat value)
    3.60 +                                    jint device_id, jint axis, jfloat value)
    3.61  {
    3.62 -    Android_OnJoy(joyId, axis, value);
    3.63 +    Android_OnJoy(device_id, axis, value);
    3.64 +}
    3.65 +
    3.66 +
    3.67 +int Java_org_libsdl_app_SDLActivity_nativeAddJoystick(
    3.68 +    JNIEnv* env, jclass jcls,
    3.69 +    jint device_id, jstring device_name, jint is_accelerometer, 
    3.70 +    jint nbuttons, jint naxes, jint nhats, jint nballs)
    3.71 +{
    3.72 +    int retval;
    3.73 +    const char *name = (*env)->GetStringUTFChars(env, device_name, NULL);
    3.74 +
    3.75 +    retval = Android_AddJoystick(device_id, name, (SDL_bool) is_accelerometer, nbuttons, naxes, nhats, nballs);
    3.76 +
    3.77 +    (*env)->ReleaseStringUTFChars(env, device_name, name);
    3.78 +    
    3.79 +    return retval;
    3.80 +}
    3.81 +
    3.82 +int Java_org_libsdl_app_SDLActivity_nativeRemoveJoystick(
    3.83 +    JNIEnv* env, jclass jcls, jint device_id)
    3.84 +{
    3.85 +    return Android_RemoveJoystick(device_id);
    3.86  }
    3.87  
    3.88  
    3.89 @@ -1247,62 +1272,12 @@
    3.90      return number;
    3.91  }
    3.92  
    3.93 -/* return the total number of plugged in joysticks */
    3.94 -int Android_JNI_GetNumJoysticks()
    3.95 +void Android_JNI_PollInputDevices()
    3.96  {
    3.97 -    JNIEnv* env = Android_JNI_GetEnv();
    3.98 -    if (!env) {
    3.99 -        return -1;
   3.100 -    }
   3.101 -    
   3.102 -    jmethodID mid = (*env)->GetStaticMethodID(env, mActivityClass, "getNumJoysticks", "()I");
   3.103 -    if (!mid) {
   3.104 -        return -1;
   3.105 -    }
   3.106 -    
   3.107 -    return (int)(*env)->CallStaticIntMethod(env, mActivityClass, mid);
   3.108 +    JNIEnv *env = Android_JNI_GetEnv();
   3.109 +    (*env)->CallStaticVoidMethod(env, mActivityClass, midPollInputDevices);    
   3.110  }
   3.111  
   3.112 -/* Return the name of joystick number "i" */
   3.113 -char* Android_JNI_GetJoystickName(int i)
   3.114 -{
   3.115 -    JNIEnv* env = Android_JNI_GetEnv();
   3.116 -    if (!env) {
   3.117 -        return SDL_strdup("");
   3.118 -    }
   3.119 -    
   3.120 -    jmethodID mid = (*env)->GetStaticMethodID(env, mActivityClass, "getJoystickName", "(I)Ljava/lang/String;");
   3.121 -    if (!mid) {
   3.122 -        return SDL_strdup("");
   3.123 -    }
   3.124 -    jstring string = (jstring)((*env)->CallStaticObjectMethod(env, mActivityClass, mid, i));
   3.125 -    const char* utf = (*env)->GetStringUTFChars(env, string, 0);
   3.126 -    if (!utf) {
   3.127 -        return SDL_strdup("");
   3.128 -    }
   3.129 -    
   3.130 -    char* text = SDL_strdup(utf);
   3.131 -    (*env)->ReleaseStringUTFChars(env, string, utf);
   3.132 -    return text;
   3.133 -}
   3.134 -
   3.135 -/* return the number of axes in the given joystick */
   3.136 -int Android_JNI_GetJoystickAxes(int joy)
   3.137 -{
   3.138 -    JNIEnv* env = Android_JNI_GetEnv();
   3.139 -    if (!env) {
   3.140 -        return -1;
   3.141 -    }
   3.142 -    
   3.143 -    jmethodID mid = (*env)->GetStaticMethodID(env, mActivityClass, "getJoystickAxes", "(I)I");
   3.144 -    if (!mid) {
   3.145 -        return -1;
   3.146 -    }
   3.147 -    
   3.148 -    return (int)(*env)->CallIntMethod(env, mActivityClass, mid, joy);
   3.149 -}
   3.150 -
   3.151 -
   3.152  /* sends message to be handled on the UI event dispatch thread */
   3.153  int Android_JNI_SendMessage(int command, int param)
   3.154  {
     4.1 --- a/src/core/android/SDL_android.h	Mon Dec 09 15:17:20 2013 -0500
     4.2 +++ b/src/core/android/SDL_android.h	Tue Dec 10 16:24:11 2013 -0300
     4.3 @@ -66,9 +66,8 @@
     4.4  int Android_JNI_GetPowerInfo(int* plugged, int* charged, int* battery, int* seconds, int* percent);
     4.5      
     4.6  /* Joystick support */
     4.7 -int Android_JNI_GetNumJoysticks();
     4.8 -char* Android_JNI_GetJoystickName(int i);
     4.9 -int Android_JNI_GetJoystickAxes(int joy);
    4.10 +void Android_JNI_PollInputDevices();
    4.11 +
    4.12  
    4.13  /* Touch support */
    4.14  int Android_JNI_GetTouchDeviceIds(int **ids);
     5.1 --- a/src/joystick/android/SDL_sysjoystick.c	Mon Dec 09 15:17:20 2013 -0500
     5.2 +++ b/src/joystick/android/SDL_sysjoystick.c	Tue Dec 10 16:24:11 2013 -0300
     5.3 @@ -23,15 +23,18 @@
     5.4  
     5.5  #ifdef SDL_JOYSTICK_ANDROID
     5.6  
     5.7 -/* This is the system specific header for the SDL joystick API */
     5.8  #include <stdio.h>              /* For the definition of NULL */
     5.9 -
    5.10  #include "SDL_error.h"
    5.11  #include "SDL_events.h"
    5.12 +
    5.13 +#if !SDL_EVENTS_DISABLED
    5.14 +#include "../../events/SDL_events_c.h"
    5.15 +#endif
    5.16 +
    5.17  #include "SDL_joystick.h"
    5.18  #include "SDL_hints.h"
    5.19  #include "SDL_assert.h"
    5.20 -#include "../SDL_sysjoystick.h"
    5.21 +#include "SDL_sysjoystick_c.h"
    5.22  #include "../SDL_joystick_c.h"
    5.23  #include "../../core/android/SDL_android.h"
    5.24  
    5.25 @@ -57,14 +60,17 @@
    5.26  #define AKEYCODE_BUTTON_16 203
    5.27  #endif
    5.28  
    5.29 -#define ANDROID_ACCELEROMETER_INDEX (SYS_numjoysticks - 1)
    5.30  #define ANDROID_ACCELEROMETER_NAME "Android Accelerometer"
    5.31 +#define ANDROID_ACCELEROMETER_DEVICE_ID INT_MIN
    5.32  #define ANDROID_MAX_NBUTTONS 36
    5.33  
    5.34 -static SDL_Joystick **SYS_Joysticks;
    5.35 -static char **SYS_JoystickNames;
    5.36 -static int SYS_numjoysticks;
    5.37 -static SDL_bool SYS_accelAsJoy;
    5.38 +static SDL_joylist_item * JoystickByDeviceId(int device_id);
    5.39 +
    5.40 +static SDL_joylist_item *SDL_joylist = NULL;
    5.41 +static SDL_joylist_item *SDL_joylist_tail = NULL;
    5.42 +static int numjoysticks = 0;
    5.43 +static int instance_counter = 0;
    5.44 +
    5.45  
    5.46  /* Function to convert Android keyCodes into SDL ones.
    5.47   * This code manipulation is done to get a sequential list of codes.
    5.48 @@ -139,78 +145,265 @@
    5.49      
    5.50  }
    5.51  
    5.52 -/* Function to scan the system for joysticks.
    5.53 - * This function should set SDL_numjoysticks to the number of available
    5.54 - * joysticks.  Joystick 0 should be the system default joystick.
    5.55 - * It should return 0, or -1 on an unrecoverable fatal error.
    5.56 - */
    5.57 +int
    5.58 +Android_OnPadDown(int device_id, int keycode)
    5.59 +{
    5.60 +    SDL_joylist_item *item;
    5.61 +    int button = keycode_to_SDL(keycode);
    5.62 +    if (button >= 0) {
    5.63 +        item = JoystickByDeviceId(device_id);
    5.64 +        if (item && item->joystick) {
    5.65 +            SDL_PrivateJoystickButton(item->joystick, button , SDL_PRESSED);
    5.66 +        }
    5.67 +        return 0;
    5.68 +    }
    5.69 +    
    5.70 +    return -1;
    5.71 +}
    5.72 +
    5.73 +int
    5.74 +Android_OnPadUp(int device_id, int keycode)
    5.75 +{
    5.76 +    SDL_joylist_item *item;
    5.77 +    int button = keycode_to_SDL(keycode);
    5.78 +    if (button >= 0) {
    5.79 +        item = JoystickByDeviceId(device_id);
    5.80 +        if (item && item->joystick) {
    5.81 +            SDL_PrivateJoystickButton(item->joystick, button, SDL_RELEASED);
    5.82 +        }
    5.83 +        return 0;
    5.84 +    }
    5.85 +    
    5.86 +    return -1;
    5.87 +}
    5.88 +
    5.89 +int
    5.90 +Android_OnJoy(int device_id, int axis, float value)
    5.91 +{
    5.92 +    /* Android gives joy info normalized as [-1.0, 1.0] or [0.0, 1.0] */
    5.93 +    SDL_joylist_item *item = JoystickByDeviceId(device_id);
    5.94 +    if (item && item->joystick) {
    5.95 +        SDL_PrivateJoystickAxis(item->joystick, axis, (Sint16) (32767.*value) );
    5.96 +    }
    5.97 +    
    5.98 +    return 0;
    5.99 +}
   5.100 +
   5.101 +
   5.102 +int
   5.103 +Android_AddJoystick(int device_id, const char *name, SDL_bool is_accelerometer, int nbuttons, int naxes, int nhats, int nballs)
   5.104 +{
   5.105 +    SDL_JoystickGUID guid;
   5.106 +    SDL_joylist_item *item;
   5.107 +#if !SDL_EVENTS_DISABLED
   5.108 +    SDL_Event event;
   5.109 +#endif
   5.110 +    
   5.111 +    if(JoystickByDeviceId(device_id) != NULL || name == NULL) {
   5.112 +        return -1;
   5.113 +    }
   5.114 +    
   5.115 +    /* the GUID is just the first 16 chars of the name for now */
   5.116 +    SDL_zero( guid );
   5.117 +    SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) );
   5.118 +
   5.119 +    item = (SDL_joylist_item *) SDL_malloc(sizeof (SDL_joylist_item));
   5.120 +    if (item == NULL) {
   5.121 +        return -1;
   5.122 +    }
   5.123 +
   5.124 +    SDL_zerop(item);
   5.125 +    item->guid = guid;
   5.126 +    item->device_id = device_id;
   5.127 +    item->name = SDL_strdup(name);
   5.128 +    if ( item->name == NULL ) {
   5.129 +         SDL_free(item);
   5.130 +         return -1;
   5.131 +    }
   5.132 +    
   5.133 +    item->is_accelerometer = is_accelerometer;
   5.134 +    if (nbuttons > -1) {
   5.135 +        item->nbuttons = nbuttons;
   5.136 +    }
   5.137 +    else {
   5.138 +        item->nbuttons = ANDROID_MAX_NBUTTONS;
   5.139 +    }
   5.140 +    item->naxes = naxes;
   5.141 +    item->nhats = nhats;
   5.142 +    item->nballs = nballs;
   5.143 +    item->device_instance = instance_counter++;
   5.144 +    if (SDL_joylist_tail == NULL) {
   5.145 +        SDL_joylist = SDL_joylist_tail = item;
   5.146 +    } else {
   5.147 +        SDL_joylist_tail->next = item;
   5.148 +        SDL_joylist_tail = item;
   5.149 +    }
   5.150 +
   5.151 +    /* Need to increment the joystick count before we post the event */
   5.152 +    ++numjoysticks;
   5.153 +
   5.154 +#if !SDL_EVENTS_DISABLED
   5.155 +    event.type = SDL_JOYDEVICEADDED;
   5.156 +
   5.157 +    if (SDL_GetEventState(event.type) == SDL_ENABLE) {
   5.158 +        event.jdevice.which = (numjoysticks - 1);
   5.159 +        if ( (SDL_EventOK == NULL) ||
   5.160 +             (*SDL_EventOK) (SDL_EventOKParam, &event) ) {
   5.161 +            SDL_PushEvent(&event);
   5.162 +        }
   5.163 +    }
   5.164 +#endif /* !SDL_EVENTS_DISABLED */
   5.165 +
   5.166 +    SDL_Log("Added joystick %s with device_id %d", name, device_id);
   5.167 +
   5.168 +    return numjoysticks;
   5.169 +}
   5.170 +
   5.171 +int 
   5.172 +Android_RemoveJoystick(int device_id)
   5.173 +{
   5.174 +    SDL_joylist_item *item = SDL_joylist;
   5.175 +    SDL_joylist_item *prev = NULL;
   5.176 +#if !SDL_EVENTS_DISABLED
   5.177 +    SDL_Event event;
   5.178 +#endif
   5.179 +    
   5.180 +    /* Don't call JoystickByDeviceId here or there'll be an infinite loop! */
   5.181 +    while (item != NULL) {
   5.182 +        if (item->device_id == device_id) {
   5.183 +            break;
   5.184 +        }
   5.185 +        prev = item;
   5.186 +        item = item->next;
   5.187 +    }
   5.188 +    
   5.189 +    if (item == NULL) {
   5.190 +        return -1;
   5.191 +    }
   5.192 +
   5.193 +    const int retval = item->device_instance;
   5.194 +    if (prev != NULL) {
   5.195 +        prev->next = item->next;
   5.196 +    } else {
   5.197 +        SDL_assert(SDL_joylist == item);
   5.198 +        SDL_joylist = item->next;
   5.199 +    }
   5.200 +    if (item == SDL_joylist_tail) {
   5.201 +        SDL_joylist_tail = prev;
   5.202 +    }
   5.203 +
   5.204 +    /* Need to decrement the joystick count before we post the event */
   5.205 +    --numjoysticks;
   5.206 +
   5.207 +#if !SDL_EVENTS_DISABLED
   5.208 +    event.type = SDL_JOYDEVICEREMOVED;
   5.209 +
   5.210 +    if (SDL_GetEventState(event.type) == SDL_ENABLE) {
   5.211 +        event.jdevice.which = item->device_instance;
   5.212 +        if ( (SDL_EventOK == NULL) ||
   5.213 +             (*SDL_EventOK) (SDL_EventOKParam, &event) ) {
   5.214 +            SDL_PushEvent(&event);
   5.215 +        }
   5.216 +    }
   5.217 +#endif /* !SDL_EVENTS_DISABLED */
   5.218 +
   5.219 +    SDL_Log("Removed joystick with device_id %d", device_id);
   5.220 +    
   5.221 +    SDL_free(item->name);
   5.222 +    SDL_free(item);
   5.223 +    return retval;
   5.224 +}
   5.225 +
   5.226 +
   5.227  int
   5.228  SDL_SYS_JoystickInit(void)
   5.229  {
   5.230 -    int i = 0;
   5.231      const char *env;
   5.232 +    SDL_SYS_JoystickDetect();
   5.233      
   5.234      env = SDL_GetHint(SDL_HINT_ACCEL_AS_JOY);
   5.235 -    if (env && !SDL_atoi(env))
   5.236 -        SYS_accelAsJoy = SDL_FALSE;
   5.237 -    else
   5.238 -        SYS_accelAsJoy = SDL_TRUE; /* Default behavior */
   5.239 -    
   5.240 -    SYS_numjoysticks = Android_JNI_GetNumJoysticks();
   5.241 -    if (SYS_accelAsJoy) {
   5.242 -        SYS_numjoysticks++;
   5.243 -    }
   5.244 -    SYS_Joysticks = (SDL_Joystick **)SDL_calloc(1, SYS_numjoysticks*sizeof(SDL_Joystick *));
   5.245 -    if (SYS_Joysticks == NULL)
   5.246 -    {
   5.247 -        return SDL_OutOfMemory();
   5.248 -    }
   5.249 -    SYS_JoystickNames = (char **)SDL_calloc(1, SYS_numjoysticks*sizeof(char *));
   5.250 -    if (SYS_JoystickNames == NULL)
   5.251 -    {
   5.252 -        SDL_free(SYS_Joysticks);
   5.253 -        SYS_Joysticks = NULL;
   5.254 -        return SDL_OutOfMemory();
   5.255 -    }
   5.256 -    
   5.257 -    for (i = 0; i < SYS_numjoysticks; i++)
   5.258 -    {
   5.259 -        if ( SYS_accelAsJoy && i == ANDROID_ACCELEROMETER_INDEX ) {
   5.260 -            SYS_JoystickNames[i] = ANDROID_ACCELEROMETER_NAME;
   5.261 -        } else {
   5.262 -            SYS_JoystickNames[i] = Android_JNI_GetJoystickName(i);
   5.263 -        }
   5.264 +    if (!env || SDL_atoi(env)) {
   5.265 +        /* Default behavior, accelerometer as joystick */
   5.266 +        Android_AddJoystick(ANDROID_ACCELEROMETER_DEVICE_ID, ANDROID_ACCELEROMETER_NAME, SDL_TRUE, 0, 3, 0, 0);
   5.267      }
   5.268     
   5.269 -    return (SYS_numjoysticks);
   5.270 +    return (numjoysticks);
   5.271 +
   5.272  }
   5.273  
   5.274  int SDL_SYS_NumJoysticks()
   5.275  {
   5.276 -    return SYS_numjoysticks;
   5.277 +    return numjoysticks;
   5.278  }
   5.279  
   5.280  void SDL_SYS_JoystickDetect()
   5.281  {
   5.282 +    /* Support for device connect/disconnect is API >= 16 only,
   5.283 +     * so we have to poll ever few seconds.
   5.284 +     * Ref: http://developer.android.com/reference/android/hardware/input/InputManager.InputDeviceListener.html
   5.285 +     */
   5.286 +    Android_JNI_PollInputDevices();   
   5.287  }
   5.288  
   5.289 -/* TODO: Hotplugging support */
   5.290  SDL_bool SDL_SYS_JoystickNeedsPolling()
   5.291  {
   5.292 -    return SDL_FALSE;
   5.293 +    return SDL_TRUE;
   5.294 +}
   5.295 +
   5.296 +static SDL_joylist_item *
   5.297 +JoystickByDevIndex(int device_index)
   5.298 +{
   5.299 +    SDL_joylist_item *item = SDL_joylist;
   5.300 +
   5.301 +    if ((device_index < 0) || (device_index >= numjoysticks)) {
   5.302 +        return NULL;
   5.303 +    }
   5.304 +
   5.305 +    while (device_index > 0) {
   5.306 +        SDL_assert(item != NULL);
   5.307 +        device_index--;
   5.308 +        item = item->next;
   5.309 +    }
   5.310 +
   5.311 +    return item;
   5.312 +}
   5.313 +
   5.314 +static SDL_joylist_item *
   5.315 +JoystickByDeviceId(int device_id)
   5.316 +{
   5.317 +    SDL_joylist_item *item = SDL_joylist;
   5.318 +
   5.319 +    while (item != NULL) {
   5.320 +        if (item->device_id == device_id) {
   5.321 +            return item;
   5.322 +        }
   5.323 +        item = item->next;
   5.324 +    }
   5.325 +    
   5.326 +    /* Joystick not found, try adding it */
   5.327 +    SDL_SYS_JoystickDetect();
   5.328 +    
   5.329 +    while (item != NULL) {
   5.330 +        if (item->device_id == device_id) {
   5.331 +            return item;
   5.332 +        }
   5.333 +        item = item->next;
   5.334 +    }
   5.335 +
   5.336 +    return NULL;
   5.337  }
   5.338  
   5.339  /* Function to get the device-dependent name of a joystick */
   5.340  const char *
   5.341  SDL_SYS_JoystickNameForDeviceIndex(int device_index)
   5.342  {
   5.343 -    return SYS_JoystickNames[device_index];
   5.344 +    return JoystickByDevIndex(device_index)->name;
   5.345  }
   5.346  
   5.347  /* Function to perform the mapping from device index to the instance id for this index */
   5.348  SDL_JoystickID SDL_SYS_GetInstanceIdOfDeviceIndex(int device_index)
   5.349  {
   5.350 -    return device_index;
   5.351 +    return JoystickByDevIndex(device_index)->device_instance;
   5.352  }
   5.353  
   5.354  /* Function to open a joystick for use.
   5.355 @@ -221,49 +414,54 @@
   5.356  int
   5.357  SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index)
   5.358  {
   5.359 -    if (device_index < SYS_numjoysticks) {
   5.360 -        joystick->nhats = 0;
   5.361 -        joystick->nballs = 0;
   5.362 -        if (SYS_accelAsJoy && device_index == ANDROID_ACCELEROMETER_INDEX) {
   5.363 -            joystick->nbuttons = 0;
   5.364 -            joystick->naxes = 3;
   5.365 -        } else {
   5.366 -            /* FIXME: Get the real number of buttons in the device? */
   5.367 -            joystick->nbuttons = ANDROID_MAX_NBUTTONS;
   5.368 -            joystick->naxes = Android_JNI_GetJoystickAxes(device_index);
   5.369 -        }
   5.370 -        
   5.371 -        SYS_Joysticks[device_index] = joystick;
   5.372 -        return 0;
   5.373 -    } else {
   5.374 -        return SDL_SetError("No joystick available with that index");
   5.375 +    SDL_joylist_item *item = JoystickByDevIndex(device_index);
   5.376 +    char *fname = NULL;
   5.377 +
   5.378 +    if (item == NULL ) {
   5.379 +        return SDL_SetError("No such device");
   5.380      }
   5.381 +    
   5.382 +    if (item->joystick != NULL) {
   5.383 +        return SDL_SetError("Joystick already opened");
   5.384 +    }
   5.385 +
   5.386 +    joystick->instance_id = item->device_instance;
   5.387 +    joystick->hwdata = (struct joystick_hwdata *) item;
   5.388 +    item->joystick = joystick;
   5.389 +    joystick->nhats = item->nhats;
   5.390 +    joystick->nballs = item->nballs;
   5.391 +    joystick->nbuttons = item->nbuttons;
   5.392 +    joystick->naxes = item->naxes;
   5.393 +
   5.394 +    return (0);
   5.395  }
   5.396  
   5.397  /* Function to determine is this joystick is attached to the system right now */
   5.398  SDL_bool SDL_SYS_JoystickAttached(SDL_Joystick *joystick)
   5.399  {
   5.400 -    return SDL_TRUE;
   5.401 +    return !joystick->closed && (joystick->hwdata != NULL);
   5.402  }
   5.403  
   5.404 -/* Function to update the state of a joystick - called as a device poll.
   5.405 - * This function shouldn't update the joystick structure directly,
   5.406 - * but instead should call SDL_PrivateJoystick*() to deliver events
   5.407 - * and update joystick device state.
   5.408 - */
   5.409  void
   5.410  SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
   5.411  {
   5.412      int i;
   5.413      Sint16 value;
   5.414      float values[3];
   5.415 +    SDL_joylist_item *item = SDL_joylist;
   5.416  
   5.417 -    if (SYS_accelAsJoy && Android_JNI_GetAccelerometerValues(values) &&
   5.418 -        joystick->instance_id == ANDROID_ACCELEROMETER_INDEX) {
   5.419 -        for ( i = 0; i < 3; i++ ) {
   5.420 -            value = (Sint16)(values[i] * 32767.0f);
   5.421 -            SDL_PrivateJoystickAxis(joystick, i, value);
   5.422 +    while (item) {
   5.423 +        if (item->is_accelerometer) {
   5.424 +            if (item->joystick) {
   5.425 +                Android_JNI_GetAccelerometerValues(values);
   5.426 +                for ( i = 0; i < 3; i++ ) {
   5.427 +                    value = (Sint16)(values[i] * 32767.0f);
   5.428 +                    SDL_PrivateJoystickAxis(item->joystick, i, value);
   5.429 +                }
   5.430 +            }
   5.431 +            break;
   5.432          }
   5.433 +        item = item->next;
   5.434      }
   5.435  }
   5.436  
   5.437 @@ -271,14 +469,10 @@
   5.438  void
   5.439  SDL_SYS_JoystickClose(SDL_Joystick * joystick)
   5.440  {
   5.441 -    int device_index;
   5.442 -    
   5.443 -    for (device_index = 0; device_index < SYS_numjoysticks; device_index++) {
   5.444 -        if ( SYS_Joysticks[device_index] == joystick ) {
   5.445 -            SYS_Joysticks[device_index] = NULL;
   5.446 -        }
   5.447 +    if (joystick->hwdata) {
   5.448 +        ((SDL_joylist_item*)joystick->hwdata)->joystick = NULL;
   5.449 +        joystick->hwdata = NULL;
   5.450      }
   5.451 -    
   5.452      joystick->closed = 1;
   5.453  }
   5.454  
   5.455 @@ -286,70 +480,29 @@
   5.456  void
   5.457  SDL_SYS_JoystickQuit(void)
   5.458  {
   5.459 -    SDL_free(SYS_JoystickNames);
   5.460 -    SDL_free(SYS_Joysticks);
   5.461 -    SYS_JoystickNames = NULL;
   5.462 -    SYS_Joysticks = NULL;
   5.463 +    SDL_joylist_item *item = NULL;
   5.464 +    SDL_joylist_item *next = NULL;
   5.465 +
   5.466 +    for (item = SDL_joylist; item; item = next) {
   5.467 +        next = item->next;
   5.468 +        SDL_free(item->name);
   5.469 +        SDL_free(item);
   5.470 +    }
   5.471 +
   5.472 +    SDL_joylist = SDL_joylist_tail = NULL;
   5.473 +
   5.474 +    numjoysticks = 0;
   5.475 +    instance_counter = 0;
   5.476  }
   5.477  
   5.478  SDL_JoystickGUID SDL_SYS_JoystickGetDeviceGUID( int device_index )
   5.479  {
   5.480 -    SDL_JoystickGUID guid;
   5.481 -    /* the GUID is just the first 16 chars of the name for now */
   5.482 -    const char *name = SDL_SYS_JoystickNameForDeviceIndex( device_index );
   5.483 -    SDL_zero( guid );
   5.484 -    SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) );
   5.485 -    return guid;
   5.486 +    return JoystickByDevIndex(device_index)->guid;
   5.487  }
   5.488  
   5.489  SDL_JoystickGUID SDL_SYS_JoystickGetGUID(SDL_Joystick * joystick)
   5.490  {
   5.491 -    SDL_JoystickGUID guid;
   5.492 -    /* the GUID is just the first 16 chars of the name for now */
   5.493 -    const char *name = joystick->name;
   5.494 -    SDL_zero( guid );
   5.495 -    SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) );
   5.496 -    return guid;
   5.497 -}
   5.498 -
   5.499 -int
   5.500 -Android_OnPadDown(int padId, int keycode)
   5.501 -{
   5.502 -    int button = keycode_to_SDL(keycode);
   5.503 -    if (button >= 0) {
   5.504 -        if (SYS_Joysticks[padId]) {
   5.505 -            SDL_PrivateJoystickButton(SYS_Joysticks[padId], button , SDL_PRESSED);
   5.506 -        }
   5.507 -        return 0;
   5.508 -    }
   5.509 -    
   5.510 -    return -1;
   5.511 -}
   5.512 -
   5.513 -int
   5.514 -Android_OnPadUp(int padId, int keycode)
   5.515 -{
   5.516 -    int button = keycode_to_SDL(keycode);
   5.517 -    if (button >= 0) {
   5.518 -        if (SYS_Joysticks[padId]) {
   5.519 -            SDL_PrivateJoystickButton(SYS_Joysticks[padId], button, SDL_RELEASED);
   5.520 -        }
   5.521 -        return 0;
   5.522 -    }
   5.523 -    
   5.524 -    return -1;
   5.525 -}
   5.526 -
   5.527 -int
   5.528 -Android_OnJoy(int joyId, int axis, float value)
   5.529 -{
   5.530 -    /* Android gives joy info normalized as [-1.0, 1.0] or [0.0, 1.0] */
   5.531 -    /* TODO: Are the reported values right? */
   5.532 -    if (SYS_Joysticks[joyId]) {
   5.533 -        SDL_PrivateJoystickAxis(SYS_Joysticks[joyId], axis, (Sint16) (32767.*value) );
   5.534 -    }
   5.535 -    
   5.536 -    return 0;
   5.537 +    return ((SDL_joylist_item*)joystick->hwdata)->guid;
   5.538  }
   5.539  
   5.540  #endif /* SDL_JOYSTICK_ANDROID */
     6.1 --- a/src/joystick/android/SDL_sysjoystick.h	Mon Dec 09 15:17:20 2013 -0500
     6.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.3 @@ -1,28 +0,0 @@
     6.4 -/*
     6.5 - Simple DirectMedia Layer
     6.6 - Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
     6.7 - 
     6.8 - This software is provided 'as-is', without any express or implied
     6.9 - warranty.  In no event will the authors be held liable for any damages
    6.10 - arising from the use of this software.
    6.11 - 
    6.12 - Permission is granted to anyone to use this software for any purpose,
    6.13 - including commercial applications, and to alter it and redistribute it
    6.14 - freely, subject to the following restrictions:
    6.15 - 
    6.16 - 1. The origin of this software must not be misrepresented; you must not
    6.17 - claim that you wrote the original software. If you use this software
    6.18 - in a product, an acknowledgment in the product documentation would be
    6.19 - appreciated but is not required.
    6.20 - 2. Altered source versions must be plainly marked as such, and must not be
    6.21 - misrepresented as being the original software.
    6.22 - 3. This notice may not be removed or altered from any source distribution.
    6.23 - */
    6.24 -
    6.25 -#include "SDL_config.h"
    6.26 -
    6.27 -extern int Android_OnPadDown(int padId, int keycode);
    6.28 -extern int Android_OnPadUp(int padId, int keycode);
    6.29 -extern int Android_OnJoy(int joyId, int axisnum, float value);
    6.30 -
    6.31 -/* vi: set ts=4 sw=4 expandtab: */
     7.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.2 +++ b/src/joystick/android/SDL_sysjoystick_c.h	Tue Dec 10 16:24:11 2013 -0300
     7.3 @@ -0,0 +1,51 @@
     7.4 +/*
     7.5 + Simple DirectMedia Layer
     7.6 + Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
     7.7 + 
     7.8 + This software is provided 'as-is', without any express or implied
     7.9 + warranty.  In no event will the authors be held liable for any damages
    7.10 + arising from the use of this software.
    7.11 + 
    7.12 + Permission is granted to anyone to use this software for any purpose,
    7.13 + including commercial applications, and to alter it and redistribute it
    7.14 + freely, subject to the following restrictions:
    7.15 + 
    7.16 + 1. The origin of this software must not be misrepresented; you must not
    7.17 + claim that you wrote the original software. If you use this software
    7.18 + in a product, an acknowledgment in the product documentation would be
    7.19 + appreciated but is not required.
    7.20 + 2. Altered source versions must be plainly marked as such, and must not be
    7.21 + misrepresented as being the original software.
    7.22 + 3. This notice may not be removed or altered from any source distribution.
    7.23 + */
    7.24 +
    7.25 +#include "SDL_config.h"
    7.26 +
    7.27 +#ifdef SDL_JOYSTICK_ANDROID
    7.28 +#include "../SDL_sysjoystick.h"
    7.29 +
    7.30 +extern int Android_OnPadDown(int device_id, int keycode);
    7.31 +extern int Android_OnPadUp(int device_id, int keycode);
    7.32 +extern int Android_OnJoy(int device_id, int axisnum, float value);
    7.33 +extern int Android_AddJoystick(int device_id, const char *name, SDL_bool is_accelerometer, int nbuttons, int naxes, int nhats, int nballs);
    7.34 +extern int Android_RemoveJoystick(int device_id);
    7.35 +
    7.36 +/* A linked list of available joysticks */
    7.37 +typedef struct SDL_joylist_item
    7.38 +{
    7.39 +    int device_instance;
    7.40 +    int device_id; /* Android's device id */
    7.41 +    char *name;   /* "SideWinder 3D Pro" or whatever */
    7.42 +    SDL_JoystickGUID guid;
    7.43 +    SDL_bool is_accelerometer;
    7.44 +    SDL_Joystick *joystick;
    7.45 +    int nbuttons, naxes, nhats, nballs;
    7.46 +    
    7.47 +    struct SDL_joylist_item *next;
    7.48 +} SDL_joylist_item;
    7.49 +
    7.50 +typedef SDL_joylist_item joystick_hwdata;
    7.51 +
    7.52 +#endif /* SDL_JOYSTICK_ANDROID */
    7.53 +
    7.54 +/* vi: set ts=4 sw=4 expandtab: */