StephaneLenclud@76: //
StephaneLenclud@76: // Copyright (C) 2014-2015 Stéphane Lenclud.
StephaneLenclud@76: //
StephaneLenclud@76: // This file is part of SharpLibHid.
StephaneLenclud@76: //
StephaneLenclud@76: // SharpDisplayManager is free software: you can redistribute it and/or modify
StephaneLenclud@76: // it under the terms of the GNU General Public License as published by
StephaneLenclud@76: // the Free Software Foundation, either version 3 of the License, or
StephaneLenclud@76: // (at your option) any later version.
StephaneLenclud@76: //
StephaneLenclud@76: // SharpDisplayManager is distributed in the hope that it will be useful,
StephaneLenclud@76: // but WITHOUT ANY WARRANTY; without even the implied warranty of
StephaneLenclud@76: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
StephaneLenclud@76: // GNU General Public License for more details.
StephaneLenclud@76: //
StephaneLenclud@76: // You should have received a copy of the GNU General Public License
StephaneLenclud@76: // along with SharpDisplayManager. If not, see .
StephaneLenclud@76: //
StephaneLenclud@76:
StephaneLenclud@76:
sl@25: using System;
sl@25: using System.Windows.Forms;
sl@25: using System.Runtime.InteropServices;
sl@25: using System.Diagnostics;
sl@25: using System.Text;
sl@25: using Microsoft.Win32.SafeHandles;
StephaneLenclud@77: using SharpLib.Win32;
sl@25:
StephaneLenclud@77: namespace SharpLib.Hid
sl@25: {
sl@25: ///
sl@25: /// Represent a HID device.
StephaneLenclud@60: /// Rename to RawInputDevice?
sl@25: ///
StephaneLenclud@52: public class HidDevice: IDisposable
sl@25: {
StephaneLenclud@63: ///
StephaneLenclud@63: /// Unique name of that HID device.
StephaneLenclud@63: /// Notably used as input to CreateFile.
StephaneLenclud@63: ///
sl@25: public string Name { get; private set; }
StephaneLenclud@63:
StephaneLenclud@63: ///
StephaneLenclud@63: /// Friendly name that people should be able to read.
StephaneLenclud@63: ///
StephaneLenclud@63: public string FriendlyName { get; private set; }
StephaneLenclud@63:
StephaneLenclud@63: //
sl@25: public string Manufacturer { get; private set; }
sl@25: public string Product { get; private set; }
sl@25: public ushort VendorId { get; private set; }
sl@25: public ushort ProductId { get; private set; }
sl@25: public ushort Version { get; private set; }
StephaneLenclud@56: //Pre-parsed HID descriptor
StephaneLenclud@52: public IntPtr PreParsedData {get; private set;}
StephaneLenclud@56: //Info
StephaneLenclud@53: public RID_DEVICE_INFO Info { get {return iInfo;} }
StephaneLenclud@53: private RID_DEVICE_INFO iInfo;
StephaneLenclud@56: //Capabilities
StephaneLenclud@56: public HIDP_CAPS Capabilities { get { return iCapabilities; } }
StephaneLenclud@56: private HIDP_CAPS iCapabilities;
StephaneLenclud@64: public string InputCapabilitiesDescription { get; private set; }
StephaneLenclud@56: //Input Button Capabilities
StephaneLenclud@56: public HIDP_BUTTON_CAPS[] InputButtonCapabilities { get { return iInputButtonCapabilities; } }
StephaneLenclud@56: private HIDP_BUTTON_CAPS[] iInputButtonCapabilities;
StephaneLenclud@58: //Input Value Capabilities
StephaneLenclud@58: public HIDP_VALUE_CAPS[] InputValueCapabilities { get { return iInputValueCapabilities; } }
StephaneLenclud@58: private HIDP_VALUE_CAPS[] iInputValueCapabilities;
StephaneLenclud@58:
StephaneLenclud@72: //
StephaneLenclud@72: public int ButtonCount { get; private set; }
StephaneLenclud@72:
sl@25: ///
sl@26: /// Class constructor will fetch this object properties from HID sub system.
sl@25: ///
sl@26: /// Device Handle as provided by RAWINPUTHEADER.hDevice, typically accessed as rawinput.header.hDevice
sl@25: public HidDevice(IntPtr hRawInputDevice)
sl@25: {
StephaneLenclud@55: //Try construct and rollback if needed
StephaneLenclud@55: try
StephaneLenclud@55: {
StephaneLenclud@55: Construct(hRawInputDevice);
StephaneLenclud@55: }
StephaneLenclud@55: catch (System.Exception ex)
StephaneLenclud@55: {
StephaneLenclud@55: //Just rollback and propagate
StephaneLenclud@55: Dispose();
StephaneLenclud@55: throw ex;
StephaneLenclud@55: }
StephaneLenclud@55: }
StephaneLenclud@55:
StephaneLenclud@55:
StephaneLenclud@55: ///
StephaneLenclud@55: /// Make sure dispose is called even if the user forgot about it.
StephaneLenclud@55: ///
StephaneLenclud@55: ~HidDevice()
StephaneLenclud@55: {
StephaneLenclud@55: Dispose();
StephaneLenclud@55: }
StephaneLenclud@55:
StephaneLenclud@55: ///
StephaneLenclud@55: /// Private constructor.
StephaneLenclud@55: ///
StephaneLenclud@55: ///
StephaneLenclud@55: private void Construct(IntPtr hRawInputDevice)
StephaneLenclud@55: {
StephaneLenclud@52: PreParsedData = IntPtr.Zero;
StephaneLenclud@58: iInputButtonCapabilities = null;
StephaneLenclud@58: iInputValueCapabilities = null;
StephaneLenclud@58:
sl@25: //Fetch various information defining the given HID device
StephaneLenclud@60: Name = Win32.RawInput.GetDeviceName(hRawInputDevice);
StephaneLenclud@52:
StephaneLenclud@53: //Fetch device info
StephaneLenclud@53: iInfo = new RID_DEVICE_INFO();
StephaneLenclud@60: if (!Win32.RawInput.GetDeviceInfo(hRawInputDevice, ref iInfo))
StephaneLenclud@53: {
StephaneLenclud@55: throw new Exception("HidDevice: GetDeviceInfo failed: " + Marshal.GetLastWin32Error().ToString());
StephaneLenclud@53: }
StephaneLenclud@55:
sl@25: //Open our device from the device name/path
StephaneLenclud@55: SafeFileHandle handle = Win32.Function.CreateFile(Name,
sl@25: Win32.FileAccess.NONE,
StephaneLenclud@55: Win32.FileShare.FILE_SHARE_READ | Win32.FileShare.FILE_SHARE_WRITE,
sl@25: IntPtr.Zero,
sl@25: Win32.CreationDisposition.OPEN_EXISTING,
sl@25: Win32.FileFlagsAttributes.FILE_FLAG_OVERLAPPED,
sl@25: IntPtr.Zero
sl@25: );
sl@25:
StephaneLenclud@55: //Check if CreateFile worked
sl@25: if (handle.IsInvalid)
sl@25: {
StephaneLenclud@55: throw new Exception("HidDevice: CreateFile failed: " + Marshal.GetLastWin32Error().ToString());
sl@25: }
StephaneLenclud@55:
StephaneLenclud@55: //Get manufacturer string
StephaneLenclud@55: StringBuilder manufacturerString = new StringBuilder(256);
StephaneLenclud@55: if (Win32.Function.HidD_GetManufacturerString(handle, manufacturerString, manufacturerString.Capacity))
sl@25: {
StephaneLenclud@55: Manufacturer = manufacturerString.ToString();
StephaneLenclud@55: }
sl@25:
StephaneLenclud@55: //Get product string
StephaneLenclud@55: StringBuilder productString = new StringBuilder(256);
StephaneLenclud@55: if (Win32.Function.HidD_GetProductString(handle, productString, productString.Capacity))
StephaneLenclud@55: {
StephaneLenclud@55: Product = productString.ToString();
StephaneLenclud@55: }
sl@25:
StephaneLenclud@55: //Get attributes
StephaneLenclud@55: Win32.HIDD_ATTRIBUTES attributes = new Win32.HIDD_ATTRIBUTES();
StephaneLenclud@55: if (Win32.Function.HidD_GetAttributes(handle, ref attributes))
StephaneLenclud@55: {
StephaneLenclud@55: VendorId = attributes.VendorID;
StephaneLenclud@55: ProductId = attributes.ProductID;
StephaneLenclud@55: Version = attributes.VersionNumber;
StephaneLenclud@55: }
sl@25:
StephaneLenclud@55: handle.Close();
StephaneLenclud@56:
StephaneLenclud@64: SetFriendlyName();
StephaneLenclud@63:
StephaneLenclud@61: //Get our HID descriptor pre-parsed data
StephaneLenclud@61: PreParsedData = Win32.RawInput.GetPreParsedData(hRawInputDevice);
StephaneLenclud@61:
StephaneLenclud@61: if (PreParsedData == IntPtr.Zero)
StephaneLenclud@61: {
StephaneLenclud@61: //We are done then.
StephaneLenclud@61: //Some devices don't have pre-parsed data.
StephaneLenclud@61: return;
StephaneLenclud@61: }
StephaneLenclud@61:
StephaneLenclud@56: //Get capabilities
StephaneLenclud@56: HidStatus status = Win32.Function.HidP_GetCaps(PreParsedData, ref iCapabilities);
StephaneLenclud@56: if (status != HidStatus.HIDP_STATUS_SUCCESS)
StephaneLenclud@56: {
StephaneLenclud@56: throw new Exception("HidDevice: HidP_GetCaps failed: " + status.ToString());
StephaneLenclud@56: }
StephaneLenclud@56:
StephaneLenclud@64: SetInputCapabilitiesDescription();
StephaneLenclud@64:
StephaneLenclud@58: //Get input button caps if needed
StephaneLenclud@58: if (Capabilities.NumberInputButtonCaps > 0)
StephaneLenclud@56: {
StephaneLenclud@58: iInputButtonCapabilities = new HIDP_BUTTON_CAPS[Capabilities.NumberInputButtonCaps];
StephaneLenclud@58: ushort buttonCapabilitiesLength = Capabilities.NumberInputButtonCaps;
StephaneLenclud@58: status = Win32.Function.HidP_GetButtonCaps(HIDP_REPORT_TYPE.HidP_Input, iInputButtonCapabilities, ref buttonCapabilitiesLength, PreParsedData);
StephaneLenclud@58: if (status != HidStatus.HIDP_STATUS_SUCCESS || buttonCapabilitiesLength != Capabilities.NumberInputButtonCaps)
StephaneLenclud@58: {
StephaneLenclud@58: throw new Exception("HidDevice: HidP_GetButtonCaps failed: " + status.ToString());
StephaneLenclud@58: }
StephaneLenclud@72:
StephaneLenclud@72: ComputeButtonCount();
StephaneLenclud@56: }
StephaneLenclud@58:
StephaneLenclud@58: //Get input value caps if needed
StephaneLenclud@58: if (Capabilities.NumberInputValueCaps > 0)
StephaneLenclud@58: {
StephaneLenclud@58: iInputValueCapabilities = new HIDP_VALUE_CAPS[Capabilities.NumberInputValueCaps];
StephaneLenclud@58: ushort valueCapabilitiesLength = Capabilities.NumberInputValueCaps;
StephaneLenclud@58: status = Win32.Function.HidP_GetValueCaps(HIDP_REPORT_TYPE.HidP_Input, iInputValueCapabilities, ref valueCapabilitiesLength, PreParsedData);
StephaneLenclud@58: if (status != HidStatus.HIDP_STATUS_SUCCESS || valueCapabilitiesLength != Capabilities.NumberInputValueCaps)
StephaneLenclud@58: {
StephaneLenclud@58: throw new Exception("HidDevice: HidP_GetValueCaps failed: " + status.ToString());
StephaneLenclud@58: }
StephaneLenclud@58: }
StephaneLenclud@64: }
StephaneLenclud@58:
StephaneLenclud@72:
StephaneLenclud@72: ///
StephaneLenclud@72: /// Useful for gamepads.
StephaneLenclud@72: ///
StephaneLenclud@72: void ComputeButtonCount()
StephaneLenclud@72: {
StephaneLenclud@72: ButtonCount = 0;
StephaneLenclud@72: foreach (HIDP_BUTTON_CAPS bc in iInputButtonCapabilities)
StephaneLenclud@72: {
StephaneLenclud@72: if (bc.IsRange)
StephaneLenclud@72: {
StephaneLenclud@72: ButtonCount += (bc.Range.UsageMax - bc.Range.UsageMin + 1);
StephaneLenclud@72: }
StephaneLenclud@72: }
StephaneLenclud@72: }
StephaneLenclud@72:
StephaneLenclud@72:
StephaneLenclud@64: ///
StephaneLenclud@64: ///
StephaneLenclud@64: ///
StephaneLenclud@64: void SetInputCapabilitiesDescription()
StephaneLenclud@64: {
StephaneLenclud@64: InputCapabilitiesDescription = "[ Input Capabilities ] Button: " + Capabilities.NumberInputButtonCaps + " - Value: " + Capabilities.NumberInputValueCaps + " - Data indices: " + Capabilities.NumberInputDataIndices;
StephaneLenclud@63: }
StephaneLenclud@63:
StephaneLenclud@63: ///
StephaneLenclud@63: ///
StephaneLenclud@63: ///
StephaneLenclud@63: private void SetFriendlyName()
StephaneLenclud@63: {
StephaneLenclud@63: //Work out proper suffix for our device root node.
StephaneLenclud@63: //That allows users to see in a glance what kind of device this is.
StephaneLenclud@63: string suffix = "";
StephaneLenclud@63: Type usageCollectionType = null;
StephaneLenclud@63: if (Info.dwType == RawInputDeviceType.RIM_TYPEHID)
StephaneLenclud@63: {
StephaneLenclud@63: //Process usage page
StephaneLenclud@76: if (Enum.IsDefined(typeof(UsagePage), Info.hid.usUsagePage))
StephaneLenclud@63: {
StephaneLenclud@63: //We know this usage page, add its name
StephaneLenclud@76: UsagePage usagePage = (UsagePage)Info.hid.usUsagePage;
StephaneLenclud@63: suffix += " ( " + usagePage.ToString() + ", ";
StephaneLenclud@76: usageCollectionType = Utils.UsageCollectionType(usagePage);
StephaneLenclud@63: }
StephaneLenclud@63: else
StephaneLenclud@63: {
StephaneLenclud@63: //We don't know this usage page, add its value
StephaneLenclud@63: suffix += " ( 0x" + Info.hid.usUsagePage.ToString("X4") + ", ";
StephaneLenclud@63: }
StephaneLenclud@63:
StephaneLenclud@63: //Process usage collection
StephaneLenclud@63: //We don't know this usage page, add its value
StephaneLenclud@63: if (usageCollectionType == null || !Enum.IsDefined(usageCollectionType, Info.hid.usUsage))
StephaneLenclud@63: {
StephaneLenclud@63: //Show Hexa
StephaneLenclud@63: suffix += "0x" + Info.hid.usUsage.ToString("X4") + " )";
StephaneLenclud@63: }
StephaneLenclud@63: else
StephaneLenclud@63: {
StephaneLenclud@63: //We know this usage page, add its name
StephaneLenclud@63: suffix += Enum.GetName(usageCollectionType, Info.hid.usUsage) + " )";
StephaneLenclud@63: }
StephaneLenclud@63: }
StephaneLenclud@63: else if (Info.dwType == RawInputDeviceType.RIM_TYPEKEYBOARD)
StephaneLenclud@63: {
StephaneLenclud@63: suffix = " - Keyboard";
StephaneLenclud@63: }
StephaneLenclud@63: else if (Info.dwType == RawInputDeviceType.RIM_TYPEMOUSE)
StephaneLenclud@63: {
StephaneLenclud@63: suffix = " - Mouse";
StephaneLenclud@63: }
StephaneLenclud@63:
StephaneLenclud@63: if (Product != null && Product.Length > 1)
StephaneLenclud@63: {
StephaneLenclud@69: //This device as a proper name, use it
StephaneLenclud@63: FriendlyName = Product + suffix;
StephaneLenclud@63: }
StephaneLenclud@63: else
StephaneLenclud@71: {
StephaneLenclud@71: //Extract friendly name from name
StephaneLenclud@71: char[] delimiterChars = { '#', '&'};
StephaneLenclud@71: string[] words = Name.Split(delimiterChars);
StephaneLenclud@71: if (words.Length >= 2)
StephaneLenclud@71: {
StephaneLenclud@71: //Use our name sub-string to describe this device
StephaneLenclud@71: FriendlyName = words[1] + " - 0x" + ProductId.ToString("X4") + suffix;
StephaneLenclud@71: }
StephaneLenclud@71: else
StephaneLenclud@71: {
StephaneLenclud@71: //No proper name just use the device ID instead
StephaneLenclud@71: FriendlyName = "0x" + ProductId.ToString("X4") + suffix;
StephaneLenclud@71: }
StephaneLenclud@63: }
StephaneLenclud@63:
StephaneLenclud@52: }
StephaneLenclud@52:
StephaneLenclud@52: ///
StephaneLenclud@52: /// Dispose is just for unmanaged clean-up.
StephaneLenclud@52: /// Make sure calling disposed multiple times does not crash.
StephaneLenclud@52: /// See: http://stackoverflow.com/questions/538060/proper-use-of-the-idisposable-interface/538238#538238
StephaneLenclud@52: ///
StephaneLenclud@52: public void Dispose()
StephaneLenclud@52: {
StephaneLenclud@52: Marshal.FreeHGlobal(PreParsedData);
StephaneLenclud@52: PreParsedData = IntPtr.Zero;
StephaneLenclud@52: }
StephaneLenclud@52:
sl@25: ///
StephaneLenclud@72: /// Provide a description for the given capabilities.
StephaneLenclud@72: /// Notably describes axis on a gamepad/joystick.
StephaneLenclud@65: ///
StephaneLenclud@65: ///
StephaneLenclud@65: ///
StephaneLenclud@73: static public string InputValueCapabilityDescription(HIDP_VALUE_CAPS aCaps)
StephaneLenclud@65: {
StephaneLenclud@73: if (!aCaps.IsRange && Enum.IsDefined(typeof(UsagePage), aCaps.UsagePage))
StephaneLenclud@65: {
StephaneLenclud@73: Type usageType = Utils.UsageType((UsagePage)aCaps.UsagePage);
StephaneLenclud@72: string name = Enum.GetName(usageType, aCaps.NotRange.Usage);
StephaneLenclud@72: if (name == null)
StephaneLenclud@72: {
StephaneLenclud@72: //Could not find that usage in our enum.
StephaneLenclud@72: //Provide a relevant warning instead.
StephaneLenclud@72: name = "Usage 0x" + aCaps.NotRange.Usage.ToString("X2") + " not defined in " + usageType.Name;
StephaneLenclud@72: }
StephaneLenclud@72: else
StephaneLenclud@72: {
StephaneLenclud@72: //Prepend our usage type name
StephaneLenclud@72: name = usageType.Name + "." + name;
StephaneLenclud@72: }
StephaneLenclud@72: return "Input Value: " + name;
StephaneLenclud@65: }
StephaneLenclud@65:
StephaneLenclud@65: return null;
StephaneLenclud@65: }
StephaneLenclud@65:
StephaneLenclud@65: public bool IsGamePad
StephaneLenclud@65: {
StephaneLenclud@65: get
StephaneLenclud@65: {
StephaneLenclud@66: return ((UsagePage)iCapabilities.UsagePage == UsagePage.GenericDesktopControls && (UsageCollection.GenericDesktop)iCapabilities.Usage == UsageCollection.GenericDesktop.GamePad);
StephaneLenclud@65: }
StephaneLenclud@65: }
StephaneLenclud@65:
StephaneLenclud@65:
StephaneLenclud@65: ///
sl@25: /// Print information about this device to our debug output.
sl@25: ///
sl@25: public void DebugWrite()
sl@25: {
sl@25: Debug.WriteLine("================ HID =========================================================================================");
sl@25: Debug.WriteLine("==== Name: " + Name);
sl@25: Debug.WriteLine("==== Manufacturer: " + Manufacturer);
sl@25: Debug.WriteLine("==== Product: " + Product);
sl@25: Debug.WriteLine("==== VendorID: 0x" + VendorId.ToString("X4"));
sl@25: Debug.WriteLine("==== ProductID: 0x" + ProductId.ToString("X4"));
sl@25: Debug.WriteLine("==== Version: " + Version.ToString());
sl@25: Debug.WriteLine("==============================================================================================================");
sl@25: }
sl@25:
sl@25: }
sl@25:
sl@25: }