Implement real canonical mode

This commit is contained in:
Baptiste Wicht 2016-08-17 20:31:16 +02:00
parent 4f44367468
commit 9eb9c5e4ea
2 changed files with 20 additions and 51 deletions

View File

@ -18,7 +18,6 @@ constexpr const size_t MAX_TERMINALS = 2;
size_t active_terminal; size_t active_terminal;
bool shift = false; bool shift = false;
bool ctrl = false;
std::array<stdio::virtual_terminal, MAX_TERMINALS> terminals; std::array<stdio::virtual_terminal, MAX_TERMINALS> terminals;
@ -36,22 +35,17 @@ void stdio::virtual_terminal::send_input(char key){
key &= ~(0x80); key &= ~(0x80);
if(key == keyboard::KEY_LEFT_SHIFT || key == keyboard::KEY_RIGHT_SHIFT){ if(key == keyboard::KEY_LEFT_SHIFT || key == keyboard::KEY_RIGHT_SHIFT){
shift = false; shift = false;
} else if(key == keyboard::KEY_LEFT_CTRL){
ctrl = false;
} }
} }
//Key pressed //Key pressed
else { else {
if(key == keyboard::KEY_LEFT_SHIFT || key == keyboard::KEY_RIGHT_SHIFT){ if(key == keyboard::KEY_LEFT_SHIFT || key == keyboard::KEY_RIGHT_SHIFT){
shift = true; shift = true;
} else if(key == keyboard::KEY_LEFT_CTRL){
ctrl = true;
} else if(key == keyboard::KEY_BACKSPACE){ } else if(key == keyboard::KEY_BACKSPACE){
if(!input_buffer.empty() || !canonical_buffer.empty()){ if(!input_buffer.empty()){
input_buffer.pop_last();
print('\b'); print('\b');
} }
input_buffer.push('\b');
} else { } else {
auto qwertz_key = auto qwertz_key =
shift shift
@ -59,16 +53,19 @@ void stdio::virtual_terminal::send_input(char key){
: keyboard::key_to_ascii(key); : keyboard::key_to_ascii(key);
if(qwertz_key){ if(qwertz_key){
if(ctrl && qwertz_key == 'c'){ input_buffer.push(qwertz_key);
input_buffer.push(200);
} else {
input_buffer.push(qwertz_key);
print(qwertz_key); print(qwertz_key);
}
if(!input_queue.empty()){ if(qwertz_key == '\n'){
input_queue.wake_up(); // Transfer current line to the canonical buffer
while(!input_buffer.empty()){
canonical_buffer.push(input_buffer.pop());
}
if(!input_queue.empty()){
input_queue.wake_up();
}
} }
} }
} }
@ -80,42 +77,18 @@ void stdio::virtual_terminal::send_input(char key){
size_t stdio::virtual_terminal::read_input(char* buffer, size_t max){ size_t stdio::virtual_terminal::read_input(char* buffer, size_t max){
size_t read = 0; size_t read = 0;
char c;
while(true){ while(true){
while(read < max && !input_buffer.empty()){ while(!canonical_buffer.empty()){
c = input_buffer.pop(); auto c = canonical_buffer.pop();
canonical_buffer.push(c); buffer[read++] = c;
if(c == '\b'){ if(read >= max || c == '\n'){
if(read > 0){ return read;
--read;
}
} else {
++read;
if(c == '\n'){
break;
}
} }
} }
if(read > 0 && (c == '\n' || c == 200 || read == max)){
read = 0;
while(!canonical_buffer.empty()){
auto value = canonical_buffer.pop();
if(value == '\b'){
--read;
} else {
buffer[read++] = value;
}
}
return read;
}
input_queue.sleep(); input_queue.sleep();
} }
} }

View File

@ -113,17 +113,13 @@ void pwd_command(const std::vector<std::string>&){
int main(){ int main(){
get_console_information(); get_console_information();
char input_buffer[128]; char input_buffer[256];
std::string current_input; std::string current_input;
print("thor> "); print("thor> ");
while(true){ while(true){
auto c = read_input(input_buffer, 127); auto c = read_input(input_buffer, 255 );
if(input_buffer[c-1] == 200){
input_buffer[c-1] = '\n';
}
if(input_buffer[c-1] == '\n'){ if(input_buffer[c-1] == '\n'){
if(c > 1){ if(c > 1){