Skip to content

Commit ab36dec

Browse files
committed
Android: Handle mouse events separately from touch events
Use the new modern events like HOVER.
1 parent 475f333 commit ab36dec

File tree

10 files changed

+233
-46
lines changed

10 files changed

+233
-46
lines changed

Common/UI/ViewGroup.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -958,7 +958,7 @@ std::string GridLayoutList::DescribeText() const {
958958
}
959959

960960
TabHolder::TabHolder(Orientation orientation, float stripSize, LayoutParams *layoutParams)
961-
: LinearLayout(Opposite(orientation), layoutParams), stripSize_(stripSize) {
961+
: LinearLayout(Opposite(orientation), layoutParams) {
962962
SetSpacing(0.0f);
963963
if (orientation == ORIENT_HORIZONTAL) {
964964
tabStrip_ = new ChoiceStrip(orientation, new LayoutParams(WRAP_CONTENT, WRAP_CONTENT));

Common/UI/ViewGroup.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,6 @@ class TabHolder : public LinearLayout {
322322
ScrollView *tabScroll_ = nullptr;
323323
AnchorLayout *contents_ = nullptr;
324324

325-
float stripSize_;
326325
int currentTab_ = 0;
327326
std::vector<View *> tabs_;
328327
std::vector<AnchorTranslateTween *> tabTweens_;

UI/DevScreens.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1268,6 +1268,11 @@ bool TouchTestScreen::key(const KeyInput &key) {
12681268
}
12691269

12701270
void TouchTestScreen::axis(const AxisInput &axis) {
1271+
if (axis.deviceId == DEVICE_ID_MOUSE && (axis.axisId == JOYSTICK_AXIS_MOUSE_REL_X || axis.axisId == JOYSTICK_AXIS_MOUSE_REL_Y)) {
1272+
// These spam a lot, don't log for now.
1273+
return;
1274+
}
1275+
12711276
char buf[512];
12721277
snprintf(buf, sizeof(buf), "Axis: %s (%d) (value %1.3f) Device ID: %d",
12731278
KeyMap::GetAxisName(axis.axisId).c_str(), axis.axisId, axis.value, axis.deviceId);
@@ -1311,12 +1316,6 @@ void TouchTestScreen::DrawForeground(UIContext &dc) {
13111316
truncate_cpy(extra_debug, Android_GetInputDeviceDebugString().c_str());
13121317
#endif
13131318

1314-
// Hm, why don't we print all the info on Android?
1315-
#if PPSSPP_PLATFORM(ANDROID)
1316-
snprintf(buffer, sizeof(buffer),
1317-
"display_res: %dx%d\n",
1318-
(int)System_GetPropertyInt(SYSPROP_DISPLAY_XRES), (int)System_GetPropertyInt(SYSPROP_DISPLAY_YRES));
1319-
#else
13201319
snprintf(buffer, sizeof(buffer),
13211320
"display_res: %dx%d\n"
13221321
"dp_res: %dx%d pixel_res: %dx%d\n"
@@ -1329,7 +1328,6 @@ void TouchTestScreen::DrawForeground(UIContext &dc) {
13291328
g_display.dpi_scale_real,
13301329
delta * 1000.0, 1.0 / delta,
13311330
extra_debug);
1332-
#endif
13331331

13341332
// On Android, also add joystick debug data.
13351333
dc.DrawTextShadow(buffer, bounds.centerX(), bounds.y + 20.0f, 0xFFFFFFFF, FLAG_DYNAMIC_ASCII);

android/jni/app-android.cpp

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1242,6 +1242,70 @@ extern "C" void Java_org_ppsspp_ppsspp_NativeApp_joystickAxis(
12421242
env->ReleaseFloatArrayElements(values, valueBuffer, JNI_ABORT); // ABORT just means we don't want changes copied back!
12431243
}
12441244

1245+
extern "C" jboolean Java_org_ppsspp_ppsspp_NativeApp_mouse(
1246+
JNIEnv *env, jclass, jfloat x, jfloat y, int button, int action) {
1247+
if (!renderer_inited)
1248+
return false;
1249+
TouchInput input{};
1250+
1251+
static float last_x = 0.0f;
1252+
static float last_y = 0.0f;
1253+
1254+
if (x == -1.0f) {
1255+
x = last_x;
1256+
} else {
1257+
last_x = x;
1258+
}
1259+
if (y == -1.0f) {
1260+
y = last_y;
1261+
} else {
1262+
last_y = y;
1263+
}
1264+
1265+
x *= g_display.dpi_scale;
1266+
y *= g_display.dpi_scale;
1267+
1268+
if (button == 0) {
1269+
// It's a pure mouse move.
1270+
input.flags = TOUCH_MOUSE | TOUCH_MOVE;
1271+
input.x = x;
1272+
input.y = y;
1273+
input.id = 0;
1274+
} else {
1275+
input.buttons = button;
1276+
input.x = x;
1277+
input.y = y;
1278+
switch (action) {
1279+
case 1:
1280+
input.flags = TOUCH_MOUSE | TOUCH_DOWN;
1281+
break;
1282+
case 2:
1283+
input.flags = TOUCH_MOUSE | TOUCH_UP;
1284+
break;
1285+
}
1286+
input.id = 0;
1287+
}
1288+
INFO_LOG(Log::System, "New-style mouse event: %f %f %d %d -> x: %f y: %f buttons: %d flags: %04x", x, y, button, action, input.x, input.y, input.buttons, input.flags);
1289+
NativeTouch(input);
1290+
1291+
// Also send mouse button key events, for binding.
1292+
if (button) {
1293+
KeyInput input{};
1294+
input.deviceId = DEVICE_ID_MOUSE;
1295+
switch (button) {
1296+
case 1: input.keyCode = NKCODE_EXT_MOUSEBUTTON_1; break;
1297+
case 2: input.keyCode = NKCODE_EXT_MOUSEBUTTON_2; break;
1298+
case 3: input.keyCode = NKCODE_EXT_MOUSEBUTTON_3; break;
1299+
default: WARN_LOG(Log::System, "Unexpected mouse button %d", button);
1300+
}
1301+
input.flags = action == 1 ? KEY_DOWN : KEY_UP;
1302+
if (input.keyCode != 0) {
1303+
NativeKey(input);
1304+
}
1305+
}
1306+
return true;
1307+
}
1308+
12451309
extern "C" jboolean Java_org_ppsspp_ppsspp_NativeApp_mouseWheelEvent(
12461310
JNIEnv *env, jclass, jfloat x, jfloat y) {
12471311
if (!renderer_inited)

android/src/org/ppsspp/ppsspp/InputDeviceState.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ public boolean onJoystickMotion(MotionEvent event) {
163163
Log.i(TAG, "Not a joystick event: source = " + event.getSource());
164164
return false;
165165
}
166+
Log.i(TAG, "onjoystick");
166167
int count = 0;
167168
for (int i = 0; i < mAxes.length; i++) {
168169
int axisId = mAxes[i];

android/src/org/ppsspp/ppsspp/MogaHack.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public static void init(Controller controller, Context context) {
6868
// Convert implicit intent to explicit intent, see http://stackoverflow.com/a/26318757
6969
Intent intent = new Intent(IControllerService.class.getName());
7070
List<ResolveInfo> resolveInfos = context.getPackageManager().queryIntentServices(intent, 0);
71-
if (resolveInfos == null || resolveInfos.size() != 1) {
71+
if (resolveInfos.size() != 1) {
7272
// What? this doesn't do anything.
7373
// Log.e("MogaHack", "Somebody is trying to intercept our intent. Disabling MOGA controller for security.");
7474
}

android/src/org/ppsspp/ppsspp/NativeActivity.java

Lines changed: 68 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,6 @@ public abstract class NativeActivity extends Activity {
9797

9898
private Vibrator vibrator;
9999

100-
private boolean isXperiaPlay;
101-
102100
// This is to avoid losing the game/menu state etc when we are just
103101
// switched-away from or rotated etc.
104102
private boolean shuttingDown;
@@ -136,6 +134,8 @@ public abstract class NativeActivity extends Activity {
136134
public static final int REQUEST_CODE_CAMERA_PERMISSION = 3;
137135
public static final int REQUEST_CODE_MICROPHONE_PERMISSION = 4;
138136

137+
public static boolean useModernMouseEvents = false;
138+
139139
// Functions for the app activity to override to change behaviour.
140140

141141
public native void registerCallbacks();
@@ -275,7 +275,7 @@ private static ArrayList<String> getSdCardPaths(final Context context) {
275275
for (String var : varNames) {
276276
Log.i(TAG, "getSdCardPaths: Checking env " + var);
277277
String secStore = System.getenv("SECONDARY_STORAGE");
278-
if (secStore != null && secStore.length() > 0) {
278+
if (secStore != null && !secStore.isEmpty()) {
279279
list = new ArrayList<String>();
280280
list.add(secStore);
281281
break;
@@ -399,8 +399,6 @@ public void Initialize() {
399399
// All other device types are treated the same.
400400
}
401401

402-
isXperiaPlay = IsXperiaPlay();
403-
404402
String extStorageState = Environment.getExternalStorageState();
405403
String extStorageDir = Environment.getExternalStorageDirectory().getAbsolutePath();
406404
File externalFiles = this.getExternalFilesDir(null);
@@ -573,10 +571,8 @@ private void updateSystemUiVisibility() {
573571
// Need API 11 to check for existence of a vibrator? Zany.
574572
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
575573
public void checkForVibrator() {
576-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
577-
if (!vibrator.hasVibrator()) {
578-
vibrator = null;
579-
}
574+
if (!vibrator.hasVibrator()) {
575+
vibrator = null;
580576
}
581577
}
582578

@@ -780,7 +776,7 @@ protected void onDestroy() {
780776
do {
781777
try {
782778
Thread.sleep(10);
783-
} catch (InterruptedException e) {
779+
} catch (InterruptedException ignored) {
784780
}
785781
tries--;
786782
} while (nativeRenderer.isRenderingFrame() && tries > 0);
@@ -973,7 +969,7 @@ protected String getInputDeviceDebugString() {
973969
for (InputDeviceState input : inputPlayers) {
974970
buffer += input.getDebugString();
975971
}
976-
if (buffer.length() == 0) {
972+
if (buffer.isEmpty()) {
977973
buffer = "(no devices)";
978974
}
979975
return buffer;
@@ -982,15 +978,28 @@ protected String getInputDeviceDebugString() {
982978
}
983979
}
984980

985-
public boolean IsXperiaPlay() {
986-
return android.os.Build.MODEL.equals("R800a") || android.os.Build.MODEL.equals("R800i") || android.os.Build.MODEL.equals("R800x") || android.os.Build.MODEL.equals("R800at") || android.os.Build.MODEL.equals("SO-01D") || android.os.Build.MODEL.equals("zeus");
987-
}
988-
989981
// We grab the keys before onKeyDown/... even see them. This is also better because it lets us
990982
// distinguish devices.
991983
@Override
992984
public boolean dispatchKeyEvent(KeyEvent event) {
993-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1 && !isXperiaPlay) {
985+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1) {
986+
Log.i(TAG, "key event" + event.getSource());
987+
if (NativeSurfaceView.isFromSource(event, InputDevice.SOURCE_MOUSE)) {
988+
Log.i(TAG, "Forwarding key event from mouse");
989+
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && !useModernMouseEvents) {
990+
// Probably a right click
991+
switch (event.getAction()) {
992+
case KeyEvent.ACTION_DOWN:
993+
NativeApp.mouse(-1, -1, 2, 1);
994+
break;
995+
case KeyEvent.ACTION_UP:
996+
NativeApp.mouse(-1, -1, 2, 2);
997+
break;
998+
}
999+
}
1000+
return true;
1001+
}
1002+
9941003
InputDeviceState state = getInputDeviceState(event);
9951004
if (state == null) {
9961005
return super.dispatchKeyEvent(event);
@@ -1022,12 +1031,14 @@ public boolean dispatchKeyEvent(KeyEvent event) {
10221031
if (!passThrough) {
10231032
switch (event.getAction()) {
10241033
case KeyEvent.ACTION_DOWN:
1034+
Log.i(TAG, "KeyEvent Down");
10251035
if (state.onKeyDown(event)) {
10261036
return true;
10271037
}
10281038
break;
10291039

10301040
case KeyEvent.ACTION_UP:
1041+
Log.i(TAG, "KeyEvent Up");
10311042
if (state.onKeyUp(event)) {
10321043
return true;
10331044
}
@@ -1055,23 +1066,24 @@ public static String getInputDesc(InputDevice input) {
10551066

10561067
@TargetApi(Build.VERSION_CODES.N)
10571068
void sendMouseDelta(float dx, float dy) {
1058-
NativeApp.mouseDelta(dx, dy);
1069+
// Ignore zero deltas.
1070+
if (Math.abs(dx) > 0.001 || Math.abs(dx) > 0.001) {
1071+
NativeApp.mouseDelta(dx, dy);
1072+
}
10591073
}
10601074

10611075
@Override
10621076
@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
10631077
public boolean onGenericMotionEvent(MotionEvent event) {
1064-
// Log.d(TAG, "onGenericMotionEvent: " + event);
1078+
// Log.i(TAG, "NativeActivity onGenericMotionEvent: " + event);
10651079
if (InputDeviceState.inputSourceIsJoystick(event.getSource())) {
1066-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1) {
1067-
InputDeviceState state = getInputDeviceState(event);
1068-
if (state == null) {
1069-
Log.w(TAG, "Joystick event but failed to get input device state.");
1070-
return super.onGenericMotionEvent(event);
1071-
}
1072-
state.onJoystickMotion(event);
1073-
return true;
1080+
InputDeviceState state = getInputDeviceState(event);
1081+
if (state == null) {
1082+
Log.w(TAG, "Joystick event but failed to get input device state.");
1083+
return super.onGenericMotionEvent(event);
10741084
}
1085+
state.onJoystickMotion(event);
1086+
return true;
10751087
}
10761088

10771089
if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
@@ -1081,15 +1093,38 @@ public boolean onGenericMotionEvent(MotionEvent event) {
10811093
float dy = event.getAxisValue(MotionEvent.AXIS_RELATIVE_Y);
10821094
sendMouseDelta(dx, dy);
10831095
}
1096+
switch (event.getAction()) {
1097+
case MotionEvent.ACTION_HOVER_MOVE:
1098+
Log.i(TAG, "Action Hover Move");
1099+
// process the mouse hover movement...
1100+
NativeApp.mouse(event.getX(), event.getY(), 0, 0);
1101+
return true;
1102+
case MotionEvent.ACTION_SCROLL:
1103+
float scrollX = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1104+
float scrollY = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1105+
Log.i(TAG, "Action Scroll: " + scrollX + " " + scrollY);
1106+
NativeApp.mouseWheelEvent(scrollX, scrollY);
1107+
return true;
1108+
}
10841109
}
1110+
}
10851111

1086-
switch (event.getAction()) {
1087-
case MotionEvent.ACTION_HOVER_MOVE:
1088-
// process the mouse hover movement...
1089-
return true;
1090-
case MotionEvent.ACTION_SCROLL:
1091-
NativeApp.mouseWheelEvent(event.getAxisValue(MotionEvent.AXIS_HSCROLL), event.getAxisValue(MotionEvent.AXIS_VSCROLL));
1092-
return true;
1112+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
1113+
int button = event.getActionButton();
1114+
switch (event.getActionMasked()) {
1115+
case MotionEvent.ACTION_BUTTON_PRESS: {
1116+
Log.i(TAG, "action button press: button: " + button);
1117+
useModernMouseEvents = true;
1118+
NativeApp.mouse(event.getX(), event.getY(), button, 1);
1119+
return true;
1120+
}
1121+
case MotionEvent.ACTION_BUTTON_RELEASE: {
1122+
Log.i(TAG, "action button release: button: " + button);
1123+
NativeApp.mouse(event.getX(), event.getY(), button, 2);
1124+
return true;
1125+
}
1126+
default:
1127+
break;
10931128
}
10941129
}
10951130
return super.onGenericMotionEvent(event);

android/src/org/ppsspp/ppsspp/NativeApp.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public class NativeApp {
5050

5151
public static native void accelerometer(float x, float y, float z);
5252

53+
public static native void mouse(float x, float y, int button, int action);
5354
public static native void mouseDelta(float x, float y);
5455
public static native void sendMessageFromJava(String msg, String arg);
5556
public static native void sendRequestResult(int seqID, boolean result, String value, int iValue);

0 commit comments

Comments
 (0)