diff --git a/ChangeLog b/ChangeLog index ad0b143..6b8471b 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,12 @@ +2005-11-30 Aaron Bockover + + * src/HalDevice.cs: Use DBusError when adding/removing property watch + * src/HalTest.cs: Stripped down test + * src/Makefile.am: Added DBusError.cs + * src/DBusError.cs: Bindings to DBusError (dbus_error_init, + dbus_error_free, dbus_error_is_set) + * src/HalContext.cs: Use DBusError in places + 2005-11-26 Aaron Bockover * src/HalContext.cs: New Parent property that returns an instantiated diff --git a/hal-sharp.mdp b/hal-sharp.mdp index c0b059a..82bada3 100644 --- a/hal-sharp.mdp +++ b/hal-sharp.mdp @@ -25,6 +25,7 @@ + diff --git a/src/DBusError.cs b/src/DBusError.cs new file mode 100644 index 0000000..4433f9d --- /dev/null +++ b/src/DBusError.cs @@ -0,0 +1,82 @@ +using System; +using System.Runtime.InteropServices; +using Mono.Unix; + +namespace Hal +{ + public class DBusError : IDisposable + { + [StructLayout(LayoutKind.Sequential)] + private struct UnmanagedError + { + public IntPtr name; + public IntPtr message; + public uint dummy1; + public uint dummy2; + public uint dummy3; + public uint dummy4; + public uint dummy5; + public IntPtr padding1; + } + + [DllImport("libdbus-1")] + private static extern void dbus_error_init(IntPtr handle); + + [DllImport("libdbus-1")] + private static extern void dbus_error_free(IntPtr handle); + + [DllImport("libdbus-1")] + private static extern bool dbus_error_is_set(IntPtr handle); + + private IntPtr error_ptr; + private UnmanagedError error; + + public DBusError() + { + error = new UnmanagedError(); + error_ptr = Marshal.AllocHGlobal(Marshal.SizeOf(error)); + dbus_error_init(error_ptr); + } + + public void Dispose() + { + dbus_error_free(error_ptr); + Marshal.FreeHGlobal(error_ptr); + } + + public void ThrowExceptionIfSet(string message) + { + if(IsSet) { + message += String.Format(": [{0}] {1}", Name, Message); + Dispose(); + throw new HalException(message); + } else { + Dispose(); + } + } + + public bool IsSet { + get { + return dbus_error_is_set(error_ptr); + } + } + + public string Name { + get { + return UnixMarshal.PtrToString(error.name); + } + } + + public string Message { + get { + return UnixMarshal.PtrToString(error.message); + } + } + + public IntPtr Raw { + get { + return error_ptr; + } + } + } +} diff --git a/src/HalContext.cs b/src/HalContext.cs index 36dcc3b..156423f 100644 --- a/src/HalContext.cs +++ b/src/HalContext.cs @@ -101,10 +101,13 @@ namespace Hal public void Initialize() { - if(!Unmanaged.libhal_ctx_init(ctx_handle, IntPtr.Zero)) { - throw new HalException("Could not initialize HAL Context"); + DBusError error = new DBusError(); + if(!Unmanaged.libhal_ctx_init(ctx_handle, error.Raw)) { + error.ThrowExceptionIfSet("Could not initialize HAL Context"); + return; } + error.Dispose(); initialized = true; } diff --git a/src/HalDevice.cs b/src/HalDevice.cs index 1b0e541..fb7cd28 100644 --- a/src/HalDevice.cs +++ b/src/HalDevice.cs @@ -188,10 +188,14 @@ namespace Hal public bool WatchProperties { set { - bool result = value - ? Unmanaged.libhal_device_add_property_watch(ctx.Raw, udi, IntPtr.Zero) - : Unmanaged.libhal_device_remove_property_watch(ctx.Raw, udi, IntPtr.Zero); + DBusError error = new DBusError(); + bool result = value + ? Unmanaged.libhal_device_add_property_watch(ctx.Raw, udi, error.Raw) + : Unmanaged.libhal_device_remove_property_watch(ctx.Raw, udi, error.Raw); + + error.ThrowExceptionIfSet("Could not update watch on property"); + if(!result) { throw new HalException("Could not " + (value ? "add" : "remove") + " property watch"); } diff --git a/src/HalTest.cs b/src/HalTest.cs index 5ae5e5b..2aa223f 100644 --- a/src/HalTest.cs +++ b/src/HalTest.cs @@ -29,14 +29,10 @@ using System; using Hal; -public class Entry +public static class HalTest { - public static void Main() + public static void Main(string [] args) { - using(Context ctx = new Context()) { - foreach(Device device in Device.FindByCapability(ctx, "portable_audio_player")) { - device.Print(); - } - } + Context ctx = new Context(); } } diff --git a/src/Makefile.am b/src/Makefile.am index c45d959..a4ec7fc 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -6,6 +6,7 @@ CLEANFILES = hal-sharp.dll hal-test.exe DISTCLEANFILES = Makefile.in *.mdb AssemblyInfo.cs hal-sharp.dll.config halsharp_sources = \ + DBusError.cs \ HalContext.cs \ HalDevice.cs \ HalUnmanaged.cs \