public void UpdateFrom(Joystick device, DeviceInfo dInfo, out JoystickState state) { if (!AppHelper.IsSameDevice(device, deviceInstanceGuid)) { ShowDeviceInfo(device, dInfo); deviceInstanceGuid = Guid.Empty; if (device != null) { deviceInstanceGuid = device.Information.InstanceGuid; isWheel = device.Information.Type == SharpDX.DirectInput.DeviceType.Driving; } } state = null; if (device != null) { try { device.Acquire(); state = device.GetCurrentState(); } catch (Exception ex) { var error = ex; } } ShowDirectInputState(state); }
static DeviceInfo GetDeviceInfo(IntPtr deviceInfoSet, SP_DEVINFO_DATA deviceInfoData, string deviceId) { var deviceName = GetDeviceDescription(deviceInfoSet, deviceInfoData); var deviceManufacturer = GetDeviceManufacturer(deviceInfoSet, deviceInfoData); var deviceClassGuid = deviceInfoData.ClassGuid; var classDescription = GetClassDescription(deviceClassGuid); Win32.DeviceNodeStatus status; GetDeviceNodeStatus(deviceInfoData.DevInst, IntPtr.Zero, out status); uint vid; uint pid; uint rev; var hwid = GetVidPidRev(deviceInfoSet, deviceInfoData, out vid, out pid, out rev); //if (deviceName.Contains("vJoy Device")) //{ // var sb = new StringBuilder(); // var props = (SPDRP[])Enum.GetValues(typeof(SPDRP)); // foreach (var item in props) // { // if (new[] { SPDRP.SPDRP_UNUSED0, SPDRP.SPDRP_UNUSED1, SPDRP.SPDRP_UNUSED2 }.Contains(item)) // { // continue; // } // try // { // var value = GetStringPropertyForDevice(deviceInfoSet, deviceInfoData, item); // sb.AppendFormat("{0}={1}\r\n", item, value); // } // catch (Exception ex) // { // sb.AppendFormat("{0}={1}\r\n", item, ex.ToString()); // } // } //} var device = new DeviceInfo(deviceId, deviceManufacturer, deviceName, deviceClassGuid, classDescription, status, vid, pid, rev); return device; }
void ShowDeviceInfo(Joystick device, DeviceInfo dInfo) { if (device == null) { // clean everything here. AppHelper.SetText(DeviceProductNameTextBox, ""); AppHelper.SetText(DeviceVendorNameTextBox, ""); AppHelper.SetText(DeviceProductGuidTextBox, ""); AppHelper.SetText(DeviceInstanceGuidTextBox, ""); AppHelper.SetText(DiCapFfStateTextBox, ""); AppHelper.SetText(DiCapAxesTextBox, ""); AppHelper.SetText(DiCapButtonsTextBox, ""); AppHelper.SetText(DiCapDPadsTextBox, ""); if (DiEffectsTable.Rows.Count > 0) DiEffectsTable.Rows.Clear(); return; } lock (MainForm.XInputLock) { var isLoaded = XInput.IsLoaded; if (isLoaded) XInput.FreeLibrary(); device.Unacquire(); device.SetCooperativeLevel(MainForm.Current, CooperativeLevel.Foreground | CooperativeLevel.Exclusive); effects = new List<EffectInfo>(); try { device.Acquire(); var forceFeedback = device.Capabilities.Flags.HasFlag(DeviceFlags.ForceFeedback); forceFeedbackState = forceFeedback ? "YES" : "NO"; effects = device.GetEffects(EffectType.All); } catch (Exception) { forceFeedbackState = "ERROR"; } DiEffectsTable.Rows.Clear(); foreach (var eff in effects) { DiEffectsTable.Rows.Add(new object[]{ eff.Name, ((EffectParameterFlags)eff.StaticParameters).ToString(), ((EffectParameterFlags)eff.DynamicParameters).ToString() }); } device.Unacquire(); device.SetCooperativeLevel(MainForm.Current, CooperativeLevel.Background | CooperativeLevel.NonExclusive); if (isLoaded) { Exception error; XInput.ReLoadLibrary(XInput.LibraryName, out error); } } AppHelper.SetText(DiCapFfStateTextBox, forceFeedbackState); AppHelper.SetText(DiCapButtonsTextBox, device.Capabilities.ButtonCount.ToString()); AppHelper.SetText(DiCapDPadsTextBox, device.Capabilities.PovCount.ToString()); var objects = AppHelper.GetDeviceObjects(device); DiObjectsDataGridView.DataSource = objects; var actuators = objects.Where(x => x.Flags.HasFlag(DeviceObjectTypeFlags.ForceFeedbackActuator)); AppHelper.SetText(ActuatorsTextBox, actuators.Count().ToString()); var di = device.Information; var slidersCount = objects.Where(x => x.GuidValue.Equals(SharpDX.DirectInput.ObjectGuid.Slider)).Count(); AppHelper.SetText(DiCapAxesTextBox, (device.Capabilities.AxeCount - slidersCount).ToString()); AppHelper.SetText(SlidersTextBox, slidersCount.ToString()); // Update PID and VID always so they wont be overwritten by load settings. short vid = BitConverter.ToInt16(di.ProductGuid.ToByteArray(), 0); short pid = BitConverter.ToInt16(di.ProductGuid.ToByteArray(), 2); AppHelper.SetText(DeviceVidTextBox, "0x{0:X4}", vid); AppHelper.SetText(DevicePidTextBox, "0x{0:X4}", pid); AppHelper.SetText(DeviceProductNameTextBox, di.ProductName); AppHelper.SetText(DeviceVendorNameTextBox, dInfo == null ? "" : dInfo.Manufacturer); AppHelper.SetText(DeviceRevTextBox, "0x{0:X4}", dInfo == null ? 0 : dInfo.Revision); AppHelper.SetText(DeviceProductGuidTextBox, di.ProductGuid.ToString()); AppHelper.SetText(DeviceInstanceGuidTextBox, di.InstanceGuid.ToString()); AppHelper.SetText(DeviceTypeTextBox, di.Type.ToString()); }
public static DeviceInfo[] GetInterfaces() { var list = new List <DeviceInfo>(); Guid hidGuid = Guid.Empty; NativeMethods.HidD_GetHidGuid(ref hidGuid); int requiredSize3 = 0; List <string> devicePathNames3 = new List <string>(); var interfaceData = new SP_DEVICE_INTERFACE_DATA(); List <string> serials = new List <string>(); interfaceData.Initialize(); var deviceInfoSet = NativeMethods.SetupDiGetClassDevs(hidGuid, IntPtr.Zero, IntPtr.Zero, DIGCF.DIGCF_DEVICEINTERFACE); for (int i2 = 0; NativeMethods.SetupDiEnumDeviceInterfaces(deviceInfoSet, IntPtr.Zero, ref hidGuid, i2, ref interfaceData); i2++) { var deviceInfoData = new SP_DEVINFO_DATA(); deviceInfoData.Initialize(); bool success = NativeMethods.SetupDiGetDeviceInterfaceDetail(deviceInfoSet, ref interfaceData, IntPtr.Zero, 0, ref requiredSize3, IntPtr.Zero); IntPtr ptrDetails = Marshal.AllocHGlobal(requiredSize3); Marshal.WriteInt32(ptrDetails, (IntPtr.Size == 4) ? (4 + Marshal.SystemDefaultCharSize) : 8); success = NativeMethods.SetupDiGetDeviceInterfaceDetail(deviceInfoSet, ref interfaceData, ptrDetails, requiredSize3, ref requiredSize3, ref deviceInfoData); var interfaceDetail = (SP_DEVICE_INTERFACE_DETAIL_DATA)Marshal.PtrToStructure(ptrDetails, typeof(SP_DEVICE_INTERFACE_DETAIL_DATA)); var devicePath = interfaceDetail.DevicePath; var deviceId = GetDeviceId(deviceInfoData.DevInst); devicePathNames3.Add(devicePath); Marshal.FreeHGlobal(ptrDetails); var accessRights = WinNT.GENERIC_READ | WinNT.GENERIC_WRITE; var shareModes = WinNT.FILE_SHARE_READ | WinNT.FILE_SHARE_WRITE; // Open the device as a file so that we can query it with HID and read/write to it. var devHandle = NativeMethods.CreateFile( interfaceDetail.DevicePath, accessRights, shareModes, IntPtr.Zero, WinNT.OPEN_EXISTING, WinNT.Overlapped, IntPtr.Zero ); if (devHandle.IsInvalid) { continue; } var ha = new HIDD_ATTRIBUTES(); ha.Size = Marshal.SizeOf(ha); var success2 = NativeMethods.HidD_GetAttributes(devHandle, ref ha); string serial = ""; string vendor = ""; string product = ""; string phdesc = ""; if (success2) { IntPtr preparsedDataPtr = new IntPtr(); HIDP_CAPS caps = new HIDP_CAPS(); // Read out the 'preparsed data'. NativeMethods.HidD_GetPreparsedData(devHandle, ref preparsedDataPtr); // feed that to GetCaps. NativeMethods.HidP_GetCaps(preparsedDataPtr, ref caps); // Free the 'preparsed data'. NativeMethods.HidD_FreePreparsedData(ref preparsedDataPtr); // This could fail if the device was recently attached. uint capacity = 126; IntPtr buffer = Marshal.AllocHGlobal((int)capacity); serial = NativeMethods.HidD_GetSerialNumberString(devHandle, buffer, capacity) ? Marshal.PtrToStringAuto(buffer) : ""; vendor = NativeMethods.HidD_GetManufacturerString(devHandle, buffer, capacity) ? Marshal.PtrToStringAuto(buffer) : ""; product = NativeMethods.HidD_GetProductString(devHandle, buffer, capacity) ? Marshal.PtrToStringAuto(buffer) : ""; phdesc = NativeMethods.HidD_GetPhysicalDescriptor(devHandle, buffer, capacity) ? Marshal.PtrToStringAuto(buffer) : ""; // Free resources. Marshal.FreeHGlobal(buffer); } uint parentDeviceInstance = 0; string parentDeviceId = null; var CRResult = NativeMethods.CM_Get_Parent(out parentDeviceInstance, deviceInfoData.DevInst, 0); if (CRResult == CR.CR_SUCCESS) { parentDeviceId = GetDeviceId(parentDeviceInstance); } var di = new DeviceInfo(deviceId, parentDeviceId, devicePath, vendor, product, hidGuid, "", DeviceNodeStatus.DN_MANUAL, ha.VendorID, ha.ProductID, ha.VersionNumber); list.Add(di); serials.Add(phdesc); devHandle.Close(); } NativeMethods.SetupDiDestroyDeviceInfoList(deviceInfoSet); deviceInfoSet = IntPtr.Zero; return(list.ToArray()); }