summaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
authora1xd <[email protected]>2020-12-01 01:41:22 -0500
committera1xd <[email protected]>2020-12-01 01:41:22 -0500
commitb7bd9f5950d9712217e2dd5b40f2aeb82294e3c7 (patch)
tree4efb25483733ed5e259c1e3dd20df029fe303a04 /common
parentfix dbgprint warning (diff)
downloadrawaccel-b7bd9f5950d9712217e2dd5b40f2aeb82294e3c7.tar.xz
rawaccel-b7bd9f5950d9712217e2dd5b40f2aeb82294e3c7.zip
refactor io
Diffstat (limited to 'common')
-rw-r--r--common/common.vcxitems1
-rw-r--r--common/rawaccel-io-def.h12
-rw-r--r--common/rawaccel-io.hpp53
3 files changed, 28 insertions, 38 deletions
diff --git a/common/common.vcxitems b/common/common.vcxitems
index 1eabfd7..9c0a208 100644
--- a/common/common.vcxitems
+++ b/common/common.vcxitems
@@ -23,6 +23,7 @@
<ClInclude Include="$(MSBuildThisFileDirectory)accel-noaccel.hpp" />
<ClInclude Include="$(MSBuildThisFileDirectory)accel-power.hpp" />
<ClInclude Include="$(MSBuildThisFileDirectory)rawaccel-error.hpp" />
+ <ClInclude Include="$(MSBuildThisFileDirectory)rawaccel-io-def.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)rawaccel-io.hpp" />
<ClInclude Include="$(MSBuildThisFileDirectory)rawaccel-settings.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)rawaccel.hpp" />
diff --git a/common/rawaccel-io-def.h b/common/rawaccel-io-def.h
new file mode 100644
index 0000000..791addb
--- /dev/null
+++ b/common/rawaccel-io-def.h
@@ -0,0 +1,12 @@
+#pragma once
+
+#ifdef _KERNEL_MODE
+#include <ntddk.h>
+#else
+#include <winioctl.h>
+#endif
+
+#define RA_DEV_TYPE 0x8888u
+
+#define RA_READ CTL_CODE(RA_DEV_TYPE, 0x888, METHOD_OUT_DIRECT, FILE_ANY_ACCESS)
+#define RA_WRITE CTL_CODE(RA_DEV_TYPE, 0x889, METHOD_BUFFERED, FILE_ANY_ACCESS)
diff --git a/common/rawaccel-io.hpp b/common/rawaccel-io.hpp
index e8641d1..4159b60 100644
--- a/common/rawaccel-io.hpp
+++ b/common/rawaccel-io.hpp
@@ -5,18 +5,16 @@
#define NOMINMAX
#include <Windows.h>
+#include "rawaccel-io-def.h"
#include "rawaccel-settings.h"
#include "rawaccel-error.hpp"
-#define RA_READ CTL_CODE(0x8888, 0x888, METHOD_OUT_DIRECT, FILE_ANY_ACCESS)
-#define RA_WRITE CTL_CODE(0x8888, 0x889, METHOD_BUFFERED, FILE_ANY_ACCESS)
-
#pragma warning(push)
#pragma warning(disable:4245) // int -> DWORD conversion while passing CTL_CODE
namespace rawaccel {
- settings read() {
+ void io_control(DWORD code, void* in, DWORD in_size, void* out, DWORD out_size) {
HANDLE ra_handle = INVALID_HANDLE_VALUE;
ra_handle = CreateFileW(L"\\\\.\\rawaccel", 0, 0, 0, OPEN_EXISTING, 0, 0);
@@ -25,18 +23,17 @@ namespace rawaccel {
throw install_error();
}
- settings args;
DWORD dummy;
BOOL success = DeviceIoControl(
ra_handle,
- RA_READ,
- NULL, // input buffer
- 0, // input buffer size
- &args, // output buffer
- sizeof(settings), // output buffer size
- &dummy, // bytes returned
- NULL // overlapped structure
+ code,
+ in,
+ in_size,
+ out,
+ out_size,
+ &dummy, // bytes returned
+ NULL // overlapped structure
);
CloseHandle(ra_handle);
@@ -44,38 +41,18 @@ namespace rawaccel {
if (!success) {
throw std::system_error(GetLastError(), std::system_category(), "DeviceIoControl failed");
}
+ }
+ settings read() {
+ settings args;
+ io_control(RA_READ, NULL, 0, &args, sizeof(settings));
return args;
}
void write(const settings& args) {
- HANDLE ra_handle = INVALID_HANDLE_VALUE;
-
- ra_handle = CreateFileW(L"\\\\.\\rawaccel", 0, 0, 0, OPEN_EXISTING, 0, 0);
-
- if (ra_handle == INVALID_HANDLE_VALUE) {
- throw install_error();
- }
-
- DWORD dummy;
-
- BOOL success = DeviceIoControl(
- ra_handle,
- RA_WRITE,
- const_cast<settings*>(&args), // input buffer
- sizeof(settings), // input buffer size
- NULL, // output buffer
- 0, // output buffer size
- &dummy, // bytes returned
- NULL // overlapped structure
- );
-
- CloseHandle(ra_handle);
-
- if (!success) {
- throw std::system_error(GetLastError(), std::system_category(), "DeviceIoControl failed");
- }
+ auto in_ptr = const_cast<settings*>(&args);
+ io_control(RA_WRITE, in_ptr, sizeof(settings), NULL, 0);
}
}