Skip to content

Commit

Permalink
[GameController] Make P/Invokes have blittable signatures. (#20528)
Browse files Browse the repository at this point in the history
Contributes towards #15684.
  • Loading branch information
rolfbjarne committed Apr 30, 2024
1 parent ac22d6f commit 8bb2048
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 48 deletions.
45 changes: 29 additions & 16 deletions src/GameController/GCExtendedGamepadSnapshot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#nullable enable

using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

using ObjCRuntime;
Expand Down Expand Up @@ -85,13 +86,17 @@ public struct GCExtendedGamepadSnapShotDataV100 {
[Deprecated (PlatformName.TvOS, 13, 0, message: "Use 'GCController.GetExtendedGamepadController()' instead.")]
#endif
[DllImport (Constants.GameControllerLibrary)]
static extern /* NSData * __nullable */ IntPtr NSDataFromGCExtendedGamepadSnapShotDataV100 (
/* GCExtendedGamepadSnapShotDataV100 * __nullable */ ref GCExtendedGamepadSnapShotDataV100 snapshotData);
unsafe static extern /* NSData * __nullable */ IntPtr NSDataFromGCExtendedGamepadSnapShotDataV100 (
/* GCExtendedGamepadSnapShotDataV100 * __nullable */ GCExtendedGamepadSnapShotDataV100* snapshotData);

public NSData? ToNSData ()
{
var p = NSDataFromGCExtendedGamepadSnapShotDataV100 (ref this);
return p == IntPtr.Zero ? null : new NSData (p);
unsafe {
fixed (GCExtendedGamepadSnapShotDataV100* self = &this) {
var p = NSDataFromGCExtendedGamepadSnapShotDataV100 (self);
return p == IntPtr.Zero ? null : new NSData (p);
}
}
}
}

Expand Down Expand Up @@ -202,8 +207,8 @@ public struct GCExtendedGamepadSnapshotData {
[iOS (12, 2)]
#endif
[DllImport (Constants.GameControllerLibrary)]
static extern /* NSData * __nullable */ IntPtr NSDataFromGCExtendedGamepadSnapshotData (
/* GCExtendedGamepadSnapshotData * __nullable */ ref GCExtendedGamepadSnapshotData snapshotData);
unsafe static extern /* NSData * __nullable */ IntPtr NSDataFromGCExtendedGamepadSnapshotData (
/* GCExtendedGamepadSnapshotData * __nullable */ GCExtendedGamepadSnapshotData* snapshotData);

#if NET
[SupportedOSPlatform ("tvos12.2")]
Expand All @@ -220,18 +225,21 @@ public struct GCExtendedGamepadSnapshotData {
#endif
public NSData? ToNSData ()
{
var p = NSDataFromGCExtendedGamepadSnapshotData (ref this);
return p == IntPtr.Zero ? null : new NSData (p);
unsafe {
fixed (GCExtendedGamepadSnapshotData* self = &this) {
var p = NSDataFromGCExtendedGamepadSnapshotData (self);
return p == IntPtr.Zero ? null : new NSData (p);
}
}
}
}

public partial class GCExtendedGamepadSnapshot {

// GCExtendedGamepadSnapshot.h
[DllImport (Constants.GameControllerLibrary)]
[return: MarshalAs (UnmanagedType.I1)]
static extern bool GCExtendedGamepadSnapShotDataV100FromNSData (
/* GCExtendedGamepadSnapShotDataV100 * __nullable */ out GCExtendedGamepadSnapShotDataV100 snapshotData,
unsafe static extern byte GCExtendedGamepadSnapShotDataV100FromNSData (
/* GCExtendedGamepadSnapShotDataV100 * __nullable */ GCExtendedGamepadSnapShotDataV100* snapshotData,
/* NSData * __nullable */ IntPtr data);

#if NET
Expand All @@ -248,14 +256,16 @@ public partial class GCExtendedGamepadSnapshot {
[iOS (12, 2)]
#endif
[DllImport (Constants.GameControllerLibrary)]
[return: MarshalAs (UnmanagedType.I1)]
static extern bool GCExtendedGamepadSnapshotDataFromNSData (
/* GCExtendedGamepadSnapshotData * __nullable */ out GCExtendedGamepadSnapshotData snapshotData,
unsafe static extern byte GCExtendedGamepadSnapshotDataFromNSData (
/* GCExtendedGamepadSnapshotData * __nullable */ GCExtendedGamepadSnapshotData* snapshotData,
/* NSData * __nullable */ IntPtr data);

public static bool TryGetSnapShotData (NSData? data, out GCExtendedGamepadSnapShotDataV100 snapshotData)
{
return GCExtendedGamepadSnapShotDataV100FromNSData (out snapshotData, data.GetHandle ());
snapshotData = default;
unsafe {
return GCExtendedGamepadSnapShotDataV100FromNSData ((GCExtendedGamepadSnapShotDataV100*) Unsafe.AsPointer<GCExtendedGamepadSnapShotDataV100> (ref snapshotData), data.GetHandle ()) != 0;
}
}

#if NET
Expand All @@ -273,7 +283,10 @@ public static bool TryGetSnapShotData (NSData? data, out GCExtendedGamepadSnapSh
#endif
public static bool TryGetExtendedSnapShotData (NSData? data, out GCExtendedGamepadSnapshotData snapshotData)
{
return GCExtendedGamepadSnapshotDataFromNSData (out snapshotData, data.GetHandle ());
snapshotData = default;
unsafe {
return GCExtendedGamepadSnapshotDataFromNSData ((GCExtendedGamepadSnapshotData*) Unsafe.AsPointer<GCExtendedGamepadSnapshotData> (ref snapshotData), data.GetHandle ()) != 0;
}
}
}
}
23 changes: 15 additions & 8 deletions src/GameController/GCGamepadSnapshot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#nullable enable

using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

using ObjCRuntime;
Expand Down Expand Up @@ -51,28 +52,34 @@ public struct GCGamepadSnapShotDataV100 {
public float /* float_t = float */ RightShoulder;

[DllImport (Constants.GameControllerLibrary)]
static extern /* NSData * __nullable */ IntPtr NSDataFromGCGamepadSnapShotDataV100 (
/* GCGamepadSnapShotDataV100 * __nullable */ ref GCGamepadSnapShotDataV100 snapshotData);
unsafe static extern /* NSData * __nullable */ IntPtr NSDataFromGCGamepadSnapShotDataV100 (
/* GCGamepadSnapShotDataV100 * __nullable */ GCGamepadSnapShotDataV100* snapshotData);

public NSData? ToNSData ()
{
var p = NSDataFromGCGamepadSnapShotDataV100 (ref this);
return p == IntPtr.Zero ? null : new NSData (p);
unsafe {
fixed (GCGamepadSnapShotDataV100* self = &this) {
var p = NSDataFromGCGamepadSnapShotDataV100 (self);
return p == IntPtr.Zero ? null : new NSData (p);
}
}
}
}

public partial class GCGamepadSnapshot {

// GCGamepadSnapshot.h
[DllImport (Constants.GameControllerLibrary)]
[return: MarshalAs (UnmanagedType.I1)]
static extern bool GCGamepadSnapShotDataV100FromNSData (
/* GCGamepadSnapShotDataV100 * __nullable */ out GCGamepadSnapShotDataV100 snapshotData,
unsafe static extern byte GCGamepadSnapShotDataV100FromNSData (
/* GCGamepadSnapShotDataV100 * __nullable */ GCGamepadSnapShotDataV100* snapshotData,
/* NSData * __nullable */ IntPtr data);

public static bool TryGetSnapshotData (NSData? data, out GCGamepadSnapShotDataV100 snapshotData)
{
return GCGamepadSnapShotDataV100FromNSData (out snapshotData, data.GetHandle ());
snapshotData = default;
unsafe {
return GCGamepadSnapShotDataV100FromNSData ((GCGamepadSnapShotDataV100*) Unsafe.AsPointer<GCGamepadSnapShotDataV100> (ref snapshotData), data.GetHandle ()) != 0;
}
}
}
}
41 changes: 27 additions & 14 deletions src/GameController/GCMicroGamepadSnapshot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#if !WATCHOS

using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

using ObjCRuntime;
Expand Down Expand Up @@ -57,13 +58,17 @@ public struct GCMicroGamepadSnapShotDataV100 {
[Deprecated (PlatformName.TvOS, 13, 0, message: "Use 'GCController.GetMicroGamepadController()' instead.")]
#endif
[DllImport (Constants.GameControllerLibrary)]
static extern /* NSData * __nullable */ IntPtr NSDataFromGCMicroGamepadSnapShotDataV100 (
/* __nullable */ ref GCMicroGamepadSnapShotDataV100 snapshotData);
unsafe static extern /* NSData * __nullable */ IntPtr NSDataFromGCMicroGamepadSnapShotDataV100 (
/* __nullable */ GCMicroGamepadSnapShotDataV100* snapshotData);

public NSData? ToNSData ()
{
var p = NSDataFromGCMicroGamepadSnapShotDataV100 (ref this);
return p == IntPtr.Zero ? null : new NSData (p);
unsafe {
fixed (GCMicroGamepadSnapShotDataV100* self = &this) {
var p = NSDataFromGCMicroGamepadSnapShotDataV100 (self);
return p == IntPtr.Zero ? null : new NSData (p);
}
}
}
}

Expand Down Expand Up @@ -111,8 +116,8 @@ public struct GCMicroGamepadSnapshotData {
[iOS (12, 2)]
#endif
[DllImport (Constants.GameControllerLibrary)]
static extern /* NSData * __nullable */ IntPtr NSDataFromGCMicroGamepadSnapshotData (
/* __nullable */ ref GCMicroGamepadSnapshotData snapshotData);
unsafe static extern /* NSData * __nullable */ IntPtr NSDataFromGCMicroGamepadSnapshotData (
/* __nullable */ GCMicroGamepadSnapshotData* snapshotData);

#if NET
[SupportedOSPlatform ("tvos12.2")]
Expand All @@ -128,8 +133,12 @@ public struct GCMicroGamepadSnapshotData {
#endif
public NSData? ToNSData ()
{
var p = NSDataFromGCMicroGamepadSnapshotData (ref this);
return p == IntPtr.Zero ? null : new NSData (p);
unsafe {
fixed (GCMicroGamepadSnapshotData* self = &this) {
var p = NSDataFromGCMicroGamepadSnapshotData (self);
return p == IntPtr.Zero ? null : new NSData (p);
}
}
}
}

Expand All @@ -150,8 +159,7 @@ public partial class GCMicroGamepadSnapshot {
[Deprecated (PlatformName.TvOS, 13, 0, message: "Use 'GCController.GetMicroGamepadController()' instead.")]
#endif
[DllImport (Constants.GameControllerLibrary)]
[return: MarshalAs (UnmanagedType.I1)]
static extern bool GCMicroGamepadSnapShotDataV100FromNSData (out GCMicroGamepadSnapShotDataV100 snapshotData, /* NSData */ IntPtr data);
unsafe static extern byte GCMicroGamepadSnapShotDataV100FromNSData (GCMicroGamepadSnapShotDataV100* snapshotData, /* NSData */ IntPtr data);

#if NET
[SupportedOSPlatform ("macos")]
Expand All @@ -168,7 +176,10 @@ public partial class GCMicroGamepadSnapshot {
#endif
public static bool TryGetSnapshotData (NSData? data, out GCMicroGamepadSnapShotDataV100 snapshotData)
{
return GCMicroGamepadSnapShotDataV100FromNSData (out snapshotData, data.GetHandle ());
snapshotData = default;
unsafe {
return GCMicroGamepadSnapShotDataV100FromNSData ((GCMicroGamepadSnapShotDataV100*) Unsafe.AsPointer<GCMicroGamepadSnapShotDataV100> (ref snapshotData), data.GetHandle ()) != 0;
}
}

#if NET
Expand All @@ -187,8 +198,7 @@ public static bool TryGetSnapshotData (NSData? data, out GCMicroGamepadSnapShotD
[iOS (12, 2)]
#endif
[DllImport (Constants.GameControllerLibrary)]
[return: MarshalAs (UnmanagedType.I1)]
static extern bool GCMicroGamepadSnapshotDataFromNSData (out GCMicroGamepadSnapshotData snapshotData, /* NSData */ IntPtr data);
unsafe static extern byte GCMicroGamepadSnapshotDataFromNSData (GCMicroGamepadSnapshotData* snapshotData, /* NSData */ IntPtr data);

#if NET
[SupportedOSPlatform ("tvos12.2")]
Expand All @@ -204,7 +214,10 @@ public static bool TryGetSnapshotData (NSData? data, out GCMicroGamepadSnapShotD
#endif
public static bool TryGetSnapshotData (NSData? data, out GCMicroGamepadSnapshotData snapshotData)
{
return GCMicroGamepadSnapshotDataFromNSData (out snapshotData, data.GetHandle ());
snapshotData = default;
unsafe {
return GCMicroGamepadSnapshotDataFromNSData ((GCMicroGamepadSnapshotData*) Unsafe.AsPointer<GCMicroGamepadSnapshotData> (ref snapshotData), data.GetHandle ()) != 0;
}
}

}
Expand Down
10 changes: 0 additions & 10 deletions tests/cecil-tests/BlittablePInvokes.KnownFailures.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,6 @@ public partial class BlittablePInvokes {
"Security.SslStatus Security.SslContext::SSLSetSessionOption(System.IntPtr,Security.SslSessionOption,System.Boolean)",
"Security.SslStatus Security.SslContext::SSLWrite(System.IntPtr,System.Byte*,System.IntPtr,System.IntPtr&)",
"System.Boolean Foundation.NSObject::xamarin_set_gchandle_with_flags_safe(System.IntPtr,System.IntPtr,Foundation.NSObject/XamarinGCHandleFlags)",
"System.Boolean GameController.GCExtendedGamepadSnapshot::GCExtendedGamepadSnapshotDataFromNSData(GameController.GCExtendedGamepadSnapshotData&,System.IntPtr)",
"System.Boolean GameController.GCExtendedGamepadSnapshot::GCExtendedGamepadSnapShotDataV100FromNSData(GameController.GCExtendedGamepadSnapShotDataV100&,System.IntPtr)",
"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 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 @@ -226,11 +221,6 @@ public partial class BlittablePInvokes {
"System.Int32 Security.SslContext::SSLSetSessionTicketsEnabled(System.IntPtr,System.Boolean)",
"System.IntPtr Foundation.NSSearchPath::NSSearchPathForDirectoriesInDomains(System.UIntPtr,System.UIntPtr,System.Boolean)",
"System.IntPtr Foundation.NSThread::xamarin_init_nsthread(System.IntPtr,System.Boolean,System.IntPtr,System.IntPtr,System.IntPtr)",
"System.IntPtr GameController.GCExtendedGamepadSnapshotData::NSDataFromGCExtendedGamepadSnapshotData(GameController.GCExtendedGamepadSnapshotData&)",
"System.IntPtr GameController.GCExtendedGamepadSnapShotDataV100::NSDataFromGCExtendedGamepadSnapShotDataV100(GameController.GCExtendedGamepadSnapShotDataV100&)",
"System.IntPtr GameController.GCGamepadSnapShotDataV100::NSDataFromGCGamepadSnapShotDataV100(GameController.GCGamepadSnapShotDataV100&)",
"System.IntPtr GameController.GCMicroGamepadSnapshotData::NSDataFromGCMicroGamepadSnapshotData(GameController.GCMicroGamepadSnapshotData&)",
"System.IntPtr GameController.GCMicroGamepadSnapShotDataV100::NSDataFromGCMicroGamepadSnapShotDataV100(GameController.GCMicroGamepadSnapShotDataV100&)",
"System.IntPtr ObjCRuntime.Selector::GetHandle(System.String)",
"System.IntPtr Security.SecAccessControl::SecAccessControlCreateWithFlags(System.IntPtr,System.IntPtr,System.IntPtr,System.IntPtr&)",
"System.IntPtr Security.SecCertificate::SecCertificateCopySerialNumberData(System.IntPtr,System.IntPtr&)",
Expand Down

12 comments on commit 8bb2048

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

Please sign in to comment.