WIP handle touch events on server

This commit is contained in:
Romain Vimont 2019-09-15 22:28:59 +02:00
parent 7b143b8fbd
commit 91ba44b653
3 changed files with 178 additions and 0 deletions

View File

@ -13,6 +13,8 @@ import java.io.IOException;
public class Controller {
private static final int MAX_FINGERS = 10;
private final Device device;
private final DesktopConnection connection;
private final DeviceMessageSender sender;
@ -23,10 +25,16 @@ public class Controller {
private final MotionEvent.PointerProperties[] mousePointerProperties = {new MotionEvent.PointerProperties()};
private final MotionEvent.PointerCoords[] mousePointerCoords = {new MotionEvent.PointerCoords()};
private long lastTouchDown;
private final FingersState fingersState = new FingersState(MAX_FINGERS);
private final MotionEvent.PointerProperties[] touchPointerProperties = new MotionEvent.PointerProperties[MAX_FINGERS];
private final MotionEvent.PointerCoords[] touchPointerCoords = new MotionEvent.PointerCoords[MAX_FINGERS];
public Controller(Device device, DesktopConnection connection) {
this.device = device;
this.connection = connection;
initMousePointer();
initTouchPointers();
sender = new DeviceMessageSender(connection);
}
@ -41,6 +49,17 @@ public class Controller {
coords.size = 1;
}
private void initTouchPointers() {
for (int i = 0; i < MAX_FINGERS; ++i) {
MotionEvent.PointerProperties props = new MotionEvent.PointerProperties();
props.toolType = MotionEvent.TOOL_TYPE_FINGER;
MotionEvent.PointerCoords coords = new MotionEvent.PointerCoords();
coords.orientation = 0;
coords.size = 1;
}
}
private void setMousePointerCoords(Point point) {
MotionEvent.PointerCoords coords = mousePointerCoords[0];
coords.x = point.getX();
@ -164,6 +183,34 @@ public class Controller {
return injectEvent(event);
}
private boolean injectTouch(int action, int fingerId, Position position, float pressure) {
long now = SystemClock.uptimeMillis();
if (action == MotionEvent.ACTION_DOWN) {
lastTouchDown = now;
}
Point point = device.getPhysicalPoint(position);
if (point == null) {
// ignore event
return false;
}
if (action == MotionEvent.ACTION_UP) {
if (!fingersState.unset(fingerId)) {
Ln.w("Unexpected ACTION_UP on unknown finger");
return false;
}
} else {
// ACTION_DOWN or ACTION_MOVE
if (!fingersState.set(fingerId, point, pressure)) {
Ln.w("Too many fingers for touch event");
return false;
}
}
int pointerCount = fingersState.update(touchPointerProperties, touchPointerCoords);
MotionEvent event = MotionEvent.obtain(lastTouchDown, now, action, pointerCount, touchPointerProperties, touchPointerCoords, 0, 0, 1f, 1f, 0, 0,
InputDevice.SOURCE_TOUCHSCREEN, 0);
return injectEvent(event);
}
private boolean injectScroll(Position position, int hScroll, int vScroll) {
long now = SystemClock.uptimeMillis();
Point point = device.getPhysicalPoint(position);

View File

@ -0,0 +1,32 @@
package com.genymobile.scrcpy;
public class Finger {
private final long id;
private Point point;
private float pressure;
public Finger(long id) {
this.id = id;
}
public long getId() {
return id;
}
public Point getPoint() {
return point;
}
public void setPoint(Point point) {
this.point = point;
}
public float getPressure() {
return pressure;
}
public void setPressure(float pressure) {
this.pressure = pressure;
}
}

View File

@ -0,0 +1,99 @@
package com.genymobile.scrcpy;
import android.view.MotionEvent;
public class FingersState {
/**
* Array of enabled fingers (can contain "null" holes).
* <p>
* Once a Finger (identified by its id received from the client) is enabled, it is never moved.
* <p>
* Its index is its local identifier injected into MotionEvents.
*/
private final Finger[] fingers;
public FingersState(int maxFingers) {
fingers = new Finger[maxFingers];
}
private int indexOf(long id) {
for (int i = 0; i < fingers.length; ++i) {
Finger finger = fingers[i];
if (finger != null && finger.getId() == id) {
return i;
}
}
return -1;
}
private int indexOfFirstEmpty() {
for (int i = 0; i < fingers.length; ++i) {
if (fingers[i] == null) {
return i;
}
}
return -1;
}
private Finger create(long id) {
int index = indexOf(id);
if (index != -1) {
// already exists, return it
return fingers[index];
}
int firstEmpty = indexOfFirstEmpty();
if (firstEmpty == -1) {
// it's full
return null;
}
Finger finger = new Finger(id);
fingers[firstEmpty] = finger;
return finger;
}
public boolean unset(int id) {
int index = indexOf(id);
if (index == -1) {
return false;
}
fingers[index] = null;
return true;
}
public boolean set(int id, Point point, float pressure) {
Finger finger = create(id);
if (finger == null) {
return false;
}
finger.setPoint(point);
finger.setPressure(pressure);
return true;
}
/**
* Initialize the motion event parameters.
*
* @param props the pointer properties
* @param coords the pointer coordinates
* @return The number of items initialized (the number of fingers).
*/
public int update(MotionEvent.PointerProperties[] props, MotionEvent.PointerCoords[] coords) {
int count = 0;
for (int i = 0; i < fingers.length; ++i) {
Finger finger = fingers[i];
if (finger != null) {
// id 0 is reserved for mouse events
props[count].id = i + 1;
Point point = finger.getPoint();
coords[i].x = point.getX();
coords[i].y = point.getY();
coords[i].pressure = finger.getPressure();
++count;
}
}
return count;
}
}