diff --git a/CMakeLists.txt b/CMakeLists.txt index 77f717f13..115a7a68b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1312,6 +1312,7 @@ elseif(WIN32) PRIVATE "hid" PRIVATE "wtsapi32" PRIVATE "imagehlp" + PRIVATE "cfgmgr32" ) elseif(FreeBSD) target_link_libraries(libfastfetch diff --git a/src/detection/gamepad/gamepad_windows.c b/src/detection/gamepad/gamepad_windows.c index 4524e4471..e6e1be3f0 100644 --- a/src/detection/gamepad/gamepad_windows.c +++ b/src/detection/gamepad/gamepad_windows.c @@ -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 diff --git a/src/detection/keyboard/keyboard_windows.c b/src/detection/keyboard/keyboard_windows.c index 955cfc65a..b04e35d6c 100644 --- a/src/detection/keyboard/keyboard_windows.c +++ b/src/detection/keyboard/keyboard_windows.c @@ -1,3 +1,5 @@ +#define INITGUID + #include "keyboard.h" #include "common/io/io.h" #include "util/mallocHelper.h" @@ -6,6 +8,17 @@ #include #include #include +#include +#include + +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; diff --git a/src/detection/mouse/mouse_windows.c b/src/detection/mouse/mouse_windows.c index 67061c25d..29273fcd9 100644 --- a/src/detection/mouse/mouse_windows.c +++ b/src/detection/mouse/mouse_windows.c @@ -1,3 +1,5 @@ +#define INITGUID + #include "mouse.h" #include "common/io/io.h" #include "util/mallocHelper.h" @@ -6,6 +8,17 @@ #include #include #include +#include +#include + +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;