diff options
| author | Jacob Palecki <[email protected]> | 2021-01-20 00:42:24 -0800 |
|---|---|---|
| committer | Jacob Palecki <[email protected]> | 2021-01-20 00:42:24 -0800 |
| commit | 77b4c7876918cac82494ec8fc15a22fdab5cf714 (patch) | |
| tree | 66b2c7d86cecf0846c3b5cdaa69c6991858ed931 /common | |
| parent | Remove debug statement (diff) | |
| parent | show custom dialog on bad input (#63) (diff) | |
| download | rawaccel-77b4c7876918cac82494ec8fc15a22fdab5cf714.tar.xz rawaccel-77b4c7876918cac82494ec8fc15a22fdab5cf714.zip | |
merge with master builds
Diffstat (limited to 'common')
| -rw-r--r-- | common/common.vcxitems | 1 | ||||
| -rw-r--r-- | common/rawaccel-io-def.h | 4 | ||||
| -rw-r--r-- | common/rawaccel-settings.h | 3 | ||||
| -rw-r--r-- | common/utility-rawinput.hpp | 74 |
4 files changed, 80 insertions, 2 deletions
diff --git a/common/common.vcxitems b/common/common.vcxitems index 2b03405..ba9bd98 100644 --- a/common/common.vcxitems +++ b/common/common.vcxitems @@ -29,6 +29,7 @@ <ClInclude Include="$(MSBuildThisFileDirectory)rawaccel-version.h" /> <ClInclude Include="$(MSBuildThisFileDirectory)rawaccel.hpp" /> <ClInclude Include="$(MSBuildThisFileDirectory)utility-install.hpp" /> + <ClInclude Include="$(MSBuildThisFileDirectory)utility-rawinput.hpp" /> <ClInclude Include="$(MSBuildThisFileDirectory)x64-util.hpp" /> <ClInclude Include="$(MSBuildThisFileDirectory)vec2.h" /> </ItemGroup> diff --git a/common/rawaccel-io-def.h b/common/rawaccel-io-def.h index d8d4088..e169390 100644 --- a/common/rawaccel-io-def.h +++ b/common/rawaccel-io-def.h @@ -8,6 +8,6 @@ #define RA_DEV_TYPE 0x8888u -#define RA_READ CTL_CODE(RA_DEV_TYPE, 0x888, METHOD_OUT_DIRECT, FILE_ANY_ACCESS) +#define RA_READ CTL_CODE(RA_DEV_TYPE, 0x888, METHOD_BUFFERED, FILE_ANY_ACCESS) #define RA_WRITE CTL_CODE(RA_DEV_TYPE, 0x889, METHOD_BUFFERED, FILE_ANY_ACCESS) -#define RA_GET_VERSION CTL_CODE(RA_DEV_TYPE, 0x88a, METHOD_OUT_DIRECT, FILE_ANY_ACCESS) +#define RA_GET_VERSION CTL_CODE(RA_DEV_TYPE, 0x88a, METHOD_BUFFERED, FILE_ANY_ACCESS) diff --git a/common/rawaccel-settings.h b/common/rawaccel-settings.h index 755fa2c..6fa2aa9 100644 --- a/common/rawaccel-settings.h +++ b/common/rawaccel-settings.h @@ -3,6 +3,8 @@ #include "vec2.h" #include "accel-base.hpp" +#define MAX_DEV_ID_LEN 200 + namespace rawaccel { using milliseconds = double; @@ -25,6 +27,7 @@ namespace rawaccel { domain_args domain_args = {}; vec2d range_weights = { 1, 1 }; milliseconds time_min = DEFAULT_TIME_MIN; + wchar_t device_id[MAX_DEV_ID_LEN] = {0}; }; } diff --git a/common/utility-rawinput.hpp b/common/utility-rawinput.hpp new file mode 100644 index 0000000..3c44068 --- /dev/null +++ b/common/utility-rawinput.hpp @@ -0,0 +1,74 @@ +#pragma once + +#pragma comment(lib, "cfgmgr32.lib") + +#include <iostream> +#include <string> +#include <system_error> +#include <vector> + +#include <Windows.h> +#include <cfgmgr32.h> +#include <initguid.h> // needed for devpkey.h to parse properly +#include <devpkey.h> + +// returns device handles corresponding to a "device id" +// https://docs.microsoft.com/en-us/windows-hardware/drivers/install/device-ids +std::vector<HANDLE> rawinput_handles_from_dev_id(const std::wstring& dev_id, DWORD input_type = RIM_TYPEMOUSE) { + const UINT RI_ERROR = -1; + + UINT num_devs = 0; + + if (GetRawInputDeviceList(NULL, &num_devs, sizeof(RAWINPUTDEVICELIST)) == RI_ERROR) { + throw std::system_error(GetLastError(), std::system_category(), "GetRawInputDeviceList failed"); + } + + auto devs = std::vector<RAWINPUTDEVICELIST>(num_devs); + + if (GetRawInputDeviceList(&devs[0], &num_devs, sizeof(RAWINPUTDEVICELIST)) == RI_ERROR) { + throw std::system_error(GetLastError(), std::system_category(), "GetRawInputDeviceList failed"); + } + + std::vector<HANDLE> handles; + + for (auto&& dev : devs) { + if (dev.dwType != input_type) continue; + + WCHAR name[256] = {}; + UINT name_size = sizeof(name); + + if (GetRawInputDeviceInfoW(dev.hDevice, RIDI_DEVICENAME, name, &name_size) == RI_ERROR) { + throw std::system_error(GetLastError(), std::system_category(), "GetRawInputDeviceInfoW failed"); + } + + ULONG id_size = 0; + DEVPROPTYPE type; + CONFIGRET cm_res; + + cm_res = CM_Get_Device_Interface_PropertyW(name, &DEVPKEY_Device_InstanceId, + &type, NULL, &id_size, 0); + + if (cm_res != CR_BUFFER_SMALL && cm_res != CR_SUCCESS) { + throw std::runtime_error("CM_Get_Device_Interface_PropertyW failed (" + + std::to_string(cm_res) + ')'); + } + + std::wstring id((static_cast<size_t>(id_size) + 1) / 2, '\0'); + + cm_res = CM_Get_Device_Interface_PropertyW(name, &DEVPKEY_Device_InstanceId, + &type, reinterpret_cast<PBYTE>(&id[0]), &id_size, 0); + + if (cm_res != CR_SUCCESS) { + throw std::runtime_error("CM_Get_Device_Interface_PropertyW failed (" + + std::to_string(cm_res) + ')'); + } + + // remove instance id + id.resize(id.find_last_of('\\')); + + if (id == dev_id) handles.push_back(dev.hDevice); + } + + return handles; +} + |