FIX: support auto-reconnect for MAC power awake

jira: [STUDIO-10820]
Change-Id: I0a7c554dc9eae4aa0f71cd1c3dabacdcd465792f
This commit is contained in:
xin.zhang 2025-04-10 17:20:35 +08:00 committed by lane.wei
parent ef2e8e7e9d
commit 582e377836
2 changed files with 88 additions and 0 deletions

View File

@ -2564,8 +2564,75 @@ void GUI_App::init_single_instance_checker(const std::string &name, const std::s
m_single_instance_checker = std::make_unique<wxSingleInstanceChecker>(boost::nowide::widen(name), boost::nowide::widen(path)); m_single_instance_checker = std::make_unique<wxSingleInstanceChecker>(boost::nowide::widen(name), boost::nowide::widen(path));
} }
#ifdef __APPLE__
void GUI_App::MacPowerCallBack(void* refcon, io_service_t service, natural_t messageType, void * messageArgument)
{
BOOST_LOG_TRIVIAL(info) << "MacPowerCallBack messageType:" << messageType;
DeviceManager* dev_manager = wxGetApp().getDeviceManager();
static std::string last_selected_machine;
if (messageType == kIOMessageSystemWillSleep)
{
if (dev_manager)
{
MachineObject* obj = dev_manager->get_selected_machine();
last_selected_machine = obj ? obj->dev_id : "";
BOOST_LOG_TRIVIAL(info) << "MacPowerCallBack save selected machine:" << last_selected_machine;
}
IOAllowPowerChange(service, (long) messageArgument);
}
else if(messageType == kIOMessageSystemHasPoweredOn)
{
if (dev_manager)
{
MachineObject* obj = dev_manager->get_selected_machine();
if (!obj && !last_selected_machine.empty())
{
dev_manager->set_selected_machine(last_selected_machine);
BOOST_LOG_TRIVIAL(info) << "MacPowerCallBack restore selected machine:" << last_selected_machine;
}
}
};
}
void GUI_App::RegisterMacPowerCallBack()
{
m_mac_io_service = IORegisterForSystemPower(m_mac_refcon, &m_mac_io_notify_port, MacPowerCallBack, &m_mac_io_obj);
if (m_mac_io_service == 0)
{
BOOST_LOG_TRIVIAL(error) << "RegisterMacPowerCallBack failed";
return;
}
CFRunLoopAddSource(CFRunLoopGetCurrent(), IONotificationPortGetRunLoopSource(m_mac_io_notify_port), kCFRunLoopCommonModes);
m_mac_powercallback_registered = true;
BOOST_LOG_TRIVIAL(error) << "RegisterMacPowerCallBack success";
}
void GUI_App::UnRegisterMacPowerCallBack()
{
if (m_mac_powercallback_registered)
{
CFRunLoopRemoveSource(CFRunLoopGetCurrent(), IONotificationPortGetRunLoopSource(m_mac_io_notify_port), kCFRunLoopCommonModes);
IODeregisterForSystemPower(&m_mac_io_obj);
IOServiceClose(m_mac_io_service);
IONotificationPortDestroy(m_mac_io_notify_port);
m_mac_powercallback_registered = false;
BOOST_LOG_TRIVIAL(info) << "UnRegisterMacPowerCallBack";
}
}
#endif
bool GUI_App::OnInit() bool GUI_App::OnInit()
{ {
#ifdef __APPLE__
RegisterMacPowerCallBack();
#endif
try { try {
return on_init_inner(); return on_init_inner();
} catch (const std::exception& e) { } catch (const std::exception& e) {
@ -2577,6 +2644,10 @@ bool GUI_App::OnInit()
int GUI_App::OnExit() int GUI_App::OnExit()
{ {
#ifdef __APPLE__
UnRegisterMacPowerCallBack();
#endif
stop_sync_user_preset(); stop_sync_user_preset();
if (m_device_manager) { if (m_device_manager) {

View File

@ -31,6 +31,11 @@
#include <mutex> #include <mutex>
#include <stack> #include <stack>
#ifdef __APPLE__
#include <IOKit/pwr_mgt/IOPMLib.h>
#include <IOKit/IOMessage.h>
#endif
//#define BBL_HAS_FIRST_PAGE 1 //#define BBL_HAS_FIRST_PAGE 1
#define STUDIO_INACTIVE_TIMEOUT 15*60*1000 #define STUDIO_INACTIVE_TIMEOUT 15*60*1000
#define LOG_FILES_MAX_NUM 30 #define LOG_FILES_MAX_NUM 30
@ -710,6 +715,18 @@ private:
std::string m_open_method; std::string m_open_method;
EPickingEffect m_picking_effect{ EPickingEffect::StencilOutline }; EPickingEffect m_picking_effect{ EPickingEffect::StencilOutline };
ColorRGB m_picking_color{ 1.0f, 1.0f, 1.0f }; ColorRGB m_picking_color{ 1.0f, 1.0f, 1.0f };
#ifdef __APPLE__
void RegisterMacPowerCallBack();
void UnRegisterMacPowerCallBack();
static void MacPowerCallBack(void* refcon, io_service_t service, natural_t messageType, void * messageArgument);
bool m_mac_powercallback_registered = false;
void* m_mac_refcon;
IONotificationPortRef m_mac_io_notify_port;
io_object_t m_mac_io_obj;
io_service_t m_mac_io_service;
#endif
}; };
DECLARE_APP(GUI_App) DECLARE_APP(GUI_App)