Don't sleep on the main thread in touch processing code

This commit is contained in:
Cameron Gutman
2022-08-02 18:20:05 -05:00
parent a73129243c
commit dacd00708f
2 changed files with 58 additions and 26 deletions

View File

@@ -1,5 +1,7 @@
package com.limelight.binding.input.touch; package com.limelight.binding.input.touch;
import android.os.Handler;
import android.os.Looper;
import android.view.View; import android.view.View;
import com.limelight.nvstream.NvConnection; import com.limelight.nvstream.NvConnection;
@@ -26,6 +28,14 @@ public class AbsoluteTouchContext implements TouchContext {
private final NvConnection conn; private final NvConnection conn;
private final int actionIndex; private final int actionIndex;
private final View targetView; private final View targetView;
private final Handler handler;
private final Runnable leftButtonUpRunnable = new Runnable() {
@Override
public void run() {
conn.sendMouseButtonUp(MouseButtonPacket.BUTTON_LEFT);
}
};
private static final int SCROLL_SPEED_FACTOR = 3; private static final int SCROLL_SPEED_FACTOR = 3;
@@ -43,6 +53,7 @@ public class AbsoluteTouchContext implements TouchContext {
this.conn = conn; this.conn = conn;
this.actionIndex = actionIndex; this.actionIndex = actionIndex;
this.targetView = view; this.targetView = view;
this.handler = new Handler(Looper.getMainLooper());
} }
@Override @Override
@@ -112,18 +123,11 @@ public class AbsoluteTouchContext implements TouchContext {
// deadzone time. We'll need to send the touch down and up events now at the // deadzone time. We'll need to send the touch down and up events now at the
// original touch down position. // original touch down position.
tapConfirmed(); tapConfirmed();
try {
// FIXME: Sleeping on the main thread sucks
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
// InterruptedException clears the thread's interrupt status. Since we can't // Release the left mouse button in 100ms to allow for apps that use polling
// handle that here, we will re-interrupt the thread to set the interrupt // to detect mouse button presses.
// status back to true. handler.removeCallbacks(leftButtonUpRunnable);
Thread.currentThread().interrupt(); handler.postDelayed(leftButtonUpRunnable, 100);
}
conn.sendMouseButtonUp(MouseButtonPacket.BUTTON_LEFT);
} }
} }

View File

@@ -1,5 +1,7 @@
package com.limelight.binding.input.touch; package com.limelight.binding.input.touch;
import android.os.Handler;
import android.os.Looper;
import android.view.View; import android.view.View;
import com.limelight.nvstream.NvConnection; import com.limelight.nvstream.NvConnection;
@@ -31,6 +33,41 @@ public class RelativeTouchContext implements TouchContext {
private final int referenceHeight; private final int referenceHeight;
private final View targetView; private final View targetView;
private final PreferenceConfiguration prefConfig; private final PreferenceConfiguration prefConfig;
private final Handler handler;
// Indexed by MouseButtonPacket.BUTTON_XXX - 1
private final Runnable[] buttonUpRunnables = new Runnable[] {
new Runnable() {
@Override
public void run() {
conn.sendMouseButtonUp(MouseButtonPacket.BUTTON_LEFT);
}
},
new Runnable() {
@Override
public void run() {
conn.sendMouseButtonUp(MouseButtonPacket.BUTTON_MIDDLE);
}
},
new Runnable() {
@Override
public void run() {
conn.sendMouseButtonUp(MouseButtonPacket.BUTTON_RIGHT);
}
},
new Runnable() {
@Override
public void run() {
conn.sendMouseButtonUp(MouseButtonPacket.BUTTON_X1);
}
},
new Runnable() {
@Override
public void run() {
conn.sendMouseButtonUp(MouseButtonPacket.BUTTON_X2);
}
}
};
private static final int TAP_MOVEMENT_THRESHOLD = 20; private static final int TAP_MOVEMENT_THRESHOLD = 20;
private static final int TAP_DISTANCE_THRESHOLD = 25; private static final int TAP_DISTANCE_THRESHOLD = 25;
@@ -49,6 +86,7 @@ public class RelativeTouchContext implements TouchContext {
this.referenceHeight = referenceHeight; this.referenceHeight = referenceHeight;
this.targetView = view; this.targetView = view;
this.prefConfig = prefConfig; this.prefConfig = prefConfig;
this.handler = new Handler(Looper.getMainLooper());
} }
@Override @Override
@@ -138,21 +176,11 @@ public class RelativeTouchContext implements TouchContext {
// Lower the mouse button // Lower the mouse button
conn.sendMouseButtonDown(buttonIndex); conn.sendMouseButtonDown(buttonIndex);
// We need to sleep a bit here because some games // Release the mouse button in 100ms to allow for apps that use polling
// do input detection by polling // to detect mouse button presses.
try { Runnable buttonUpRunnable = buttonUpRunnables[buttonIndex - 1];
Thread.sleep(100); handler.removeCallbacks(buttonUpRunnable);
} catch (InterruptedException e) { handler.postDelayed(buttonUpRunnable, 100);
e.printStackTrace();
// InterruptedException clears the thread's interrupt status. Since we can't
// handle that here, we will re-interrupt the thread to set the interrupt
// status back to true.
Thread.currentThread().interrupt();
}
// Raise the mouse button
conn.sendMouseButtonUp(buttonIndex);
} }
} }