Reimplemented Android cursor API support using reflection so it builds with older SDKs

Sam Lantinga 2018-03-16 11:08:53 -07:00
parent e20d4173bf
commit f536fbea71
4 changed files with 78 additions and 68 deletions

View File

@ -77,10 +77,8 @@ public class SDLActivity extends Activity {
protected static boolean mScreenKeyboardShown; protected static boolean mScreenKeyboardShown;
protected static ViewGroup mLayout; protected static ViewGroup mLayout;
protected static SDLClipboardHandler mClipboardHandler; protected static SDLClipboardHandler mClipboardHandler;
//#CURSORIMPLEENTATION protected static Hashtable<Integer, Object> mCursors;
//protected static Hashtable<Integer, PointerIcon> mCursors; protected static int mLastCursorID;
//protected static int mLastCursorID;
//protected static PointerIcon mActiveCursor;
// This is what SDL runs in. It invokes SDL_main(), eventually // This is what SDL runs in. It invokes SDL_main(), eventually
@ -153,10 +151,8 @@ public class SDLActivity extends Activity {
mTextEdit = null; mTextEdit = null;
mLayout = null; mLayout = null;
mClipboardHandler = null; mClipboardHandler = null;
//#CURSORIMPLEENTATION mCursors = new Hashtable<Integer, Object>();
//mCursors = new Hashtable<Integer, PointerIcon>(); mLastCursorID = 0;
//mLastCursorID = 0;
//mActiveCursor = null;
mSDLThread = null; mSDLThread = null;
mExitCalledFromJava = false; mExitCalledFromJava = false;
mBrokenLibraries = false; mBrokenLibraries = false;
@ -1093,69 +1089,91 @@ public class SDLActivity extends Activity {
/** /**
* This method is called by SDL using JNI. * This method is called by SDL using JNI.
*/ */
/**
* #CURSORIMPLEENTATION
* The cursor implementation requires API 24 or above
*
public static int createCustomCursor(int[] colors, int width, int height, int hotSpotX, int hotSpotY) { public static int createCustomCursor(int[] colors, int width, int height, int hotSpotX, int hotSpotY) {
Bitmap bitmap = Bitmap.createBitmap(colors, width, height, Bitmap.Config.ARGB_8888); Bitmap bitmap = Bitmap.createBitmap(colors, width, height, Bitmap.Config.ARGB_8888);
++mLastCursorID; ++mLastCursorID;
mCursors.put(mLastCursorID, PointerIcon.create(bitmap, hotSpotX, hotSpotY)); // This requires API 24, so use reflection to implement this
try {
Class PointerIconClass = Class.forName("android.view.PointerIcon");
Class[] arg_types = new Class[] { Bitmap.class, float.class, float.class };
Method create = PointerIconClass.getMethod("create", arg_types);
mCursors.put(mLastCursorID, create.invoke(null, bitmap, hotSpotX, hotSpotY));
} catch (Exception e) {
return 0;
}
return mLastCursorID; return mLastCursorID;
} }
public static void setCustomCursor(int cursorID) { /**
mActiveCursor = mCursors.get(cursorID); * This method is called by SDL using JNI.
*/
public static boolean setCustomCursor(int cursorID) {
// This requires API 24, so use reflection to implement this
try {
Class PointerIconClass = Class.forName("android.view.PointerIcon");
Method setPointerIcon = SDLSurface.class.getMethod("setPointerIcon", PointerIconClass);
setPointerIcon.invoke(mSurface, mCursors.get(cursorID));
} catch (Exception e) {
return false;
}
return true;
} }
public static void setSystemCursor(int cursorID) { /**
* This method is called by SDL using JNI.
*/
public static boolean setSystemCursor(int cursorID) {
int cursor_type = 0; //PointerIcon.TYPE_NULL;
switch (cursorID) { switch (cursorID) {
case SDL_SYSTEM_CURSOR_NONE:
mActiveCursor = PointerIcon.getSystemIcon(SDL.getContext(), PointerIcon.TYPE_NULL);
break;
case SDL_SYSTEM_CURSOR_ARROW: case SDL_SYSTEM_CURSOR_ARROW:
mActiveCursor = PointerIcon.getSystemIcon(SDL.getContext(), PointerIcon.TYPE_ARROW); cursor_type = 1000; //PointerIcon.TYPE_ARROW;
break; break;
case SDL_SYSTEM_CURSOR_IBEAM: case SDL_SYSTEM_CURSOR_IBEAM:
mActiveCursor = PointerIcon.getSystemIcon(SDL.getContext(), PointerIcon.TYPE_TEXT); cursor_type = 1008; //PointerIcon.TYPE_TEXT;
break; break;
case SDL_SYSTEM_CURSOR_WAIT: case SDL_SYSTEM_CURSOR_WAIT:
mActiveCursor = PointerIcon.getSystemIcon(SDL.getContext(), PointerIcon.TYPE_WAIT); cursor_type = 1004; //PointerIcon.TYPE_WAIT;
break; break;
case SDL_SYSTEM_CURSOR_CROSSHAIR: case SDL_SYSTEM_CURSOR_CROSSHAIR:
mActiveCursor = PointerIcon.getSystemIcon(SDL.getContext(), PointerIcon.TYPE_CROSSHAIR); cursor_type = 1007; //PointerIcon.TYPE_CROSSHAIR;
break; break;
case SDL_SYSTEM_CURSOR_WAITARROW: case SDL_SYSTEM_CURSOR_WAITARROW:
mActiveCursor = PointerIcon.getSystemIcon(SDL.getContext(), PointerIcon.TYPE_WAIT); cursor_type = 1004; //PointerIcon.TYPE_WAIT;
break; break;
case SDL_SYSTEM_CURSOR_SIZENWSE: case SDL_SYSTEM_CURSOR_SIZENWSE:
mActiveCursor = PointerIcon.getSystemIcon(SDL.getContext(), PointerIcon.TYPE_TOP_LEFT_DIAGONAL_DOUBLE_ARROW); cursor_type = 1017; //PointerIcon.TYPE_TOP_LEFT_DIAGONAL_DOUBLE_ARROW;
break; break;
case SDL_SYSTEM_CURSOR_SIZENESW: case SDL_SYSTEM_CURSOR_SIZENESW:
mActiveCursor = PointerIcon.getSystemIcon(SDL.getContext(), PointerIcon.TYPE_TOP_RIGHT_DIAGONAL_DOUBLE_ARROW); cursor_type = 1016; //PointerIcon.TYPE_TOP_RIGHT_DIAGONAL_DOUBLE_ARROW;
break; break;
case SDL_SYSTEM_CURSOR_SIZEWE: case SDL_SYSTEM_CURSOR_SIZEWE:
mActiveCursor = PointerIcon.getSystemIcon(SDL.getContext(), PointerIcon.TYPE_HORIZONTAL_DOUBLE_ARROW); cursor_type = 1014; //PointerIcon.TYPE_HORIZONTAL_DOUBLE_ARROW;
break; break;
case SDL_SYSTEM_CURSOR_SIZENS: case SDL_SYSTEM_CURSOR_SIZENS:
mActiveCursor = PointerIcon.getSystemIcon(SDL.getContext(), PointerIcon.TYPE_VERTICAL_DOUBLE_ARROW); cursor_type = 1015; //PointerIcon.TYPE_VERTICAL_DOUBLE_ARROW;
break; break;
case SDL_SYSTEM_CURSOR_SIZEALL: case SDL_SYSTEM_CURSOR_SIZEALL:
mActiveCursor = PointerIcon.getSystemIcon(SDL.getContext(), PointerIcon.TYPE_GRAB); cursor_type = 1020; //PointerIcon.TYPE_GRAB;
break; break;
case SDL_SYSTEM_CURSOR_NO: case SDL_SYSTEM_CURSOR_NO:
mActiveCursor = PointerIcon.getSystemIcon(SDL.getContext(), PointerIcon.TYPE_NO_DROP); cursor_type = 1012; //PointerIcon.TYPE_NO_DROP;
break; break;
case SDL_SYSTEM_CURSOR_HAND: case SDL_SYSTEM_CURSOR_HAND:
mActiveCursor = PointerIcon.getSystemIcon(SDL.getContext(), PointerIcon.TYPE_HAND); cursor_type = 1002; //PointerIcon.TYPE_HAND;
break; break;
} }
// This requires API 24, so use reflection to implement this
try {
Class PointerIconClass = Class.forName("android.view.PointerIcon");
Class[] arg_types = new Class[] { Context.class, int.class };
Method getSystemIcon = PointerIconClass.getMethod("getSystemIcon", arg_types);
Method setPointerIcon = SDLSurface.class.getMethod("setPointerIcon", PointerIconClass);
setPointerIcon.invoke(mSurface, getSystemIcon.invoke(null, SDL.getContext(), cursor_type));
} catch (Exception e) {
return false;
}
return true;
} }
public static PointerIcon getCursor() {
return mActiveCursor;
}
*/
} }
/** /**
@ -1547,14 +1565,6 @@ class SDLSurface extends SurfaceView implements SurfaceHolder.Callback,
event.values[2] / SensorManager.GRAVITY_EARTH); event.values[2] / SensorManager.GRAVITY_EARTH);
} }
} }
/**
* #CURSORIMPLEENTATION
@Override
public PointerIcon onResolvePointerIcon(MotionEvent event, int pointerIndex) {
return SDLActivity.getCursor();
}
*/
} }
/* This is a fake invisible editor view that receives the input and defines the /* This is a fake invisible editor view that receives the input and defines the

View File

@ -335,17 +335,16 @@ JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(nativeSetupJNI)(JNIEnv* mEnv, jclass c
"getManifestEnvironmentVariables", "()Z"); "getManifestEnvironmentVariables", "()Z");
midGetDisplayDPI = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass, "getDisplayDPI", "()Landroid/util/DisplayMetrics;"); midGetDisplayDPI = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass, "getDisplayDPI", "()Landroid/util/DisplayMetrics;");
/* Custom cursor implementation is only available on API 24 and above */
midCreateCustomCursor = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass, "createCustomCursor", "([IIIII)I"); midCreateCustomCursor = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass, "createCustomCursor", "([IIIII)I");
midSetCustomCursor = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass, "setCustomCursor", "(I)V"); midSetCustomCursor = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass, "setCustomCursor", "(I)Z");
midSetSystemCursor = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass, "setSystemCursor", "(I)V"); midSetSystemCursor = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass, "setSystemCursor", "(I)Z");
if (!midGetNativeSurface || if (!midGetNativeSurface ||
!midSetActivityTitle || !midSetWindowStyle || !midSetOrientation || !midGetContext || !midIsAndroidTV || !midInputGetInputDeviceIds || !midSetActivityTitle || !midSetWindowStyle || !midSetOrientation || !midGetContext || !midIsAndroidTV || !midInputGetInputDeviceIds ||
!midSendMessage || !midShowTextInput || !midIsScreenKeyboardShown || !midSendMessage || !midShowTextInput || !midIsScreenKeyboardShown ||
!midClipboardSetText || !midClipboardGetText || !midClipboardHasText || !midClipboardSetText || !midClipboardGetText || !midClipboardHasText ||
!midOpenAPKExpansionInputStream || !midGetManifestEnvironmentVariables|| !midGetDisplayDPI) { !midOpenAPKExpansionInputStream || !midGetManifestEnvironmentVariables || !midGetDisplayDPI ||
!midCreateCustomCursor || !midSetCustomCursor || !midSetSystemCursor) {
__android_log_print(ANDROID_LOG_WARN, "SDL", "Missing some Java callbacks, do you have the latest version of SDLActivity.java?"); __android_log_print(ANDROID_LOG_WARN, "SDL", "Missing some Java callbacks, do you have the latest version of SDLActivity.java?");
} }
@ -2178,34 +2177,29 @@ int Android_JNI_CreateCustomCursor(SDL_Surface *surface, int hot_x, int hot_y)
{ {
JNIEnv *mEnv = Android_JNI_GetEnv(); JNIEnv *mEnv = Android_JNI_GetEnv();
int custom_cursor = 0; int custom_cursor = 0;
if (midCreateCustomCursor) { jintArray pixels;
jintArray pixels; pixels = (*mEnv)->NewIntArray(mEnv, surface->w * surface->h);
pixels = (*mEnv)->NewIntArray(mEnv, surface->w * surface->h); if (pixels) {
if (!pixels) {
return 0;
}
(*mEnv)->SetIntArrayRegion(mEnv, pixels, 0, surface->w * surface->h, (int *)surface->pixels); (*mEnv)->SetIntArrayRegion(mEnv, pixels, 0, surface->w * surface->h, (int *)surface->pixels);
custom_cursor = (*mEnv)->CallStaticIntMethod(mEnv, mActivityClass, midCreateCustomCursor, pixels, surface->w, surface->h, hot_x, hot_y); custom_cursor = (*mEnv)->CallStaticIntMethod(mEnv, mActivityClass, midCreateCustomCursor, pixels, surface->w, surface->h, hot_x, hot_y);
(*mEnv)->DeleteLocalRef(mEnv, pixels); (*mEnv)->DeleteLocalRef(mEnv, pixels);
} else {
SDL_OutOfMemory();
} }
return custom_cursor; return custom_cursor;
} }
void Android_JNI_SetCustomCursor(int cursorID) SDL_bool Android_JNI_SetCustomCursor(int cursorID)
{ {
JNIEnv *mEnv = Android_JNI_GetEnv(); JNIEnv *mEnv = Android_JNI_GetEnv();
if (midSetCustomCursor) { return (*mEnv)->CallStaticBooleanMethod(mEnv, mActivityClass, midSetCustomCursor, cursorID);
(*mEnv)->CallStaticVoidMethod(mEnv, mActivityClass, midSetCustomCursor, cursorID);
}
} }
void Android_JNI_SetSystemCursor(int cursorID) SDL_bool Android_JNI_SetSystemCursor(int cursorID)
{ {
JNIEnv *mEnv = Android_JNI_GetEnv(); JNIEnv *mEnv = Android_JNI_GetEnv();
if (midSetSystemCursor) { return (*mEnv)->CallStaticBooleanMethod(mEnv, mActivityClass, midSetSystemCursor, cursorID);
(*mEnv)->CallStaticVoidMethod(mEnv, mActivityClass, midSetSystemCursor, cursorID);
}
} }
#endif /* __ANDROID__ */ #endif /* __ANDROID__ */

View File

@ -104,8 +104,8 @@ int Android_JNI_ShowMessageBox(const SDL_MessageBoxData *messageboxdata, int *bu
/* Cursor support */ /* Cursor support */
int Android_JNI_CreateCustomCursor(SDL_Surface *surface, int hot_x, int hot_y); int Android_JNI_CreateCustomCursor(SDL_Surface *surface, int hot_x, int hot_y);
void Android_JNI_SetCustomCursor(int cursorID); SDL_bool Android_JNI_SetCustomCursor(int cursorID);
void Android_JNI_SetSystemCursor(int cursorID); SDL_bool Android_JNI_SetSystemCursor(int cursorID);
/* Ends C function definitions when using C++ */ /* Ends C function definitions when using C++ */
#ifdef __cplusplus #ifdef __cplusplus

View File

@ -121,12 +121,18 @@ Android_ShowCursor(SDL_Cursor * cursor)
if (cursor) { if (cursor) {
SDL_AndroidCursorData *data = (SDL_AndroidCursorData*)cursor->driverdata; SDL_AndroidCursorData *data = (SDL_AndroidCursorData*)cursor->driverdata;
if (data->custom_cursor) { if (data->custom_cursor) {
Android_JNI_SetCustomCursor(data->custom_cursor); if (!Android_JNI_SetCustomCursor(data->custom_cursor)) {
return SDL_Unsupported();
}
} else { } else {
Android_JNI_SetSystemCursor(data->system_cursor); if (!Android_JNI_SetSystemCursor(data->system_cursor)) {
return SDL_Unsupported();
}
} }
} else { } else {
Android_JNI_SetSystemCursor(-1); if (!Android_JNI_SetSystemCursor(-1)) {
return SDL_Unsupported();
}
} }
return 0; return 0;
} }