private static void WriteEvents(Interface iface, CSharpWriter w, CSharpWriter hlw) { var events = iface.Events; foreach (var ev in events) { if (GenerateComments) { WriteDescription(ev.Element, w); } w.Line(GetEventDelegate(iface, ev)); w.Line(); } if (events.Length > 0) { w.Line($"internal struct {iface.RawName}_listener"); w.OpenBlock(); foreach (var ev in events) { w.Line($"public IntPtr {ev.RawName};"); } w.Line(); w.Line($"public static {iface.RawName}_listener* Alloc("); for (int i = 0; i < events.Length; i++) { var ev = events[i]; if (i != 0) { w.Line(","); } w.AppendIndented($"{GetDelegateName(iface, ev)} {ev.RawName}"); } w.Line(")"); w.OpenBlock(); w.Line($"var ret = ({iface.RawName}_listener*) Marshal.AllocHGlobal(sizeof({iface.RawName}_listener));"); w.Line($"Set(ret, {string.Join(",", events.Select(ev => ev.RawName))});"); w.Line("return ret;"); w.CloseBlock(); w.Line(); w.Line($"public static void Set({iface.RawName}_listener* listener"); foreach (var ev in events) { w.Line(","); w.AppendIndented($"{GetDelegateName(iface, ev)} {ev.RawName}"); } w.Line(")"); w.OpenBlock(); foreach (var ev in events) { w.Append($"if ({ev.RawName} != null) "); w.Line($"listener->{ev.RawName} = Marshal.GetFunctionPointerForDelegate<{GetDelegateName(iface, ev)}>({ev.RawName});"); } w.CloseBlock(); w.CloseBlock(); // struct listener w.Line(); if (GenerateComments) { w.DocSummary($"Set the callbacks for the given <see cref=\"{iface.RawName}\"/>."); foreach (var ev in events) { WriteArgsDescription(ev.Element, w); } } w.Line($"public static int {iface.RawName}_add_listener({iface.RawName}* iface, {iface.RawName}_listener* listener)"); w.OpenBlock(); w.Line("return WaylandClient.wl_proxy_add_listener((wl_proxy*) iface, listener, null);"); w.CloseBlock(); hlw.Append("public void SetListener("); for (var i = 0; i < events.Length; i++) { var ev = events[i]; hlw.Line(i == 0 ? "" : ","); hlw.AppendIndented($"{_protocolClassName}.{GetDelegateName(iface, ev)} {ev.RawName}"); } hlw.Line(")"); hlw.OpenBlock(); foreach (var ev in events) { hlw.Line($"_{ev.RawName} = {ev.RawName};"); } hlw.Line($"_listener = {_protocolClassName}.{iface.RawName}_listener.Alloc({string.Join(", ", events.Select(e => e.RawName))});"); hlw.Line($"{_protocolClassName}.{iface.RawName}_add_listener(Pointer, _listener);"); hlw.CloseBlock(); hlw.Line("public void FreeListener() { if (_listener != null) Marshal.FreeHGlobal((IntPtr) _listener); }"); } }