From c9351ab7768b5230440404fdeb4faf16f6f6985b Mon Sep 17 00:00:00 2001 From: FeralChild64 Date: Sat, 5 Nov 2022 20:40:35 +0100 Subject: Allow to cancel interactive mouse mapping with a keyboard --- src/hardware/mouse/mouse_manymouse.cpp | 24 +++++++++++++++++++++++- src/hardware/mouse/mouse_manymouse.h | 1 + 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/hardware/mouse/mouse_manymouse.cpp b/src/hardware/mouse/mouse_manymouse.cpp index 34b9aa472..46f9c03a2 100644 --- a/src/hardware/mouse/mouse_manymouse.cpp +++ b/src/hardware/mouse/mouse_manymouse.cpp @@ -22,6 +22,7 @@ #include "callback.h" #include "checks.h" +#include "dos_inc.h" #include "math_utils.h" #include "pic.h" #include "string_utils.h" @@ -254,6 +255,9 @@ bool ManyMouseGlue::ProbeForMapping(uint8_t &physical_device_idx) bool success = false; while (!shutdown_requested) { + if (IsCancelRequested()) + break; // user cancelled using a keyboard + // Poll mouse events, handle critical ones if (!ManyMouse_PollEvent(&event)) { CALLBACK_Idle(); @@ -272,7 +276,7 @@ bool ManyMouseGlue::ProbeForMapping(uint8_t &physical_device_idx) physical_device_idx = static_cast(event.device); if (event.item >= 1) - break; // user cancelled the interactive mouse mapping + break; // user cancelled using a mouse button // Do not accept already mapped devices bool already_mapped = false; @@ -293,6 +297,24 @@ bool ManyMouseGlue::ProbeForMapping(uint8_t &physical_device_idx) return success; } +bool ManyMouseGlue::IsCancelRequested() +{ + constexpr uint8_t code_ctrl_c = 0x03; + constexpr uint8_t code_esc = 0x1b; + + while (!(Files[STDIN]->GetInformation() & (1 << 6))) { + // A key is waiting, read it + uint16_t count = 1; + uint8_t code = 0; + DOS_ReadFile(STDIN, &code, &count); + // Check if requested to cancel + if (code == code_ctrl_c || code == code_esc || code == 'q' || code == 'Q') + return true; + } + + return false; +} + uint8_t ManyMouseGlue::GetIdx(const std::regex ®ex) { assert(max_mice < UINT8_MAX); diff --git a/src/hardware/mouse/mouse_manymouse.h b/src/hardware/mouse/mouse_manymouse.h index 69753e145..f9b0a0fc1 100644 --- a/src/hardware/mouse/mouse_manymouse.h +++ b/src/hardware/mouse/mouse_manymouse.h @@ -73,6 +73,7 @@ private: ManyMouseGlue(const ManyMouseGlue &) = delete; ManyMouseGlue &operator=(const ManyMouseGlue &) = delete; + bool IsCancelRequested(); void Tick(); friend void manymouse_tick(uint32_t); -- cgit v1.2.3