Adding missing entries in consumer usage table.
Adding magic MyTv code in MCE usage table as handled by MP.
2 using System.Windows.Forms;
3 using System.Runtime.InteropServices;
4 using System.Diagnostics;
6 using Microsoft.Win32.SafeHandles;
8 using System.Collections.Generic;
14 /// Represent a HID event.
18 public bool IsValid { get; private set; }
19 public bool IsForeground { get; private set; }
20 public bool IsBackground { get{return !IsForeground;} }
21 public bool IsMouse { get; private set; }
22 public bool IsKeyboard { get; private set; }
23 public bool IsGeneric { get; private set; }
24 public bool IsButtonDown { get { return Usages.Count == 1 && Usages[0] != 0; } }
25 public bool IsButtonUp { get { return Usages.Count == 1 && Usages[0] == 0; } }
27 public HidDevice Device { get; private set; }
29 public ushort UsagePage { get; private set; }
30 public ushort UsageCollection { get; private set; }
31 public uint UsageId { get { return ((uint)UsagePage << 16 | (uint)UsageCollection); } }
32 public List<ushort> Usages { get; private set; }
36 /// Initialize an HidEvent from a WM_INPUT message
38 /// <param name="hRawInputDevice">Device Handle as provided by RAWINPUTHEADER.hDevice, typically accessed as rawinput.header.hDevice</param>
39 public HidEvent(Message aMessage)
45 Usages = new List<ushort>();
47 if (aMessage.Msg != Const.WM_INPUT)
49 //Has to be a WM_INPUT message
53 if (Macro.GET_RAWINPUT_CODE_WPARAM(aMessage.WParam) == Const.RIM_INPUT)
57 else if (Macro.GET_RAWINPUT_CODE_WPARAM(aMessage.WParam) == Const.RIM_INPUTSINK)
62 //Declare some pointers
63 IntPtr rawInputBuffer = IntPtr.Zero;
64 //My understanding is that this is basically our HID descriptor
65 IntPtr preParsedData = IntPtr.Zero;
70 RAWINPUT rawInput = new RAWINPUT();
71 if (!RawInput.GetRawInputData(aMessage.LParam, ref rawInput, ref rawInputBuffer))
77 RID_DEVICE_INFO deviceInfo = new RID_DEVICE_INFO();
78 if (!RawInput.GetDeviceInfo(rawInput.header.hDevice, ref deviceInfo))
83 //Get various information about this HID device
84 Device = new Hid.HidDevice(rawInput.header.hDevice);
86 if (rawInput.header.dwType == Const.RIM_TYPEHID) //Check that our raw input is HID
90 Debug.WriteLine("WM_INPUT source device is HID.");
91 //Get Usage Page and Usage
92 //Debug.WriteLine("Usage Page: 0x" + deviceInfo.hid.usUsagePage.ToString("X4") + " Usage ID: 0x" + deviceInfo.hid.usUsage.ToString("X4"));
93 UsagePage = deviceInfo.hid.usUsagePage;
94 UsageCollection = deviceInfo.hid.usUsage;
96 preParsedData = RawInput.GetPreParsedData(rawInput.header.hDevice);
98 if (!(rawInput.hid.dwSizeHid > 1 //Make sure our HID msg size more than 1. In fact the first ushort is irrelevant to us for now
99 && rawInput.hid.dwCount > 0)) //Check that we have at least one HID msg
104 //Allocate a buffer for one HID input
105 byte[] hidInputReport = new byte[rawInput.hid.dwSizeHid];
107 Debug.WriteLine("Raw input contains " + rawInput.hid.dwCount + " HID input report(s)");
109 //For each HID input report in our raw input
110 for (int i = 0; i < rawInput.hid.dwCount; i++)
112 //Compute the address from which to copy our HID input
113 int hidInputOffset = 0;
116 byte* source = (byte*)rawInputBuffer;
117 source += sizeof(RAWINPUTHEADER) + sizeof(RAWHID) + (rawInput.hid.dwSizeHid * i);
118 hidInputOffset = (int)source;
121 //Copy HID input into our buffer
122 Marshal.Copy(new IntPtr(hidInputOffset), hidInputReport, 0, (int)rawInput.hid.dwSizeHid);
124 //Print HID input report in our debug output
125 string hidDump = "HID input report: ";
126 foreach (byte b in hidInputReport)
128 hidDump += b.ToString("X2");
130 Debug.WriteLine(hidDump);
133 uint usageCount = 1; //Assuming a single usage per input report. Is that correct?
134 Win32.USAGE_AND_PAGE[] usages = new Win32.USAGE_AND_PAGE[usageCount];
135 Win32.HidStatus status = Win32.Function.HidP_GetUsagesEx(Win32.HIDP_REPORT_TYPE.HidP_Input, 0, usages, ref usageCount, preParsedData, hidInputReport, (uint)hidInputReport.Length);
136 if (status != Win32.HidStatus.HIDP_STATUS_SUCCESS)
138 Debug.WriteLine("Could not parse HID data!");
142 //Debug.WriteLine("UsagePage: 0x" + usages[0].UsagePage.ToString("X4"));
143 //Debug.WriteLine("Usage: 0x" + usages[0].Usage.ToString("X4"));
144 //Add this usage to our list
145 Usages.Add(usages[0].Usage);
150 else if (rawInput.header.dwType == Const.RIM_TYPEMOUSE)
154 Debug.WriteLine("WM_INPUT source device is Mouse.");
155 // do mouse handling...
157 else if (rawInput.header.dwType == Const.RIM_TYPEKEYBOARD)
161 Debug.WriteLine("WM_INPUT source device is Keyboard.");
162 // do keyboard handling...
163 Debug.WriteLine("Type: " + deviceInfo.keyboard.dwType.ToString());
164 Debug.WriteLine("SubType: " + deviceInfo.keyboard.dwSubType.ToString());
165 Debug.WriteLine("Mode: " + deviceInfo.keyboard.dwKeyboardMode.ToString());
166 Debug.WriteLine("Number of function keys: " + deviceInfo.keyboard.dwNumberOfFunctionKeys.ToString());
167 Debug.WriteLine("Number of indicators: " + deviceInfo.keyboard.dwNumberOfIndicators.ToString());
168 Debug.WriteLine("Number of keys total: " + deviceInfo.keyboard.dwNumberOfKeysTotal.ToString());
173 //Always executed when leaving our try block
174 Marshal.FreeHGlobal(rawInputBuffer);
175 Marshal.FreeHGlobal(preParsedData);
182 /// Print information about this device to our debug output.
184 public void DebugWrite()
188 Debug.WriteLine("==== Invalid HidEvent");
192 if (IsGeneric) Debug.WriteLine("==== Generic");
193 if (IsKeyboard) Debug.WriteLine("==== Keyboard");
194 if (IsMouse) Debug.WriteLine("==== Mouse");
195 Debug.WriteLine("==== Foreground: " + IsForeground.ToString());
196 Debug.WriteLine("==== UsagePage: 0x" + UsagePage.ToString("X4"));
197 Debug.WriteLine("==== UsageCollection: 0x" + UsageCollection.ToString("X4"));
198 foreach (ushort usage in Usages)
200 Debug.WriteLine("==== Usage: 0x" + usage.ToString("X4"));