diff options
| author | a1xd <[email protected]> | 2021-04-25 21:44:45 -0400 |
|---|---|---|
| committer | a1xd <[email protected]> | 2021-04-25 21:44:45 -0400 |
| commit | 046c6bbef0a2a5f208c80f0d4666411ce991ef4a (patch) | |
| tree | 38b1ddb713492da54c6e6e1844003035bcf3db2d | |
| parent | Merge pull request #81 from a1xd/log-unhandled-ex (diff) | |
| download | rawaccel-046c6bbef0a2a5f208c80f0d4666411ce991ef4a.tar.xz rawaccel-046c6bbef0a2a5f208c80f0d4666411ce991ef4a.zip | |
ignore cfgmgr errors when retrieving device id
| -rw-r--r-- | common/utility-rawinput.hpp | 37 | ||||
| -rw-r--r-- | wrapper/wrapper.cpp | 13 |
2 files changed, 27 insertions, 23 deletions
diff --git a/common/utility-rawinput.hpp b/common/utility-rawinput.hpp index c43084b..5506109 100644 --- a/common/utility-rawinput.hpp +++ b/common/utility-rawinput.hpp @@ -11,35 +11,33 @@ #include <initguid.h> // needed for devpkey.h to parse properly #include <devpkey.h> -std::wstring dev_prop_wstr_from_interface(const WCHAR* interface_name, const DEVPROPKEY* key) { +// Returns an empty string on failure +// +// interface names from GetRawInputDeviceInfo are not guaranteed to be valid; +// CM_Get_Device_Interface_PropertyW can return CR_NO_SUCH_DEVICE_INTERFACE +std::wstring dev_id_from_interface(const WCHAR* interface_name) { ULONG size = 0; DEVPROPTYPE type; CONFIGRET cm_res; - cm_res = CM_Get_Device_Interface_PropertyW(interface_name, key, + cm_res = CM_Get_Device_Interface_PropertyW(interface_name, &DEVPKEY_Device_InstanceId, &type, NULL, &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) + ')'); - } + if (cm_res != CR_BUFFER_SMALL && cm_res != CR_SUCCESS) return {}; - std::wstring prop((size + 1) / 2, L'\0'); + std::wstring id((size + 1) / 2, L'\0'); - cm_res = CM_Get_Device_Interface_PropertyW(interface_name, key, - &type, reinterpret_cast<PBYTE>(&prop[0]), &size, 0); + cm_res = CM_Get_Device_Interface_PropertyW(interface_name, &DEVPKEY_Device_InstanceId, + &type, reinterpret_cast<PBYTE>(&id[0]), &size, 0); - if (cm_res != CR_SUCCESS) { - throw std::runtime_error("CM_Get_Device_Interface_PropertyW failed (" + - std::to_string(cm_res) + ')'); - } + if (cm_res != CR_SUCCESS) return {}; - return prop; -} + auto instance_delim_pos = id.find_last_of('\\'); + + if (instance_delim_pos != std::string::npos) { + id.resize(instance_delim_pos); + } -std::wstring dev_id_from_interface(const WCHAR* interface_name) { - auto id = dev_prop_wstr_from_interface(interface_name, &DEVPKEY_Device_InstanceId); - id.resize(id.find_last_of('\\')); return id; } @@ -79,7 +77,8 @@ std::vector<HANDLE> rawinput_handles_from_dev_id(const std::wstring& device_id, std::vector<HANDLE> handles; rawinput_foreach_with_interface([&](const auto& dev, const WCHAR* name) { - if (device_id == dev_id_from_interface(name)) { + auto id = dev_id_from_interface(name); + if (!id.empty() && id == device_id) { handles.push_back(dev.hDevice); } }, input_type); diff --git a/wrapper/wrapper.cpp b/wrapper/wrapper.cpp index 71a8cf6..5f44859 100644 --- a/wrapper/wrapper.cpp +++ b/wrapper/wrapper.cpp @@ -286,10 +286,15 @@ std::vector<device_info> get_unique_device_info() { std::vector<device_info> info; rawinput_foreach_with_interface([&](const auto& dev, const WCHAR* name) { - info.push_back({ - L"", // get_property_wstr(name, &DEVPKEY_Device_FriendlyName), /* doesn't work */ - dev_id_from_interface(name) - }); + auto id = dev_id_from_interface(name); + + if (!id.empty()) { + info.push_back({ + L"", // get_property_wstr(name, &DEVPKEY_Device_FriendlyName), /* doesn't work */ + id + }); + } + }); std::sort(info.begin(), info.end(), |