diff options
Diffstat (limited to 'src/emu/natkeyboard.cpp')
-rw-r--r-- | src/emu/natkeyboard.cpp | 155 |
1 files changed, 68 insertions, 87 deletions
diff --git a/src/emu/natkeyboard.cpp b/src/emu/natkeyboard.cpp index 681f64cd263..1abe9a140ff 100644 --- a/src/emu/natkeyboard.cpp +++ b/src/emu/natkeyboard.cpp @@ -34,7 +34,8 @@ // CONSTANTS //************************************************************************** -const int KEY_BUFFER_SIZE = 4096; +const u32 KEY_BUFFER_CHUNK_SIZE = 0x1000; +const u32 KEY_BUFFER_MAX_SIZE = 0x800000; const char32_t INVALID_CHAR = '?'; @@ -47,7 +48,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 +345,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)); } @@ -455,23 +456,14 @@ void natural_keyboard::post_char(char32_t ch, bool normalize_crlf) // post - post a unicode encoded string //------------------------------------------------- -void natural_keyboard::post(const char32_t *text, size_t length, const attotime &rate) +void natural_keyboard::post(std::u32string_view text, const attotime &rate) { // set the fixed rate m_current_rate = rate; - // 0 length means strlen - if (length == 0) - for (const char32_t *scan = text; *scan != 0; scan++) - length++; - - // iterate over characters or until the buffer is full up - while (length > 0 && !full()) - { - // fetch next character - post_char(*text++, true); - length--; - } + // iterate over characters + for (char32_t ch : text) + post_char(ch, true); } @@ -479,21 +471,17 @@ void natural_keyboard::post(const char32_t *text, size_t length, const attotime // post_utf8 - post a UTF-8 encoded string //------------------------------------------------- -void natural_keyboard::post_utf8(const char *text, size_t length, const attotime &rate) +void natural_keyboard::post_utf8(std::string_view text, const attotime &rate) { // set the fixed rate m_current_rate = rate; - // 0-length means strlen - if (length == 0) - length = strlen(text); - // iterate until out of characters - while (length > 0) + while (!text.empty()) { // decode the next character char32_t uc; - int count = uchar_from_utf8(&uc, text, length); + int count = uchar_from_utf8(&uc, text); if (count < 0) { count = 1; @@ -502,83 +490,74 @@ void natural_keyboard::post_utf8(const char *text, size_t length, const attotime // append to the buffer post_char(uc, true); - text += count; - length -= count; + text.remove_prefix(count); } } -void natural_keyboard::post_utf8(std::string_view text, const attotime &rate) -{ - if (!text.empty()) - post_utf8(text.data(), text.size(), rate); -} - - //------------------------------------------------- // post_coded - post a coded string //------------------------------------------------- -void natural_keyboard::post_coded(const char *text, size_t length, const attotime &rate) +void natural_keyboard::post_coded(std::string_view text, const attotime &rate) { + using namespace std::literals; static const struct { - const char *key; + std::string_view key; char32_t code; } codes[] = { - { "BACKSPACE", 8 }, - { "BS", 8 }, - { "BKSP", 8 }, - { "DEL", UCHAR_MAMEKEY(DEL) }, - { "DELETE", UCHAR_MAMEKEY(DEL) }, - { "END", UCHAR_MAMEKEY(END) }, - { "ENTER", 13 }, - { "ESC", '\033' }, - { "HOME", UCHAR_MAMEKEY(HOME) }, - { "INS", UCHAR_MAMEKEY(INSERT) }, - { "INSERT", UCHAR_MAMEKEY(INSERT) }, - { "PGDN", UCHAR_MAMEKEY(PGDN) }, - { "PGUP", UCHAR_MAMEKEY(PGUP) }, - { "SPACE", 32 }, - { "TAB", 9 }, - { "F1", UCHAR_MAMEKEY(F1) }, - { "F2", UCHAR_MAMEKEY(F2) }, - { "F3", UCHAR_MAMEKEY(F3) }, - { "F4", UCHAR_MAMEKEY(F4) }, - { "F5", UCHAR_MAMEKEY(F5) }, - { "F6", UCHAR_MAMEKEY(F6) }, - { "F7", UCHAR_MAMEKEY(F7) }, - { "F8", UCHAR_MAMEKEY(F8) }, - { "F9", UCHAR_MAMEKEY(F9) }, - { "F10", UCHAR_MAMEKEY(F10) }, - { "F11", UCHAR_MAMEKEY(F11) }, - { "F12", UCHAR_MAMEKEY(F12) }, - { "QUOTE", '\"' } + { "BACKSPACE"sv, 8 }, + { "BS"sv, 8 }, + { "BKSP"sv, 8 }, + { "CAPSLOCK"sv, UCHAR_MAMEKEY(CAPSLOCK) }, + { "CR"sv, 13 }, + { "DEL"sv, UCHAR_MAMEKEY(DEL) }, + { "DELETE"sv, UCHAR_MAMEKEY(DEL) }, + { "END"sv, UCHAR_MAMEKEY(END) }, + { "ENTER"sv, 13 }, + { "ESC"sv, '\033' }, + { "HOME"sv, UCHAR_MAMEKEY(HOME) }, + { "INS"sv, UCHAR_MAMEKEY(INSERT) }, + { "INSERT"sv, UCHAR_MAMEKEY(INSERT) }, + { "LF"sv, 10 }, + { "PGDN"sv, UCHAR_MAMEKEY(PGDN) }, + { "PGUP"sv, UCHAR_MAMEKEY(PGUP) }, + { "SPACE"sv, 32 }, + { "TAB"sv, 9 }, + { "F1"sv, UCHAR_MAMEKEY(F1) }, + { "F2"sv, UCHAR_MAMEKEY(F2) }, + { "F3"sv, UCHAR_MAMEKEY(F3) }, + { "F4"sv, UCHAR_MAMEKEY(F4) }, + { "F5"sv, UCHAR_MAMEKEY(F5) }, + { "F6"sv, UCHAR_MAMEKEY(F6) }, + { "F7"sv, UCHAR_MAMEKEY(F7) }, + { "F8"sv, UCHAR_MAMEKEY(F8) }, + { "F9"sv, UCHAR_MAMEKEY(F9) }, + { "F10"sv, UCHAR_MAMEKEY(F10) }, + { "F11"sv, UCHAR_MAMEKEY(F11) }, + { "F12"sv, UCHAR_MAMEKEY(F12) }, + { "QUOTE"sv, '\"' } }; // set the fixed rate m_current_rate = rate; - // 0-length means strlen - if (length == 0) - length = strlen(text); - // iterate through the source string - size_t curpos = 0; - while (curpos < length) + while (!text.empty()) { // extract next character - char32_t ch = text[curpos]; - size_t increment = 1; + char32_t ch = text.front(); + std::string_view::size_type increment = 1; // look for escape characters if (ch == '{') for (auto & code : codes) { - size_t keylen = strlen(code.key); - if (curpos + keylen + 2 <= length) - if (core_strnicmp(code.key, &text[curpos + 1], keylen) == 0 && text[curpos + keylen + 1] == '}') + std::string_view::size_type keylen = code.key.length(); + if (keylen + 2 <= text.length()) + if (util::strequpper(text.substr(1, keylen), code.key) && text[keylen + 1] == '}') { ch = code.code; increment = keylen + 2; @@ -588,18 +567,11 @@ void natural_keyboard::post_coded(const char *text, size_t length, const attotim // if we got a code, post it if (ch != 0) post_char(ch); - curpos += increment; + text.remove_prefix(increment); } } -void natural_keyboard::post_coded(std::string_view text, const attotime &rate) -{ - if (!text.empty()) - post_coded(text.data(), text.size(), rate); -} - - //------------------------------------------------- // paste - does a paste from the keyboard //------------------------------------------------- @@ -704,11 +676,11 @@ void natural_keyboard::build_codes() { if (!(curshift & ~mask)) { - // fetch the code, ignoring 0 and shifters + // fetch the code, ignoring shifters std::vector<char32_t> const codes = field.keyboard_codes(curshift); for (char32_t code : codes) { - if (((code < UCHAR_SHIFT_BEGIN) || (code > UCHAR_SHIFT_END)) && (code != 0)) + if ((code < UCHAR_SHIFT_BEGIN) || (code > UCHAR_SHIFT_END)) { m_have_charkeys = true; keycode_map::iterator const found(devinfo.codemap.find(code)); @@ -855,10 +827,19 @@ void natural_keyboard::internal_post(char32_t ch) } // add to the buffer, resizing if necessary - 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[m_bufend] = ch; + size_t size = m_buffer.size(); + + if ((m_bufend + 1) % size == m_bufbegin) + { + if (size >= KEY_BUFFER_MAX_SIZE) + return; + + 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) % size; } |