Mouse / Keyboard: try harder to get device name

This commit is contained in:
李通洲
2024-10-09 21:43:33 +08:00
parent aece7cb550
commit ee29f767a0
4 changed files with 81 additions and 62 deletions
+1
View File
@@ -1312,6 +1312,7 @@ elseif(WIN32)
PRIVATE "hid"
PRIVATE "wtsapi32"
PRIVATE "imagehlp"
PRIVATE "cfgmgr32"
)
elseif(FreeBSD)
target_link_libraries(libfastfetch
+4 -4
View File
@@ -131,6 +131,10 @@ const char* ffDetectGamepad(FFlist* devices /* List of FFGamepadDevice */)
}
}
wchar_t serialNumber[127] = L"";
if (HidD_GetSerialNumberString(hHidFile, serialNumber, sizeof(serialNumber)))
ffStrbufSetWS(&device->serial, serialNumber);
PHIDP_PREPARSED_DATA preparsedData = NULL;
if (HidD_GetPreparsedData(hHidFile, &preparsedData))
{
@@ -140,10 +144,6 @@ const char* ffDetectGamepad(FFlist* devices /* List of FFGamepadDevice */)
if (!NT_SUCCESS(capsResult))
continue;
wchar_t serialNumber[127] = L"";
if (HidD_GetSerialNumberString(hHidFile, serialNumber, sizeof(serialNumber)))
ffStrbufSetWS(&device->serial, serialNumber);
if (
(rdi.hid.dwVendorId == 0x054C && (
rdi.hid.dwProductId == 0x05C4 || // PS4 Gen1
+39 -29
View File
@@ -1,3 +1,5 @@
#define INITGUID
#include "keyboard.h"
#include "common/io/io.h"
#include "util/mallocHelper.h"
@@ -6,6 +8,17 @@
#include <winternl.h>
#include <windows.h>
#include <hidsdi.h>
#include <cfgmgr32.h>
#include <devpkey.h>
CMAPI CONFIGRET CM_Get_Device_Interface_PropertyW(
_In_ LPCWSTR pszDeviceInterface,
_In_ const DEVPROPKEY *PropertyKey,
_Out_ DEVPROPTYPE *PropertyType,
_Out_ PBYTE PropertyBuffer,
_Inout_ PULONG PropertyBufferSize,
_In_ ULONG ulFlags
);
const char* ffDetectKeyboard(FFlist* devices /* List of FFKeyboardDevice */)
{
@@ -14,10 +27,12 @@ const char* ffDetectKeyboard(FFlist* devices /* List of FFKeyboardDevice */)
return "GetRawInputDeviceList(NULL) failed";
if (nDevices == 0)
return "No HID devices found";
RAWINPUTDEVICELIST* FF_AUTO_FREE pRawInputDeviceList = (RAWINPUTDEVICELIST*) malloc(sizeof(RAWINPUTDEVICELIST) * nDevices);
if ((nDevices = GetRawInputDeviceList(pRawInputDeviceList, &nDevices, sizeof(RAWINPUTDEVICELIST))) == (UINT) -1)
return "GetRawInputDeviceList(pRawInputDeviceList) failed";
for (UINT i = 0; i < nDevices; ++i)
{
if (pRawInputDeviceList[i].dwType != RIM_TYPEKEYBOARD) continue;
@@ -29,7 +44,7 @@ const char* ffDetectKeyboard(FFlist* devices /* List of FFKeyboardDevice */)
if (GetRawInputDeviceInfoW(hDevice, RIDI_DEVICEINFO, &rdi, &rdiSize) == (UINT) -1)
continue;
WCHAR devName[MAX_PATH] = L"";
WCHAR devName[MAX_PATH];
UINT nameSize = MAX_PATH;
if (GetRawInputDeviceInfoW(hDevice, RIDI_DEVICENAME, devName, &nameSize) == (UINT) -1)
continue;
@@ -38,43 +53,38 @@ const char* ffDetectKeyboard(FFlist* devices /* List of FFKeyboardDevice */)
ffStrbufInit(&device->serial);
ffStrbufInit(&device->name);
wchar_t buffer[MAX_PATH];
HANDLE FF_AUTO_CLOSE_FD hHidFile = CreateFileW(devName, 0 /* must be 0 instead of GENERIC_READ */, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
if (hHidFile == INVALID_HANDLE_VALUE)
if (hHidFile != INVALID_HANDLE_VALUE)
{
ffStrbufSetF(&device->name, "Unknown keyboard %04X-%04X", (unsigned) rdi.hid.dwVendorId, (unsigned) rdi.hid.dwProductId);
continue;
if (HidD_GetProductString(hHidFile, buffer, (ULONG) sizeof(buffer)))
ffStrbufSetWS(&device->name, buffer);
if (HidD_GetSerialNumberString(hHidFile, buffer, sizeof(buffer)))
ffStrbufSetWS(&device->serial, buffer);
}
wchar_t displayName[126];
if (HidD_GetProductString(hHidFile, displayName, sizeof(displayName)))
if (!device->name.length)
{
wchar_t manufacturer[126];
if (HidD_GetManufacturerString(hHidFile, manufacturer, sizeof(manufacturer)))
// https://stackoverflow.com/a/64321096/9976392
DEVPROPTYPE propertyType;
ULONG propertySize = sizeof(buffer);
if (CM_Get_Device_Interface_PropertyW(devName, &DEVPKEY_Device_InstanceId, &propertyType, (PBYTE) buffer, &propertySize, 0) == CR_SUCCESS)
{
ffStrbufSetWS(&device->name, manufacturer);
FF_STRBUF_AUTO_DESTROY displayNameStr = ffStrbufCreateWS(displayName);
ffStrbufAppendC(&device->name, ' ');
ffStrbufAppend(&device->name, &displayNameStr);
}
else
{
ffStrbufSetWS(&device->name, displayName);
DEVINST devInst;
if (CM_Locate_DevNodeW(&devInst, buffer, CM_LOCATE_DEVNODE_NORMAL) == CR_SUCCESS)
{
propertySize = sizeof(buffer);
if (CM_Get_DevNode_PropertyW(devInst, &DEVPKEY_NAME, &propertyType, (PBYTE) buffer, &propertySize, 0) == CR_SUCCESS)
ffStrbufSetWS(&device->name, buffer);
}
}
}
PHIDP_PREPARSED_DATA preparsedData = NULL;
if (HidD_GetPreparsedData(hHidFile, &preparsedData))
{
HIDP_CAPS caps;
NTSTATUS capsResult = HidP_GetCaps(preparsedData, &caps);
HidD_FreePreparsedData(preparsedData);
if (!NT_SUCCESS(capsResult))
continue;
wchar_t serialNumber[127] = L"";
if (HidD_GetSerialNumberString(hHidFile, serialNumber, sizeof(serialNumber)))
ffStrbufSetWS(&device->serial, serialNumber);
}
if (!device->name.length)
ffStrbufSetF(&device->name, "Unknown device %04X-%04X", (unsigned) rdi.hid.dwVendorId, (unsigned) rdi.hid.dwProductId);
}
return NULL;
+37 -29
View File
@@ -1,3 +1,5 @@
#define INITGUID
#include "mouse.h"
#include "common/io/io.h"
#include "util/mallocHelper.h"
@@ -6,6 +8,17 @@
#include <winternl.h>
#include <windows.h>
#include <hidsdi.h>
#include <cfgmgr32.h>
#include <devpkey.h>
CMAPI CONFIGRET CM_Get_Device_Interface_PropertyW(
_In_ LPCWSTR pszDeviceInterface,
_In_ const DEVPROPKEY *PropertyKey,
_Out_ DEVPROPTYPE *PropertyType,
_Out_ PBYTE PropertyBuffer,
_Inout_ PULONG PropertyBufferSize,
_In_ ULONG ulFlags
);
const char* ffDetectMouse(FFlist* devices /* List of FFMouseDevice */)
{
@@ -29,7 +42,7 @@ const char* ffDetectMouse(FFlist* devices /* List of FFMouseDevice */)
if (GetRawInputDeviceInfoW(hDevice, RIDI_DEVICEINFO, &rdi, &rdiSize) == (UINT) -1)
continue;
WCHAR devName[MAX_PATH] = L"";
WCHAR devName[MAX_PATH];
UINT nameSize = MAX_PATH;
if (GetRawInputDeviceInfoW(hDevice, RIDI_DEVICENAME, devName, &nameSize) == (UINT) -1)
continue;
@@ -38,43 +51,38 @@ const char* ffDetectMouse(FFlist* devices /* List of FFMouseDevice */)
ffStrbufInit(&device->serial);
ffStrbufInit(&device->name);
wchar_t buffer[MAX_PATH];
HANDLE FF_AUTO_CLOSE_FD hHidFile = CreateFileW(devName, 0 /* must be 0 instead of GENERIC_READ */, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
if (hHidFile == INVALID_HANDLE_VALUE)
if (hHidFile != INVALID_HANDLE_VALUE)
{
ffStrbufSetF(&device->name, "Unknown mouse %04X-%04X", (unsigned) rdi.hid.dwVendorId, (unsigned) rdi.hid.dwProductId);
continue;
if (HidD_GetProductString(hHidFile, buffer, (ULONG) sizeof(buffer)))
ffStrbufSetWS(&device->name, buffer);
if (HidD_GetSerialNumberString(hHidFile, buffer, sizeof(buffer)))
ffStrbufSetWS(&device->serial, buffer);
}
wchar_t displayName[126];
if (HidD_GetProductString(hHidFile, displayName, sizeof(displayName)))
if (!device->name.length)
{
wchar_t manufacturer[126];
if (HidD_GetManufacturerString(hHidFile, manufacturer, sizeof(manufacturer)))
// https://stackoverflow.com/a/64321096/9976392
DEVPROPTYPE propertyType;
ULONG propertySize = sizeof(buffer);
if (CM_Get_Device_Interface_PropertyW(devName, &DEVPKEY_Device_InstanceId, &propertyType, (PBYTE) buffer, &propertySize, 0) == CR_SUCCESS)
{
ffStrbufSetWS(&device->name, manufacturer);
FF_STRBUF_AUTO_DESTROY displayNameStr = ffStrbufCreateWS(displayName);
ffStrbufAppendC(&device->name, ' ');
ffStrbufAppend(&device->name, &displayNameStr);
}
else
{
ffStrbufSetWS(&device->name, displayName);
DEVINST devInst;
if (CM_Locate_DevNodeW(&devInst, buffer, CM_LOCATE_DEVNODE_NORMAL) == CR_SUCCESS)
{
propertySize = sizeof(buffer);
if (CM_Get_DevNode_PropertyW(devInst, &DEVPKEY_NAME, &propertyType, (PBYTE) buffer, &propertySize, 0) == CR_SUCCESS)
ffStrbufSetWS(&device->name, buffer);
}
}
}
PHIDP_PREPARSED_DATA preparsedData = NULL;
if (HidD_GetPreparsedData(hHidFile, &preparsedData))
{
HIDP_CAPS caps;
NTSTATUS capsResult = HidP_GetCaps(preparsedData, &caps);
HidD_FreePreparsedData(preparsedData);
if (!NT_SUCCESS(capsResult))
continue;
wchar_t serialNumber[127] = L"";
if (HidD_GetSerialNumberString(hHidFile, serialNumber, sizeof(serialNumber)))
ffStrbufSetWS(&device->serial, serialNumber);
}
if (!device->name.length)
ffStrbufSetF(&device->name, "Unknown device %04X-%04X", (unsigned) rdi.hid.dwVendorId, (unsigned) rdi.hid.dwProductId);
}
return NULL;