diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 10850ad0c7..4f3c5e546a 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -257,6 +257,10 @@ extern "C" { } #endif +#ifdef Q_OS_MAC +#include "MacHelper.h" +#endif + #if defined(Q_OS_ANDROID) #include #include "AndroidHelper.h" @@ -960,6 +964,9 @@ bool setupEssentials(int& argc, char** argv, bool runningMarkerExisted) { DependencyManager::set(); DependencyManager::set(); DependencyManager::set(); +#ifdef Q_OS_MAC + DependencyManager::set(); +#endif QString setBookmarkValue = getCmdOption(argc, constArgv, "--setBookmark"); if (!setBookmarkValue.isEmpty()) { @@ -2856,6 +2863,9 @@ Application::~Application() { _gameWorkload.shutdown(); DependencyManager::destroy(); +#ifdef Q_OS_MAC + DependencyManager::destroy(); +#endif _entityClipboard->eraseAllOctreeElements(); _entityClipboard.reset(); diff --git a/interface/src/MacHelper.cpp b/interface/src/MacHelper.cpp new file mode 100755 index 0000000000..8527f02918 --- /dev/null +++ b/interface/src/MacHelper.cpp @@ -0,0 +1,58 @@ +// +// MacHelper.h +// interface/src +// +// Created by Howard Stearns +// Copyright 2019 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#include "InterfaceLogging.h" +#include "MacHelper.h" +#include + +#ifdef Q_OS_MAC +#include +#include + +// The type definitions in these variables come from IOKit, which includes a definition of Duration that conflicts with ours. +// So... we include these definitions here rather than in the .h, as the .h is included in Application.cpp which +// uses Duration. +static io_connect_t root_port; +static IONotificationPortRef notifyPortRef; +static io_object_t notifierObject; +static void* refCon; + +static void sleepHandler(void* refCon, io_service_t service, natural_t messageType, void* messageArgument) { + if (messageType == kIOMessageSystemHasPoweredOn) { + qCInfo(interfaceapp) << "Waking up from sleep or hybernation."; + QMetaObject::invokeMethod(DependencyManager::get().data(), "noteAwakening", Qt::QueuedConnection); + } +} +#endif + +MacHelper::MacHelper() { +#ifdef Q_OS_MAC + root_port = IORegisterForSystemPower(refCon, ¬ifyPortRef, sleepHandler, ¬ifierObject); + if (root_port == 0) { + qCWarning(interfaceapp) << "IORegisterForSystemPower failed"; + return; + } + CFRunLoopAddSource(CFRunLoopGetCurrent(), + IONotificationPortGetRunLoopSource(notifyPortRef), + kCFRunLoopCommonModes); +#endif +} + +MacHelper::~MacHelper() { +#ifdef Q_OS_MAC + CFRunLoopRemoveSource(CFRunLoopGetCurrent(), + IONotificationPortGetRunLoopSource(notifyPortRef), + kCFRunLoopCommonModes); + IODeregisterForSystemPower(¬ifierObject); + IOServiceClose(root_port); + IONotificationPortDestroy(notifyPortRef); +#endif +} diff --git a/interface/src/MacHelper.h b/interface/src/MacHelper.h new file mode 100755 index 0000000000..52ad4d3e55 --- /dev/null +++ b/interface/src/MacHelper.h @@ -0,0 +1,21 @@ +// +// MacHelper.h +// interface/src +// +// Created by Howard Stearns +// Copyright 2019 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#pragma once + +#include "DependencyManager.h" + +class MacHelper : public Dependency { +public: + MacHelper(); + ~MacHelper(); +}; +