From 16dc4df3d438142ae378c9c6983585d06e0c6a33 Mon Sep 17 00:00:00 2001 From: a1xd <68629610+a1xd@users.noreply.github.com> Date: Mon, 29 Mar 2021 17:14:49 -0400 Subject: refactor common/settings only driver compiles remove accel-base types merge linear + classic move gain cap logic into classic impl, cap is now set in terms of output use cap/limit to determine negation remove weight, add replacement for power mode only remove legacy offset option remove naturalgain mode add legacy mode flag naturalgain -> natural natural -> natural + legacy flag add dpi setting and more accel args + defaults (prep for ips mode) replace output speed cap with input cap --- driver/driver.cpp | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) (limited to 'driver/driver.cpp') diff --git a/driver/driver.cpp b/driver/driver.cpp index 0c21f34..235571c 100644 --- a/driver/driver.cpp +++ b/driver/driver.cpp @@ -1,8 +1,9 @@ +#include "driver.h" + #include #include #include -#include "driver.h" #ifdef ALLOC_PRAGMA #pragma alloc_text (INIT, DriverEntry) @@ -11,8 +12,6 @@ #pragma alloc_text (PAGE, RawaccelControl) #endif -namespace ra = rawaccel; - using milliseconds = double; using lut_value_t = ra::si_pair; @@ -21,7 +20,7 @@ struct { milliseconds tick_interval = 0; // set in DriverEntry ra::mouse_modifier modifier; vec2 lookups = {}; -} global; +} global = {}; VOID RawaccelCallback( @@ -59,7 +58,7 @@ Arguments: bool any = num_packets > 0; bool rel_move = !(InputDataStart->Flags & MOUSE_MOVE_ABSOLUTE); bool dev_match = global.args.device_id[0] == 0 || - wcsncmp(devExt->dev_id, global.args.device_id, MAX_DEV_ID_LEN) == 0; + wcsncmp(devExt->dev_id, global.args.device_id, ra::MAX_DEV_ID_LEN) == 0; if (any && rel_move && dev_match) { // if IO is backed up to the point where we get more than 1 packet here @@ -507,7 +506,7 @@ Return Value: if (NT_SUCCESS(nts)) { auto* id_ptr = reinterpret_cast(iosb.Information); - wcsncpy(FilterGetData(hDevice)->dev_id, id_ptr, MAX_DEV_ID_LEN); + wcsncpy(FilterGetData(hDevice)->dev_id, id_ptr, ra::MAX_DEV_ID_LEN); DebugPrint(("Device ID = %ws\n", id_ptr)); ExFreePool(id_ptr); } -- cgit v1.2.3 From 11045335c14371847411b8fb5096f479e18fbf5e Mon Sep 17 00:00:00 2001 From: a1xd <68629610+a1xd@users.noreply.github.com> Date: Mon, 29 Mar 2021 19:57:33 -0400 Subject: add zero/inf/nan guards --- driver/driver.cpp | 57 ++++++++++++++++++++++++++++++------------------------- 1 file changed, 31 insertions(+), 26 deletions(-) (limited to 'driver/driver.cpp') diff --git a/driver/driver.cpp b/driver/driver.cpp index 235571c..5b9eca4 100644 --- a/driver/driver.cpp +++ b/driver/driver.cpp @@ -67,40 +67,45 @@ Arguments: auto it = InputDataStart; do { - vec2d input = { - static_cast(it->LastX), - static_cast(it->LastY) - }; - - global.modifier.apply_rotation(input); - global.modifier.apply_angle_snap(input); - - if (enable_accel) { - auto time_supplier = [=] { - counter_t now = KeQueryPerformanceCounter(NULL).QuadPart; - counter_t ticks = now - devExt->counter; - devExt->counter = now; - milliseconds time = ticks * global.tick_interval; - return clampsd(time, global.args.time_min, 100); + if (it->LastX || it->LastY) { + vec2d input = { + static_cast(it->LastX), + static_cast(it->LastY) }; - global.modifier.apply_acceleration(input, time_supplier); - } + global.modifier.apply_rotation(input); + global.modifier.apply_angle_snap(input); + + if (enable_accel) { + auto time_supplier = [=] { + counter_t now = KeQueryPerformanceCounter(NULL).QuadPart; + counter_t ticks = now - devExt->counter; + devExt->counter = now; + milliseconds time = ticks * global.tick_interval; + return clampsd(time, global.args.time_min, 100); + }; - global.modifier.apply_sensitivity(input); + global.modifier.apply_acceleration(input, time_supplier); + } - double carried_result_x = input.x + devExt->carry.x; - double carried_result_y = input.y + devExt->carry.y; + global.modifier.apply_sensitivity(input); - LONG out_x = static_cast(carried_result_x); - LONG out_y = static_cast(carried_result_y); + double carried_result_x = input.x + devExt->carry.x; + double carried_result_y = input.y + devExt->carry.y; - devExt->carry.x = carried_result_x - out_x; - devExt->carry.y = carried_result_y - out_y; + LONG out_x = static_cast(carried_result_x); + LONG out_y = static_cast(carried_result_y); - it->LastX = out_x; - it->LastY = out_y; + double carry_x = carried_result_x - out_x; + double carry_y = carried_result_y - out_y; + if (!infnan(carry_x + carry_y)) { + devExt->carry.x = carried_result_x - out_x; + devExt->carry.y = carried_result_y - out_y; + it->LastX = out_x; + it->LastY = out_y; + } + } } while (++it != InputDataEnd); } -- cgit v1.2.3 From 4456e2bc8b9c1ef9c1aa2e3509adc8f456bb5fdc Mon Sep 17 00:00:00 2001 From: a1xd <68629610+a1xd@users.noreply.github.com> Date: Tue, 30 Mar 2021 18:26:20 -0400 Subject: put utility in namespace --- driver/driver.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'driver/driver.cpp') diff --git a/driver/driver.cpp b/driver/driver.cpp index 5b9eca4..49f375d 100644 --- a/driver/driver.cpp +++ b/driver/driver.cpp @@ -82,7 +82,7 @@ Arguments: counter_t ticks = now - devExt->counter; devExt->counter = now; milliseconds time = ticks * global.tick_interval; - return clampsd(time, global.args.time_min, 100); + return ra::clampsd(time, global.args.time_min, 100); }; global.modifier.apply_acceleration(input, time_supplier); @@ -99,7 +99,7 @@ Arguments: double carry_x = carried_result_x - out_x; double carry_y = carried_result_y - out_y; - if (!infnan(carry_x + carry_y)) { + if (!ra::infnan(carry_x + carry_y)) { devExt->carry.x = carried_result_x - out_x; devExt->carry.y = carried_result_y - out_y; it->LastX = out_x; -- cgit v1.2.3 From fa3ebfb1eb054ba88824a908c996094bb98e85c5 Mon Sep 17 00:00:00 2001 From: a1xd <68629610+a1xd@users.noreply.github.com> Date: Tue, 30 Mar 2021 18:27:02 -0400 Subject: refactor lut/motivity --- driver/driver.cpp | 41 +++++++++++------------------------------ 1 file changed, 11 insertions(+), 30 deletions(-) (limited to 'driver/driver.cpp') diff --git a/driver/driver.cpp b/driver/driver.cpp index 49f375d..89f09dc 100644 --- a/driver/driver.cpp +++ b/driver/driver.cpp @@ -13,13 +13,11 @@ #endif using milliseconds = double; -using lut_value_t = ra::si_pair; struct { ra::settings args; milliseconds tick_interval = 0; // set in DriverEntry ra::mouse_modifier modifier; - vec2 lookups = {}; } global = {}; VOID @@ -164,7 +162,7 @@ Return Value: case RA_READ: status = WdfRequestRetrieveOutputBuffer( Request, - sizeof(ra::settings), + sizeof(ra::io_t), &buffer, NULL ); @@ -172,14 +170,18 @@ Return Value: DebugPrint(("RetrieveOutputBuffer failed: 0x%x\n", status)); } else { - *reinterpret_cast(buffer) = global.args; - bytes_out = sizeof(ra::settings); + ra::io_t& output = *reinterpret_cast(buffer); + + output.args = global.args; + output.mod = global.modifier; + + bytes_out = sizeof(ra::io_t); } break; case RA_WRITE: status = WdfRequestRetrieveInputBuffer( Request, - sizeof(ra::settings), + sizeof(ra::io_t), &buffer, NULL ); @@ -191,14 +193,10 @@ Return Value: interval.QuadPart = static_cast(ra::WRITE_DELAY) * -10000; KeDelayExecutionThread(KernelMode, FALSE, &interval); - ra::settings new_settings = *reinterpret_cast(buffer); - - if (new_settings.time_min <= 0 || _isnanf(static_cast(new_settings.time_min))) { - new_settings.time_min = ra::settings{}.time_min; - } + ra::io_t& input = *reinterpret_cast(buffer); - global.args = new_settings; - global.modifier = { global.args, global.lookups }; + global.args = input.args; + global.modifier = input.mod; } break; case RA_GET_VERSION: @@ -276,23 +274,6 @@ Routine Description: KeQueryPerformanceCounter(&freq); global.tick_interval = 1e3 / freq.QuadPart; - auto make_lut = [] { - const size_t POOL_SIZE = sizeof(lut_value_t) * ra::LUT_SIZE; - - auto pool = ExAllocatePoolWithTag(NonPagedPool, POOL_SIZE, 'AR'); - - if (!pool) { - DebugPrint(("RA - failed to allocate LUT\n")); - } - else { - RtlZeroMemory(pool, POOL_SIZE); - } - - return reinterpret_cast(pool); - }; - - global.lookups = { make_lut(), make_lut() }; - CreateControlDevice(driver); } else { -- cgit v1.2.3 From 14bde56daf188bfc027dc8ead5b45ec0aa1109d6 Mon Sep 17 00:00:00 2001 From: a1xd <68629610+a1xd@users.noreply.github.com> Date: Thu, 1 Apr 2021 01:51:31 -0400 Subject: update rest grapher is still broken refactored io / error handling a bit --- driver/driver.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'driver/driver.cpp') diff --git a/driver/driver.cpp b/driver/driver.cpp index 89f09dc..fbc2d3d 100644 --- a/driver/driver.cpp +++ b/driver/driver.cpp @@ -210,7 +210,7 @@ Return Value: DebugPrint(("RetrieveOutputBuffer failed: 0x%x\n", status)); } else { - *reinterpret_cast(buffer) = { RA_VER_MAJOR, RA_VER_MINOR, RA_VER_PATCH }; + *reinterpret_cast(buffer) = ra::version; bytes_out = sizeof(ra::version_t); } break; -- cgit v1.2.3 From 98de0eaac2f6d780da8ff4ad9533dad10be40204 Mon Sep 17 00:00:00 2001 From: a1xd <68629610+a1xd@users.noreply.github.com> Date: Thu, 1 Apr 2021 18:15:50 -0400 Subject: add flag to negate device match --- driver/driver.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'driver/driver.cpp') diff --git a/driver/driver.cpp b/driver/driver.cpp index fbc2d3d..ec340ea 100644 --- a/driver/driver.cpp +++ b/driver/driver.cpp @@ -55,8 +55,9 @@ Arguments: bool any = num_packets > 0; bool rel_move = !(InputDataStart->Flags & MOUSE_MOVE_ABSOLUTE); - bool dev_match = global.args.device_id[0] == 0 || - wcsncmp(devExt->dev_id, global.args.device_id, ra::MAX_DEV_ID_LEN) == 0; + bool dev_match = global.args.device_id[0] == 0 || + global.args.ignore == + bool(wcsncmp(devExt->dev_id, global.args.device_id, ra::MAX_DEV_ID_LEN)); if (any && rel_move && dev_match) { // if IO is backed up to the point where we get more than 1 packet here -- cgit v1.2.3 From e9866f27d78d9909fd4639cbd14a54b8ad5c2ec1 Mon Sep 17 00:00:00 2001 From: a1xd <68629610+a1xd@users.noreply.github.com> Date: Thu, 1 Apr 2021 18:33:00 -0400 Subject: driver - apply accel disregarding num packets add setting for max time threshold --- driver/driver.cpp | 32 +++++++++++++------------------- 1 file changed, 13 insertions(+), 19 deletions(-) (limited to 'driver/driver.cpp') diff --git a/driver/driver.cpp b/driver/driver.cpp index ec340ea..a7fe3b6 100644 --- a/driver/driver.cpp +++ b/driver/driver.cpp @@ -60,9 +60,18 @@ Arguments: bool(wcsncmp(devExt->dev_id, global.args.device_id, ra::MAX_DEV_ID_LEN)); if (any && rel_move && dev_match) { - // if IO is backed up to the point where we get more than 1 packet here - // then applying accel is pointless as we can't get an accurate timing - bool enable_accel = num_packets == 1; + milliseconds time; + + if (global.args.time_min == global.args.time_max) { + time = global.args.time_min; + } + else { + counter_t now = KeQueryPerformanceCounter(NULL).QuadPart; + counter_t ticks = now - devExt->counter; + devExt->counter = now; + milliseconds t = ticks * global.tick_interval / num_packets; + time = ra::clampsd(t, global.args.time_min, global.args.time_max); + } auto it = InputDataStart; do { @@ -72,22 +81,7 @@ Arguments: static_cast(it->LastY) }; - global.modifier.apply_rotation(input); - global.modifier.apply_angle_snap(input); - - if (enable_accel) { - auto time_supplier = [=] { - counter_t now = KeQueryPerformanceCounter(NULL).QuadPart; - counter_t ticks = now - devExt->counter; - devExt->counter = now; - milliseconds time = ticks * global.tick_interval; - return ra::clampsd(time, global.args.time_min, 100); - }; - - global.modifier.apply_acceleration(input, time_supplier); - } - - global.modifier.apply_sensitivity(input); + global.modifier.modify(input, time); double carried_result_x = input.x + devExt->carry.x; double carried_result_y = input.y + devExt->carry.y; -- cgit v1.2.3 From 2bfe95fb4e271d723a0aae537ec6c8732785442e Mon Sep 17 00:00:00 2001 From: a1xd <68629610+a1xd@users.noreply.github.com> Date: Thu, 1 Apr 2021 21:35:13 -0400 Subject: check for safe mode before hooking into dev stack can't stop the driver from being loaded in safe mode, an early return here is the best we can do --- driver/driver.cpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'driver/driver.cpp') diff --git a/driver/driver.cpp b/driver/driver.cpp index a7fe3b6..0c272e1 100644 --- a/driver/driver.cpp +++ b/driver/driver.cpp @@ -20,6 +20,8 @@ struct { ra::mouse_modifier modifier; } global = {}; +extern "C" PULONG InitSafeBootMode; + VOID RawaccelCallback( IN PDEVICE_OBJECT DeviceObject, @@ -433,13 +435,17 @@ Return Value: NTSTATUS status; WDFDEVICE hDevice; WDF_IO_QUEUE_CONFIG ioQueueConfig; - + UNREFERENCED_PARAMETER(Driver); PAGED_CODE(); DebugPrint(("Enter FilterEvtDeviceAdd \n")); + if (*InitSafeBootMode > 0) { + return STATUS_SUCCESS; + } + // // Tell the framework that you are filter driver. Framework // takes care of inherting all the device flags & characterstics -- cgit v1.2.3 From d8140fb31ba622f48756986d4d66db6b6ab8b511 Mon Sep 17 00:00:00 2001 From: a1xd <68629610+a1xd@users.noreply.github.com> Date: Thu, 1 Apr 2021 23:28:41 -0400 Subject: use callbacks for applying accel --- driver/driver.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'driver/driver.cpp') diff --git a/driver/driver.cpp b/driver/driver.cpp index 0c272e1..fa2415d 100644 --- a/driver/driver.cpp +++ b/driver/driver.cpp @@ -16,7 +16,8 @@ using milliseconds = double; struct { ra::settings args; - milliseconds tick_interval = 0; // set in DriverEntry + milliseconds tick_interval; + vec2 invokers; ra::mouse_modifier modifier; } global = {}; @@ -83,7 +84,7 @@ Arguments: static_cast(it->LastY) }; - global.modifier.modify(input, time); + global.modifier.modify(input, global.invokers, time); double carried_result_x = input.x + devExt->carry.x; double carried_result_y = input.y + devExt->carry.y; @@ -193,6 +194,7 @@ Return Value: ra::io_t& input = *reinterpret_cast(buffer); global.args = input.args; + global.invokers = ra::invokers(input.args); global.modifier = input.mod; } break; -- cgit v1.2.3 From c55d1bfd01147fa014ac07d4b03ef3cad8427ae6 Mon Sep 17 00:00:00 2001 From: a1xd <68629610+a1xd@users.noreply.github.com> Date: Thu, 8 Apr 2021 02:30:01 -0400 Subject: optimize a bit/refactor modify --- driver/driver.cpp | 33 +++++++++++++-------------------- 1 file changed, 13 insertions(+), 20 deletions(-) (limited to 'driver/driver.cpp') diff --git a/driver/driver.cpp b/driver/driver.cpp index fa2415d..feace77 100644 --- a/driver/driver.cpp +++ b/driver/driver.cpp @@ -23,6 +23,7 @@ struct { extern "C" PULONG InitSafeBootMode; +__declspec(guard(ignore)) VOID RawaccelCallback( IN PDEVICE_OBJECT DeviceObject, @@ -56,26 +57,18 @@ Arguments: auto num_packets = InputDataEnd - InputDataStart; - bool any = num_packets > 0; - bool rel_move = !(InputDataStart->Flags & MOUSE_MOVE_ABSOLUTE); - bool dev_match = global.args.device_id[0] == 0 || - global.args.ignore == - bool(wcsncmp(devExt->dev_id, global.args.device_id, ra::MAX_DEV_ID_LEN)); - - if (any && rel_move && dev_match) { - milliseconds time; - - if (global.args.time_min == global.args.time_max) { - time = global.args.time_min; - } - else { - counter_t now = KeQueryPerformanceCounter(NULL).QuadPart; - counter_t ticks = now - devExt->counter; - devExt->counter = now; - milliseconds t = ticks * global.tick_interval / num_packets; - time = ra::clampsd(t, global.args.time_min, global.args.time_max); - } - + if (num_packets > 0 && + !(InputDataStart->Flags & MOUSE_MOVE_ABSOLUTE) && + (global.args.device_id[0] == 0 || + bool(wcsncmp(devExt->dev_id, global.args.device_id, ra::MAX_DEV_ID_LEN)) == + global.args.ignore)) { + counter_t now = KeQueryPerformanceCounter(NULL).QuadPart; + counter_t ticks = now - devExt->counter; + devExt->counter = now; + milliseconds raw_elapsed = ticks * global.tick_interval; + milliseconds time = ra::clampsd(raw_elapsed / num_packets, + global.args.time_min, + global.args.time_max); auto it = InputDataStart; do { if (it->LastX || it->LastY) { -- cgit v1.2.3