Skip to content

Commit

Permalink
Create binding helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
tj_devel709 committed Oct 19, 2023
1 parent 1201398 commit 5bdc17a
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 59 deletions.
25 changes: 25 additions & 0 deletions src/BindingHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,38 @@
namespace BindingHelpers
{
internal static class BindingHelpers {
// this method is helpful for the getter for something like:
// unsafe PMPrinter* PMPrinter { get; }
//
// we can change this to:
// [Internal]
// [Export ("PMPrinter")]
// IntPtr _GetPMPrinter ();
//
// and then add this to the manual file:
// public PMPrinter? PMPrinter {
// get => BindingHelpers.GetPtrToStruct<PMPrinter> (_GetPMPrinter ());
// }
public static T? GetPtrToStruct<T> (IntPtr intPtr) where T : struct
{
if (intPtr == IntPtr.Zero)
return null;
return Marshal.PtrToStructure<T> (intPtr);
}

// this method is helpful for the setter for something like:
// unsafe MPSScaleTransform* ScaleTransform { get; set; }
//
// we can change this to:
// [Export ("setScaleTransform:")]
// [Internal]
// void _SetScaleTransform (IntPtr value);
//
// and then add this to the manual file:
// public MPSScaleTransform? ScaleTransform {
// ...
// set => BindingHelpers.SetPtrToStruct<MPSScaleTransform> (value, (ptr) => _SetScaleTransform (ptr));
// }
public static void SetPtrToStruct<T> (object? value, Action<IntPtr> setAction) where T : struct
{
if (value.HasValue) {
Expand Down
64 changes: 11 additions & 53 deletions src/PrintCore/PDEPlugInCallbackProtocol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,75 +6,33 @@
using System.Runtime.InteropServices;
using Foundation;
using ObjCRuntime;
// TODO add reference to BindingHelpers
using BindingHelpers;

namespace PrintCore {

public partial class PDEPlugInCallbackProtocol {

// public virtual MPSScaleTransform? ScaleTransform {
// get {
// var ptr = _GetScaleTransform ();
// if (ptr == IntPtr.Zero)
// return null;
// return Marshal.PtrToStructure<MPSScaleTransform> (ptr);
// }
// set {
// if (value.HasValue) {
// IntPtr ptr = Marshal.AllocHGlobal (size_of_scale_transform);
// try {
// Marshal.StructureToPtr<MPSScaleTransform> (value.Value, ptr, false);
// _SetScaleTransform (ptr);
// } finally {
// Marshal.FreeHGlobal (ptr);
// }
// } else {
// _SetScaleTransform (IntPtr.Zero);
// }
// }
// }

public PMPrintSession? PrintSession {
get => GetPtrToStruct<PMPrintSession> (_GetPrintSession ());
get => BindingHelpers.GetPtrToStruct<PMPrintSession> (_GetPrintSession ());
}

public PMPrintSession? PrintSession {
get => GetPtrToStruct<PMPrintSession> (_GetPrintSettings ());
public PMPrintSession? PrintSettings {
get => BindingHelpers.GetPtrToStruct<PMPrintSession> (_GetPrintSettings ());
}

public PMPageFormat? PageFormat {
get => GetPtrToStruct<PMPageFormat> (_GetPageFormat ());
get => BindingHelpers.GetPtrToStruct<PMPageFormat> (_GetPageFormat ());
}

public PMPrinter? PMPrinter {
get => GetPtrToStruct<PMPrinter> (_GetPMPrinter ());
get => BindingHelpers.GetPtrToStruct<PMPrinter> (_GetPMPrinter ());
}

public ppd_file_s? PpdFile {
get => GetPtrToStruct<ppd_file_s> (_GetPpdFile ());
}

internal T? GetPtrToStruct<T> (IntPtr intPtr) where T : struct
{
if (intPtr == IntPtr.Zero)
return null;
return Marshal.PtrToStructure<T> (intPtr);
}

internal void SetPtrToStruct<T> (object? value, Action<IntPtr> setAction) where T : struct
{
if (value.HasValue) {
var size_of_scale_transform = Marshal.SizeOf<T> ();
IntPtr ptr = Marshal.AllocHGlobal (size_of_scale_transform);
try {
Marshal.StructureToPtr<T> (value.Value, ptr, false);
setAction (ptr);
} finally {
Marshal.FreeHGlobal (ptr);
}
} else {
setAction (IntPtr.Zero);
}
}
// TODO Figure out how to make the ppd_file_s: https://gist.github.com/tj-devel709/ef051c6750fb304d826f58059d2c1acd#file-ppd-h-L286
// public ppd_file_s? PpdFile {
// get => BindingHelpers.GetPtrToStruct<ppd_file_s> (_GetPpdFile ());
// }
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/frameworks.sources
Original file line number Diff line number Diff line change
Expand Up @@ -1482,7 +1482,8 @@ PHOTOSUI_API_SOURCES = \
# PrintCode
PRINTCORE_SOURCES = \
PrintCore/Defs.cs \
PrintCore/PrintCore.cs
PrintCore/PrintCore.cs \
PrintCore/PDEPlugInCallbackProtocol.cs \

# PushToTalk
PUSHTOTALK_SOURCES = \
Expand Down
10 changes: 5 additions & 5 deletions src/printcore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ interface PDEPanel
[BaseType (typeof (NSObject))]
interface PDEPlugIn
{
[Abstract]
[Export ("initWithBundle:")]
NativeHandle Constructor (NSBundle bundle);

Expand All @@ -91,20 +90,21 @@ interface IPDEPlugInCallbackProtocol { }
[Protocol]
interface PDEPlugInCallbackProtocol
{
[NullAllowed, Export ("printSession")]
[Abstract]
[Internal]
[Export ("printSession")]
// Original: unsafe PMPrintSession* PrintSession { get; }
IntPtr _GetPrintSession ();

[Abstract]
[NullAllowed, Export ("printSettings")]
[Internal]
[Export ("printSettings")]
// Original: unsafe PMPrintSession* PrintSettings { get; }
IntPtr _GetPrintSettings ();

[Abstract]
[Internal]
[NullAllowed, Export ("pageFormat")]
[Export ("pageFormat")]
// Original: unsafe PMPageFormat* PageFormat { get; }
IntPtr _GetPageFormat ();

Expand All @@ -116,7 +116,7 @@ interface PDEPlugInCallbackProtocol

[Abstract]
[Internal]
[NullAllowed, Export ("ppdFile")]
[Export ("ppdFile")]
// Original: unsafe ppd_file_s* PpdFile { get; }
IntPtr _GetPpdFile ();

Expand Down

0 comments on commit 5bdc17a

Please sign in to comment.