refactor: move videoform to outside of device

This commit is contained in:
Barry 2022-05-23 09:33:36 +08:00
commit ea0d55b175
15 changed files with 471 additions and 321 deletions

View file

@ -105,6 +105,13 @@ set(QC_COMMON_SOURCES
) )
source_group(common FILES ${QC_COMMON_SOURCES}) source_group(common FILES ${QC_COMMON_SOURCES})
# include
set(QC_INCLUDE_SOURCES
include/QtScrcpyCore.h
include/QtScrcpyCoreDef.h
)
source_group(include FILES ${QC_INCLUDE_SOURCES})
# device # device
set(QC_DEVICE_SOURCES set(QC_DEVICE_SOURCES
device/device.h device/device.h
@ -259,6 +266,7 @@ qt5_add_translation(QC_QM_FILES ${QC_TS_FILES})
# all sources # all sources
set(QC_PROJECT_SOURCES set(QC_PROJECT_SOURCES
${QC_INCLUDE_SOURCES}
${QC_ADB_SOURCES} ${QC_ADB_SOURCES}
${QC_COMMON_SOURCES} ${QC_COMMON_SOURCES}
${QC_DEVICE_SOURCES} ${QC_DEVICE_SOURCES}

View file

@ -42,6 +42,7 @@ SOURCES += \
main.cpp main.cpp
# 子工程 # 子工程
include ($$PWD/include/include.pri)
include ($$PWD/common/common.pri) include ($$PWD/common/common.pri)
include ($$PWD/adb/adb.pri) include ($$PWD/adb/adb.pri)
include ($$PWD/uibase/uibase.pri) include ($$PWD/uibase/uibase.pri)

View file

@ -8,13 +8,14 @@
#include "decoder.h" #include "decoder.h"
#include "device.h" #include "device.h"
#include "filehandler.h" #include "filehandler.h"
#include "mousetap/mousetap.h"
#include "recorder.h" #include "recorder.h"
#include "server.h" #include "server.h"
#include "stream.h" #include "stream.h"
#include "videoform.h" #include "videoform.h"
Device::Device(DeviceParams params, QObject *parent) : QObject(parent), m_params(params) namespace qsc {
Device::Device(DeviceParams params, QObject *parent) : IDevice(parent), m_params(params)
{ {
if (!params.display && m_params.recordFileName.trimmed().isEmpty()) { if (!params.display && m_params.recordFileName.trimmed().isEmpty()) {
qCritical("not display must be recorded"); qCritical("not display must be recorded");
@ -23,8 +24,8 @@ Device::Device(DeviceParams params, QObject *parent) : QObject(parent), m_params
if (params.display) { if (params.display) {
m_decoder = new Decoder([this](int width, int height, uint8_t* dataY, uint8_t* dataU, uint8_t* dataV, int linesizeY, int linesizeU, int linesizeV) { m_decoder = new Decoder([this](int width, int height, uint8_t* dataY, uint8_t* dataU, uint8_t* dataV, int linesizeY, int linesizeU, int linesizeV) {
if (m_videoForm) { for (const auto& item : m_deviceObservers) {
m_videoForm->updateRender(width, height, dataY, dataU, dataV, linesizeY, linesizeU, linesizeV); item->onFrame(width, height, dataY, dataU, dataV, linesizeY, linesizeU, linesizeV);
} }
}, this); }, this);
m_fileHandler = new FileHandler(this); m_fileHandler = new FileHandler(this);
@ -35,8 +36,6 @@ Device::Device(DeviceParams params, QObject *parent) : QObject(parent), m_params
return m_server->getControlSocket()->write(buffer.data(), buffer.length()); return m_server->getControlSocket()->write(buffer.data(), buffer.length());
}, params.gameScript, this); }, params.gameScript, this);
m_videoForm = new VideoForm(params.framelessWindow, Config::getInstance().getSkin());
m_videoForm->setDevice(this);
} }
m_stream = new Stream([this](quint8 *buf, qint32 bufSize) -> qint32 { m_stream = new Stream([this](quint8 *buf, qint32 bufSize) -> qint32 {
@ -57,17 +56,27 @@ Device::Device(DeviceParams params, QObject *parent) : QObject(parent), m_params
Device::~Device() Device::~Device()
{ {
disconnectDevice(); Device::disconnectDevice();
} }
VideoForm *Device::getVideoForm() void Device::setUserData(void *data)
{ {
return m_videoForm; m_userData = data;
} }
Server *Device::getServer() void *Device::getUserData()
{ {
return m_server; return m_userData;
}
void Device::registerDeviceObserver(DeviceObserver *observer)
{
m_deviceObservers.insert(observer);
}
void Device::deRegisterDeviceObserver(DeviceObserver *observer)
{
m_deviceObservers.erase(observer);
} }
const QString &Device::getSerial() const QString &Device::getSerial()
@ -75,15 +84,6 @@ const QString &Device::getSerial()
return m_params.serial; return m_params.serial;
} }
const QSize Device::frameSize()
{
QSize size;
if (!m_videoForm) {
return size;
}
return m_videoForm->frameSize();
}
void Device::updateScript(QString script) void Device::updateScript(QString script)
{ {
if (m_controller) { if (m_controller) {
@ -119,34 +119,26 @@ void Device::showTouch(bool show)
qInfo() << getSerial() << " show touch " << (show ? "enable" : "disable"); qInfo() << getSerial() << " show touch " << (show ? "enable" : "disable");
} }
bool Device::isReversePort(quint16 port)
{
if (m_server && m_server->isReverse() && port == m_server->getParams().localPort) {
return true;
}
return false;
}
void Device::initSignals() void Device::initSignals()
{ {
if (m_controller) { if (m_controller) {
connect(m_controller, &Controller::grabCursor, this, &Device::grabCursor); connect(m_controller, &Controller::grabCursor, this, [this](bool grab){
} for (const auto& item : m_deviceObservers) {
if (m_controller) { item->grabCursor(grab);
/* }
connect(this, &Device::postAppSwitch, m_controller, &Controller::onPostAppSwitch); });
connect(this, &Device::postPower, m_controller, &Controller::onPostPower);
connect(this, &Device::postVolumeUp, m_controller, &Controller::onPostVolumeUp);
connect(this, &Device::postVolumeDown, m_controller, &Controller::onPostVolumeDown);
connect(this, &Device::postCopy, m_controller, &Controller::onCopy);
connect(this, &Device::postCut, m_controller, &Controller::onCut);
connect(this, &Device::setScreenPowerMode, m_controller, &Controller::onSetScreenPowerMode);
connect(this, &Device::expandNotificationPanel, m_controller, &Controller::onExpandNotificationPanel);
connect(this, &Device::collapsePanel, m_controller, &Controller::onCollapsePanel);
connect(this, &Device::mouseEvent, m_controller, &Controller::onMouseEvent);
connect(this, &Device::wheelEvent, m_controller, &Controller::onWheelEvent);
connect(this, &Device::keyEvent, m_controller, &Controller::onKeyEvent);
connect(this, &Device::postBackOrScreenOn, m_controller, &Controller::onPostBackOrScreenOn);
connect(this, &Device::setDeviceClipboard, m_controller, &Controller::onSetDeviceClipboard);
connect(this, &Device::clipboardPaste, m_controller, &Controller::onClipboardPaste);
connect(this, &Device::postTextInput, m_controller, &Controller::onPostTextInput);
*/
} }
if (m_fileHandler) { if (m_fileHandler) {
connect(m_fileHandler, &FileHandler::fileHandlerResult, this, [this](FileHandler::FILE_HANDLER_RESULT processResult, bool isApk) { connect(m_fileHandler, &FileHandler::fileHandlerResult, this, [](FileHandler::FILE_HANDLER_RESULT processResult, bool isApk) {
QString tipsType = ""; QString tipsType = "";
if (isApk) { if (isApk) {
tipsType = tr("install apk"); tipsType = tr("install apk");
@ -174,29 +166,6 @@ void Device::initSignals()
double diff = m_startTimeCount.elapsed() / 1000.0; double diff = m_startTimeCount.elapsed() / 1000.0;
qInfo() << QString("server start finish in %1s").arg(diff).toStdString().c_str(); qInfo() << QString("server start finish in %1s").arg(diff).toStdString().c_str();
// update ui
if (m_videoForm) {
// must be show before updateShowSize
m_videoForm->show();
QString name = Config::getInstance().getNickName(m_params.serial);
if (name.isEmpty()) {
name = Config::getInstance().getTitle();
}
m_videoForm->setWindowTitle(name + "-" + m_params.serial);
m_videoForm->updateShowSize(size);
bool deviceVer = size.height() > size.width();
QRect rc = Config::getInstance().getRect(getSerial());
bool rcVer = rc.height() > rc.width();
// same width/height rate
if (rc.isValid() && (deviceVer == rcVer)) {
// mark: resize is for fix setGeometry magneticwidget bug
m_videoForm->resize(rc.size());
m_videoForm->setGeometry(rc);
}
}
// init recorder // init recorder
if (m_recorder) { if (m_recorder) {
m_recorder->setFrameSize(size); m_recorder->setFrameSize(size);
@ -272,7 +241,11 @@ void Device::initSignals()
} }
if (m_decoder) { if (m_decoder) {
connect(m_decoder, &Decoder::updateFPS, m_videoForm, &VideoForm::updateFPS); connect(m_decoder, &Decoder::updateFPS, this, [this](quint32 fps) {
for (const auto& item : m_deviceObservers) {
item->updateFPS(fps);
}
});
} }
} }
@ -333,10 +306,6 @@ void Device::disconnectDevice()
} }
m_recorder->close(); m_recorder->close();
} }
if (m_videoForm) {
m_videoForm->close();
m_videoForm->deleteLater();
}
emit deviceDisconnected(m_params.serial); emit deviceDisconnected(m_params.serial);
} }
@ -413,11 +382,17 @@ void Device::postCut()
m_controller->cut(); m_controller->cut();
} }
void Device::setScreenPowerMode(ControlMsg::ScreenPowerMode mode) void Device::setScreenPowerMode(bool open)
{ {
if (!m_controller) { if (!m_controller) {
return; return;
} }
ControlMsg::ScreenPowerMode mode{};
if (open) {
mode = ControlMsg::SPM_NORMAL;
} else {
mode = ControlMsg::SPM_OFF;
}
m_controller->setScreenPowerMode(mode); m_controller->setScreenPowerMode(mode);
} }
@ -517,15 +492,6 @@ void Device::keyEvent(const QKeyEvent *from, const QSize &frameSize, const QSize
m_controller->keyEvent(from, frameSize, showSize); m_controller->keyEvent(from, frameSize, showSize);
} }
void Device::grabCursor(bool grab)
{
if (!m_videoForm) {
return;
}
QRect rc = m_videoForm->getGrabCursorRect();
MouseTap::getInstance()->enableMouseEventTap(rc, grab);
}
bool Device::isCurrentCustomKeymap() bool Device::isCurrentCustomKeymap()
{ {
if (!m_controller) { if (!m_controller) {
@ -562,3 +528,5 @@ bool Device::saveFrame(int width, int height, uint8_t* dataRGB32)
qInfo() << "screenshot save to " << absFilePath; qInfo() << "screenshot save to " << absFilePath;
return true; return true;
} }
}

View file

@ -1,10 +1,13 @@
#ifndef DEVICE_H #ifndef DEVICE_H
#define DEVICE_H #define DEVICE_H
#include <set>
#include <QElapsedTimer> #include <QElapsedTimer>
#include <QPointer> #include <QPointer>
#include <QTime> #include <QTime>
#include "../include/QtScrcpyCore.h"
#include "controlmsg.h" #include "controlmsg.h"
class QMouseEvent; class QMouseEvent;
@ -19,76 +22,59 @@ class Stream;
class VideoForm; class VideoForm;
class Controller; class Controller;
struct AVFrame; struct AVFrame;
class Device : public QObject
namespace qsc {
class Device : public IDevice
{ {
Q_OBJECT Q_OBJECT
public: public:
struct DeviceParams
{
QString serverLocalPath = ""; // 本地安卓server路径
QString serverRemotePath = ""; // 要推送到远端设备的server路径
QString recordFileName = ""; // 视频录制文件名
QString recordPath = ""; // 视频保存路径
QString serial = ""; // 设备序列号
quint16 localPort = 27183; // reverse时本地监听端口
quint16 maxSize = 720; // 视频分辨率
quint32 bitRate = 2000000; // 视频比特率
quint32 maxFps = 60; // 视频最大帧率
bool closeScreen = false; // 启动时自动息屏
bool useReverse = true; // true:先使用adb reverse失败后自动使用adb forwardfalse:直接使用adb forward
bool display = true; // 是否显示画面(或者仅仅后台录制)
QString gameScript = ""; // 游戏映射脚本
bool renderExpiredFrames = false; // 是否渲染延迟视频帧
int lockVideoOrientation = -1; // 是否锁定视频方向
bool stayAwake = false; // 是否保持唤醒
bool framelessWindow = false; // 是否无边框窗口
};
explicit Device(DeviceParams params, QObject *parent = nullptr); explicit Device(DeviceParams params, QObject *parent = nullptr);
virtual ~Device(); virtual ~Device();
bool connectDevice(); void setUserData(void* data) override;
void disconnectDevice(); void* getUserData() override;
void postGoBack();
void postGoHome(); void registerDeviceObserver(DeviceObserver* observer) override;
void postGoMenu(); void deRegisterDeviceObserver(DeviceObserver* observer) override;
void postAppSwitch();
void postPower(); bool connectDevice() override;
void postVolumeUp(); void disconnectDevice() override;
void postVolumeDown();
void postCopy();
void postCut();
void setScreenPowerMode(ControlMsg::ScreenPowerMode mode);
void expandNotificationPanel();
void collapsePanel();
void postBackOrScreenOn(bool down);
void postTextInput(QString &text);
void requestDeviceClipboard();
void setDeviceClipboard(bool pause = true);
void clipboardPaste();
void pushFileRequest(const QString &file, const QString &devicePath = "");
void installApkRequest(const QString &apkFile);
// key map // key map
void mouseEvent(const QMouseEvent *from, const QSize &frameSize, const QSize &showSize); void mouseEvent(const QMouseEvent *from, const QSize &frameSize, const QSize &showSize) override;
void wheelEvent(const QWheelEvent *from, const QSize &frameSize, const QSize &showSize); void wheelEvent(const QWheelEvent *from, const QSize &frameSize, const QSize &showSize) override;
void keyEvent(const QKeyEvent *from, const QSize &frameSize, const QSize &showSize); void keyEvent(const QKeyEvent *from, const QSize &frameSize, const QSize &showSize) override;
void screenshot(); void postGoBack() override;
void showTouch(bool show); void postGoHome() override;
void grabCursor(bool grab); void postGoMenu() override;
void postAppSwitch() override;
void postPower() override;
void postVolumeUp() override;
void postVolumeDown() override;
void postCopy() override;
void postCut() override;
void setScreenPowerMode(bool open) override;
void expandNotificationPanel() override;
void collapsePanel() override;
void postBackOrScreenOn(bool down) override;
void postTextInput(QString &text) override;
void requestDeviceClipboard() override;
void setDeviceClipboard(bool pause = true) override;
void clipboardPaste() override;
void pushFileRequest(const QString &file, const QString &devicePath = "") override;
void installApkRequest(const QString &apkFile) override;
VideoForm *getVideoForm(); void screenshot() override;
Server *getServer(); void showTouch(bool show) override;
const QString &getSerial(); //void grabCursor(bool grab);
const QSize frameSize();
void updateScript(QString script); bool isReversePort(quint16 port) override;
bool isCurrentCustomKeymap(); const QString &getSerial() override;
signals: void updateScript(QString script) override;
void deviceConnected(bool success, const QString& serial, const QString& deviceName, const QSize& size); bool isCurrentCustomKeymap() override;
void deviceDisconnected(QString serial);
private: private:
void initSignals(); void initSignals();
@ -103,11 +89,12 @@ private:
QPointer<Stream> m_stream; QPointer<Stream> m_stream;
QPointer<Recorder> m_recorder = Q_NULLPTR; QPointer<Recorder> m_recorder = Q_NULLPTR;
// ui
QPointer<VideoForm> m_videoForm;
QElapsedTimer m_startTimeCount; QElapsedTimer m_startTimeCount;
DeviceParams m_params; DeviceParams m_params;
std::set<DeviceObserver*> m_deviceObservers;
void* m_userData = nullptr;
}; };
}
#endif // DEVICE_H #endif // DEVICE_H

View file

@ -6,14 +6,30 @@
#include "devicemanage.h" #include "devicemanage.h"
#include "server.h" #include "server.h"
#include "videoform.h" #include "videoform.h"
#include "device.h"
namespace qsc {
#define DM_MAX_DEVICES_NUM 1000 #define DM_MAX_DEVICES_NUM 1000
DeviceManage::DeviceManage(QObject *parent) : QObject(parent) {} IDeviceManage& IDeviceManage::getInstance() {
static DeviceManage dm;
return dm;
}
DeviceManage::DeviceManage() {}
DeviceManage::~DeviceManage() {} DeviceManage::~DeviceManage() {}
bool DeviceManage::connectDevice(Device::DeviceParams params) QPointer<IDevice> DeviceManage::getDevice(const QString &serial)
{
if (!m_devices.contains(serial)) {
return QPointer<IDevice>();
}
return m_devices[serial];
}
bool DeviceManage::connectDevice(qsc::DeviceParams params)
{ {
if (params.serial.trimmed().isEmpty()) { if (params.serial.trimmed().isEmpty()) {
return false; return false;
@ -39,7 +55,7 @@ bool DeviceManage::connectDevice(Device::DeviceParams params)
} }
} }
*/ */
Device *device = new Device(params); IDevice *device = new Device(params);
connect(device, &Device::deviceConnected, this, &DeviceManage::onDeviceConnected); connect(device, &Device::deviceConnected, this, &DeviceManage::onDeviceConnected);
connect(device, &Device::deviceDisconnected, this, &DeviceManage::onDeviceDisconnected); connect(device, &Device::deviceDisconnected, this, &DeviceManage::onDeviceDisconnected);
if (!device->connectDevice()) { if (!device->connectDevice()) {
@ -56,7 +72,7 @@ bool DeviceManage::connectDevice(Device::DeviceParams params)
void DeviceManage::updateScript(QString script) void DeviceManage::updateScript(QString script)
{ {
m_script = script; m_script = script;
QMapIterator<QString, QPointer<Device>> i(m_devices); QMapIterator<QString, QPointer<IDevice>> i(m_devices);
while (i.hasNext()) { while (i.hasNext()) {
i.next(); i.next();
if (i.value()) { if (i.value()) {
@ -65,36 +81,6 @@ void DeviceManage::updateScript(QString script)
} }
} }
bool DeviceManage::staysOnTop(const QString &serial)
{
if (!serial.isEmpty() && m_devices.contains(serial)) {
auto it = m_devices.find(serial);
if (!it->data()) {
return false;
}
if (!it->data()->getVideoForm()) {
return false;
}
it->data()->getVideoForm()->staysOnTop();
}
return true;
}
void DeviceManage::showFPS(const QString &serial, bool show)
{
if (!serial.isEmpty() && m_devices.contains(serial)) {
auto it = m_devices.find(serial);
if (!it->data()) {
return;
}
if (!it->data()->getVideoForm()) {
return;
}
it->data()->getVideoForm()->showFPS(show);
}
return;
}
bool DeviceManage::disconnectDevice(const QString &serial) bool DeviceManage::disconnectDevice(const QString &serial)
{ {
bool ret = false; bool ret = false;
@ -110,7 +96,7 @@ bool DeviceManage::disconnectDevice(const QString &serial)
void DeviceManage::disconnectAllDevice() void DeviceManage::disconnectAllDevice()
{ {
QMapIterator<QString, QPointer<Device>> i(m_devices); QMapIterator<QString, QPointer<IDevice>> i(m_devices);
while (i.hasNext()) { while (i.hasNext()) {
i.next(); i.next();
if (i.value()) { if (i.value()) {
@ -121,16 +107,16 @@ void DeviceManage::disconnectAllDevice()
void DeviceManage::onDeviceConnected(bool success, const QString &serial, const QString &deviceName, const QSize &size) void DeviceManage::onDeviceConnected(bool success, const QString &serial, const QString &deviceName, const QSize &size)
{ {
emit deviceConnected(success, serial, deviceName, size);
if (!success) { if (!success) {
removeDevice(serial); removeDevice(serial);
} }
emit deviceConnected(success, serial, deviceName, size);
} }
void DeviceManage::onDeviceDisconnected(QString serial) void DeviceManage::onDeviceDisconnected(QString serial)
{ {
removeDevice(serial);
emit deviceDisconnected(serial); emit deviceDisconnected(serial);
removeDevice(serial);
} }
quint16 DeviceManage::getFreePort() quint16 DeviceManage::getFreePort()
@ -138,11 +124,11 @@ quint16 DeviceManage::getFreePort()
quint16 port = m_localPortStart; quint16 port = m_localPortStart;
while (port < m_localPortStart + DM_MAX_DEVICES_NUM) { while (port < m_localPortStart + DM_MAX_DEVICES_NUM) {
bool used = false; bool used = false;
QMapIterator<QString, QPointer<Device>> i(m_devices); QMapIterator<QString, QPointer<IDevice>> i(m_devices);
while (i.hasNext()) { while (i.hasNext()) {
i.next(); i.next();
auto device = i.value(); auto device = i.value();
if (device && device->getServer() && device->getServer()->isReverse() && port == device->getServer()->getParams().localPort) { if (device && device->isReversePort(port)) {
used = true; used = true;
break; break;
} }
@ -162,3 +148,5 @@ void DeviceManage::removeDevice(const QString &serial)
m_devices.remove(serial); m_devices.remove(serial);
} }
} }
}

View file

@ -4,26 +4,24 @@
#include <QMap> #include <QMap>
#include <QPointer> #include <QPointer>
#include "device.h" #include "../include/QtScrcpyCore.h"
class DeviceManage : public QObject namespace qsc {
class DeviceManage : public IDeviceManage
{ {
Q_OBJECT Q_OBJECT
public: public:
explicit DeviceManage(QObject *parent = nullptr); explicit DeviceManage();
virtual ~DeviceManage(); virtual ~DeviceManage();
bool connectDevice(Device::DeviceParams params); virtual QPointer<IDevice> getDevice(const QString& serial) override;
bool disconnectDevice(const QString &serial);
void disconnectAllDevice();
void updateScript(QString script); bool connectDevice(qsc::DeviceParams params) override;
bool staysOnTop(const QString &serial); bool disconnectDevice(const QString &serial) override;
void showFPS(const QString &serial, bool show); void disconnectAllDevice() override;
signals: void updateScript(QString script) override;
void deviceConnected(bool success, const QString& serial, const QString& deviceName, const QSize& size);
void deviceDisconnected(QString serial);
protected slots: protected slots:
void onDeviceConnected(bool success, const QString& serial, const QString& deviceName, const QSize& size); void onDeviceConnected(bool success, const QString& serial, const QString& deviceName, const QSize& size);
@ -34,9 +32,10 @@ private:
void removeDevice(const QString& serial); void removeDevice(const QString& serial);
private: private:
QMap<QString, QPointer<Device>> m_devices; QMap<QString, QPointer<IDevice>> m_devices;
quint16 m_localPortStart = 27183; quint16 m_localPortStart = 27183;
QString m_script; QString m_script;
}; };
}
#endif // DEVICEMANAGE_H #endif // DEVICEMANAGE_H

View file

@ -0,0 +1,93 @@
#pragma once
#include <QMouseEvent>
#include "QtScrcpyCoreDef.h"
namespace qsc {
class DeviceObserver {
protected:
DeviceObserver() {
}
virtual ~DeviceObserver() {
}
public:
virtual void onFrame(int width, int height, uint8_t* dataY, uint8_t* dataU, uint8_t* dataV, int linesizeY, int linesizeU, int linesizeV) {}
virtual void updateFPS(quint32 fps) {}
virtual void grabCursor(bool grab) {}
};
class IDevice : public QObject {
Q_OBJECT
public:
IDevice(QObject *parent = nullptr) : QObject(parent) {}
virtual ~IDevice(){}
signals:
void deviceConnected(bool success, const QString& serial, const QString& deviceName, const QSize& size);
void deviceDisconnected(QString serial);
public:
virtual void setUserData(void* data) = 0;
virtual void* getUserData() = 0;
virtual void registerDeviceObserver(DeviceObserver* observer) = 0;
virtual void deRegisterDeviceObserver(DeviceObserver* observer) = 0;
virtual bool connectDevice() = 0;
virtual void disconnectDevice() = 0;
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 void postGoBack() = 0;
virtual void postGoHome() = 0;
virtual void postGoMenu() = 0;
virtual void postAppSwitch() = 0;
virtual void postPower() = 0;
virtual void postVolumeUp() = 0;
virtual void postVolumeDown() = 0;
virtual void postCopy() = 0;
virtual void postCut() = 0;
virtual void setScreenPowerMode(bool open) = 0;
virtual void expandNotificationPanel() = 0;
virtual void collapsePanel() = 0;
virtual void postBackOrScreenOn(bool down) = 0;
virtual void postTextInput(QString &text) = 0;
virtual void requestDeviceClipboard() = 0;
virtual void setDeviceClipboard(bool pause = true) = 0;
virtual void clipboardPaste() = 0;
virtual void pushFileRequest(const QString &file, const QString &devicePath = "") = 0;
virtual void installApkRequest(const QString &apkFile) = 0;
virtual void screenshot() = 0;
virtual void showTouch(bool show) = 0;
virtual bool isReversePort(quint16 port) = 0;
virtual const QString &getSerial() = 0;
virtual void updateScript(QString script) = 0;
virtual bool isCurrentCustomKeymap() = 0;
};
class IDeviceManage : public QObject {
Q_OBJECT
public:
static IDeviceManage& getInstance();
virtual QPointer<IDevice> getDevice(const QString& serial) = 0;
virtual bool connectDevice(DeviceParams params) = 0;
virtual bool disconnectDevice(const QString &serial) = 0;
virtual void disconnectAllDevice() = 0;
virtual void updateScript(QString script) = 0;
signals:
void deviceConnected(bool success, const QString& serial, const QString& deviceName, const QSize& size);
void deviceDisconnected(QString serial);
};
}

View file

@ -0,0 +1,26 @@
#pragma once
#include <QString>
namespace qsc {
struct DeviceParams {
QString serverLocalPath = ""; // 本地安卓server路径
QString serverRemotePath = ""; // 要推送到远端设备的server路径
QString recordFileName = ""; // 视频录制文件名
QString recordPath = ""; // 视频保存路径
QString serial = ""; // 设备序列号
quint16 localPort = 27183; // reverse时本地监听端口
quint16 maxSize = 720; // 视频分辨率
quint32 bitRate = 2000000; // 视频比特率
quint32 maxFps = 60; // 视频最大帧率
bool closeScreen = false; // 启动时自动息屏
bool useReverse = true; // true:先使用adb reverse失败后自动使用adb forwardfalse:直接使用adb forward
bool display = true; // 是否显示画面(或者仅仅后台录制)
QString gameScript = ""; // 游戏映射脚本
bool renderExpiredFrames = false; // 是否渲染延迟视频帧
int lockVideoOrientation = -1; // 是否锁定视频方向
bool stayAwake = false; // 是否保持唤醒
bool framelessWindow = false; // 是否无边框窗口
};
}

View file

@ -0,0 +1,3 @@
HEADERS += \
$$PWD/QtScrcpyCore.h \
$$PWD/QtScrcpyCoreDef.h

View file

@ -95,15 +95,15 @@ Dialog::Dialog(QWidget *parent) : QDialog(parent), ui(new Ui::Dialog)
}); });
connect(m_hideIcon, &QSystemTrayIcon::activated, this, &Dialog::slotActivated); connect(m_hideIcon, &QSystemTrayIcon::activated, this, &Dialog::slotActivated);
connect(&m_deviceManage, &DeviceManage::deviceConnected, this, &Dialog::onDeviceConnected); connect(&qsc::IDeviceManage::getInstance(), &qsc::IDeviceManage::deviceConnected, this, &Dialog::onDeviceConnected);
connect(&m_deviceManage, &DeviceManage::deviceDisconnected, this, &Dialog::onDeviceDisconnected); connect(&qsc::IDeviceManage::getInstance(), &qsc::IDeviceManage::deviceDisconnected, this, &Dialog::onDeviceDisconnected);
} }
Dialog::~Dialog() Dialog::~Dialog()
{ {
qDebug() << "~Dialog()"; qDebug() << "~Dialog()";
updateBootConfig(false); updateBootConfig(false);
m_deviceManage.disconnectAllDevice(); qsc::IDeviceManage::getInstance().disconnectAllDevice();
delete ui; delete ui;
} }
@ -280,7 +280,7 @@ void Dialog::on_startServerBtn_clicked()
// this is ok that "native" toUshort is 0 // this is ok that "native" toUshort is 0
quint16 videoSize = ui->maxSizeBox->currentText().trimmed().toUShort(); quint16 videoSize = ui->maxSizeBox->currentText().trimmed().toUShort();
Device::DeviceParams params; qsc::DeviceParams params;
params.serial = ui->serialBox->currentText().trimmed(); params.serial = ui->serialBox->currentText().trimmed();
params.maxSize = videoSize; params.maxSize = videoSize;
params.bitRate = getBitRate(); params.bitRate = getBitRate();
@ -298,17 +298,12 @@ void Dialog::on_startServerBtn_clicked()
params.serverLocalPath = getServerPath(); params.serverLocalPath = getServerPath();
params.serverRemotePath = Config::getInstance().getServerPath(); params.serverRemotePath = Config::getInstance().getServerPath();
m_deviceManage.connectDevice(params); qsc::IDeviceManage::getInstance().connectDevice(params);
if (ui->alwaysTopCheck->isChecked()) {
m_deviceManage.staysOnTop(params.serial);
}
m_deviceManage.showFPS(params.serial, ui->fpsCheck->isChecked());
} }
void Dialog::on_stopServerBtn_clicked() void Dialog::on_stopServerBtn_clicked()
{ {
if (m_deviceManage.disconnectDevice(ui->serialBox->currentText().trimmed())) { if (qsc::IDeviceManage::getInstance().disconnectDevice(ui->serialBox->currentText().trimmed())) {
outLog("stop server"); outLog("stop server");
} }
} }
@ -423,12 +418,51 @@ void Dialog::getIPbyIp()
void Dialog::onDeviceConnected(bool success, const QString &serial, const QString &deviceName, const QSize &size) void Dialog::onDeviceConnected(bool success, const QString &serial, const QString &deviceName, const QSize &size)
{ {
if (!success) {
return;
}
auto videoForm = new VideoForm(ui->framelessCheck->isChecked(), Config::getInstance().getSkin());
videoForm->setSerial(serial);
qsc::IDeviceManage::getInstance().getDevice(serial)->setUserData(static_cast<void*>(videoForm));
qsc::IDeviceManage::getInstance().getDevice(serial)->registerDeviceObserver(videoForm);
videoForm->showFPS(ui->fpsCheck->isChecked());
if (ui->alwaysTopCheck->isChecked()) {
videoForm->staysOnTop();
}
// must be show before updateShowSize
videoForm->show();
QString name = Config::getInstance().getNickName(serial);
if (name.isEmpty()) {
name = Config::getInstance().getTitle();
}
videoForm->setWindowTitle(name + "-" + serial);
videoForm->updateShowSize(size);
bool deviceVer = size.height() > size.width();
QRect rc = Config::getInstance().getRect(serial);
bool rcVer = rc.height() > rc.width();
// same width/height rate
if (rc.isValid() && (deviceVer == rcVer)) {
// mark: resize is for fix setGeometry magneticwidget bug
videoForm->resize(rc.size());
videoForm->setGeometry(rc);
}
} }
void Dialog::onDeviceDisconnected(QString serial) void Dialog::onDeviceDisconnected(QString serial)
{ {
auto data = qsc::IDeviceManage::getInstance().getDevice(serial)->getUserData();
if (data) {
VideoForm* vf = static_cast<VideoForm*>(data);
qsc::IDeviceManage::getInstance().getDevice(serial)->deRegisterDeviceObserver(vf);
vf->close();
vf->deleteLater();
}
} }
void Dialog::on_wirelessDisConnectBtn_clicked() void Dialog::on_wirelessDisConnectBtn_clicked()
@ -474,7 +508,7 @@ void Dialog::on_clearOut_clicked()
void Dialog::on_stopAllServerBtn_clicked() void Dialog::on_stopAllServerBtn_clicked()
{ {
m_deviceManage.disconnectAllDevice(); qsc::IDeviceManage::getInstance().disconnectAllDevice();
} }
void Dialog::on_refreshGameScriptBtn_clicked() void Dialog::on_refreshGameScriptBtn_clicked()
@ -497,7 +531,7 @@ void Dialog::on_refreshGameScriptBtn_clicked()
void Dialog::on_applyScriptBtn_clicked() void Dialog::on_applyScriptBtn_clicked()
{ {
m_deviceManage.updateScript(getGameScript(ui->gameBox->currentText())); qsc::IDeviceManage::getInstance().updateScript(getGameScript(ui->gameBox->currentText()));
} }
void Dialog::on_recordScreenCheck_clicked(bool checked) void Dialog::on_recordScreenCheck_clicked(bool checked)

View file

@ -10,7 +10,7 @@
#include "adbprocess.h" #include "adbprocess.h"
#include "devicemanage.h" #include "../include/QtScrcpyCore.h"
namespace Ui namespace Ui
{ {
@ -76,7 +76,6 @@ protected:
private: private:
Ui::Dialog *ui; Ui::Dialog *ui;
AdbProcess m_adb; AdbProcess m_adb;
DeviceManage m_deviceManage;
QSystemTrayIcon *m_hideIcon; QSystemTrayIcon *m_hideIcon;
QMenu *m_menu; QMenu *m_menu;
QAction *m_showWindow; QAction *m_showWindow;

View file

@ -3,7 +3,6 @@
#include <QMouseEvent> #include <QMouseEvent>
#include <QShowEvent> #include <QShowEvent>
#include "device.h"
#include "iconhelper.h" #include "iconhelper.h"
#include "toolform.h" #include "toolform.h"
#include "ui_toolform.h" #include "ui_toolform.h"
@ -23,12 +22,9 @@ ToolForm::~ToolForm()
delete ui; delete ui;
} }
void ToolForm::setDevice(Device *device) void ToolForm::setSerial(const QString &serial)
{ {
if (!device) { m_serial = serial;
return;
}
m_device = device;
} }
void ToolForm::initStyle() void ToolForm::initStyle()
@ -52,7 +48,8 @@ void ToolForm::initStyle()
void ToolForm::updateGroupControl() void ToolForm::updateGroupControl()
{ {
if (!m_device) { auto device = qsc::IDeviceManage::getInstance().getDevice(m_serial);
if (!device) {
return; return;
} }
} }
@ -92,7 +89,8 @@ void ToolForm::hideEvent(QHideEvent *event)
void ToolForm::on_fullScreenBtn_clicked() void ToolForm::on_fullScreenBtn_clicked()
{ {
if (!m_device) { auto device = qsc::IDeviceManage::getInstance().getDevice(m_serial);
if (!device) {
return; return;
} }
@ -101,105 +99,115 @@ void ToolForm::on_fullScreenBtn_clicked()
void ToolForm::on_returnBtn_clicked() void ToolForm::on_returnBtn_clicked()
{ {
if (!m_device) { auto device = qsc::IDeviceManage::getInstance().getDevice(m_serial);
if (!device) {
return; return;
} }
m_device->postGoBack(); device->postGoBack();
} }
void ToolForm::on_homeBtn_clicked() void ToolForm::on_homeBtn_clicked()
{ {
if (!m_device) { auto device = qsc::IDeviceManage::getInstance().getDevice(m_serial);
if (!device) {
return; return;
} }
m_device->postGoHome(); device->postGoHome();
} }
void ToolForm::on_menuBtn_clicked() void ToolForm::on_menuBtn_clicked()
{ {
if (!m_device) { auto device = qsc::IDeviceManage::getInstance().getDevice(m_serial);
if (!device) {
return; return;
} }
m_device->postGoMenu(); device->postGoMenu();
} }
void ToolForm::on_appSwitchBtn_clicked() void ToolForm::on_appSwitchBtn_clicked()
{ {
if (!m_device) { auto device = qsc::IDeviceManage::getInstance().getDevice(m_serial);
if (!device) {
return; return;
} }
emit m_device->postAppSwitch(); emit device->postAppSwitch();
} }
void ToolForm::on_powerBtn_clicked() void ToolForm::on_powerBtn_clicked()
{ {
if (!m_device) { auto device = qsc::IDeviceManage::getInstance().getDevice(m_serial);
if (!device) {
return; return;
} }
emit m_device->postPower(); emit device->postPower();
} }
void ToolForm::on_screenShotBtn_clicked() void ToolForm::on_screenShotBtn_clicked()
{ {
if (!m_device) { auto device = qsc::IDeviceManage::getInstance().getDevice(m_serial);
if (!device) {
return; return;
} }
m_device->screenshot(); device->screenshot();
} }
void ToolForm::on_volumeUpBtn_clicked() void ToolForm::on_volumeUpBtn_clicked()
{ {
if (!m_device) { auto device = qsc::IDeviceManage::getInstance().getDevice(m_serial);
if (!device) {
return; return;
} }
emit m_device->postVolumeUp(); emit device->postVolumeUp();
} }
void ToolForm::on_volumeDownBtn_clicked() void ToolForm::on_volumeDownBtn_clicked()
{ {
if (!m_device) { auto device = qsc::IDeviceManage::getInstance().getDevice(m_serial);
if (!device) {
return; return;
} }
emit m_device->postVolumeDown(); emit device->postVolumeDown();
} }
void ToolForm::on_closeScreenBtn_clicked() void ToolForm::on_closeScreenBtn_clicked()
{ {
if (!m_device) { auto device = qsc::IDeviceManage::getInstance().getDevice(m_serial);
if (!device) {
return; return;
} }
emit m_device->setScreenPowerMode(ControlMsg::SPM_OFF); emit device->setScreenPowerMode(false);
} }
void ToolForm::on_expandNotifyBtn_clicked() void ToolForm::on_expandNotifyBtn_clicked()
{ {
if (!m_device) { auto device = qsc::IDeviceManage::getInstance().getDevice(m_serial);
if (!device) {
return; return;
} }
emit m_device->expandNotificationPanel(); emit device->expandNotificationPanel();
} }
void ToolForm::on_touchBtn_clicked() void ToolForm::on_touchBtn_clicked()
{ {
if (!m_device) { auto device = qsc::IDeviceManage::getInstance().getDevice(m_serial);
if (!device) {
return; return;
} }
m_showTouch = !m_showTouch; m_showTouch = !m_showTouch;
m_device->showTouch(m_showTouch); device->showTouch(m_showTouch);
} }
void ToolForm::on_groupControlBtn_clicked() void ToolForm::on_groupControlBtn_clicked()
{ {
if (!m_device) {
return;
}
} }
void ToolForm::on_openScreenBtn_clicked() void ToolForm::on_openScreenBtn_clicked()
{ {
if (!m_device) { auto device = qsc::IDeviceManage::getInstance().getDevice(m_serial);
if (!device) {
return; return;
} }
emit m_device->setScreenPowerMode(ControlMsg::SPM_NORMAL); emit device->setScreenPowerMode(true);
} }

View file

@ -4,7 +4,7 @@
#include <QPointer> #include <QPointer>
#include <QWidget> #include <QWidget>
#include "device.h" #include "../include/QtScrcpyCore.h"
#include "magneticwidget.h" #include "magneticwidget.h"
namespace Ui namespace Ui
@ -21,7 +21,7 @@ public:
explicit ToolForm(QWidget *adsorbWidget, AdsorbPositions adsorbPos); explicit ToolForm(QWidget *adsorbWidget, AdsorbPositions adsorbPos);
~ToolForm(); ~ToolForm();
void setDevice(Device *device); void setSerial(const QString& serial);
protected: protected:
void mousePressEvent(QMouseEvent *event); void mousePressEvent(QMouseEvent *event);
@ -54,7 +54,7 @@ private:
private: private:
Ui::ToolForm *ui; Ui::ToolForm *ui;
QPoint m_dragPosition; QPoint m_dragPosition;
QPointer<Device> m_device; QString m_serial;
bool m_showTouch = false; bool m_showTouch = false;
}; };

View file

@ -15,10 +15,10 @@
#include "config.h" #include "config.h"
#include "controller.h" #include "controller.h"
#include "device.h"
#include "iconhelper.h" #include "iconhelper.h"
#include "qyuvopenglwidget.h" #include "qyuvopenglwidget.h"
#include "toolform.h" #include "toolform.h"
#include "mousetap/mousetap.h"
#include "ui_videoform.h" #include "ui_videoform.h"
#include "videoform.h" #include "videoform.h"
extern "C" extern "C"
@ -161,11 +161,16 @@ void VideoForm::updateRender(int width, int height, uint8_t* dataY, uint8_t* dat
m_videoWidget->updateTextures(dataY, dataU, dataV, linesizeY, linesizeU, linesizeV); m_videoWidget->updateTextures(dataY, dataU, dataV, linesizeY, linesizeU, linesizeV);
} }
void VideoForm::setSerial(const QString &serial)
{
m_serial = serial;
}
void VideoForm::showToolForm(bool show) void VideoForm::showToolForm(bool show)
{ {
if (!m_toolForm) { if (!m_toolForm) {
m_toolForm = new ToolForm(this, ToolForm::AP_OUTSIDE_RIGHT); m_toolForm = new ToolForm(this, ToolForm::AP_OUTSIDE_RIGHT);
m_toolForm->setDevice(m_device); m_toolForm->setSerial(m_serial);
} }
m_toolForm->move(pos().x() + geometry().width(), pos().y() + 30); m_toolForm->move(pos().x() + geometry().width(), pos().y() + 30);
m_toolForm->setVisible(show); m_toolForm->setVisible(show);
@ -190,7 +195,8 @@ void VideoForm::installShortcut()
shortcut = new QShortcut(QKeySequence("Ctrl+f"), this); shortcut = new QShortcut(QKeySequence("Ctrl+f"), this);
shortcut->setAutoRepeat(false); shortcut->setAutoRepeat(false);
connect(shortcut, &QShortcut::activated, this, [this]() { connect(shortcut, &QShortcut::activated, this, [this]() {
if (!m_device) { auto device = qsc::IDeviceManage::getInstance().getDevice(m_serial);
if (!device) {
return; return;
} }
switchFullScreen(); switchFullScreen();
@ -210,138 +216,152 @@ void VideoForm::installShortcut()
shortcut = new QShortcut(QKeySequence("Ctrl+h"), this); shortcut = new QShortcut(QKeySequence("Ctrl+h"), this);
shortcut->setAutoRepeat(false); shortcut->setAutoRepeat(false);
connect(shortcut, &QShortcut::activated, this, [this]() { connect(shortcut, &QShortcut::activated, this, [this]() {
if (!m_device) { auto device = qsc::IDeviceManage::getInstance().getDevice(m_serial);
if (!device) {
return; return;
} }
m_device->postGoHome(); device->postGoHome();
}); });
// postGoBack // postGoBack
shortcut = new QShortcut(QKeySequence("Ctrl+b"), this); shortcut = new QShortcut(QKeySequence("Ctrl+b"), this);
shortcut->setAutoRepeat(false); shortcut->setAutoRepeat(false);
connect(shortcut, &QShortcut::activated, this, [this]() { connect(shortcut, &QShortcut::activated, this, [this]() {
if (!m_device) { auto device = qsc::IDeviceManage::getInstance().getDevice(m_serial);
if (!device) {
return; return;
} }
m_device->postGoBack(); device->postGoBack();
}); });
// postAppSwitch // postAppSwitch
shortcut = new QShortcut(QKeySequence("Ctrl+s"), this); shortcut = new QShortcut(QKeySequence("Ctrl+s"), this);
shortcut->setAutoRepeat(false); shortcut->setAutoRepeat(false);
connect(shortcut, &QShortcut::activated, this, [this]() { connect(shortcut, &QShortcut::activated, this, [this]() {
if (!m_device) { auto device = qsc::IDeviceManage::getInstance().getDevice(m_serial);
if (!device) {
return; return;
} }
emit m_device->postAppSwitch(); emit device->postAppSwitch();
}); });
// postGoMenu // postGoMenu
shortcut = new QShortcut(QKeySequence("Ctrl+m"), this); shortcut = new QShortcut(QKeySequence("Ctrl+m"), this);
shortcut->setAutoRepeat(false); shortcut->setAutoRepeat(false);
connect(shortcut, &QShortcut::activated, this, [this]() { connect(shortcut, &QShortcut::activated, this, [this]() {
if (!m_device) { auto device = qsc::IDeviceManage::getInstance().getDevice(m_serial);
if (!device) {
return; return;
} }
m_device->postGoMenu(); device->postGoMenu();
}); });
// postVolumeUp // postVolumeUp
shortcut = new QShortcut(QKeySequence("Ctrl+up"), this); shortcut = new QShortcut(QKeySequence("Ctrl+up"), this);
connect(shortcut, &QShortcut::activated, this, [this]() { connect(shortcut, &QShortcut::activated, this, [this]() {
if (!m_device) { auto device = qsc::IDeviceManage::getInstance().getDevice(m_serial);
if (!device) {
return; return;
} }
emit m_device->postVolumeUp(); emit device->postVolumeUp();
}); });
// postVolumeDown // postVolumeDown
shortcut = new QShortcut(QKeySequence("Ctrl+down"), this); shortcut = new QShortcut(QKeySequence("Ctrl+down"), this);
connect(shortcut, &QShortcut::activated, this, [this]() { connect(shortcut, &QShortcut::activated, this, [this]() {
if (!m_device) { auto device = qsc::IDeviceManage::getInstance().getDevice(m_serial);
if (!device) {
return; return;
} }
emit m_device->postVolumeDown(); emit device->postVolumeDown();
}); });
// postPower // postPower
shortcut = new QShortcut(QKeySequence("Ctrl+p"), this); shortcut = new QShortcut(QKeySequence("Ctrl+p"), this);
shortcut->setAutoRepeat(false); shortcut->setAutoRepeat(false);
connect(shortcut, &QShortcut::activated, this, [this]() { connect(shortcut, &QShortcut::activated, this, [this]() {
if (!m_device) { auto device = qsc::IDeviceManage::getInstance().getDevice(m_serial);
if (!device) {
return; return;
} }
emit m_device->postPower(); emit device->postPower();
}); });
// setScreenPowerMode(ControlMsg::SPM_OFF) // setScreenPowerMode(ControlMsg::SPM_OFF)
shortcut = new QShortcut(QKeySequence("Ctrl+o"), this); shortcut = new QShortcut(QKeySequence("Ctrl+o"), this);
shortcut->setAutoRepeat(false); shortcut->setAutoRepeat(false);
connect(shortcut, &QShortcut::activated, this, [this]() { connect(shortcut, &QShortcut::activated, this, [this]() {
if (!m_device) { auto device = qsc::IDeviceManage::getInstance().getDevice(m_serial);
if (!device) {
return; return;
} }
emit m_device->setScreenPowerMode(ControlMsg::SPM_OFF); emit device->setScreenPowerMode(ControlMsg::SPM_OFF);
}); });
// expandNotificationPanel // expandNotificationPanel
shortcut = new QShortcut(QKeySequence("Ctrl+n"), this); shortcut = new QShortcut(QKeySequence("Ctrl+n"), this);
shortcut->setAutoRepeat(false); shortcut->setAutoRepeat(false);
connect(shortcut, &QShortcut::activated, this, [this]() { connect(shortcut, &QShortcut::activated, this, [this]() {
if (!m_device) { auto device = qsc::IDeviceManage::getInstance().getDevice(m_serial);
if (!device) {
return; return;
} }
emit m_device->expandNotificationPanel(); emit device->expandNotificationPanel();
}); });
// collapsePanel // collapsePanel
shortcut = new QShortcut(QKeySequence("Ctrl+Shift+n"), this); shortcut = new QShortcut(QKeySequence("Ctrl+Shift+n"), this);
shortcut->setAutoRepeat(false); shortcut->setAutoRepeat(false);
connect(shortcut, &QShortcut::activated, this, [this]() { connect(shortcut, &QShortcut::activated, this, [this]() {
if (!m_device) { auto device = qsc::IDeviceManage::getInstance().getDevice(m_serial);
if (!device) {
return; return;
} }
emit m_device->collapsePanel(); emit device->collapsePanel();
}); });
// copy // copy
shortcut = new QShortcut(QKeySequence("Ctrl+c"), this); shortcut = new QShortcut(QKeySequence("Ctrl+c"), this);
shortcut->setAutoRepeat(false); shortcut->setAutoRepeat(false);
connect(shortcut, &QShortcut::activated, this, [this]() { connect(shortcut, &QShortcut::activated, this, [this]() {
if (!m_device) { auto device = qsc::IDeviceManage::getInstance().getDevice(m_serial);
if (!device) {
return; return;
} }
emit m_device->postCopy(); emit device->postCopy();
}); });
// cut // cut
shortcut = new QShortcut(QKeySequence("Ctrl+x"), this); shortcut = new QShortcut(QKeySequence("Ctrl+x"), this);
shortcut->setAutoRepeat(false); shortcut->setAutoRepeat(false);
connect(shortcut, &QShortcut::activated, this, [this]() { connect(shortcut, &QShortcut::activated, this, [this]() {
if (!m_device) { auto device = qsc::IDeviceManage::getInstance().getDevice(m_serial);
if (!device) {
return; return;
} }
emit m_device->postCut(); emit device->postCut();
}); });
// clipboardPaste // clipboardPaste
shortcut = new QShortcut(QKeySequence("Ctrl+v"), this); shortcut = new QShortcut(QKeySequence("Ctrl+v"), this);
shortcut->setAutoRepeat(false); shortcut->setAutoRepeat(false);
connect(shortcut, &QShortcut::activated, this, [this]() { connect(shortcut, &QShortcut::activated, this, [this]() {
if (!m_device) { auto device = qsc::IDeviceManage::getInstance().getDevice(m_serial);
if (!device) {
return; return;
} }
emit m_device->setDeviceClipboard(); emit device->setDeviceClipboard();
}); });
// setDeviceClipboard // setDeviceClipboard
shortcut = new QShortcut(QKeySequence("Ctrl+Shift+v"), this); shortcut = new QShortcut(QKeySequence("Ctrl+Shift+v"), this);
shortcut->setAutoRepeat(false); shortcut->setAutoRepeat(false);
connect(shortcut, &QShortcut::activated, this, [this]() { connect(shortcut, &QShortcut::activated, this, [this]() {
if (!m_device) { auto device = qsc::IDeviceManage::getInstance().getDevice(m_serial);
if (!device) {
return; return;
} }
emit m_device->clipboardPaste(); emit device->clipboardPaste();
}); });
} }
@ -420,7 +440,7 @@ void VideoForm::updateShowSize(const QSize &newSize)
showSize.setHeight(showSize.width() / m_widthHeightRatio); showSize.setHeight(showSize.width() / m_widthHeightRatio);
} }
if (isFullScreen() && m_device) { if (isFullScreen() && qsc::IDeviceManage::getInstance().getDevice(m_serial)) {
switchFullScreen(); switchFullScreen();
} }
@ -501,6 +521,17 @@ void VideoForm::updateFPS(quint32 fps)
m_fpsLabel->setText(QString("FPS:%1").arg(fps)); m_fpsLabel->setText(QString("FPS:%1").arg(fps));
} }
void VideoForm::grabCursor(bool grab)
{
QRect rc = getGrabCursorRect();
MouseTap::getInstance()->enableMouseEventTap(rc, grab);
}
void VideoForm::onFrame(int width, int height, uint8_t *dataY, uint8_t *dataU, uint8_t *dataV, int linesizeY, int linesizeU, int linesizeV)
{
updateRender(width, height, dataY, dataU, dataV, linesizeY, linesizeU, linesizeV);
}
void VideoForm::staysOnTop(bool top) void VideoForm::staysOnTop(bool top)
{ {
bool needShow = false; bool needShow = false;
@ -516,33 +547,29 @@ void VideoForm::staysOnTop(bool top)
} }
} }
void VideoForm::setDevice(Device *device)
{
m_device = device;
}
void VideoForm::mousePressEvent(QMouseEvent *event) void VideoForm::mousePressEvent(QMouseEvent *event)
{ {
auto device = qsc::IDeviceManage::getInstance().getDevice(m_serial);
if (event->button() == Qt::MiddleButton) { if (event->button() == Qt::MiddleButton) {
if (m_device && !m_device->isCurrentCustomKeymap()) { if (device && !device->isCurrentCustomKeymap()) {
m_device->postGoHome(); device->postGoHome();
return; return;
} }
} }
if (event->button() == Qt::RightButton) { if (event->button() == Qt::RightButton) {
if (m_device && !m_device->isCurrentCustomKeymap()) { if (device && !device->isCurrentCustomKeymap()) {
m_device->postGoBack(); device->postGoBack();
return; return;
} }
} }
if (m_videoWidget->geometry().contains(event->pos())) { if (m_videoWidget->geometry().contains(event->pos())) {
if (!m_device) { if (!device) {
return; return;
} }
event->setLocalPos(m_videoWidget->mapFrom(this, event->localPos().toPoint())); event->setLocalPos(m_videoWidget->mapFrom(this, event->localPos().toPoint()));
emit m_device->mouseEvent(event, m_videoWidget->frameSize(), m_videoWidget->size()); emit device->mouseEvent(event, m_videoWidget->frameSize(), m_videoWidget->size());
// debug keymap pos // debug keymap pos
if (event->button() == Qt::LeftButton) { if (event->button() == Qt::LeftButton) {
@ -561,8 +588,9 @@ void VideoForm::mousePressEvent(QMouseEvent *event)
void VideoForm::mouseReleaseEvent(QMouseEvent *event) void VideoForm::mouseReleaseEvent(QMouseEvent *event)
{ {
auto device = qsc::IDeviceManage::getInstance().getDevice(m_serial);
if (m_dragPosition.isNull()) { if (m_dragPosition.isNull()) {
if (!m_device) { if (!device) {
return; return;
} }
event->setLocalPos(m_videoWidget->mapFrom(this, event->localPos().toPoint())); event->setLocalPos(m_videoWidget->mapFrom(this, event->localPos().toPoint()));
@ -581,7 +609,7 @@ void VideoForm::mouseReleaseEvent(QMouseEvent *event)
local.setY(m_videoWidget->height()); local.setY(m_videoWidget->height());
} }
event->setLocalPos(local); event->setLocalPos(local);
emit m_device->mouseEvent(event, m_videoWidget->frameSize(), m_videoWidget->size()); emit device->mouseEvent(event, m_videoWidget->frameSize(), m_videoWidget->size());
} else { } else {
m_dragPosition = QPoint(0, 0); m_dragPosition = QPoint(0, 0);
} }
@ -589,12 +617,13 @@ void VideoForm::mouseReleaseEvent(QMouseEvent *event)
void VideoForm::mouseMoveEvent(QMouseEvent *event) void VideoForm::mouseMoveEvent(QMouseEvent *event)
{ {
auto device = qsc::IDeviceManage::getInstance().getDevice(m_serial);
if (m_videoWidget->geometry().contains(event->pos())) { if (m_videoWidget->geometry().contains(event->pos())) {
if (!m_device) { if (!device) {
return; return;
} }
event->setLocalPos(m_videoWidget->mapFrom(this, event->localPos().toPoint())); event->setLocalPos(m_videoWidget->mapFrom(this, event->localPos().toPoint()));
emit m_device->mouseEvent(event, m_videoWidget->frameSize(), m_videoWidget->size()); emit device->mouseEvent(event, m_videoWidget->frameSize(), m_videoWidget->size());
} else if (!m_dragPosition.isNull()) { } else if (!m_dragPosition.isNull()) {
if (event->buttons() & Qt::LeftButton) { if (event->buttons() & Qt::LeftButton) {
move(event->globalPos() - m_dragPosition); move(event->globalPos() - m_dragPosition);
@ -605,30 +634,32 @@ void VideoForm::mouseMoveEvent(QMouseEvent *event)
void VideoForm::mouseDoubleClickEvent(QMouseEvent *event) void VideoForm::mouseDoubleClickEvent(QMouseEvent *event)
{ {
auto device = qsc::IDeviceManage::getInstance().getDevice(m_serial);
if (event->button() == Qt::LeftButton && !m_videoWidget->geometry().contains(event->pos())) { if (event->button() == Qt::LeftButton && !m_videoWidget->geometry().contains(event->pos())) {
if (!isMaximized()) { if (!isMaximized()) {
removeBlackRect(); removeBlackRect();
} }
} }
if (event->button() == Qt::RightButton && m_device && !m_device->isCurrentCustomKeymap()) { if (event->button() == Qt::RightButton && device && !device->isCurrentCustomKeymap()) {
emit m_device->postBackOrScreenOn(event->type() == QEvent::MouseButtonPress); emit device->postBackOrScreenOn(event->type() == QEvent::MouseButtonPress);
} }
if (m_videoWidget->geometry().contains(event->pos())) { if (m_videoWidget->geometry().contains(event->pos())) {
if (!m_device) { if (!device) {
return; return;
} }
event->setLocalPos(m_videoWidget->mapFrom(this, event->localPos().toPoint())); event->setLocalPos(m_videoWidget->mapFrom(this, event->localPos().toPoint()));
emit m_device->mouseEvent(event, m_videoWidget->frameSize(), m_videoWidget->size()); emit device->mouseEvent(event, m_videoWidget->frameSize(), m_videoWidget->size());
} }
} }
void VideoForm::wheelEvent(QWheelEvent *event) void VideoForm::wheelEvent(QWheelEvent *event)
{ {
auto device = qsc::IDeviceManage::getInstance().getDevice(m_serial);
#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0) #if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
if (m_videoWidget->geometry().contains(event->position().toPoint())) { if (m_videoWidget->geometry().contains(event->position().toPoint())) {
if (!m_device) { if (!device) {
return; return;
} }
QPointF pos = m_videoWidget->mapFrom(this, event->position().toPoint()); QPointF pos = m_videoWidget->mapFrom(this, event->position().toPoint());
@ -636,7 +667,7 @@ void VideoForm::wheelEvent(QWheelEvent *event)
pos, event->globalPosition(), event->pixelDelta(), event->angleDelta(), event->buttons(), event->modifiers(), event->phase(), event->inverted()); pos, event->globalPosition(), event->pixelDelta(), event->angleDelta(), event->buttons(), event->modifiers(), event->phase(), event->inverted());
#else #else
if (m_videoWidget->geometry().contains(event->pos())) { if (m_videoWidget->geometry().contains(event->pos())) {
if (!m_device) { if (!device) {
return; return;
} }
QPointF pos = m_videoWidget->mapFrom(this, event->pos()); QPointF pos = m_videoWidget->mapFrom(this, event->pos());
@ -645,28 +676,30 @@ void VideoForm::wheelEvent(QWheelEvent *event)
pos, event->globalPosF(), event->pixelDelta(), event->angleDelta(), event->delta(), event->orientation(), pos, event->globalPosF(), event->pixelDelta(), event->angleDelta(), event->delta(), event->orientation(),
event->buttons(), event->modifiers(), event->phase(), event->source(), event->inverted()); event->buttons(), event->modifiers(), event->phase(), event->source(), event->inverted());
#endif #endif
emit m_device->wheelEvent(&wheelEvent, m_videoWidget->frameSize(), m_videoWidget->size()); emit device->wheelEvent(&wheelEvent, m_videoWidget->frameSize(), m_videoWidget->size());
} }
} }
void VideoForm::keyPressEvent(QKeyEvent *event) void VideoForm::keyPressEvent(QKeyEvent *event)
{ {
if (!m_device) { auto device = qsc::IDeviceManage::getInstance().getDevice(m_serial);
if (!device) {
return; return;
} }
if (Qt::Key_Escape == event->key() && !event->isAutoRepeat() && isFullScreen()) { if (Qt::Key_Escape == event->key() && !event->isAutoRepeat() && isFullScreen()) {
switchFullScreen(); switchFullScreen();
} }
emit m_device->keyEvent(event, m_videoWidget->frameSize(), m_videoWidget->size()); emit device->keyEvent(event, m_videoWidget->frameSize(), m_videoWidget->size());
} }
void VideoForm::keyReleaseEvent(QKeyEvent *event) void VideoForm::keyReleaseEvent(QKeyEvent *event)
{ {
if (!m_device) { auto device = qsc::IDeviceManage::getInstance().getDevice(m_serial);
if (!device) {
return; return;
} }
emit m_device->keyEvent(event, m_videoWidget->frameSize(), m_videoWidget->size()); emit device->keyEvent(event, m_videoWidget->frameSize(), m_videoWidget->size());
} }
void VideoForm::paintEvent(QPaintEvent *paint) void VideoForm::paintEvent(QPaintEvent *paint)
@ -715,11 +748,12 @@ void VideoForm::resizeEvent(QResizeEvent *event)
void VideoForm::closeEvent(QCloseEvent *event) void VideoForm::closeEvent(QCloseEvent *event)
{ {
Q_UNUSED(event) Q_UNUSED(event)
if (!m_device) { auto device = qsc::IDeviceManage::getInstance().getDevice(m_serial);
if (!device) {
return; return;
} }
Config::getInstance().setRect(m_device->getSerial(), geometry()); Config::getInstance().setRect(device->getSerial(), geometry());
m_device->disconnectDevice(); device->disconnectDevice();
} }
void VideoForm::dragEnterEvent(QDragEnterEvent *event) void VideoForm::dragEnterEvent(QDragEnterEvent *event)
@ -739,7 +773,8 @@ void VideoForm::dragLeaveEvent(QDragLeaveEvent *event)
void VideoForm::dropEvent(QDropEvent *event) void VideoForm::dropEvent(QDropEvent *event)
{ {
if (!m_device) { auto device = qsc::IDeviceManage::getInstance().getDevice(m_serial);
if (!device) {
return; return;
} }
const QMimeData *qm = event->mimeData(); const QMimeData *qm = event->mimeData();
@ -755,9 +790,9 @@ void VideoForm::dropEvent(QDropEvent *event)
} }
if (fileInfo.isFile() && fileInfo.suffix() == "apk") { if (fileInfo.isFile() && fileInfo.suffix() == "apk") {
emit m_device->installApkRequest(file); emit device->installApkRequest(file);
continue; continue;
} }
emit m_device->pushFileRequest(file, Config::getInstance().getPushFilePath() + fileInfo.fileName()); emit device->pushFileRequest(file, Config::getInstance().getPushFilePath() + fileInfo.fileName());
} }
} }

View file

@ -4,17 +4,18 @@
#include <QPointer> #include <QPointer>
#include <QWidget> #include <QWidget>
#include "../include/QtScrcpyCore.h"
namespace Ui namespace Ui
{ {
class videoForm; class videoForm;
} }
class ToolForm; class ToolForm;
class Device;
class FileHandler; class FileHandler;
class QYUVOpenGLWidget; class QYUVOpenGLWidget;
class QLabel; class QLabel;
class VideoForm : public QWidget class VideoForm : public QWidget, public qsc::DeviceObserver
{ {
Q_OBJECT Q_OBJECT
public: public:
@ -24,7 +25,7 @@ public:
void staysOnTop(bool top = true); void staysOnTop(bool top = true);
void updateShowSize(const QSize &newSize); void updateShowSize(const QSize &newSize);
void updateRender(int width, int height, uint8_t* dataY, uint8_t* dataU, uint8_t* dataV, int linesizeY, int linesizeU, int linesizeV); void updateRender(int width, int height, uint8_t* dataY, uint8_t* dataU, uint8_t* dataV, int linesizeY, int linesizeU, int linesizeV);
void setDevice(Device *device); void setSerial(const QString& serial);
QRect getGrabCursorRect(); QRect getGrabCursorRect();
const QSize &frameSize(); const QSize &frameSize();
void resizeSquare(); void resizeSquare();
@ -32,10 +33,12 @@ public:
void showFPS(bool show); void showFPS(bool show);
void switchFullScreen(); void switchFullScreen();
public slots:
void updateFPS(quint32 fps);
private: private:
void onFrame(int width, int height, uint8_t* dataY, uint8_t* dataU, uint8_t* dataV,
int linesizeY, int linesizeU, int linesizeV) override;
void updateFPS(quint32 fps) override;
void grabCursor(bool grab) override;
void updateStyleSheet(bool vertical); void updateStyleSheet(bool vertical);
QMargins getMargins(bool vertical); QMargins getMargins(bool vertical);
void initUI(); void initUI();
@ -78,9 +81,7 @@ private:
float m_widthHeightRatio = 0.5f; float m_widthHeightRatio = 0.5f;
bool m_skin = true; bool m_skin = true;
QPoint m_fullScreenBeforePos; QPoint m_fullScreenBeforePos;
QString m_serial;
//outside member
QPointer<Device> m_device;
}; };
#endif // VIDEOFORM_H #endif // VIDEOFORM_H