diff options
Diffstat (limited to 'common/rawaccel-io.hpp')
| -rw-r--r-- | common/rawaccel-io.hpp | 122 |
1 files changed, 70 insertions, 52 deletions
diff --git a/common/rawaccel-io.hpp b/common/rawaccel-io.hpp index 703ea92..a80e254 100644 --- a/common/rawaccel-io.hpp +++ b/common/rawaccel-io.hpp @@ -1,66 +1,84 @@ #pragma once -#include <system_error> - -#define NOMINMAX -#include <Windows.h> - #include "rawaccel-io-def.h" -#include "rawaccel-settings.h" #include "rawaccel-version.h" #include "rawaccel-error.hpp" +#include "rawaccel.hpp" #pragma warning(push) #pragma warning(disable:4245) // int -> DWORD conversion while passing CTL_CODE namespace rawaccel { - 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); - - if (ra_handle == INVALID_HANDLE_VALUE) { - throw install_error(); - } - - DWORD dummy; - - BOOL success = DeviceIoControl( - ra_handle, - code, - in, - in_size, - out, - out_size, - &dummy, // bytes returned - NULL // overlapped structure - ); - - CloseHandle(ra_handle); - - 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) { - auto in_ptr = const_cast<settings*>(&args); - io_control(RA_WRITE, in_ptr, sizeof(settings), NULL, 0); - } - - version_t get_version() { - version_t ver; - io_control(RA_GET_VERSION, NULL, 0, &ver, sizeof(version_t)); - return ver; - } + inline 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); + + if (ra_handle == INVALID_HANDLE_VALUE) { + throw install_error(); + } + + DWORD dummy; + + BOOL success = DeviceIoControl( + ra_handle, + code, + in, + in_size, + out, + out_size, + &dummy, // bytes returned + NULL // overlapped structure + ); + + CloseHandle(ra_handle); + + if (!success) { + throw sys_error("DeviceIoControl failed"); + } + } + + inline void read(io_t& args) + { + io_control(RA_READ, NULL, 0, &args, sizeof(io_t)); + } + + inline void write(const io_t& args) + { + io_control(RA_WRITE, const_cast<io_t*>(&args), sizeof(io_t), NULL, 0); + } + + inline version_t get_version() + { + version_t v; + + try { + io_control(RA_GET_VERSION, NULL, 0, &v, sizeof(version_t)); + } + catch (const sys_error&) { + // assume request is not implemented (< 1.3) + v = { 0 }; + } + + return v; + } + + inline version_t valid_version_or_throw() + { + auto v = get_version(); + + if (v < min_driver_version) { + throw error("reinstallation required"); + } + + if (version < v) { + throw error("newer driver is installed"); + } + + return v; + } } |