update:重构controller

controller管理inputconvert和receiver
receiver负责接收终端的消息
inputconvert负责转化鼠标键盘消息
This commit is contained in:
Barry 2019-06-20 09:18:00 +08:00
commit 94162fe4e0
23 changed files with 163 additions and 101 deletions

1
.gitignore vendored
View file

@ -132,3 +132,4 @@
/output/win/debug/QtScrcpy.pdb
/output/win/debug/QtScrcpy.exe
/output/win/release/QtScrcpy.exe
build

View file

@ -55,7 +55,7 @@ include ($$PWD/decoder/decoder.pri)
include ($$PWD/render/render.pri)
include ($$PWD/stream/stream.pri)
include ($$PWD/android/android.pri)
include ($$PWD/inputcontrol/inputcontrol.pri)
include ($$PWD/controller/controller.pri)
include ($$PWD/uibase/uibase.pri)
include ($$PWD/fontawesome/fontawesome.pri)
include ($$PWD/filehandler/filehandler.pri)
@ -72,7 +72,7 @@ INCLUDEPATH += \
$$PWD/render \
$$PWD/stream \
$$PWD/android \
$$PWD/inputcontrol \
$$PWD/controller \
$$PWD/uibase \
$$PWD/filehandler \
$$PWD/recorder \

View file

@ -8,6 +8,11 @@
Controller::Controller(QObject* parent) : QObject(parent)
{
m_receiver = new Receiver(this);
Q_ASSERT(m_receiver);
m_inputConvert = new InputConvertGame(this);
Q_ASSERT(m_inputConvert);
connect(m_inputConvert, &InputConvertGame::grabCursor, this, &Controller::grabCursor);
}
Controller::~Controller()
@ -21,12 +26,7 @@ void Controller::setControlSocket(QTcpSocket* controlSocket)
return;
}
m_controlSocket = controlSocket;
connect(controlSocket, &QTcpSocket::readyRead, m_receiver, &Receiver::onReadyRead);
}
QTcpSocket *Controller::getControlSocket()
{
return m_controlSocket;
m_receiver->setControlSocket(controlSocket);
}
void Controller::postControlMsg(ControlMsg *controlMsg)
@ -43,6 +43,27 @@ void Controller::test(QRect rc)
postControlMsg(controlMsg);
}
void Controller::mouseEvent(const QMouseEvent *from, const QSize &frameSize, const QSize &showSize)
{
if (m_inputConvert) {
m_inputConvert->mouseEvent(from, frameSize, showSize);
}
}
void Controller::wheelEvent(const QWheelEvent *from, const QSize &frameSize, const QSize &showSize)
{
if (m_inputConvert) {
m_inputConvert->wheelEvent(from, frameSize, showSize);
}
}
void Controller::keyEvent(const QKeyEvent *from, const QSize &frameSize, const QSize &showSize)
{
if (m_inputConvert) {
m_inputConvert->keyEvent(from, frameSize, showSize);
}
}
bool Controller::event(QEvent *event)
{
if (event && event->type() == ControlMsg::Control) {

View file

@ -4,8 +4,9 @@
#include <QObject>
#include <QPointer>
#include "inputconvertgame.h"
class QTcpSocket;
class ControlMsg;
class Receiver;
class Controller : public QObject
{
@ -15,10 +16,17 @@ public:
virtual ~Controller();
void setControlSocket(QTcpSocket* controlSocket);
QTcpSocket* getControlSocket();
void postControlMsg(ControlMsg* controlMsg);
void test(QRect rc);
// for input convert
void mouseEvent(const QMouseEvent* from, const QSize& frameSize, const QSize& showSize);
void wheelEvent(const QWheelEvent* from, const QSize& frameSize, const QSize& showSize);
void keyEvent(const QKeyEvent* from, const QSize& frameSize, const QSize& showSize);
signals:
void grabCursor(bool grab);
protected:
bool event(QEvent *event);
@ -28,6 +36,7 @@ private:
private:
QPointer<QTcpSocket> m_controlSocket;
QPointer<Receiver> m_receiver;
QPointer<InputConvertBase> m_inputConvert;
};
#endif // CONTROLLER_H

View file

@ -0,0 +1,14 @@
HEADERS += \
$$PWD/controller.h
SOURCES += \
$$PWD/controller.cpp
include ($$PWD/receiver/receiver.pri)
include ($$PWD/inputconvert/inputconvert.pri)
INCLUDEPATH += \
$$PWD/receiver \
$$PWD/inputconvert

View file

@ -1,18 +1,12 @@
HEADERS += \
$$PWD/controller.h \
$$PWD/inputconvertbase.h \
$$PWD/inputconvertgame.h \
$$PWD/inputconvertnormal.h \
$$PWD/receiver.h \
$$PWD/controlmsg.h \
$$PWD/devicemsg.h
$$PWD/controlmsg.h
SOURCES += \
$$PWD/controller.cpp \
$$PWD/inputconvertbase.cpp \
$$PWD/inputconvertgame.cpp \
$$PWD/inputconvertnormal.cpp \
$$PWD/receiver.cpp \
$$PWD/controlmsg.cpp \
$$PWD/devicemsg.cpp
$$PWD/controlmsg.cpp

View file

@ -0,0 +1,22 @@
#include "inputconvertbase.h"
#include "controller.h"
InputConvertBase::InputConvertBase(Controller* controller)
: QObject(controller)
, m_controller(controller)
{
Q_ASSERT(controller);
}
InputConvertBase::~InputConvertBase()
{
}
void InputConvertBase::sendControlMsg(ControlMsg *msg)
{
if (msg && m_controller) {
m_controller->postControlMsg(msg);
}
}

View file

@ -4,14 +4,16 @@
#include <QMouseEvent>
#include <QWheelEvent>
#include <QKeyEvent>
#include <QPointer>
#include "controlmsg.h"
#include "controller.h"
class InputConvertBase
class Controller;
class InputConvertBase : public QObject
{
Q_OBJECT
public:
InputConvertBase();
InputConvertBase(Controller* controller);
virtual ~InputConvertBase();
// the frame size may be different from the real device size, so we need the size
@ -20,11 +22,14 @@ public:
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;
void setControlSocket(QTcpSocket* controlSocket);
signals:
void grabCursor(bool grab);
protected:
void sendControlMsg(ControlMsg* msg);
private:
Controller m_controller;
QPointer<Controller> m_controller;
};
#endif // INPUTCONVERTBASE_H

View file

@ -6,7 +6,8 @@
#define CURSOR_POS_CHECK 50
InputConvertGame::InputConvertGame(QObject* parent) : QObject(parent)
InputConvertGame::InputConvertGame(Controller* controller)
: InputConvertNormal(controller)
{
}

View file

@ -5,20 +5,17 @@
#include "inputconvertnormal.h"
#define MULTI_TOUCH_MAX_NUM 10
class InputConvertGame : public QObject, public InputConvertNormal
class InputConvertGame : public InputConvertNormal
{
Q_OBJECT
public:
InputConvertGame(QObject* parent = Q_NULLPTR);
InputConvertGame(Controller* controller);
virtual ~InputConvertGame();
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);
signals:
void grabCursor(bool grab);
protected:
void updateSize(const QSize& frameSize, const QSize& showSize);
void sendTouchDownEvent(int id, QPointF pos);

View file

@ -1,6 +1,7 @@
#include "inputconvertnormal.h"
InputConvertNormal::InputConvertNormal()
InputConvertNormal::InputConvertNormal(Controller* controller)
: InputConvertBase(controller)
{
}

View file

@ -5,8 +5,9 @@
class InputConvertNormal : public InputConvertBase
{
Q_OBJECT
public:
InputConvertNormal();
InputConvertNormal(Controller* controller);
virtual ~InputConvertNormal();
virtual void mouseEvent(const QMouseEvent* from, const QSize& frameSize, const QSize& showSize);

View file

@ -1,37 +1,42 @@
#include <QTcpSocket>
#include <QApplication>
#include <QClipboard>
#include <QTcpSocket>
#include "receiver.h"
#include "controller.h"
#include "devicemsg.h"
Receiver::Receiver(Controller* controller) : QObject(controller)
{
m_controller = controller;
Q_ASSERT(controller);
Receiver::Receiver(QObject *parent) : QObject(parent)
{
}
Receiver::~Receiver()
{
}
void Receiver::setControlSocket(QTcpSocket *controlSocket)
{
if (m_controlSocket || !controlSocket) {
return;
}
m_controlSocket = controlSocket;
connect(controlSocket, &QTcpSocket::readyRead, this, &Receiver::onReadyRead);
}
void Receiver::onReadyRead()
{
QTcpSocket* controlSocket = m_controller->getControlSocket();
if (!controlSocket) {
{
if (!m_controlSocket) {
return;
}
while (controlSocket->bytesAvailable()) {
QByteArray byteArray = controlSocket->peek(controlSocket->bytesAvailable());
while (m_controlSocket->bytesAvailable()) {
QByteArray byteArray = m_controlSocket->peek(m_controlSocket->bytesAvailable());
DeviceMsg deviceMsg;
qint32 consume = deviceMsg.deserialize(byteArray);
if (0 >= consume) {
break;
}
controlSocket->read(consume);
m_controlSocket->read(consume);
processMsg(&deviceMsg);
}
}

View file

@ -3,15 +3,17 @@
#include <QPointer>
class Controller;
class QTcpSocket;
class DeviceMsg;
class Receiver : public QObject
{
Q_OBJECT
public:
explicit Receiver(Controller *controller);
explicit Receiver(QObject *parent = Q_NULLPTR);
virtual ~Receiver();
void setControlSocket(QTcpSocket *controlSocket);
public slots:
void onReadyRead();
@ -19,7 +21,7 @@ protected:
void processMsg(DeviceMsg *deviceMsg);
private:
QPointer<Controller> m_controller;
QPointer<QTcpSocket> m_controlSocket;
};
#endif // RECEIVER_H

View file

@ -0,0 +1,7 @@
HEADERS += \
$$PWD/devicemsg.h \
$$PWD/receiver.h
SOURCES += \
$$PWD/devicemsg.cpp \
$$PWD/receiver.cpp

View file

@ -1,24 +0,0 @@
#include "inputconvertbase.h"
InputConvertBase::InputConvertBase()
{
}
InputConvertBase::~InputConvertBase()
{
}
void InputConvertBase::setControlSocket(QTcpSocket *controlSocket)
{
m_controller.setControlSocket(controlSocket);
}
void InputConvertBase::sendControlMsg(ControlMsg *msg)
{
if (msg) {
m_controller.postControlMsg(msg);
}
}

View file

@ -20,7 +20,7 @@
#include "ui_videoform.h"
#include "iconhelper.h"
#include "toolform.h"
#include "controlmsg.h"
//#include "controlmsg.h"
#include "mousetap/mousetap.h"
VideoForm::VideoForm(const QString& serial, quint16 maxSize, quint32 bitRate, const QString& fileName, bool closeScreen, QWidget *parent) :
@ -39,6 +39,8 @@ VideoForm::VideoForm(const QString& serial, quint16 maxSize, quint32 bitRate, co
m_vb->init();
m_decoder = new Decoder(m_vb);
m_stream.setDecoder(m_decoder);
m_controller = new Controller(this);
if (!fileName.trimmed().isEmpty()) {
m_recorder = new Recorder(fileName.trimmed());
m_stream.setRecoder(m_recorder);
@ -149,7 +151,8 @@ void VideoForm::initSignals()
QMessageBox::information(this, "QtScrcpy", tr("file transfer failed"), QMessageBox::Ok);
}
});
connect(&m_inputConvert, &InputConvertGame::grabCursor, this, [this](bool grab){
connect(m_controller, &Controller::grabCursor, this, [this](bool grab){
#ifdef Q_OS_WIN32
MouseTap::getInstance()->enableMouseEventTap(ui->videoWidget, grab);
@ -185,7 +188,7 @@ void VideoForm::initSignals()
m_stream.startDecode();
// init controller
m_inputConvert.setControlSocket(m_server->getControlSocket());
m_controller->setControlSocket(m_server->getControlSocket());
if (m_closeScreen) {
setScreenPowerMode(ControlMsg::SPM_OFF);
@ -358,7 +361,7 @@ void VideoForm::postTurnOn()
if (!controlMsg) {
return;
}
m_inputConvert.sendControlMsg(controlMsg);
m_controller->postControlMsg(controlMsg);
}
void VideoForm::expandNotificationPanel()
@ -367,7 +370,7 @@ void VideoForm::expandNotificationPanel()
if (!controlMsg) {
return;
}
m_inputConvert.sendControlMsg(controlMsg);
m_controller->postControlMsg(controlMsg);
}
void VideoForm::collapseNotificationPanel()
@ -376,7 +379,7 @@ void VideoForm::collapseNotificationPanel()
if (!controlMsg) {
return;
}
m_inputConvert.sendControlMsg(controlMsg);
m_controller->postControlMsg(controlMsg);
}
void VideoForm::requestDeviceClipboard()
@ -385,7 +388,7 @@ void VideoForm::requestDeviceClipboard()
if (!controlMsg) {
return;
}
m_inputConvert.sendControlMsg(controlMsg);
m_controller->postControlMsg(controlMsg);
}
void VideoForm::setDeviceClipboard()
@ -397,7 +400,7 @@ void VideoForm::setDeviceClipboard()
return;
}
controlMsg->setSetClipboardMsgData(text);
m_inputConvert.sendControlMsg(controlMsg);
m_controller->postControlMsg(controlMsg);
}
void VideoForm::clipboardPaste()
@ -414,7 +417,7 @@ void VideoForm::postTextInput(QString& text)
return;
}
controlMsg->setInjectTextMsgData(text);
m_inputConvert.sendControlMsg(controlMsg);
m_controller->postControlMsg(controlMsg);
}
void VideoForm::setScreenPowerMode(ControlMsg::ScreenPowerMode mode)
@ -424,7 +427,7 @@ void VideoForm::setScreenPowerMode(ControlMsg::ScreenPowerMode mode)
return;
}
controlMsg->setSetScreenPowerModeData(mode);
m_inputConvert.sendControlMsg(controlMsg);
m_controller->postControlMsg(controlMsg);
}
void VideoForm::staysOnTop(bool top)
@ -454,21 +457,21 @@ void VideoForm::postKeyCodeClick(AndroidKeycode keycode)
return;
}
controlEventDown->setInjectKeycodeMsgData(AKEY_EVENT_ACTION_DOWN, keycode, AMETA_NONE);
m_inputConvert.sendControlMsg(controlEventDown);
m_controller->postControlMsg(controlEventDown);
ControlMsg* controlEventUp = new ControlMsg(ControlMsg::CMT_INJECT_KEYCODE);
if (!controlEventUp) {
return;
}
controlEventUp->setInjectKeycodeMsgData(AKEY_EVENT_ACTION_UP, keycode, AMETA_NONE);
m_inputConvert.sendControlMsg(controlEventUp);
m_controller->postControlMsg(controlEventUp);
}
void VideoForm::mousePressEvent(QMouseEvent *event)
{
if (ui->videoWidget->geometry().contains(event->pos())) {
event->setLocalPos(ui->videoWidget->mapFrom(this, event->localPos().toPoint()));
m_inputConvert.mouseEvent(event, ui->videoWidget->frameSize(), ui->videoWidget->size());
m_controller->mouseEvent(event, ui->videoWidget->frameSize(), ui->videoWidget->size());
} else {
if (event->button() == Qt::LeftButton) {
m_dragPosition = event->globalPos() - frameGeometry().topLeft();
@ -481,7 +484,7 @@ void VideoForm::mouseReleaseEvent(QMouseEvent *event)
{
if (ui->videoWidget->geometry().contains(event->pos())) {
event->setLocalPos(ui->videoWidget->mapFrom(this, event->localPos().toPoint()));
m_inputConvert.mouseEvent(event, ui->videoWidget->frameSize(), ui->videoWidget->size());
m_controller->mouseEvent(event, ui->videoWidget->frameSize(), ui->videoWidget->size());
}
}
@ -489,7 +492,7 @@ void VideoForm::mouseMoveEvent(QMouseEvent *event)
{
if (ui->videoWidget->geometry().contains(event->pos())) {
event->setLocalPos(ui->videoWidget->mapFrom(this, event->localPos().toPoint()));
m_inputConvert.mouseEvent(event, ui->videoWidget->frameSize(), ui->videoWidget->size());
m_controller->mouseEvent(event, ui->videoWidget->frameSize(), ui->videoWidget->size());
} else {
if (event->buttons() & Qt::LeftButton) {
move(event->globalPos() - m_dragPosition);
@ -509,7 +512,7 @@ void VideoForm::wheelEvent(QWheelEvent *event)
*/
QWheelEvent wheelEvent(pos, event->globalPosF(), event->delta(),
event->buttons(), event->modifiers(), event->orientation());
m_inputConvert.wheelEvent(&wheelEvent, ui->videoWidget->frameSize(), ui->videoWidget->size());
m_controller->wheelEvent(&wheelEvent, ui->videoWidget->frameSize(), ui->videoWidget->size());
}
}
@ -533,13 +536,13 @@ void VideoForm::keyPressEvent(QKeyEvent *event)
}
//qDebug() << "keyPressEvent" << event->isAutoRepeat();
m_inputConvert.keyEvent(event, ui->videoWidget->frameSize(), ui->videoWidget->size());
m_controller->keyEvent(event, ui->videoWidget->frameSize(), ui->videoWidget->size());
}
void VideoForm::keyReleaseEvent(QKeyEvent *event)
{
//qDebug() << "keyReleaseEvent" << event->isAutoRepeat();
m_inputConvert.keyEvent(event, ui->videoWidget->frameSize(), ui->videoWidget->size());
m_controller->keyEvent(event, ui->videoWidget->frameSize(), ui->videoWidget->size());
}
void VideoForm::paintEvent(QPaintEvent *paint)

View file

@ -7,9 +7,8 @@
#include "server.h"
#include "stream.h"
#include "inputconvertnormal.h"
#include "inputconvertgame.h"
#include "filehandler.h"
#include "controller.h"
namespace Ui {
class videoForm;
@ -22,7 +21,6 @@ class Decoder;
class VideoForm : public QWidget
{
Q_OBJECT
public:
explicit VideoForm(const QString& serial, quint16 maxSize = 720, quint32 bitRate = 8000000, const QString& fileName = "", bool closeScreen = false, QWidget *parent = 0);
~VideoForm();
@ -72,25 +70,31 @@ protected:
void dropEvent(QDropEvent *event);
private:
Ui::videoForm *ui;
QSize frameSize;
Server* m_server = Q_NULLPTR;
Stream m_stream;
// ui
Ui::videoForm *ui;
QPointer<ToolForm> m_toolForm;
QPointer<QWidget> m_loadingWidget;
// server relevant
Server* m_server = Q_NULLPTR;
VideoBuffer* m_vb = Q_NULLPTR;
Decoder* m_decoder = Q_NULLPTR;
//InputConvertNormal m_inputConvert;
InputConvertGame m_inputConvert;
Recorder* m_recorder = Q_NULLPTR;
QPointer<Controller> m_controller;
Stream m_stream;
FileHandler m_fileHandler;
// server params
QString m_serial = "";
quint16 m_maxSize = 720;
quint32 m_bitRate = 8000000;
// assist member
QSize frameSize;
QPoint m_dragPosition;
float m_widthHeightRatio = 0.5f;
QPointer<ToolForm> m_toolForm;
Recorder* m_recorder = Q_NULLPTR;
QTime m_startTimeCount;
QPointer<QWidget> m_loadingWidget;
bool m_closeScreen = false;
QTime m_startTimeCount;
};
#endif // VIDEOFORM_H

View file

@ -12,7 +12,6 @@ b35733edb6df2a00b6af9b1c98627d344c377963
只录制不启动窗口先重构目前启动流程在videoform里
跳过帧改为动态配置,而不是静态编译 https://github.com/Genymobile/scrcpy/commit/ebccb9f6cc111e8acfbe10d656cac5c1f1b744a0
单独线程打印帧率 https://github.com/Genymobile/scrcpy/commit/e2a272bf99ecf48fcb050177113f903b3fb323c4
ÖØ¹¹input
重构videoform
重构整个目录
群控