2005-11-18 Aaron Bockover <aaron@aaronbock.net>

* src/Makefile.am: Added rule to build HalTest.cs
    * src/HalContext.cs: Removed destructor, must use Dispose()
    * src/*.cs: Tabs->Spaces, reformatted code a little


svn path=/trunk/hal-sharp/; revision=53248
This commit is contained in:
Aaron Bockover 2005-11-18 21:11:50 +00:00
Родитель fcf32b6dca
Коммит 9f380a670b
11 изменённых файлов: 906 добавлений и 1034 удалений

Просмотреть файл

@ -1,3 +1,9 @@
2005-11-18 Aaron Bockover <aaron@aaronbock.net>
* src/Makefile.am: Added rule to build HalTest.cs
* src/HalContext.cs: Removed destructor, must use Dispose()
* src/*.cs: Tabs->Spaces, reformatted code a little
2005-07-17 Aaron Bockover <aaron@aaronbock.net>
* HalTest.cs: GTK test for events using main context

Просмотреть файл

@ -1,5 +1,5 @@
<Project name="hal-sharp" fileversion="2.0" language="C#" ctype="DotNetProject">
<Configurations>
<Configurations active="Debug">
<Configuration name="Debug" ctype="DotNetProjectConfiguration">
<Output directory="./src" assembly="hal-sharp" />
<Build debugmode="True" target="Exe" />
@ -13,11 +13,6 @@
<CodeGeneration compiler="Csc" warninglevel="4" optimize="True" unsafecodeallowed="False" generateoverflowchecks="True" generatexmldocumentation="False" ctype="CSharpCompilerParameters" />
</Configuration>
</Configurations>
<References>
<ProjectReference type="Gac" localcopy="True" refto="Mono.Posix, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=0738eb9f132ed756" />
<ProjectReference type="Gac" localcopy="True" refto="gtk-sharp, Version=2.0.0.0, Culture=neutral, PublicKeyToken=35e10195dab3c99f" />
<ProjectReference type="Gac" localcopy="True" refto="glib-sharp, Version=2.0.0.0, Culture=neutral, PublicKeyToken=35e10195dab3c99f" />
</References>
<DeploymentInformation strategy="File">
<excludeFiles />
</DeploymentInformation>
@ -31,4 +26,8 @@
<File name="./src/HalDefines.cs" subtype="Code" buildaction="Compile" />
<File name="./src/HalCallbacks.cs" subtype="Code" buildaction="Compile" />
</Contents>
<References>
<ProjectReference type="Gac" localcopy="True" refto="Mono.Posix, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=0738eb9f132ed756" />
<ProjectReference type="Gac" localcopy="True" refto="gtk-sharp, Version=2.0.0.0, Culture=neutral, PublicKeyToken=35e10195dab3c99f" />
</References>
</Project>

Просмотреть файл

@ -1,10 +1,10 @@
<Combine name="Hal" fileversion="2.0">
<Configurations>
<Configurations active="Debug">
<Configuration name="Debug" ctype="CombineConfiguration">
<Entry build="True" name="hal-sharp" />
<Entry configuration="Debug" build="True" name="hal-sharp" />
</Configuration>
<Configuration name="Release" ctype="CombineConfiguration">
<Entry build="True" name="hal-sharp" />
<Entry configuration="Release" build="True" name="hal-sharp" />
</Configuration>
</Configurations>
<StartMode startupentry="hal-sharp" single="True">

Просмотреть файл

@ -32,28 +32,22 @@ using System.Runtime.InteropServices;
namespace Hal
{
// Raw HAL Callbacks
public delegate void DeviceAddedCallback(IntPtr ctx, IntPtr udi);
public delegate void DeviceRemovedCallback(IntPtr ctx, IntPtr udi);
public delegate void DeviceNewCapabilityCallback(IntPtr ctx, IntPtr udi,
IntPtr capability);
public delegate void DeviceLostCapabilityCallback(IntPtr ctx, IntPtr udi,
IntPtr capability);
public delegate void DevicePropertyModifiedCallback(IntPtr ctx, IntPtr udi,
internal delegate void DeviceAddedCallback(IntPtr ctx, IntPtr udi);
internal delegate void DeviceRemovedCallback(IntPtr ctx, IntPtr udi);
internal delegate void DeviceNewCapabilityCallback(IntPtr ctx, IntPtr udi, IntPtr capability);
internal delegate void DeviceLostCapabilityCallback(IntPtr ctx, IntPtr udi, IntPtr capability);
internal delegate void DevicePropertyModifiedCallback(IntPtr ctx, IntPtr udi,
IntPtr key, bool is_removed, bool is_added);
public delegate void DeviceConditionCallback(IntPtr ctx, IntPtr udi,
internal delegate void DeviceConditionCallback(IntPtr ctx, IntPtr udi,
IntPtr condition_name, IntPtr condition_details);
// Managed Event Handlers
public delegate void DeviceAddedHandler(object o, DeviceAddedArgs args);
public delegate void DeviceRemovedHandler(object o, DeviceRemovedArgs args);
public delegate void DeviceNewCapabilityHandler(object o,
DeviceNewCapabilityArgs args);
public delegate void DeviceLostCapabilityHandler(object o,
DeviceLostCapabilityArgs args);
public delegate void DevicePropertyModifiedHandler(object o,
DevicePropertyModifiedArgs args);
public delegate void DeviceConditionHandler(object o,
DeviceConditionArgs args);
public delegate void DeviceNewCapabilityHandler(object o, DeviceNewCapabilityArgs args);
public delegate void DeviceLostCapabilityHandler(object o, DeviceLostCapabilityArgs args);
public delegate void DevicePropertyModifiedHandler(object o, DevicePropertyModifiedArgs args);
public delegate void DeviceConditionHandler(object o, DeviceConditionArgs args);
public class DeviceAddedArgs : EventArgs
{

Просмотреть файл

@ -47,15 +47,15 @@ namespace Hal
protected Context(IntPtr dbus_conn)
{
IntPtr ctx = Unmanaged.libhal_ctx_new();
if(ctx == IntPtr.Zero)
if(ctx == IntPtr.Zero) {
throw new HalException("Could not create HAL Context");
}
ctx_handle = new HandleRef(this, ctx);
DbusConnection = dbus_conn;
}
public Context(DbusBusType type) :
this(Unmanaged.dbus_bus_get(type, IntPtr.Zero))
public Context(DbusBusType type) : this(Unmanaged.dbus_bus_get(type, IntPtr.Zero))
{
Initialize();
}
@ -65,17 +65,7 @@ namespace Hal
Initialize();
}
~Context()
{
Cleanup();
}
public void Dispose()
{
Cleanup();
}
private void Cleanup()
{
ContextShutdown();
ContextFree();
@ -83,8 +73,9 @@ namespace Hal
private bool ContextShutdown()
{
if(ctx_handle.Handle == IntPtr.Zero || !initialized)
if(ctx_handle.Handle == IntPtr.Zero || !initialized) {
return false;
}
return Unmanaged.libhal_ctx_shutdown(ctx_handle, IntPtr.Zero);
}
@ -100,42 +91,37 @@ namespace Hal
public void IntegrateMainLoop()
{
// would be nice to let user specify an optional GMainContext
Unmanaged.dbus_connection_setup_with_g_main(dbus_conn,
Unmanaged.g_main_context_default());
Unmanaged.dbus_connection_setup_with_g_main(dbus_conn, Unmanaged.g_main_context_default());
mainLoopIntegrated = true;
}
public void Initialize()
{
if(!Unmanaged.libhal_ctx_init(ctx_handle, IntPtr.Zero))
if(!Unmanaged.libhal_ctx_init(ctx_handle, IntPtr.Zero)) {
throw new HalException("Could not initialize HAL Context");
}
initialized = true;
}
public IntPtr DbusConnection
{
public IntPtr DbusConnection {
set {
dbus_conn = value;
if(!Unmanaged.libhal_ctx_set_dbus_connection(ctx_handle,
dbus_conn))
throw new HalException(
"Could not set D-Bus Connection for HAL");
if(!Unmanaged.libhal_ctx_set_dbus_connection(ctx_handle, dbus_conn)) {
throw new HalException("Could not set D-Bus Connection for HAL");
}
}
}
public bool UseCache
{
public bool UseCache {
set {
if(!Unmanaged.libhal_ctx_set_cache(ctx_handle, value))
throw new HalException("Could not set D-Bus Use Cache to '"
+ value + "'");
if(!Unmanaged.libhal_ctx_set_cache(ctx_handle, value)) {
throw new HalException("Could not set D-Bus Use Cache to '" + value + "'");
}
}
}
public HandleRef Raw
{
public HandleRef Raw {
get {
return ctx_handle;
}
@ -147,29 +133,33 @@ namespace Hal
private bool AddEvent(Type evType, object ev)
{
if(EventTable[evType] == null)
if(EventTable[evType] == null) {
EventTable[evType] = new ArrayList();
}
(EventTable[evType] as ArrayList).Add(ev);
if(!mainLoopIntegrated)
if(!mainLoopIntegrated) {
IntegrateMainLoop();
}
return (EventTable[evType] as ArrayList).Count == 1;
}
private void RemoveEvent(Type evType, object ev)
{
if(EventTable[evType] == null)
if(EventTable[evType] == null) {
return;
}
(EventTable[evType] as ArrayList).Remove(ev);
}
private ArrayList GetEvents(Type evType)
{
if(EventTable[evType] == null)
if(EventTable[evType] == null) {
return null;
}
return EventTable[evType] as ArrayList;
}
@ -177,10 +167,11 @@ namespace Hal
public event DeviceAddedHandler DeviceAdded
{
add {
if(AddEvent(typeof(DeviceAddedHandler), value))
if(AddEvent(typeof(DeviceAddedHandler), value)) {
Unmanaged.libhal_ctx_set_device_added(ctx_handle,
new DeviceAddedCallback(OnHalDeviceAdded));
}
}
remove {
RemoveEvent(typeof(DeviceAddedHandler), value);
@ -190,10 +181,11 @@ namespace Hal
public event DeviceRemovedHandler DeviceRemoved
{
add {
if(AddEvent(typeof(DeviceRemovedHandler), value))
if(AddEvent(typeof(DeviceRemovedHandler), value)) {
Unmanaged.libhal_ctx_set_device_removed(ctx_handle,
new DeviceRemovedCallback(OnHalDeviceRemoved));
}
}
remove {
RemoveEvent(typeof(DeviceRemovedHandler), value);
@ -203,10 +195,10 @@ namespace Hal
public event DeviceNewCapabilityHandler DeviceNewCapability
{
add {
if(AddEvent(typeof(DeviceNewCapabilityHandler), value))
if(AddEvent(typeof(DeviceNewCapabilityHandler), value)) {
Unmanaged.libhal_ctx_set_device_new_capability(ctx_handle,
new DeviceNewCapabilityCallback(
OnHalDeviceNewCapability));
new DeviceNewCapabilityCallback(OnHalDeviceNewCapability));
}
}
remove {
@ -217,10 +209,10 @@ namespace Hal
public event DeviceLostCapabilityHandler DeviceLostCapability
{
add {
if(AddEvent(typeof(DeviceLostCapabilityHandler), value))
if(AddEvent(typeof(DeviceLostCapabilityHandler), value)) {
Unmanaged.libhal_ctx_set_device_lost_capability(ctx_handle,
new DeviceLostCapabilityCallback(
OnHalDeviceLostCapability));
new DeviceLostCapabilityCallback(OnHalDeviceLostCapability));
}
}
remove {
@ -231,10 +223,10 @@ namespace Hal
public event DevicePropertyModifiedHandler DevicePropertyModified
{
add {
if(AddEvent(typeof(DevicePropertyModifiedHandler), value))
if(AddEvent(typeof(DevicePropertyModifiedHandler), value)) {
Unmanaged.libhal_ctx_set_device_property_modified(
ctx_handle, new DevicePropertyModifiedCallback(
OnHalDevicePropertyModified));
ctx_handle, new DevicePropertyModifiedCallback(OnHalDevicePropertyModified));
}
}
remove {
@ -245,10 +237,11 @@ namespace Hal
public event DeviceConditionHandler DeviceCondition
{
add {
if(AddEvent(typeof(DeviceConditionHandler), value))
if(AddEvent(typeof(DeviceConditionHandler), value)) {
Unmanaged.libhal_ctx_set_device_condition(ctx_handle,
new DeviceConditionCallback(OnHalDeviceCondition));
}
}
remove {
RemoveEvent(typeof(DeviceConditionHandler), value);
@ -257,8 +250,7 @@ namespace Hal
private void OnHalDeviceAdded(IntPtr ctx, IntPtr udiPtr)
{
foreach(DeviceAddedHandler addedHandler in
GetEvents(typeof(DeviceAddedHandler))) {
foreach(DeviceAddedHandler addedHandler in GetEvents(typeof(DeviceAddedHandler))) {
DeviceAddedHandler handler = addedHandler;
if(handler != null) {
@ -272,8 +264,7 @@ namespace Hal
private void OnHalDeviceRemoved(IntPtr ctx, IntPtr udiPtr)
{
foreach(DeviceRemovedHandler removedHandler in
GetEvents(typeof(DeviceRemovedHandler))) {
foreach(DeviceRemovedHandler removedHandler in GetEvents(typeof(DeviceRemovedHandler))) {
DeviceRemovedHandler handler = removedHandler;
if(handler != null) {
@ -288,8 +279,7 @@ namespace Hal
private void OnHalDeviceNewCapability(IntPtr ctx, IntPtr udiPtr,
IntPtr capPtr)
{
foreach(DeviceNewCapabilityHandler newCapHandler in
GetEvents(typeof(DeviceNewCapabilityHandler))) {
foreach(DeviceNewCapabilityHandler newCapHandler in GetEvents(typeof(DeviceNewCapabilityHandler))) {
DeviceNewCapabilityHandler handler = newCapHandler;
if(handler != null) {
@ -307,8 +297,7 @@ namespace Hal
private void OnHalDeviceLostCapability(IntPtr ctx, IntPtr udiPtr,
IntPtr capPtr)
{
foreach(DeviceLostCapabilityHandler lostCapHandler in
GetEvents(typeof(DeviceLostCapabilityHandler))) {
foreach(DeviceLostCapabilityHandler lostCapHandler in GetEvents(typeof(DeviceLostCapabilityHandler))) {
DeviceLostCapabilityHandler handler = lostCapHandler;
if(handler != null) {
@ -326,8 +315,7 @@ namespace Hal
private void OnHalDevicePropertyModified(IntPtr ctx, IntPtr udiPtr,
IntPtr keyPtr, bool isRemoved, bool isAdded)
{
foreach(DevicePropertyModifiedHandler propModHandler in
GetEvents(typeof(DevicePropertyModifiedHandler))) {
foreach(DevicePropertyModifiedHandler propModHandler in GetEvents(typeof(DevicePropertyModifiedHandler))) {
DevicePropertyModifiedHandler handler = propModHandler;
if(handler != null) {
@ -347,8 +335,7 @@ namespace Hal
private void OnHalDeviceCondition(IntPtr ctx, IntPtr udiPtr,
IntPtr namePtr, IntPtr detailsPtr)
{
foreach(DeviceConditionHandler condHandler in
GetEvents(typeof(DeviceConditionHandler))) {
foreach(DeviceConditionHandler condHandler in GetEvents(typeof(DeviceConditionHandler))) {
DeviceConditionHandler handler = condHandler;
if(handler != null) {

Просмотреть файл

@ -56,8 +56,7 @@ namespace Hal
public bool PropertyExists(string key)
{
return Unmanaged.libhal_device_property_exists(ctx.Raw,
udi, key, IntPtr.Zero);
return Unmanaged.libhal_device_property_exists(ctx.Raw, udi, key, IntPtr.Zero);
}
public string [] GetPropertyStringList(string key)
@ -65,8 +64,7 @@ namespace Hal
IntPtr ptr;
string [] properties;
ptr = Unmanaged.libhal_device_get_property_strlist(ctx.Raw,
udi, key, IntPtr.Zero);
ptr = Unmanaged.libhal_device_get_property_strlist(ctx.Raw, udi, key, IntPtr.Zero);
properties = UnixMarshal.PtrToStringArray(ptr);
Unmanaged.libhal_free_string_array(ptr);
@ -75,32 +73,27 @@ namespace Hal
public bool PropertyStringListAppend(string key, string val)
{
return Unmanaged.libhal_device_property_strlist_append(ctx.Raw,
udi, key, val, IntPtr.Zero);
return Unmanaged.libhal_device_property_strlist_append(ctx.Raw, udi, key, val, IntPtr.Zero);
}
public bool PropertyStringListPrepend(string key, string val)
{
return Unmanaged.libhal_device_property_strlist_append(ctx.Raw,
udi, key, val, IntPtr.Zero);
return Unmanaged.libhal_device_property_strlist_append(ctx.Raw, udi, key, val, IntPtr.Zero);
}
public bool PropertyStringListRemove(string key, string val)
{
return Unmanaged.libhal_device_property_strlist_remove(ctx.Raw,
udi, key, val, IntPtr.Zero);
return Unmanaged.libhal_device_property_strlist_remove(ctx.Raw, udi, key, val, IntPtr.Zero);
}
public bool PropertyStringListRemoveIndex(string key, uint index)
{
return Unmanaged.libhal_device_property_strlist_remove_index(ctx.Raw,
udi, key, index, IntPtr.Zero);
return Unmanaged.libhal_device_property_strlist_remove_index(ctx.Raw, udi, key, index, IntPtr.Zero);
}
public string GetPropertyString(string key)
{
IntPtr ptr = Unmanaged.libhal_device_get_property_string(ctx.Raw,
udi, key, IntPtr.Zero);
IntPtr ptr = Unmanaged.libhal_device_get_property_string(ctx.Raw, udi, key, IntPtr.Zero);
string str = Marshal.PtrToStringAnsi(ptr);
Unmanaged.libhal_free_string(ptr);
return str;
@ -108,77 +101,65 @@ namespace Hal
public bool SetPropertyString(string key, string val)
{
return Unmanaged.libhal_device_set_property_string(ctx.Raw,
udi, key, val, IntPtr.Zero);
return Unmanaged.libhal_device_set_property_string(ctx.Raw, udi, key, val, IntPtr.Zero);
}
public int GetPropertyInt(string key)
{
return Unmanaged.libhal_device_get_property_int(ctx.Raw,
udi, key, IntPtr.Zero);
return Unmanaged.libhal_device_get_property_int(ctx.Raw, udi, key, IntPtr.Zero);
}
public bool SetPropertyInt(string key, int val)
{
return Unmanaged.libhal_device_set_property_int(ctx.Raw,
udi, key, val, IntPtr.Zero);
return Unmanaged.libhal_device_set_property_int(ctx.Raw, udi, key, val, IntPtr.Zero);
}
public UInt64 GetPropertyUint64(string key)
{
return Unmanaged.libhal_device_get_property_uint64(ctx.Raw,
udi, key, IntPtr.Zero);
return Unmanaged.libhal_device_get_property_uint64(ctx.Raw, udi, key, IntPtr.Zero);
}
public bool SetPropertyString(string key, UInt64 val)
{
return Unmanaged.libhal_device_set_property_uint64(ctx.Raw,
udi, key, val, IntPtr.Zero);
return Unmanaged.libhal_device_set_property_uint64(ctx.Raw, udi, key, val, IntPtr.Zero);
}
public double GetPropertyDouble(string key)
{
return Unmanaged.libhal_device_get_property_double(ctx.Raw,
udi, key, IntPtr.Zero);
return Unmanaged.libhal_device_get_property_double(ctx.Raw, udi, key, IntPtr.Zero);
}
public bool SetPropertyDouble(string key, double val)
{
return Unmanaged.libhal_device_set_property_double(ctx.Raw,
udi, key, val, IntPtr.Zero);
return Unmanaged.libhal_device_set_property_double(ctx.Raw, udi, key, val, IntPtr.Zero);
}
public bool GetPropertyBool(string key)
{
return Unmanaged.libhal_device_get_property_bool(ctx.Raw,
udi, key, IntPtr.Zero);
return Unmanaged.libhal_device_get_property_bool(ctx.Raw, udi, key, IntPtr.Zero);
}
public bool SetPropertyBool(string key, bool val)
{
return Unmanaged.libhal_device_set_property_bool(ctx.Raw,
udi, key, val, IntPtr.Zero);
return Unmanaged.libhal_device_set_property_bool(ctx.Raw, udi, key, val, IntPtr.Zero);
}
public bool AddCapability(string capability)
{
return Unmanaged.libhal_device_add_capability(ctx.Raw,
udi, capability, IntPtr.Zero);
return Unmanaged.libhal_device_add_capability(ctx.Raw, udi, capability, IntPtr.Zero);
}
public bool QueryCapability(string capability)
{
return Unmanaged.libhal_device_query_capability(ctx.Raw,
udi, capability, IntPtr.Zero);
return Unmanaged.libhal_device_query_capability(ctx.Raw, udi, capability, IntPtr.Zero);
}
public void Lock(string reason)
{
string reason_why_locked;
if(!Unmanaged.libhal_device_lock(ctx.Raw, udi, reason,
out reason_why_locked, IntPtr.Zero))
throw new HalException("Could not lock device: "
+ reason_why_locked);
if(!Unmanaged.libhal_device_lock(ctx.Raw, udi, reason, out reason_why_locked, IntPtr.Zero)) {
throw new HalException("Could not lock device: " + reason_why_locked);
}
}
public bool Unlock()
@ -188,8 +169,7 @@ namespace Hal
public bool EmitCondition(string conditionName, string conditionDetails)
{
return Unmanaged.libhal_device_emit_condition(ctx.Raw, udi,
conditionName, conditionDetails, IntPtr.Zero);
return Unmanaged.libhal_device_emit_condition(ctx.Raw, udi, conditionName, conditionDetails, IntPtr.Zero);
}
public bool Rescan()
@ -208,15 +188,13 @@ 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);
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);
if(!result)
throw new HalException("Could not " + (value ? "add"
: "remove") + " property watch");
if(!result) {
throw new HalException("Could not " + (value ? "add" : "remove") + " property watch");
}
}
}
@ -227,9 +205,9 @@ namespace Hal
}
set {
if(!SetPropertyString(key, value))
throw new HalException("Could not set property '"
+ key + "'");
if(!SetPropertyString(key, value)) {
throw new HalException("Could not set property '" + key + "'");
}
}
}
@ -238,15 +216,13 @@ namespace Hal
return udi;
}
public string Udi
{
public string Udi {
get {
return udi;
}
}
public bool Exists
{
public bool Exists {
get {
return Unmanaged.libhal_device_exists(ctx.Raw,
udi, IntPtr.Zero);
@ -260,8 +236,7 @@ namespace Hal
return Unmanaged.libhal_device_exists(ctx.Raw, udi, IntPtr.Zero);
}
public static string [] FindUdis(Context ctx, FindBy findMethod,
string key, string query)
public static string [] FindUdis(Context ctx, FindBy findMethod, string key, string query)
{
IntPtr ptr;
string [] deviceUdis;
@ -293,8 +268,9 @@ namespace Hal
{
Device [] devices = new Device[udis.Length];
for(int i = 0; i < udis.Length; i++)
for(int i = 0; i < udis.Length; i++) {
devices[i] = new Device(ctx, udis[i]);
}
return devices;
}

Просмотреть файл

@ -28,127 +28,13 @@
using System;
using Hal;
using Gtk;
using System.Collections;
public class Entry
{
public static void Main()
{
Application.Init();
new CdromTest();
//new GtkTest();
Application.Run();
/* Context ctx = new Context(); */
/*foreach(Device device in Device.FindByCapability(ctx, "net"))
device.Print();*/
/*foreach(Device device in Device.GetAll(ctx))
device.Print();*/
/*foreach(Device device in Device.FindByStringMatch(ctx, "info.vendor", "Intel Corporation"))
device.Print();*/
/*Device device = new Device(ctx, "/org/freedesktop/Hal/devices/net_00_05_4e_42_57_6b");
string [] capabilities = device.GetPropertyStringList("info.capabilities");
foreach(string cap in capabilities)
Console.WriteLine(cap);*/
}
}
public class CdromTest
{
private Context ctx;
private ArrayList disks = new ArrayList();
public CdromTest()
{
ctx = new Context();
ctx.DeviceAdded += OnDeviceAdded;
ctx.DeviceRemoved += OnDeviceRemoved;
}
private void OnDeviceAdded(object o, DeviceAddedArgs args)
{
Device parentDevice = new Device(ctx, args.Device["info.parent"]);
if(parentDevice["storage.drive_type"].Equals("cdrom")) {
Console.WriteLine("Found drive with disk: {0} ({1})",
parentDevice,
parentDevice["storage.model"]);
}
}
private void OnDeviceRemoved(object o, DeviceRemovedArgs args)
{
if(disks.IndexOf(args.Device.Udi) >= 0) {
Console.WriteLine("Disk removed: {0}", args.Device);
disks.Remove(args.Device.Udi);
}
}
}
public class GtkTest
{
private Window win;
private Context ctx;
public GtkTest()
{
ctx = new Context();
ctx.DeviceAdded += OnHalDeviceAdded;
ctx.DeviceRemoved += OnHalDeviceRemoved;
ctx.DeviceCondition += OnHalDeviceCondition;
ctx.DeviceLostCapability += OnHalDeviceLostCapability;
ctx.DeviceNewCapability += OnHalDeviceNewCapability;
ctx.DevicePropertyModified += OnHalDevicePropertyModified;
win = new Window("HAL Event Loop Test");
win.DeleteEvent += OnWindowDeleteEvent;
win.ShowAll();
}
public void OnHalDeviceAdded(object o, DeviceAddedArgs args)
{
Console.WriteLine("Device Added: " + args.Device);
args.Device.WatchProperties = true;
}
public void OnHalDeviceRemoved(object o, DeviceRemovedArgs args)
{
Console.WriteLine("Device Removed: " + args.Device);
}
public void OnHalDeviceCondition(object o, DeviceConditionArgs args)
{
Console.WriteLine("Device Condition: " + args.Device);
}
public void OnHalDeviceLostCapability(object o,
DeviceLostCapabilityArgs args)
{
Console.WriteLine("Device Lost Capability: " + args.Device);
}
public void OnHalDeviceNewCapability(object o,
DeviceNewCapabilityArgs args)
{
Console.WriteLine("Device New Capability: " + args.Device);
}
public void OnHalDevicePropertyModified(object o,
DevicePropertyModifiedArgs args)
{
Console.WriteLine("Device Property Modified: " + args.Device +
", Key: " + args.Key + ", Is Removed: " + args.IsRemoved +
", Is Added: " + args.IsAdded);
}
public void OnWindowDeleteEvent(object o, EventArgs args)
{
Application.Quit();
Context ctx = new Context();
ctx.Dispose();
}
}

Просмотреть файл

@ -256,9 +256,24 @@ namespace Hal
public static extern void dbus_connection_setup_with_g_main(
IntPtr dbus_connection, IntPtr g_main_context);
[DllImport("libdbus-glib-1")]
public static extern void dbus_g_thread_init();
// ughhh
[DllImport("libglib-2.0.so")]
public static extern IntPtr g_main_context_default();
[DllImport("libglib-2.0.so")]
public static extern IntPtr g_main_context_new();
[DllImport("libgobject-2.0.so")]
public static extern void g_type_init();
[DllImport("libglib-2.0.so")]
public static extern void g_main_context_iteration(IntPtr ctx,
bool mayblock);
[DllImport("libglib-2.0.so")]
public static extern bool g_main_context_pending(IntPtr ctx);
}
}

Просмотреть файл

@ -1,8 +1,9 @@
MCS_FLAGS=-debug
halsharpdir=$(pkglibdir)
halsharp_SCRIPTS = hal-sharp.dll
EXTRA_DIST = $(halsharp_sources) HalTest.cs
CLEANFILES = hal-sharp.dll AssemblyInfo.cs
DISTCLEANFILES = Makefile.in *.exe *.mdb
CLEANFILES = hal-sharp.dll hal-sharp.exe
DISTCLEANFILES = Makefile.in *.mdb AssemblyInfo.cs
halsharp_sources = \
HalContext.cs \
@ -15,6 +16,14 @@ halsharp_sources = \
halsharp_build_sources = $(addprefix $(srcdir)/, $(halsharp_sources))
hal-sharp.dll:
$(MCS) -target:library -out:$@ -r:Mono.Posix $(halsharp_build_sources)
all: hal-sharp.dll hal-sharp.exe
hal-sharp.dll:
$(MCS) $(MCS_FLAGS) -target:library -out:$@ -r:Mono.Posix $(halsharp_build_sources)
hal-sharp.exe:
$(MCS) $(MCS_FLAGS) -r:hal-sharp.dll -out:$@ HalTest.cs
run:
mono hal-sharp.exe