Copyright and namespace.
2 // Copyright (C) 2014-2015 Stéphane Lenclud.
4 // This file is part of SharpLibHid.
6 // SharpDisplayManager is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation, either version 3 of the License, or
9 // (at your option) any later version.
11 // SharpDisplayManager is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with SharpDisplayManager. If not, see <http://www.gnu.org/licenses/>.
21 using System.Runtime.InteropServices;
22 using System.Diagnostics;
23 using System.Windows.Forms;
28 /// Provide some utility functions for raw input handling.
30 static public class RawInput
35 /// <param name="aRawInputHandle"></param>
36 /// <param name="aRawInput"></param>
37 /// <param name="rawInputBuffer">Caller must free up memory on the pointer using Marshal.FreeHGlobal</param>
38 /// <returns></returns>
39 public static bool GetRawInputData(IntPtr aRawInputHandle, ref RAWINPUT aRawInput, ref IntPtr rawInputBuffer)
42 rawInputBuffer = IntPtr.Zero;
47 uint sizeOfHeader = (uint)Marshal.SizeOf(typeof(RAWINPUTHEADER));
49 //Get the size of our raw input data.
50 Win32.Function.GetRawInputData(aRawInputHandle, Const.RID_INPUT, IntPtr.Zero, ref dwSize, sizeOfHeader);
52 //Allocate a large enough buffer
53 rawInputBuffer = Marshal.AllocHGlobal((int)dwSize);
55 //Now read our RAWINPUT data
56 if (Win32.Function.GetRawInputData(aRawInputHandle, Const.RID_INPUT, rawInputBuffer, ref dwSize, (uint)Marshal.SizeOf(typeof(RAWINPUTHEADER))) != dwSize)
62 aRawInput = (RAWINPUT)Marshal.PtrToStructure(rawInputBuffer, typeof(RAWINPUT));
66 Debug.WriteLine("GetRawInputData failed!");
76 /// <param name="hDevice"></param>
77 /// <param name="deviceInfo"></param>
78 /// <returns></returns>
79 public static bool GetDeviceInfo(IntPtr hDevice, ref RID_DEVICE_INFO deviceInfo)
82 IntPtr deviceInfoBuffer = IntPtr.Zero;
86 uint deviceInfoSize = (uint)Marshal.SizeOf(typeof(RID_DEVICE_INFO));
87 deviceInfoBuffer = Marshal.AllocHGlobal((int)deviceInfoSize);
89 int res = Win32.Function.GetRawInputDeviceInfo(hDevice, Win32.RawInputDeviceInfoType.RIDI_DEVICEINFO, deviceInfoBuffer, ref deviceInfoSize);
92 Debug.WriteLine("WM_INPUT could not read device info: " + Marshal.GetLastWin32Error().ToString());
97 deviceInfo = (RID_DEVICE_INFO)Marshal.PtrToStructure(deviceInfoBuffer, typeof(RID_DEVICE_INFO));
101 Debug.WriteLine("GetRawInputData failed!");
106 //Always executes, prevents memory leak
107 Marshal.FreeHGlobal(deviceInfoBuffer);
115 /// Fetch pre-parsed data corresponding to HID descriptor for the given HID device.
117 /// <param name="device"></param>
118 /// <returns></returns>
119 public static IntPtr GetPreParsedData(IntPtr hDevice)
122 int result = Win32.Function.GetRawInputDeviceInfo(hDevice, RawInputDeviceInfoType.RIDI_PREPARSEDDATA, IntPtr.Zero, ref ppDataSize);
125 Debug.WriteLine("Failed to get raw input pre-parsed data size: " + result + " : " + Marshal.GetLastWin32Error());
129 IntPtr ppData = Marshal.AllocHGlobal((int)ppDataSize);
130 result = Win32.Function.GetRawInputDeviceInfo(hDevice, RawInputDeviceInfoType.RIDI_PREPARSEDDATA, ppData, ref ppDataSize);
133 Debug.WriteLine("Failed to get raw input pre-parsed data: " + result + " : " + Marshal.GetLastWin32Error());
142 /// <param name="device"></param>
143 /// <returns></returns>
144 public static string GetDeviceName(IntPtr device)
146 uint deviceNameSize = 256;
147 int result = Win32.Function.GetRawInputDeviceInfo(device, RawInputDeviceInfoType.RIDI_DEVICENAME, IntPtr.Zero, ref deviceNameSize);
153 IntPtr deviceName = Marshal.AllocHGlobal((int)deviceNameSize * 2); // size is the character count not byte count
156 result = Win32.Function.GetRawInputDeviceInfo(device, RawInputDeviceInfoType.RIDI_DEVICENAME, deviceName, ref deviceNameSize);
159 return Marshal.PtrToStringAnsi(deviceName, result - 1); // -1 for NULL termination
166 Marshal.FreeHGlobal(deviceName);
172 /// Populate the given tree-view control with our Raw Input Devices.
174 /// <param name="aTreeView"></param>
175 public static void PopulateDeviceList(TreeView aTreeView)
178 //Get our list of devices
179 RAWINPUTDEVICELIST[] ridList = null;
180 uint deviceCount = 0;
181 int res = Win32.Function.GetRawInputDeviceList(ridList, ref deviceCount,(uint)Marshal.SizeOf(typeof(RAWINPUTDEVICELIST)));
188 ridList = new RAWINPUTDEVICELIST[deviceCount];
189 res = Win32.Function.GetRawInputDeviceList(ridList, ref deviceCount, (uint)Marshal.SizeOf(typeof(RAWINPUTDEVICELIST)));
190 if (res != deviceCount)
196 //For each our device add a node to our treeview
197 foreach (RAWINPUTDEVICELIST device in ridList)
199 SharpLibHid.HidDevice hidDevice=new SharpLibHid.HidDevice(device.hDevice);
201 TreeNode node = null;
202 if (hidDevice.Product != null && hidDevice.Product.Length > 1)
204 //Add the devices with a proper name at the top
205 node = aTreeView.Nodes.Insert(0, hidDevice.Name, hidDevice.FriendlyName);
209 //Add other once at the bottom
210 node = aTreeView.Nodes.Add(hidDevice.Name, hidDevice.FriendlyName);
213 node.Nodes.Add("Manufacturer: " + hidDevice.Manufacturer);
214 node.Nodes.Add("Product ID: 0x" + hidDevice.ProductId.ToString("X4"));
215 node.Nodes.Add("Vendor ID: 0x" + hidDevice.VendorId.ToString("X4"));
216 node.Nodes.Add("Version: " + hidDevice.Version);
217 node.Nodes.Add(hidDevice.Info.dwType.ToString());
218 if (hidDevice.Info.dwType == RawInputDeviceType.RIM_TYPEHID)
220 node.Nodes.Add("UsagePage / UsageCollection: 0x" + hidDevice.Info.hid.usUsagePage.ToString("X4") + " / 0x" + hidDevice.Info.hid.usUsage.ToString("X4"));
223 if (hidDevice.InputCapabilitiesDescription != null)
225 node.Nodes.Add(hidDevice.InputCapabilitiesDescription);
229 node.Nodes.Add("Button Count: " + hidDevice.ButtonCount);
231 //Those can be joystick/gamepad axis
232 if (hidDevice.InputValueCapabilities != null)
234 foreach (HIDP_VALUE_CAPS caps in hidDevice.InputValueCapabilities)
236 string des = SharpLibHid.HidDevice.InputValueCapabilityDescription(caps);
245 node.Nodes.Add(hidDevice.Name);