Consolidating our HID input report parsing.
1.1 --- a/HumanInterfaceDevice.cs Fri Dec 05 22:50:39 2014 +0100
1.2 +++ b/HumanInterfaceDevice.cs Fri Dec 05 23:18:13 2014 +0100
1.3 @@ -247,16 +247,6 @@
1.4 public enum ConsumerControl: ushort
1.5 {
1.6 /// <summary>
1.7 - /// Alternative code for properties.
1.8 - /// Also supported by Windows Media Center.
1.9 - /// </summary>
1.10 - MceProperties = 0x000A,
1.11 - /// <summary>
1.12 - /// Alternative code for program guide.
1.13 - /// Also supported by Windows Media Center.
1.14 - /// </summary>
1.15 - MceProgramGuide = 0x0012,
1.16 - /// <summary>
1.17 /// Sent by MCE remotes.
1.18 /// </summary>
1.19 MediaSelectProgramGuide = 0x008D,
2.1 --- a/RemoteControlDevice.cs Fri Dec 05 22:50:39 2014 +0100
2.2 +++ b/RemoteControlDevice.cs Fri Dec 05 23:18:13 2014 +0100
2.3 @@ -331,7 +331,7 @@
2.4 if (this.ButtonPressed != null)
2.5 {
2.6 RemoteControlButton button=RemoteControlButton.Unknown;
2.7 - if (aUsage == (ushort)ConsumerControl.AppCtrlProperties || aUsage == (ushort)ConsumerControl.MceProperties)
2.8 + if (aUsage == (ushort)ConsumerControl.AppCtrlProperties)
2.9 {
2.10 button = RemoteControlButton.MoreInfo;
2.11 }
2.12 @@ -339,7 +339,7 @@
2.13 {
2.14 button = RemoteControlButton.Print;
2.15 }
2.16 - else if (aUsage == (ushort)ConsumerControl.MediaSelectProgramGuide || aUsage == (ushort)ConsumerControl.MceProgramGuide)
2.17 + else if (aUsage == (ushort)ConsumerControl.MediaSelectProgramGuide)
2.18 {
2.19 button = RemoteControlButton.Guide;
2.20 }
2.21 @@ -456,7 +456,9 @@
2.22 //Allocate a buffer for one HID input
2.23 byte[] hidInputReport = new byte[rawInput.hid.dwSizeHid];
2.24
2.25 - //For each HID input in our raw input
2.26 + Debug.WriteLine("Raw input contains " + rawInput.hid.dwCount + " HID input report(s)");
2.27 +
2.28 + //For each HID input report in our raw input
2.29 for (int i = 0; i < rawInput.hid.dwCount; i++)
2.30 {
2.31 //Compute the address from which to copy our HID input
2.32 @@ -471,52 +473,29 @@
2.33 //Copy HID input into our buffer
2.34 Marshal.Copy(new IntPtr(hidInputOffset), hidInputReport, 0, (int)rawInput.hid.dwSizeHid);
2.35
2.36 - //Print HID raw input in our debug output
2.37 - string hidDump = "HID " + rawInput.hid.dwCount + "/" + rawInput.hid.dwSizeHid + ":";
2.38 + //Print HID input report in our debug output
2.39 + string hidDump = "HID input report: ";
2.40 foreach (byte b in hidInputReport)
2.41 {
2.42 hidDump += b.ToString("X2");
2.43 }
2.44 Debug.WriteLine(hidDump);
2.45
2.46 - ushort usage = 0;
2.47 - //hidInput[0] //Not sure what's the meaning of the code at offset 0
2.48 - if (hidInputReport.Length == 2)
2.49 - {
2.50 - //Single byte code
2.51 - usage = hidInputReport[1]; //Get button code
2.52 - }
2.53 - else if (hidInputReport.Length > 2) //Defensive
2.54 - {
2.55 - //Assuming double bytes code
2.56 - usage = (ushort)((hidInputReport[2] << 8) + hidInputReport[1]);
2.57 - }
2.58 -
2.59 - Debug.WriteLine("Usage: 0x" + usage.ToString("X4"));
2.60 -
2.61 //Proper parsing now
2.62 - //byte[] report = new byte[input.data.hid.dwSizeHid];
2.63 - //Marshal.Copy(IntPtr.Add(rawInput, HID_INPUT_DATA_OFFSET), report, 0, report.Length);
2.64 - uint usageCount = rawInput.hid.dwCount;
2.65 + uint usageCount = 1; //Assuming a single usage per input report. Is that correct?
2.66 Win32.USAGE_AND_PAGE[] usages = new Win32.USAGE_AND_PAGE[usageCount];
2.67 Win32.HidStatus status = Win32.Function.HidP_GetUsagesEx(Win32.HIDP_REPORT_TYPE.HidP_Input, 0, usages, ref usageCount, preParsedData, hidInputReport, (uint)hidInputReport.Length);
2.68 if (status != Win32.HidStatus.HIDP_STATUS_SUCCESS)
2.69 {
2.70 Debug.WriteLine("Could not parse HID data!");
2.71 - //this.LogError("Twinhan HID remote: failed to get raw input HID usages, device = {0}, status = {1}", device.Name, status);
2.72 - //Dump.DumpBinary(rawInput, (int)input.header.dwSize);
2.73 - //return false;
2.74 }
2.75 else
2.76 {
2.77 Debug.WriteLine("UsagePage: 0x" + usages[0].UsagePage.ToString("X4"));
2.78 Debug.WriteLine("Usage: 0x" + usages[0].Usage.ToString("X4"));
2.79 + //Call on our Usage Page handler
2.80 + usagePageHandler(usages[0].Usage);
2.81 }
2.82 -
2.83 -
2.84 -
2.85 - //Call on our Usage Page handler
2.86 - usagePageHandler(usage);
2.87 }
2.88
2.89 }