2021-05-09 06:37:36 +12:00
|
|
|
/* Copyright 2021 Aristocratos (jakob@qvantnet.com)
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
|
|
|
|
indent = tab
|
|
|
|
tab-size = 4
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _btop_input_included_
|
2021-05-15 23:24:24 +12:00
|
|
|
#define _btop_input_included_ 1
|
2021-05-09 06:37:36 +12:00
|
|
|
|
|
|
|
#include <string>
|
2021-05-29 12:32:36 +12:00
|
|
|
#include <robin_hood.h>
|
2021-05-09 06:37:36 +12:00
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
#include <btop_tools.h>
|
|
|
|
|
2021-05-29 12:32:36 +12:00
|
|
|
using std::string, robin_hood::unordered_flat_map, std::cin;
|
2021-05-17 08:58:16 +12:00
|
|
|
using namespace Tools;
|
2021-05-09 06:37:36 +12:00
|
|
|
|
2021-05-22 12:13:56 +12:00
|
|
|
/* The input functions relies on the following std::cin options being set:
|
|
|
|
cin.sync_with_stdio(false);
|
|
|
|
cin.tie(NULL);
|
|
|
|
These will automatically be set when running Term::init() from btop_tools.h
|
|
|
|
*/
|
2021-05-09 06:37:36 +12:00
|
|
|
|
2021-05-15 04:54:37 +12:00
|
|
|
//* Functions and variables for handling keyboard and mouse input
|
|
|
|
namespace Input {
|
|
|
|
namespace {
|
|
|
|
//* Map for translating key codes to readable values
|
2021-05-29 12:32:36 +12:00
|
|
|
const unordered_flat_map<string, string> Key_escapes = {
|
2021-05-15 04:54:37 +12:00
|
|
|
{"\033", "escape"},
|
|
|
|
{"\n", "enter"},
|
|
|
|
{" ", "space"},
|
|
|
|
{"\x7f", "backspace"},
|
|
|
|
{"\x08", "backspace"},
|
|
|
|
{"[A", "up"},
|
|
|
|
{"OA", "up"},
|
|
|
|
{"[B", "down"},
|
|
|
|
{"OB", "down"},
|
|
|
|
{"[D", "left"},
|
|
|
|
{"OD", "left"},
|
|
|
|
{"[C", "right"},
|
|
|
|
{"OC", "right"},
|
|
|
|
{"[2~", "insert"},
|
|
|
|
{"[3~", "delete"},
|
|
|
|
{"[H", "home"},
|
|
|
|
{"[F", "end"},
|
|
|
|
{"[5~", "page_up"},
|
|
|
|
{"[6~", "page_down"},
|
|
|
|
{"\t", "tab"},
|
|
|
|
{"[Z", "shift_tab"},
|
|
|
|
{"OP", "f1"},
|
|
|
|
{"OQ", "f2"},
|
|
|
|
{"OR", "f3"},
|
|
|
|
{"OS", "f4"},
|
|
|
|
{"[15~", "f5"},
|
|
|
|
{"[17~", "f6"},
|
|
|
|
{"[18~", "f7"},
|
|
|
|
{"[19~", "f8"},
|
|
|
|
{"[20~", "f9"},
|
|
|
|
{"[21~", "f10"},
|
|
|
|
{"[23~", "f11"},
|
|
|
|
{"[24~", "f12"}
|
|
|
|
};
|
2021-05-19 08:11:34 +12:00
|
|
|
}
|
2021-05-09 06:37:36 +12:00
|
|
|
|
2021-05-15 04:54:37 +12:00
|
|
|
//* Last entered key
|
2021-05-09 06:37:36 +12:00
|
|
|
string last = "";
|
|
|
|
|
2021-05-15 04:54:37 +12:00
|
|
|
//* Poll keyboard & mouse input for <timeout> ms and return input availabilty as a bool
|
|
|
|
bool poll(int timeout=0){
|
|
|
|
if (timeout < 1) return cin.rdbuf()->in_avail() > 0;
|
2021-05-09 06:37:36 +12:00
|
|
|
auto timer = 0;
|
2021-05-15 04:54:37 +12:00
|
|
|
while (timer * 10 <= timeout) {
|
2021-05-09 06:37:36 +12:00
|
|
|
if (cin.rdbuf()->in_avail() > 0) return true;
|
2021-05-09 10:18:51 +12:00
|
|
|
sleep_ms(10);
|
2021-05-15 04:54:37 +12:00
|
|
|
++timer;
|
2021-05-09 06:37:36 +12:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-05-15 04:54:37 +12:00
|
|
|
//* Get a key or mouse action from input
|
2021-05-09 06:37:36 +12:00
|
|
|
string get(){
|
2021-05-09 10:18:51 +12:00
|
|
|
string key;
|
2021-05-09 06:37:36 +12:00
|
|
|
while (cin.rdbuf()->in_avail() > 0 && key.size() < 100) key += cin.get();
|
2021-05-09 10:18:51 +12:00
|
|
|
if (!key.empty()){
|
2021-05-09 06:37:36 +12:00
|
|
|
if (key.substr(0,2) == Fx::e) key.erase(0, 1);
|
|
|
|
if (Key_escapes.contains(key)) key = Key_escapes.at(key);
|
|
|
|
else if (ulen(key) > 1) key = "";
|
2021-05-15 04:54:37 +12:00
|
|
|
last = key;
|
2021-05-09 06:37:36 +12:00
|
|
|
}
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
2021-05-31 03:01:57 +12:00
|
|
|
//* Wait until input is available
|
|
|
|
void wait(bool clear=false){
|
|
|
|
while (cin.rdbuf()->in_avail() < 1) sleep_ms(10);
|
|
|
|
if (clear) get();
|
|
|
|
}
|
|
|
|
|
2021-05-15 04:54:37 +12:00
|
|
|
void clear(){
|
|
|
|
last.clear();
|
2021-05-09 06:37:36 +12:00
|
|
|
}
|
|
|
|
|
2021-05-19 08:11:34 +12:00
|
|
|
}
|
2021-05-09 06:37:36 +12:00
|
|
|
|
|
|
|
#endif
|