Added a simple key normalization layer in Text.cpp, used in Main.cpp in

the main read loop.
This commit is contained in:
casey 2016-07-08 22:08:52 -07:00
parent 65dfb59be6
commit edf9dfd257
11 changed files with 75 additions and 47 deletions

View File

@ -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") {

View File

@ -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;
}

View File

@ -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;

View File

@ -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;
}

View File

@ -162,7 +162,7 @@ bool SearchLayout::KeyPress(const std::string& key) {
CategoryListView* category = dynamic_cast<CategoryListView*>(focus.get());
if (category) {
if (key == "^M") {
if (key == "KEY_ENTER") {
int index = (int) category->GetSelectedIndex();
if (index >= 0) {
this->SearchResultSelected(

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -35,6 +35,7 @@
#include "stdafx.h"
#include "Text.h"
#include <unordered_map>
#include <boost/lexical_cast.hpp>
#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<std::string, std::string> 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;
}
}
}

View File

@ -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);
}
}

View File

@ -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);