Skip to content

Commit

Permalink
Merge pull request #2988 from MartinZikmund/dev/mazi/displayrequest-m…
Browse files Browse the repository at this point in the history
…acos

feat(DisplayRequest): macOS support
  • Loading branch information
jeromelaban authored Apr 15, 2020
2 parents c3508a0 + 80ce63e commit 69943cf
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,27 @@
#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()
{
global::Windows.Foundation.Metadata.ApiInformation.TryRaiseNotImplemented("Windows.System.Display.DisplayRequest", "DisplayRequest.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()
{
Expand Down
39 changes: 39 additions & 0 deletions src/Uno.UWP/Helpers/IOKit.macOS.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
2 changes: 1 addition & 1 deletion src/Uno.UWP/System/Display/DisplayRequest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#if __ANDROID__ || __IOS__
#if __ANDROID__ || __IOS__ || __MACOS__
using System;
using System.Collections.Generic;
using System.Text;
Expand Down
21 changes: 21 additions & 0 deletions src/Uno.UWP/System/Display/DisplayRequest.macOS.cs
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 69943cf

Please sign in to comment.