parent
e010199752
commit
b92199dea3
@ -1,136 +0,0 @@
|
||||
#include <QDebug>
|
||||
|
||||
#include "controlevent.h"
|
||||
#include "bufferutil.h"
|
||||
|
||||
ControlEvent::ControlEvent(ControlEventType controlEventType)
|
||||
: QScrcpyEvent(Control)
|
||||
{
|
||||
m_data.type = controlEventType;
|
||||
}
|
||||
|
||||
ControlEvent::~ControlEvent()
|
||||
{
|
||||
if (CET_SET_CLIPBOARD == m_data.type
|
||||
&& Q_NULLPTR != m_data.setClipboardEvent.text) {
|
||||
delete m_data.setClipboardEvent.text;
|
||||
m_data.setClipboardEvent.text = Q_NULLPTR;
|
||||
} else if (CET_TEXT == m_data.type
|
||||
&& Q_NULLPTR != m_data.textEvent.text){
|
||||
delete m_data.textEvent.text;
|
||||
m_data.textEvent.text = Q_NULLPTR;
|
||||
}
|
||||
}
|
||||
|
||||
void ControlEvent::setKeycodeEventData(AndroidKeyeventAction action, AndroidKeycode keycode, AndroidMetastate metastate)
|
||||
{
|
||||
m_data.keycodeEvent.action = action;
|
||||
m_data.keycodeEvent.keycode = keycode;
|
||||
m_data.keycodeEvent.metastate = metastate;
|
||||
}
|
||||
|
||||
void ControlEvent::setTextEventData(QString& text)
|
||||
{
|
||||
// write length (2 byte) + string (non nul-terminated)
|
||||
if (CONTROL_EVENT_TEXT_MAX_LENGTH < text.length()) {
|
||||
// injecting a text takes time, so limit the text length
|
||||
text = text.left(CONTROL_EVENT_TEXT_MAX_LENGTH);
|
||||
}
|
||||
QByteArray tmp = text.toUtf8();
|
||||
m_data.textEvent.text = new char[tmp.length() + 1];
|
||||
memcpy(m_data.textEvent.text, tmp.data(), tmp.length());
|
||||
m_data.textEvent.text[tmp.length()] = '\0';
|
||||
}
|
||||
|
||||
void ControlEvent::setMouseEventData(AndroidMotioneventAction action, AndroidMotioneventButtons buttons, QRect position)
|
||||
{
|
||||
m_data.mouseEvent.action = action;
|
||||
m_data.mouseEvent.buttons = buttons;
|
||||
m_data.mouseEvent.position = position;
|
||||
}
|
||||
|
||||
void ControlEvent::setTouchEventData(quint32 id, AndroidMotioneventAction action, QRect position)
|
||||
{
|
||||
m_data.touchEvent.action = action;
|
||||
m_data.touchEvent.id = id;
|
||||
m_data.touchEvent.position = position;
|
||||
}
|
||||
|
||||
void ControlEvent::setScrollEventData(QRect position, qint32 hScroll, qint32 vScroll)
|
||||
{
|
||||
m_data.scrollEvent.position = position;
|
||||
m_data.scrollEvent.hScroll = hScroll;
|
||||
m_data.scrollEvent.vScroll = vScroll;
|
||||
}
|
||||
|
||||
void ControlEvent::setSetClipboardEventData(QString &text)
|
||||
{
|
||||
if (text.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
if (CONTROL_EVENT_CLIPBOARD_TEXT_MAX_LENGTH < text.length()) {
|
||||
text = text.left(CONTROL_EVENT_CLIPBOARD_TEXT_MAX_LENGTH);
|
||||
}
|
||||
|
||||
QByteArray tmp = text.toUtf8();
|
||||
m_data.setClipboardEvent.text = new char[tmp.length() + 1];
|
||||
memcpy(m_data.setClipboardEvent.text, tmp.data(), tmp.length());
|
||||
m_data.setClipboardEvent.text[tmp.length()] = '\0';
|
||||
}
|
||||
|
||||
void ControlEvent::writePosition(QBuffer &buffer, const QRect& value)
|
||||
{
|
||||
BufferUtil::write16(buffer, value.left());
|
||||
BufferUtil::write16(buffer, value.top());
|
||||
BufferUtil::write16(buffer, value.width());
|
||||
BufferUtil::write16(buffer, value.height());
|
||||
}
|
||||
|
||||
QByteArray ControlEvent::serializeData()
|
||||
{
|
||||
QByteArray byteArray;
|
||||
QBuffer buffer(&byteArray);
|
||||
buffer.open(QBuffer::WriteOnly);
|
||||
buffer.putChar(m_data.type);
|
||||
|
||||
switch (m_data.type) {
|
||||
case CET_KEYCODE:
|
||||
buffer.putChar(m_data.keycodeEvent.action);
|
||||
BufferUtil::write32(buffer, m_data.keycodeEvent.keycode);
|
||||
BufferUtil::write32(buffer, m_data.keycodeEvent.metastate);
|
||||
break;
|
||||
case CET_TEXT:
|
||||
BufferUtil::write16(buffer, strlen(m_data.textEvent.text));
|
||||
buffer.write(m_data.textEvent.text, strlen(m_data.textEvent.text));
|
||||
break;
|
||||
case CET_MOUSE:
|
||||
buffer.putChar(m_data.mouseEvent.action);
|
||||
BufferUtil::write32(buffer, m_data.mouseEvent.buttons);
|
||||
writePosition(buffer, m_data.mouseEvent.position);
|
||||
break;
|
||||
case CET_TOUCH:
|
||||
buffer.putChar(m_data.touchEvent.id);
|
||||
buffer.putChar(m_data.touchEvent.action);
|
||||
writePosition(buffer, m_data.touchEvent.position);
|
||||
break;
|
||||
case CET_SCROLL:
|
||||
writePosition(buffer, m_data.scrollEvent.position);
|
||||
BufferUtil::write32(buffer, m_data.scrollEvent.hScroll);
|
||||
BufferUtil::write32(buffer, m_data.scrollEvent.vScroll);
|
||||
break;
|
||||
case CET_SET_CLIPBOARD:
|
||||
BufferUtil::write16(buffer, strlen(m_data.setClipboardEvent.text));
|
||||
buffer.write(m_data.setClipboardEvent.text, strlen(m_data.setClipboardEvent.text));
|
||||
break;
|
||||
case CET_BACK_OR_SCREEN_ON:
|
||||
case CET_EXPAND_NOTIFICATION_PANEL:
|
||||
case CET_COLLAPSE_NOTIFICATION_PANEL:
|
||||
case CET_GET_CLIPBOARD:
|
||||
break;
|
||||
default:
|
||||
qDebug() << "Unknown event type:" << m_data.type;
|
||||
break;
|
||||
}
|
||||
buffer.close();
|
||||
return byteArray;
|
||||
}
|
||||
@ -0,0 +1,136 @@
|
||||
#include <QDebug>
|
||||
|
||||
#include "controlmsg.h"
|
||||
#include "bufferutil.h"
|
||||
|
||||
ControlMsg::ControlMsg(ControlMsgType controlMsgType)
|
||||
: QScrcpyEvent(Control)
|
||||
{
|
||||
m_data.type = controlMsgType;
|
||||
}
|
||||
|
||||
ControlMsg::~ControlMsg()
|
||||
{
|
||||
if (CMT_SET_CLIPBOARD == m_data.type
|
||||
&& Q_NULLPTR != m_data.setClipboardMsg.text) {
|
||||
delete m_data.setClipboardMsg.text;
|
||||
m_data.setClipboardMsg.text = Q_NULLPTR;
|
||||
} else if (CMT_INJECT_TEXT == m_data.type
|
||||
&& Q_NULLPTR != m_data.injectTextMsg.text){
|
||||
delete m_data.injectTextMsg.text;
|
||||
m_data.injectTextMsg.text = Q_NULLPTR;
|
||||
}
|
||||
}
|
||||
|
||||
void ControlMsg::setInjectKeycodeMsgData(AndroidKeyeventAction action, AndroidKeycode keycode, AndroidMetastate metastate)
|
||||
{
|
||||
m_data.injectKeycodeMsg.action = action;
|
||||
m_data.injectKeycodeMsg.keycode = keycode;
|
||||
m_data.injectKeycodeMsg.metastate = metastate;
|
||||
}
|
||||
|
||||
void ControlMsg::setInjectTextMsgData(QString& text)
|
||||
{
|
||||
// write length (2 byte) + string (non nul-terminated)
|
||||
if (CONTROL_MSG_TEXT_MAX_LENGTH < text.length()) {
|
||||
// injecting a text takes time, so limit the text length
|
||||
text = text.left(CONTROL_MSG_TEXT_MAX_LENGTH);
|
||||
}
|
||||
QByteArray tmp = text.toUtf8();
|
||||
m_data.injectTextMsg.text = new char[tmp.length() + 1];
|
||||
memcpy(m_data.injectTextMsg.text, tmp.data(), tmp.length());
|
||||
m_data.injectTextMsg.text[tmp.length()] = '\0';
|
||||
}
|
||||
|
||||
void ControlMsg::setInjectMouseMsgData(AndroidMotioneventAction action, AndroidMotioneventButtons buttons, QRect position)
|
||||
{
|
||||
m_data.injectMouseMsg.action = action;
|
||||
m_data.injectMouseMsg.buttons = buttons;
|
||||
m_data.injectMouseMsg.position = position;
|
||||
}
|
||||
|
||||
void ControlMsg::setInjectTouchMsgData(quint32 id, AndroidMotioneventAction action, QRect position)
|
||||
{
|
||||
m_data.injectTouchMsg.action = action;
|
||||
m_data.injectTouchMsg.id = id;
|
||||
m_data.injectTouchMsg.position = position;
|
||||
}
|
||||
|
||||
void ControlMsg::setInjectScrollMsgData(QRect position, qint32 hScroll, qint32 vScroll)
|
||||
{
|
||||
m_data.injectScrollMsg.position = position;
|
||||
m_data.injectScrollMsg.hScroll = hScroll;
|
||||
m_data.injectScrollMsg.vScroll = vScroll;
|
||||
}
|
||||
|
||||
void ControlMsg::setSetClipboardMsgData(QString &text)
|
||||
{
|
||||
if (text.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
if (CONTROL_MSG_CLIPBOARD_TEXT_MAX_LENGTH < text.length()) {
|
||||
text = text.left(CONTROL_MSG_CLIPBOARD_TEXT_MAX_LENGTH);
|
||||
}
|
||||
|
||||
QByteArray tmp = text.toUtf8();
|
||||
m_data.setClipboardMsg.text = new char[tmp.length() + 1];
|
||||
memcpy(m_data.setClipboardMsg.text, tmp.data(), tmp.length());
|
||||
m_data.setClipboardMsg.text[tmp.length()] = '\0';
|
||||
}
|
||||
|
||||
void ControlMsg::writePosition(QBuffer &buffer, const QRect& value)
|
||||
{
|
||||
BufferUtil::write16(buffer, value.left());
|
||||
BufferUtil::write16(buffer, value.top());
|
||||
BufferUtil::write16(buffer, value.width());
|
||||
BufferUtil::write16(buffer, value.height());
|
||||
}
|
||||
|
||||
QByteArray ControlMsg::serializeData()
|
||||
{
|
||||
QByteArray byteArray;
|
||||
QBuffer buffer(&byteArray);
|
||||
buffer.open(QBuffer::WriteOnly);
|
||||
buffer.putChar(m_data.type);
|
||||
|
||||
switch (m_data.type) {
|
||||
case CMT_INJECT_KEYCODE:
|
||||
buffer.putChar(m_data.injectKeycodeMsg.action);
|
||||
BufferUtil::write32(buffer, m_data.injectKeycodeMsg.keycode);
|
||||
BufferUtil::write32(buffer, m_data.injectKeycodeMsg.metastate);
|
||||
break;
|
||||
case CMT_INJECT_TEXT:
|
||||
BufferUtil::write16(buffer, strlen(m_data.injectTextMsg.text));
|
||||
buffer.write(m_data.injectTextMsg.text, strlen(m_data.injectTextMsg.text));
|
||||
break;
|
||||
case CMT_INJECT_MOUSE:
|
||||
buffer.putChar(m_data.injectMouseMsg.action);
|
||||
BufferUtil::write32(buffer, m_data.injectMouseMsg.buttons);
|
||||
writePosition(buffer, m_data.injectMouseMsg.position);
|
||||
break;
|
||||
case CMT_INJECT_TOUCH:
|
||||
buffer.putChar(m_data.injectTouchMsg.id);
|
||||
buffer.putChar(m_data.injectTouchMsg.action);
|
||||
writePosition(buffer, m_data.injectTouchMsg.position);
|
||||
break;
|
||||
case CMT_INJECT_SCROLL:
|
||||
writePosition(buffer, m_data.injectScrollMsg.position);
|
||||
BufferUtil::write32(buffer, m_data.injectScrollMsg.hScroll);
|
||||
BufferUtil::write32(buffer, m_data.injectScrollMsg.vScroll);
|
||||
break;
|
||||
case CMT_SET_CLIPBOARD:
|
||||
BufferUtil::write16(buffer, strlen(m_data.setClipboardMsg.text));
|
||||
buffer.write(m_data.setClipboardMsg.text, strlen(m_data.setClipboardMsg.text));
|
||||
break;
|
||||
case CMT_BACK_OR_SCREEN_ON:
|
||||
case CMT_EXPAND_NOTIFICATION_PANEL:
|
||||
case CMT_COLLAPSE_NOTIFICATION_PANEL:
|
||||
case CMT_GET_CLIPBOARD:
|
||||
break;
|
||||
default:
|
||||
qDebug() << "Unknown event type:" << m_data.type;
|
||||
break;
|
||||
}
|
||||
buffer.close();
|
||||
return byteArray;
|
||||
}
|
||||
@ -1,69 +0,0 @@
|
||||
#include <QDebug>
|
||||
|
||||
#include "deviceevent.h"
|
||||
#include "bufferutil.h"
|
||||
|
||||
DeviceEvent::DeviceEvent(QObject *parent) : QObject(parent)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
DeviceEvent::~DeviceEvent()
|
||||
{
|
||||
if (DET_GET_CLIPBOARD == m_data.type
|
||||
&& Q_NULLPTR != m_data.clipboardEvent.text) {
|
||||
delete m_data.clipboardEvent.text;
|
||||
m_data.clipboardEvent.text = Q_NULLPTR;
|
||||
}
|
||||
}
|
||||
|
||||
DeviceEvent::DeviceEventType DeviceEvent::type()
|
||||
{
|
||||
return m_data.type;
|
||||
}
|
||||
|
||||
void DeviceEvent::getClipboardEventData(QString& text)
|
||||
{
|
||||
text = QString::fromUtf8(m_data.clipboardEvent.text);
|
||||
}
|
||||
|
||||
qint32 DeviceEvent::deserialize(QByteArray& byteArray)
|
||||
{
|
||||
QBuffer buf(&byteArray);
|
||||
buf.open(QBuffer::ReadOnly);
|
||||
|
||||
qint64 len = buf.size();
|
||||
char c = 0;
|
||||
qint32 ret = 0;
|
||||
|
||||
if (len < 3) {
|
||||
// at least type + empty string length
|
||||
return 0; // not available
|
||||
}
|
||||
|
||||
buf.getChar(&c);
|
||||
m_data.type = (DeviceEventType)c;
|
||||
switch (m_data.type) {
|
||||
case DET_GET_CLIPBOARD: {
|
||||
quint16 clipboardLen = BufferUtil::read16(buf);
|
||||
if (clipboardLen > len - 3) {
|
||||
ret = 0; // not available
|
||||
break;
|
||||
}
|
||||
|
||||
QByteArray text = buf.readAll();
|
||||
m_data.clipboardEvent.text = new char[text.length() + 1];
|
||||
memcpy(m_data.clipboardEvent.text, text.data(), text.length());
|
||||
m_data.clipboardEvent.text[text.length()] = '\0';
|
||||
|
||||
ret = 3 + clipboardLen;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
qWarning("Unsupported device event type: %d", (int) m_data.type);
|
||||
ret = -1; // error, we cannot recover
|
||||
}
|
||||
|
||||
buf.close();
|
||||
return ret;
|
||||
}
|
||||
@ -1,42 +0,0 @@
|
||||
#ifndef DEVICEEVENT_H
|
||||
#define DEVICEEVENT_H
|
||||
|
||||
#include <QBuffer>
|
||||
|
||||
#define DEVICE_EVENT_QUEUE_SIZE 64
|
||||
#define DEVICE_EVENT_TEXT_MAX_LENGTH 4093
|
||||
#define DEVICE_EVENT_SERIALIZED_MAX_SIZE (3 + DEVICE_EVENT_TEXT_MAX_LENGTH)
|
||||
|
||||
class DeviceEvent : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
enum DeviceEventType {
|
||||
DET_NULL = -1,
|
||||
// 和服务端对应
|
||||
DET_GET_CLIPBOARD = 0,
|
||||
};
|
||||
explicit DeviceEvent(QObject *parent = nullptr);
|
||||
virtual ~DeviceEvent();
|
||||
|
||||
DeviceEvent::DeviceEventType type();
|
||||
void getClipboardEventData(QString& text);
|
||||
|
||||
qint32 deserialize(QByteArray& byteArray);
|
||||
|
||||
private:
|
||||
struct DeviceEventData {
|
||||
DeviceEventType type = DET_NULL;
|
||||
union {
|
||||
struct {
|
||||
char* text = Q_NULLPTR;
|
||||
} clipboardEvent;
|
||||
};
|
||||
DeviceEventData(){}
|
||||
~DeviceEventData(){}
|
||||
};
|
||||
|
||||
DeviceEventData m_data;
|
||||
};
|
||||
|
||||
#endif // DEVICEEVENT_H
|
||||
@ -0,0 +1,69 @@
|
||||
#include <QDebug>
|
||||
|
||||
#include "devicemsg.h"
|
||||
#include "bufferutil.h"
|
||||
|
||||
DeviceMsg::DeviceMsg(QObject *parent) : QObject(parent)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
DeviceMsg::~DeviceMsg()
|
||||
{
|
||||
if (DMT_GET_CLIPBOARD == m_data.type
|
||||
&& Q_NULLPTR != m_data.clipboardMsg.text) {
|
||||
delete m_data.clipboardMsg.text;
|
||||
m_data.clipboardMsg.text = Q_NULLPTR;
|
||||
}
|
||||
}
|
||||
|
||||
DeviceMsg::DeviceMsgType DeviceMsg::type()
|
||||
{
|
||||
return m_data.type;
|
||||
}
|
||||
|
||||
void DeviceMsg::getClipboardMsgData(QString& text)
|
||||
{
|
||||
text = QString::fromUtf8(m_data.clipboardMsg.text);
|
||||
}
|
||||
|
||||
qint32 DeviceMsg::deserialize(QByteArray& byteArray)
|
||||
{
|
||||
QBuffer buf(&byteArray);
|
||||
buf.open(QBuffer::ReadOnly);
|
||||
|
||||
qint64 len = buf.size();
|
||||
char c = 0;
|
||||
qint32 ret = 0;
|
||||
|
||||
if (len < 3) {
|
||||
// at least type + empty string length
|
||||
return 0; // not available
|
||||
}
|
||||
|
||||
buf.getChar(&c);
|
||||
m_data.type = (DeviceMsgType)c;
|
||||
switch (m_data.type) {
|
||||
case DMT_GET_CLIPBOARD: {
|
||||
quint16 clipboardLen = BufferUtil::read16(buf);
|
||||
if (clipboardLen > len - 3) {
|
||||
ret = 0; // not available
|
||||
break;
|
||||
}
|
||||
|
||||
QByteArray text = buf.readAll();
|
||||
m_data.clipboardMsg.text = new char[text.length() + 1];
|
||||
memcpy(m_data.clipboardMsg.text, text.data(), text.length());
|
||||
m_data.clipboardMsg.text[text.length()] = '\0';
|
||||
|
||||
ret = 3 + clipboardLen;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
qWarning("Unsupported device msg type: %d", (int) m_data.type);
|
||||
ret = -1; // error, we cannot recover
|
||||
}
|
||||
|
||||
buf.close();
|
||||
return ret;
|
||||
}
|
||||
@ -0,0 +1,42 @@
|
||||
#ifndef DEVICEMSG_H
|
||||
#define DEVICEMSG_H
|
||||
|
||||
#include <QBuffer>
|
||||
|
||||
#define DEVICE_MSG_QUEUE_SIZE 64
|
||||
#define DEVICE_MSG_TEXT_MAX_LENGTH 4093
|
||||
#define DEVICE_MSG_SERIALIZED_MAX_SIZE (3 + DEVICE_MSG_TEXT_MAX_LENGTH)
|
||||
|
||||
class DeviceMsg : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
enum DeviceMsgType {
|
||||
DMT_NULL = -1,
|
||||
// 和服务端对应
|
||||
DMT_GET_CLIPBOARD = 0,
|
||||
};
|
||||
explicit DeviceMsg(QObject *parent = nullptr);
|
||||
virtual ~DeviceMsg();
|
||||
|
||||
DeviceMsg::DeviceMsgType type();
|
||||
void getClipboardMsgData(QString& text);
|
||||
|
||||
qint32 deserialize(QByteArray& byteArray);
|
||||
|
||||
private:
|
||||
struct DeviceMsgData {
|
||||
DeviceMsgType type = DMT_NULL;
|
||||
union {
|
||||
struct {
|
||||
char* text = Q_NULLPTR;
|
||||
} clipboardMsg;
|
||||
};
|
||||
DeviceMsgData(){}
|
||||
~DeviceMsgData(){}
|
||||
};
|
||||
|
||||
DeviceMsgData m_data;
|
||||
};
|
||||
|
||||
#endif // DEVICEMSG_H
|
||||
@ -1,18 +1,18 @@
|
||||
HEADERS += \
|
||||
$$PWD/controlevent.h \
|
||||
$$PWD/controller.h \
|
||||
$$PWD/inputconvertbase.h \
|
||||
$$PWD/inputconvertgame.h \
|
||||
$$PWD/inputconvertnormal.h \
|
||||
$$PWD/deviceevent.h \
|
||||
$$PWD/receiver.h
|
||||
$$PWD/receiver.h \
|
||||
$$PWD/controlmsg.h \
|
||||
$$PWD/devicemsg.h
|
||||
|
||||
SOURCES += \
|
||||
$$PWD/controlevent.cpp \
|
||||
$$PWD/controller.cpp \
|
||||
$$PWD/inputconvertbase.cpp \
|
||||
$$PWD/inputconvertgame.cpp \
|
||||
$$PWD/inputconvertnormal.cpp \
|
||||
$$PWD/deviceevent.cpp \
|
||||
$$PWD/receiver.cpp
|
||||
$$PWD/receiver.cpp \
|
||||
$$PWD/controlmsg.cpp \
|
||||
$$PWD/devicemsg.cpp
|
||||
|
||||
|
||||
@ -1,27 +0,0 @@
|
||||
package com.genymobile.scrcpy;
|
||||
|
||||
public final class DeviceEvent {
|
||||
|
||||
public static final int TYPE_GET_CLIPBOARD = 0;
|
||||
|
||||
private int type;
|
||||
private String text;
|
||||
|
||||
private DeviceEvent() {
|
||||
}
|
||||
|
||||
public static DeviceEvent createGetClipboardEvent(String text) {
|
||||
DeviceEvent event = new DeviceEvent();
|
||||
event.type = TYPE_GET_CLIPBOARD;
|
||||
event.text = text;
|
||||
return event;
|
||||
}
|
||||
|
||||
public int getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public String getText() {
|
||||
return text;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,27 @@
|
||||
package com.genymobile.scrcpy;
|
||||
|
||||
public final class DeviceMessage {
|
||||
|
||||
public static final int TYPE_CLIPBOARD = 0;
|
||||
|
||||
private int type;
|
||||
private String text;
|
||||
|
||||
private DeviceMessage() {
|
||||
}
|
||||
|
||||
public static DeviceMessage createClipboard(String text) {
|
||||
DeviceMessage event = new DeviceMessage();
|
||||
event.type = TYPE_CLIPBOARD;
|
||||
event.text = text;
|
||||
return event;
|
||||
}
|
||||
|
||||
public int getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public String getText() {
|
||||
return text;
|
||||
}
|
||||
}
|
||||
@ -1,173 +0,0 @@
|
||||
package com.genymobile.scrcpy;
|
||||
|
||||
import android.view.KeyEvent;
|
||||
import android.view.MotionEvent;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Arrays;
|
||||
|
||||
|
||||
public class ControlEventReaderTest {
|
||||
|
||||
@Test
|
||||
public void testParseKeycodeEvent() throws IOException {
|
||||
ControlEventReader reader = new ControlEventReader();
|
||||
|
||||
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||||
DataOutputStream dos = new DataOutputStream(bos);
|
||||
dos.writeByte(ControlEvent.TYPE_KEYCODE);
|
||||
dos.writeByte(KeyEvent.ACTION_UP);
|
||||
dos.writeInt(KeyEvent.KEYCODE_ENTER);
|
||||
dos.writeInt(KeyEvent.META_CTRL_ON);
|
||||
byte[] packet = bos.toByteArray();
|
||||
|
||||
reader.readFrom(new ByteArrayInputStream(packet));
|
||||
ControlEvent event = reader.next();
|
||||
|
||||
Assert.assertEquals(ControlEvent.TYPE_KEYCODE, event.getType());
|
||||
Assert.assertEquals(KeyEvent.ACTION_UP, event.getAction());
|
||||
Assert.assertEquals(KeyEvent.KEYCODE_ENTER, event.getKeycode());
|
||||
Assert.assertEquals(KeyEvent.META_CTRL_ON, event.getMetaState());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParseTextEvent() throws IOException {
|
||||
ControlEventReader reader = new ControlEventReader();
|
||||
|
||||
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||||
DataOutputStream dos = new DataOutputStream(bos);
|
||||
dos.writeByte(ControlEvent.TYPE_TEXT);
|
||||
byte[] text = "testé".getBytes(StandardCharsets.UTF_8);
|
||||
dos.writeShort(text.length);
|
||||
dos.write(text);
|
||||
byte[] packet = bos.toByteArray();
|
||||
|
||||
reader.readFrom(new ByteArrayInputStream(packet));
|
||||
ControlEvent event = reader.next();
|
||||
|
||||
Assert.assertEquals(ControlEvent.TYPE_TEXT, event.getType());
|
||||
Assert.assertEquals("testé", event.getText());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParseLongTextEvent() throws IOException {
|
||||
ControlEventReader reader = new ControlEventReader();
|
||||
|
||||
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||||
DataOutputStream dos = new DataOutputStream(bos);
|
||||
dos.writeByte(ControlEvent.TYPE_TEXT);
|
||||
byte[] text = new byte[ControlEventReader.TEXT_MAX_LENGTH];
|
||||
Arrays.fill(text, (byte) 'a');
|
||||
dos.writeShort(text.length);
|
||||
dos.write(text);
|
||||
byte[] packet = bos.toByteArray();
|
||||
|
||||
reader.readFrom(new ByteArrayInputStream(packet));
|
||||
ControlEvent event = reader.next();
|
||||
|
||||
Assert.assertEquals(ControlEvent.TYPE_TEXT, event.getType());
|
||||
Assert.assertEquals(new String(text, StandardCharsets.US_ASCII), event.getText());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParseMouseEvent() throws IOException {
|
||||
ControlEventReader reader = new ControlEventReader();
|
||||
|
||||
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||||
DataOutputStream dos = new DataOutputStream(bos);
|
||||
dos.writeByte(ControlEvent.TYPE_KEYCODE);
|
||||
dos.writeByte(MotionEvent.ACTION_DOWN);
|
||||
dos.writeInt(MotionEvent.BUTTON_PRIMARY);
|
||||
dos.writeInt(KeyEvent.META_CTRL_ON);
|
||||
byte[] packet = bos.toByteArray();
|
||||
|
||||
reader.readFrom(new ByteArrayInputStream(packet));
|
||||
ControlEvent event = reader.next();
|
||||
|
||||
Assert.assertEquals(ControlEvent.TYPE_KEYCODE, event.getType());
|
||||
Assert.assertEquals(MotionEvent.ACTION_DOWN, event.getAction());
|
||||
Assert.assertEquals(MotionEvent.BUTTON_PRIMARY, event.getKeycode());
|
||||
Assert.assertEquals(KeyEvent.META_CTRL_ON, event.getMetaState());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMultiEvents() throws IOException {
|
||||
ControlEventReader reader = new ControlEventReader();
|
||||
|
||||
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||||
DataOutputStream dos = new DataOutputStream(bos);
|
||||
|
||||
dos.writeByte(ControlEvent.TYPE_KEYCODE);
|
||||
dos.writeByte(KeyEvent.ACTION_UP);
|
||||
dos.writeInt(KeyEvent.KEYCODE_ENTER);
|
||||
dos.writeInt(KeyEvent.META_CTRL_ON);
|
||||
|
||||
dos.writeByte(ControlEvent.TYPE_KEYCODE);
|
||||
dos.writeByte(MotionEvent.ACTION_DOWN);
|
||||
dos.writeInt(MotionEvent.BUTTON_PRIMARY);
|
||||
dos.writeInt(KeyEvent.META_CTRL_ON);
|
||||
|
||||
byte[] packet = bos.toByteArray();
|
||||
reader.readFrom(new ByteArrayInputStream(packet));
|
||||
|
||||
ControlEvent event = reader.next();
|
||||
Assert.assertEquals(ControlEvent.TYPE_KEYCODE, event.getType());
|
||||
Assert.assertEquals(KeyEvent.ACTION_UP, event.getAction());
|
||||
Assert.assertEquals(KeyEvent.KEYCODE_ENTER, event.getKeycode());
|
||||
Assert.assertEquals(KeyEvent.META_CTRL_ON, event.getMetaState());
|
||||
|
||||
event = reader.next();
|
||||
Assert.assertEquals(ControlEvent.TYPE_KEYCODE, event.getType());
|
||||
Assert.assertEquals(MotionEvent.ACTION_DOWN, event.getAction());
|
||||
Assert.assertEquals(MotionEvent.BUTTON_PRIMARY, event.getKeycode());
|
||||
Assert.assertEquals(KeyEvent.META_CTRL_ON, event.getMetaState());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPartialEvents() throws IOException {
|
||||
ControlEventReader reader = new ControlEventReader();
|
||||
|
||||
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||||
DataOutputStream dos = new DataOutputStream(bos);
|
||||
|
||||
dos.writeByte(ControlEvent.TYPE_KEYCODE);
|
||||
dos.writeByte(KeyEvent.ACTION_UP);
|
||||
dos.writeInt(KeyEvent.KEYCODE_ENTER);
|
||||
dos.writeInt(KeyEvent.META_CTRL_ON);
|
||||
|
||||
dos.writeByte(ControlEvent.TYPE_KEYCODE);
|
||||
dos.writeByte(MotionEvent.ACTION_DOWN);
|
||||
|
||||
byte[] packet = bos.toByteArray();
|
||||
reader.readFrom(new ByteArrayInputStream(packet));
|
||||
|
||||
ControlEvent event = reader.next();
|
||||
Assert.assertEquals(ControlEvent.TYPE_KEYCODE, event.getType());
|
||||
Assert.assertEquals(KeyEvent.ACTION_UP, event.getAction());
|
||||
Assert.assertEquals(KeyEvent.KEYCODE_ENTER, event.getKeycode());
|
||||
Assert.assertEquals(KeyEvent.META_CTRL_ON, event.getMetaState());
|
||||
|
||||
event = reader.next();
|
||||
Assert.assertNull(event); // the event is not complete
|
||||
|
||||
bos.reset();
|
||||
dos.writeInt(MotionEvent.BUTTON_PRIMARY);
|
||||
dos.writeInt(KeyEvent.META_CTRL_ON);
|
||||
packet = bos.toByteArray();
|
||||
reader.readFrom(new ByteArrayInputStream(packet));
|
||||
|
||||
// the event is now complete
|
||||
event = reader.next();
|
||||
Assert.assertEquals(ControlEvent.TYPE_KEYCODE, event.getType());
|
||||
Assert.assertEquals(MotionEvent.ACTION_DOWN, event.getAction());
|
||||
Assert.assertEquals(MotionEvent.BUTTON_PRIMARY, event.getKeycode());
|
||||
Assert.assertEquals(KeyEvent.META_CTRL_ON, event.getMetaState());
|
||||
}
|
||||
}
|
||||
Loading…
Reference in new issue