From f613d730cbdad45947410dc45a50b76a415329fd Mon Sep 17 00:00:00 2001 From: artdeell Date: Wed, 19 Jun 2024 17:45:51 +0300 Subject: [PATCH] Workaround[mouse]: Improve the workaround --- .../net/kdt/pojavlaunch/MainActivity.java | 23 ++++++++++++++++--- .../kdt/pojavlaunch/MinecraftGLSurface.java | 17 -------------- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/MainActivity.java b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/MainActivity.java index 3221abcbc2..4909197dbe 100644 --- a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/MainActivity.java +++ b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/MainActivity.java @@ -25,6 +25,7 @@ import android.os.IBinder; import android.provider.DocumentsContract; import android.util.Log; +import android.view.InputDevice; import android.view.KeyEvent; import android.view.LayoutInflater; import android.view.MotionEvent; @@ -38,6 +39,7 @@ import android.widget.Toast; import androidx.annotation.NonNull; +import androidx.annotation.RequiresApi; import androidx.core.content.ContextCompat; import androidx.drawerlayout.widget.DrawerLayout; @@ -630,10 +632,25 @@ public void onServiceDisconnected(ComponentName name) { } + /* + * Android 14 (or some devices, at least) seems to dispatch the the captured mouse events as trackball events + * due to a bug(?) somewhere(????) + */ + @RequiresApi(api = Build.VERSION_CODES.O) + private boolean checkCaptureDispatchConditions(MotionEvent event) { + int eventSource = event.getSource(); + // On my device, the mouse sends events as a relative mouse device. + // Not comparing with == here because apparently `eventSource` is a mask that can + // sometimes indicate multiple sources, like in the case of InputDevice.SOURCE_TOUCHPAD + // (which is *also* an InputDevice.SOURCE_MOUSE when controlling a cursor) + return (eventSource & InputDevice.SOURCE_MOUSE_RELATIVE) != 0 || + (eventSource & InputDevice.SOURCE_MOUSE) != 0; + } + @Override public boolean dispatchTrackballEvent(MotionEvent ev) { - // Android 14: route trackball events directly to the MinecraftGLSurface - if(minecraftGLView != null) return minecraftGLView.dispatchTrackballEvent(ev); - return super.dispatchTrackballEvent(ev); + if(MainActivity.isAndroid8OrHigher() && checkCaptureDispatchConditions(ev)) + return minecraftGLView.dispatchCapturedPointerEvent(ev); + else return super.dispatchTrackballEvent(ev); } } diff --git a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/MinecraftGLSurface.java b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/MinecraftGLSurface.java index 8e7b3a67c6..7ea6225126 100644 --- a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/MinecraftGLSurface.java +++ b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/MinecraftGLSurface.java @@ -79,7 +79,6 @@ public class MinecraftGLSurface extends View implements GrabListener { private final InGUIEventProcessor mInGUIProcessor = new InGUIEventProcessor(mScaleFactor); private TouchEventProcessor mCurrentTouchProcessor = mInGUIProcessor; private AndroidPointerCapture mPointerCapture; - private boolean mCaptureEngaged; private boolean mLastGrabState = false; public MinecraftGLSurface(Context context) { @@ -408,20 +407,4 @@ public void setSurfaceReadyListener(SurfaceReadyListener listener){ mSurfaceReadyListenerLock.notifyAll(); } } - - @Override - public void onPointerCaptureChange(boolean hasCapture) { - mCaptureEngaged = hasCapture; - super.onPointerCaptureChange(hasCapture); - } - - @Override - public boolean dispatchTrackballEvent(MotionEvent event) { - // Android 14 has broke the pointer capture, which causes the OS to route the captured pointer events to trackball-related methods. - // Correct this by sending trackball events as captured pointer events when the pointer is captured. - if(mCaptureEngaged && mPointerCapture != null && MainActivity.isAndroid8OrHigher()) { - return mPointerCapture.onCapturedPointer(this, event); - } - return super.dispatchTrackballEvent(event); - } }