master ohmu/fastfetch-openbsd / src / detection / mouse / mouse_windows.c
ohmu  ·  2026-10-15
 1#define INITGUID
 2
 3#include "mouse.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* ffDetectMouse(FFlist* devices /* List of FFMouseDevice */) {
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    FF_AUTO_FREE RAWINPUTDEVICELIST* pRawInputDeviceList = (RAWINPUTDEVICELIST*) malloc(sizeof(RAWINPUTDEVICELIST) * nDevices);
25    if ((nDevices = GetRawInputDeviceList(pRawInputDeviceList, &nDevices, sizeof(RAWINPUTDEVICELIST))) == (UINT) -1) {
26        FF_DEBUG("GetRawInputDeviceList(pRawInputDeviceList) failed: %s", ffDebugWin32Error(GetLastError()));
27        return "GetRawInputDeviceList(pRawInputDeviceList) failed";
28    }
29
30    for (UINT i = 0; i < nDevices; ++i) {
31        if (pRawInputDeviceList[i].dwType != RIM_TYPEMOUSE) {
32            continue;
33        }
34
35        HANDLE hDevice = pRawInputDeviceList[i].hDevice;
36
37        RID_DEVICE_INFO rdi;
38        UINT rdiSize = sizeof(rdi);
39        if (GetRawInputDeviceInfoW(hDevice, RIDI_DEVICEINFO, &rdi, &rdiSize) == (UINT) -1) {
40            continue;
41        }
42
43        WCHAR devName[MAX_PATH];
44        UINT nameSize = MAX_PATH;
45        if (GetRawInputDeviceInfoW(hDevice, RIDI_DEVICENAME, devName, &nameSize) == (UINT) -1) {
46            continue;
47        }
48
49        FFMouseDevice* device = FF_LIST_ADD(FFMouseDevice, *devices);
50        ffStrbufInit(&device->serial);
51        ffStrbufInit(&device->name);
52
53        wchar_t buffer[MAX_PATH];
54
55        FF_AUTO_CLOSE_FD HANDLE hHidFile = CreateFileW(devName, 0 /* must be 0 instead of GENERIC_READ */, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr);
56        if (hHidFile != INVALID_HANDLE_VALUE) {
57            if (HidD_GetProductString(hHidFile, buffer, (ULONG) sizeof(buffer))) {
58                ffStrbufSetWS(&device->name, buffer);
59            }
60
61            if (HidD_GetSerialNumberString(hHidFile, buffer, sizeof(buffer))) {
62                ffStrbufSetWS(&device->serial, buffer);
63            }
64        }
65
66        if (!device->name.length) {
67            // https://stackoverflow.com/a/64321096/9976392
68            DEVPROPTYPE propertyType;
69            ULONG propertySize = sizeof(buffer);
70
71            if (CM_Get_Device_Interface_PropertyW(devName, &DEVPKEY_Device_InstanceId, &propertyType, (PBYTE) buffer, &propertySize, 0) == CR_SUCCESS) {
72                DEVINST devInst;
73                if (CM_Locate_DevNodeW(&devInst, buffer, CM_LOCATE_DEVNODE_NORMAL) == CR_SUCCESS) {
74                    propertySize = sizeof(buffer);
75                    if (CM_Get_DevNode_PropertyW(devInst, &DEVPKEY_NAME, &propertyType, (PBYTE) buffer, &propertySize, 0) == CR_SUCCESS) {
76                        ffStrbufSetWS(&device->name, buffer);
77                    }
78                }
79            }
80        }
81
82        if (!device->name.length) {
83            ffStrbufSetF(&device->name, "Unknown device %04X-%04X", (unsigned) rdi.hid.dwVendorId, (unsigned) rdi.hid.dwProductId);
84        }
85    }
86
87    return nullptr;
88}