From 04545ecbb0fd8211199c3ff4f0d7600facc99f76 Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Sat, 17 Jul 2021 14:01:12 -0500 Subject: [PATCH] Avoid tons of redundant calls to InputEvent.getSource() --- app/src/main/java/com/limelight/Game.java | 31 ++++++++++--------- .../AndroidNativePointerCaptureProvider.java | 5 +-- 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/com/limelight/Game.java b/app/src/main/java/com/limelight/Game.java index 77d2b537..45c4d91c 100644 --- a/app/src/main/java/com/limelight/Game.java +++ b/app/src/main/java/com/limelight/Game.java @@ -1089,8 +1089,9 @@ public class Game extends Activity implements SurfaceHolder.Callback, // Handle a synthetic back button event that some Android OS versions // create as a result of a right-click. This event WILL repeat if // the right mouse button is held down, so we ignore those. - if ((event.getSource() == InputDevice.SOURCE_MOUSE || - event.getSource() == InputDevice.SOURCE_MOUSE_RELATIVE) && + int eventSource = event.getSource(); + if ((eventSource == InputDevice.SOURCE_MOUSE || + eventSource == InputDevice.SOURCE_MOUSE_RELATIVE) && event.getKeyCode() == KeyEvent.KEYCODE_BACK) { // Send the right mouse button event if mouse back and forward @@ -1159,8 +1160,9 @@ public class Game extends Activity implements SurfaceHolder.Callback, // Handle a synthetic back button event that some Android OS versions // create as a result of a right-click. - if ((event.getSource() == InputDevice.SOURCE_MOUSE || - event.getSource() == InputDevice.SOURCE_MOUSE_RELATIVE) && + int eventSource = event.getSource(); + if ((eventSource == InputDevice.SOURCE_MOUSE || + eventSource == InputDevice.SOURCE_MOUSE_RELATIVE) && event.getKeyCode() == KeyEvent.KEYCODE_BACK) { // Send the right mouse button event if mouse back and forward @@ -1233,19 +1235,20 @@ public class Game extends Activity implements SurfaceHolder.Callback, return false; } - if ((event.getSource() & InputDevice.SOURCE_CLASS_JOYSTICK) != 0) { + int eventSource = event.getSource(); + if ((eventSource & InputDevice.SOURCE_CLASS_JOYSTICK) != 0) { if (controllerHandler.handleMotionEvent(event)) { return true; } } - else if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0 || - (event.getSource() & InputDevice.SOURCE_CLASS_POSITION) != 0 || - event.getSource() == InputDevice.SOURCE_MOUSE_RELATIVE) + else if ((eventSource & InputDevice.SOURCE_CLASS_POINTER) != 0 || + (eventSource & InputDevice.SOURCE_CLASS_POSITION) != 0 || + eventSource == InputDevice.SOURCE_MOUSE_RELATIVE) { // This case is for mice and non-finger touch devices - if (event.getSource() == InputDevice.SOURCE_MOUSE || - (event.getSource() & InputDevice.SOURCE_CLASS_POSITION) != 0 || // SOURCE_TOUCHPAD - event.getSource() == InputDevice.SOURCE_MOUSE_RELATIVE || + if (eventSource == InputDevice.SOURCE_MOUSE || + (eventSource & InputDevice.SOURCE_CLASS_POSITION) != 0 || // SOURCE_TOUCHPAD + eventSource == InputDevice.SOURCE_MOUSE_RELATIVE || (event.getPointerCount() >= 1 && (event.getToolType(0) == MotionEvent.TOOL_TYPE_MOUSE || event.getToolType(0) == MotionEvent.TOOL_TYPE_STYLUS || @@ -1272,7 +1275,7 @@ public class Game extends Activity implements SurfaceHolder.Callback, conn.sendMouseMove(deltaX, deltaY); } } - else if ((event.getSource() & InputDevice.SOURCE_CLASS_POSITION) != 0) { + else if ((eventSource & InputDevice.SOURCE_CLASS_POSITION) != 0) { // If this input device is not associated with the view itself (like a trackpad), // we'll convert the device-specific coordinates to use to send the cursor position. // This really isn't ideal but it's probably better than nothing. @@ -1282,8 +1285,8 @@ public class Game extends Activity implements SurfaceHolder.Callback, // support pointer capture. InputDevice device = event.getDevice(); if (device != null) { - InputDevice.MotionRange xRange = device.getMotionRange(MotionEvent.AXIS_X, event.getSource()); - InputDevice.MotionRange yRange = device.getMotionRange(MotionEvent.AXIS_Y, event.getSource()); + InputDevice.MotionRange xRange = device.getMotionRange(MotionEvent.AXIS_X, eventSource); + InputDevice.MotionRange yRange = device.getMotionRange(MotionEvent.AXIS_Y, eventSource); // All touchpads coordinate planes should start at (0, 0) if (xRange != null && yRange != null && xRange.getMin() == 0 && yRange.getMin() == 0) { diff --git a/app/src/main/java/com/limelight/binding/input/capture/AndroidNativePointerCaptureProvider.java b/app/src/main/java/com/limelight/binding/input/capture/AndroidNativePointerCaptureProvider.java index 09bb97fc..555a3290 100644 --- a/app/src/main/java/com/limelight/binding/input/capture/AndroidNativePointerCaptureProvider.java +++ b/app/src/main/java/com/limelight/binding/input/capture/AndroidNativePointerCaptureProvider.java @@ -41,8 +41,9 @@ public class AndroidNativePointerCaptureProvider extends AndroidPointerIconCaptu // SOURCE_MOUSE_RELATIVE is how SOURCE_MOUSE appears when our view has pointer capture. // SOURCE_TOUCHPAD will have relative axes populated iff our view has pointer capture. // See https://developer.android.com/reference/android/view/View#requestPointerCapture() - return event.getSource() == InputDevice.SOURCE_MOUSE_RELATIVE || - (event.getSource() == InputDevice.SOURCE_TOUCHPAD && targetView.hasPointerCapture()); + int eventSource = event.getSource(); + return eventSource == InputDevice.SOURCE_MOUSE_RELATIVE || + (eventSource == InputDevice.SOURCE_TOUCHPAD && targetView.hasPointerCapture()); } @Override