Added support for Android relative mouse mode on API 24 and above
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
diff --git a/android-project/app/src/main/java/org/libsdl/app/SDLActivity.java b/android-project/app/src/main/java/org/libsdl/app/SDLActivity.java
index 3ea99da..b1cd982 100644
--- a/android-project/app/src/main/java/org/libsdl/app/SDLActivity.java
+++ b/android-project/app/src/main/java/org/libsdl/app/SDLActivity.java
@@ -79,11 +79,24 @@ public class SDLActivity extends Activity {
protected static SDLClipboardHandler mClipboardHandler;
protected static Hashtable<Integer, Object> mCursors;
protected static int mLastCursorID;
+ protected static SDLGenericMotionListener_API12 mMotionListener;
// This is what SDL runs in. It invokes SDL_main(), eventually
protected static Thread mSDLThread;
+ protected static SDLGenericMotionListener_API12 getMotionListener() {
+ if (mMotionListener == null) {
+ if (Build.VERSION.SDK_INT >= 24) {
+ mMotionListener = new SDLGenericMotionListener_API24();
+ } else {
+ mMotionListener = new SDLGenericMotionListener_API12();
+ }
+ }
+
+ return mMotionListener;
+ }
+
/**
* This method returns the name of the shared object with the application entry point
* It can be overridden by derived classes.
@@ -543,7 +556,7 @@ public class SDLActivity extends Activity {
public static native void onNativeKeyDown(int keycode);
public static native void onNativeKeyUp(int keycode);
public static native void onNativeKeyboardFocusLost();
- public static native void onNativeMouse(int button, int action, float x, float y);
+ public static native void onNativeMouse(int button, int action, float x, float y, boolean relative);
public static native void onNativeTouch(int touchDevId, int pointerFingerId,
int action, float x,
float y, float p);
@@ -644,6 +657,22 @@ public class SDLActivity extends Activity {
/**
* This method is called by SDL using JNI.
*/
+ public static boolean supportsRelativeMouse()
+ {
+ return SDLActivity.getMotionListener().supportsRelativeMouse();
+ }
+
+ /**
+ * This method is called by SDL using JNI.
+ */
+ public static boolean setRelativeMouseEnabled(boolean enabled)
+ {
+ return SDLActivity.getMotionListener().setRelativeMouseEnabled(enabled);
+ }
+
+ /**
+ * This method is called by SDL using JNI.
+ */
public static boolean sendMessage(int command, int param) {
if (mSingleton == null) {
return false;
@@ -1231,7 +1260,7 @@ class SDLSurface extends SurfaceView implements SurfaceHolder.Callback,
mSensorManager = (SensorManager)context.getSystemService(Context.SENSOR_SERVICE);
if (Build.VERSION.SDK_INT >= 12) {
- setOnGenericMotionListener(new SDLGenericMotionListener_API12());
+ setOnGenericMotionListener(SDLActivity.getMotionListener());
}
// Some arbitrary defaults to avoid a potential division by zero
@@ -1456,7 +1485,14 @@ class SDLSurface extends SurfaceView implements SurfaceHolder.Callback,
mouseButton = 1; // oh well.
}
}
- SDLActivity.onNativeMouse(mouseButton, action, event.getX(0), event.getY(0));
+
+ // We need to check if we're in relative mouse mode and get the axis offset rather than the x/y values
+ // if we are. We'll leverage our existing mouse motion listener
+ SDLGenericMotionListener_API12 motionListener = SDLActivity.getMotionListener();
+ x = motionListener.getEventX(event);
+ y = motionListener.getEventY(event);
+
+ SDLActivity.onNativeMouse(mouseButton, action, x, y, motionListener.inRelativeMode());
} else {
switch(action) {
case MotionEvent.ACTION_MOVE:
diff --git a/android-project/app/src/main/java/org/libsdl/app/SDLControllerManager.java b/android-project/app/src/main/java/org/libsdl/app/SDLControllerManager.java
index d0e9c8d..c2d394c 100644
--- a/android-project/app/src/main/java/org/libsdl/app/SDLControllerManager.java
+++ b/android-project/app/src/main/java/org/libsdl/app/SDLControllerManager.java
@@ -534,14 +534,14 @@ class SDLGenericMotionListener_API12 implements View.OnGenericMotionListener {
case MotionEvent.ACTION_SCROLL:
x = event.getAxisValue(MotionEvent.AXIS_HSCROLL, 0);
y = event.getAxisValue(MotionEvent.AXIS_VSCROLL, 0);
- SDLActivity.onNativeMouse(0, action, x, y);
+ SDLActivity.onNativeMouse(0, action, x, y, false);
return true;
case MotionEvent.ACTION_HOVER_MOVE:
x = event.getX(0);
y = event.getY(0);
- SDLActivity.onNativeMouse(0, action, x, y);
+ SDLActivity.onNativeMouse(0, action, x, y, false);
return true;
default:
@@ -556,5 +556,112 @@ class SDLGenericMotionListener_API12 implements View.OnGenericMotionListener {
// Event was not managed
return false;
}
+
+ public boolean supportsRelativeMouse() {
+ return false;
+ }
+
+ public boolean inRelativeMode() {
+ return false;
+ }
+
+ public boolean setRelativeMouseEnabled(boolean enabled) {
+ return false;
+ }
+
+ public float getEventX(MotionEvent event) {
+ return event.getX(0);
+ }
+
+ public float getEventY(MotionEvent event) {
+ return event.getY(0);
+ }
+
+}
+
+class SDLGenericMotionListener_API24 extends SDLGenericMotionListener_API12 {
+ // Generic Motion (mouse hover, joystick...) events go here
+
+ private boolean mRelativeModeEnabled;
+
+ @Override
+ public boolean onGenericMotion(View v, MotionEvent event) {
+ float x, y;
+ int action;
+
+ switch ( event.getSource() ) {
+ case InputDevice.SOURCE_JOYSTICK:
+ case InputDevice.SOURCE_GAMEPAD:
+ case InputDevice.SOURCE_DPAD:
+ return SDLControllerManager.handleJoystickMotionEvent(event);
+
+ case InputDevice.SOURCE_MOUSE:
+ if (!SDLActivity.mSeparateMouseAndTouch) {
+ break;
+ }
+ action = event.getActionMasked();
+ switch (action) {
+ case MotionEvent.ACTION_SCROLL:
+ x = event.getAxisValue(MotionEvent.AXIS_HSCROLL, 0);
+ y = event.getAxisValue(MotionEvent.AXIS_VSCROLL, 0);
+ SDLActivity.onNativeMouse(0, action, x, y, false);
+ return true;
+
+ case MotionEvent.ACTION_HOVER_MOVE:
+ if (mRelativeModeEnabled) {
+ x = event.getAxisValue(MotionEvent.AXIS_RELATIVE_X);
+ y = event.getAxisValue(MotionEvent.AXIS_RELATIVE_Y);
+ }
+ else {
+ x = event.getX(0);
+ y = event.getY(0);
+ }
+
+ SDLActivity.onNativeMouse(0, action, x, y, mRelativeModeEnabled);
+ return true;
+
+ default:
+ break;
+ }
+ break;
+
+ default:
+ break;
+ }
+
+ // Event was not managed
+ return false;
+ }
+
+ public boolean supportsRelativeMouse() {
+ return true;
+ }
+
+ public boolean inRelativeMode() {
+ return mRelativeModeEnabled;
+ }
+
+ public boolean setRelativeMouseEnabled(boolean enabled) {
+ mRelativeModeEnabled = enabled;
+ return true;
+ }
+
+ public float getEventX(MotionEvent event) {
+ if (mRelativeModeEnabled) {
+ return event.getAxisValue(MotionEvent.AXIS_RELATIVE_X);
+ }
+ else {
+ return event.getX(0);
+ }
+ }
+
+ public float getEventY(MotionEvent event) {
+ if (mRelativeModeEnabled) {
+ return event.getAxisValue(MotionEvent.AXIS_RELATIVE_Y);
+ }
+ else {
+ return event.getY(0);
+ }
+ }
}