#region Copyright(c) Travis Robinson
#endregion
using System;
using libusbK;
namespace Hot.Plug.Detect
{
internal class Program
{
#region For Window Applications
private const int WM_USER = 0x400;
private const int WM_USER_HOT_BASE = WM_USER;
private const int WM_USER_HOT_REMOVAL = WM_USER_HOT_BASE;
private const int WM_USER_HOT_ARRIVAL = WM_USER_HOT_BASE + 1;
#endregion
private static void Main()
{
KHOT_PARAMS hotInitParams = new KHOT_PARAMS();
hotInitParams.PatternMatch.DeviceInterfaceGUID = "*";
hotInitParams.Flags = KHOT_FLAG.PLUG_ALL_ON_INIT;
hotInitParams.OnHotPlug = OnHotPlug;
Console.WriteLine("Monitoring libusbK arrival/removal events.");
Console.WriteLine("[PatternMatch]");
Console.WriteLine(hotInitParams.PatternMatch.ToString());
Console.WriteLine("Press [ENTER] to exit..");
while (Console.KeyAvailable) Console.ReadKey(true);
AllKFunctions.LibK_SetDefaultContext(KLIB_HANDLE_TYPE.HOTK, new IntPtr(Int32.MaxValue));
HotK hot = new HotK(ref hotInitParams);
Console.ReadLine();
hot.Free();
}
private static void OnHotPlug(KHOT_HANDLE hotHandle,
KLST_DEVINFO_HANDLE deviceInfo,
KLST_SYNC_FLAG plugType)
{
string plugText;
int totalPluggedDeviceCount = (int) hotHandle.GetContext().ToInt64();
if (totalPluggedDeviceCount == int.MaxValue)
{
Console.WriteLine("OnHotPlug is being called for the first time on handle:{0}", hotHandle.Pointer);
totalPluggedDeviceCount = 0;
}
switch (plugType)
{
case KLST_SYNC_FLAG.ADDED:
plugText = "Arrival";
totalPluggedDeviceCount++;
break;
case KLST_SYNC_FLAG.REMOVED:
plugText = "Removal";
totalPluggedDeviceCount--;
break;
default:
throw new ArgumentOutOfRangeException("plugType");
}
hotHandle.SetContext(new IntPtr(totalPluggedDeviceCount));
Console.WriteLine("\n[OnHotPlug] Device {0}:{1} \n",
plugText,
deviceInfo);
Console.WriteLine("Total Plugged Device Count: {0}",
totalPluggedDeviceCount);
}
}
}