From edf9dfd257af8dc08bf1a1757308c752959741e8 Mon Sep 17 00:00:00 2001 From: casey Date: Fri, 8 Jul 2016 22:08:52 -0700 Subject: [PATCH] Added a simple key normalization layer in Text.cpp, used in Main.cpp in the main read loop. --- src/musikbox/Main.cpp | 23 ++++---- src/musikbox/app/layout/BrowseLayout.cpp | 2 +- src/musikbox/app/layout/IndexerLayout.cpp | 4 +- src/musikbox/app/layout/NowPlayingLayout.cpp | 2 +- src/musikbox/app/layout/SearchLayout.cpp | 2 +- src/musikbox/app/layout/TrackSearchLayout.cpp | 2 +- src/musikbox/app/util/GlobalHotkeys.cpp | 4 +- src/musikbox/cursespp/Checkbox.cpp | 2 +- src/musikbox/cursespp/Text.cpp | 21 +++++++ src/musikbox/cursespp/Text.h | 4 ++ src/musikbox/cursespp/TextInput.cpp | 56 ++++++++++--------- 11 files changed, 75 insertions(+), 47 deletions(-) diff --git a/src/musikbox/Main.cpp b/src/musikbox/Main.cpp index 0eb8d733e..40c8e5457 100644 --- a/src/musikbox/Main.cpp +++ b/src/musikbox/Main.cpp @@ -181,23 +181,25 @@ static inline std::string readKeyPress(int64 ch) { /* multi-byte UTF8 character */ else if (ch >= 194 && ch <= 223) { kn = ""; - kn += (char)ch; - kn += (char)getch(); + kn += (char) ch; + kn += (char) getch(); } else if (ch >= 224 && ch <= 239) { kn = ""; - kn += (char)ch; - kn += (char)getch(); - kn += (char)getch(); + kn += (char) ch; + kn += (char) getch(); + kn += (char) getch(); } else if (ch >= 240 && ch <= 244) { kn = ""; - kn += (char)ch; - kn += (char)getch(); - kn += (char)getch(); - kn += (char)getch(); + kn += (char) ch; + kn += (char) getch(); + kn += (char) getch(); + kn += (char) getch(); } + kn = key::Normalize(kn); + // std::cerr << "keyname: " << kn << std::endl; // std::cerr << "ch: " << ch << std::endl; @@ -244,7 +246,6 @@ int main(int argc, char* argv[]) #ifdef __PDCURSES__ PDC_set_resize_limits(12, 60, 60, 250); - PDC_set_title("musikbox ♫"); PDC_set_function_key(FUNCTION_KEY_SHUT_DOWN, 4); #endif @@ -324,7 +325,7 @@ int main(int argc, char* argv[]) else if (kn == "KEY_RESIZE") { resizeAt = now() + REDRAW_DEBOUNCE_MS; } - else if (kn == "M-`" || kn == "M-~" || kn == "M-bquote" || kn == "^@") { + else if (kn == "M-`" || kn == "M-~") { changeLayout(state, consoleLayout); } else if (kn == "M-a") { diff --git a/src/musikbox/app/layout/BrowseLayout.cpp b/src/musikbox/app/layout/BrowseLayout.cpp index 835fb02c2..ae0559a41 100755 --- a/src/musikbox/app/layout/BrowseLayout.cpp +++ b/src/musikbox/app/layout/BrowseLayout.cpp @@ -153,7 +153,7 @@ void BrowseLayout::OnCategoryViewInvalidated( } bool BrowseLayout::KeyPress(const std::string& key) { - if (key == "^M") { /* enter. play the selection */ + if (key == "KEY_ENTER") { playback::Play(this->trackList, this->playback, this->GetFocus()); return true; } diff --git a/src/musikbox/app/layout/IndexerLayout.cpp b/src/musikbox/app/layout/IndexerLayout.cpp index 55cad5d07..7e1014ee2 100755 --- a/src/musikbox/app/layout/IndexerLayout.cpp +++ b/src/musikbox/app/layout/IndexerLayout.cpp @@ -230,7 +230,7 @@ void IndexerLayout::DrillIntoSelectedDirectory() { } bool IndexerLayout::KeyPress(const std::string& key) { - if (key == "^M") { + if (key == "KEY_ENTER") { if (this->GetFocus() == this->browseList) { this->DrillIntoSelectedDirectory(); return true; @@ -242,7 +242,7 @@ bool IndexerLayout::KeyPress(const std::string& key) { return true; } } - else if (key == "^H" || key == "^?" || key == "KEY_BACKSPACE") { /* backspace */ + else if (key == "KEY_BACKSPACE") { /* backspace */ if (this->GetFocus() == this->addedPathsList) { this->RemoveSelectedDirectory(); return true; diff --git a/src/musikbox/app/layout/NowPlayingLayout.cpp b/src/musikbox/app/layout/NowPlayingLayout.cpp index dd7f4bfee..416db737e 100755 --- a/src/musikbox/app/layout/NowPlayingLayout.cpp +++ b/src/musikbox/app/layout/NowPlayingLayout.cpp @@ -126,7 +126,7 @@ void NowPlayingLayout::RequeryTrackList() { } bool NowPlayingLayout::KeyPress(const std::string& key) { - if (key == "^M") { /* enter. play the selection */ + if (key == "KEY_ENTER") { this->playback.Play(this->trackList->GetSelectedIndex()); return true; } diff --git a/src/musikbox/app/layout/SearchLayout.cpp b/src/musikbox/app/layout/SearchLayout.cpp index fdbf886f6..85a5da1b7 100755 --- a/src/musikbox/app/layout/SearchLayout.cpp +++ b/src/musikbox/app/layout/SearchLayout.cpp @@ -162,7 +162,7 @@ bool SearchLayout::KeyPress(const std::string& key) { CategoryListView* category = dynamic_cast(focus.get()); if (category) { - if (key == "^M") { + if (key == "KEY_ENTER") { int index = (int) category->GetSelectedIndex(); if (index >= 0) { this->SearchResultSelected( diff --git a/src/musikbox/app/layout/TrackSearchLayout.cpp b/src/musikbox/app/layout/TrackSearchLayout.cpp index a3527811f..8121d14fa 100755 --- a/src/musikbox/app/layout/TrackSearchLayout.cpp +++ b/src/musikbox/app/layout/TrackSearchLayout.cpp @@ -147,7 +147,7 @@ void TrackSearchLayout::OnEnterPressed(cursespp::TextInput* sender) { } bool TrackSearchLayout::KeyPress(const std::string& key) { - if (key == "^M") { /* enter. play the selection */ + if (key == "KEY_ENTER") { playback::Play(this->trackList, this->playback, this->GetFocus()); return true; } diff --git a/src/musikbox/app/util/GlobalHotkeys.cpp b/src/musikbox/app/util/GlobalHotkeys.cpp index 28798649f..33927a5c4 100755 --- a/src/musikbox/app/util/GlobalHotkeys.cpp +++ b/src/musikbox/app/util/GlobalHotkeys.cpp @@ -82,11 +82,11 @@ bool GlobalHotkeys::Handle(const std::string& kn) { this->transport.SetPosition(time + 10.0f); return true; } - else if (kn == "M-," || kn == "M-comma") { + else if (kn == "M-,") { playback::ToggleRepeatMode(this->playback); return true; } - else if (kn == "M-." || kn == "M-stop") { + else if (kn == "M-.") { this->playback.ToggleShuffle(); return true; } diff --git a/src/musikbox/cursespp/Checkbox.cpp b/src/musikbox/cursespp/Checkbox.cpp index c9934672d..5e4c8e512 100755 --- a/src/musikbox/cursespp/Checkbox.cpp +++ b/src/musikbox/cursespp/Checkbox.cpp @@ -113,7 +113,7 @@ void Checkbox::Redraw() { } bool Checkbox::KeyPress(const std::string& key) { - if (key == " " || key == "^M") { + if (key == " " || key == "KEY_ENTER") { this->SetChecked(!this->checked); return true; } diff --git a/src/musikbox/cursespp/Text.cpp b/src/musikbox/cursespp/Text.cpp index c9d107b7c..a8c1ba626 100755 --- a/src/musikbox/cursespp/Text.cpp +++ b/src/musikbox/cursespp/Text.cpp @@ -35,6 +35,7 @@ #include "stdafx.h" #include "Text.h" +#include #include #define PAD(str, count) for (size_t i = 0; i < count; i++) { str += " "; } @@ -102,4 +103,24 @@ namespace cursespp { } } } + + namespace key { + static std::unordered_map KEY_MAPPING = { + { "M-bquote", "M-`" }, + { "^@", "M-`" }, + { "M-comma", "M-," }, + { "M-stop", "M-." }, + { "^H", "KEY_BACKSPACE" }, + { "^?", "KEY_BACKSPACE" }, + { "M-^H", "M-KEY_BACKSPACE" }, + { "M-^?", "M-KEY_BACKSPACE" }, + { "M-bksp", "M-KEY_BACKSPACE" }, + { "^M", "KEY_ENTER" } + }; + + std::string Normalize(const std::string& kn) { + auto it = KEY_MAPPING.find(kn); + return (it != KEY_MAPPING.end()) ? it->second : kn; + } + } } diff --git a/src/musikbox/cursespp/Text.h b/src/musikbox/cursespp/Text.h index 5223c47f0..0e8edbc76 100755 --- a/src/musikbox/cursespp/Text.h +++ b/src/musikbox/cursespp/Text.h @@ -47,4 +47,8 @@ namespace cursespp { std::string Ellipsize(const std::string& str, size_t len); std::string Align(const std::string& str, TextAlign align, size_t len); } + + namespace key { + std::string Normalize(const std::string& keyname); + } } diff --git a/src/musikbox/cursespp/TextInput.cpp b/src/musikbox/cursespp/TextInput.cpp index fbbbf0ba1..367b86667 100755 --- a/src/musikbox/cursespp/TextInput.cpp +++ b/src/musikbox/cursespp/TextInput.cpp @@ -100,33 +100,17 @@ size_t TextInput::Position() { } bool TextInput::Write(const std::string& key) { - if (key == "M-^H" || key == "M-^?" || key == "M-bksp" || key == "M-KEY_BACKSPACE") { - this->SetText(""); - } - else if (key == "^H" || key == "^?" || key == "KEY_BACKSPACE") { /* backspace */ - if (this->position > 0) { - if (removeUtf8Char(this->buffer, this->position)) { - this->position = std::max(0, this->position - 1); - this->TextChanged(this, this->buffer); - } - } - } - else if (key == "^M") { /* return */ - this->EnterPressed(this); + /* one character at a time. if it's more than one character, we're + dealing with an escape sequence and should not print it. */ + if (u8len(key) == 1) { + size_t offset = u8offset(this->buffer, this->position); + offset = (offset == std::string::npos) ? 0 : offset; + this->buffer.insert(offset, key); + this->TextChanged(this, this->buffer); + ++this->position; } else { - /* one character at a time. if it's more than one character, we're - dealing with an escape sequence and should not print it. */ - if (u8len(key) == 1) { - size_t offset = u8offset(this->buffer, this->position); - offset = (offset == std::string::npos) ? 0 : offset; - this->buffer.insert(offset, key); - this->TextChanged(this, this->buffer); - ++this->position; - } - else { - return false; - } + return false; } this->bufferLength = u8len(buffer); @@ -135,7 +119,25 @@ bool TextInput::Write(const std::string& key) { } bool TextInput::KeyPress(const std::string& key) { - if (key == "KEY_LEFT") { + if (key == "M-KEY_BACKSPACE") { + this->SetText(""); + return true; + } + else if (key == "KEY_BACKSPACE") { + if (this->position > 0) { + if (removeUtf8Char(this->buffer, this->position)) { + redrawContents(*this, buffer); + this->position = std::max(0, this->position - 1); + this->TextChanged(this, this->buffer); + } + } + return true; + } + else if (key == "KEY_ENTER") { + this->EnterPressed(this); + return true; + } + else if (key == "KEY_LEFT") { return this->OffsetPosition(-1); } else if (key == "KEY_RIGHT") { @@ -152,7 +154,7 @@ bool TextInput::KeyPress(const std::string& key) { return true; } else if (key == "KEY_DC") { - if (this->bufferLength > this->position) { + if ((int) this->bufferLength > this->position) { removeUtf8Char(this->buffer, this->position + 1); this->bufferLength = u8len(buffer); redrawContents(*this, buffer);