From a0e2c4ac3073e8bf52c05d5b45288b53e1620f5b Mon Sep 17 00:00:00 2001 From: Kateryna Kostiuk <kateryna.kostiuk@savoirfairelinux.com> Date: Fri, 10 Feb 2023 14:01:20 -0500 Subject: [PATCH] macOS: update connections when the system wakes up Sometimes, it is not possible to place a call after the system returns from sleep mode. This patch ensures that the connectivity changed is called when the system returns from sleep mode. Change-Id: Id9f1331b89ae37a1244ea10a8c02282c4c8a35e5 --- src/app/os/macos/connectivitymonitor.mm | 37 ++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/src/app/os/macos/connectivitymonitor.mm b/src/app/os/macos/connectivitymonitor.mm index f2fd6ff72..586a446f2 100644 --- a/src/app/os/macos/connectivitymonitor.mm +++ b/src/app/os/macos/connectivitymonitor.mm @@ -17,14 +17,46 @@ */ #include <CoreFoundation/CoreFoundation.h> +#include <AppKit/AppKit.h> #include <IOKit/pwr_mgt/IOPMLib.h> #include <SystemConfiguration/SystemConfiguration.h> #include "connectivitymonitor.h" #include <QDebug> +typedef void(^SystemMonitorCallback)(void); + +@interface SystemMonitor: NSObject + +-(void)startWithCallBack:(SystemMonitorCallback) callback; +@property (nonatomic, copy) SystemMonitorCallback callback; + +@end + +@implementation SystemMonitor + +-(void)startWithCallBack:(SystemMonitorCallback)callback { + self.callback = callback; + [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver: self + selector: @selector(receiveWakeNotification:) + name: NSWorkspaceDidWakeNotification object: NULL]; +} + +- (void)receiveWakeNotification:(NSNotification*)note +{ + _callback(); +} + +-(void)dealloc { + [_callback release], _callback = nil; + [super dealloc]; +} + +@end + dispatch_queue_t scNetworkQueue; SCNetworkReachabilityRef currentReachability; +SystemMonitor* systemMonitor; static void ReachabilityCallback(SCNetworkReachabilityRef __unused target, SCNetworkConnectionFlags flags, void* info) { @@ -36,6 +68,10 @@ ConnectivityMonitor::ConnectivityMonitor(QObject* parent) : QObject(parent) { scNetworkQueue = dispatch_queue_create("scNetworkReachability", DISPATCH_QUEUE_SERIAL); + systemMonitor = [[SystemMonitor alloc] init]; + [systemMonitor startWithCallBack: ^(void) { + Q_EMIT connectivityChanged(); + }]; SCNetworkReachabilityRef reachabilityRef = NULL; void (^callbackBlock)(SCNetworkReachabilityFlags) = ^(SCNetworkReachabilityFlags flags) { dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ @@ -61,7 +97,6 @@ ConnectivityMonitor::~ConnectivityMonitor() { SCNetworkReachabilitySetCallback(currentReachability, NULL, NULL); currentReachability = NULL; - qDebug() << "Destroying connectivity monitor"; } bool -- GitLab