Compare commits
9 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
783eb95795 | ||
|
7e21f28f2c | ||
|
ccfd4db370 | ||
|
6d26ed888e | ||
|
73afdaf251 | ||
|
bb1bf99c6e | ||
|
7aeec1c331 | ||
|
3bb2222a34 | ||
|
bdf2644664 |
@ -20,6 +20,12 @@ buffer_write32be(uint8_t *buf, uint32_t value) {
|
||||
buf[3] = value;
|
||||
}
|
||||
|
||||
static inline void
|
||||
buffer_write64be(uint8_t *buf, uint64_t value) {
|
||||
buffer_write32be(buf, value >> 32);
|
||||
buffer_write32be(&buf[4], (uint32_t) value);
|
||||
}
|
||||
|
||||
static inline uint16_t
|
||||
buffer_read16be(const uint8_t *buf) {
|
||||
return (buf[0] << 8) | buf[1];
|
||||
|
@ -1,6 +1,7 @@
|
||||
#include "control_msg.h"
|
||||
|
||||
#include <string.h>
|
||||
#include <SDL2/SDL_assert.h>
|
||||
|
||||
#include "config.h"
|
||||
#include "buffer_util.h"
|
||||
@ -24,6 +25,16 @@ write_string(const char *utf8, size_t max_len, unsigned char *buf) {
|
||||
return 2 + len;
|
||||
}
|
||||
|
||||
static uint16_t
|
||||
to_fixed_point_16(float f) {
|
||||
SDL_assert(f >= 0.0f && f <= 1.0f);
|
||||
uint32_t u = f * 0x1p16f; // 2^16
|
||||
if (u >= 0xffff) {
|
||||
u = 0xffff;
|
||||
}
|
||||
return (uint16_t) u;
|
||||
}
|
||||
|
||||
size_t
|
||||
control_msg_serialize(const struct control_msg *msg, unsigned char *buf) {
|
||||
buf[0] = msg->type;
|
||||
@ -43,6 +54,14 @@ control_msg_serialize(const struct control_msg *msg, unsigned char *buf) {
|
||||
buffer_write32be(&buf[2], msg->inject_mouse_event.buttons);
|
||||
write_position(&buf[6], &msg->inject_mouse_event.position);
|
||||
return 18;
|
||||
case CONTROL_MSG_TYPE_INJECT_TOUCH_EVENT:
|
||||
buf[1] = msg->inject_touch_event.action;
|
||||
buffer_write64be(&buf[2], msg->inject_touch_event.finger_id);
|
||||
write_position(&buf[10], &msg->inject_touch_event.position);
|
||||
uint16_t pressure =
|
||||
to_fixed_point_16(msg->inject_touch_event.pressure);
|
||||
buffer_write16be(&buf[22], pressure);
|
||||
return 24;
|
||||
case CONTROL_MSG_TYPE_INJECT_SCROLL_EVENT:
|
||||
write_position(&buf[1], &msg->inject_scroll_event.position);
|
||||
buffer_write32be(&buf[13],
|
||||
|
@ -19,6 +19,7 @@ enum control_msg_type {
|
||||
CONTROL_MSG_TYPE_INJECT_KEYCODE,
|
||||
CONTROL_MSG_TYPE_INJECT_TEXT,
|
||||
CONTROL_MSG_TYPE_INJECT_MOUSE_EVENT,
|
||||
CONTROL_MSG_TYPE_INJECT_TOUCH_EVENT,
|
||||
CONTROL_MSG_TYPE_INJECT_SCROLL_EVENT,
|
||||
CONTROL_MSG_TYPE_BACK_OR_SCREEN_ON,
|
||||
CONTROL_MSG_TYPE_EXPAND_NOTIFICATION_PANEL,
|
||||
@ -50,6 +51,12 @@ struct control_msg {
|
||||
enum android_motionevent_buttons buttons;
|
||||
struct position position;
|
||||
} inject_mouse_event;
|
||||
struct {
|
||||
enum android_motionevent_action action;
|
||||
uint64_t finger_id;
|
||||
struct position position;
|
||||
float pressure;
|
||||
} inject_touch_event;
|
||||
struct {
|
||||
struct position position;
|
||||
int32_t hscroll;
|
||||
|
@ -207,6 +207,34 @@ convert_mouse_motion(const SDL_MouseMotionEvent *from, struct size screen_size,
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool
|
||||
convert_touch_action(SDL_EventType from, enum android_motionevent_action *to) {
|
||||
switch (from) {
|
||||
MAP(SDL_FINGERMOTION, AMOTION_EVENT_ACTION_MOVE);
|
||||
MAP(SDL_FINGERDOWN, AMOTION_EVENT_ACTION_DOWN);
|
||||
MAP(SDL_FINGERUP, AMOTION_EVENT_ACTION_UP);
|
||||
FAIL;
|
||||
}
|
||||
}
|
||||
|
||||
bool
|
||||
convert_touch(const SDL_TouchFingerEvent *from, struct size screen_size,
|
||||
struct control_msg *to) {
|
||||
to->type = CONTROL_MSG_TYPE_INJECT_TOUCH_EVENT;
|
||||
|
||||
if (!convert_touch_action(from->type, &to->inject_touch_event.action)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
to->inject_touch_event.finger_id = from->fingerId;
|
||||
to->inject_touch_event.position.screen_size = screen_size;
|
||||
// SDL touch event coordinates are normalized in the range [0; 1]
|
||||
to->inject_touch_event.position.point.x = from->x * screen_size.width;
|
||||
to->inject_touch_event.position.point.y = from->y * screen_size.height;
|
||||
to->inject_touch_event.pressure = from->pressure;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool
|
||||
convert_mouse_wheel(const SDL_MouseWheelEvent *from, struct position position,
|
||||
struct control_msg *to) {
|
||||
|
@ -30,6 +30,10 @@ bool
|
||||
convert_mouse_motion(const SDL_MouseMotionEvent *from, struct size screen_size,
|
||||
struct control_msg *to);
|
||||
|
||||
bool
|
||||
convert_touch(const SDL_TouchFingerEvent *from, struct size screen_size,
|
||||
struct control_msg *to);
|
||||
|
||||
// on Android, a scroll event requires the current mouse position
|
||||
bool
|
||||
convert_mouse_wheel(const SDL_MouseWheelEvent *from, struct position position,
|
||||
|
@ -389,6 +389,10 @@ input_manager_process_mouse_motion(struct input_manager *input_manager,
|
||||
// do not send motion events when no button is pressed
|
||||
return;
|
||||
}
|
||||
if (event->which == SDL_TOUCH_MOUSEID) {
|
||||
// simulated from touch events, so it's a duplicate
|
||||
return;
|
||||
}
|
||||
struct control_msg msg;
|
||||
if (convert_mouse_motion(event, input_manager->screen->frame_size, &msg)) {
|
||||
if (!controller_push_msg(input_manager->controller, &msg)) {
|
||||
@ -397,6 +401,17 @@ input_manager_process_mouse_motion(struct input_manager *input_manager,
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
input_manager_process_touch(struct input_manager *input_manager,
|
||||
const SDL_TouchFingerEvent *event) {
|
||||
struct control_msg msg;
|
||||
if (convert_touch(event, input_manager->screen->frame_size, &msg)) {
|
||||
if (!controller_push_msg(input_manager->controller, &msg)) {
|
||||
LOGW("Could not request 'inject touch event'");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static bool
|
||||
is_outside_device_screen(struct input_manager *input_manager, int x, int y)
|
||||
{
|
||||
|
@ -29,6 +29,10 @@ void
|
||||
input_manager_process_mouse_motion(struct input_manager *input_manager,
|
||||
const SDL_MouseMotionEvent *event);
|
||||
|
||||
void
|
||||
input_manager_process_touch(struct input_manager *input_manager,
|
||||
const SDL_TouchFingerEvent *event);
|
||||
|
||||
void
|
||||
input_manager_process_mouse_button(struct input_manager *input_manager,
|
||||
const SDL_MouseButtonEvent *event,
|
||||
|
@ -181,6 +181,11 @@ handle_event(SDL_Event *event, bool control) {
|
||||
input_manager_process_mouse_button(&input_manager, &event->button,
|
||||
control);
|
||||
break;
|
||||
case SDL_FINGERMOTION:
|
||||
case SDL_FINGERDOWN:
|
||||
case SDL_FINGERUP:
|
||||
input_manager_process_touch(&input_manager, &event->tfinger);
|
||||
break;
|
||||
case SDL_DROPFILE: {
|
||||
if (!control) {
|
||||
break;
|
||||
|
@ -99,6 +99,41 @@ static void test_serialize_inject_mouse_event(void) {
|
||||
};
|
||||
assert(!memcmp(buf, expected, sizeof(expected)));
|
||||
}
|
||||
#include <stdio.h>
|
||||
static void test_serialize_inject_touch_event(void) {
|
||||
struct control_msg msg = {
|
||||
.type = CONTROL_MSG_TYPE_INJECT_TOUCH_EVENT,
|
||||
.inject_touch_event = {
|
||||
.action = AMOTION_EVENT_ACTION_DOWN,
|
||||
.finger_id = 0x1234567887654321L,
|
||||
.position = {
|
||||
.point = {
|
||||
.x = 100,
|
||||
.y = 200,
|
||||
},
|
||||
.screen_size = {
|
||||
.width = 1080,
|
||||
.height = 1920,
|
||||
},
|
||||
},
|
||||
.pressure = 1.0f,
|
||||
},
|
||||
};
|
||||
|
||||
unsigned char buf[CONTROL_MSG_SERIALIZED_MAX_SIZE];
|
||||
int size = control_msg_serialize(&msg, buf);
|
||||
assert(size == 24);
|
||||
|
||||
const unsigned char expected[] = {
|
||||
CONTROL_MSG_TYPE_INJECT_TOUCH_EVENT,
|
||||
0x00, // AKEY_EVENT_ACTION_DOWN
|
||||
0x12, 0x34, 0x56, 0x78, 0x87, 0x65, 0x43, 0x21, // finger id
|
||||
0x00, 0x00, 0x00, 0x64, 0x00, 0x00, 0x00, 0xc8, // 100 200
|
||||
0x04, 0x38, 0x07, 0x80, // 1080 1920
|
||||
0xff, 0xff, // pressure
|
||||
};
|
||||
assert(!memcmp(buf, expected, sizeof(expected)));
|
||||
}
|
||||
|
||||
static void test_serialize_inject_scroll_event(void) {
|
||||
struct control_msg msg = {
|
||||
@ -237,6 +272,7 @@ int main(void) {
|
||||
test_serialize_inject_text();
|
||||
test_serialize_inject_text_long();
|
||||
test_serialize_inject_mouse_event();
|
||||
test_serialize_inject_touch_event();
|
||||
test_serialize_inject_scroll_event();
|
||||
test_serialize_back_or_screen_on();
|
||||
test_serialize_expand_notification_panel();
|
||||
|
@ -8,13 +8,14 @@ public final class ControlMessage {
|
||||
public static final int TYPE_INJECT_KEYCODE = 0;
|
||||
public static final int TYPE_INJECT_TEXT = 1;
|
||||
public static final int TYPE_INJECT_MOUSE_EVENT = 2;
|
||||
public static final int TYPE_INJECT_SCROLL_EVENT = 3;
|
||||
public static final int TYPE_BACK_OR_SCREEN_ON = 4;
|
||||
public static final int TYPE_EXPAND_NOTIFICATION_PANEL = 5;
|
||||
public static final int TYPE_COLLAPSE_NOTIFICATION_PANEL = 6;
|
||||
public static final int TYPE_GET_CLIPBOARD = 7;
|
||||
public static final int TYPE_SET_CLIPBOARD = 8;
|
||||
public static final int TYPE_SET_SCREEN_POWER_MODE = 9;
|
||||
public static final int TYPE_INJECT_TOUCH_EVENT = 3;
|
||||
public static final int TYPE_INJECT_SCROLL_EVENT = 4;
|
||||
public static final int TYPE_BACK_OR_SCREEN_ON = 5;
|
||||
public static final int TYPE_EXPAND_NOTIFICATION_PANEL = 6;
|
||||
public static final int TYPE_COLLAPSE_NOTIFICATION_PANEL = 7;
|
||||
public static final int TYPE_GET_CLIPBOARD = 8;
|
||||
public static final int TYPE_SET_CLIPBOARD = 9;
|
||||
public static final int TYPE_SET_SCREEN_POWER_MODE = 10;
|
||||
|
||||
private int type;
|
||||
private String text;
|
||||
@ -22,6 +23,8 @@ public final class ControlMessage {
|
||||
private int action; // KeyEvent.ACTION_* or MotionEvent.ACTION_* or POWER_MODE_*
|
||||
private int keycode; // KeyEvent.KEYCODE_*
|
||||
private int buttons; // MotionEvent.BUTTON_*
|
||||
private long fingerId;
|
||||
private float pressure;
|
||||
private Position position;
|
||||
private int hScroll;
|
||||
private int vScroll;
|
||||
@ -54,6 +57,16 @@ public final class ControlMessage {
|
||||
return msg;
|
||||
}
|
||||
|
||||
public static ControlMessage createInjectTouchEvent(int action, long fingerId, Position position, float pressure) {
|
||||
ControlMessage msg = new ControlMessage();
|
||||
msg.type = TYPE_INJECT_TOUCH_EVENT;
|
||||
msg.action = action;
|
||||
msg.fingerId = fingerId;
|
||||
msg.pressure = pressure;
|
||||
msg.position = position;
|
||||
return msg;
|
||||
}
|
||||
|
||||
public static ControlMessage createInjectScrollEvent(Position position, int hScroll, int vScroll) {
|
||||
ControlMessage msg = new ControlMessage();
|
||||
msg.type = TYPE_INJECT_SCROLL_EVENT;
|
||||
@ -110,6 +123,14 @@ public final class ControlMessage {
|
||||
return buttons;
|
||||
}
|
||||
|
||||
public long getFingerId() {
|
||||
return fingerId;
|
||||
}
|
||||
|
||||
public float getPressure() {
|
||||
return pressure;
|
||||
}
|
||||
|
||||
public Position getPosition() {
|
||||
return position;
|
||||
}
|
||||
|
@ -10,6 +10,7 @@ public class ControlMessageReader {
|
||||
|
||||
private static final int INJECT_KEYCODE_PAYLOAD_LENGTH = 9;
|
||||
private static final int INJECT_MOUSE_EVENT_PAYLOAD_LENGTH = 17;
|
||||
private static final int INJECT_TOUCH_EVENT_PAYLOAD_LENGTH = 21;
|
||||
private static final int INJECT_SCROLL_EVENT_PAYLOAD_LENGTH = 20;
|
||||
private static final int SET_SCREEN_POWER_MODE_PAYLOAD_LENGTH = 1;
|
||||
|
||||
@ -62,6 +63,9 @@ public class ControlMessageReader {
|
||||
case ControlMessage.TYPE_INJECT_MOUSE_EVENT:
|
||||
msg = parseInjectMouseEvent();
|
||||
break;
|
||||
case ControlMessage.TYPE_INJECT_TOUCH_EVENT:
|
||||
msg = parseInjectTouchEvent();
|
||||
break;
|
||||
case ControlMessage.TYPE_INJECT_SCROLL_EVENT:
|
||||
msg = parseInjectScrollEvent();
|
||||
break;
|
||||
@ -130,6 +134,20 @@ public class ControlMessageReader {
|
||||
return ControlMessage.createInjectMouseEvent(action, buttons, position);
|
||||
}
|
||||
|
||||
private ControlMessage parseInjectTouchEvent() {
|
||||
if (buffer.remaining() < INJECT_TOUCH_EVENT_PAYLOAD_LENGTH) {
|
||||
return null;
|
||||
}
|
||||
int action = toUnsigned(buffer.get());
|
||||
long fingerId = buffer.getLong();
|
||||
Position position = readPosition(buffer);
|
||||
// 16 bits fixed-point
|
||||
int pressureInt = toUnsigned(buffer.getShort());
|
||||
// convert it to a float between 0 and 1 (0x1p16f is 2^16 as float)
|
||||
float pressure = pressureInt == 0xffff ? 1f : (pressureInt / 0x1p16f);
|
||||
return ControlMessage.createInjectTouchEvent(action, fingerId, position, pressure);
|
||||
}
|
||||
|
||||
private ControlMessage parseInjectScrollEvent() {
|
||||
if (buffer.remaining() < INJECT_SCROLL_EVENT_PAYLOAD_LENGTH) {
|
||||
return null;
|
||||
|
@ -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;
|
||||
@ -20,35 +22,55 @@ public class Controller {
|
||||
private final KeyCharacterMap charMap = KeyCharacterMap.load(KeyCharacterMap.VIRTUAL_KEYBOARD);
|
||||
|
||||
private long lastMouseDown;
|
||||
private final MotionEvent.PointerProperties[] pointerProperties = {new MotionEvent.PointerProperties()};
|
||||
private final MotionEvent.PointerCoords[] pointerCoords = {new MotionEvent.PointerCoords()};
|
||||
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;
|
||||
initPointer();
|
||||
initMousePointer();
|
||||
initTouchPointers();
|
||||
sender = new DeviceMessageSender(connection);
|
||||
}
|
||||
|
||||
private void initPointer() {
|
||||
MotionEvent.PointerProperties props = pointerProperties[0];
|
||||
private void initMousePointer() {
|
||||
MotionEvent.PointerProperties props = mousePointerProperties[0];
|
||||
props.id = 0;
|
||||
props.toolType = MotionEvent.TOOL_TYPE_FINGER;
|
||||
|
||||
MotionEvent.PointerCoords coords = pointerCoords[0];
|
||||
MotionEvent.PointerCoords coords = mousePointerCoords[0];
|
||||
coords.orientation = 0;
|
||||
coords.pressure = 1;
|
||||
coords.size = 1;
|
||||
}
|
||||
|
||||
private void setPointerCoords(Point point) {
|
||||
MotionEvent.PointerCoords coords = pointerCoords[0];
|
||||
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;
|
||||
|
||||
touchPointerProperties[i] = props;
|
||||
touchPointerCoords[i] = coords;
|
||||
}
|
||||
}
|
||||
|
||||
private void setMousePointerCoords(Point point) {
|
||||
MotionEvent.PointerCoords coords = mousePointerCoords[0];
|
||||
coords.x = point.getX();
|
||||
coords.y = point.getY();
|
||||
}
|
||||
|
||||
private void setScroll(int hScroll, int vScroll) {
|
||||
MotionEvent.PointerCoords coords = pointerCoords[0];
|
||||
MotionEvent.PointerCoords coords = mousePointerCoords[0];
|
||||
coords.setAxisValue(MotionEvent.AXIS_HSCROLL, hScroll);
|
||||
coords.setAxisValue(MotionEvent.AXIS_VSCROLL, vScroll);
|
||||
}
|
||||
@ -90,6 +112,9 @@ public class Controller {
|
||||
case ControlMessage.TYPE_INJECT_MOUSE_EVENT:
|
||||
injectMouse(msg.getAction(), msg.getButtons(), msg.getPosition());
|
||||
break;
|
||||
case ControlMessage.TYPE_INJECT_TOUCH_EVENT:
|
||||
injectTouch(msg.getAction(), msg.getFingerId(), msg.getPosition(), msg.getPressure());
|
||||
break;
|
||||
case ControlMessage.TYPE_INJECT_SCROLL_EVENT:
|
||||
injectScroll(msg.getPosition(), msg.getHScroll(), msg.getVScroll());
|
||||
break;
|
||||
@ -158,8 +183,52 @@ public class Controller {
|
||||
// ignore event
|
||||
return false;
|
||||
}
|
||||
setPointerCoords(point);
|
||||
MotionEvent event = MotionEvent.obtain(lastMouseDown, now, action, 1, pointerProperties, pointerCoords, 0, buttons, 1f, 1f, 0, 0,
|
||||
setMousePointerCoords(point);
|
||||
MotionEvent event = MotionEvent.obtain(lastMouseDown, now, action, 1, mousePointerProperties, mousePointerCoords, 0, buttons, 1f, 1f, 0, 0,
|
||||
InputDevice.SOURCE_TOUCHSCREEN, 0);
|
||||
return injectEvent(event);
|
||||
}
|
||||
|
||||
private boolean injectTouch(int action, long fingerId, Position position, float pressure) {
|
||||
long now = SystemClock.uptimeMillis();
|
||||
|
||||
Point point = device.getPhysicalPoint(position);
|
||||
if (point == null) {
|
||||
// ignore event
|
||||
return false;
|
||||
}
|
||||
|
||||
int fingerIndex = fingersState.getFingerIndex(fingerId);
|
||||
if (fingerIndex == -1) {
|
||||
Ln.w("Too many fingers for touch event");
|
||||
return false;
|
||||
}
|
||||
Finger finger = fingersState.get(fingerIndex);
|
||||
finger.setPoint(point);
|
||||
finger.setPressure(pressure);
|
||||
finger.setUp(action == MotionEvent.ACTION_UP);
|
||||
|
||||
// FAIL: action_up will always remove the finger, and the event will not be written!
|
||||
int pointerCount = fingersState.update(touchPointerProperties, touchPointerCoords);
|
||||
fingersState.cleanUp();
|
||||
|
||||
Ln.d("pointerCount = " + pointerCount);
|
||||
for (int i = 0; i < pointerCount; ++i) {
|
||||
Ln.d("props = " + touchPointerProperties[i].id);
|
||||
Ln.d("coords = " + touchPointerCoords[i].x + "," + touchPointerCoords[i].y);
|
||||
}
|
||||
|
||||
if (pointerCount > 1) {
|
||||
if (action == MotionEvent.ACTION_UP) {
|
||||
action = MotionEvent.ACTION_POINTER_UP | (fingerIndex << MotionEvent.ACTION_POINTER_INDEX_SHIFT);
|
||||
} else if (action == MotionEvent.ACTION_DOWN) {
|
||||
action = MotionEvent.ACTION_POINTER_DOWN | (fingerIndex << MotionEvent.ACTION_POINTER_INDEX_SHIFT);
|
||||
}
|
||||
} else if (action == MotionEvent.ACTION_DOWN) {
|
||||
lastTouchDown = now;
|
||||
}
|
||||
|
||||
MotionEvent event = MotionEvent.obtain(lastTouchDown, now, action, pointerCount, touchPointerProperties, touchPointerCoords, 0, 0, 1f, 1f, 0, 0,
|
||||
InputDevice.SOURCE_TOUCHSCREEN, 0);
|
||||
return injectEvent(event);
|
||||
}
|
||||
@ -171,9 +240,9 @@ public class Controller {
|
||||
// ignore event
|
||||
return false;
|
||||
}
|
||||
setPointerCoords(point);
|
||||
setMousePointerCoords(point);
|
||||
setScroll(hScroll, vScroll);
|
||||
MotionEvent event = MotionEvent.obtain(lastMouseDown, now, MotionEvent.ACTION_SCROLL, 1, pointerProperties, pointerCoords, 0, 0, 1f, 1f, 0,
|
||||
MotionEvent event = MotionEvent.obtain(lastMouseDown, now, MotionEvent.ACTION_SCROLL, 1, mousePointerProperties, mousePointerCoords, 0, 0, 1f, 1f, 0,
|
||||
0, InputDevice.SOURCE_MOUSE, 0);
|
||||
return injectEvent(event);
|
||||
}
|
||||
|
47
server/src/main/java/com/genymobile/scrcpy/Finger.java
Normal file
47
server/src/main/java/com/genymobile/scrcpy/Finger.java
Normal file
@ -0,0 +1,47 @@
|
||||
package com.genymobile.scrcpy;
|
||||
|
||||
public class Finger {
|
||||
|
||||
private final long id;
|
||||
private final int localId;
|
||||
private Point point;
|
||||
private float pressure;
|
||||
private boolean up;
|
||||
|
||||
public Finger(long id, int localId) {
|
||||
this.id = id;
|
||||
this.localId = localId;
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public int getLocalId() {
|
||||
return localId;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
public boolean isUp() {
|
||||
return up;
|
||||
}
|
||||
|
||||
public void setUp(boolean up) {
|
||||
this.up = up;
|
||||
}
|
||||
}
|
101
server/src/main/java/com/genymobile/scrcpy/FingersState.java
Normal file
101
server/src/main/java/com/genymobile/scrcpy/FingersState.java
Normal file
@ -0,0 +1,101 @@
|
||||
package com.genymobile.scrcpy;
|
||||
|
||||
import android.view.MotionEvent;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class FingersState {
|
||||
|
||||
private final List<Finger> fingers = new ArrayList<>();
|
||||
private int maxFingers;
|
||||
|
||||
public FingersState(int maxFingers) {
|
||||
this.maxFingers = maxFingers;
|
||||
}
|
||||
|
||||
private int indexOf(long id) {
|
||||
for (int i = 0; i < fingers.size(); ++i) {
|
||||
Finger finger = fingers.get(i);
|
||||
if (finger.getId() == id) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
private boolean isLocalIdAvailable(int localId) {
|
||||
for (int i = 0; i < fingers.size(); ++i) {
|
||||
Finger finger = fingers.get(i);
|
||||
if (finger.getLocalId() == localId) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private int nextUnusedLocalId() {
|
||||
for (int localId = 0; localId < maxFingers; ++localId) {
|
||||
if (isLocalIdAvailable(localId)) {
|
||||
return localId;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
public Finger get(int index) {
|
||||
return fingers.get(index);
|
||||
}
|
||||
|
||||
public int getFingerIndex(long id) {
|
||||
int index = indexOf(id);
|
||||
if (index != -1) {
|
||||
// already exists, return it
|
||||
return index;
|
||||
}
|
||||
if (fingers.size() >= maxFingers) {
|
||||
// it's full
|
||||
return -1;
|
||||
}
|
||||
// id 0 is reserved for mouse events
|
||||
int localId = nextUnusedLocalId();
|
||||
if (localId == -1) {
|
||||
throw new AssertionError("fingers.size() < maxFingers implies that a local id is available");
|
||||
}
|
||||
Finger finger = new Finger(id, localId);
|
||||
fingers.add(finger);
|
||||
// return the index of the finger
|
||||
return fingers.size() - 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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) {
|
||||
for (int i = 0; i < fingers.size(); ++i) {
|
||||
Finger finger = fingers.get(i);
|
||||
|
||||
// id 0 is reserved for mouse events
|
||||
props[i].id = finger.getLocalId();
|
||||
|
||||
Point point = finger.getPoint();
|
||||
coords[i].x = point.getX();
|
||||
coords[i].y = point.getY();
|
||||
coords[i].pressure = finger.getPressure();
|
||||
}
|
||||
return fingers.size();
|
||||
}
|
||||
|
||||
public void cleanUp() {
|
||||
for (int i = fingers.size() - 1; i >= 0; --i) {
|
||||
Finger finger = fingers.get(i);
|
||||
if (finger.isUp()) {
|
||||
fingers.remove(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -105,6 +105,37 @@ public class ControlMessageReaderTest {
|
||||
Assert.assertEquals(1920, event.getPosition().getScreenSize().getHeight());
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("checkstyle:MagicNumber")
|
||||
public void testParseTouchEvent() throws IOException {
|
||||
ControlMessageReader reader = new ControlMessageReader();
|
||||
|
||||
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||||
DataOutputStream dos = new DataOutputStream(bos);
|
||||
dos.writeByte(ControlMessage.TYPE_INJECT_TOUCH_EVENT);
|
||||
dos.writeByte(MotionEvent.ACTION_DOWN);
|
||||
dos.writeLong(-42); // fingerId
|
||||
dos.writeInt(100);
|
||||
dos.writeInt(200);
|
||||
dos.writeShort(1080);
|
||||
dos.writeShort(1920);
|
||||
dos.writeShort(0xffff); // pressure
|
||||
|
||||
byte[] packet = bos.toByteArray();
|
||||
|
||||
reader.readFrom(new ByteArrayInputStream(packet));
|
||||
ControlMessage event = reader.next();
|
||||
|
||||
Assert.assertEquals(ControlMessage.TYPE_INJECT_TOUCH_EVENT, event.getType());
|
||||
Assert.assertEquals(MotionEvent.ACTION_DOWN, event.getAction());
|
||||
Assert.assertEquals(-42, event.getFingerId());
|
||||
Assert.assertEquals(100, event.getPosition().getPoint().getX());
|
||||
Assert.assertEquals(200, event.getPosition().getPoint().getY());
|
||||
Assert.assertEquals(1080, event.getPosition().getScreenSize().getWidth());
|
||||
Assert.assertEquals(1920, event.getPosition().getScreenSize().getHeight());
|
||||
Assert.assertEquals(1f, event.getPressure(), 0f); // must be exact
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("checkstyle:MagicNumber")
|
||||
public void testParseScrollEvent() throws IOException {
|
||||
|
Loading…
x
Reference in New Issue
Block a user