From 213ca0b86bf9a811b8d194f0defb962abc82c458 Mon Sep 17 00:00:00 2001 From: pit-ray Date: Fri, 3 Dec 2021 16:54:10 +0900 Subject: [PATCH] Added a global LogPooler to allow pooling of Logs across modes. --- src/bind/easyclick/input_hinter.cpp | 4 +- src/bind/emu/edi_motion.cpp | 7 ++- src/bind/mode/command_mode.cpp | 4 +- src/bind/mode/instant_mode.cpp | 4 +- src/bind/window/switch_window.cpp | 4 +- src/bind/window/window_resizer.cpp | 4 +- src/core/logpooler.cpp | 77 +++++++++++++++++++++++++++++ src/core/logpooler.hpp | 38 ++++++++++++++ src/core/main_loop.cpp | 27 ++-------- src/core/mapgate.cpp | 10 ++-- src/core/mapgate.hpp | 5 +- 11 files changed, 145 insertions(+), 39 deletions(-) create mode 100644 src/core/logpooler.cpp create mode 100644 src/core/logpooler.hpp diff --git a/src/bind/easyclick/input_hinter.cpp b/src/bind/easyclick/input_hinter.cpp index ab742017..57ae66ff 100644 --- a/src/bind/easyclick/input_hinter.cpp +++ b/src/bind/easyclick/input_hinter.cpp @@ -7,6 +7,7 @@ #include "core/char_logger.hpp" #include "core/entry.hpp" #include "core/key_absorber.hpp" +#include "core/logpooler.hpp" #include "util/winwrap.hpp" namespace vind @@ -91,7 +92,8 @@ namespace vind }; while(core::update_background() && !(pimpl->cancel_running_)) { - if(!CHAR_LOGGED(lgr.logging_state())) { + auto log = core::LogPooler::get_instance().pop_log() ; + if(!CHAR_LOGGED(lgr.logging_state(log))) { continue ; } diff --git a/src/bind/emu/edi_motion.cpp b/src/bind/emu/edi_motion.cpp index ee87ca30..a88ec831 100644 --- a/src/bind/emu/edi_motion.cpp +++ b/src/bind/emu/edi_motion.cpp @@ -5,6 +5,7 @@ #include "core/entry.hpp" #include "core/func_finder.hpp" #include "core/key_absorber.hpp" +#include "core/logpooler.hpp" #include "core/mode.hpp" #include "core/ntype_logger.hpp" #include "edi_delete.hpp" @@ -25,7 +26,8 @@ namespace core::InstantKeyAbsorber ika ; while(core::update_background()) { - auto result = lgr.logging_state() ; + auto log = core::LogPooler::get_instance().pop_log() ; + auto result = lgr.logging_state(log) ; if(NTYPE_EMPTY(result)) { continue ; } @@ -85,7 +87,8 @@ namespace core::InstantKeyAbsorber ika ; while(core::update_background()) { - auto result = lgr.logging_state() ; + auto log = core::LogPooler::get_instance().pop_log() ; + auto result = lgr.logging_state(log) ; auto parent_result = parent_lgr.logging_state() ; if(NTYPE_EMPTY(result) && NTYPE_EMPTY(parent_result)) { continue ; diff --git a/src/bind/mode/command_mode.cpp b/src/bind/mode/command_mode.cpp index bb6cd321..5a1bd45c 100644 --- a/src/bind/mode/command_mode.cpp +++ b/src/bind/mode/command_mode.cpp @@ -15,6 +15,7 @@ #include "core/key_logger_base.hpp" #include "core/keycode_def.hpp" #include "core/keycodecvt.hpp" +#include "core/logpooler.hpp" #include "core/mode.hpp" #include "core/ntype_logger.hpp" #include "opt/vcmdline.hpp" @@ -178,7 +179,8 @@ namespace vind auto& p_cmdp = pimpl->ch_.get_hist_point() ; auto& lgr = p_cmdp->logger ; - if(CHAR_EMPTY(lgr.logging_state())) { + auto log = core::LogPooler::get_instance().pop_log() ; + if(CHAR_EMPTY(lgr.logging_state(log))) { continue ; } diff --git a/src/bind/mode/instant_mode.cpp b/src/bind/mode/instant_mode.cpp index e11a3a6a..a4d24f2b 100644 --- a/src/bind/mode/instant_mode.cpp +++ b/src/bind/mode/instant_mode.cpp @@ -5,6 +5,7 @@ #include "core/func_finder.hpp" #include "core/key_absorber.hpp" #include "core/key_logger_base.hpp" +#include "core/logpooler.hpp" #include "core/mode.hpp" #include "core/ntype_logger.hpp" #include "opt/vcmdline.hpp" @@ -43,7 +44,8 @@ namespace vind pimpl->finder_.reset_parser_states(lcx_vmode) ; core::NTypeLogger lgr ; while(core::update_background()) { - auto result = lgr.logging_state() ; + auto log = core::LogPooler::get_instance().pop_log() ; + auto result = lgr.logging_state(log) ; if(NTYPE_EMPTY(result)) { continue ; } diff --git a/src/bind/window/switch_window.cpp b/src/bind/window/switch_window.cpp index 82579a7e..01c5f12d 100644 --- a/src/bind/window/switch_window.cpp +++ b/src/bind/window/switch_window.cpp @@ -9,6 +9,7 @@ #include "core/func_finder.hpp" #include "core/key_absorber.hpp" #include "core/key_logger_base.hpp" +#include "core/logpooler.hpp" #include "core/mode.hpp" #include "core/ntype_logger.hpp" #include "util/def.hpp" @@ -75,7 +76,8 @@ namespace vind core::NTypeLogger lgr ; std::size_t actid = 0 ; while(core::update_background()) { - if(!NTYPE_LOGGED(lgr.logging_state())) { + auto log = core::LogPooler::get_instance().pop_log() ; + if(!NTYPE_LOGGED(lgr.logging_state(log))) { continue ; } diff --git a/src/bind/window/window_resizer.cpp b/src/bind/window/window_resizer.cpp index d173d869..8de0e05b 100644 --- a/src/bind/window/window_resizer.cpp +++ b/src/bind/window/window_resizer.cpp @@ -13,6 +13,7 @@ #include "core/func_finder.hpp" #include "core/g_params.hpp" #include "core/key_absorber.hpp" +#include "core/logpooler.hpp" #include "core/ntype_logger.hpp" #include "opt/vcmdline.hpp" #include "util/constant_accelerator.hpp" @@ -220,7 +221,8 @@ namespace vind std::size_t actid = 0 ; while(core::update_background()) { - if(!NTYPE_LOGGED(lgr.logging_state())) { + auto log = core::LogPooler::get_instance().pop_log() ; + if(!NTYPE_LOGGED(lgr.logging_state(log))) { continue ; } diff --git a/src/core/logpooler.cpp b/src/core/logpooler.cpp new file mode 100644 index 00000000..6ebd081c --- /dev/null +++ b/src/core/logpooler.cpp @@ -0,0 +1,77 @@ +#include "logpooler.hpp" + +#include "key_absorber.hpp" +#include "key_log.hpp" +#include "mapgate.hpp" + +#include + + +#if defined(DEBUG) + +#include "key_logger_base.hpp" + +#include + +#endif + + +namespace vind +{ + namespace core + { + struct LogPooler::Impl { + std::queue pool_{} ; + } ; + + LogPooler::LogPooler() + : pimpl(std::make_unique()) + {} + + LogPooler::~LogPooler() noexcept = default ; + + LogPooler& LogPooler::get_instance() { + static LogPooler instance ; + return instance ; + } + + void LogPooler::push_log(const KeyLog& log) { + pimpl->pool_.push(log) ; + } + + void LogPooler::push_log(KeyLog&& log) { + pimpl->pool_.push(std::move(log)) ; + } + + KeyLog LogPooler::pop_log() { + KeyLog log ; + if(pimpl->pool_.empty()) { + log = get_pressed_list() ; + + auto logs = MapGate::get_instance().map_logger(log) ; + if(!logs.empty()) { + auto itr = std::make_move_iterator(logs.begin()) ; + log = std::move(*itr) ; + itr ++ ; + + // + // To simulate the input, make a state transition with an empty log. + // This is to make the logger recognize that it is a key release, + // not a long pressing of the key. + // + while(itr != std::make_move_iterator(logs.end())) { + pimpl->pool_.emplace() ; + pimpl->pool_.push(std::move(*itr)) ; + itr ++ ; + } + } + } + else { + log = std::move(pimpl->pool_.front()) ; + pimpl->pool_.pop() ; + } + + return log ; + } + } +} diff --git a/src/core/logpooler.hpp b/src/core/logpooler.hpp new file mode 100644 index 00000000..ea528ee8 --- /dev/null +++ b/src/core/logpooler.hpp @@ -0,0 +1,38 @@ +#ifndef _LOGPOOLER_HPP +#define _LOGPOOLER_HPP + +#include + + +namespace vind +{ + namespace core + { + class KeyLog ; + + class LogPooler { + private: + struct Impl ; + std::unique_ptr pimpl ; + + explicit LogPooler() ; + virtual ~LogPooler() noexcept ; + + public: + static LogPooler& get_instance() ; + + void push_log(const KeyLog& log) ; + void push_log(KeyLog&& log) ; + + KeyLog pop_log() ; + + LogPooler(LogPooler&&) = delete ; + LogPooler& operator=(LogPooler&&) = delete ; + LogPooler(const LogPooler&) = delete ; + LogPooler& operator=(const LogPooler&) = delete ; + } ; + + } +} + +#endif diff --git a/src/core/main_loop.cpp b/src/core/main_loop.cpp index bb2dd523..5f733e62 100644 --- a/src/core/main_loop.cpp +++ b/src/core/main_loop.cpp @@ -8,6 +8,7 @@ #include "key_logger_base.hpp" #include "keycode_def.hpp" #include "logger_parser.hpp" +#include "logpooler.hpp" #include "mapgate.hpp" #include "ntype_logger.hpp" @@ -24,8 +25,6 @@ namespace core::NTypeLogger g_ntlgr{} ; core::FuncFinder g_funcfinder{} ; bind::BindedFunc::SPtr g_active_func = nullptr ; - - core::MapGate::KeyLogPool g_logpool{} ; } namespace vind @@ -33,7 +32,6 @@ namespace vind namespace core { void initialize_mainloop() { g_ntlgr.clear() ; - core::MapGate::KeyLogPool().swap(g_logpool) ; g_active_func = nullptr ; } @@ -47,26 +45,7 @@ namespace vind } void update_mainloop() { - auto has_pool = !g_logpool.empty() ; - - KeyLog log{} ; - if(!has_pool) { - log = get_pressed_list() ; - - auto logpool = MapGate::get_instance().map_logger(log) ; - if(!logpool.empty()) { - log = std::move(logpool.front()) ; - logpool.pop() ; - - if(!logpool.empty()) { - g_logpool.swap(logpool) ; - } - } - } - else { - log = std::move(g_logpool.front()) ; - g_logpool.pop() ; - } + auto log = LogPooler::get_instance().pop_log() ; auto result = g_ntlgr.logging_state(log) ; @@ -79,7 +58,7 @@ namespace vind return ; } - std::cout << print(g_ntlgr) << std::endl ; + // std::cout << print(g_ntlgr) << std::endl ; if(g_ntlgr.is_long_pressing()) { if(g_active_func) { diff --git a/src/core/mapgate.cpp b/src/core/mapgate.cpp index 8ba1b0fa..45e7685e 100644 --- a/src/core/mapgate.cpp +++ b/src/core/mapgate.cpp @@ -306,7 +306,7 @@ namespace vind namespace core { struct MapGate::Impl { - ModeArray> logpool_table_{} ; + ModeArray>> logpool_table_{} ; ModeArray mgr_table_{} ; ModeArray syncmap_table_{} ; @@ -412,7 +412,7 @@ namespace vind parsers.push_back(std::move(parser)) ; for(const auto& keyset : solved_target_cmds[mapid]) { - logpool[func->id()].emplace(keyset.begin(), keyset.end()) ; + logpool[func->id()].emplace_back(keyset.begin(), keyset.end()) ; } } @@ -424,7 +424,7 @@ namespace vind parsers.push_back(std::move(parser)) ; for(const auto& keyset : map.target_command()) { - logpool[func->id()].emplace(keyset.begin(), keyset.end()) ; + logpool[func->id()].emplace_back(keyset.begin(), keyset.end()) ; } } @@ -439,7 +439,7 @@ namespace vind } } - MapGate::KeyLogPool MapGate::map_logger( + std::vector MapGate::map_logger( const KeyLog& log, Mode mode) { auto midx = static_cast(mode) ; @@ -463,7 +463,7 @@ namespace vind mgr.reset_parser_states() ; } - return KeyLogPool{} ; + return std::vector{} ; } bool MapGate::map_syncstate( diff --git a/src/core/mapgate.hpp b/src/core/mapgate.hpp index d9cfdac7..6dd5dc26 100644 --- a/src/core/mapgate.hpp +++ b/src/core/mapgate.hpp @@ -5,7 +5,7 @@ #include "mode.hpp" #include -#include +#include namespace vind @@ -38,8 +38,7 @@ namespace vind // but in the case of map, the key message is generated in a state that is passed to Windows. // // - using KeyLogPool = std::queue ; - KeyLogPool map_logger( + std::vector map_logger( const KeyLog& log, Mode mode=get_global_mode()) ;