fix typo and wrapper/input code from lut2 merge

This commit is contained in:
a1xd 2021-07-06 16:10:52 -04:00
parent 31efc792f5
commit 9f1ebe0fea
8 changed files with 119 additions and 145 deletions

View file

@ -31,7 +31,6 @@
<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)vec2.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)utility.hpp" />
</ItemGroup>

View file

@ -1,103 +0,0 @@
#pragma once
#pragma comment(lib, "cfgmgr32.lib")
#include <algorithm>
#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>
template <typename Func>
void rawinput_foreach_dev_with_id(Func fn, bool with_instance_id = false,
DWORD input_type = RIM_TYPEMOUSE)
{
const UINT RI_ERROR = -1;
// get number of devices
UINT num_devs = 0;
if (GetRawInputDeviceList(NULL, &num_devs, sizeof(RAWINPUTDEVICELIST)) != 0) {
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) {
return;
}
std::wstring name;
std::wstring id;
DEVPROPTYPE type;
CONFIGRET cm_res;
for (auto&& dev : devs) {
if (dev.dwType != input_type) continue;
// get interface name length
UINT name_len = 0;
if (GetRawInputDeviceInfoW(dev.hDevice, RIDI_DEVICENAME, NULL, &name_len) == RI_ERROR) {
continue;
}
name.resize(name_len);
if (GetRawInputDeviceInfoW(dev.hDevice, RIDI_DEVICENAME, &name[0], &name_len) == RI_ERROR) {
continue;
}
// get sizeof dev instance id
ULONG id_size = 0;
cm_res = CM_Get_Device_Interface_PropertyW(&name[0], &DEVPKEY_Device_InstanceId,
&type, NULL, &id_size, 0);
if (cm_res != CR_BUFFER_SMALL && cm_res != CR_SUCCESS) continue;
id.resize((id_size + 1) / 2);
cm_res = CM_Get_Device_Interface_PropertyW(&name[0], &DEVPKEY_Device_InstanceId,
&type, reinterpret_cast<PBYTE>(&id[0]), &id_size, 0);
if (cm_res != CR_SUCCESS) continue;
if (!with_instance_id) {
auto instance_delim_pos = id.find_last_of('\\');
if (instance_delim_pos != std::string::npos) id.resize(instance_delim_pos);
}
fn(dev, id);
}
}
inline
std::vector<HANDLE> rawinput_handles_from_dev_id(const std::wstring& device_id,
bool with_instance_id = false,
DWORD input_type = RIM_TYPEMOUSE)
{
std::vector<HANDLE> handles;
rawinput_foreach_dev_with_id([&](const auto& dev, const std::wstring& id) {
if (id == device_id) handles.push_back(dev.hDevice);
}, with_instance_id, input_type);
return handles;
}
inline
std::vector<std::wstring> rawinput_dev_id_list(bool with_instance_id = false,
DWORD input_type = RIM_TYPEMOUSE)
{
std::vector<std::wstring> ids;
rawinput_foreach_dev_with_id([&](const auto& dev, const std::wstring& id) {
ids.push_back(id);
}, with_instance_id, input_type);
std::sort(ids.begin(), ids.end());
ids.erase(std::unique(ids.begin(), ids.end()), ids.end());
return ids;
}

View file

@ -90,7 +90,7 @@ namespace grapher.Models.Serialized
PollRateField.SetToEntered(GuiSettings.PollRate);
ShowLastMouseMoveMenuItem.Checked = GuiSettings.ShowLastMouseMove;
ShowVelocityAndGainMoveMenuItem.Checked = GuiSettings.ShowVelocityAndGain;
StreamingModeMenuItem.Checked = GUISettings.StreamingMode;
StreamingModeMenuItem.Checked = GuiSettings.StreamingMode;
AutoWriteMenuItem.Checked = GuiSettings.AutoWriteToDriverOnStartup;
}

View file

@ -1,43 +1,52 @@
#include "input.h"
#include "interop-exception.h"
#include <utility-rawinput.hpp>
#include <algorithm>
#include <msclr\marshal_cppstd.h>
#include <algorithm>
using namespace System;
using namespace System::Collections::Generic;
struct device_info {
std::wstring name;
std::wstring id;
};
std::vector<HANDLE> rawinput_handles_from_id(const std::wstring& device_id)
{
std::vector<HANDLE> handles;
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)
});
rawinput_foreach([&](const auto& dev) {
if (dev.id == device_id) handles.push_back(dev.handle);
});
std::sort(info.begin(), info.end(),
[](auto&& l, auto&& r) { return l.id < r.id; });
auto last = std::unique(info.begin(), info.end(),
[](auto&& l, auto&& r) { return l.id == r.id; });
info.erase(last, info.end());
return info;
return handles;
}
std::vector<std::wstring> rawinput_id_list()
{
std::vector<std::wstring> ids;
rawinput_foreach([&](const auto& dev) {
ids.push_back(dev.id);
});
std::sort(ids.begin(), ids.end());
ids.erase(std::unique(ids.begin(), ids.end()), ids.end());
return ids;
}
public ref struct RawInputInteropException : InteropException {
RawInputInteropException(System::String^ what) :
InteropException(what) {}
RawInputInteropException(const char* what) :
InteropException(what) {}
RawInputInteropException(const std::exception& e) :
InteropException(e) {}
};
public ref struct RawInputInterop
{
static void AddHandlesFromID(String^ deviceID, List<IntPtr>^ rawInputHandles)
{
try
{
std::vector<HANDLE> nativeHandles = rawinput_handles_from_dev_id(
std::vector<HANDLE> nativeHandles = rawinput_handles_from_id(
msclr::interop::marshal_as<std::wstring>(deviceID));
for (auto nh : nativeHandles) rawInputHandles->Add(IntPtr(nh));
@ -48,21 +57,18 @@ public ref struct RawInputInterop
}
}
static List<ValueTuple<String^, String^>>^ GetDeviceIDs()
static List<String^>^ GetDeviceIDs()
{
try
{
auto managed = gcnew List<ValueTuple<String^, String^>>();
auto ids = gcnew List<String^>();
for (auto&& [name, id] : get_unique_device_info())
for (auto&& name : rawinput_id_list())
{
managed->Add(
ValueTuple<String^, String^>(
msclr::interop::marshal_as<String^>(name),
msclr::interop::marshal_as<String^>(id)));
ids->Add(msclr::interop::marshal_as<String^>(name));
}
return managed;
return ids;
}
catch (const std::exception& e)
{
@ -71,4 +77,3 @@ public ref struct RawInputInterop
}
};

78
wrapper/input.h Normal file
View file

@ -0,0 +1,78 @@
#pragma once
#pragma comment(lib, "cfgmgr32.lib")
#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>
struct rawinput_device {
HANDLE handle;
std::wstring id;
};
template <typename Func>
void rawinput_foreach(Func fn, DWORD device_type = RIM_TYPEMOUSE)
{
const UINT RI_ERROR = -1;
// get number of devices
UINT num_devs = 0;
if (GetRawInputDeviceList(NULL, &num_devs, sizeof(RAWINPUTDEVICELIST)) != 0) {
throw std::system_error(GetLastError(), std::system_category(), "GetRawInputDeviceList failed");
}
auto dev_list = std::vector<RAWINPUTDEVICELIST>(num_devs);
if (GetRawInputDeviceList(&dev_list[0], &num_devs, sizeof(RAWINPUTDEVICELIST)) == RI_ERROR) {
return;
}
std::wstring name;
rawinput_device dev;
DEVPROPTYPE prop_type;
CONFIGRET cm_res;
for (auto [handle, dev_type] : dev_list) {
if (dev_type != device_type) continue;
// get interface name length
UINT name_len = 0;
if (GetRawInputDeviceInfoW(handle, RIDI_DEVICENAME, NULL, &name_len) == RI_ERROR) {
continue;
}
name.resize(name_len);
if (GetRawInputDeviceInfoW(handle, RIDI_DEVICENAME, &name[0], &name_len) == RI_ERROR) {
continue;
}
// get sizeof device instance id
ULONG id_size = 0;
cm_res = CM_Get_Device_Interface_PropertyW(&name[0], &DEVPKEY_Device_InstanceId,
&prop_type, NULL, &id_size, 0);
if (cm_res != CR_BUFFER_SMALL && cm_res != CR_SUCCESS) continue;
dev.id.resize((id_size + 1) / 2);
cm_res = CM_Get_Device_Interface_PropertyW(&name[0], &DEVPKEY_Device_InstanceId,
&prop_type, reinterpret_cast<PBYTE>(&dev.id[0]), &id_size, 0);
if (cm_res != CR_SUCCESS) continue;
// pop instance id
auto instance_delim_pos = dev.id.find_last_of('\\');
if (instance_delim_pos != std::string::npos) dev.id.resize(instance_delim_pos);
dev.handle = handle;
fn(dev);
}
}

View file

@ -10,12 +10,3 @@ public ref struct InteropException : System::Exception {
InteropException(const std::exception& e) :
InteropException(e.what()) {}
};
public ref struct RawInputInteropException : InteropException {
RawInputInteropException(System::String^ what) :
InteropException(what) {}
RawInputInteropException(const char* what) :
InteropException(what) {}
RawInputInteropException(const std::exception& e) :
InteropException(e) {}
};

View file

@ -85,6 +85,7 @@ copy /Y "$(TargetDir)Newtonsoft.Json.dll" "$(SolutionDir)signed\Newtonsoft.Json.
</ResourceCompile>
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="input.h" />
<ClInclude Include="interop-exception.h" />
<ClInclude Include="resource.h" />
</ItemGroup>

View file

@ -21,6 +21,9 @@
<ClInclude Include="interop-exception.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="input.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="wrapper.cpp">