Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(DisplayRequest): macOS support #2988

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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