From c916037773d7d3d8d37ca2c5a8899b7b728e377d Mon Sep 17 00:00:00 2001 From: Owen Fraser-Green Date: Tue, 23 Mar 2004 12:10:32 +0000 Subject: First checkin of the Mono bindings. --- mono/DBusType/Array.cs | 130 +++++++++++++++++++++++++++++++++++++++ mono/DBusType/Boolean.cs | 86 ++++++++++++++++++++++++++ mono/DBusType/Byte.cs | 86 ++++++++++++++++++++++++++ mono/DBusType/Custom.cs | 109 +++++++++++++++++++++++++++++++++ mono/DBusType/Dict.cs | 145 ++++++++++++++++++++++++++++++++++++++++++++ mono/DBusType/Double.cs | 86 ++++++++++++++++++++++++++ mono/DBusType/IDBusType.cs | 16 +++++ mono/DBusType/Int32.cs | 86 ++++++++++++++++++++++++++ mono/DBusType/Int64.cs | 86 ++++++++++++++++++++++++++ mono/DBusType/Nil.cs | 68 +++++++++++++++++++++ mono/DBusType/ObjectPath.cs | 110 +++++++++++++++++++++++++++++++++ mono/DBusType/String.cs | 86 ++++++++++++++++++++++++++ mono/DBusType/UInt32.cs | 87 ++++++++++++++++++++++++++ mono/DBusType/UInt64.cs | 87 ++++++++++++++++++++++++++ 14 files changed, 1268 insertions(+) create mode 100644 mono/DBusType/Array.cs create mode 100644 mono/DBusType/Boolean.cs create mode 100644 mono/DBusType/Byte.cs create mode 100644 mono/DBusType/Custom.cs create mode 100644 mono/DBusType/Dict.cs create mode 100644 mono/DBusType/Double.cs create mode 100644 mono/DBusType/IDBusType.cs create mode 100644 mono/DBusType/Int32.cs create mode 100644 mono/DBusType/Int64.cs create mode 100644 mono/DBusType/Nil.cs create mode 100644 mono/DBusType/ObjectPath.cs create mode 100644 mono/DBusType/String.cs create mode 100644 mono/DBusType/UInt32.cs create mode 100644 mono/DBusType/UInt64.cs (limited to 'mono/DBusType') diff --git a/mono/DBusType/Array.cs b/mono/DBusType/Array.cs new file mode 100644 index 00000000..3bce3afa --- /dev/null +++ b/mono/DBusType/Array.cs @@ -0,0 +1,130 @@ +using System; +using System.Collections; +using System.Runtime.InteropServices; +using System.Reflection.Emit; + +using DBus; + +namespace DBus.DBusType +{ + /// + /// Array. + /// + public class Array : IDBusType + { + public const char Code = 'a'; + private System.Array val; + private ArrayList elements; + private Type elementType; + + private Array() + { + } + + public Array(System.Array val) + { + this.val = val; + this.elementType = Arguments.MatchType(val.GetType().UnderlyingSystemType); + } + + public Array(IntPtr iter) + { + IntPtr arrayIter = Marshal.AllocCoTaskMem(Arguments.DBusMessageIterSize); + + int elementTypeCode; + dbus_message_iter_init_array_iterator(iter, arrayIter, out elementTypeCode); + this.elementType = (Type) Arguments.DBusTypes[(char) elementTypeCode]; + + elements = new ArrayList(); + + do { + object [] pars = new Object[1]; + pars[0] = arrayIter; + DBusType.IDBusType dbusType = (DBusType.IDBusType) Activator.CreateInstance(elementType, pars); + elements.Add(dbusType); + } while (dbus_message_iter_next(arrayIter)); + + Marshal.FreeCoTaskMem(arrayIter); + } + + public void Append(IntPtr iter) + { + IntPtr arrayIter = Marshal.AllocCoTaskMem(Arguments.DBusMessageIterSize); + + if (!dbus_message_iter_append_array(iter, + arrayIter, + (int) Arguments.GetCode(this.elementType))) { + throw new ApplicationException("Failed to append INT32 argument:" + val); + } + + foreach (object element in this.val) { + object [] pars = new Object[1]; + pars[0] = element; + DBusType.IDBusType dbusType = (DBusType.IDBusType) Activator.CreateInstance(elementType, pars); + dbusType.Append(arrayIter); + } + + Marshal.FreeCoTaskMem(arrayIter); + } + + public static bool Suits(System.Type type) + { + if (type.IsArray) { + return true; + } + + return false; + } + + public static void EmitMarshalIn(ILGenerator generator, Type type) + { + if (type.IsByRef) { + generator.Emit(OpCodes.Ldind_Ref); + } + } + + public static void EmitMarshalOut(ILGenerator generator, Type type, bool isReturn) + { + generator.Emit(OpCodes.Castclass, type); + if (!isReturn) { + generator.Emit(OpCodes.Stind_Ref); + } + } + + public object Get() + { + throw new ArgumentException("Cannot call Get on an Array without specifying type."); + } + + public object Get(System.Type type) + { + if (Arguments.Suits(elementType, type.UnderlyingSystemType)) { + this.val = System.Array.CreateInstance(type.UnderlyingSystemType, elements.Count); + int i = 0; + foreach (DBusType.IDBusType element in elements) { + this.val.SetValue(element.Get(type.UnderlyingSystemType), i++); + } + } else { + throw new ArgumentException("Cannot cast DBus.Type.Array to type '" + type.ToString() + "'"); + } + + return this.val; + } + + [DllImport("dbus-1")] + private extern static void dbus_message_iter_init_array_iterator(IntPtr iter, + IntPtr arrayIter, + out int elementType); + + [DllImport("dbus-1")] + private extern static bool dbus_message_iter_append_array(IntPtr iter, + IntPtr arrayIter, + int elementType); + + [DllImport("dbus-1")] + private extern static bool dbus_message_iter_has_next(IntPtr iter); + + [DllImport("dbus-1")] + private extern static bool dbus_message_iter_next(IntPtr iter); + } +} diff --git a/mono/DBusType/Boolean.cs b/mono/DBusType/Boolean.cs new file mode 100644 index 00000000..ef8ed498 --- /dev/null +++ b/mono/DBusType/Boolean.cs @@ -0,0 +1,86 @@ +using System; +using System.Runtime.InteropServices; +using System.Reflection.Emit; + +using DBus; + +namespace DBus.DBusType +{ + /// + /// Boolean + /// + public class Boolean : IDBusType + { + public const char Code = 'b'; + private System.Boolean val; + + private Boolean() + { + } + + public Boolean(System.Boolean val) + { + this.val = val; + } + + public Boolean(IntPtr iter) + { + this.val = dbus_message_iter_get_boolean(iter); + } + + public void Append(IntPtr iter) + { + if (!dbus_message_iter_append_boolean(iter, val)) + throw new ApplicationException("Failed to append BOOLEAN argument:" + val); + } + + public static bool Suits(System.Type type) + { + switch (type.ToString()) { + case "System.Boolean": + case "System.Boolean&": + return true; + } + + return false; + } + + public static void EmitMarshalIn(ILGenerator generator, Type type) + { + if (type.IsByRef) { + generator.Emit(OpCodes.Ldind_I1); + } + } + + public static void EmitMarshalOut(ILGenerator generator, Type type, bool isReturn) + { + generator.Emit(OpCodes.Unbox, type); + generator.Emit(OpCodes.Ldind_I1); + if (!isReturn) { + generator.Emit(OpCodes.Stind_I1); + } + } + + public object Get() + { + return this.val; + } + + public object Get(System.Type type) + { + switch (type.ToString()) { + case "System.Boolean": + case "System.Boolean&": + return this.val; + default: + throw new ArgumentException("Cannot cast DBus.Type.Boolean to type '" + type.ToString() + "'"); + } + } + + [DllImport("dbus-1")] + private extern static System.Boolean dbus_message_iter_get_boolean(IntPtr iter); + + [DllImport("dbus-1")] + private extern static bool dbus_message_iter_append_boolean(IntPtr iter, System.Boolean value); + } +} diff --git a/mono/DBusType/Byte.cs b/mono/DBusType/Byte.cs new file mode 100644 index 00000000..eaffd26e --- /dev/null +++ b/mono/DBusType/Byte.cs @@ -0,0 +1,86 @@ +using System; +using System.Runtime.InteropServices; +using System.Reflection.Emit; + +using DBus; + +namespace DBus.DBusType +{ + /// + /// Byte + /// + public class Byte : IDBusType + { + public const char Code = 'y'; + private System.Byte val; + + private Byte() + { + } + + public Byte(System.Byte val) + { + this.val = val; + } + + public Byte(IntPtr iter) + { + this.val = dbus_message_iter_get_byte(iter); + } + + public void Append(IntPtr iter) + { + if (!dbus_message_iter_append_byte(iter, val)) + throw new ApplicationException("Failed to append BYTE argument:" + val); + } + + public static bool Suits(System.Type type) + { + switch (type.ToString()) { + case "System.Byte": + case "System.Byte&": + return true; + } + + return false; + } + + public static void EmitMarshalIn(ILGenerator generator, Type type) + { + if (type.IsByRef) { + generator.Emit(OpCodes.Ldind_U1); + } + } + + public static void EmitMarshalOut(ILGenerator generator, Type type, bool isReturn) + { + generator.Emit(OpCodes.Unbox, type); + generator.Emit(OpCodes.Ldind_U1); + if (!isReturn) { + generator.Emit(OpCodes.Stind_I1); + } + } + + public object Get() + { + return this.val; + } + + public object Get(System.Type type) + { + switch (type.ToString()) { + case "System.Byte": + case "System.Byte&": + return this.val; + default: + throw new ArgumentException("Cannot cast DBus.Type.Byte to type '" + type.ToString() + "'"); + } + } + + [DllImport("dbus-1")] + private extern static System.Byte dbus_message_iter_get_byte(IntPtr iter); + + [DllImport("dbus-1")] + private extern static bool dbus_message_iter_append_byte(IntPtr iter, System.Byte value); + } +} diff --git a/mono/DBusType/Custom.cs b/mono/DBusType/Custom.cs new file mode 100644 index 00000000..d3eb7629 --- /dev/null +++ b/mono/DBusType/Custom.cs @@ -0,0 +1,109 @@ +using System; +using System.Runtime.InteropServices; +using System.Reflection.Emit; + +using DBus; + +namespace DBus.DBusType +{ + /// + /// A named byte array, used for custom types. + /// + public class Custom : IDBusType + { + public const char Code = 'c'; + private DBus.Custom val; + + private Custom() + { + } + + public Custom(DBus.Custom val) + { + this.val = val; + } + + public Custom(IntPtr iter) + { + string name; + IntPtr value; + int len; + + if (!dbus_message_iter_get_custom(iter, out name, out value, out len)) { + throw new ApplicationException("Failed to get CUSTOM argument."); + } + + this.val.Name = name; + this.val.Data = new byte[len]; + Marshal.Copy(value, this.val.Data, 0, len); + } + + public void Append(IntPtr iter) + { + IntPtr data = Marshal.AllocCoTaskMem(this.val.Data.Length); + try { + Marshal.Copy(this.val.Data, 0, data, this.val.Data.Length); + if (!dbus_message_iter_append_custom(iter, this.val.Name, data, this.val.Data.Length)) { + throw new ApplicationException("Failed to append CUSTOM argument:" + val); + } + } finally { + Marshal.FreeCoTaskMem(data); + } + } + + public static bool Suits(System.Type type) + { + switch (type.ToString()) { + case "DBus.Custom": + case "DBus.Custom&": + return true; + } + + return false; + } + + public static void EmitMarshalIn(ILGenerator generator, Type type) + { + if (type.IsByRef) { + generator.Emit(OpCodes.Ldobj, type); + } + } + + public static void EmitMarshalOut(ILGenerator generator, Type type, bool isReturn) + { + generator.Emit(OpCodes.Unbox, type); + generator.Emit(OpCodes.Ldobj, type); + if (!isReturn) { + generator.Emit(OpCodes.Stobj, type); + } + } + + public object Get() + { + return this.val; + } + + public object Get(System.Type type) + { + switch (type.ToString()) { + case "DBus.Custom": + case "DBus.Custom&": + return this.val; + default: + throw new ArgumentException("Cannot cast DBus.Type.Custom to type '" + type.ToString() + "'"); + } + } + + [DllImport("dbus-1")] + private extern static bool dbus_message_iter_get_custom(IntPtr iter, + out string name, + out IntPtr value, + out int len); + + [DllImport("dbus-1")] + private extern static bool dbus_message_iter_append_custom(IntPtr iter, + string name, + IntPtr data, + int len); + } +} diff --git a/mono/DBusType/Dict.cs b/mono/DBusType/Dict.cs new file mode 100644 index 00000000..e6fce159 --- /dev/null +++ b/mono/DBusType/Dict.cs @@ -0,0 +1,145 @@ +using System; +using System.Collections; +using System.Runtime.InteropServices; +using System.Reflection.Emit; + +using DBus; + +namespace DBus.DBusType +{ + /// + /// Dict. + /// + public class Dict : IDBusType + { + public const char Code = 'm'; + private Hashtable val; + + private Dict() + { + } + + public Dict(IDictionary val) + { + this.val = new Hashtable(); + foreach (DictionaryEntry entry in val) { + this.val.Add(entry.Key, entry.Value); + } + } + + public Dict(IntPtr iter) + { + IntPtr dictIter = Marshal.AllocCoTaskMem(Arguments.DBusMessageIterSize); + + dbus_message_iter_init_dict_iterator(iter, dictIter); + + this.val = new Hashtable(); + + do { + string key = dbus_message_iter_get_dict_key(dictIter); + + // Get the argument type and get the value + Type elementType = (Type) DBus.Arguments.DBusTypes[(char) dbus_message_iter_get_arg_type(dictIter)]; + object [] pars = new Object[1]; + pars[0] = dictIter; + DBusType.IDBusType dbusType = (DBusType.IDBusType) Activator.CreateInstance(elementType, pars); + this.val.Add(key, dbusType); + } while (dbus_message_iter_next(dictIter)); + + Marshal.FreeCoTaskMem(dictIter); + } + + public void Append(IntPtr iter) + { + IntPtr dictIter = Marshal.AllocCoTaskMem(Arguments.DBusMessageIterSize); + + if (!dbus_message_iter_append_dict(iter, + dictIter)) { + throw new ApplicationException("Failed to append DICT argument:" + val); + } + + foreach (DictionaryEntry entry in this.val) { + if (!dbus_message_iter_append_dict_key(dictIter, (string) entry.Key)) { + throw new ApplicationException("Failed to append DICT key:" + entry.Key); + } + + // Get the element type + Type elementType = Arguments.MatchType(entry.Value.GetType()); + object [] pars = new Object[1]; + pars[0] = entry.Value; + DBusType.IDBusType dbusType = (DBusType.IDBusType) Activator.CreateInstance(elementType, pars); + dbusType.Append(dictIter); + } + + Marshal.FreeCoTaskMem(dictIter); + } + + public static bool Suits(System.Type type) + { + if (typeof(IDictionary).IsAssignableFrom(type)) { + return true; + } + + return false; + } + + public static void EmitMarshalIn(ILGenerator generator, Type type) + { + if (type.IsByRef) { + generator.Emit(OpCodes.Ldind_Ref); + } + } + + public static void EmitMarshalOut(ILGenerator generator, Type type, bool isReturn) + { + generator.Emit(OpCodes.Castclass, type); + if (!isReturn) { + generator.Emit(OpCodes.Stind_Ref); + } + } + + public object Get() + { + return Get(typeof(Hashtable)); + } + + public object Get(System.Type type) + { + IDictionary retVal; + + if (Suits(type)) { + retVal = (IDictionary) Activator.CreateInstance(type, new object[0]); + foreach (DictionaryEntry entry in this.val) { + retVal.Add(entry.Key, ((IDBusType) entry.Value).Get()); + } + } else { + throw new ArgumentException("Cannot cast DBus.Type.Dict to type '" + type.ToString() + "'"); + } + + return retVal; + } + + [DllImport("dbus-1")] + private extern static void dbus_message_iter_init_dict_iterator(IntPtr iter, + IntPtr dictIter); + + [DllImport("dbus-1")] + private extern static bool dbus_message_iter_append_dict(IntPtr iter, + IntPtr dictIter); + + [DllImport("dbus-1")] + private extern static bool dbus_message_iter_has_next(IntPtr iter); + + [DllImport("dbus-1")] + private extern static bool dbus_message_iter_next(IntPtr iter); + + [DllImport("dbus-1")] + private extern static string dbus_message_iter_get_dict_key (IntPtr dictIter); + + [DllImport("dbus-1")] + private extern static bool dbus_message_iter_append_dict_key (IntPtr dictIter, + string value); + [DllImport("dbus-1")] + private extern static int dbus_message_iter_get_arg_type(IntPtr iter); + } +} diff --git a/mono/DBusType/Double.cs b/mono/DBusType/Double.cs new file mode 100644 index 00000000..d578822f --- /dev/null +++ b/mono/DBusType/Double.cs @@ -0,0 +1,86 @@ +using System; +using System.Runtime.InteropServices; +using System.Reflection.Emit; + +using DBus; + +namespace DBus.DBusType +{ + /// + /// IEEE 754 double + /// + public class Double : IDBusType + { + public const char Code = 'd'; + private System.Double val; + + private Double() + { + } + + public Double(System.Double val) + { + this.val = val; + } + + public Double(IntPtr iter) + { + this.val = dbus_message_iter_get_double(iter); + } + + public void Append(IntPtr iter) + { + if (!dbus_message_iter_append_double(iter, val)) + throw new ApplicationException("Failed to append DOUBLE argument:" + val); + } + + public static bool Suits(System.Type type) + { + switch (type.ToString()) { + case "System.Double": + case "System.Double&": + return true; + } + + return false; + } + + public static void EmitMarshalIn(ILGenerator generator, Type type) + { + if (type.IsByRef) { + generator.Emit(OpCodes.Ldind_R8); + } + } + + public static void EmitMarshalOut(ILGenerator generator, Type type, bool isReturn) + { + generator.Emit(OpCodes.Unbox, type); + generator.Emit(OpCodes.Ldind_R8); + if (!isReturn) { + generator.Emit(OpCodes.Stind_R8); + } + } + + public object Get() + { + return this.val; + } + + public object Get(System.Type type) + { + switch (type.ToString()) { + case "System.Double": + case "System.Double&": + return this.val; + default: + throw new ArgumentException("Cannot cast DBus.Type.Double to type '" + type.ToString() + "'"); + } + } + + [DllImport("dbus-1")] + private extern static System.Double dbus_message_iter_get_double(IntPtr iter); + + [DllImport("dbus-1")] + private extern static bool dbus_message_iter_append_double(IntPtr iter, System.Double value); + } +} diff --git a/mono/DBusType/IDBusType.cs b/mono/DBusType/IDBusType.cs new file mode 100644 index 00000000..447c8208 --- /dev/null +++ b/mono/DBusType/IDBusType.cs @@ -0,0 +1,16 @@ +using System; + +namespace DBus.DBusType +{ + /// + /// Base class for DBusTypes + /// + public interface IDBusType + { + object Get(); + + object Get(System.Type type); + + void Append(IntPtr iter); + } +} diff --git a/mono/DBusType/Int32.cs b/mono/DBusType/Int32.cs new file mode 100644 index 00000000..b617a9a0 --- /dev/null +++ b/mono/DBusType/Int32.cs @@ -0,0 +1,86 @@ +using System; +using System.Runtime.InteropServices; +using System.Reflection.Emit; + +using DBus; + +namespace DBus.DBusType +{ + /// + /// 32-bit integer. + /// + public class Int32 : IDBusType + { + public const char Code = 'i'; + private System.Int32 val; + + private Int32() + { + } + + public Int32(System.Int32 val) + { + this.val = val; + } + + public Int32(IntPtr iter) + { + this.val = dbus_message_iter_get_int32(iter); + } + + public void Append(IntPtr iter) + { + if (!dbus_message_iter_append_int32(iter, val)) + throw new ApplicationException("Failed to append INT32 argument:" + val); + } + + public static bool Suits(System.Type type) + { + switch (type.ToString()) { + case "System.Int32": + case "System.Int32&": + return true; + } + + return false; + } + + public static void EmitMarshalIn(ILGenerator generator, Type type) + { + if (type.IsByRef) { + generator.Emit(OpCodes.Ldind_I4); + } + } + + public static void EmitMarshalOut(ILGenerator generator, Type type, bool isReturn) + { + generator.Emit(OpCodes.Unbox, type); + generator.Emit(OpCodes.Ldind_I4); + if (!isReturn) { + generator.Emit(OpCodes.Stind_I4); + } + } + + public object Get() + { + return this.val; + } + + public object Get(System.Type type) + { + switch (type.ToString()) { + case "System.Int32": + case "System.Int32&": + return this.val; + default: + throw new ArgumentException("Cannot cast DBus.Type.Int32 to type '" + type.ToString() + "'"); + } + } + + [DllImport("dbus-1")] + private extern static System.Int32 dbus_message_iter_get_int32(IntPtr iter); + + [DllImport("dbus-1")] + private extern static bool dbus_message_iter_append_int32(IntPtr iter, System.Int32 value); + } +} diff --git a/mono/DBusType/Int64.cs b/mono/DBusType/Int64.cs new file mode 100644 index 00000000..0905af74 --- /dev/null +++ b/mono/DBusType/Int64.cs @@ -0,0 +1,86 @@ +using System; +using System.Runtime.InteropServices; +using System.Reflection.Emit; + +using DBus; + +namespace DBus.DBusType +{ + /// + /// 64-bit integer. + /// + public class Int64 : IDBusType + { + public const char Code = 'x'; + private System.Int64 val; + + private Int64() + { + } + + public Int64(System.Int64 val) + { + this.val = val; + } + + public Int64(IntPtr iter) + { + this.val = dbus_message_iter_get_int64(iter); + } + + public void Append(IntPtr iter) + { + if (!dbus_message_iter_append_int64(iter, val)) + throw new ApplicationException("Failed to append INT64 argument:" + val); + } + + public static bool Suits(System.Type type) + { + switch (type.ToString()) { + case "System.Int64": + case "System.Int64&": + return true; + } + + return false; + } + + public static void EmitMarshalIn(ILGenerator generator, Type type) + { + if (type.IsByRef) { + generator.Emit(OpCodes.Ldind_I8); + } + } + + public static void EmitMarshalOut(ILGenerator generator, Type type, bool isReturn) + { + generator.Emit(OpCodes.Unbox, type); + generator.Emit(OpCodes.Ldind_I8); + if (!isReturn) { + generator.Emit(OpCodes.Stind_I8); + } + } + + public object Get() + { + return this.val; + } + + public object Get(System.Type type) + { + switch (type.ToString()) { + case "System.Int64": + case "System.Int64&": + return this.val; + default: + throw new ArgumentException("Cannot cast DBus.Type.Int64 to type '" + type.ToString() + "'"); + } + } + + [DllImport("dbus-1")] + private extern static System.Int64 dbus_message_iter_get_int64(IntPtr iter); + + [DllImport("dbus-1")] + private extern static bool dbus_message_iter_append_int64(IntPtr iter, System.Int64 value); + } +} diff --git a/mono/DBusType/Nil.cs b/mono/DBusType/Nil.cs new file mode 100644 index 00000000..e39b64a9 --- /dev/null +++ b/mono/DBusType/Nil.cs @@ -0,0 +1,68 @@ +using System; +using System.Runtime.InteropServices; +using System.Reflection.Emit; + +using DBus; + +namespace DBus.DBusType +{ + /// + /// Marks a "void"/"unset"/"nonexistent"/"null" argument. + /// + public class Nil : IDBusType + { + public const char Code = 'v'; + + private Nil() + { + } + + public Nil(object nil) + { + } + + public Nil(IntPtr iter) + { + } + + public void Append(IntPtr iter) + { + if (!dbus_message_iter_append_nil(iter)) + throw new ApplicationException("Failed to append NIL argument"); + } + + public static bool Suits(System.Type type) + { + return false; + } + + public static void EmitMarshalIn(ILGenerator generator, Type type) + { + if (type.IsByRef) { + generator.Emit(OpCodes.Ldind_I1); + } + } + + public static void EmitMarshalOut(ILGenerator generator, Type type, bool isReturn) + { + generator.Emit(OpCodes.Unbox, type); + generator.Emit(OpCodes.Ldind_I1); + if (!isReturn) { + generator.Emit(OpCodes.Stind_I1); + } + } + + public object Get() + { + return null; + } + + public object Get(System.Type type) + { + throw new ArgumentException("Cannot cast DBus.Type.Nil to type '" + type.ToString() + "'"); + } + + [DllImport("dbus-1")] + private extern static bool dbus_message_iter_append_nil(IntPtr iter); + } +} diff --git a/mono/DBusType/ObjectPath.cs b/mono/DBusType/ObjectPath.cs new file mode 100644 index 00000000..e20ae18d --- /dev/null +++ b/mono/DBusType/ObjectPath.cs @@ -0,0 +1,110 @@ +using System; +using System.Runtime.InteropServices; +using System.Reflection.Emit; + +using DBus; + +namespace DBus.DBusType +{ + /// + /// An object path. + /// + public class ObjectPath : IDBusType + { + public const char Code = 'o'; + private string pathName = null; + private object val = null; + private Service service = null; + + private ObjectPath() + { + } + + public ObjectPath(object val) + { + this.val = val; + } + + public ObjectPath(IntPtr iter) + { + + this.pathName = Marshal.PtrToStringAnsi(dbus_message_iter_get_object_path(iter)); + } + + public void SetService(Service service) + { + this.service = service; + } + + private string PathName + { + get { + if (this.pathName == null && this.val != null) { + Handler handler = this.service.GetHandler(this.val); + this.pathName = handler.PathName; + } + + return this.pathName; + } + } + + public void Append(IntPtr iter) + { + if (PathName == null) { + throw new ApplicationException("Unable to append ObjectPath before calling SetService()"); + } + + if (!dbus_message_iter_append_object_path(iter, Marshal.StringToHGlobalAnsi(PathName))) + throw new ApplicationException("Failed to append OBJECT_PATH argument:" + val); + } + + public static bool Suits(System.Type type) + { + object[] attributes = type.GetCustomAttributes(typeof(InterfaceAttribute), true); + if (attributes.Length == 1) { + return true; + } else { + return false; + } + } + + public static void EmitMarshalIn(ILGenerator generator, Type type) + { + if (type.IsByRef) { + generator.Emit(OpCodes.Ldind_Ref); + } + } + + public static void EmitMarshalOut(ILGenerator generator, Type type, bool isReturn) + { + generator.Emit(OpCodes.Castclass, type); + if (!isReturn) { + generator.Emit(OpCodes.Stind_Ref); + } + } + + public object Get() + { + throw new ArgumentException("Cannot call Get on an ObjectPath without specifying type."); + } + + public object Get(System.Type type) + { + if (this.service == null) { + throw new ApplicationException("Unable to get ObjectPath before calling SetService()"); + } + + try { + return this.service.GetObject(type, PathName); + } catch(Exception ex) { + throw new ArgumentException("Cannot cast object pointed to by Object Path to type '" + type.ToString() + "': " + ex); + } + } + + [DllImport("dbus-1")] + private extern static IntPtr dbus_message_iter_get_object_path(IntPtr iter); + + [DllImport("dbus-1")] + private extern static bool dbus_message_iter_append_object_path(IntPtr iter, IntPtr pathName); + } +} diff --git a/mono/DBusType/String.cs b/mono/DBusType/String.cs new file mode 100644 index 00000000..1eda1f25 --- /dev/null +++ b/mono/DBusType/String.cs @@ -0,0 +1,86 @@ +using System; +using System.Runtime.InteropServices; +using System.Reflection.Emit; + +using DBus; + +namespace DBus.DBusType +{ + /// + /// A string. + /// + public class String : IDBusType + { + public const char Code = 's'; + private string val; + + private String() + { + } + + public String(string val) + { + this.val = val; + } + + public String(IntPtr iter) + { + this.val = Marshal.PtrToStringAnsi(dbus_message_iter_get_string(iter)); + } + + public void Append(IntPtr iter) + { + if (!dbus_message_iter_append_string(iter, Marshal.StringToHGlobalAnsi(val))) + throw new ApplicationException("Failed to append STRING argument:" + val); + } + + public static bool Suits(System.Type type) + { + switch (type.ToString()) { + case "System.String": + case "System.String&": + return true; + } + + return false; + } + + public static void EmitMarshalIn(ILGenerator generator, Type type) + { + if (type.IsByRef) { + generator.Emit(OpCodes.Ldind_Ref); + } + } + + public static void EmitMarshalOut(ILGenerator generator, Type type, bool isReturn) + { + generator.Emit(OpCodes.Castclass, type); + if (!isReturn) { + generator.Emit(OpCodes.Stind_Ref); + } + } + + public object Get() + { + return this.val; + } + + public object Get(System.Type type) + { + switch (type.ToString()) + { + case "System.String": + case "System.String&": + return this.val; + default: + throw new ArgumentException("Cannot cast DBus.Type.String to type '" + type.ToString() + "'"); + } + } + + [DllImport("dbus-1")] + private extern static IntPtr dbus_message_iter_get_string(IntPtr iter); + + [DllImport("dbus-1")] + private extern static bool dbus_message_iter_append_string(IntPtr iter, IntPtr value); + } +} diff --git a/mono/DBusType/UInt32.cs b/mono/DBusType/UInt32.cs new file mode 100644 index 00000000..9c0e350a --- /dev/null +++ b/mono/DBusType/UInt32.cs @@ -0,0 +1,87 @@ +using System; +using System.Runtime.InteropServices; +using System.Reflection.Emit; + +using DBus; + +namespace DBus.DBusType +{ + /// + /// 32-bit unsigned integer. + /// + public class UInt32 : IDBusType + { + public const char Code = 'u'; + private System.UInt32 val; + + private UInt32() + { + } + + public UInt32(System.UInt32 val) + { + this.val = val; + } + + public UInt32(IntPtr iter) + { + this.val = dbus_message_iter_get_uint32(iter); + } + + public void Append(IntPtr iter) + { + if (!dbus_message_iter_append_uint32(iter, val)) + throw new ApplicationException("Failed to append UINT32 argument:" + val); + } + + public static bool Suits(System.Type type) + { + switch (type.ToString()) { + case "System.UInt32": + case "System.UInt32&": + return true; + } + + return false; + } + + public static void EmitMarshalIn(ILGenerator generator, Type type) + { + if (type.IsByRef) { + generator.Emit(OpCodes.Ldind_U4); + } + } + + public static void EmitMarshalOut(ILGenerator generator, Type type, bool isReturn) + { + generator.Emit(OpCodes.Unbox, type); + generator.Emit(OpCodes.Ldind_U4); + if (!isReturn) { + generator.Emit(OpCodes.Stind_I4); + } + } + + public object Get() + { + return this.val; + } + + public object Get(System.Type type) + { + switch (type.ToString()) + { + case "System.UInt32": + case "System.UInt32&": + return this.val; + default: + throw new ArgumentException("Cannot cast DBus.Type.UInt32 to type '" + type.ToString() + "'"); + } + } + + [DllImport("dbus-1")] + private extern static System.UInt32 dbus_message_iter_get_uint32(IntPtr iter); + + [DllImport("dbus-1")] + private extern static bool dbus_message_iter_append_uint32(IntPtr iter, System.UInt32 value); + } +} diff --git a/mono/DBusType/UInt64.cs b/mono/DBusType/UInt64.cs new file mode 100644 index 00000000..2e474795 --- /dev/null +++ b/mono/DBusType/UInt64.cs @@ -0,0 +1,87 @@ +using System; +using System.Runtime.InteropServices; +using System.Reflection.Emit; + +using DBus; + +namespace DBus.DBusType +{ + /// + /// 64-bit unsigned integer. + /// + public class UInt64 : IDBusType + { + public const char Code = 't'; + private System.UInt64 val; + + private UInt64() + { + } + + public UInt64(System.UInt64 val) + { + this.val = val; + } + + public UInt64(IntPtr iter) + { + this.val = dbus_message_iter_get_uint64(iter); + } + + public void Append(IntPtr iter) + { + if (!dbus_message_iter_append_uint64(iter, val)) + throw new ApplicationException("Failed to append UINT64 argument:" + val); + } + + public static bool Suits(System.Type type) + { + switch (type.ToString()) { + case "System.UInt64": + case "System.UInt64&": + return true; + } + + return false; + } + + public static void EmitMarshalIn(ILGenerator generator, Type type) + { + if (type.IsByRef) { + generator.Emit(OpCodes.Ldind_I8); + } + } + + public static void EmitMarshalOut(ILGenerator generator, Type type, bool isReturn) + { + generator.Emit(OpCodes.Unbox, type); + generator.Emit(OpCodes.Ldind_I8); + if (!isReturn) { + generator.Emit(OpCodes.Stind_I8); + } + } + + public object Get() + { + return this.val; + } + + public object Get(System.Type type) + { + switch (type.ToString()) + { + case "System.UInt64": + case "System.UInt64&": + return this.val; + default: + throw new ArgumentException("Cannot cast DBus.Type.UInt64 to type '" + type.ToString() + "'"); + } + } + + [DllImport("dbus-1")] + private extern static System.UInt64 dbus_message_iter_get_uint64(IntPtr iter); + + [DllImport("dbus-1")] + private extern static bool dbus_message_iter_append_uint64(IntPtr iter, System.UInt64 value); + } +} -- cgit