diff --git a/QtScrcpy/device/device.cpp b/QtScrcpy/device/device.cpp index 0f027ea..9dc12eb 100644 --- a/QtScrcpy/device/device.cpp +++ b/QtScrcpy/device/device.cpp @@ -286,8 +286,9 @@ void Device::onSetControlState(Device* device, Device::GroupControlState state) if (m_controlState == state) { return; } + GroupControlState oldState = m_controlState; m_controlState = state; - emit controlStateChange(this, m_controlState); + emit controlStateChange(this, oldState, m_controlState); } void Device::onGrabCursor(bool grab) diff --git a/QtScrcpy/device/device.h b/QtScrcpy/device/device.h index 9a43b91..cc7dd6d 100644 --- a/QtScrcpy/device/device.h +++ b/QtScrcpy/device/device.h @@ -84,7 +84,7 @@ signals: void grabCursor(bool grab); // for notify - void controlStateChange(Device* device, Device::GroupControlState state); + void controlStateChange(Device* device, Device::GroupControlState oldState, Device::GroupControlState newState); public slots: void onScreenshot(); diff --git a/QtScrcpy/device/ui/toolform.cpp b/QtScrcpy/device/ui/toolform.cpp index dc7172b..e640bd4 100644 --- a/QtScrcpy/device/ui/toolform.cpp +++ b/QtScrcpy/device/ui/toolform.cpp @@ -215,9 +215,10 @@ void ToolForm::on_groupControlBtn_clicked() } } -void ToolForm::onControlStateChange(Device *device, Device::GroupControlState state) +void ToolForm::onControlStateChange(Device *device, Device::GroupControlState oldState, Device::GroupControlState newState) { Q_UNUSED(device) - Q_UNUSED(state) + Q_UNUSED(oldState) + Q_UNUSED(newState) updateGroupControl(); } diff --git a/QtScrcpy/device/ui/toolform.h b/QtScrcpy/device/ui/toolform.h index 1c4d10b..037e55c 100644 --- a/QtScrcpy/device/ui/toolform.h +++ b/QtScrcpy/device/ui/toolform.h @@ -45,7 +45,7 @@ private slots: void on_touchBtn_clicked(); void on_groupControlBtn_clicked(); - void onControlStateChange(Device* device, Device::GroupControlState state); + void onControlStateChange(Device* device, Device::GroupControlState oldState, Device::GroupControlState newState); private: void initStyle(); diff --git a/QtScrcpy/devicemanage/devicemanage.cpp b/QtScrcpy/devicemanage/devicemanage.cpp index 8379911..f0d0e44 100644 --- a/QtScrcpy/devicemanage/devicemanage.cpp +++ b/QtScrcpy/devicemanage/devicemanage.cpp @@ -1,4 +1,7 @@ #include +#include +#include +#include #include "devicemanage.h" #include "server.h" @@ -44,6 +47,7 @@ bool DeviceManage::connectDevice(Device::DeviceParams params) */ Device *device = new Device(params); connect(device, &Device::deviceDisconnect, this, &DeviceManage::onDeviceDisconnect); + connect(device, &Device::controlStateChange, this, &DeviceManage::onControlStateChange); m_devices[params.serial] = device; return true; } @@ -102,13 +106,114 @@ void DeviceManage::disconnectAllDevice() } } +void DeviceManage::setGroupControlSignals(Device *host, Device *client, bool install) +{ + if (!host || !client) { + return; + } + if (install) { + connect(host, &Device::postGoBack, client, &Device::postGoBack); + connect(host, &Device::postGoHome, client, &Device::postGoHome); + connect(host, &Device::postGoMenu, client, &Device::postGoMenu); + connect(host, &Device::postAppSwitch, client, &Device::postAppSwitch); + connect(host, &Device::postPower, client, &Device::postPower); + connect(host, &Device::postVolumeUp, client, &Device::postVolumeUp); + connect(host, &Device::postVolumeDown, client, &Device::postVolumeDown); + connect(host, &Device::setScreenPowerMode, client, &Device::setScreenPowerMode); + connect(host, &Device::expandNotificationPanel, client, &Device::expandNotificationPanel); + connect(host, &Device::postTurnOn, client, &Device::postTurnOn); + connect(host, &Device::postTextInput, client, &Device::postTextInput); + connect(host, &Device::setDeviceClipboard, client, &Device::setDeviceClipboard); + connect(host, &Device::clipboardPaste, client, &Device::clipboardPaste); + connect(host, &Device::pushFileRequest, client, &Device::pushFileRequest); + connect(host, &Device::installApkRequest, client, &Device::installApkRequest); + connect(host, &Device::mouseEvent, client, &Device::mouseEvent); + connect(host, &Device::wheelEvent, client, &Device::wheelEvent); + connect(host, &Device::keyEvent, client, &Device::keyEvent); + connect(host, &Device::screenshot, client, &Device::screenshot); + connect(host, &Device::showTouch, client, &Device::showTouch); + // dont connect requestDeviceClipboard + //connect(host, &Device::requestDeviceClipboard, client, &Device::requestDeviceClipboard); + } else { + disconnect(host, &Device::postGoBack, client, &Device::postGoBack); + disconnect(host, &Device::postGoHome, client, &Device::postGoHome); + disconnect(host, &Device::postGoMenu, client, &Device::postGoMenu); + disconnect(host, &Device::postAppSwitch, client, &Device::postAppSwitch); + disconnect(host, &Device::postPower, client, &Device::postPower); + disconnect(host, &Device::postVolumeUp, client, &Device::postVolumeUp); + disconnect(host, &Device::postVolumeDown, client, &Device::postVolumeDown); + disconnect(host, &Device::setScreenPowerMode, client, &Device::setScreenPowerMode); + disconnect(host, &Device::expandNotificationPanel, client, &Device::expandNotificationPanel); + disconnect(host, &Device::postTurnOn, client, &Device::postTurnOn); + disconnect(host, &Device::postTextInput, client, &Device::postTextInput); + disconnect(host, &Device::setDeviceClipboard, client, &Device::setDeviceClipboard); + disconnect(host, &Device::clipboardPaste, client, &Device::clipboardPaste); + disconnect(host, &Device::pushFileRequest, client, &Device::pushFileRequest); + disconnect(host, &Device::installApkRequest, client, &Device::installApkRequest); + disconnect(host, &Device::mouseEvent, client, &Device::mouseEvent); + disconnect(host, &Device::wheelEvent, client, &Device::wheelEvent); + disconnect(host, &Device::keyEvent, client, &Device::keyEvent); + disconnect(host, &Device::screenshot, client, &Device::screenshot); + disconnect(host, &Device::showTouch, client, &Device::showTouch); + } +} + +void DeviceManage::setGroupControlHost(Device *host, bool install) +{ + QMapIterator> i(m_devices); + while (i.hasNext()) { + i.next(); + if (!i.value()) { + continue; + } + if (i.value() == host) { + continue; + } + if (install) { + if (host) { + setGroupControlSignals(host, i.value(), true); + } + emit i.value()->setControlState(i.value(), Device::GroupControlState::GCS_CLIENT); + } else { + if (host) { + setGroupControlSignals(host, i.value(), false); + } + emit i.value()->setControlState(i.value(), Device::GroupControlState::GCS_FREE); + } + } +} + void DeviceManage::onDeviceDisconnect(QString serial) { if (!serial.isEmpty() && m_devices.contains(serial)) { + if (m_devices[serial]->controlState() == Device::GroupControlState::GCS_HOST) { + setGroupControlHost(nullptr, false); + } m_devices.remove(serial); } } +void DeviceManage::onControlStateChange(Device *device, Device::GroupControlState oldState, Device::GroupControlState newState) +{ + if (!device) { + return; + } + // free to host + if (oldState == Device::GroupControlState::GCS_FREE + && newState == Device::GroupControlState::GCS_HOST) { + // install control signals + setGroupControlHost(device, true); + return; + } + // host to free + if (oldState == Device::GroupControlState::GCS_HOST + && newState == Device::GroupControlState::GCS_FREE) { + // uninstall control signals + setGroupControlHost(device, false); + return; + } +} + quint16 DeviceManage::getFreePort() { quint16 port = m_localPortStart; diff --git a/QtScrcpy/devicemanage/devicemanage.h b/QtScrcpy/devicemanage/devicemanage.h index bfe1f7c..cbef25c 100644 --- a/QtScrcpy/devicemanage/devicemanage.h +++ b/QtScrcpy/devicemanage/devicemanage.h @@ -20,8 +20,13 @@ public: bool disconnectDevice(const QString &serial); void disconnectAllDevice(); +protected: + void setGroupControlSignals(Device *host, Device *client, bool install); + void setGroupControlHost(Device *host, bool install); + protected slots: void onDeviceDisconnect(QString serial); + void onControlStateChange(Device* device, Device::GroupControlState oldState, Device::GroupControlState newState); private: quint16 getFreePort();