1.1 --- a/HidEvent.cs Sat Feb 14 23:41:12 2015 +0100
1.2 +++ b/HidEvent.cs Sun Feb 15 00:04:28 2015 +0100
1.3 @@ -13,12 +13,13 @@
1.4 {
1.5 /// <summary>
1.6 /// Represent a HID event.
1.7 + /// TODO: Rename this into HidRawInput?
1.8 /// </summary>
1.9 - public class HidEvent: IDisposable
1.10 + public class HidEvent : IDisposable
1.11 {
1.12 public bool IsValid { get; private set; }
1.13 - public bool IsForeground { get; private set; }
1.14 - public bool IsBackground { get{return !IsForeground;} }
1.15 + public bool IsForeground { get; private set; }
1.16 + public bool IsBackground { get { return !IsForeground; } }
1.17 public bool IsMouse { get; private set; }
1.18 public bool IsKeyboard { get; private set; }
1.19 public bool IsGeneric { get; private set; }
1.20 @@ -33,9 +34,10 @@
1.21 public ushort UsageCollection { get; private set; }
1.22 public uint UsageId { get { return ((uint)UsagePage << 16 | (uint)UsageCollection); } }
1.23 public List<ushort> Usages { get; private set; }
1.24 - public byte[] InputReport { get; private set; }
1.25 - //
1.26 - public delegate void HidEventRepeatDelegate(HidEvent aHidEvent);
1.27 + //TODO: We need a collection of input report
1.28 + public byte[] InputReport { get; private set; }
1.29 + //
1.30 + public delegate void HidEventRepeatDelegate(HidEvent aHidEvent);
1.31 public event HidEventRepeatDelegate OnHidEventRepeat;
1.32
1.33 private System.Timers.Timer Timer { get; set; }
1.34 @@ -73,7 +75,7 @@
1.35 IsValid = false;
1.36 IsKeyboard = false;
1.37 IsGeneric = false;
1.38 -
1.39 +
1.40
1.41 Time = DateTime.Now;
1.42 OriginalTime = DateTime.Now;
1.43 @@ -111,7 +113,7 @@
1.44
1.45
1.46 //Get various information about this HID device
1.47 - Device = new Hid.HidDevice(rawInput.header.hDevice);
1.48 + Device = new Hid.HidDevice(rawInput.header.hDevice);
1.49
1.50 if (rawInput.header.dwType == Const.RIM_TYPEHID) //Check that our raw input is HID
1.51 {
1.52 @@ -130,7 +132,7 @@
1.53 }
1.54
1.55 //Allocate a buffer for one HID input
1.56 - InputReport = new byte[rawInput.hid.dwSizeHid];
1.57 + InputReport = new byte[rawInput.hid.dwSizeHid];
1.58
1.59 Debug.WriteLine("Raw input contains " + rawInput.hid.dwCount + " HID input report(s)");
1.60
1.61 @@ -147,53 +149,53 @@
1.62 }
1.63
1.64 //Copy HID input into our buffer
1.65 - Marshal.Copy(new IntPtr(hidInputOffset), InputReport, 0, (int)rawInput.hid.dwSizeHid);
1.66 + Marshal.Copy(new IntPtr(hidInputOffset), InputReport, 0, (int)rawInput.hid.dwSizeHid);
1.67
1.68 //Print HID input report in our debug output
1.69 //string hidDump = "HID input report: " + InputReportString();
1.70 //Debug.WriteLine(hidDump);
1.71
1.72 //Do proper parsing of our HID report
1.73 - //First query our usage count
1.74 - uint usageCount = 0;
1.75 + //First query our usage count
1.76 + uint usageCount = 0;
1.77 Win32.USAGE_AND_PAGE[] usages = null;
1.78 - Win32.HidStatus status = Win32.Function.HidP_GetUsagesEx(Win32.HIDP_REPORT_TYPE.HidP_Input, 0, usages, ref usageCount, Device.PreParsedData, InputReport, (uint)InputReport.Length);
1.79 - if (status == Win32.HidStatus.HIDP_STATUS_BUFFER_TOO_SMALL)
1.80 - {
1.81 - //Allocate a large enough buffer
1.82 - usages = new Win32.USAGE_AND_PAGE[usageCount];
1.83 - //...and fetch our usages
1.84 + Win32.HidStatus status = Win32.Function.HidP_GetUsagesEx(Win32.HIDP_REPORT_TYPE.HidP_Input, 0, usages, ref usageCount, Device.PreParsedData, InputReport, (uint)InputReport.Length);
1.85 + if (status == Win32.HidStatus.HIDP_STATUS_BUFFER_TOO_SMALL)
1.86 + {
1.87 + //Allocate a large enough buffer
1.88 + usages = new Win32.USAGE_AND_PAGE[usageCount];
1.89 + //...and fetch our usages
1.90 status = Win32.Function.HidP_GetUsagesEx(Win32.HIDP_REPORT_TYPE.HidP_Input, 0, usages, ref usageCount, Device.PreParsedData, InputReport, (uint)InputReport.Length);
1.91 - if (status != Win32.HidStatus.HIDP_STATUS_SUCCESS)
1.92 - {
1.93 - Debug.WriteLine("Second pass could not parse HID data: " + status.ToString());
1.94 - }
1.95 - }
1.96 - else if (status != Win32.HidStatus.HIDP_STATUS_SUCCESS)
1.97 - {
1.98 - Debug.WriteLine("First pass could not parse HID data: " + status.ToString());
1.99 - }
1.100 + if (status != Win32.HidStatus.HIDP_STATUS_SUCCESS)
1.101 + {
1.102 + Debug.WriteLine("Second pass could not parse HID data: " + status.ToString());
1.103 + }
1.104 + }
1.105 + else if (status != Win32.HidStatus.HIDP_STATUS_SUCCESS)
1.106 + {
1.107 + Debug.WriteLine("First pass could not parse HID data: " + status.ToString());
1.108 + }
1.109
1.110 - Debug.WriteLine("Usage count: " + usageCount.ToString());
1.111 + Debug.WriteLine("Usage count: " + usageCount.ToString());
1.112
1.113 //Copy usages into this event
1.114 - if (usages != null)
1.115 - {
1.116 - foreach (USAGE_AND_PAGE up in usages)
1.117 - {
1.118 - //Debug.WriteLine("UsagePage: 0x" + usages[0].UsagePage.ToString("X4"));
1.119 - //Debug.WriteLine("Usage: 0x" + usages[0].Usage.ToString("X4"));
1.120 - //Add this usage to our list
1.121 - Usages.Add(up.Usage);
1.122 - }
1.123 - }
1.124 + if (usages != null)
1.125 + {
1.126 + foreach (USAGE_AND_PAGE up in usages)
1.127 + {
1.128 + //Debug.WriteLine("UsagePage: 0x" + usages[0].UsagePage.ToString("X4"));
1.129 + //Debug.WriteLine("Usage: 0x" + usages[0].Usage.ToString("X4"));
1.130 + //Add this usage to our list
1.131 + Usages.Add(up.Usage);
1.132 + }
1.133 + }
1.134 }
1.135 }
1.136 else if (rawInput.header.dwType == Const.RIM_TYPEMOUSE)
1.137 {
1.138 IsMouse = true;
1.139
1.140 - Debug.WriteLine("WM_INPUT source device is Mouse.");
1.141 + Debug.WriteLine("WM_INPUT source device is Mouse.");
1.142 // do mouse handling...
1.143 }
1.144 else if (rawInput.header.dwType == Const.RIM_TYPEKEYBOARD)
1.145 @@ -219,10 +221,10 @@
1.146 //
1.147 if (IsButtonDown)
1.148 {
1.149 - //TODO: Make this optional
1.150 + //TODO: Make this optional
1.151 StartRepeatTimer(iRepeatDelay);
1.152 }
1.153 -
1.154 +
1.155 IsValid = true;
1.156 }
1.157
1.158 @@ -235,9 +237,9 @@
1.159 Timer.Enabled = false;
1.160 //Initial delay do not use auto reset
1.161 //After our initial delay however we do setup our timer one more time using auto reset
1.162 - Timer.AutoReset = (RepeatCount!=0);
1.163 - Timer.Interval = aInterval;
1.164 - Timer.Enabled = true;
1.165 + Timer.AutoReset = (RepeatCount != 0);
1.166 + Timer.Interval = aInterval;
1.167 + Timer.Enabled = true;
1.168 }
1.169
1.170 static private void OnRepeatTimerElapsed(object sender, ElapsedEventArgs e, HidEvent aHidEvent)
1.171 @@ -250,7 +252,7 @@
1.172
1.173 aHidEvent.RepeatCount++;
1.174 aHidEvent.Time = DateTime.Now;
1.175 - if (aHidEvent.RepeatCount==1)
1.176 + if (aHidEvent.RepeatCount == 1)
1.177 {
1.178 //Re-Start our timer only after the initial delay
1.179 aHidEvent.StartRepeatTimer(aHidEvent.iRepeatSpeed);
1.180 @@ -260,79 +262,79 @@
1.181 aHidEvent.OnHidEventRepeat(aHidEvent);
1.182 }
1.183
1.184 - /// <summary>
1.185 - /// Print information about this device to our debug output.
1.186 - /// </summary>
1.187 - public void DebugWrite()
1.188 - {
1.189 - if (!IsValid)
1.190 - {
1.191 - Debug.WriteLine("==== Invalid HidEvent");
1.192 - return;
1.193 - }
1.194 - Device.DebugWrite();
1.195 - if (IsGeneric) Debug.WriteLine("==== Generic");
1.196 - if (IsKeyboard) Debug.WriteLine("==== Keyboard");
1.197 - if (IsMouse) Debug.WriteLine("==== Mouse");
1.198 - Debug.WriteLine("==== Foreground: " + IsForeground.ToString());
1.199 - Debug.WriteLine("==== UsagePage: 0x" + UsagePage.ToString("X4"));
1.200 - Debug.WriteLine("==== UsageCollection: 0x" + UsageCollection.ToString("X4"));
1.201 - Debug.WriteLine("==== InputReport: 0x" + InputReportString());
1.202 - foreach (ushort usage in Usages)
1.203 - {
1.204 - Debug.WriteLine("==== Usage: 0x" + usage.ToString("X4"));
1.205 - }
1.206 - }
1.207 + /// <summary>
1.208 + /// Print information about this device to our debug output.
1.209 + /// </summary>
1.210 + public void DebugWrite()
1.211 + {
1.212 + if (!IsValid)
1.213 + {
1.214 + Debug.WriteLine("==== Invalid HidEvent");
1.215 + return;
1.216 + }
1.217 + Device.DebugWrite();
1.218 + if (IsGeneric) Debug.WriteLine("==== Generic");
1.219 + if (IsKeyboard) Debug.WriteLine("==== Keyboard");
1.220 + if (IsMouse) Debug.WriteLine("==== Mouse");
1.221 + Debug.WriteLine("==== Foreground: " + IsForeground.ToString());
1.222 + Debug.WriteLine("==== UsagePage: 0x" + UsagePage.ToString("X4"));
1.223 + Debug.WriteLine("==== UsageCollection: 0x" + UsageCollection.ToString("X4"));
1.224 + Debug.WriteLine("==== InputReport: 0x" + InputReportString());
1.225 + foreach (ushort usage in Usages)
1.226 + {
1.227 + Debug.WriteLine("==== Usage: 0x" + usage.ToString("X4"));
1.228 + }
1.229 + }
1.230
1.231 - /// <summary>
1.232 - ///
1.233 - /// </summary>
1.234 - /// <returns></returns>
1.235 - public string InputReportString()
1.236 - {
1.237 - string hidDump = "";
1.238 - foreach (byte b in InputReport)
1.239 - {
1.240 - hidDump += b.ToString("X2");
1.241 - }
1.242 - return hidDump;
1.243 - }
1.244 + /// <summary>
1.245 + ///
1.246 + /// </summary>
1.247 + /// <returns></returns>
1.248 + public string InputReportString()
1.249 + {
1.250 + string hidDump = "";
1.251 + foreach (byte b in InputReport)
1.252 + {
1.253 + hidDump += b.ToString("X2");
1.254 + }
1.255 + return hidDump;
1.256 + }
1.257
1.258
1.259 - /// <summary>
1.260 - /// Create a list view item describing this HidEvent
1.261 - /// </summary>
1.262 - /// <returns></returns>
1.263 + /// <summary>
1.264 + /// Create a list view item describing this HidEvent
1.265 + /// </summary>
1.266 + /// <returns></returns>
1.267 public ListViewItem ToListViewItem()
1.268 {
1.269 string usageText = "";
1.270
1.271 - foreach (ushort usage in Usages)
1.272 - {
1.273 - if (usageText != "")
1.274 - {
1.275 - //Add a separator
1.276 - usageText += ", ";
1.277 - }
1.278 + foreach (ushort usage in Usages)
1.279 + {
1.280 + if (usageText != "")
1.281 + {
1.282 + //Add a separator
1.283 + usageText += ", ";
1.284 + }
1.285
1.286 - UsagePage usagePage = (UsagePage)UsagePage;
1.287 - switch (usagePage)
1.288 - {
1.289 - case Hid.UsagePage.Consumer:
1.290 - usageText += ((Hid.UsageTables.ConsumerControl)usage).ToString();
1.291 - break;
1.292 + UsagePage usagePage = (UsagePage)UsagePage;
1.293 + switch (usagePage)
1.294 + {
1.295 + case Hid.UsagePage.Consumer:
1.296 + usageText += ((Hid.UsageTables.ConsumerControl)usage).ToString();
1.297 + break;
1.298
1.299 - case Hid.UsagePage.WindowsMediaCenterRemoteControl:
1.300 - usageText += ((Hid.UsageTables.WindowsMediaCenterRemoteControl)usage).ToString();
1.301 - break;
1.302 + case Hid.UsagePage.WindowsMediaCenterRemoteControl:
1.303 + usageText += ((Hid.UsageTables.WindowsMediaCenterRemoteControl)usage).ToString();
1.304 + break;
1.305
1.306 - default:
1.307 - usageText += usage.ToString("X2");
1.308 - break;
1.309 - }
1.310 - }
1.311 + default:
1.312 + usageText += usage.ToString("X2");
1.313 + break;
1.314 + }
1.315 + }
1.316
1.317 - ListViewItem item = new ListViewItem(new[] { usageText, InputReportString(), UsagePage.ToString("X2"), UsageCollection.ToString("X2"), RepeatCount.ToString(), Time.ToString("HH:mm:ss:fff") });
1.318 + ListViewItem item = new ListViewItem(new[] { usageText, InputReportString(), UsagePage.ToString("X2"), UsageCollection.ToString("X2"), RepeatCount.ToString(), Time.ToString("HH:mm:ss:fff") });
1.319 return item;
1.320 }
1.321