Skip to content

Commit

Permalink
Android: add name for Touch devices and simplification, from bug 3958
Browse files Browse the repository at this point in the history
  • Loading branch information
1bsyl committed Jan 10, 2019
1 parent d23c2f0 commit 7a1d1ba
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 44 deletions.
Expand Up @@ -741,6 +741,7 @@ public static native void onNativeTouch(int touchDevId, int pointerFingerId,
public static native String nativeGetHint(String name);
public static native void nativeSetenv(String name, String value);
public static native void onNativeOrientationChanged(int orientation);
public static native void nativeAddTouch(int touchId, String name);

/**
* This method is called by SDL using JNI.
Expand Down Expand Up @@ -1062,16 +1063,14 @@ public static void setSurfaceViewFormat(int format) {

/**
* 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[] filtered = new int[ids.length];
int used = 0;

for (int i = 0; i < ids.length; ++i) {
InputDevice device = InputDevice.getDevice(ids[i]);
if ((device != null) && ((device.getSources() & sources) != 0)) {
filtered[used++] = device.getId();
if (device != null && (device.getSources() & InputDevice.SOURCE_TOUCHSCREEN) != 0) {
nativeAddTouch(device.getId(), device.getName());
}
}
return Arrays.copyOf(filtered, used);
Expand Down
50 changes: 23 additions & 27 deletions src/core/android/SDL_android.c
Expand Up @@ -149,6 +149,10 @@ JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeOrientationChanged)(
JNIEnv *env, jclass cls,
jint orientation);

JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(nativeAddTouch)(
JNIEnv* env, jclass cls,
jint touchId, jstring name);

/* Java class SDLInputConnection */
JNIEXPORT void JNICALL SDL_JAVA_INTERFACE_INPUT_CONNECTION(nativeCommitText)(
JNIEnv *env, jclass cls,
Expand Down Expand Up @@ -238,7 +242,7 @@ static jmethodID midIsAndroidTV;
static jmethodID midIsChromebook;
static jmethodID midIsDeXMode;
static jmethodID midManualBackButton;
static jmethodID midInputGetInputDeviceIds;
static jmethodID midInitTouch;
static jmethodID midSendMessage;
static jmethodID midShowTextInput;
static jmethodID midIsScreenKeyboardShown;
Expand Down Expand Up @@ -366,8 +370,8 @@ JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(nativeSetupJNI)(JNIEnv *mEnv, jclass c
"isDeXMode", "()Z");
midManualBackButton = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,
"manualBackButton", "()V");
midInputGetInputDeviceIds = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,
"inputGetInputDeviceIds", "(I)[I");
midInitTouch = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,
"initTouch", "()V");
midSendMessage = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,
"sendMessage", "(II)Z");
midShowTextInput = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,
Expand Down Expand Up @@ -396,7 +400,7 @@ JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(nativeSetupJNI)(JNIEnv *mEnv, jclass c


if (!midGetNativeSurface || !midSetSurfaceViewFormat ||
!midSetActivityTitle || !midSetWindowStyle || !midSetOrientation || !midGetContext || !midIsTablet || !midIsAndroidTV || !midInputGetInputDeviceIds ||
!midSetActivityTitle || !midSetWindowStyle || !midSetOrientation || !midGetContext || !midIsTablet || !midIsAndroidTV || !midInitTouch ||
!midSendMessage || !midShowTextInput || !midIsScreenKeyboardShown ||
!midClipboardSetText || !midClipboardGetText || !midClipboardHasText ||
!midOpenAPKExpansionInputStream || !midGetManifestEnvironmentVariables || !midGetDisplayDPI ||
Expand Down Expand Up @@ -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 */
JNIEXPORT jint JNICALL SDL_JAVA_CONTROLLER_INTERFACE(onNativePadDown)(
JNIEnv *env, jclass jcls,
Expand Down Expand Up @@ -2074,29 +2089,10 @@ int Android_JNI_GetPowerInfo(int *plugged, int *charged, int *battery, int *seco
return 0;
}

/* returns number of found touch devices as return value and ids in parameter ids */
int Android_JNI_GetTouchDeviceIds(int **ids) {
JNIEnv *env = Android_JNI_GetEnv();
jint sources = 4098; /* == InputDevice.SOURCE_TOUCHSCREEN */
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;
/* Add all touch devices */
int Android_JNI_InitTouch() {
JNIEnv *env = Android_JNI_GetEnv();
(*env)->CallStaticVoidMethod(env, mActivityClass, midInitTouch);
}

/* sets the mSeparateMouseAndTouch field */
Expand Down
1 change: 0 additions & 1 deletion src/core/android/SDL_android.h
Expand Up @@ -95,7 +95,6 @@ void Android_JNI_SuspendScreenSaver(SDL_bool suspend);
/* Touch support */
int Android_JNI_InitTouch(void);
void Android_JNI_SetSeparateMouseAndTouch(SDL_bool new_value);
int Android_JNI_GetTouchDeviceIds(int **ids);

/* Threads */
#include <jni.h>
Expand Down
12 changes: 2 additions & 10 deletions src/video/android/SDL_androidtouch.c
Expand Up @@ -63,19 +63,11 @@ SeparateEventsHintWatcher(void *userdata, const char *name,

void Android_InitTouch(void)
{
int i;
int *ids;
const int number = Android_JNI_GetTouchDeviceIds(&ids);

SDL_AddHintCallback(SDL_HINT_ANDROID_SEPARATE_MOUSE_AND_TOUCH,
SeparateEventsHintWatcher, NULL);

if (0 < number) {
for (i = 0; i < number; ++i) {
SDL_AddTouch((SDL_TouchID) ids[i], SDL_TOUCH_DEVICE_DIRECT, ""); /* no error handling */
}
SDL_free(ids);
}
/* Add all touch devices */
Android_JNI_InitTouch();
}

void Android_QuitTouch(void)
Expand Down

0 comments on commit 7a1d1ba

Please sign in to comment.