From 80ce63ef8a4dac26e6206c199b9f3ea15b1539e3 Mon Sep 17 00:00:00 2001 From: Martin Zikmund Date: Tue, 14 Apr 2020 21:00:36 +0200 Subject: [PATCH] feat(DisplayRequest): macOS support --- .../Windows.System.Display/DisplayRequest.cs | 8 ++-- src/Uno.UWP/Helpers/IOKit.macOS.cs | 39 +++++++++++++++++++ src/Uno.UWP/System/Display/DisplayRequest.cs | 2 +- .../System/Display/DisplayRequest.macOS.cs | 21 ++++++++++ 4 files changed, 65 insertions(+), 5 deletions(-) create mode 100644 src/Uno.UWP/Helpers/IOKit.macOS.cs create mode 100644 src/Uno.UWP/System/Display/DisplayRequest.macOS.cs diff --git a/src/Uno.UWP/Generated/3.0.0.0/Windows.System.Display/DisplayRequest.cs b/src/Uno.UWP/Generated/3.0.0.0/Windows.System.Display/DisplayRequest.cs index bc2433248e20..06b7e744d892 100644 --- a/src/Uno.UWP/Generated/3.0.0.0/Windows.System.Display/DisplayRequest.cs +++ b/src/Uno.UWP/Generated/3.0.0.0/Windows.System.Display/DisplayRequest.cs @@ -2,12 +2,12 @@ #pragma warning disable 114 // new keyword hiding namespace Windows.System.Display { - #if false || false || NET461 || __WASM__ || __MACOS__ + #if false || false || NET461 || __WASM__ || false [global::Uno.NotImplemented] #endif public partial class DisplayRequest { - #if false || false || NET461 || __WASM__ || __MACOS__ + #if false || false || NET461 || __WASM__ || false [global::Uno.NotImplemented] public DisplayRequest() { @@ -15,14 +15,14 @@ public DisplayRequest() } #endif // Forced skipping of method Windows.System.Display.DisplayRequest.DisplayRequest() - #if false || false || NET461 || __WASM__ || __MACOS__ + #if false || false || NET461 || __WASM__ || false [global::Uno.NotImplemented] public void RequestActive() { global::Windows.Foundation.Metadata.ApiInformation.TryRaiseNotImplemented("Windows.System.Display.DisplayRequest", "void DisplayRequest.RequestActive()"); } #endif - #if false || false || NET461 || __WASM__ || __MACOS__ + #if false || false || NET461 || __WASM__ || false [global::Uno.NotImplemented] public void RequestRelease() { diff --git a/src/Uno.UWP/Helpers/IOKit.macOS.cs b/src/Uno.UWP/Helpers/IOKit.macOS.cs new file mode 100644 index 000000000000..47b9c1da805c --- /dev/null +++ b/src/Uno.UWP/Helpers/IOKit.macOS.cs @@ -0,0 +1,39 @@ +using System; +using System.Runtime.InteropServices; +using CoreFoundation; + +namespace Uno.Helpers +{ + internal static class IOKit + { + private const string IOKitLibrary = "/System/Library/Frameworks/IOKit.framework/IOKit"; + private const uint kIOPMAssertionLevelOn = 255; + + private static readonly CFString kIOPMAssertionTypePreventUserIdleDisplaySleep = "PreventUserIdleDisplaySleep"; + + [DllImport(IOKitLibrary)] + static extern uint IOPMAssertionCreateWithName(IntPtr type, uint level, IntPtr name, out uint id); + + [DllImport(IOKitLibrary)] + static extern uint IOPMAssertionRelease(uint id); + + internal static bool PreventUserIdleDisplaySleep(CFString name, out uint id) + { + var result = IOPMAssertionCreateWithName( + kIOPMAssertionTypePreventUserIdleDisplaySleep.Handle, + kIOPMAssertionLevelOn, + name.Handle, + out var newId); + + id = result == 0 ? newId : 0; + + return result == 0; + } + + internal static bool AllowUserIdleDisplaySleep(uint id) + { + var result = IOPMAssertionRelease(id); + return result == 0; + } + } +} diff --git a/src/Uno.UWP/System/Display/DisplayRequest.cs b/src/Uno.UWP/System/Display/DisplayRequest.cs index a84e5611093d..508059456d68 100644 --- a/src/Uno.UWP/System/Display/DisplayRequest.cs +++ b/src/Uno.UWP/System/Display/DisplayRequest.cs @@ -1,4 +1,4 @@ -#if __ANDROID__ || __IOS__ +#if __ANDROID__ || __IOS__ || __MACOS__ using System; using System.Collections.Generic; using System.Text; diff --git a/src/Uno.UWP/System/Display/DisplayRequest.macOS.cs b/src/Uno.UWP/System/Display/DisplayRequest.macOS.cs new file mode 100644 index 000000000000..63ca2e6b7a32 --- /dev/null +++ b/src/Uno.UWP/System/Display/DisplayRequest.macOS.cs @@ -0,0 +1,21 @@ +#if __MACOS__ +using Uno.Helpers; + +namespace Windows.System.Display +{ + public partial class DisplayRequest + { + private uint _displayRequestId; + + partial void ActivateScreenLock() + { + IOKit.PreventUserIdleDisplaySleep("DisplayRequest", out _displayRequestId); + } + + partial void DeactivateScreenLock() + { + IOKit.AllowUserIdleDisplaySleep(_displayRequestId); + } + } +} +#endif \ No newline at end of file