Search code examples
event-handlingandroid-ndkjoystick

Android NDK joystick events


Does anyone know how to capture joystick events from the Android NDK?

The NDK does not contain any APIs to let me extract axis information from an AMotionEvent. (AXIS_X and AXIS_Y are mapped to the pointer (x,y) pair, but I need more than that.) There are Java APIs to do this... but when I'm using the NDK, events aren't delivered to Java-land, so I simply never receive them.

And I haven't found any way to turn an NDK AMotionEvent into a Java MotionEvent so that I can pass it through to Java via JNI and extract the information that way.

I absolutely have to get joystick information to my app, but so far I've found no way to do so, no matter how evil. Any ideas?


Solution

  • The people at Nvidia Tegra have managed to do this by using an undocumented function call. They do an dlopen and a dlsym on libandroid.so to look for AMotionEvent_getAxisValue()

    void* lib = dlopen("libandroid.so",0);
    ...
    s_AMotionEvent_getAxisValue = (float(*)(const AInputEvent*,int32_t axis, size_t pointer_index))
            dlsym(lib,"AMotionEvent_getAxisValue");
    

    For more information, you should consult nvidias android samples in Tegra Android Development Pack