diff --git a/QtScrcpy/device/device.cpp b/QtScrcpy/device/device.cpp index 0c2fec3..664e295 100644 --- a/QtScrcpy/device/device.cpp +++ b/QtScrcpy/device/device.cpp @@ -257,7 +257,7 @@ void Device::initSignals() emit m_controller->onSetScreenPowerMode(ControlMsg::SPM_OFF); } } else { - disconnectDevice(); + m_server->stop(); } }); connect(m_server, &Server::serverStoped, this, [this]() { diff --git a/QtScrcpy/devicemanage/devicemanage.cpp b/QtScrcpy/devicemanage/devicemanage.cpp index b733c7b..ec3f170 100644 --- a/QtScrcpy/devicemanage/devicemanage.cpp +++ b/QtScrcpy/devicemanage/devicemanage.cpp @@ -40,6 +40,7 @@ bool DeviceManage::connectDevice(Device::DeviceParams params) } */ Device *device = new Device(params); + connect(device, &Device::deviceConnected, this, &DeviceManage::onDeviceConnected); connect(device, &Device::deviceDisconnected, this, &DeviceManage::onDeviceDisconnected); connect(device, &Device::controlStateChange, this, &DeviceManage::onControlStateChange); if (!device->connectDevice()) { @@ -190,15 +191,16 @@ void DeviceManage::setGroupControlHost(Device *host, bool install) } } +void DeviceManage::onDeviceConnected(bool success, const QString &serial, const QString &deviceName, const QSize &size) +{ + if (!success) { + removeDevice(serial); + } +} + void DeviceManage::onDeviceDisconnected(QString serial) { - if (!serial.isEmpty() && m_devices.contains(serial)) { - if (m_devices[serial]->controlState() == Device::GroupControlState::GCS_HOST) { - setGroupControlHost(nullptr, false); - } - m_devices[serial]->deleteLater(); - m_devices.remove(serial); - } + removeDevice(serial); } void DeviceManage::onControlStateChange(Device *device, Device::GroupControlState oldState, Device::GroupControlState newState) @@ -300,3 +302,14 @@ quint16 DeviceManage::getFreePort() } return 0; } + +void DeviceManage::removeDevice(const QString &serial) +{ + if (!serial.isEmpty() && m_devices.contains(serial)) { + if (m_devices[serial]->controlState() == Device::GroupControlState::GCS_HOST) { + setGroupControlHost(nullptr, false); + } + m_devices[serial]->deleteLater(); + m_devices.remove(serial); + } +} diff --git a/QtScrcpy/devicemanage/devicemanage.h b/QtScrcpy/devicemanage/devicemanage.h index 48d4f4b..a26252a 100644 --- a/QtScrcpy/devicemanage/devicemanage.h +++ b/QtScrcpy/devicemanage/devicemanage.h @@ -26,6 +26,7 @@ protected: void setGroupControlHost(Device *host, bool install); protected slots: + void onDeviceConnected(bool success, const QString& serial, const QString& deviceName, const QSize& size); void onDeviceDisconnected(QString serial); void onControlStateChange(Device *device, Device::GroupControlState oldState, Device::GroupControlState newState); @@ -36,6 +37,7 @@ protected slots: private: quint16 getFreePort(); + void removeDevice(const QString& serial); private: QMap> m_devices;