From 6d9f657e95147024f2eee57b0b076f77c98d9c3a Mon Sep 17 00:00:00 2001 From: hap Date: Tue, 22 Apr 2025 02:56:58 +0200 Subject: [PATCH] natkeyboard: fix issue with dynamic buffer resizing --- src/emu/natkeyboard.cpp | 24 +++++++++++------------- src/emu/natkeyboard.h | 1 - src/frontend/mame/luaengine_input.cpp | 1 - src/mame/msx/msx_keyboard.cpp | 4 ++-- 4 files changed, 13 insertions(+), 17 deletions(-) diff --git a/src/emu/natkeyboard.cpp b/src/emu/natkeyboard.cpp index d250f4c3b82..4933de13b38 100644 --- a/src/emu/natkeyboard.cpp +++ b/src/emu/natkeyboard.cpp @@ -34,7 +34,7 @@ // CONSTANTS //************************************************************************** -const int KEY_BUFFER_SIZE = 4096; +const u32 KEY_BUFFER_CHUNK_SIZE = 0x1000; const char32_t INVALID_CHAR = '?'; @@ -47,7 +47,7 @@ const char32_t INVALID_CHAR = '?'; struct char_info { char32_t ch; - const char *alternate; // alternative string, in UTF-8 + const char *alternate; // alternative string, in UTF-8 static const char_info *find(char32_t target); }; @@ -344,7 +344,7 @@ natural_keyboard::natural_keyboard(running_machine &machine) build_codes(); if (!m_keyboards.empty()) { - m_buffer.resize(KEY_BUFFER_SIZE); + m_buffer.resize(KEY_BUFFER_CHUNK_SIZE); m_timer = machine.scheduler().timer_alloc(timer_expired_delegate(FUNC(natural_keyboard::timer), this)); } @@ -460,15 +460,9 @@ void natural_keyboard::post(std::u32string_view text, const attotime &rate) // set the fixed rate m_current_rate = rate; - // iterate over characters or until the buffer is full up + // iterate over characters for (char32_t ch : text) - { - if (full()) - break; - - // fetch next character post_char(ch, true); - } } @@ -832,10 +826,14 @@ void natural_keyboard::internal_post(char32_t ch) } // add to the buffer, resizing if necessary - m_buffer[m_bufend++] = ch; + m_buffer[m_bufend] = ch; if ((m_bufend + 1) % m_buffer.size() == m_bufbegin) - m_buffer.resize(m_buffer.size() + KEY_BUFFER_SIZE); - m_bufend %= m_buffer.size(); + { + m_buffer.insert(m_buffer.begin() + m_bufbegin, KEY_BUFFER_CHUNK_SIZE, INVALID_CHAR); + m_bufbegin += KEY_BUFFER_CHUNK_SIZE; + } + + m_bufend = (m_bufend + 1) % m_buffer.size(); } diff --git a/src/emu/natkeyboard.h b/src/emu/natkeyboard.h index 6903f662e47..57b58ffd173 100644 --- a/src/emu/natkeyboard.h +++ b/src/emu/natkeyboard.h @@ -45,7 +45,6 @@ public: // getters and queries running_machine &machine() const { return m_machine; } bool empty() const { return (m_bufbegin == m_bufend); } - bool full() const { return ((m_bufend + 1) % m_buffer.size()) == m_bufbegin; } bool can_post() const { return m_have_charkeys || !m_queue_chars.isnull(); } bool is_posting() const { return (!empty() || (!m_charqueue_empty.isnull() && !m_charqueue_empty())); } bool in_use() const { return m_in_use; } diff --git a/src/frontend/mame/luaengine_input.cpp b/src/frontend/mame/luaengine_input.cpp index 04db2a13e84..8ec97321cfb 100644 --- a/src/frontend/mame/luaengine_input.cpp +++ b/src/frontend/mame/luaengine_input.cpp @@ -208,7 +208,6 @@ void lua_engine::initialize_input(sol::table &emu) natkeyboard_type["paste"] = &natural_keyboard::paste; natkeyboard_type["dump"] = static_cast(&natural_keyboard::dump); natkeyboard_type["empty"] = sol::property(&natural_keyboard::empty); - natkeyboard_type["full"] = sol::property(&natural_keyboard::full); natkeyboard_type["can_post"] = sol::property(&natural_keyboard::can_post); natkeyboard_type["is_posting"] = sol::property(&natural_keyboard::is_posting); natkeyboard_type["in_use"] = sol::property(&natural_keyboard::in_use, &natural_keyboard::set_in_use); diff --git a/src/mame/msx/msx_keyboard.cpp b/src/mame/msx/msx_keyboard.cpp index 3c8ee0362da..804840e86d6 100644 --- a/src/mame/msx/msx_keyboard.cpp +++ b/src/mame/msx/msx_keyboard.cpp @@ -232,10 +232,10 @@ INPUT_PORTS_START(msxjp) PORT_MODIFY("KEY2") PORT_BIT(0x01, IP_ACTIVE_LOW, IPT_KEYBOARD) PORT_CODE(KEYCODE_QUOTE) PORT_CHAR(':') PORT_CHAR('*') PORT_BIT(0x02, IP_ACTIVE_LOW, IPT_KEYBOARD) PORT_CODE(KEYCODE_BACKSLASH) PORT_CHAR(']') PORT_CHAR('}') - PORT_BIT(0x20, IP_ACTIVE_LOW, IPT_KEYBOARD) PORT_NAME("_") PORT_CODE(KEYCODE_TILDE) PORT_CHAR('_') + PORT_BIT(0x20, IP_ACTIVE_LOW, IPT_KEYBOARD) PORT_NAME("_") PORT_CODE(KEYCODE_TILDE) PORT_CHAR() PORT_CHAR('_') PORT_MODIFY("KEY6") - PORT_BIT(0x10, IP_ACTIVE_LOW, IPT_KEYBOARD) PORT_NAME("KANA") PORT_CODE(KEYCODE_PGDN) PORT_CHAR(UCHAR_MAMEKEY(F7)) + PORT_BIT(0x10, IP_ACTIVE_LOW, IPT_KEYBOARD) PORT_NAME("KANA") PORT_CODE(KEYCODE_PGDN) PORT_CHAR(UCHAR_MAMEKEY(F7)) INPUT_PORTS_END