diff --git a/QtScrcpy/device/device.cpp b/QtScrcpy/device/device.cpp index 15d6414..0f027ea 100644 --- a/QtScrcpy/device/device.cpp +++ b/QtScrcpy/device/device.cpp @@ -130,7 +130,7 @@ void Device::initSignals() { connect(this, &Device::screenshot, this, &Device::onScreenshot); connect(this, &Device::showTouch, this, &Device::onShowTouch); - connect(this, &Device::setMainControl, this, &Device::onSetMainControl); + connect(this, &Device::setControlState, this, &Device::onSetControlState); connect(this, &Device::grabCursor, this, &Device::onGrabCursor); if (m_controller) { @@ -185,7 +185,7 @@ void Device::initSignals() tips = tr("%1 failed").arg(tipsType); } qInfo() << tips; - if (!m_mainControl) { + if (m_controlState == GCS_CLIENT) { return; } QMessageBox::information(m_videoForm, "QtScrcpy", tips, QMessageBox::Ok); @@ -280,13 +280,14 @@ void Device::startServer() }); } -void Device::onSetMainControl(Device* device, bool mainControl) +void Device::onSetControlState(Device* device, Device::GroupControlState state) { Q_UNUSED(device) - if (m_mainControl == mainControl) { + if (m_controlState == state) { return; } - m_mainControl = mainControl; + m_controlState = state; + emit controlStateChange(this, m_controlState); } void Device::onGrabCursor(bool grab) @@ -294,13 +295,16 @@ void Device::onGrabCursor(bool grab) if (!m_videoForm) { return; } + if (m_controlState == GCS_CLIENT) { + return; + } QRect rc = m_videoForm->getGrabCursorRect(); MouseTap::getInstance()->enableMouseEventTap(rc, grab); } -bool Device::mainControl() +Device::GroupControlState Device::controlState() { - return m_mainControl; + return m_controlState; } bool Device::saveFrame(const AVFrame* frame) diff --git a/QtScrcpy/device/device.h b/QtScrcpy/device/device.h index 3a28f37..9a43b91 100644 --- a/QtScrcpy/device/device.h +++ b/QtScrcpy/device/device.h @@ -35,6 +35,11 @@ public: QString gameScript = ""; // 游戏映射脚本 bool renderExpiredFrames = false; // 是否渲染延迟视频帧 }; + enum GroupControlState { + GCS_FREE = 0, + GCS_HOST, + GCS_CLIENT, + }; explicit Device(DeviceParams params, QObject *parent = nullptr); virtual ~Device(); @@ -43,7 +48,7 @@ public: const QString &getSerial(); void updateScript(QString script); - bool mainControl(); + Device::GroupControlState controlState(); signals: void deviceDisconnect(QString serial); @@ -75,13 +80,16 @@ signals: // self connect signal and slots void screenshot(); void showTouch(bool show); - void setMainControl(Device* device, bool mainControl); + void setControlState(Device* device, Device::GroupControlState state); void grabCursor(bool grab); + // for notify + void controlStateChange(Device* device, Device::GroupControlState state); + public slots: void onScreenshot(); void onShowTouch(bool show); - void onSetMainControl(Device* device, bool mainControl); + void onSetControlState(Device* device, Device::GroupControlState state); void onGrabCursor(bool grab); private: @@ -105,7 +113,7 @@ private: QTime m_startTimeCount; DeviceParams m_params; - bool m_mainControl = false; + GroupControlState m_controlState = GCS_FREE; }; #endif // DEVICE_H diff --git a/QtScrcpy/device/ui/toolform.cpp b/QtScrcpy/device/ui/toolform.cpp index c21d142..dc7172b 100644 --- a/QtScrcpy/device/ui/toolform.cpp +++ b/QtScrcpy/device/ui/toolform.cpp @@ -26,7 +26,11 @@ ToolForm::~ToolForm() void ToolForm::setDevice(Device *device) { + if (!device) { + return; + } m_device = device; + connect(m_device, &Device::controlStateChange, this, &ToolForm::onControlStateChange); } void ToolForm::initStyle() @@ -52,10 +56,16 @@ void ToolForm::updateGroupControl() if (!m_device) { return; } - if (m_device->mainControl()) { - ui->groupControlBtn->setStyleSheet("color: red"); - } else { + switch (m_device->controlState()) { + case Device::GroupControlState::GCS_FREE: ui->groupControlBtn->setStyleSheet("color: #DCDCDC"); + break; + case Device::GroupControlState::GCS_HOST: + ui->groupControlBtn->setStyleSheet("color: red"); + break; + case Device::GroupControlState::GCS_CLIENT: + ui->groupControlBtn->setStyleSheet("color: green"); + break; } } @@ -196,6 +206,18 @@ void ToolForm::on_groupControlBtn_clicked() if (!m_device) { return; } - emit m_device->setMainControl(m_device, !m_device->mainControl()); + Device::GroupControlState state = m_device->controlState(); + if (state == Device::GroupControlState::GCS_FREE) { + emit m_device->setControlState(m_device, Device::GroupControlState::GCS_HOST); + } + if (state == Device::GroupControlState::GCS_HOST) { + emit m_device->setControlState(m_device, Device::GroupControlState::GCS_FREE); + } +} + +void ToolForm::onControlStateChange(Device *device, Device::GroupControlState state) +{ + Q_UNUSED(device) + Q_UNUSED(state) updateGroupControl(); } diff --git a/QtScrcpy/device/ui/toolform.h b/QtScrcpy/device/ui/toolform.h index 28f38c5..1c4d10b 100644 --- a/QtScrcpy/device/ui/toolform.h +++ b/QtScrcpy/device/ui/toolform.h @@ -5,6 +5,7 @@ #include #include "magneticwidget.h" +#include "device.h" namespace Ui { class ToolForm; @@ -44,6 +45,8 @@ private slots: void on_touchBtn_clicked(); void on_groupControlBtn_clicked(); + void onControlStateChange(Device* device, Device::GroupControlState state); + private: void initStyle(); void updateGroupControl(); diff --git a/QtScrcpy/device/ui/videoform.cpp b/QtScrcpy/device/ui/videoform.cpp index c9d9c40..ea4a2c9 100644 --- a/QtScrcpy/device/ui/videoform.cpp +++ b/QtScrcpy/device/ui/videoform.cpp @@ -184,8 +184,8 @@ void VideoForm::updateShowSize(const QSize &newSize) showSize.setHeight(showSize.width() / m_widthHeightRatio); } - if (isFullScreen()) { - onSwitchFullScreen(); + if (isFullScreen() && m_device) { + emit m_device->switchFullScreen(); } if (m_skin) { QMargins m = getMargins(vertical); @@ -351,13 +351,13 @@ void VideoForm::wheelEvent(QWheelEvent *event) void VideoForm::keyPressEvent(QKeyEvent *event) { + if (!m_device) { + return; + } if (Qt::Key_Escape == event->key() && !event->isAutoRepeat() && isFullScreen()) { - onSwitchFullScreen(); - } - if (!m_device) { - return; + emit m_device->switchFullScreen(); } if (event->key() == Qt::Key_C && (event->modifiers() & Qt::ControlModifier)) { emit m_device->requestDeviceClipboard();