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

Signals, Cairo, Events + General Cleanup #323

Merged
merged 43 commits into from
Aug 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
dfa77f4
Stop using ClosureMarshal workaround
mjakeman May 19, 2021
3019b01
WIP: Attempt to fix issues with GValue
mjakeman May 20, 2021
3ae0b76
Merge branch 'feature/fix_window_sample' of http://github.com/gircore…
mjakeman May 23, 2021
f3e7815
Fix handling of unions
mjakeman May 23, 2021
a1eddc4
Handle value (non-pointer) records in the Generator
mjakeman May 26, 2021
dbc2177
Add DrawingArea sample
mjakeman May 26, 2021
ac12361
Miscellaneous API additions
mjakeman May 26, 2021
359b780
Basic implementation of Cairo
mjakeman May 26, 2021
62faa2e
Merge branch 'develop' into wip/mjake/signals
mjakeman May 26, 2021
0fd4a8b
Merge branch 'wip/mjake/signals' of https://github.com/gircore/gir.co…
mjakeman Jun 5, 2021
77a0be4
Merge branch 'develop' of https://github.com/gircore/gir.core into wi…
mjakeman Jun 5, 2021
a53742e
Naive attempt at Record Mapping
mjakeman Jun 6, 2021
35bf1c2
Register records in module, support cairo context
mjakeman Jun 7, 2021
d463deb
Fix GLib module generation
mjakeman Jun 7, 2021
df60cd1
Clean up legacy code / recreate solution from scratch
mjakeman Jun 7, 2021
f56a94f
Rename Gstreamer.csproj to GStreamer.csproj
mjakeman Jun 7, 2021
5336e7e
Fix enum/bitfield generation
mjakeman Jul 9, 2021
5f9fa52
Add TextEditor Demo
mjakeman Jul 9, 2021
820d668
Add TextEditor Demo
mjakeman Jul 9, 2021
354e824
Register union types (treat them like records)
mjakeman Jul 14, 2021
1c20378
Merge branch 'wip/mjake/signals' of https://github.com/gircore/gir.co…
mjakeman Jul 14, 2021
a11a827
Make handwritten cairo bindings cross-platform
mjakeman Jul 14, 2021
c9fb546
Special case Gdk.Event
mjakeman Jul 15, 2021
5c9975e
Implement every type of Gdk.Event
mjakeman Jul 15, 2021
2b53306
Merge branch 'develop' into wip/mjake/signals
mjakeman Jul 15, 2021
4f48650
Merge remote-tracking branch 'origin/develop' into wip/mjake/signals
mjakeman Aug 1, 2021
e88c744
Disable deletion in the text editor
mjakeman Aug 1, 2021
e28b058
Upload generated files for debugging
mjakeman Aug 1, 2021
620ed74
Upload only on failure
mjakeman Aug 1, 2021
a030d64
Fix upload path
mjakeman Aug 1, 2021
d9a9461
CI: Exclude build artifacts (only upload source code)
mjakeman Aug 1, 2021
cde3d7b
Add more helpful logging for WriteSignalArgsProperties
mjakeman Aug 1, 2021
59b03de
Merge branch 'develop' into feature/genarator_improvements
badcel Aug 7, 2021
41106fb
Fix remaining merge error
badcel Aug 7, 2021
4c34e37
Update solution
badcel Aug 7, 2021
0b25b47
Add xlib / gmodule
badcel Aug 7, 2021
2161cc3
Cleanup
badcel Aug 7, 2021
2e25750
Try to avoid factory methods.
badcel Aug 7, 2021
6aeab75
Revert some test code
badcel Aug 8, 2021
fe05f41
Fix reference error
badcel Aug 8, 2021
8775b87
Add Tests
badcel Aug 8, 2021
a6b6123
Add sample projects
badcel Aug 8, 2021
d73171b
Runtime: Use attribute to find factory method.
badcel Aug 9, 2021
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
10 changes: 10 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,13 @@ jobs:
if: ${{ matrix.os == 'ubuntu-latest' }}
run: dotnet run -- --release --xml-documentation --targets integrationTest samples
working-directory: './Build'

- name: (Failure) Upload Generated Files
if: ${{ failure() }}
uses: actions/upload-artifact@v2
with:
name: ${{ matrix.os }}-generated
path: |
${{ github.workspace }}
!**/bin
!**/obj
13 changes: 9 additions & 4 deletions Build/Projects.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,18 @@ public static class Projects
private const string GTK3_WINDOW = SAMPLE + "Gtk3/Window";
private const string GTK3_DECLARATIVE_UI = SAMPLE + "Gtk3/DeclarativeUi";
private const string GTK3_ABOUT_DIALOG = SAMPLE + "Gtk3/AboutDialog";

private const string GTK3_QUICKSTART = SAMPLE + "Gtk3/QuickStart";
private const string GTK3_DRAWINGAREA = SAMPLE + "Gtk3/DrawingArea";
private const string GTK3_TEXTEDITOR = SAMPLE + "Gtk3/TextEditor";

// private const string DBUS_SAMPLE = SAMPLE + "DBus/";
// private const string GSTREAMER_SAMPLE = SAMPLE + "GStreamer/";
// private const string GTK3_APP_SAMPLE = SAMPLE + "Gtk3/GtkApp/";
// private const string GTK3_BUILDER_SAMPLE = SAMPLE + "Gtk3/Builder";
// private const string GTK3_QUICKSTART = SAMPLE + "Gtk3/QuickStart";

// private const string GTK3_COMPOSITE_TEMPLATE_SOURCEGENERATOR = SAMPLE + "Gtk3/CompositeTemplates/UsingSourceGenerator";
// private const string GTK3_COMPOSITE_TEMPLATE_NO_SOURCEGENERATOR = SAMPLE + "Gtk3/CompositeTemplates/NoSourceGenerator";
// private const string GTK4_SIMPLE_WINDOW_SAMPLE = SAMPLE + "Gtk4/SimpleWindow/";
// private const string GDKPIXBUF_TEST_MEMORY_LEAKS = SAMPLE + "GdkPixbuf/TestMemoryLeaks";

private const string INTEGRATION = "../Integration/";

Expand All @@ -65,7 +67,10 @@ public static class Projects
GDK_PIXBUF_TEST_MEMORY_LEAKS,
GTK3_WINDOW,
GTK3_DECLARATIVE_UI,
GTK3_ABOUT_DIALOG
GTK3_ABOUT_DIALOG,
GTK3_QUICKSTART,
GTK3_DRAWINGAREA,
GTK3_TEXTEDITOR
};

public static IEnumerable<Project> AllLibraries = new[]
Expand Down
8 changes: 4 additions & 4 deletions Generator/Convert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,13 @@ internal static string NativeToManaged(TransferableAnyType transferable, string
// Record Conversions (safe handles)
ArrayTypeReference { Type: Record r, TypeReference: { CTypeReference: { IsPointer: true } } } when useSafeHandle => $"{fromParam}.Select(x => new {r.Write(Target.Managed, currentNamespace)}(x)).ToArray()",
ResolveableTypeReference { Type: Record r, CTypeReference: { IsPointer: true } } when useSafeHandle => $"new {r.Write(Target.Managed, currentNamespace)}({fromParam})",

// Record Conversions (raw pointers)
ArrayTypeReference { Type: Record r, TypeReference: { CTypeReference: { IsPointer: true } } } when !useSafeHandle => $"{fromParam}.Select(x => new {r.Write(Target.Managed, currentNamespace)}(new {SafeHandleFromRecord(r)}(x))).ToArray()",
ResolveableTypeReference { Type: Record r, CTypeReference: { IsPointer: true } } when !useSafeHandle => $"new {r.Write(Target.Managed, currentNamespace)}(new {SafeHandleFromRecord(r)}({fromParam}))",

//Record Conversions without pointers are not working yet
ArrayTypeReference { Type: Record r, TypeReference: { CTypeReference: { IsPointer: false } } } => $"({r.Write(Target.Managed, currentNamespace)}[]) default!; //TODO: Fixme",
ArrayTypeReference { Type: Record r, TypeReference: { CTypeReference: { IsPointer: false } } } => $"({qualifiedType}[]) {fromParam}.Select(x => new {qualifiedType}({SafeHandleFromRecord(r, true)}(x))).ToArray();",
ResolveableTypeReference { Type: Record r, CTypeReference: { IsPointer: false } } => $"({r.Write(Target.Managed, currentNamespace)}) default!; //TODO: Fixme",

// Class Conversions
Expand All @@ -97,9 +97,9 @@ internal static string NativeToManaged(TransferableAnyType transferable, string
};
}

private static string SafeHandleFromRecord(Record r)
private static string SafeHandleFromRecord(Record r, bool managedHandle = false)
{
var type = r.GetMetadataString("SafeHandleRefName");
var type = r.GetMetadataString(managedHandle ? "SafeHandleRefManagedFunc" : "SafeHandleRefName");
var nspace = $"{r.Repository.Namespace}.Native";
return nspace + "." + type;
}
Expand Down
27 changes: 20 additions & 7 deletions Generator/Extensions/AnyTypeExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,32 @@ private static string WriteNativeType(AnyType anyType, Namespace currentNamespac
// Arrays of byte can be marshalled automatically, no IntPtr needed
{ TypeReference: ArrayTypeReference { Type: { } s } } when s.Name == "byte" => "byte[]",

//References to records which are not using a pointer
// References to records which are not using a pointer
{ TypeReference: ArrayTypeReference { Type: Record r, TypeReference: { CTypeReference: { IsPointer: false } } } } => GetStructName(r, currentNamespace) + "[]",
{ TypeReference: ArrayTypeReference { Type: Record r, TypeReference: { CTypeReference: null } } } => GetStructName(r, currentNamespace) + "[]",
{ TypeReference: { Type: Record r, CTypeReference: { IsPointer: false } } } => GetStructName(r, currentNamespace),


//References to records which are using a pointer

// References to records which are using a pointer
{ TypeReference: ArrayTypeReference { Type: Record, TypeReference: { CTypeReference: { IsPointer: true } } } } => "IntPtr[]", //Array of SafeHandle not supported by runtime
{ TypeReference: ResolveableTypeReference { Type: Record r, CTypeReference: { IsPointer: true } } } => GetSafeHandleName(r, currentNamespace, useSafeHandle),


// References to unions which are not using a pointer
{ TypeReference: ArrayTypeReference { Type: Union u, TypeReference: { CTypeReference: { IsPointer: false } } } } => GetStructName(u, currentNamespace) + "[]",
{ TypeReference: ArrayTypeReference { Type: Union u, TypeReference: { CTypeReference: null } } } => GetStructName(u, currentNamespace) + "[]",
{ TypeReference: { Type: Union u, CTypeReference: { IsPointer: false } } } => GetStructName(u, currentNamespace),

// References to unions which are using a pointer
{ TypeReference: ArrayTypeReference { Type: Union, TypeReference: { CTypeReference: { IsPointer: true } } } } => "IntPtr[]", //Array of SafeHandle not supported by runtime
{ TypeReference: ResolveableTypeReference { Type: Union, CTypeReference: { IsPointer: true } } } => "IntPtr",

// Primitives - Marshal directly
{ TypeReference: ArrayTypeReference { Type: PrimitiveValueType s } } => s.Write(Target.Native, currentNamespace) + "[]",
{ TypeReference: { Type: PrimitiveValueType s } } => s.Write(Target.Native, currentNamespace),

// Enumerations - Marshal directly
{ TypeReference: ArrayTypeReference { Type: Enumeration } } => anyType.TypeReference.Type.Write(Target.Native, currentNamespace) + "[]",
{ TypeReference: { Type: Enumeration } } => anyType.TypeReference.Type.Write(Target.Native, currentNamespace),

// Short path for strings as strings are pointers which should not be handled as pointers
{ TypeReference: { Type: String r } } => r.Name,

Expand All @@ -68,7 +76,12 @@ private static string GetStructName(Record r, Namespace currentNamespace)
{
return AddNamespace(currentNamespace, r.Repository.Namespace, r.GetMetadataString("StructRefName"), Target.Native);
}


private static string GetStructName(Union u, Namespace currentNamespace)
{
return AddNamespace(currentNamespace, u.Repository.Namespace, u.GetMetadataString("StructRefName"), Target.Native);
}

private static string GetSafeHandleName(Record r, Namespace currentNamespace, bool useSafeHandle)
{
if (useSafeHandle)
Expand Down
2 changes: 1 addition & 1 deletion Generator/Extensions/FieldsExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static string WriteUnionStructFields(this IEnumerable<Field> fields, Name

foreach (Field field in fields)
{
builder.AppendLine("//[FieldOffset(0)] TODO Enable offset");
builder.AppendLine("[FieldOffset(0)]");
builder.AppendLine(field.WriteNative(currentNamespace));
}

Expand Down
19 changes: 13 additions & 6 deletions Generator/Extensions/ParameterListExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,19 @@ public static string WriteSignalArgsProperties(this ParameterList parameterList,
var index = 0;
foreach (var argument in parameterList.GetParameters())
{
index += 1;
var type = argument.WriteType(Target.Native, currentNamespace);
var name = new GirLoader.Helper.String(argument.Name).ToPascalCase();

builder.AppendLine($"//TODO: public {type} {name} => Args[{index}].Extract<{type}>();");
builder.AppendLine($"public string {name} => \"\";");
try
{
index += 1;
var type = argument.WriteType(Target.Managed, currentNamespace);
var name = new GirLoader.Helper.String(argument.Name).ToPascalCase();

builder.AppendLine($"public {type} {name} => Args[{index}].Extract<{type}>();");
}
catch (Exception e)
{
Log.Error($"Could not write signal properties for {argument.Name}: {e.Message}");
throw;
}
}

return builder.ToString();
Expand Down
9 changes: 7 additions & 2 deletions Generator/Extensions/TypeExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,18 @@ internal static string Write(this Type type, Target target, Namespace currentNam
ComplexType c when !c.Repository.Namespace.IsForeignTo(currentNamespace) => c.Name,
ComplexType c => $"{c.Repository.Namespace.GetName(target)}.{c.Name}",

_ => throw new Exception($"Unknown type {type}.")
_ => throw new Exception($"Unknown type: {type}")
};
}

public static string WriteTypeRegistration(this Type type)
public static string WriteTypeRegistrationClass(this Type type)
{
return $"TypeDictionary.Add(typeof({type.Name}), new GObject.Type(Native.{type.Name}.Instance.Methods.GetGType()));\r\n";
}

public static string WriteTypeRegistrationBoxed(this Type type)
{
return $"TypeDictionary.Add(typeof({type.Name}), new GObject.Type(Native.{type.Name}.Methods.GetGType()));\r\n";
}
}
}
5 changes: 4 additions & 1 deletion Generator/Services/TypeRenamer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,9 @@ private void SetUnionMetadata(Union union)
{
union.Metadata["Name"] = union.Name;
union.Metadata["StructName"] = "Struct";
union.Metadata["StructRefName"] = $"{union.Name}.Struct";

union.Name = new TypeName($"{union.Name}.Struct");
union.Name = new TypeName($"{union.Name}");
}

private void SetRecordMetadata(IEnumerable<Record> records)
Expand All @@ -121,6 +122,7 @@ private void SetClassStructMetadata(Record record)
record.Metadata["StructRefName"] = $"{className}.Class";
record.Metadata["SafeHandleName"] = "Handle";
record.Metadata["SafeHandleRefName"] = $"{className}.Handle";
record.Metadata["SafeHandleRefManagedFunc"] = $"{className}.ManagedHandle.Create";

record.Name = new TypeName($"{className}.Class");
}
Expand All @@ -132,6 +134,7 @@ private void SetRecordMetadata(Record record)
record.Metadata["StructRefName"] = $"{record.Name}.Struct";
record.Metadata["SafeHandleName"] = "Handle";
record.Metadata["SafeHandleRefName"] = $"{record.Name}.Handle";
record.Metadata["SafeHandleRefManagedFunc"] = $"{record.Name}.ManagedHandle.Create";

record.Name = new TypeName($"{record.Name}");
}
Expand Down
17 changes: 13 additions & 4 deletions Generator/Services/Writer/WriteModuleService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Generator.Factories;
Expand Down Expand Up @@ -37,17 +38,25 @@ public void Write(Namespace ns, string outputDir)

private void WriteTypeDictionaryInitialization(Namespace ns, string outputDir)
{
var classes = ns.Classes.Where(x => !x.IsFundamental);
IEnumerable<Type> classes = ns.Classes.Where(x => !x.IsFundamental);
IEnumerable<Type> records = ns.Records.Where(x => x.GetTypeFunction is not null);
IEnumerable<Type> unions = ns.Unions.Where(x => x.GetTypeFunction is not null);

IEnumerable<Type> types = classes.Concat(records);

if (!classes.Any())
if (!types.Any())
return;

var scriptObject = new ScriptObject()
{
{ "namespace", ns },
{ "classes", classes},
{ "classes", classes },
{ "records", records },
{ "unions", unions }
};
scriptObject.Import("write_type_registration", new Func<Type, string>(s => s.WriteTypeRegistration()));
scriptObject.Import("write_type_registration", new Func<Type, string>(s => s.WriteTypeRegistrationClass()));
scriptObject.Import("write_type_registration_record", new Func<Type, string>(s => s.WriteTypeRegistrationBoxed()));
scriptObject.Import("write_type_registration_union", new Func<Type, string>(s => s.WriteTypeRegistrationBoxed()));

_writeHelperService.Write(
projectName: ns.ToCanonicalName(),
Expand Down
28 changes: 27 additions & 1 deletion Generator/Templates/module_type_registration.sbntxt
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{{ if namespace.name != "GLib" # TODO: Temporary until we merge GLib + GObject }}
using GObject.Native;
{{ end }}
using System;

namespace {{ namespace.name }}
Expand All @@ -7,16 +9,40 @@ namespace {{ namespace.name }}
{
static partial void RegisterTypes()
{
{{ if namespace.name != "GLib" # TODO: Temporary until we merge GLib + GObject }}
{{ for $class in classes}}
try
{
{{ $class | write_type_registration }}
}
catch (Exception e)
{
Console.WriteLine($"Could not register type '{{ $class.name }}': {e.Message}");
Console.WriteLine($"Could not register class type '{{ $class.name }}': {e.Message}");
}
{{ end }}

{{ for $record in records }}
try
{
{{ $record | write_type_registration_record }}
}
catch (Exception e)
badcel marked this conversation as resolved.
Show resolved Hide resolved
{
Console.WriteLine($"Could not register record type '{{ $record.name }}': {e.Message}");
}
{{ end }}

{{ for $union in unions }}
try
{
{{ $union | write_type_registration_union }}
}
catch (Exception e)
{
Console.WriteLine($"Could not register union type '{{ $union.name }}': {e.Message}");
}
{{ end }}
{{ end }}
}
}
}
2 changes: 2 additions & 0 deletions Generator/Templates/record.sbntxt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ namespace {{ namespace.name }}
// Override this to perform additional steps in the constructor
partial void Initialize();

public {{ $record_name }}(IntPtr ptr) : this(new {{$safe_handle}}(ptr)){ }

public {{ $record_name }}({{ $safe_handle }} handle)
{
_handle = handle;
Expand Down
3 changes: 1 addition & 2 deletions Generator/Templates/union.native.struct.sbntxt
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//TODO: Enable explicit union generation if it does not break documentation generation anymore
//[StructLayout(LayoutKind.Explicit)]
[StructLayout(LayoutKind.Explicit)]
public partial struct {{ get_metadata "StructName" }}
{
{{ include 'union.native.struct.fields.sbntxt' }}
Expand Down
3 changes: 2 additions & 1 deletion Generator/Templates/union.sbntxt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ using System.Runtime.InteropServices;
namespace {{ namespace.name }}
{
// AUTOGENERATED FILE - DO NOT MODIFY
public partial record {{get_metadata "Name"}}
public partial class {{ get_metadata "Name" }}
{
// TODO: Handle Unions
}
}
Loading