Ejemplo n.º 1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="GorgonRawHIDInfo"/> class.
        /// </summary>
        /// <param name="deviceHandle">The device handle.</param>
        /// <param name="hidPath">The hid path.</param>
        /// <param name="className">Name of the class.</param>
        /// <param name="description">The description.</param>
        /// <param name="deviceInfo">The device information.</param>
        internal GorgonRawHIDInfo(IntPtr deviceHandle, string hidPath, string className, string description, RID_DEVICE_INFO_HID deviceInfo)
        {
            Handle      = deviceHandle;
            HIDPath     = hidPath;
            DeviceClass = className;
            Description = description;

            ProductID = deviceInfo.dwProductId;
            VendorID  = deviceInfo.dwVendorId;
            Version   = deviceInfo.dwVersionNumber;
            Usage     = (HIDUsage)deviceInfo.usUsage;
            UsagePage = (HIDUsagePage)deviceInfo.usUsagePage;
        }
Ejemplo n.º 2
0
 private bool Is3DxDevice(RID_DEVICE_INFO_HID hidInfo)
 {
     return(hidInfo.dwVendorID == LOGITECH_3DX_VID &&
            hidInfo.usUsagePage == HID_USAGE_PAGE_GENERIC &&
            hidInfo.usUsage == HID_USAGE_GENERIC_MULTIAXIS_CONTROLLER);
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Iterates through the list provided by GetRawInputDeviceList,
        /// counting 3Dx devices and adding them to deviceList.
        /// </summary>
        /// <returns>The number of 3Dx devices found.</returns>
        public int EnumerateDevices()
        {
            var NumberOfDevices = 0;
            uint deviceCount = 0;
            var dwSize = (Marshal.SizeOf(typeof(RAWINPUTDEVICELIST)));

            if (GetRawInputDeviceList(IntPtr.Zero, ref deviceCount, (uint)dwSize) == 0)
            {
                var pRawInputDeviceList = Marshal.AllocHGlobal((int)(dwSize * deviceCount));
                GetRawInputDeviceList(pRawInputDeviceList, ref deviceCount, (uint)dwSize);

                for (var i = 0; i < deviceCount; i++)
                {
                    uint pcbSize = 0;

                    var rid = (RAWINPUTDEVICELIST)Marshal.PtrToStructure(
                                               new IntPtr((pRawInputDeviceList.ToInt32() + (dwSize * i))),
                                               typeof(RAWINPUTDEVICELIST));

                    GetRawInputDeviceInfo(rid.hDevice, RIDI_DEVICENAME, IntPtr.Zero, ref pcbSize);

                    if (pcbSize > 0)
                    {
                        var pData = Marshal.AllocHGlobal((int)pcbSize);
                        GetRawInputDeviceInfo(rid.hDevice, RIDI_DEVICENAME, pData, ref pcbSize);
                        var deviceName = Marshal.PtrToStringAnsi(pData);

                        //If the device is identified as a HID device with usagePage 1, usage 8,
                        //create a DeviceInfo object to store information about it
                        if (rid.dwType == RIM_TYPEHID)
                        {
                            var dInfo = new DeviceInfo();

                            // Create an object to set the size in the struct.  Then marshall it to a ptr to pass to RI.
                            var hidInfo = new RID_DEVICE_INFO_HID();
                            var dwHidInfoSize = Marshal.SizeOf(hidInfo);
                            hidInfo.cbSize = dwHidInfoSize;
                            var pHIDData = Marshal.AllocHGlobal(dwHidInfoSize);
                            Marshal.StructureToPtr(hidInfo, pHIDData, true);

                            var uHidInfoSize = (uint)dwHidInfoSize;

                            GetRawInputDeviceInfo(rid.hDevice, RIDI_DEVICEINFO, pHIDData, ref uHidInfoSize);

                            // Marshal back to an object to retrieve info
                            hidInfo = (RID_DEVICE_INFO_HID)Marshal.PtrToStructure(pHIDData, typeof(RID_DEVICE_INFO_HID));

                            dInfo.deviceName = Marshal.PtrToStringAnsi(pData);
                            dInfo.deviceHandle = rid.hDevice;

                            //If it is a 3Dx device and it isn't already in the list,
                            //add it to the deviceList hashtable and increase the
                            //NumberOfDevices count
                            if (Is3DxDevice(hidInfo) && !deviceList.Contains(rid.hDevice))
                            {
                                Console.WriteLine("Using 3Dx device: " + deviceName);
                                NumberOfDevices++;
                                deviceList.Add(rid.hDevice, dInfo);
                            }
                            Marshal.FreeHGlobal(pHIDData);
                        }
                        Marshal.FreeHGlobal(pData);
                    }
                }

                Marshal.FreeHGlobal(pRawInputDeviceList);
                return NumberOfDevices;
            }
            throw new ApplicationException("Error!");
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Iterates through the list provided by GetRawInputDeviceList,
        /// counting 3Dx devices and adding them to deviceList.
        /// </summary>
        /// <returns>The number of 3Dx devices found.</returns>
        public int EnumerateDevices()
        {
            int  NumberOfDevices = 0;
            uint deviceCount     = 0;
            int  dwSize          = (Marshal.SizeOf(typeof(RAWINPUTDEVICELIST)));

            if (GetRawInputDeviceList(IntPtr.Zero, ref deviceCount, (uint)dwSize) == 0)
            {
                IntPtr pRawInputDeviceList = Marshal.AllocHGlobal((int)(dwSize * deviceCount));
                GetRawInputDeviceList(pRawInputDeviceList, ref deviceCount, (uint)dwSize);

                for (int i = 0; i < deviceCount; i++)
                {
                    uint pcbSize = 0;

                    RAWINPUTDEVICELIST rid = (RAWINPUTDEVICELIST)Marshal.PtrToStructure(
                        new IntPtr((pRawInputDeviceList.ToInt32() + (dwSize * i))),
                        typeof(RAWINPUTDEVICELIST));

                    GetRawInputDeviceInfo(rid.hDevice, RIDI_DEVICENAME, IntPtr.Zero, ref pcbSize);

                    if (pcbSize > 0)
                    {
                        IntPtr pData = Marshal.AllocHGlobal((int)pcbSize);
                        GetRawInputDeviceInfo(rid.hDevice, RIDI_DEVICENAME, pData, ref pcbSize);
                        string deviceName = Marshal.PtrToStringAnsi(pData);

                        //If the device is identified as a HID device with usagePage 1, usage 8,
                        //create a DeviceInfo object to store information about it
                        if (rid.dwType == RIM_TYPEHID)
                        {
                            DeviceInfo dInfo = new DeviceInfo();

                            // Create an object to set the size in the struct.  Then marshall it to a ptr to pass to RI.
                            RID_DEVICE_INFO_HID hidInfo = new RID_DEVICE_INFO_HID();
                            int dwHidInfoSize           = Marshal.SizeOf(hidInfo);
                            hidInfo.cbSize = dwHidInfoSize;
                            IntPtr pHIDData = Marshal.AllocHGlobal(dwHidInfoSize);
                            Marshal.StructureToPtr(hidInfo, pHIDData, true);

                            uint uHidInfoSize = (uint)dwHidInfoSize;

                            GetRawInputDeviceInfo(rid.hDevice, RIDI_DEVICEINFO, pHIDData, ref uHidInfoSize);

                            // Marshal back to an object to retrieve info
                            hidInfo = (RID_DEVICE_INFO_HID)Marshal.PtrToStructure(pHIDData, typeof(RID_DEVICE_INFO_HID));

                            dInfo.deviceName   = Marshal.PtrToStringAnsi(pData);
                            dInfo.deviceHandle = rid.hDevice;


                            //If it is a 3Dx device and it isn't already in the list,
                            //add it to the deviceList hashtable and increase the
                            //NumberOfDevices count
                            if (Is3DxDevice(hidInfo) && !deviceList.Contains(rid.hDevice))
                            {
                                Console.WriteLine("Using 3Dx device: " + deviceName);
                                NumberOfDevices++;
                                deviceList.Add(rid.hDevice, dInfo);
                            }
                            Marshal.FreeHGlobal(pHIDData);
                        }
                        Marshal.FreeHGlobal(pData);
                    }
                }

                Marshal.FreeHGlobal(pRawInputDeviceList);
                return(NumberOfDevices);
            }
            else
            {
                throw new ApplicationException("Error!");
            }
        }
Ejemplo n.º 5
0
 private bool Is3DxDevice(RID_DEVICE_INFO_HID hidInfo)
 {
     return hidInfo.dwVendorID == LOGITECH_3DX_VID &&
         hidInfo.usUsagePage == HID_USAGE_PAGE_GENERIC &&
         hidInfo.usUsage == HID_USAGE_GENERIC_MULTIAXIS_CONTROLLER;
 }