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

[MediaAccessibility] Make P/Invokes have blittable signatures. #20120

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
17 changes: 12 additions & 5 deletions src/MediaAccessibility/MAImageCaptioning.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,24 @@ public static partial class MAImageCaptioning {

[DllImport (Constants.MediaAccessibilityLibrary)]
// __attribute__((cf_returns_retained))
static extern /* CFStringRef _Nullable */ IntPtr MAImageCaptioningCopyCaption (/* CFURLRef _Nonnull */ IntPtr url, /* CFErrorRef _Nullable * */ out IntPtr error);
unsafe static extern /* CFStringRef _Nullable */ IntPtr MAImageCaptioningCopyCaption (/* CFURLRef _Nonnull */ IntPtr url, /* CFErrorRef _Nullable * */ IntPtr* error);

static public string? GetCaption (NSUrl url, out NSError? error)
{
if (url is null)
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (url));

var result = MAImageCaptioningCopyCaption (url.Handle, out var e);
IntPtr result;
IntPtr e;
unsafe {
result = MAImageCaptioningCopyCaption (url.Handle, &e);
}
error = e == IntPtr.Zero ? null : new NSError (e);
return CFString.FromHandle (result, releaseHandle: true);
}

[DllImport (Constants.MediaAccessibilityLibrary)]
[return: MarshalAs (UnmanagedType.I1)]
static extern bool MAImageCaptioningSetCaption (/* CFURLRef _Nonnull */ IntPtr url, /* CFStringRef _Nullable */ IntPtr @string, /* CFErrorRef _Nullable * */ out IntPtr error);
unsafe static extern byte MAImageCaptioningSetCaption (/* CFURLRef _Nonnull */ IntPtr url, /* CFStringRef _Nullable */ IntPtr @string, /* CFErrorRef _Nullable * */ IntPtr* error);

static public bool SetCaption (NSUrl url, string @string, out NSError? error)
{
Expand All @@ -47,7 +50,11 @@ static public bool SetCaption (NSUrl url, string @string, out NSError? error)

var s = CFString.CreateNative (@string);
try {
var result = MAImageCaptioningSetCaption (url.Handle, s, out var e);
bool result;
IntPtr e;
unsafe {
result = MAImageCaptioningSetCaption (url.Handle, s, &e) != 0;
}
error = e == IntPtr.Zero ? null : new NSError (e);
return result;
} finally {
Expand Down
97 changes: 65 additions & 32 deletions src/MediaAccessibility/MediaAccessibility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,14 @@ static MACaptionAppearance ()
#endif

[DllImport (Constants.MediaAccessibilityLibrary)]
[return: MarshalAs (UnmanagedType.I1)]
static extern bool MACaptionAppearanceAddSelectedLanguage (nint domain,
static extern byte MACaptionAppearanceAddSelectedLanguage (nint domain,
/* CFStringRef __nonnull */ IntPtr language);

public static bool AddSelectedLanguage (MACaptionAppearanceDomain domain, string language)
{
// this will throw an ANE if language is null
using (var lang = new CFString (language)) {
return MACaptionAppearanceAddSelectedLanguage ((int) domain, lang.Handle);
return MACaptionAppearanceAddSelectedLanguage ((int) domain, lang.Handle) != 0;
}
}

Expand Down Expand Up @@ -107,120 +106,154 @@ public static NSString [] GetPreferredCaptioningMediaCharacteristics (MACaptionA
}

[DllImport (Constants.MediaAccessibilityLibrary)]
static extern /* CGColorRef __nonnull */ IntPtr MACaptionAppearanceCopyForegroundColor (nint domain,
/* MACaptionAppearanceBehavior * __nullable */ ref nint behavior);
unsafe static extern /* CGColorRef __nonnull */ IntPtr MACaptionAppearanceCopyForegroundColor (nint domain,
/* MACaptionAppearanceBehavior * __nullable */ nint* behavior);

public static CGColor GetForegroundColor (MACaptionAppearanceDomain domain, ref MACaptionAppearanceBehavior behavior)
{
nint b = (int) behavior;
var rv = new CGColor (MACaptionAppearanceCopyForegroundColor ((int) domain, ref b), owns: true);
IntPtr handle;
unsafe {
handle = MACaptionAppearanceCopyForegroundColor ((int) domain, &b);
}
var rv = new CGColor (handle, owns: true);
behavior = (MACaptionAppearanceBehavior) (int) b;
return rv;
}

[DllImport (Constants.MediaAccessibilityLibrary)]
static extern /* CGColorRef __nonnull */ IntPtr MACaptionAppearanceCopyBackgroundColor (nint domain,
/* MACaptionAppearanceBehavior * __nullable */ ref nint behavior);
unsafe static extern /* CGColorRef __nonnull */ IntPtr MACaptionAppearanceCopyBackgroundColor (nint domain,
/* MACaptionAppearanceBehavior * __nullable */ nint* behavior);

public static CGColor GetBackgroundColor (MACaptionAppearanceDomain domain, ref MACaptionAppearanceBehavior behavior)
{
nint b = (int) behavior;
var rv = new CGColor (MACaptionAppearanceCopyBackgroundColor ((int) domain, ref b), owns: true);
IntPtr handle;
unsafe {
handle = MACaptionAppearanceCopyBackgroundColor ((int) domain, &b);
}
var rv = new CGColor (handle, owns: true);
behavior = (MACaptionAppearanceBehavior) (int) b;
return rv;
}

[DllImport (Constants.MediaAccessibilityLibrary)]
static extern /* CGColorRef __nonnull */ IntPtr MACaptionAppearanceCopyWindowColor (nint domain,
/* MACaptionAppearanceBehavior * __nullable */ ref nint behavior);
unsafe static extern /* CGColorRef __nonnull */ IntPtr MACaptionAppearanceCopyWindowColor (nint domain,
/* MACaptionAppearanceBehavior * __nullable */ nint* behavior);

public static CGColor GetWindowColor (MACaptionAppearanceDomain domain, ref MACaptionAppearanceBehavior behavior)
{
nint b = (int) behavior;
var rv = new CGColor (MACaptionAppearanceCopyWindowColor ((int) domain, ref b), owns: true);
IntPtr handle;
unsafe {
handle = MACaptionAppearanceCopyWindowColor ((int) domain, &b);
}
var rv = new CGColor (handle, owns: true);
behavior = (MACaptionAppearanceBehavior) (int) b;
return rv;
}

[DllImport (Constants.MediaAccessibilityLibrary)]
static extern nfloat MACaptionAppearanceGetForegroundOpacity (nint domain, ref nint behavior);
unsafe static extern nfloat MACaptionAppearanceGetForegroundOpacity (nint domain, nint* behavior);

public static nfloat GetForegroundOpacity (MACaptionAppearanceDomain domain, ref MACaptionAppearanceBehavior behavior)
{
nint b = (int) behavior;
var rv = MACaptionAppearanceGetForegroundOpacity ((int) domain, ref b);
nfloat rv;
unsafe {
rv = MACaptionAppearanceGetForegroundOpacity ((int) domain, &b);
}
behavior = (MACaptionAppearanceBehavior) (int) b;
return rv;
}

[DllImport (Constants.MediaAccessibilityLibrary)]
static extern nfloat MACaptionAppearanceGetBackgroundOpacity (nint domain,
/* MACaptionAppearanceBehavior * __nullable */ ref nint behavior);
unsafe static extern nfloat MACaptionAppearanceGetBackgroundOpacity (nint domain,
/* MACaptionAppearanceBehavior * __nullable */ nint* behavior);

public static nfloat GetBackgroundOpacity (MACaptionAppearanceDomain domain, ref MACaptionAppearanceBehavior behavior)
{
nint b = (int) behavior;
var rv = MACaptionAppearanceGetBackgroundOpacity ((int) domain, ref b);
nfloat rv;
unsafe {
rv = MACaptionAppearanceGetBackgroundOpacity ((int) domain, &b);
}
behavior = (MACaptionAppearanceBehavior) (int) b;
return rv;
}

[DllImport (Constants.MediaAccessibilityLibrary)]
static extern nfloat MACaptionAppearanceGetWindowOpacity (nint domain,
/* MACaptionAppearanceBehavior * __nullable */ ref nint behavior);
unsafe static extern nfloat MACaptionAppearanceGetWindowOpacity (nint domain,
/* MACaptionAppearanceBehavior * __nullable */ nint* behavior);

public static nfloat GetWindowOpacity (MACaptionAppearanceDomain domain, ref MACaptionAppearanceBehavior behavior)
{
nint b = (int) behavior;
var rv = MACaptionAppearanceGetWindowOpacity ((int) domain, ref b);
nfloat rv;
unsafe {
rv = MACaptionAppearanceGetWindowOpacity ((int) domain, &b);
}
behavior = (MACaptionAppearanceBehavior) (int) b;
return rv;
}

[DllImport (Constants.MediaAccessibilityLibrary)]
static extern nfloat MACaptionAppearanceGetWindowRoundedCornerRadius (nint domain,
/* MACaptionAppearanceBehavior * __nullable */ ref nint behavior);
unsafe static extern nfloat MACaptionAppearanceGetWindowRoundedCornerRadius (nint domain,
/* MACaptionAppearanceBehavior * __nullable */ nint* behavior);

public static nfloat GetWindowRoundedCornerRadius (MACaptionAppearanceDomain domain, ref MACaptionAppearanceBehavior behavior)
{
nint b = (int) behavior;
var rv = MACaptionAppearanceGetWindowRoundedCornerRadius ((int) domain, ref b);
nfloat rv;
unsafe {
rv = MACaptionAppearanceGetWindowRoundedCornerRadius ((int) domain, &b);
}
behavior = (MACaptionAppearanceBehavior) (int) b;
return rv;
}

[DllImport (Constants.MediaAccessibilityLibrary)]
static extern /* CTFontDescriptorRef __nonnull */ IntPtr MACaptionAppearanceCopyFontDescriptorForStyle (nint domain,
/* MACaptionAppearanceBehavior * __nullable */ ref nint behavior, nint fontStyle);
unsafe static extern /* CTFontDescriptorRef __nonnull */ IntPtr MACaptionAppearanceCopyFontDescriptorForStyle (nint domain,
/* MACaptionAppearanceBehavior * __nullable */ nint* behavior, nint fontStyle);

public static CTFontDescriptor GetFontDescriptor (MACaptionAppearanceDomain domain, ref MACaptionAppearanceBehavior behavior, MACaptionAppearanceFontStyle fontStyle)
{
nint b = (int) behavior;
var rv = new CTFontDescriptor (MACaptionAppearanceCopyFontDescriptorForStyle ((int) domain, ref b, (int) fontStyle), owns: true);
IntPtr handle;
unsafe {
handle = MACaptionAppearanceCopyFontDescriptorForStyle ((int) domain, &b, (int) fontStyle);
}
var rv = new CTFontDescriptor (handle, owns: true);
behavior = (MACaptionAppearanceBehavior) (int) b;
return rv;
}

[DllImport (Constants.MediaAccessibilityLibrary)]
static extern nfloat MACaptionAppearanceGetRelativeCharacterSize (nint domain,
/* MACaptionAppearanceBehavior * __nullable */ ref nint behavior);
unsafe static extern nfloat MACaptionAppearanceGetRelativeCharacterSize (nint domain,
/* MACaptionAppearanceBehavior * __nullable */ nint* behavior);

public static nfloat GetRelativeCharacterSize (MACaptionAppearanceDomain domain, ref MACaptionAppearanceBehavior behavior)
{
nint b = (int) behavior;
var rv = MACaptionAppearanceGetRelativeCharacterSize ((int) domain, ref b);
nfloat rv;
unsafe {
rv = MACaptionAppearanceGetRelativeCharacterSize ((int) domain, &b);
}
behavior = (MACaptionAppearanceBehavior) (int) b;
return rv;
}

[DllImport (Constants.MediaAccessibilityLibrary)]
static extern nint MACaptionAppearanceGetTextEdgeStyle (nint domain,
/* MACaptionAppearanceBehavior * __nullable */ ref nint behavior);
unsafe static extern nint MACaptionAppearanceGetTextEdgeStyle (nint domain,
/* MACaptionAppearanceBehavior * __nullable */ nint* behavior);

public static MACaptionAppearanceTextEdgeStyle GetTextEdgeStyle (MACaptionAppearanceDomain domain, ref MACaptionAppearanceBehavior behavior)
{
nint b = (int) behavior;
var rv = MACaptionAppearanceGetTextEdgeStyle ((int) domain, ref b);
nint rv;
unsafe {
rv = MACaptionAppearanceGetTextEdgeStyle ((int) domain, &b);
}
behavior = (MACaptionAppearanceBehavior) (int) b;
return (MACaptionAppearanceTextEdgeStyle) (int) rv;
}
Expand Down
13 changes: 0 additions & 13 deletions tests/cecil-tests/BlittablePInvokes.KnownFailures.cs
Original file line number Diff line number Diff line change
Expand Up @@ -329,8 +329,6 @@ public partial class BlittablePInvokes {
"System.Boolean GameController.GCGamepadSnapshot::GCGamepadSnapShotDataV100FromNSData(GameController.GCGamepadSnapShotDataV100&,System.IntPtr)",
"System.Boolean GameController.GCMicroGamepadSnapshot::GCMicroGamepadSnapshotDataFromNSData(GameController.GCMicroGamepadSnapshotData&,System.IntPtr)",
"System.Boolean GameController.GCMicroGamepadSnapshot::GCMicroGamepadSnapShotDataV100FromNSData(GameController.GCMicroGamepadSnapShotDataV100&,System.IntPtr)",
"System.Boolean MediaAccessibility.MACaptionAppearance::MACaptionAppearanceAddSelectedLanguage(System.IntPtr,System.IntPtr)",
"System.Boolean MediaAccessibility.MAImageCaptioning::MAImageCaptioningSetCaption(System.IntPtr,System.IntPtr,System.IntPtr&)",
"System.Boolean Network.NWAdvertiseDescriptor::nw_advertise_descriptor_get_no_auto_rename(System.IntPtr)",
"System.Boolean Network.NWBrowserDescriptor::nw_browse_descriptor_get_include_txt_record(System.IntPtr)",
"System.Boolean Network.NWConnectionGroup::nw_connection_group_reinsert_extracted_connection(System.IntPtr,System.IntPtr)",
Expand Down Expand Up @@ -502,12 +500,6 @@ public partial class BlittablePInvokes {
"System.IntPtr GameController.GCGamepadSnapShotDataV100::NSDataFromGCGamepadSnapShotDataV100(GameController.GCGamepadSnapShotDataV100&)",
"System.IntPtr GameController.GCMicroGamepadSnapshotData::NSDataFromGCMicroGamepadSnapshotData(GameController.GCMicroGamepadSnapshotData&)",
"System.IntPtr GameController.GCMicroGamepadSnapShotDataV100::NSDataFromGCMicroGamepadSnapShotDataV100(GameController.GCMicroGamepadSnapShotDataV100&)",
"System.IntPtr MediaAccessibility.MACaptionAppearance::MACaptionAppearanceCopyBackgroundColor(System.IntPtr,System.IntPtr&)",
"System.IntPtr MediaAccessibility.MACaptionAppearance::MACaptionAppearanceCopyFontDescriptorForStyle(System.IntPtr,System.IntPtr&,System.IntPtr)",
"System.IntPtr MediaAccessibility.MACaptionAppearance::MACaptionAppearanceCopyForegroundColor(System.IntPtr,System.IntPtr&)",
"System.IntPtr MediaAccessibility.MACaptionAppearance::MACaptionAppearanceCopyWindowColor(System.IntPtr,System.IntPtr&)",
"System.IntPtr MediaAccessibility.MACaptionAppearance::MACaptionAppearanceGetTextEdgeStyle(System.IntPtr,System.IntPtr&)",
"System.IntPtr MediaAccessibility.MAImageCaptioning::MAImageCaptioningCopyCaption(System.IntPtr,System.IntPtr&)",
"System.IntPtr ModelIO.MDLVertexDescriptor::MTKModelIOVertexDescriptorFromMetalWithError(System.IntPtr,System.IntPtr&)",
"System.IntPtr ObjCRuntime.Selector::GetHandle(System.String)",
"System.IntPtr SearchKit.SKIndex::SKIndexOpenWithURL(System.IntPtr,System.IntPtr,System.Boolean)",
Expand All @@ -525,11 +517,6 @@ public partial class BlittablePInvokes {
"System.IntPtr SystemConfiguration.NetworkReachability::SCNetworkReachabilityCreateWithAddressPair(System.IntPtr,System.IntPtr,SystemConfiguration.NetworkReachability/sockaddr_in&)",
"System.IntPtr SystemConfiguration.NetworkReachability::SCNetworkReachabilityCreateWithAddressPair(System.IntPtr,SystemConfiguration.NetworkReachability/sockaddr_in&,System.IntPtr)",
"System.IntPtr SystemConfiguration.NetworkReachability::SCNetworkReachabilityCreateWithAddressPair(System.IntPtr,SystemConfiguration.NetworkReachability/sockaddr_in&,SystemConfiguration.NetworkReachability/sockaddr_in&)",
"System.Runtime.InteropServices.NFloat MediaAccessibility.MACaptionAppearance::MACaptionAppearanceGetBackgroundOpacity(System.IntPtr,System.IntPtr&)",
"System.Runtime.InteropServices.NFloat MediaAccessibility.MACaptionAppearance::MACaptionAppearanceGetForegroundOpacity(System.IntPtr,System.IntPtr&)",
"System.Runtime.InteropServices.NFloat MediaAccessibility.MACaptionAppearance::MACaptionAppearanceGetRelativeCharacterSize(System.IntPtr,System.IntPtr&)",
"System.Runtime.InteropServices.NFloat MediaAccessibility.MACaptionAppearance::MACaptionAppearanceGetWindowOpacity(System.IntPtr,System.IntPtr&)",
"System.Runtime.InteropServices.NFloat MediaAccessibility.MACaptionAppearance::MACaptionAppearanceGetWindowRoundedCornerRadius(System.IntPtr,System.IntPtr&)",
"System.Void CoreFoundation.CFMessagePort::CFMessagePortGetContext(System.IntPtr,CoreFoundation.CFMessagePort/ContextProxy&)",
"System.Void CoreFoundation.CFStream::CFStreamCreateBoundPair(System.IntPtr,System.IntPtr&,System.IntPtr&,System.IntPtr)",
"System.Void CoreFoundation.CFStream::CFStreamCreatePairWithPeerSocketSignature(System.IntPtr,CoreFoundation.CFSocketSignature&,System.IntPtr&,System.IntPtr&)",
Expand Down
Loading