mirror of
https://github.com/RawAccelOfficial/rawaccel.git
synced 2025-05-11 18:36:46 +02:00
refactor io
This commit is contained in:
parent
7e1bd8c5e5
commit
b7bd9f5950
4 changed files with 66 additions and 87 deletions
|
@ -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" />
|
||||
|
|
12
common/rawaccel-io-def.h
Normal file
12
common/rawaccel-io-def.h
Normal file
|
@ -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)
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,10 +1,8 @@
|
|||
#include <rawaccel.hpp>
|
||||
#include <rawaccel-io-def.h>
|
||||
|
||||
#include "driver.h"
|
||||
|
||||
#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)
|
||||
|
||||
#ifdef ALLOC_PRAGMA
|
||||
#pragma alloc_text (INIT, DriverEntry)
|
||||
#pragma alloc_text (PAGE, EvtDeviceAdd)
|
||||
|
@ -144,70 +142,61 @@ Return Value:
|
|||
{
|
||||
NTSTATUS status;
|
||||
void* buffer;
|
||||
size_t size;
|
||||
|
||||
UNREFERENCED_PARAMETER(Queue);
|
||||
|
||||
|
||||
UNREFERENCED_PARAMETER(OutputBufferLength);
|
||||
UNREFERENCED_PARAMETER(InputBufferLength);
|
||||
PAGED_CODE();
|
||||
|
||||
DebugPrint(("Ioctl received into filter control object.\n"));
|
||||
|
||||
if (IoControlCode == RA_WRITE && InputBufferLength == sizeof(ra::settings)) {
|
||||
LARGE_INTEGER interval;
|
||||
interval.QuadPart = static_cast<LONGLONG>(ra::WRITE_DELAY) * -10000;
|
||||
KeDelayExecutionThread(KernelMode, FALSE, &interval);
|
||||
|
||||
status = WdfRequestRetrieveInputBuffer(
|
||||
Request,
|
||||
sizeof(ra::settings),
|
||||
&buffer,
|
||||
&size
|
||||
);
|
||||
|
||||
if (!NT_SUCCESS(status)) {
|
||||
DebugPrint(("RetrieveInputBuffer failed: 0x%x\n", status));
|
||||
// status maps to win32 error code 1359: ERROR_INTERNAL_ERROR
|
||||
WdfRequestComplete(Request, STATUS_MESSAGE_LOST);
|
||||
return;
|
||||
}
|
||||
|
||||
ra::settings new_settings = *reinterpret_cast<ra::settings*>(buffer);
|
||||
|
||||
if (new_settings.time_min <= 0 || _isnanf(static_cast<float>(new_settings.time_min))) {
|
||||
new_settings.time_min = ra::settings{}.time_min;
|
||||
}
|
||||
|
||||
global.args = new_settings;
|
||||
global.modifier = { global.args, global.lookups };
|
||||
|
||||
WdfRequestComplete(Request, STATUS_SUCCESS);
|
||||
}
|
||||
else if (IoControlCode == RA_READ && OutputBufferLength == sizeof(ra::settings)) {
|
||||
switch (IoControlCode) {
|
||||
case RA_READ:
|
||||
status = WdfRequestRetrieveOutputBuffer(
|
||||
Request,
|
||||
sizeof(ra::settings),
|
||||
&buffer,
|
||||
&size
|
||||
NULL
|
||||
);
|
||||
|
||||
if (!NT_SUCCESS(status)) {
|
||||
DebugPrint(("RetrieveOutputBuffer failed: 0x%x\n", status));
|
||||
// status maps to win32 error code 1359: ERROR_INTERNAL_ERROR
|
||||
WdfRequestComplete(Request, STATUS_MESSAGE_LOST);
|
||||
return;
|
||||
}
|
||||
else {
|
||||
*reinterpret_cast<ra::settings*>(buffer) = global.args;
|
||||
}
|
||||
break;
|
||||
case RA_WRITE:
|
||||
status = WdfRequestRetrieveInputBuffer(
|
||||
Request,
|
||||
sizeof(ra::settings),
|
||||
&buffer,
|
||||
NULL
|
||||
);
|
||||
if (!NT_SUCCESS(status)) {
|
||||
DebugPrint(("RetrieveInputBuffer failed: 0x%x\n", status));
|
||||
}
|
||||
else {
|
||||
LARGE_INTEGER interval;
|
||||
interval.QuadPart = static_cast<LONGLONG>(ra::WRITE_DELAY) * -10000;
|
||||
KeDelayExecutionThread(KernelMode, FALSE, &interval);
|
||||
|
||||
*reinterpret_cast<ra::settings*>(buffer) = global.args;
|
||||
ra::settings new_settings = *reinterpret_cast<ra::settings*>(buffer);
|
||||
|
||||
WdfRequestComplete(Request, STATUS_SUCCESS);
|
||||
}
|
||||
else {
|
||||
DebugPrint(("Received unknown request: in %uB, out %uB\n", InputBufferLength, OutputBufferLength));
|
||||
// status maps to win32 error code 1784: ERROR_INVALID_USER_BUFFER
|
||||
WdfRequestComplete(Request, STATUS_INVALID_BUFFER_SIZE);
|
||||
if (new_settings.time_min <= 0 || _isnanf(static_cast<float>(new_settings.time_min))) {
|
||||
new_settings.time_min = ra::settings{}.time_min;
|
||||
}
|
||||
|
||||
global.args = new_settings;
|
||||
global.modifier = { global.args, global.lookups };
|
||||
}
|
||||
break;
|
||||
default:
|
||||
status = STATUS_INVALID_DEVICE_REQUEST;
|
||||
break;
|
||||
}
|
||||
|
||||
WdfRequestComplete(Request, status);
|
||||
|
||||
}
|
||||
#pragma warning(pop) // enable 28118 again
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue