master ohmu/fastfetch-openbsd / src / detection / keyboard / keyboard_windows.c
ohmu  ·  2026-10-15
 1#define INITGUID
 2
 3#include "keyboard.h"
 4#include "common/debug.h"
 5#include "common/io.h"
 6#include "common/mallocHelper.h"
 7#include "common/windows/unicode.h"
 8
 9#include <windows.h>
10#include <hidsdi.h>
11#include <cfgmgr32.h>
12#include <devpkey.h>
13
14const char* ffDetectKeyboard(FFlist* devices /* List of FFKeyboardDevice */) {
15    UINT nDevices = 0;
16    if (GetRawInputDeviceList(nullptr, &nDevices, sizeof(RAWINPUTDEVICELIST))) {
17        FF_DEBUG("GetRawInputDeviceList(nullptr) failed: %s", ffDebugWin32Error(GetLastError()));
18        return "GetRawInputDeviceList(nullptr) failed";
19    }
20    if (nDevices == 0) {
21        FF_DEBUG("No HID devices found");
22        return "No HID devices found";
23    }
24
25    FF_AUTO_FREE RAWINPUTDEVICELIST* pRawInputDeviceList = (RAWINPUTDEVICELIST*) malloc(sizeof(RAWINPUTDEVICELIST) * nDevices);
26    if ((nDevices = GetRawInputDeviceList(pRawInputDeviceList, &nDevices, sizeof(RAWINPUTDEVICELIST))) == (UINT) -1) {
27        FF_DEBUG("GetRawInputDeviceList(pRawInputDeviceList) failed: %s", ffDebugWin32Error(GetLastError()));
28        return "GetRawInputDeviceList(pRawInputDeviceList) failed";
29    }
30
31    for (UINT i = 0; i < nDevices; ++i) {
32        if (pRawInputDeviceList[i].dwType != RIM_TYPEKEYBOARD) {
33            continue;
34        }
35
36        HANDLE hDevice = pRawInputDeviceList[i].hDevice;
37
38        RID_DEVICE_INFO rdi;
39        UINT rdiSize = sizeof(rdi);
40        if (GetRawInputDeviceInfoW(hDevice, RIDI_DEVICEINFO, &rdi, &rdiSize) == (UINT) -1) {
41            continue;
42        }
43
44        WCHAR devName[MAX_PATH];
45        UINT nameSize = MAX_PATH;
46        if (GetRawInputDeviceInfoW(hDevice, RIDI_DEVICENAME, devName, &nameSize) == (UINT) -1) {
47            continue;
48        }
49
50        FFKeyboardDevice* device = FF_LIST_ADD(FFKeyboardDevice, *devices);
51        ffStrbufInit(&device->serial);
52        ffStrbufInit(&device->name);
53
54        wchar_t buffer[MAX_PATH];
55
56        FF_AUTO_CLOSE_FD HANDLE hHidFile = CreateFileW(devName, 0 /* must be 0 instead of GENERIC_READ */, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr);
57        if (hHidFile != INVALID_HANDLE_VALUE) {
58            if (HidD_GetProductString(hHidFile, buffer, (ULONG) sizeof(buffer))) {
59                ffStrbufSetWS(&device->name, buffer);
60            }
61
62            if (HidD_GetSerialNumberString(hHidFile, buffer, sizeof(buffer))) {
63                ffStrbufSetWS(&device->serial, buffer);
64            }
65        }
66
67        if (!device->name.length) {
68            // https://stackoverflow.com/a/64321096/9976392
69            DEVPROPTYPE propertyType;
70            ULONG propertySize = sizeof(buffer);
71
72            if (CM_Get_Device_Interface_PropertyW(devName, &DEVPKEY_Device_InstanceId, &propertyType, (PBYTE) buffer, &propertySize, 0) == CR_SUCCESS) {
73                DEVINST devInst;
74                if (CM_Locate_DevNodeW(&devInst, buffer, CM_LOCATE_DEVNODE_NORMAL) == CR_SUCCESS) {
75                    propertySize = sizeof(buffer);
76                    if (CM_Get_DevNode_PropertyW(devInst, &DEVPKEY_NAME, &propertyType, (PBYTE) buffer, &propertySize, 0) == CR_SUCCESS) {
77                        ffStrbufSetWS(&device->name, buffer);
78                    }
79                }
80            }
81        }
82
83        if (!device->name.length) {
84            ffStrbufSetF(&device->name, "Unknown device %04X-%04X", (unsigned) rdi.hid.dwVendorId, (unsigned) rdi.hid.dwProductId);
85        }
86    }
87
88    return nullptr;
89}