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

[CoreGraphics] Bind the CGSession API. #21024

Merged
merged 2 commits into from
Aug 14, 2024
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
58 changes: 58 additions & 0 deletions src/CoreGraphics/CGSession.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#if NET
#if __MACOS__ || __MACCATALYST__

using System.Runtime.InteropServices;

using Foundation;
using ObjCRuntime;

namespace CoreGraphics {

[SupportedOSPlatform ("maccatalyst")]
[SupportedOSPlatform ("macos")]
[UnsupportedOSPlatform ("ios")]
[UnsupportedOSPlatform ("tvos")]
public static class CGSession {
[DllImport (Constants.CoreGraphicsLibrary)]
extern static /* CFDictionaryRef __nullable */ IntPtr CGSessionCopyCurrentDictionary ();

/// <summary>Get the properties for the current window server session.</summary>
/// <returns>The properties for the current window server session.</returns>
/// <remarks>Returns null if the caller is not running in a Quartz GUI session or the window server is disabled.</remarks>
public static CGSessionProperties? GetProperties ()
{
var handle = CGSessionCopyCurrentDictionary ();
if (handle == IntPtr.Zero)
return null;
var dict = Runtime.GetNSObject<NSDictionary> (handle, owns: true);
return new CGSessionProperties (dict);
}
}

// This looks like it should be in an api definition bound using [Field] attributes,
// but these aren't actual native fields, in the headers they're declared as constants:
// #define kCGSession*Key @"SomeStringValue"
[SupportedOSPlatform ("maccatalyst")]
[SupportedOSPlatform ("macos")]
[UnsupportedOSPlatform ("ios")]
[UnsupportedOSPlatform ("tvos")]
public static class CGSessionKeys {
/// <summary>A key for the user ID for the session's current user.</summary>
public static NSString UserIdKey { get => (NSString) "kCGSSessionUserIDKey"; }

/// <summary>A key for the short user name for the session's current user.</summary>
public static NSString UserNameKey { get => (NSString) "kCGSSessionUserNameKey"; }

/// <summary>A key for the set of hardware composing a console for the session's current user.</summary>
public static NSString ConsoleSetKey { get => (NSString) "kCGSSessionConsoleSetKey"; }

/// <summary>A key for indicating whether the session's on a console.</summary>
public static NSString OnConsoleKey { get => (NSString) "kCGSSessionOnConsoleKey"; }

/// <summary>A key for indicating whether the session's login operation has been don</summary>
public static NSString LoginDoneKey { get => (NSString) "kCGSessionLoginDoneKey"; }
}
}

#endif // __MACOS__ || __MACCATALYST__
#endif // NET
12 changes: 12 additions & 0 deletions src/coregraphics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -504,4 +504,16 @@ interface CGDisplayStreamYCbCrMatrixOptionKeys {
[Field ("kCGDisplayStreamYCbCrMatrix_SMPTE_240M_1995")]
NSString Smpte_240M_1995 { get; }
}

#if NET
[NoiOS, NoTV, NoWatch, MacCatalyst (13, 1)]
[StrongDictionary ("CGSessionKeys")]
interface CGSessionProperties {
uint UserId { get; }
string UserName { get; }
uint ConsoleSet { get; }
bool OnConsole { get; }
bool LoginDone { get; }
}
#endif
}
1 change: 1 addition & 0 deletions src/frameworks.sources
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,7 @@ COREGRAPHICS_SOURCES = \
CoreGraphics/CGPDFStream.cs \
CoreGraphics/CGPDFString.cs \
CoreGraphics/CGPdfTagType.cs \
CoreGraphics/CGSession.cs \
CoreGraphics/CGShading.cs \
CoreGraphics/NativeDrawingMethods.cs \

Expand Down
10 changes: 10 additions & 0 deletions tests/cecil-tests/Documentation.KnownFailures.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30269,6 +30269,8 @@ M:CoreGraphics.CGRectExtensions.IsInfinite(CoreGraphics.CGRect)
M:CoreGraphics.CGRectExtensions.IsNull(CoreGraphics.CGRect)
M:CoreGraphics.CGRectExtensions.Standardize(CoreGraphics.CGRect)
M:CoreGraphics.CGRectExtensions.UnionWith(CoreGraphics.CGRect,CoreGraphics.CGRect)
M:CoreGraphics.CGSessionProperties.#ctor
M:CoreGraphics.CGSessionProperties.#ctor(Foundation.NSDictionary)
M:CoreGraphics.CGShading.CreateAxial(CoreGraphics.CGColorSpace,CoreGraphics.CGPoint,CoreGraphics.CGPoint,CoreGraphics.CGFunction,System.Boolean,System.Boolean)
M:CoreGraphics.CGShading.CreateRadial(CoreGraphics.CGColorSpace,CoreGraphics.CGPoint,System.Runtime.InteropServices.NFloat,CoreGraphics.CGPoint,System.Runtime.InteropServices.NFloat,CoreGraphics.CGFunction,System.Boolean,System.Boolean)
M:CoreGraphics.CGShading.Release
Expand Down Expand Up @@ -59868,6 +59870,11 @@ P:CoreGraphics.CGRect.Top
P:CoreGraphics.CGRect.Width
P:CoreGraphics.CGRect.X
P:CoreGraphics.CGRect.Y
P:CoreGraphics.CGSessionProperties.ConsoleSet
P:CoreGraphics.CGSessionProperties.LoginDone
P:CoreGraphics.CGSessionProperties.OnConsole
P:CoreGraphics.CGSessionProperties.UserId
P:CoreGraphics.CGSessionProperties.UserName
P:CoreGraphics.CGSize.Height
P:CoreGraphics.CGSize.IsEmpty
P:CoreGraphics.CGSize.Width
Expand Down Expand Up @@ -78125,6 +78132,9 @@ T:CoreGraphics.CGRect
T:CoreGraphics.CGRectEdge
T:CoreGraphics.CGRectExtensions
T:CoreGraphics.CGScrollEventUnit
T:CoreGraphics.CGSession
T:CoreGraphics.CGSessionKeys
T:CoreGraphics.CGSessionProperties
T:CoreGraphics.CGShading
T:CoreGraphics.CGSize
T:CoreGraphics.CGVector
Expand Down
15 changes: 15 additions & 0 deletions tests/monotouch-test/CoreVideo/CVDisplayLinkTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,17 @@ namespace MonoTouchFixtures.CoreVideo {
[Preserve (AllMembers = true)]
public class CVDisplayLinkTest {

void IgnoreIfLockedScreen ()
{
#if NET
var props = CGSession.GetProperties ();
var value = props.Dictionary [(NSString) "CGSSessionScreenIsLocked"]; // This key isn't documented, so no binding for it.
var isLocked = (value as NSNumber)?.BoolValue;
if (isLocked == true)
Assert.Ignore ("The screen is locked.");
#endif
}

[Test]
public void CreateFromDisplayIdValidIdTest ()
{
Expand Down Expand Up @@ -61,6 +72,7 @@ public void CreateFromOpenGLMaskTest ()
public void DefaultConstructorTest ()
{
TestRuntime.AssertNotVSTS ();
IgnoreIfLockedScreen ();
Assert.DoesNotThrow (() => {
using var displayLink = new CVDisplayLink ();
});
Expand All @@ -70,6 +82,7 @@ public void DefaultConstructorTest ()
public void SetCurrentDisplayOpenGLTest ()
{
TestRuntime.AssertNotVSTS ();
IgnoreIfLockedScreen ();
Assert.DoesNotThrow (() => {
using var displayLink = new CVDisplayLink ();
displayLink.SetCurrentDisplay (CGDisplay.MainDisplayID);
Expand All @@ -80,6 +93,7 @@ public void SetCurrentDisplayOpenGLTest ()
public void GetCurrentDisplayTest ()
{
TestRuntime.AssertNotVSTS ();
IgnoreIfLockedScreen ();
Assert.DoesNotThrow (() => {
using var displayLink = new CVDisplayLink ();
Assert.AreEqual (CGDisplay.MainDisplayID, displayLink.GetCurrentDisplay ());
Expand All @@ -100,6 +114,7 @@ public void TryTranslateTimeValidTest ()
{
TestRuntime.AssertNotVSTS ();
TestRuntime.AssertSystemVersion (ApplePlatform.MacOSX, 12, 0);
IgnoreIfLockedScreen ();
var outTime = new CVTimeStamp {
Version = 0,
Flags = (1L << 0) | (1L << 1), // kCVTimeStampVideoTimeValid | kCVTimeStampHostTimeValid
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@
!missing-pinvoke! CGReleaseDisplayFadeReservation is not bound
!missing-pinvoke! CGRequestScreenCaptureAccess is not bound
!missing-pinvoke! CGRestorePermanentDisplayConfiguration is not bound
!missing-pinvoke! CGSessionCopyCurrentDictionary is not bound
!missing-pinvoke! CGSetDisplayTransferByByteTable is not bound
!missing-pinvoke! CGSetDisplayTransferByTable is not bound
!missing-pinvoke! CGWarpMouseCursorPosition is not bound
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@
!missing-pinvoke! CGPSConverterIsConverting is not bound
!missing-pinvoke! CGReleaseDisplayFadeReservation is not bound
!missing-pinvoke! CGRestorePermanentDisplayConfiguration is not bound
!missing-pinvoke! CGSessionCopyCurrentDictionary is not bound
!missing-pinvoke! CGSetDisplayTransferByByteTable is not bound
!missing-pinvoke! CGSetDisplayTransferByTable is not bound
!missing-pinvoke! CGWarpMouseCursorPosition is not bound
Expand Down