private List <int> GetDeviceButtonStatus() { //Sony PlayStation DualSense 5 only reports 14 out of 15 buttons List <int> buttonStatus = new List <int>(); IntPtr preparsedDataPointer = IntPtr.Zero; try { Debug.WriteLine("Getting device button status."); if (HidD_GetPreparsedData(FileHandle, ref preparsedDataPointer)) { int buttonCapsLength = Capabilities.NumberInputButtonCaps; if (buttonCapsLength > 0) { ButtonValueCaps[] buttonCaps = new ButtonValueCaps[buttonCapsLength]; HidP_GetButtonCaps(HIDP_REPORT_TYPE.HidP_Input, buttonCaps, ref buttonCapsLength, preparsedDataPointer); foreach (ButtonValueCaps buttonCap in buttonCaps) { //Get input report byte[] reportBuffer = new byte[Capabilities.InputReportByteLength]; reportBuffer[0] = buttonCap.ReportID; HidD_GetInputReport(FileHandle, reportBuffer, reportBuffer.Length); //Get number of buttons int numberOfButtons = buttonCap.Range.UsageMax - buttonCap.Range.UsageMin + 1; //Check pressed buttons ushort[] usageList = new ushort[numberOfButtons]; HidP_GetUsages(HIDP_REPORT_TYPE.HidP_Input, buttonCap.UsagePage, 0, usageList, ref numberOfButtons, preparsedDataPointer, reportBuffer, reportBuffer.Length); //Add pressed buttons for (int i = 0; i < numberOfButtons; i++) { int pressedButton = usageList[i] - buttonCap.Range.UsageMin; Debug.WriteLine("Button " + pressedButton + " is pressed."); buttonStatus.Add(pressedButton); } } } return(buttonStatus); } else { Debug.WriteLine("Failed to get device buttons."); return(buttonStatus); } } catch (Exception ex) { Debug.WriteLine("Failed to get device buttons: " + ex.Message); return(buttonStatus); } finally { if (preparsedDataPointer != IntPtr.Zero) { HidD_FreePreparsedData(preparsedDataPointer); } } }
private List <int> GetDeviceValueStatus() { List <int> valueStatus = new List <int>(); IntPtr preparsedDataPointer = IntPtr.Zero; try { Debug.WriteLine("Getting device value status."); if (HidD_GetPreparsedData(FileHandle, ref preparsedDataPointer)) { int valueCapsLength = Capabilities.NumberInputValueCaps; if (valueCapsLength > 0) { ButtonValueCaps[] valueCaps = new ButtonValueCaps[valueCapsLength]; HidP_GetValueCaps(HIDP_REPORT_TYPE.HidP_Input, valueCaps, ref valueCapsLength, preparsedDataPointer); foreach (ButtonValueCaps valueCap in valueCaps) { //Get input report byte[] reportBuffer = new byte[Capabilities.InputReportByteLength]; reportBuffer[0] = valueCap.ReportID; HidD_GetInputReport(FileHandle, reportBuffer, reportBuffer.Length); //Get usage value HidP_GetUsageValue(HIDP_REPORT_TYPE.HidP_Input, valueCap.UsagePage, 0, valueCap.UsageMin, out uint usageValue, preparsedDataPointer, reportBuffer, reportBuffer.Length); //Show value in console Debug.WriteLine(valueCap.NotRangeUsageName + "/" + usageValue); } } return(valueStatus); } else { Debug.WriteLine("Failed to get device values."); return(valueStatus); } } catch (Exception ex) { Debug.WriteLine("Failed to get device values: " + ex.Message); return(valueStatus); } finally { if (preparsedDataPointer != IntPtr.Zero) { HidD_FreePreparsedData(preparsedDataPointer); } } }