view InteropCodeGen/gendotnet.cs @ 5:00fb4879d273

Small fix in the keyboard mappings for the space key
author Ivo Smits
date Fri, 06 May 2011 03:56:30 +0200
parents 64226294e26b
children
line wrap: on
line source

/* This script can be used to generate C# interface definitions for XPCOM
 * interop, based on an xidl file. It is currently not very flexible and
 * biased towards VirtualBox.
 * 
 * To use, put the appropriate VirtualBox.xidl file in the same directory as
 * the application, and run it while piping it's output to a file:
 *  mono InteropCodeGen.exe > VirtualBox.cs
 * Then, for compatibility with the interop assembly generated by VS.Net, a
 * few modifications need to be made using the following regular expressions:
 *  sed -i VirtualBox.cs "s/void LockMachine(\[MarshalAs(UnmanagedType.Interface)\] ISession psession, LockType plockType);/void LockMachine([MarshalAs(UnmanagedType.Interface)\] Session psession, LockType plockType);/"
 *  sed -i VirtualBox.cs "s/LaunchVMProcess(\[MarshalAs(UnmanagedType\.Interface)\] ISession psession,/LaunchVMProcess([MarshalAs(UnmanagedType.Interface)] Session psession,/"
 *  sed -i VirtualBox.cs "s/\(interface IVirtualBoxErrorInfo {\)/\1\n\tvoid dummy1();void dummy2();void dummy3();void dummy4();void dummy5(); void dummy6(); void dummy7(); void dummy8(); void dummy9(); void dummy10();/"
 * Then, a few lines need to be appended to the file:
 *  namespace VirtualBox {
 *   [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)][Guid("12F4DCDB-12B2-4EC1-B7CD-DDD9F6C5BF4D")][ComImport()]interface Session : ISession {}
 *  }
 * Then, compile the generated file:
 *  gmcs -target:library -out:Interop.VirtualBox.XPCOM.dll VirtualBox.cs
 */
//+reference System.dll
//+reference System.Xml.dll
using System;
using System.Xml.XPath;
using System.IO;
using System.Collections.Generic;
class Program {
	enum ParameterType {
		In,
		Out,
		Reference,
		Return,
		Param,
	}
	class TypeInfo {
		public String Name { get; private set; }
		public String[] Attributes { get; private set; }

		public TypeInfo(String name, params String[] attributes) {
			this.Name = name;
			this.Attributes = attributes;
		}
		public void WriteDeclaration(TextWriter o, ParameterType vartype) {
			WriteAttributes(o, vartype);
			o.Write(Name);
		}
		public void WriteAttributes(TextWriter o, ParameterType vartype) {
			foreach (String attribute in Attributes) {
				o.Write("[");
				if (vartype == ParameterType.Return) o.Write("return: ");
				else if (vartype == ParameterType.Param) o.Write("param: ");
				o.Write(attribute);
				o.Write("] ");
			}
			if (vartype == ParameterType.Out) o.Write("out ");
			else if (vartype == ParameterType.Reference) o.Write("ref ");
		}
	}
	static Dictionary<String, TypeInfo> Types = new Dictionary<string, TypeInfo>();
	static void AddInterfaceTypes(params String[] names) {
		foreach (String name in names) Types.Add(name, new TypeInfo(name, "MarshalAs(UnmanagedType.Interface)"));
	}
	static void AddEnumTypes(params String[] names) {
		foreach (String name in names) Types.Add(name, new TypeInfo(name));
	}
	static void InitTypes() {
		Types.Add("short", new TypeInfo("Int16"));
		Types.Add("unsigned short", new TypeInfo("UInt16"));
		Types.Add("unsigned long", new TypeInfo("UInt32"));
		Types.Add("long long", new TypeInfo("Int64"));
		Types.Add("long", new TypeInfo("Int32"));
		Types.Add("octet", new TypeInfo("Byte"));
		Types.Add("unsigned long long", new TypeInfo("UInt32"));
		//Types.Add("boolean", new TypeInfo("Boolean", "MarshalAs(UnmanagedType.Bool)"));
		Types.Add("boolean", new TypeInfo("Int32"));
		Types.Add("wstring", new TypeInfo("String", "MarshalAs(UnmanagedType.LPWStr)"));
		//Types.Add("uuid", new TypeInfo("Guid", true, "MarshalAs(UnmanagedType.LPStruct)"));
		Types.Add("uuid", new TypeInfo("Guid", "MarshalAs(UnmanagedType.LPStruct)"));
		Types.Add("$unknown", new TypeInfo("IntPtr"));

		AddInterfaceTypes("IMachine", "ISession", "IVirtualBox", "IHost", "IConsole", "ISystemProperties");
		AddInterfaceTypes("IProgress", "IDisplay", "IMouse", "IKeyboard", "IFramebuffer", "IFramebufferOverlay");
		AddInterfaceTypes("IMachineDebugger", "IVirtualBoxErrorInfo", "IEventSource", "IEventListener", "IEventContext");
		AddInterfaceTypes("IConsoleCallback", "IEvent", "IBandwidthControl", "IVRDEServerInfo", "IAdditionsFacility");
		AddInterfaceTypes("IExtPackPlugIn", "IExtPackFile", "IExtPack", "IBIOSSettings", "IVFSExplorer", "IUSBController");
		AddInterfaceTypes("IStorageController", "IVRDPServer", "ISnapshot", "INATEngine", "IUSBDeviceFilter");
		AddInterfaceTypes("IMediumAttachment", "IUSBDevice", "IParallelPort", "ISerialPort", "INetworkAdapter");
		AddInterfaceTypes("IMedium", "IGuestOSType", "ISharedFolder", "IPerformanceCollector", "IDHCPServer");
		AddInterfaceTypes("IAppliance", "IVirtualBoxCallback", "IPerformanceMetric", "IMediumFormat", "IHostUSBDevice");
		AddInterfaceTypes("IHostNetworkInterface", "IHostUSBDeviceFilter", "IRemoteDisplayInfo", "IGuest");
		AddInterfaceTypes("IVirtualSystemDescription", "IAudioAdapter", "IExtPackManager", "IVRDEServer");
		AddInterfaceTypes("IPciDeviceAttachment", "IBandwidthGroup");
	
		AddEnumTypes("LockType", "MediumVariant", "AccessMode", "NATProtocol", "FirmwareType", "StorageControllerType");
		AddEnumTypes("StorageBus", "SessionType", "SessionState", "USBDeviceState", "USBDeviceFilterAction");
		AddEnumTypes("AudioControllerType", "AudioDriverType", "VRDPAuthType", "MachineState", "NetworkAdapterType");
		AddEnumTypes("NetworkAttachmentType", "PortMode", "DeviceType", "MediumState", "VFSType", "MediumType");
		AddEnumTypes("HostNetworkInterfaceType", "DeviceActivity", "HostNetworkInterfaceMediumType", "HostNetworkInterfaceStatus");
		AddEnumTypes("ProcessorFeature", "BIOSBootMenuMode", "VirtualSystemDescriptionType", "VirtualSystemDescriptionValueType");
		AddEnumTypes("KeyboardHidType", "ClipboardMode", "PointingHidType", "CPUPropertyType", "HWVirtExPropertyType");
		AddEnumTypes("ChipsetType", "FaultToleranceState", "BandwidthGroupType", "ExecuteProcessStatus", "VBoxEventType");
		AddEnumTypes("GuestMonitorChangedEventType", "AuthType", "NetworkAdapterPromiscModePolicy", "AdditionsRunLevelType");
		AddEnumTypes("AdditionsFacilityStatus", "AdditionsFacilityType", "AdditionsFacilityClass", "CleanupMode", "DataType");
	}
	static void Main(string[] args) {
		InitTypes();

		XPathDocument doc = new XPathDocument("VirtualBox.xidl");
		XPathNavigator nav = doc.CreateNavigator();
		nav = nav.SelectSingleNode("/idl/library[@name='VirtualBox']");
		TextWriter o = Console.Out;
		o.WriteLine("using System;");
		o.WriteLine("using System.Runtime.CompilerServices;");
		o.WriteLine("using System.Runtime.InteropServices;");
		o.WriteLine("namespace VirtualBox {");
		foreach (XPathNavigator intf in nav.Select("interface")) {
			ProcessInterface(intf, o, nav);
		}
		foreach (XPathNavigator intf in nav.Select("enum")) {
			ProcessEnum(intf, o);
		}
		o.WriteLine("}");
	}
	static void ProcessEnum(XPathNavigator nav, TextWriter o) {
		String typeName = nav.SelectSingleNode("@name").Value;
		if (!Types.ContainsKey(typeName)) Types.Add(typeName, new TypeInfo(typeName));
		o.WriteLine("[Guid(\"{0}\")]", nav.SelectSingleNode("@uuid").Value);
		o.WriteLine("enum " + typeName + " {");
		foreach (XPathNavigator member in nav.SelectChildren(XPathNodeType.Element)) {
			switch (member.Name) {
				case "desc":
					break;
				case "const":
					o.WriteLine("\t{0} = {1},", member.SelectSingleNode("@name").Value, member.SelectSingleNode("@value").Value);
					break;
				default:
					Console.Error.WriteLine("Unknown member type {0}", member.Name);
					break;
			}
		}
		o.WriteLine("}");
	}
	static void ProcessInterface(XPathNavigator nav, TextWriter o, XPathNavigator parentNav) {
		String typeName = nav.SelectSingleNode("@name").Value;
		if (!Types.ContainsKey(typeName)) Types.Add(typeName, new TypeInfo(typeName, "MarshalAs(UnmanagedType.Interface)"));
		o.WriteLine("[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]");
		o.WriteLine("[Guid(\"{0}\")]", nav.SelectSingleNode("@uuid").Value);
		o.WriteLine("[ComImport()]");
		o.Write("interface " + typeName);
		o.WriteLine(" {");
		ProcessInterfaceMembersAndInherited(nav, o, parentNav);
		o.WriteLine("}");
	}
	static void ProcessInterfaceMembersAndInherited(XPathNavigator nav, TextWriter o, XPathNavigator parentNav) {
		XPathItem item = nav.SelectSingleNode("@extends");
		if (item != null && item.Value[0] != '$') {
			XPathNavigator inav = parentNav.SelectSingleNode("interface[@name='" + item.Value + "']");
			ProcessInterfaceMembersAndInherited(inav, o, parentNav);
		}
		ProcessInterfaceMembers(nav.SelectChildren(XPathNodeType.Element), o);
	}
	static void ProcessInterfaceMembers(XPathNodeIterator members, TextWriter oattribute) {
		XPathItem item;
		StringWriter omethod = new StringWriter();
		foreach (XPathNavigator member in members) {
			switch (member.Name) {
				case "desc":
					break;
				case "attribute": {
						String mName = member.SelectSingleNode("@name").Value;
						item = member.SelectSingleNode("@readonly");
						Boolean mReadOnly = item == null ? false : item.Value == "yes";
						mName = mName.Substring(0, 1).ToUpper() + mName.Substring(1);
						oattribute.Write("\t");
						TypeInfo t = GetTypeDeclaration(member);
						oattribute.Write(t.Name);
						oattribute.Write(" ");
						oattribute.Write(mName);
						oattribute.WriteLine(" {");
						oattribute.Write("\t\t[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)] ");
						t.WriteAttributes(oattribute, ParameterType.Return);
						oattribute.WriteLine("get;");
						if (!mReadOnly) {
							oattribute.Write("\t\t[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)] ");
							t.WriteAttributes(oattribute, ParameterType.Param);
							oattribute.WriteLine("set;");
						}
						oattribute.WriteLine("\t}");
					} break;
				case "method": {
						String mName = member.SelectSingleNode("@name").Value;
						XPathNodeIterator mParamReturn = member.Select("param[@dir='return']");
						omethod.WriteLine("\t[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]");
						omethod.Write("\t");
						bool mReturn = false;
						if (mParamReturn == null || mParamReturn.Count != 1) {
							omethod.Write("void");
						} else {
							mParamReturn.MoveNext();
							mReturn = true;
							WriteTypeDeclaration(omethod, mParamReturn.Current, ParameterType.Return);
						}
						mName = mName.Substring(0, 1).ToUpper() + mName.Substring(1);
						omethod.Write(" ");
						omethod.Write(mName);
						omethod.Write("(");
						XPathNodeIterator mParams = member.Select("param");
						bool first = true;
						if (mParams.MoveNext()) {
							while (true) {
								XPathNavigator mParam = mParams.Current;
								String pDir = mParam.SelectSingleNode("@dir").Value;
								if (pDir == "return" && mReturn) {
									if (mParams.MoveNext()) {
										continue;
									} else {
										break;
									}
								} else if (pDir == "in") {
								} else if (pDir == "out") {
								} else {
									Console.Error.WriteLine("Unsupported parameter direction {0}", pDir);
								}
								if (first) {
									first = false;
								} else {
									omethod.Write(", ");
								}
								WriteTypeDeclaration(omethod, mParam, pDir == "out" ? ParameterType.Out : ParameterType.In);
								omethod.Write(" p");
								omethod.Write(mParam.SelectSingleNode("@name").Value);
								if (!mParams.MoveNext()) break;
							}
						}
						omethod.WriteLine(");");
					} break;
				default:
					Console.Error.WriteLine("Unknown member type {0}", member.Name);
					break;
			}
		}
		oattribute.Write((omethod as StringWriter).ToString());
	}
	private static TypeInfo GetTypeDeclaration(XPathNavigator nav) {
		ParameterType pt = ParameterType.Param;
		return GetTypeDeclaration(nav, ref pt);
	}
	private static void WriteTypeDeclaration(TextWriter o, XPathNavigator nav, ParameterType paramType) {
		TypeInfo t = GetTypeDeclaration(nav, ref paramType);
		t.WriteDeclaration(o, paramType);
	}
	private static TypeInfo GetTypeDeclaration(XPathNavigator nav, ref ParameterType paramType) {
		String mType = nav.SelectSingleNode("@type").Value;
		XPathItem item;
		item = nav.SelectSingleNode("@mod");
		String mMod = item == null ? null : item.Value;
		item = nav.SelectSingleNode("@safearray");
		Boolean pArray = item == null ? false : item.Value == "yes";

		TypeInfo t;
		if (!Types.TryGetValue(mType, out t)) {
			Console.Error.WriteLine("Type {0} is not supported", mType);
			t = new TypeInfo("void");
		}
		if (mMod == "ptr") {
			if (paramType == ParameterType.In) {
				paramType = ParameterType.Reference;
			} else {
				t = new TypeInfo("IntPtr");
			}
		} else if (mMod == "string") {
			t = new TypeInfo("String", "MarshalAs(UnmanagedType.LPWStr)");
		} else if (mMod != null) {
			Console.Error.WriteLine("Type modifier {0} is not supported", mMod);
			t = new TypeInfo("void");
		}
		if (pArray) {
			t = new TypeInfo("Array", "MarshalAs(UnmanagedType.SafeArray)");
			if (paramType == ParameterType.In || paramType == ParameterType.Out) paramType = ParameterType.Reference;
		}
		return t;
	}
}