Skip to content

Commit

Permalink
Workaround[mouse]: Improve the workaround
Browse files Browse the repository at this point in the history
  • Loading branch information
artdeell committed Jun 19, 2024
1 parent bd02d25 commit f613d73
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
}

0 comments on commit f613d73

Please sign in to comment.