Android: add name for Touch devices and simplification, from bug 3958

This commit is contained in:
Sylvain Becker 2019-01-10 21:40:57 +01:00
parent d23c2f07e3
commit 7a1d1baefc
4 changed files with 30 additions and 44 deletions

View File

@ -741,6 +741,7 @@ public class SDLActivity extends Activity implements View.OnSystemUiVisibilityCh
public static native String nativeGetHint(String name); public static native String nativeGetHint(String name);
public static native void nativeSetenv(String name, String value); public static native void nativeSetenv(String name, String value);
public static native void onNativeOrientationChanged(int orientation); public static native void onNativeOrientationChanged(int orientation);
public static native void nativeAddTouch(int touchId, String name);
/** /**
* This method is called by SDL using JNI. * This method is called by SDL using JNI.
@ -1062,16 +1063,14 @@ public class SDLActivity extends Activity implements View.OnSystemUiVisibilityCh
/** /**
* This method is called by SDL using JNI. * This method is called by SDL using JNI.
* @return an array which may be empty but is never null.
*/ */
public static int[] inputGetInputDeviceIds(int sources) { public static void initTouch() {
int[] ids = InputDevice.getDeviceIds(); int[] ids = InputDevice.getDeviceIds();
int[] filtered = new int[ids.length];
int used = 0;
for (int i = 0; i < ids.length; ++i) { for (int i = 0; i < ids.length; ++i) {
InputDevice device = InputDevice.getDevice(ids[i]); InputDevice device = InputDevice.getDevice(ids[i]);
if ((device != null) && ((device.getSources() & sources) != 0)) { if (device != null && (device.getSources() & InputDevice.SOURCE_TOUCHSCREEN) != 0) {
filtered[used++] = device.getId(); nativeAddTouch(device.getId(), device.getName());
} }
} }
return Arrays.copyOf(filtered, used); return Arrays.copyOf(filtered, used);

View File

@ -149,6 +149,10 @@ JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeOrientationChanged)(
JNIEnv *env, jclass cls, JNIEnv *env, jclass cls,
jint orientation); jint orientation);
JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(nativeAddTouch)(
JNIEnv* env, jclass cls,
jint touchId, jstring name);
/* Java class SDLInputConnection */ /* Java class SDLInputConnection */
JNIEXPORT void JNICALL SDL_JAVA_INTERFACE_INPUT_CONNECTION(nativeCommitText)( JNIEXPORT void JNICALL SDL_JAVA_INTERFACE_INPUT_CONNECTION(nativeCommitText)(
JNIEnv *env, jclass cls, JNIEnv *env, jclass cls,
@ -238,7 +242,7 @@ static jmethodID midIsAndroidTV;
static jmethodID midIsChromebook; static jmethodID midIsChromebook;
static jmethodID midIsDeXMode; static jmethodID midIsDeXMode;
static jmethodID midManualBackButton; static jmethodID midManualBackButton;
static jmethodID midInputGetInputDeviceIds; static jmethodID midInitTouch;
static jmethodID midSendMessage; static jmethodID midSendMessage;
static jmethodID midShowTextInput; static jmethodID midShowTextInput;
static jmethodID midIsScreenKeyboardShown; static jmethodID midIsScreenKeyboardShown;
@ -366,8 +370,8 @@ JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(nativeSetupJNI)(JNIEnv *mEnv, jclass c
"isDeXMode", "()Z"); "isDeXMode", "()Z");
midManualBackButton = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass, midManualBackButton = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,
"manualBackButton", "()V"); "manualBackButton", "()V");
midInputGetInputDeviceIds = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass, midInitTouch = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,
"inputGetInputDeviceIds", "(I)[I"); "initTouch", "()V");
midSendMessage = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass, midSendMessage = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,
"sendMessage", "(II)Z"); "sendMessage", "(II)Z");
midShowTextInput = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass, midShowTextInput = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,
@ -396,7 +400,7 @@ JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(nativeSetupJNI)(JNIEnv *mEnv, jclass c
if (!midGetNativeSurface || !midSetSurfaceViewFormat || if (!midGetNativeSurface || !midSetSurfaceViewFormat ||
!midSetActivityTitle || !midSetWindowStyle || !midSetOrientation || !midGetContext || !midIsTablet || !midIsAndroidTV || !midInputGetInputDeviceIds || !midSetActivityTitle || !midSetWindowStyle || !midSetOrientation || !midGetContext || !midIsTablet || !midIsAndroidTV || !midInitTouch ||
!midSendMessage || !midShowTextInput || !midIsScreenKeyboardShown || !midSendMessage || !midShowTextInput || !midIsScreenKeyboardShown ||
!midClipboardSetText || !midClipboardGetText || !midClipboardHasText || !midClipboardSetText || !midClipboardGetText || !midClipboardHasText ||
!midOpenAPKExpansionInputStream || !midGetManifestEnvironmentVariables || !midGetDisplayDPI || !midOpenAPKExpansionInputStream || !midGetManifestEnvironmentVariables || !midGetDisplayDPI ||
@ -596,6 +600,17 @@ JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeOrientationChanged)(
} }
} }
JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(nativeAddTouch)(
JNIEnv* env, jclass cls,
jint touchId, jstring name)
{
const char *utfname = (*env)->GetStringUTFChars(env, name, NULL);
SDL_AddTouch((SDL_TouchID) touchId, SDL_TOUCH_DEVICE_DIRECT, utfname);
(*env)->ReleaseStringUTFChars(env, name, utfname);
}
/* Paddown */ /* Paddown */
JNIEXPORT jint JNICALL SDL_JAVA_CONTROLLER_INTERFACE(onNativePadDown)( JNIEXPORT jint JNICALL SDL_JAVA_CONTROLLER_INTERFACE(onNativePadDown)(
JNIEnv *env, jclass jcls, JNIEnv *env, jclass jcls,
@ -2074,29 +2089,10 @@ int Android_JNI_GetPowerInfo(int *plugged, int *charged, int *battery, int *seco
return 0; return 0;
} }
/* returns number of found touch devices as return value and ids in parameter ids */ /* Add all touch devices */
int Android_JNI_GetTouchDeviceIds(int **ids) { int Android_JNI_InitTouch() {
JNIEnv *env = Android_JNI_GetEnv(); JNIEnv *env = Android_JNI_GetEnv();
jint sources = 4098; /* == InputDevice.SOURCE_TOUCHSCREEN */ (*env)->CallStaticVoidMethod(env, mActivityClass, midInitTouch);
jintArray array = (jintArray) (*env)->CallStaticObjectMethod(env, mActivityClass, midInputGetInputDeviceIds, sources);
int number = 0;
*ids = NULL;
if (array) {
number = (int) (*env)->GetArrayLength(env, array);
if (0 < number) {
jint *elements = (*env)->GetIntArrayElements(env, array, NULL);
if (elements) {
int i;
*ids = SDL_malloc(number * sizeof (**ids));
for (i = 0; i < number; ++i) { /* not assuming sizeof (jint) == sizeof (int) */
(*ids)[i] = elements[i];
}
(*env)->ReleaseIntArrayElements(env, array, elements, JNI_ABORT);
}
}
(*env)->DeleteLocalRef(env, array);
}
return number;
} }
/* sets the mSeparateMouseAndTouch field */ /* sets the mSeparateMouseAndTouch field */

View File

@ -95,7 +95,6 @@ void Android_JNI_SuspendScreenSaver(SDL_bool suspend);
/* Touch support */ /* Touch support */
int Android_JNI_InitTouch(void); int Android_JNI_InitTouch(void);
void Android_JNI_SetSeparateMouseAndTouch(SDL_bool new_value); void Android_JNI_SetSeparateMouseAndTouch(SDL_bool new_value);
int Android_JNI_GetTouchDeviceIds(int **ids);
/* Threads */ /* Threads */
#include <jni.h> #include <jni.h>

View File

@ -63,19 +63,11 @@ SeparateEventsHintWatcher(void *userdata, const char *name,
void Android_InitTouch(void) void Android_InitTouch(void)
{ {
int i;
int *ids;
const int number = Android_JNI_GetTouchDeviceIds(&ids);
SDL_AddHintCallback(SDL_HINT_ANDROID_SEPARATE_MOUSE_AND_TOUCH, SDL_AddHintCallback(SDL_HINT_ANDROID_SEPARATE_MOUSE_AND_TOUCH,
SeparateEventsHintWatcher, NULL); SeparateEventsHintWatcher, NULL);
if (0 < number) { /* Add all touch devices */
for (i = 0; i < number; ++i) { Android_JNI_InitTouch();
SDL_AddTouch((SDL_TouchID) ids[i], SDL_TOUCH_DEVICE_DIRECT, ""); /* no error handling */
}
SDL_free(ids);
}
} }
void Android_QuitTouch(void) void Android_QuitTouch(void)