RemoteControlDevice.cs
changeset 17 8f7e35c3bfd1
parent 16 9a3e77655031
child 18 24ac84ab9620
     1.1 --- a/RemoteControlDevice.cs	Wed Dec 03 21:54:45 2014 +0100
     1.2 +++ b/RemoteControlDevice.cs	Fri Dec 05 22:50:39 2014 +0100
     1.3 @@ -395,8 +395,10 @@
     1.4                  Debug.WriteLine("================BACKGROUND");
     1.5              }
     1.6  
     1.7 -            //Declare a pointer
     1.8 +            //Declare some pointers
     1.9              IntPtr rawInputBuffer = IntPtr.Zero;
    1.10 +            //My understanding is that this is basically our HID descriptor
    1.11 +            IntPtr preParsedData = IntPtr.Zero;
    1.12  
    1.13              try
    1.14              {
    1.15 @@ -421,6 +423,9 @@
    1.16                      //Get Usage Page and Usage
    1.17                      Debug.WriteLine("Usage Page: 0x" + deviceInfo.hid.usUsagePage.ToString("X4") + " Usage ID: 0x" + deviceInfo.hid.usUsage.ToString("X4"));
    1.18  
    1.19 +
    1.20 +                    preParsedData = RawInput.GetPreParsedData(rawInput.header.hDevice);
    1.21 +
    1.22                      //
    1.23                      HidUsageHandler usagePageHandler=null;
    1.24  
    1.25 @@ -449,7 +454,7 @@
    1.26  
    1.27  
    1.28                      //Allocate a buffer for one HID input
    1.29 -                    byte[] hidInput = new byte[rawInput.hid.dwSizeHid];
    1.30 +                    byte[] hidInputReport = new byte[rawInput.hid.dwSizeHid];
    1.31  
    1.32                      //For each HID input in our raw input
    1.33                      for (int i = 0; i < rawInput.hid.dwCount; i++)
    1.34 @@ -464,11 +469,11 @@
    1.35                          }
    1.36  
    1.37                          //Copy HID input into our buffer
    1.38 -                        Marshal.Copy(new IntPtr(hidInputOffset), hidInput, 0, rawInput.hid.dwSizeHid);
    1.39 +                        Marshal.Copy(new IntPtr(hidInputOffset), hidInputReport, 0, (int)rawInput.hid.dwSizeHid);
    1.40  
    1.41                          //Print HID raw input in our debug output
    1.42                          string hidDump = "HID " + rawInput.hid.dwCount + "/" + rawInput.hid.dwSizeHid + ":";
    1.43 -                        foreach (byte b in hidInput)
    1.44 +                        foreach (byte b in hidInputReport)
    1.45                          {
    1.46                              hidDump += b.ToString("X2");
    1.47                          }
    1.48 @@ -476,19 +481,40 @@
    1.49                          
    1.50                          ushort usage = 0;
    1.51                          //hidInput[0] //Not sure what's the meaning of the code at offset 0
    1.52 -                        if (hidInput.Length == 2)
    1.53 +                        if (hidInputReport.Length == 2)
    1.54                          {
    1.55                              //Single byte code
    1.56 -                            usage = hidInput[1]; //Get button code
    1.57 +                            usage = hidInputReport[1]; //Get button code
    1.58                          }
    1.59 -                        else if (hidInput.Length > 2) //Defensive
    1.60 +                        else if (hidInputReport.Length > 2) //Defensive
    1.61                          {
    1.62                              //Assuming double bytes code
    1.63 -                            usage = (ushort)((hidInput[2] << 8) + hidInput[1]);
    1.64 +                            usage = (ushort)((hidInputReport[2] << 8) + hidInputReport[1]);
    1.65                          }
    1.66  
    1.67                          Debug.WriteLine("Usage: 0x" + usage.ToString("X4"));
    1.68  
    1.69 +                        //Proper parsing now
    1.70 +                        //byte[] report = new byte[input.data.hid.dwSizeHid];
    1.71 +                        //Marshal.Copy(IntPtr.Add(rawInput, HID_INPUT_DATA_OFFSET), report, 0, report.Length);
    1.72 +                        uint usageCount = rawInput.hid.dwCount;
    1.73 +                        Win32.USAGE_AND_PAGE[] usages = new Win32.USAGE_AND_PAGE[usageCount];
    1.74 +                        Win32.HidStatus status = Win32.Function.HidP_GetUsagesEx(Win32.HIDP_REPORT_TYPE.HidP_Input, 0, usages, ref usageCount, preParsedData, hidInputReport, (uint)hidInputReport.Length);
    1.75 +                        if (status != Win32.HidStatus.HIDP_STATUS_SUCCESS)
    1.76 +                        {
    1.77 +                            Debug.WriteLine("Could not parse HID data!");
    1.78 +                            //this.LogError("Twinhan HID remote: failed to get raw input HID usages, device = {0}, status = {1}", device.Name, status);
    1.79 +                            //Dump.DumpBinary(rawInput, (int)input.header.dwSize);
    1.80 +                            //return false;
    1.81 +                        }
    1.82 +                        else
    1.83 +                        {
    1.84 +                            Debug.WriteLine("UsagePage: 0x" + usages[0].UsagePage.ToString("X4"));
    1.85 +                            Debug.WriteLine("Usage: 0x" + usages[0].Usage.ToString("X4"));
    1.86 +                        }
    1.87 +
    1.88 +
    1.89 +
    1.90                          //Call on our Usage Page handler
    1.91                          usagePageHandler(usage);
    1.92                      }
    1.93 @@ -510,6 +536,7 @@
    1.94              {
    1.95                  //Always executed when leaving our try block
    1.96                  Marshal.FreeHGlobal(rawInputBuffer);
    1.97 +                Marshal.FreeHGlobal(preParsedData);
    1.98              }
    1.99  		}
   1.100