private static void WriteEnum(Interface iface, WlEnum e, CSharpWriter w) { if (GenerateComments) { WriteDescription(e.Element, w); } if (e.Bitfield) { w.Line("[Flags]"); } w.Line($"internal enum {iface.RawName}_{e.Name}"); w.OpenBlock(); foreach (var entry in e.Entries) { if (GenerateComments) { w.DocSummary(entry.Summary); } var name = Util.ToPascalCase(entry.Name); // number only names get a 'V' prepended if (name.All(c => char.IsDigit(c))) { name = "V" + name; } w.Line($"{name} = {entry.Value},"); if (GenerateComments) { w.Line(); } } w.CloseBlock(); }
private static void WriteDescription(XElement e, CSharpWriter w) { if (GenerateComments) { var summary = e.Attribute(SummaryAttrib); var descr = e.Element(DescriptionElement); if (descr != null) { var descrStr = descr.Value; var summaryStr = summary == null ? string.Empty : summary.Value + "\n"; descrStr = $"{summaryStr}<p>" + descrStr + "</p>"; var lines = descrStr .Trim() .Split('\n') .SelectMany(l => string.IsNullOrEmpty(l) ? new [] { "</p>", "<p>" } : new [] { l }) .Select(s => s.Trim()); w.DocSummary(lines); } } }
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); }"); } }