1// Copyright 2005-2019 The Mumble Developers. All rights reserved.
2// Use of this source code is governed by a BSD-style license
3// that can be found in the LICENSE file at the root of the
4// Mumble source tree or at <https://www.mumble.info/LICENSE>.
5
6#include "mumble_pch.hpp"
7
8#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1070
9static bool appNapSuppressed = false;
10#endif
11
12void MUSuppressAppNap(bool suppress) {
13#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1070
14	NSProcessInfo *processInfo = [NSProcessInfo processInfo];
15	if (![processInfo respondsToSelector:@selector(disableAutomaticTermination:)]) {
16		return;
17	}
18
19	if (suppress == appNapSuppressed) {
20		qWarning("AppNap: attempt to set AppNap suppression state to %s while already in that state.", suppress ? "true" : "false");
21		return;
22	}
23
24	QString translatedReason = QApplication::tr("Mumble is currently connected to a server");
25	NSString *reason = const_cast<NSString *>(reinterpret_cast<const NSString *>(CFStringCreateWithCharacters(kCFAllocatorDefault, reinterpret_cast<const UniChar *>(translatedReason.unicode()), translatedReason.length())));
26
27	if (suppress) {
28		[[NSProcessInfo processInfo] disableAutomaticTermination:reason];
29		qWarning("AppNap: suppressed with reason: '%s'", qPrintable(translatedReason));
30	} else {
31		[[NSProcessInfo processInfo] enableAutomaticTermination:reason];
32		qWarning("AppNap: re-enabled, was suppressed with reason: '%s'", qPrintable(translatedReason));
33	}
34
35	appNapSuppressed = suppress;
36
37	[reason release];
38#else
39	Q_UNUSED(suppress);
40#endif
41}
42