Skip to content

Commit

Permalink
Merge pull request #263 from barry-ran/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
barry-ran authored Aug 2, 2020
2 parents b2d947f + 9290348 commit e499cf9
Show file tree
Hide file tree
Showing 13 changed files with 231 additions and 14 deletions.
9 changes: 9 additions & 0 deletions QtScrcpy/device/controller/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ void Controller::updateScript(QString gameScript)
connect(m_inputConvert, &InputConvertBase::grabCursor, this, &Controller::grabCursor);
}

bool Controller::isCurrentCustomKeymap()
{
if (!m_inputConvert) {
return false;
}

return m_inputConvert->isCurrentCustomKeymap();
}

void Controller::onPostBackOrScreenOn()
{
ControlMsg *controlMsg = new ControlMsg(ControlMsg::CMT_BACK_OR_SCREEN_ON);
Expand Down
1 change: 1 addition & 0 deletions QtScrcpy/device/controller/controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class Controller : public QObject
void test(QRect rc);

void updateScript(QString gameScript = "");
bool isCurrentCustomKeymap();

public slots:
void onPostGoBack();
Expand Down
4 changes: 4 additions & 0 deletions QtScrcpy/device/controller/inputconvert/inputconvertbase.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ class InputConvertBase : public QObject
virtual void mouseEvent(const QMouseEvent *from, const QSize &frameSize, const QSize &showSize) = 0;
virtual void wheelEvent(const QWheelEvent *from, const QSize &frameSize, const QSize &showSize) = 0;
virtual void keyEvent(const QKeyEvent *from, const QSize &frameSize, const QSize &showSize) = 0;
virtual bool isCurrentCustomKeymap()
{
return false;
};

signals:
void grabCursor(bool grab);
Expand Down
56 changes: 50 additions & 6 deletions QtScrcpy/device/controller/inputconvert/inputconvertgame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ void InputConvertGame::keyEvent(const QKeyEvent *from, const QSize &frameSize, c
}

// small eyes
if (from->key() == m_keyMap.getMouseMoveMap().data.mouseMove.smallEyes.key) {
if (m_keyMap.isValidMouseMoveMap() && from->key() == m_keyMap.getMouseMoveMap().data.mouseMove.smallEyes.key) {
m_ctrlMouseMove.smallEyes = (QEvent::KeyPress == from->type());

if (QEvent::KeyPress == from->type()) {
Expand Down Expand Up @@ -110,6 +110,9 @@ void InputConvertGame::keyEvent(const QKeyEvent *from, const QSize &frameSize, c
case KeyMap::KMT_CLICK_TWICE:
processKeyClick(node.data.clickTwice.keyNode.pos, true, false, from);
return;
case KeyMap::KMT_CLICK_MULTI:
processKeyClickMulti(node.data.clickMulti.keyNode.delayClickNodes, node.data.clickMulti.keyNode.delayClickNodesCount, from);
return;
case KeyMap::KMT_DRAG:
processKeyDrag(node.data.drag.keyNode.pos, node.data.drag.keyNode.extendPos, from);
return;
Expand All @@ -121,6 +124,11 @@ void InputConvertGame::keyEvent(const QKeyEvent *from, const QSize &frameSize, c
}
}

bool InputConvertGame::isCurrentCustomKeymap()
{
return m_gameMap;
}

void InputConvertGame::loadKeyMap(const QString &json)
{
m_keyMap.loadKeyMap(json);
Expand Down Expand Up @@ -164,8 +172,15 @@ void InputConvertGame::sendTouchEvent(int id, QPointF pos, AndroidMotioneventAct
if (!controlMsg) {
return;
}
controlMsg->setInjectTouchMsgData(
static_cast<quint64>(id), action, static_cast<AndroidMotioneventButtons>(0), QRect(calcFrameAbsolutePos(pos).toPoint(), m_frameSize), 1.0f);

QPoint absolutePos = calcFrameAbsolutePos(pos).toPoint();
static QPoint lastAbsolutePos = absolutePos;
if (AMOTION_EVENT_ACTION_MOVE == action && lastAbsolutePos == absolutePos) {
return;
}
lastAbsolutePos = absolutePos;

controlMsg->setInjectTouchMsgData(static_cast<quint64>(id), action, static_cast<AndroidMotioneventButtons>(0), QRect(absolutePos, m_frameSize), 1.0f);
sendControlMsg(controlMsg);
}

Expand Down Expand Up @@ -302,6 +317,34 @@ void InputConvertGame::processKeyClick(const QPointF &clickPos, bool clickTwice,
}
}

void InputConvertGame::processKeyClickMulti(const KeyMap::DelayClickNode *nodes, const int count, const QKeyEvent *from)
{
if (QEvent::KeyPress != from->type()) {
return;
}

int key = from->key();
int delay = 0;
QPointF clickPos;

for (int i = 0; i < count; i++) {
delay += nodes[i].delay;
clickPos = nodes[i].pos;
QTimer::singleShot(delay, this, [this, key, clickPos]() {
int id = attachTouchID(key);
sendTouchDownEvent(id, clickPos);
});

// Don't up it too fast
delay += 20;
QTimer::singleShot(delay, this, [this, key, clickPos]() {
int id = getTouchID(key);
sendTouchUpEvent(id, clickPos);
detachTouchID(key);
});
}
}

void InputConvertGame::processKeyDrag(const QPointF &startPos, QPointF endPos, const QKeyEvent *from)
{
if (QEvent::KeyPress == from->type()) {
Expand Down Expand Up @@ -361,8 +404,8 @@ bool InputConvertGame::processMouseMove(const QMouseEvent *from)
m_ctrlMouseMove.lastConverPos.setX(m_ctrlMouseMove.lastConverPos.x() + distance.x() / m_showSize.width());
m_ctrlMouseMove.lastConverPos.setY(m_ctrlMouseMove.lastConverPos.y() + distance.y() / m_showSize.height());

if (m_ctrlMouseMove.lastConverPos.x() < 0.1 || m_ctrlMouseMove.lastConverPos.x() > 0.8 || m_ctrlMouseMove.lastConverPos.y() < 0.1
|| m_ctrlMouseMove.lastConverPos.y() > 0.8) {
if (m_ctrlMouseMove.lastConverPos.x() < 0.05 || m_ctrlMouseMove.lastConverPos.x() > 0.95 || m_ctrlMouseMove.lastConverPos.y() < 0.05
|| m_ctrlMouseMove.lastConverPos.y() > 0.95) {
if (m_ctrlMouseMove.smallEyes) {
m_processMouseMove = false;
int delay = 30;
Expand Down Expand Up @@ -442,7 +485,7 @@ void InputConvertGame::mouseMoveStopTouch()
void InputConvertGame::startMouseMoveTimer()
{
stopMouseMoveTimer();
m_ctrlMouseMove.timer = startTimer(1000);
m_ctrlMouseMove.timer = startTimer(500);
}

void InputConvertGame::stopMouseMoveTimer()
Expand All @@ -456,6 +499,7 @@ void InputConvertGame::stopMouseMoveTimer()
bool InputConvertGame::switchGameMap()
{
m_gameMap = !m_gameMap;
qInfo() << tr("current keymap mode: %1").arg(m_gameMap ? tr("custom") : tr("normal"));

if (!m_keyMap.isValidMouseMoveMap()) {
return m_gameMap;
Expand Down
4 changes: 4 additions & 0 deletions QtScrcpy/device/controller/inputconvert/inputconvertgame.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class InputConvertGame : public InputConvertNormal
virtual void mouseEvent(const QMouseEvent *from, const QSize &frameSize, const QSize &showSize);
virtual void wheelEvent(const QWheelEvent *from, const QSize &frameSize, const QSize &showSize);
virtual void keyEvent(const QKeyEvent *from, const QSize &frameSize, const QSize &showSize);
virtual bool isCurrentCustomKeymap();

void loadKeyMap(const QString &json);

Expand All @@ -40,6 +41,9 @@ class InputConvertGame : public InputConvertNormal
// click
void processKeyClick(const QPointF &clickPos, bool clickTwice, bool switchMap, const QKeyEvent *from);

// click mutil
void processKeyClickMulti(const KeyMap::DelayClickNode *nodes, const int count, const QKeyEvent *from);

// drag
void processKeyDrag(const QPointF &startPos, QPointF endPos, const QKeyEvent *from);

Expand Down
80 changes: 79 additions & 1 deletion QtScrcpy/device/controller/inputconvert/keymap/keymap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,41 @@ void KeyMap::loadKeyMap(const QString &json)
keyMapNode.data.click.switchMap = getItemBool(node, "switchMap");
m_keyMapNodes.push_back(keyMapNode);
} break;
case KeyMap::KMT_CLICK_MULTI: {
// safe check
if (!checkForClickMulti(node)) {
qWarning() << "json error: keyMapNodes node format error";
break;
}
QPair<ActionType, int> key = getItemKey(node, "key");
if (key.first == AT_INVALID) {
qWarning() << "json error: keyMapNodes node invalid key: " << node.value("key").toString();
break;
}
KeyMapNode keyMapNode;
keyMapNode.type = type;
keyMapNode.data.clickMulti.keyNode.type = key.first;
keyMapNode.data.clickMulti.keyNode.key = key.second;

QJsonArray clickNodes = node.value("clickNodes").toArray();
QJsonObject clickNode;
keyMapNode.data.clickMulti.keyNode.delayClickNodesCount = 0;

for (int i = 0; i < clickNodes.size(); i++) {
if (i >= MAX_DELAY_CLICK_NODES) {
qInfo() << "clickNodes too much, up to " << MAX_DELAY_CLICK_NODES;
break;
}
clickNode = clickNodes.at(i).toObject();
DelayClickNode delayClickNode;
delayClickNode.delay = getItemDouble(clickNode, "delay");
delayClickNode.pos = getItemPos(clickNode, "pos");
keyMapNode.data.clickMulti.keyNode.delayClickNodes[i] = delayClickNode;
keyMapNode.data.clickMulti.keyNode.delayClickNodesCount++;
}

m_keyMapNodes.push_back(keyMapNode);
} break;
case KeyMap::KMT_STEER_WHEEL: {
// safe check
if (!checkForSteerWhell(node)) {
Expand Down Expand Up @@ -242,7 +277,7 @@ void KeyMap::loadKeyMap(const QString &json)
}
// this must be called after m_keyMapNodes is stable
makeReverseMap();
qInfo() << "Script updated.";
qInfo() << tr("Script updated, current keymap mode:normal, Press ~ key to switch keymap mode");

parseError:
if (!errorString.isEmpty()) {
Expand Down Expand Up @@ -310,6 +345,10 @@ void KeyMap::makeReverseMap()
QMultiHash<int, KeyMapNode *> &m = node.data.clickTwice.keyNode.type == AT_KEY ? m_rmapKey : m_rmapMouse;
m.insert(node.data.clickTwice.keyNode.key, &node);
} break;
case KMT_CLICK_MULTI: {
QMultiHash<int, KeyMapNode *> &m = node.data.clickMulti.keyNode.type == AT_KEY ? m_rmapKey : m_rmapMouse;
m.insert(node.data.clickMulti.keyNode.key, &node);
} break;
case KMT_STEER_WHEEL: {
QMultiHash<int, KeyMapNode *> &ml = node.data.steerWheel.left.type == AT_KEY ? m_rmapKey : m_rmapMouse;
ml.insert(node.data.steerWheel.left.key, &node);
Expand Down Expand Up @@ -410,6 +449,45 @@ bool KeyMap::checkForClick(const QJsonObject &node)
return checkForClickTwice(node) && checkItemBool(node, "switchMap");
}

bool KeyMap::checkForClickMulti(const QJsonObject &node)
{
bool ret = true;

if (!node.contains("clickNodes") || !node.value("clickNodes").isArray()) {
qWarning("json error: no find clickNodes");
return false;
}

QJsonArray clickNodes = node.value("clickNodes").toArray();
QJsonObject clickNode;
int size = clickNodes.size();
if (0 == size) {
qWarning("json error: clickNodes is empty");
return false;
}

for (int i = 0; i < size; i++) {
if (!clickNodes.at(i).isObject()) {
qWarning("json error: clickNodes node must be json object");
ret = false;
break;
}

clickNode = clickNodes.at(i).toObject();
if (!checkForDelayClickNode(clickNode)) {
ret = false;
break;
}
}

return ret;
}

bool KeyMap::checkForDelayClickNode(const QJsonObject &node)
{
return checkItemPos(node, "pos") && checkItemDouble(node, "delay");
}

bool KeyMap::checkForClickTwice(const QJsonObject &node)
{
return checkItemString(node, "key") && checkItemPos(node, "pos");
Expand Down
24 changes: 21 additions & 3 deletions QtScrcpy/device/controller/inputconvert/keymap/keymap.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#include <QRectF>
#include <QVector>

#define MAX_DELAY_CLICK_NODES 50

class KeyMap : public QObject
{
Q_OBJECT
Expand All @@ -18,6 +20,7 @@ class KeyMap : public QObject
KMT_INVALID = -1,
KMT_CLICK = 0,
KMT_CLICK_TWICE,
KMT_CLICK_MULTI,
KMT_STEER_WHEEL,
KMT_DRAG,
KMT_MOUSE_MOVE
Expand All @@ -32,13 +35,21 @@ class KeyMap : public QObject
};
Q_ENUM(ActionType)

struct DelayClickNode
{
int delay = 0;
QPointF pos = QPointF(0, 0);
};

struct KeyNode
{
ActionType type = AT_INVALID;
int key = Qt::Key_unknown;
QPointF pos = QPointF(0, 0); // normal key
QPointF extendPos = QPointF(0, 0); // for drag
double extendOffset = 0.0; // for steerWheel
QPointF pos = QPointF(0, 0); // normal key
QPointF extendPos = QPointF(0, 0); // for drag
double extendOffset = 0.0; // for steerWheel
DelayClickNode delayClickNodes[MAX_DELAY_CLICK_NODES]; // for multi clicks
int delayClickNodesCount = 0;

KeyNode(
ActionType type = AT_INVALID,
Expand Down Expand Up @@ -66,6 +77,10 @@ class KeyMap : public QObject
KeyNode keyNode;
} clickTwice;
struct
{
KeyNode keyNode;
} clickMulti;
struct
{
QPointF centerPos = { 0.0, 0.0 };
KeyNode left, right, up, down;
Expand All @@ -83,6 +98,7 @@ class KeyMap : public QObject
DATA() {}
~DATA() {}
} data;

KeyMapNode() {}
~KeyMapNode() {}
};
Expand Down Expand Up @@ -116,6 +132,8 @@ class KeyMap : public QObject

// safe check for KeyMapNode
bool checkForClick(const QJsonObject &node);
bool checkForClickMulti(const QJsonObject &node);
bool checkForDelayClickNode(const QJsonObject &node);
bool checkForClickTwice(const QJsonObject &node);
bool checkForSteerWhell(const QJsonObject &node);
bool checkForDrag(const QJsonObject &node);
Expand Down
8 changes: 8 additions & 0 deletions QtScrcpy/device/device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,14 @@ Device::GroupControlState Device::controlState()
return m_controlState;
}

bool Device::isCurrentCustomKeymap()
{
if (!m_controller) {
return false;
}
return m_controller->isCurrentCustomKeymap();
}

bool Device::saveFrame(const AVFrame *frame)
{
if (!frame) {
Expand Down
2 changes: 2 additions & 0 deletions QtScrcpy/device/device.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ class Device : public QObject
void updateScript(QString script);
Device::GroupControlState controlState();

bool isCurrentCustomKeymap();

signals:
void deviceDisconnect(QString serial);

Expand Down
6 changes: 3 additions & 3 deletions QtScrcpy/device/ui/videoform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ void VideoForm::setDevice(Device *device)
void VideoForm::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::MiddleButton) {
if (m_device) {
if (m_device && !m_device->isCurrentCustomKeymap()) {
emit m_device->postGoHome();
}
}
Expand Down Expand Up @@ -580,7 +580,7 @@ void VideoForm::mouseDoubleClickEvent(QMouseEvent *event)
}
}

if (event->button() == Qt::RightButton && m_device) {
if (event->button() == Qt::RightButton && m_device && !m_device->isCurrentCustomKeymap()) {
emit m_device->postBackOrScreenOn();
}

Expand Down Expand Up @@ -701,7 +701,7 @@ void VideoForm::dropEvent(QDropEvent *event)
const QMimeData *qm = event->mimeData();
QList<QUrl> urls = qm->urls();

for (const QUrl& url : urls) {
for (const QUrl &url : urls) {
QString file = url.toLocalFile();
QFileInfo fileInfo(file);

Expand Down
Loading

0 comments on commit e499cf9

Please sign in to comment.