2020-01-18 09:38:21 +01:00
|
|
|
|
/*
|
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
2021-05-23 15:47:41 +02:00
|
|
|
|
* Copyright (c) 2021, Daniel Bertalan <dani@danielbertalan.dev>
|
2020-01-18 09:38:21 +01:00
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
|
*/
|
|
|
|
|
|
|
2021-05-08 20:37:43 +02:00
|
|
|
|
#include "Terminal.h"
|
2021-01-24 15:28:26 +01:00
|
|
|
|
#include <AK/Debug.h>
|
2019-08-12 17:32:16 +02:00
|
|
|
|
#include <AK/StringBuilder.h>
|
2020-05-27 00:31:30 +03:00
|
|
|
|
#include <AK/StringView.h>
|
2019-08-12 17:32:16 +02:00
|
|
|
|
#include <LibVT/Terminal.h>
|
2021-04-16 22:58:51 +03:00
|
|
|
|
#ifdef KERNEL
|
|
|
|
|
|
# include <Kernel/TTY/VirtualConsole.h>
|
|
|
|
|
|
#endif
|
2019-08-12 17:32:16 +02:00
|
|
|
|
|
|
|
|
|
|
namespace VT {
|
|
|
|
|
|
|
2021-04-16 22:58:51 +03:00
|
|
|
|
#ifndef KERNEL
|
2019-08-12 17:32:16 +02:00
|
|
|
|
Terminal::Terminal(TerminalClient& client)
|
2021-04-16 22:58:51 +03:00
|
|
|
|
#else
|
|
|
|
|
|
Terminal::Terminal(Kernel::VirtualConsole& client)
|
|
|
|
|
|
#endif
|
2019-08-12 17:32:16 +02:00
|
|
|
|
: m_client(client)
|
2021-05-08 20:37:43 +02:00
|
|
|
|
, m_parser(*this)
|
2019-08-12 17:32:16 +02:00
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-04-16 22:58:51 +03:00
|
|
|
|
#ifndef KERNEL
|
2019-08-12 17:32:16 +02:00
|
|
|
|
void Terminal::clear()
|
|
|
|
|
|
{
|
|
|
|
|
|
for (size_t i = 0; i < rows(); ++i)
|
2021-05-23 15:47:41 +02:00
|
|
|
|
active_buffer()[i].clear(m_current_state.attribute);
|
2019-08-12 17:32:16 +02:00
|
|
|
|
set_cursor(0, 0);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-05 23:34:02 +02:00
|
|
|
|
void Terminal::clear_including_history()
|
|
|
|
|
|
{
|
|
|
|
|
|
m_history.clear();
|
2020-09-09 19:06:57 -04:00
|
|
|
|
m_history_start = 0;
|
|
|
|
|
|
|
2020-07-05 23:34:02 +02:00
|
|
|
|
clear();
|
|
|
|
|
|
|
|
|
|
|
|
m_client.terminal_history_changed();
|
|
|
|
|
|
}
|
2021-04-16 22:58:51 +03:00
|
|
|
|
#endif
|
2020-07-05 23:34:02 +02:00
|
|
|
|
|
2021-05-15 14:58:24 +02:00
|
|
|
|
void Terminal::alter_mode(bool should_set, Parameters params, Intermediates intermediates)
|
|
|
|
|
|
{
|
2021-05-24 09:36:41 +02:00
|
|
|
|
auto steady_cursor_to_blinking = [](CursorStyle style) {
|
|
|
|
|
|
switch (style) {
|
|
|
|
|
|
case SteadyBar:
|
|
|
|
|
|
return BlinkingBar;
|
|
|
|
|
|
case SteadyBlock:
|
|
|
|
|
|
return BlinkingBlock;
|
|
|
|
|
|
case SteadyUnderline:
|
|
|
|
|
|
return BlinkingUnderline;
|
|
|
|
|
|
default:
|
|
|
|
|
|
return style;
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
auto blinking_cursor_to_steady = [](CursorStyle style) {
|
|
|
|
|
|
switch (style) {
|
|
|
|
|
|
case BlinkingBar:
|
|
|
|
|
|
return SteadyBar;
|
|
|
|
|
|
case BlinkingBlock:
|
|
|
|
|
|
return SteadyBlock;
|
|
|
|
|
|
case BlinkingUnderline:
|
|
|
|
|
|
return SteadyUnderline;
|
|
|
|
|
|
default:
|
|
|
|
|
|
return style;
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2021-05-15 14:58:24 +02:00
|
|
|
|
if (intermediates.size() > 0 && intermediates[0] == '?') {
|
|
|
|
|
|
for (auto mode : params) {
|
|
|
|
|
|
switch (mode) {
|
|
|
|
|
|
case 3: {
|
|
|
|
|
|
// 80/132-column mode (DECCOLM)
|
2021-05-24 09:36:41 +02:00
|
|
|
|
unsigned new_columns = should_set ? 132 : 80;
|
2021-05-15 14:58:24 +02:00
|
|
|
|
dbgln_if(TERMINAL_DEBUG, "Setting {}-column mode", new_columns);
|
|
|
|
|
|
set_size(new_columns, rows());
|
|
|
|
|
|
clear();
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
2021-05-24 09:36:41 +02:00
|
|
|
|
case 12:
|
|
|
|
|
|
if (should_set) {
|
|
|
|
|
|
// Start blinking cursor
|
|
|
|
|
|
m_cursor_style = steady_cursor_to_blinking(m_cursor_style);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// Stop blinking cursor
|
|
|
|
|
|
m_cursor_style = blinking_cursor_to_steady(m_cursor_style);
|
|
|
|
|
|
}
|
|
|
|
|
|
m_client.set_cursor_style(m_cursor_style);
|
|
|
|
|
|
break;
|
2021-05-15 14:58:24 +02:00
|
|
|
|
case 25:
|
2021-05-24 09:36:41 +02:00
|
|
|
|
if (should_set) {
|
|
|
|
|
|
// Show cursor
|
|
|
|
|
|
m_cursor_style = m_saved_cursor_style;
|
|
|
|
|
|
m_client.set_cursor_style(m_cursor_style);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// Hide cursor
|
|
|
|
|
|
m_saved_cursor_style = m_cursor_style;
|
|
|
|
|
|
m_cursor_style = None;
|
|
|
|
|
|
m_client.set_cursor_style(None);
|
|
|
|
|
|
}
|
2021-05-15 14:58:24 +02:00
|
|
|
|
break;
|
2021-05-23 15:47:41 +02:00
|
|
|
|
case 1047:
|
|
|
|
|
|
#ifndef KERNEL
|
|
|
|
|
|
if (should_set) {
|
|
|
|
|
|
dbgln_if(TERMINAL_DEBUG, "Switching to Alternate Screen Buffer");
|
|
|
|
|
|
m_use_alternate_screen_buffer = true;
|
|
|
|
|
|
clear();
|
|
|
|
|
|
} else {
|
|
|
|
|
|
dbgln_if(TERMINAL_DEBUG, "Switching to Normal Screen Buffer");
|
|
|
|
|
|
m_use_alternate_screen_buffer = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
m_need_full_flush = true;
|
|
|
|
|
|
#else
|
|
|
|
|
|
dbgln("Alternate Screen Buffer is not supported");
|
|
|
|
|
|
#endif
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 1048:
|
|
|
|
|
|
if (should_set)
|
|
|
|
|
|
SCOSC();
|
|
|
|
|
|
else
|
|
|
|
|
|
SCORC();
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 1049:
|
|
|
|
|
|
#ifndef KERNEL
|
|
|
|
|
|
if (should_set) {
|
|
|
|
|
|
dbgln_if(TERMINAL_DEBUG, "Switching to Alternate Screen Buffer and saving state");
|
|
|
|
|
|
m_normal_saved_state = m_current_state;
|
|
|
|
|
|
m_use_alternate_screen_buffer = true;
|
|
|
|
|
|
clear();
|
|
|
|
|
|
} else {
|
|
|
|
|
|
dbgln_if(TERMINAL_DEBUG, "Switching to Normal Screen Buffer and restoring state");
|
|
|
|
|
|
m_current_state = m_normal_saved_state;
|
|
|
|
|
|
m_use_alternate_screen_buffer = false;
|
|
|
|
|
|
set_cursor(cursor_row(), cursor_column());
|
|
|
|
|
|
}
|
|
|
|
|
|
m_need_full_flush = true;
|
|
|
|
|
|
#else
|
|
|
|
|
|
dbgln("Alternate Screen Buffer is not supported");
|
|
|
|
|
|
#endif
|
|
|
|
|
|
break;
|
2021-05-24 12:01:59 +02:00
|
|
|
|
case 2004:
|
|
|
|
|
|
dbgln_if(TERMINAL_DEBUG, "Setting bracketed mode enabled={}", should_set);
|
|
|
|
|
|
m_needs_bracketed_paste = should_set;
|
|
|
|
|
|
break;
|
2021-05-15 14:58:24 +02:00
|
|
|
|
default:
|
2021-05-17 16:33:47 +02:00
|
|
|
|
dbgln("Terminal::alter_mode: Unimplemented private mode {} (should_set={})", mode, should_set);
|
2021-05-15 14:58:24 +02:00
|
|
|
|
break;
|
|
|
|
|
|
}
|
2019-08-12 17:32:16 +02:00
|
|
|
|
}
|
|
|
|
|
|
} else {
|
2021-05-15 14:58:24 +02:00
|
|
|
|
for (auto mode : params) {
|
|
|
|
|
|
switch (mode) {
|
|
|
|
|
|
// FIXME: implement *something* for this
|
|
|
|
|
|
default:
|
2021-05-17 16:33:47 +02:00
|
|
|
|
dbgln("Terminal::alter_mode: Unimplemented mode {} (should_set={})", mode, should_set);
|
2021-05-15 14:58:24 +02:00
|
|
|
|
break;
|
|
|
|
|
|
}
|
2019-08-12 17:32:16 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-05-15 14:58:24 +02:00
|
|
|
|
void Terminal::RM(Parameters params, Intermediates intermediates)
|
2020-01-26 14:21:54 +00:00
|
|
|
|
{
|
2021-05-24 09:36:41 +02:00
|
|
|
|
alter_mode(false, params, intermediates);
|
2020-01-26 14:21:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-05-15 14:58:24 +02:00
|
|
|
|
void Terminal::SM(Parameters params, Intermediates intermediates)
|
2020-01-26 14:21:54 +00:00
|
|
|
|
{
|
2021-05-24 09:36:41 +02:00
|
|
|
|
alter_mode(true, params, intermediates);
|
2020-01-26 14:21:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-05-08 20:37:43 +02:00
|
|
|
|
void Terminal::SGR(Parameters params)
|
2019-08-12 17:32:16 +02:00
|
|
|
|
{
|
|
|
|
|
|
if (params.is_empty()) {
|
2021-05-23 15:47:41 +02:00
|
|
|
|
m_current_state.attribute.reset();
|
2019-08-12 17:32:16 +02:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
2021-05-17 16:33:47 +02:00
|
|
|
|
auto parse_color = [&]() -> Optional<u32> {
|
|
|
|
|
|
if (params.size() < 2) {
|
|
|
|
|
|
dbgln("Color code has no type");
|
|
|
|
|
|
return {};
|
|
|
|
|
|
}
|
2020-05-10 12:11:03 +04:30
|
|
|
|
u32 color = 0;
|
2021-05-17 16:33:47 +02:00
|
|
|
|
switch (params[1]) {
|
2020-05-10 12:11:03 +04:30
|
|
|
|
case 5: // 8-bit
|
2021-05-17 16:33:47 +02:00
|
|
|
|
if (params.size() < 3) {
|
|
|
|
|
|
dbgln("8-bit color code has too few parameters");
|
|
|
|
|
|
return {};
|
|
|
|
|
|
}
|
|
|
|
|
|
return xterm_colors[params[2]];
|
2020-05-10 12:11:03 +04:30
|
|
|
|
case 2: // 24-bit
|
2021-05-17 16:33:47 +02:00
|
|
|
|
if (params.size() < 5) {
|
|
|
|
|
|
dbgln("24-bit color code has too few parameters");
|
|
|
|
|
|
return {};
|
|
|
|
|
|
}
|
2020-05-10 12:11:03 +04:30
|
|
|
|
for (size_t i = 0; i < 3; ++i) {
|
|
|
|
|
|
color <<= 8;
|
2021-05-17 16:33:47 +02:00
|
|
|
|
color |= params[i + 2];
|
2020-05-10 12:11:03 +04:30
|
|
|
|
}
|
2021-05-17 16:33:47 +02:00
|
|
|
|
return color;
|
2020-05-10 12:11:03 +04:30
|
|
|
|
default:
|
2021-05-17 16:33:47 +02:00
|
|
|
|
dbgln("Unknown color type {}", params[1]);
|
|
|
|
|
|
return {};
|
2020-05-10 12:11:03 +04:30
|
|
|
|
}
|
2021-05-17 16:33:47 +02:00
|
|
|
|
};
|
2020-05-10 12:11:03 +04:30
|
|
|
|
|
2021-05-17 16:33:47 +02:00
|
|
|
|
if (params[0] == 38) {
|
2021-05-23 15:47:41 +02:00
|
|
|
|
m_current_state.attribute.foreground_color = parse_color().value_or(m_current_state.attribute.foreground_color);
|
2021-05-17 16:33:47 +02:00
|
|
|
|
} else if (params[0] == 48) {
|
2021-05-23 15:47:41 +02:00
|
|
|
|
m_current_state.attribute.background_color = parse_color().value_or(m_current_state.attribute.background_color);
|
2021-05-17 16:33:47 +02:00
|
|
|
|
} else {
|
|
|
|
|
|
// A single escape sequence may set multiple parameters.
|
|
|
|
|
|
for (auto param : params) {
|
|
|
|
|
|
switch (param) {
|
|
|
|
|
|
case 0:
|
|
|
|
|
|
// Reset
|
2021-05-23 15:47:41 +02:00
|
|
|
|
m_current_state.attribute.reset();
|
2021-05-17 16:33:47 +02:00
|
|
|
|
break;
|
|
|
|
|
|
case 1:
|
2021-05-23 15:47:41 +02:00
|
|
|
|
m_current_state.attribute.flags |= Attribute::Bold;
|
2021-05-17 16:33:47 +02:00
|
|
|
|
break;
|
|
|
|
|
|
case 3:
|
2021-05-23 15:47:41 +02:00
|
|
|
|
m_current_state.attribute.flags |= Attribute::Italic;
|
2021-05-17 16:33:47 +02:00
|
|
|
|
break;
|
|
|
|
|
|
case 4:
|
2021-05-23 15:47:41 +02:00
|
|
|
|
m_current_state.attribute.flags |= Attribute::Underline;
|
2021-05-17 16:33:47 +02:00
|
|
|
|
break;
|
|
|
|
|
|
case 5:
|
2021-05-23 15:47:41 +02:00
|
|
|
|
m_current_state.attribute.flags |= Attribute::Blink;
|
2021-05-17 16:33:47 +02:00
|
|
|
|
break;
|
|
|
|
|
|
case 7:
|
2021-05-23 15:47:41 +02:00
|
|
|
|
m_current_state.attribute.flags |= Attribute::Negative;
|
2021-05-17 16:33:47 +02:00
|
|
|
|
break;
|
|
|
|
|
|
case 22:
|
2021-05-23 15:47:41 +02:00
|
|
|
|
m_current_state.attribute.flags &= ~Attribute::Bold;
|
2021-05-17 16:33:47 +02:00
|
|
|
|
break;
|
|
|
|
|
|
case 23:
|
2021-05-23 15:47:41 +02:00
|
|
|
|
m_current_state.attribute.flags &= ~Attribute::Italic;
|
2021-05-17 16:33:47 +02:00
|
|
|
|
break;
|
|
|
|
|
|
case 24:
|
2021-05-23 15:47:41 +02:00
|
|
|
|
m_current_state.attribute.flags &= ~Attribute::Underline;
|
2021-05-17 16:33:47 +02:00
|
|
|
|
break;
|
|
|
|
|
|
case 25:
|
2021-05-23 15:47:41 +02:00
|
|
|
|
m_current_state.attribute.flags &= ~Attribute::Blink;
|
2021-05-17 16:33:47 +02:00
|
|
|
|
break;
|
|
|
|
|
|
case 27:
|
2021-05-23 15:47:41 +02:00
|
|
|
|
m_current_state.attribute.flags &= ~Attribute::Negative;
|
2021-05-17 16:33:47 +02:00
|
|
|
|
break;
|
|
|
|
|
|
case 30:
|
|
|
|
|
|
case 31:
|
|
|
|
|
|
case 32:
|
|
|
|
|
|
case 33:
|
|
|
|
|
|
case 34:
|
|
|
|
|
|
case 35:
|
|
|
|
|
|
case 36:
|
|
|
|
|
|
case 37:
|
|
|
|
|
|
// Foreground color
|
2021-05-23 15:47:41 +02:00
|
|
|
|
if (m_current_state.attribute.flags & Attribute::Bold)
|
2021-05-17 16:33:47 +02:00
|
|
|
|
param += 8;
|
2021-05-23 15:47:41 +02:00
|
|
|
|
m_current_state.attribute.foreground_color = xterm_colors[param - 30];
|
2021-05-17 16:33:47 +02:00
|
|
|
|
break;
|
|
|
|
|
|
case 39:
|
|
|
|
|
|
// reset foreground
|
2021-05-23 15:47:41 +02:00
|
|
|
|
m_current_state.attribute.foreground_color = Attribute::default_foreground_color;
|
2021-05-17 16:33:47 +02:00
|
|
|
|
break;
|
|
|
|
|
|
case 40:
|
|
|
|
|
|
case 41:
|
|
|
|
|
|
case 42:
|
|
|
|
|
|
case 43:
|
|
|
|
|
|
case 44:
|
|
|
|
|
|
case 45:
|
|
|
|
|
|
case 46:
|
|
|
|
|
|
case 47:
|
|
|
|
|
|
// Background color
|
2021-05-23 15:47:41 +02:00
|
|
|
|
if (m_current_state.attribute.flags & Attribute::Bold)
|
2021-05-17 16:33:47 +02:00
|
|
|
|
param += 8;
|
2021-05-23 15:47:41 +02:00
|
|
|
|
m_current_state.attribute.background_color = xterm_colors[param - 40];
|
2021-05-17 16:33:47 +02:00
|
|
|
|
break;
|
|
|
|
|
|
case 49:
|
|
|
|
|
|
// reset background
|
2021-05-23 15:47:41 +02:00
|
|
|
|
m_current_state.attribute.background_color = Attribute::default_background_color;
|
2021-05-17 16:33:47 +02:00
|
|
|
|
break;
|
|
|
|
|
|
default:
|
|
|
|
|
|
dbgln("FIXME: SGR: p: {}", param);
|
2020-05-10 12:11:03 +04:30
|
|
|
|
}
|
2019-08-12 17:32:16 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-05-08 20:37:43 +02:00
|
|
|
|
void Terminal::SCOSC()
|
2019-08-12 17:32:16 +02:00
|
|
|
|
{
|
2021-05-23 15:47:41 +02:00
|
|
|
|
dbgln_if(TERMINAL_DEBUG, "Save cursor position");
|
|
|
|
|
|
m_saved_cursor_position = m_current_state.cursor;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Terminal::SCORC()
|
|
|
|
|
|
{
|
|
|
|
|
|
dbgln_if(TERMINAL_DEBUG, "Restore cursor position");
|
|
|
|
|
|
m_current_state.cursor = m_saved_cursor_position;
|
|
|
|
|
|
set_cursor(cursor_row(), cursor_column());
|
2019-08-12 17:32:16 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-05-23 15:47:41 +02:00
|
|
|
|
void Terminal::DECSC()
|
2019-08-12 17:32:16 +02:00
|
|
|
|
{
|
2021-05-23 15:47:41 +02:00
|
|
|
|
dbgln_if(TERMINAL_DEBUG, "Save cursor (and other state)");
|
|
|
|
|
|
if (m_use_alternate_screen_buffer) {
|
|
|
|
|
|
m_alternate_saved_state = m_current_state;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
m_normal_saved_state = m_current_state;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Terminal::DECRC()
|
|
|
|
|
|
{
|
|
|
|
|
|
dbgln_if(TERMINAL_DEBUG, "Restore cursor (and other state)");
|
|
|
|
|
|
if (m_use_alternate_screen_buffer) {
|
|
|
|
|
|
m_current_state = m_alternate_saved_state;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
m_current_state = m_normal_saved_state;
|
|
|
|
|
|
}
|
|
|
|
|
|
set_cursor(cursor_row(), cursor_column());
|
2019-08-12 17:32:16 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-05-08 20:37:43 +02:00
|
|
|
|
void Terminal::XTERM_WM(Parameters params)
|
2019-08-12 17:32:16 +02:00
|
|
|
|
{
|
|
|
|
|
|
if (params.size() < 1)
|
|
|
|
|
|
return;
|
2021-05-24 10:40:00 +02:00
|
|
|
|
switch (params[0]) {
|
|
|
|
|
|
case 22: {
|
|
|
|
|
|
if (params.size() > 1 && params[1] == 1) {
|
|
|
|
|
|
dbgln("FIXME: we don't support icon titles");
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
dbgln_if(TERMINAL_DEBUG, "Title stack push: {}", m_current_window_title);
|
|
|
|
|
|
[[maybe_unused]] auto rc = m_title_stack.try_append(move(m_current_window_title));
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
case 23: {
|
|
|
|
|
|
if (params.size() > 1 && params[1] == 1)
|
|
|
|
|
|
return;
|
|
|
|
|
|
if (m_title_stack.is_empty()) {
|
|
|
|
|
|
dbgln("Shenanigans: Tried to pop from empty title stack");
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
m_current_window_title = m_title_stack.take_last();
|
|
|
|
|
|
dbgln_if(TERMINAL_DEBUG, "Title stack pop: {}", m_current_window_title);
|
|
|
|
|
|
m_client.set_window_title(m_current_window_title);
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
default:
|
|
|
|
|
|
dbgln("FIXME: XTERM_WM: Ps: {} (param count: {})", params[0], params.size());
|
|
|
|
|
|
}
|
2019-08-12 17:32:16 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-05-08 20:37:43 +02:00
|
|
|
|
void Terminal::DECSTBM(Parameters params)
|
2019-08-12 17:32:16 +02:00
|
|
|
|
{
|
|
|
|
|
|
unsigned top = 1;
|
|
|
|
|
|
unsigned bottom = m_rows;
|
|
|
|
|
|
if (params.size() >= 1)
|
|
|
|
|
|
top = params[0];
|
|
|
|
|
|
if (params.size() >= 2)
|
|
|
|
|
|
bottom = params[1];
|
|
|
|
|
|
if ((bottom - top) < 2 || bottom > m_rows) {
|
2021-01-16 22:52:54 +01:00
|
|
|
|
dbgln("Error: DECSTBM: scrolling region invalid: {}-{}", top, bottom);
|
2019-08-12 17:32:16 +02:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
m_scroll_region_top = top - 1;
|
|
|
|
|
|
m_scroll_region_bottom = bottom - 1;
|
|
|
|
|
|
set_cursor(0, 0);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-05-08 20:37:43 +02:00
|
|
|
|
void Terminal::CUP(Parameters params)
|
2019-08-12 17:32:16 +02:00
|
|
|
|
{
|
2020-01-26 13:42:00 +00:00
|
|
|
|
// CUP – Cursor Position
|
2019-08-12 17:32:16 +02:00
|
|
|
|
unsigned row = 1;
|
|
|
|
|
|
unsigned col = 1;
|
|
|
|
|
|
if (params.size() >= 1)
|
|
|
|
|
|
row = params[0];
|
|
|
|
|
|
if (params.size() >= 2)
|
|
|
|
|
|
col = params[1];
|
|
|
|
|
|
set_cursor(row - 1, col - 1);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-05-08 20:37:43 +02:00
|
|
|
|
void Terminal::HVP(Parameters params)
|
2020-01-25 19:49:42 +01:00
|
|
|
|
{
|
|
|
|
|
|
unsigned row = 1;
|
|
|
|
|
|
unsigned col = 1;
|
2021-05-23 15:47:41 +02:00
|
|
|
|
if (params.size() >= 1 && params[0] != 0)
|
2020-01-25 19:49:42 +01:00
|
|
|
|
row = params[0];
|
2021-05-23 15:47:41 +02:00
|
|
|
|
if (params.size() >= 2 && params[1] != 0)
|
2020-01-25 19:49:42 +01:00
|
|
|
|
col = params[1];
|
|
|
|
|
|
set_cursor(row - 1, col - 1);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-05-08 20:37:43 +02:00
|
|
|
|
void Terminal::CUU(Parameters params)
|
2019-08-12 17:32:16 +02:00
|
|
|
|
{
|
2021-05-23 15:47:41 +02:00
|
|
|
|
unsigned num = 1;
|
|
|
|
|
|
if (params.size() >= 1 && params[0] != 0)
|
2019-08-12 17:32:16 +02:00
|
|
|
|
num = params[0];
|
2021-05-23 15:47:41 +02:00
|
|
|
|
int new_row = cursor_row() - num;
|
2019-08-12 17:32:16 +02:00
|
|
|
|
if (new_row < 0)
|
|
|
|
|
|
new_row = 0;
|
2021-05-23 15:47:41 +02:00
|
|
|
|
set_cursor(new_row, cursor_column());
|
2019-08-12 17:32:16 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-05-08 20:37:43 +02:00
|
|
|
|
void Terminal::CUD(Parameters params)
|
2019-08-12 17:32:16 +02:00
|
|
|
|
{
|
2021-05-23 15:47:41 +02:00
|
|
|
|
unsigned num = 1;
|
|
|
|
|
|
if (params.size() >= 1 && params[0] != 0)
|
2019-08-12 17:32:16 +02:00
|
|
|
|
num = params[0];
|
2021-05-23 15:47:41 +02:00
|
|
|
|
unsigned new_row = cursor_row() + num;
|
2019-08-12 17:32:16 +02:00
|
|
|
|
if (new_row >= m_rows)
|
|
|
|
|
|
new_row = m_rows - 1;
|
2021-05-23 15:47:41 +02:00
|
|
|
|
set_cursor(new_row, cursor_column());
|
2019-08-12 17:32:16 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-05-08 20:37:43 +02:00
|
|
|
|
void Terminal::CUF(Parameters params)
|
2019-08-12 17:32:16 +02:00
|
|
|
|
{
|
2021-05-23 15:47:41 +02:00
|
|
|
|
unsigned num = 1;
|
|
|
|
|
|
if (params.size() >= 1 && params[0] != 0)
|
2019-08-12 17:32:16 +02:00
|
|
|
|
num = params[0];
|
2021-05-23 15:47:41 +02:00
|
|
|
|
unsigned new_column = cursor_column() + num;
|
2019-08-12 17:32:16 +02:00
|
|
|
|
if (new_column >= m_columns)
|
|
|
|
|
|
new_column = m_columns - 1;
|
2021-05-23 15:47:41 +02:00
|
|
|
|
set_cursor(cursor_row(), new_column);
|
2019-08-12 17:32:16 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-05-08 20:37:43 +02:00
|
|
|
|
void Terminal::CUB(Parameters params)
|
2019-08-12 17:32:16 +02:00
|
|
|
|
{
|
2021-05-23 15:47:41 +02:00
|
|
|
|
unsigned num = 1;
|
|
|
|
|
|
if (params.size() >= 1 && params[0] != 0)
|
2019-08-12 17:32:16 +02:00
|
|
|
|
num = params[0];
|
2021-05-23 15:47:41 +02:00
|
|
|
|
int new_column = (int)cursor_column() - num;
|
2019-08-12 17:32:16 +02:00
|
|
|
|
if (new_column < 0)
|
|
|
|
|
|
new_column = 0;
|
2021-05-23 15:47:41 +02:00
|
|
|
|
set_cursor(cursor_row(), new_column);
|
2019-08-12 17:32:16 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-05-08 20:37:43 +02:00
|
|
|
|
void Terminal::CHA(Parameters params)
|
2019-08-12 17:32:16 +02:00
|
|
|
|
{
|
2021-05-23 15:47:41 +02:00
|
|
|
|
unsigned new_column = 1;
|
|
|
|
|
|
if (params.size() >= 1 && params[0] != 0)
|
2019-08-12 17:32:16 +02:00
|
|
|
|
new_column = params[0] - 1;
|
2021-05-23 15:47:41 +02:00
|
|
|
|
set_cursor(cursor_row(), new_column);
|
2019-08-12 17:32:16 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-05-08 20:37:43 +02:00
|
|
|
|
void Terminal::REP(Parameters params)
|
2019-08-12 17:32:16 +02:00
|
|
|
|
{
|
2021-05-23 15:47:41 +02:00
|
|
|
|
unsigned count = 1;
|
|
|
|
|
|
if (params.size() >= 1 && params[0] != 0)
|
|
|
|
|
|
count = params[0];
|
2019-08-12 17:32:16 +02:00
|
|
|
|
|
2021-05-23 15:47:41 +02:00
|
|
|
|
for (unsigned i = 0; i < count; ++i)
|
|
|
|
|
|
put_character_at(m_current_state.cursor.row, m_current_state.cursor.column++, m_last_code_point);
|
2019-08-12 17:32:16 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-05-08 20:37:43 +02:00
|
|
|
|
void Terminal::VPA(Parameters params)
|
2019-08-12 17:32:16 +02:00
|
|
|
|
{
|
2021-05-23 15:47:41 +02:00
|
|
|
|
unsigned new_row = 1;
|
|
|
|
|
|
if (params.size() >= 1 && params[0] != 0)
|
2019-08-12 17:32:16 +02:00
|
|
|
|
new_row = params[0] - 1;
|
2021-05-23 15:47:41 +02:00
|
|
|
|
set_cursor(new_row, cursor_column());
|
2019-08-12 17:32:16 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-05-08 20:37:43 +02:00
|
|
|
|
void Terminal::ECH(Parameters params)
|
2019-08-12 17:32:16 +02:00
|
|
|
|
{
|
|
|
|
|
|
// Erase characters (without moving cursor)
|
2021-05-23 15:47:41 +02:00
|
|
|
|
unsigned num = 1;
|
|
|
|
|
|
if (params.size() >= 1 && params[0] != 0)
|
2019-08-12 17:32:16 +02:00
|
|
|
|
num = params[0];
|
|
|
|
|
|
// Clear from cursor to end of line.
|
2021-05-23 15:47:41 +02:00
|
|
|
|
for (unsigned i = cursor_column(); i < num; ++i) {
|
|
|
|
|
|
put_character_at(cursor_row(), i, ' ');
|
2019-08-12 17:32:16 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-05-08 20:37:43 +02:00
|
|
|
|
void Terminal::EL(Parameters params)
|
2019-08-12 17:32:16 +02:00
|
|
|
|
{
|
2021-05-23 15:47:41 +02:00
|
|
|
|
unsigned mode = 0;
|
2019-08-12 17:32:16 +02:00
|
|
|
|
if (params.size() >= 1)
|
|
|
|
|
|
mode = params[0];
|
|
|
|
|
|
switch (mode) {
|
|
|
|
|
|
case 0:
|
|
|
|
|
|
// Clear from cursor to end of line.
|
2021-05-23 15:47:41 +02:00
|
|
|
|
for (int i = cursor_column(); i < m_columns; ++i) {
|
|
|
|
|
|
put_character_at(cursor_row(), i, ' ');
|
2019-08-12 17:32:16 +02:00
|
|
|
|
}
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 1:
|
|
|
|
|
|
// Clear from cursor to beginning of line.
|
2021-05-23 15:47:41 +02:00
|
|
|
|
for (int i = 0; i <= cursor_column(); ++i) {
|
|
|
|
|
|
put_character_at(cursor_row(), i, ' ');
|
2019-08-12 17:32:16 +02:00
|
|
|
|
}
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 2:
|
|
|
|
|
|
// Clear the complete line
|
|
|
|
|
|
for (int i = 0; i < m_columns; ++i) {
|
2021-05-23 15:47:41 +02:00
|
|
|
|
put_character_at(cursor_row(), i, ' ');
|
2019-08-12 17:32:16 +02:00
|
|
|
|
}
|
|
|
|
|
|
break;
|
|
|
|
|
|
default:
|
2021-05-08 20:37:43 +02:00
|
|
|
|
unimplemented_csi_sequence(params, {}, 'K');
|
2019-08-12 17:32:16 +02:00
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-05-08 20:37:43 +02:00
|
|
|
|
void Terminal::ED(Parameters params)
|
2019-08-12 17:32:16 +02:00
|
|
|
|
{
|
2021-05-23 15:47:41 +02:00
|
|
|
|
unsigned mode = 0;
|
2019-08-12 17:32:16 +02:00
|
|
|
|
if (params.size() >= 1)
|
|
|
|
|
|
mode = params[0];
|
|
|
|
|
|
switch (mode) {
|
|
|
|
|
|
case 0:
|
|
|
|
|
|
// Clear from cursor to end of screen.
|
2021-05-23 15:47:41 +02:00
|
|
|
|
for (int i = cursor_column(); i < m_columns; ++i)
|
|
|
|
|
|
put_character_at(cursor_row(), i, ' ');
|
|
|
|
|
|
for (int row = cursor_row() + 1; row < m_rows; ++row) {
|
2019-08-12 17:32:16 +02:00
|
|
|
|
for (int column = 0; column < m_columns; ++column) {
|
|
|
|
|
|
put_character_at(row, column, ' ');
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 1:
|
2020-01-25 20:51:49 +01:00
|
|
|
|
// Clear from cursor to beginning of screen.
|
2021-05-23 15:47:41 +02:00
|
|
|
|
for (int i = cursor_column(); i >= 0; --i)
|
|
|
|
|
|
put_character_at(cursor_row(), i, ' ');
|
|
|
|
|
|
for (int row = cursor_row() - 1; row >= 0; --row) {
|
2019-08-12 17:32:16 +02:00
|
|
|
|
for (int column = 0; column < m_columns; ++column) {
|
|
|
|
|
|
put_character_at(row, column, ' ');
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 2:
|
|
|
|
|
|
clear();
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 3:
|
|
|
|
|
|
// FIXME: <esc>[3J should also clear the scrollback buffer.
|
|
|
|
|
|
clear();
|
|
|
|
|
|
break;
|
|
|
|
|
|
default:
|
2021-05-08 20:37:43 +02:00
|
|
|
|
unimplemented_csi_sequence(params, {}, 'J');
|
2019-08-12 17:32:16 +02:00
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-05-08 20:37:43 +02:00
|
|
|
|
void Terminal::SU(Parameters params)
|
2019-08-12 17:32:16 +02:00
|
|
|
|
{
|
|
|
|
|
|
int count = 1;
|
|
|
|
|
|
if (params.size() >= 1)
|
|
|
|
|
|
count = params[0];
|
|
|
|
|
|
|
|
|
|
|
|
for (u16 i = 0; i < count; i++)
|
|
|
|
|
|
scroll_up();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-05-08 20:37:43 +02:00
|
|
|
|
void Terminal::SD(Parameters params)
|
2019-08-12 17:32:16 +02:00
|
|
|
|
{
|
|
|
|
|
|
int count = 1;
|
|
|
|
|
|
if (params.size() >= 1)
|
|
|
|
|
|
count = params[0];
|
|
|
|
|
|
|
|
|
|
|
|
for (u16 i = 0; i < count; i++)
|
|
|
|
|
|
scroll_down();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-05-24 09:36:41 +02:00
|
|
|
|
void Terminal::DECSCUSR(Parameters params)
|
|
|
|
|
|
{
|
|
|
|
|
|
unsigned style = 1;
|
|
|
|
|
|
if (params.size() >= 1 && params[0] != 0)
|
|
|
|
|
|
style = params[0];
|
|
|
|
|
|
switch (style) {
|
|
|
|
|
|
case 1:
|
|
|
|
|
|
m_client.set_cursor_style(BlinkingBlock);
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 2:
|
|
|
|
|
|
m_client.set_cursor_style(SteadyBlock);
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 3:
|
|
|
|
|
|
m_client.set_cursor_style(BlinkingUnderline);
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 4:
|
|
|
|
|
|
m_client.set_cursor_style(SteadyUnderline);
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 5:
|
|
|
|
|
|
m_client.set_cursor_style(BlinkingBar);
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 6:
|
|
|
|
|
|
m_client.set_cursor_style(SteadyBar);
|
|
|
|
|
|
break;
|
|
|
|
|
|
default:
|
|
|
|
|
|
dbgln("Unknown cursor style {}", style);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-04-16 22:58:51 +03:00
|
|
|
|
#ifndef KERNEL
|
2021-05-08 20:37:43 +02:00
|
|
|
|
void Terminal::IL(Parameters params)
|
2019-08-12 17:32:16 +02:00
|
|
|
|
{
|
2021-05-23 15:47:41 +02:00
|
|
|
|
unsigned count = 1;
|
|
|
|
|
|
if (params.size() >= 1 && params[0] != 0)
|
2019-08-12 17:32:16 +02:00
|
|
|
|
count = params[0];
|
|
|
|
|
|
invalidate_cursor();
|
|
|
|
|
|
for (; count > 0; --count) {
|
2021-05-23 15:47:41 +02:00
|
|
|
|
active_buffer().insert(cursor_row() + m_scroll_region_top, make<Line>(m_columns));
|
|
|
|
|
|
if (m_scroll_region_bottom + 1 < active_buffer().size())
|
|
|
|
|
|
active_buffer().remove(m_scroll_region_bottom + 1);
|
2019-08-12 17:32:16 +02:00
|
|
|
|
else
|
2021-05-23 15:47:41 +02:00
|
|
|
|
active_buffer().remove(active_buffer().size() - 1);
|
2019-08-12 17:32:16 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
m_need_full_flush = true;
|
|
|
|
|
|
}
|
2021-04-16 22:58:51 +03:00
|
|
|
|
#endif
|
2019-08-12 17:32:16 +02:00
|
|
|
|
|
2021-05-08 20:37:43 +02:00
|
|
|
|
void Terminal::DA(Parameters)
|
2020-01-25 19:12:08 +01:00
|
|
|
|
{
|
|
|
|
|
|
emit_string("\033[?1;0c");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-04-16 22:58:51 +03:00
|
|
|
|
#ifndef KERNEL
|
2021-05-08 20:37:43 +02:00
|
|
|
|
void Terminal::DL(Parameters params)
|
2019-08-12 17:32:16 +02:00
|
|
|
|
{
|
|
|
|
|
|
int count = 1;
|
2021-05-23 15:47:41 +02:00
|
|
|
|
if (params.size() >= 1 && params[0] != 0)
|
2019-08-12 17:32:16 +02:00
|
|
|
|
count = params[0];
|
|
|
|
|
|
|
2021-05-23 15:47:41 +02:00
|
|
|
|
if (count == 1 && cursor_row() == 0) {
|
2019-08-12 17:32:16 +02:00
|
|
|
|
scroll_up();
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-05-23 15:47:41 +02:00
|
|
|
|
int max_count = m_rows - (m_scroll_region_top + cursor_row());
|
2019-08-12 17:32:16 +02:00
|
|
|
|
count = min(count, max_count);
|
|
|
|
|
|
|
|
|
|
|
|
for (int c = count; c > 0; --c) {
|
2021-05-23 15:47:41 +02:00
|
|
|
|
active_buffer().remove(cursor_row() + m_scroll_region_top);
|
|
|
|
|
|
if (m_scroll_region_bottom < active_buffer().size())
|
|
|
|
|
|
active_buffer().insert(m_scroll_region_bottom, make<Line>(m_columns));
|
2019-08-12 17:32:16 +02:00
|
|
|
|
else
|
2021-05-23 15:47:41 +02:00
|
|
|
|
active_buffer().append(make<Line>(m_columns));
|
2019-08-12 17:32:16 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-05-08 20:37:43 +02:00
|
|
|
|
void Terminal::DCH(Parameters params)
|
2019-08-12 17:32:16 +02:00
|
|
|
|
{
|
|
|
|
|
|
int num = 1;
|
2021-05-23 15:47:41 +02:00
|
|
|
|
if (params.size() >= 1 && params[0] != 0)
|
2019-08-12 17:32:16 +02:00
|
|
|
|
num = params[0];
|
|
|
|
|
|
|
2021-05-23 15:47:41 +02:00
|
|
|
|
auto& line = active_buffer()[cursor_row()];
|
2019-08-12 17:32:16 +02:00
|
|
|
|
// Move n characters of line to the left
|
2021-05-23 15:47:41 +02:00
|
|
|
|
for (size_t i = cursor_column(); i < line.length() - num; i++)
|
2020-08-05 16:31:20 -04:00
|
|
|
|
line.set_code_point(i, line.code_point(i + num));
|
2019-08-12 17:32:16 +02:00
|
|
|
|
|
|
|
|
|
|
// Fill remainder of line with blanks
|
2021-02-26 20:28:22 +01:00
|
|
|
|
for (size_t i = line.length() - num; i < line.length(); i++)
|
2020-08-05 16:31:20 -04:00
|
|
|
|
line.set_code_point(i, ' ');
|
2019-08-12 17:32:16 +02:00
|
|
|
|
|
2020-05-15 18:57:50 +02:00
|
|
|
|
line.set_dirty(true);
|
2019-08-12 17:32:16 +02:00
|
|
|
|
}
|
2021-04-16 22:58:51 +03:00
|
|
|
|
#endif
|
2019-08-12 17:32:16 +02:00
|
|
|
|
|
2021-05-17 16:25:35 +02:00
|
|
|
|
void Terminal::linefeed()
|
2019-08-12 17:32:16 +02:00
|
|
|
|
{
|
2021-05-23 15:47:41 +02:00
|
|
|
|
u16 new_row = cursor_row();
|
|
|
|
|
|
if (cursor_row() == m_scroll_region_bottom) {
|
2019-08-12 17:32:16 +02:00
|
|
|
|
scroll_up();
|
|
|
|
|
|
} else {
|
|
|
|
|
|
++new_row;
|
2021-05-08 20:37:43 +02:00
|
|
|
|
};
|
2021-05-17 16:25:35 +02:00
|
|
|
|
// We shouldn't jump to the first column after receiving a line feed.
|
|
|
|
|
|
// The TTY will take care of generating the carriage return.
|
2021-05-23 15:47:41 +02:00
|
|
|
|
set_cursor(new_row, cursor_column());
|
2019-08-12 17:32:16 +02:00
|
|
|
|
}
|
2021-05-17 16:25:35 +02:00
|
|
|
|
|
2021-05-08 20:37:43 +02:00
|
|
|
|
void Terminal::carriage_return()
|
|
|
|
|
|
{
|
2021-05-23 15:47:41 +02:00
|
|
|
|
set_cursor(cursor_row(), 0);
|
2021-05-08 20:37:43 +02:00
|
|
|
|
}
|
2019-08-12 17:32:16 +02:00
|
|
|
|
|
2021-04-16 22:58:51 +03:00
|
|
|
|
#ifndef KERNEL
|
2019-08-12 17:32:16 +02:00
|
|
|
|
void Terminal::scroll_up()
|
|
|
|
|
|
{
|
2021-05-23 15:47:41 +02:00
|
|
|
|
dbgln_if(TERMINAL_DEBUG, "Scroll up 1 line");
|
2019-08-12 17:32:16 +02:00
|
|
|
|
// NOTE: We have to invalidate the cursor first.
|
|
|
|
|
|
invalidate_cursor();
|
2021-05-23 15:47:41 +02:00
|
|
|
|
if (m_scroll_region_top == 0 && !m_use_alternate_screen_buffer) {
|
|
|
|
|
|
auto line = move(active_buffer().ptr_at(m_scroll_region_top));
|
2020-09-09 19:06:57 -04:00
|
|
|
|
add_line_to_history(move(line));
|
2019-08-19 19:07:52 +02:00
|
|
|
|
m_client.terminal_history_changed();
|
|
|
|
|
|
}
|
2021-05-23 15:47:41 +02:00
|
|
|
|
active_buffer().remove(m_scroll_region_top);
|
|
|
|
|
|
active_buffer().insert(m_scroll_region_bottom, make<Line>(m_columns));
|
2019-08-12 17:32:16 +02:00
|
|
|
|
m_need_full_flush = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Terminal::scroll_down()
|
|
|
|
|
|
{
|
2021-05-23 15:47:41 +02:00
|
|
|
|
dbgln_if(TERMINAL_DEBUG, "Scroll down 1 line");
|
2019-08-12 17:32:16 +02:00
|
|
|
|
// NOTE: We have to invalidate the cursor first.
|
|
|
|
|
|
invalidate_cursor();
|
2021-05-23 15:47:41 +02:00
|
|
|
|
active_buffer().remove(m_scroll_region_bottom);
|
|
|
|
|
|
active_buffer().insert(m_scroll_region_top, make<Line>(m_columns));
|
2019-08-12 17:32:16 +02:00
|
|
|
|
m_need_full_flush = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-04-16 22:58:51 +03:00
|
|
|
|
void Terminal::put_character_at(unsigned row, unsigned column, u32 code_point)
|
|
|
|
|
|
{
|
|
|
|
|
|
VERIFY(row < rows());
|
|
|
|
|
|
VERIFY(column < columns());
|
2021-05-23 15:47:41 +02:00
|
|
|
|
auto& line = active_buffer()[row];
|
2021-04-16 22:58:51 +03:00
|
|
|
|
line.set_code_point(column, code_point);
|
2021-05-23 15:47:41 +02:00
|
|
|
|
line.attribute_at(column) = m_current_state.attribute;
|
2021-04-16 22:58:51 +03:00
|
|
|
|
line.attribute_at(column).flags |= Attribute::Touched;
|
|
|
|
|
|
line.set_dirty(true);
|
|
|
|
|
|
|
|
|
|
|
|
m_last_code_point = code_point;
|
|
|
|
|
|
}
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
2021-05-23 15:47:41 +02:00
|
|
|
|
void Terminal::set_cursor(unsigned a_row, unsigned a_column, bool skip_debug)
|
2019-08-12 17:32:16 +02:00
|
|
|
|
{
|
|
|
|
|
|
unsigned row = min(a_row, m_rows - 1u);
|
|
|
|
|
|
unsigned column = min(a_column, m_columns - 1u);
|
2021-05-23 15:47:41 +02:00
|
|
|
|
if (row == cursor_row() && column == cursor_column())
|
2019-08-12 17:32:16 +02:00
|
|
|
|
return;
|
2021-02-23 20:42:32 +01:00
|
|
|
|
VERIFY(row < rows());
|
|
|
|
|
|
VERIFY(column < columns());
|
2019-08-12 17:32:16 +02:00
|
|
|
|
invalidate_cursor();
|
2021-05-23 15:47:41 +02:00
|
|
|
|
m_current_state.cursor.row = row;
|
|
|
|
|
|
m_current_state.cursor.column = column;
|
2020-01-25 19:50:23 +01:00
|
|
|
|
m_stomp = false;
|
2019-08-12 17:32:16 +02:00
|
|
|
|
invalidate_cursor();
|
2021-05-23 15:47:41 +02:00
|
|
|
|
if (!skip_debug)
|
|
|
|
|
|
dbgln_if(TERMINAL_DEBUG, "Set cursor position: {},{}", cursor_row(), cursor_column());
|
2019-08-12 17:32:16 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-01-25 20:01:43 +01:00
|
|
|
|
void Terminal::NEL()
|
|
|
|
|
|
{
|
2021-05-17 16:25:35 +02:00
|
|
|
|
linefeed();
|
2021-05-08 20:37:43 +02:00
|
|
|
|
carriage_return();
|
2020-01-25 20:01:43 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-01-25 20:17:50 +01:00
|
|
|
|
void Terminal::IND()
|
|
|
|
|
|
{
|
2020-01-26 13:35:39 +00:00
|
|
|
|
CUD({});
|
2020-01-25 20:17:50 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Terminal::RI()
|
|
|
|
|
|
{
|
2020-01-26 13:33:27 +00:00
|
|
|
|
CUU({});
|
2020-01-25 20:17:50 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-05-08 20:37:43 +02:00
|
|
|
|
void Terminal::DSR(Parameters params)
|
2020-04-09 07:43:12 +04:30
|
|
|
|
{
|
2020-05-09 10:38:57 +02:00
|
|
|
|
if (params.size() == 1 && params[0] == 5) {
|
|
|
|
|
|
// Device status
|
|
|
|
|
|
emit_string("\033[0n"); // Terminal status OK!
|
|
|
|
|
|
} else if (params.size() == 1 && params[0] == 6) {
|
|
|
|
|
|
// Cursor position query
|
2021-05-23 15:47:41 +02:00
|
|
|
|
emit_string(String::formatted("\e[{};{}R", cursor_row() + 1, cursor_column() + 1));
|
2020-05-09 10:38:57 +02:00
|
|
|
|
} else {
|
2021-01-09 18:51:44 +01:00
|
|
|
|
dbgln("Unknown DSR");
|
2020-05-09 10:38:57 +02:00
|
|
|
|
}
|
2020-04-09 07:43:12 +04:30
|
|
|
|
}
|
|
|
|
|
|
|
2021-04-16 22:58:51 +03:00
|
|
|
|
#ifndef KERNEL
|
2021-05-08 20:37:43 +02:00
|
|
|
|
void Terminal::ICH(Parameters params)
|
2021-01-08 01:33:17 -05:00
|
|
|
|
{
|
2021-05-23 15:47:41 +02:00
|
|
|
|
unsigned num = 1;
|
|
|
|
|
|
if (params.size() >= 1 && params[0] != 0)
|
2021-01-08 01:33:17 -05:00
|
|
|
|
num = params[0];
|
|
|
|
|
|
|
2021-05-23 15:47:41 +02:00
|
|
|
|
auto& line = active_buffer()[cursor_row()];
|
2021-01-08 01:33:17 -05:00
|
|
|
|
// Move characters after cursor to the right
|
2021-05-23 15:47:41 +02:00
|
|
|
|
for (unsigned i = line.length() - num; i >= cursor_column(); --i)
|
2021-01-08 01:33:17 -05:00
|
|
|
|
line.set_code_point(i + num, line.code_point(i));
|
|
|
|
|
|
|
|
|
|
|
|
// Fill n characters after cursor with blanks
|
2021-05-23 15:47:41 +02:00
|
|
|
|
for (unsigned i = 0; i < num; i++)
|
|
|
|
|
|
line.set_code_point(cursor_column() + i, ' ');
|
2021-01-08 01:33:17 -05:00
|
|
|
|
|
|
|
|
|
|
line.set_dirty(true);
|
|
|
|
|
|
}
|
2021-04-16 22:58:51 +03:00
|
|
|
|
#endif
|
2021-01-08 01:33:17 -05:00
|
|
|
|
|
2021-05-08 20:37:43 +02:00
|
|
|
|
void Terminal::on_input(u8 byte)
|
2019-08-12 17:32:16 +02:00
|
|
|
|
{
|
2021-05-08 20:37:43 +02:00
|
|
|
|
m_parser.on_input(byte);
|
|
|
|
|
|
}
|
2020-05-16 19:47:49 +02:00
|
|
|
|
|
2021-05-08 20:37:43 +02:00
|
|
|
|
void Terminal::emit_code_point(u32 code_point)
|
|
|
|
|
|
{
|
2021-05-23 15:47:41 +02:00
|
|
|
|
auto new_column = cursor_column() + 1;
|
2021-05-08 20:37:43 +02:00
|
|
|
|
if (new_column < columns()) {
|
2021-05-23 15:47:41 +02:00
|
|
|
|
put_character_at(cursor_row(), cursor_column(), code_point);
|
|
|
|
|
|
set_cursor(cursor_row(), new_column, true);
|
2020-05-16 19:47:49 +02:00
|
|
|
|
return;
|
2019-08-12 17:32:16 +02:00
|
|
|
|
}
|
2021-05-08 20:37:43 +02:00
|
|
|
|
if (m_stomp) {
|
|
|
|
|
|
m_stomp = false;
|
|
|
|
|
|
carriage_return();
|
2021-05-17 16:25:35 +02:00
|
|
|
|
linefeed();
|
2021-05-23 15:47:41 +02:00
|
|
|
|
put_character_at(cursor_row(), cursor_column(), code_point);
|
|
|
|
|
|
set_cursor(cursor_row(), 1);
|
2021-05-08 20:37:43 +02:00
|
|
|
|
} else {
|
|
|
|
|
|
// Curious: We wait once on the right-hand side
|
|
|
|
|
|
m_stomp = true;
|
2021-05-23 15:47:41 +02:00
|
|
|
|
put_character_at(cursor_row(), cursor_column(), code_point);
|
2021-05-08 20:37:43 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2019-08-12 17:32:16 +02:00
|
|
|
|
|
2021-05-08 20:37:43 +02:00
|
|
|
|
void Terminal::execute_control_code(u8 code)
|
|
|
|
|
|
{
|
|
|
|
|
|
switch (code) {
|
|
|
|
|
|
case '\a':
|
|
|
|
|
|
m_client.beep();
|
2019-08-12 17:32:16 +02:00
|
|
|
|
return;
|
2021-05-08 20:37:43 +02:00
|
|
|
|
case '\b':
|
2021-05-23 15:47:41 +02:00
|
|
|
|
if (cursor_column()) {
|
|
|
|
|
|
set_cursor(cursor_row(), cursor_column() - 1);
|
2019-08-12 17:32:16 +02:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
return;
|
|
|
|
|
|
case '\t': {
|
2021-05-23 15:47:41 +02:00
|
|
|
|
for (unsigned i = cursor_column() + 1; i < columns(); ++i) {
|
2019-08-12 17:32:16 +02:00
|
|
|
|
if (m_horizontal_tabs[i]) {
|
2021-05-23 15:47:41 +02:00
|
|
|
|
set_cursor(cursor_row(), i);
|
2019-08-12 17:32:16 +02:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
case '\n':
|
2021-05-17 16:25:35 +02:00
|
|
|
|
case '\v':
|
|
|
|
|
|
case '\f':
|
|
|
|
|
|
linefeed();
|
2019-08-12 17:32:16 +02:00
|
|
|
|
return;
|
2021-05-08 20:37:43 +02:00
|
|
|
|
case '\r':
|
|
|
|
|
|
carriage_return();
|
|
|
|
|
|
return;
|
|
|
|
|
|
default:
|
|
|
|
|
|
unimplemented_control_code(code);
|
2019-08-12 17:32:16 +02:00
|
|
|
|
}
|
2021-05-08 20:37:43 +02:00
|
|
|
|
}
|
2019-08-12 17:32:16 +02:00
|
|
|
|
|
2021-05-08 20:37:43 +02:00
|
|
|
|
void Terminal::execute_escape_sequence(Intermediates intermediates, bool ignore, u8 last_byte)
|
|
|
|
|
|
{
|
|
|
|
|
|
// FIXME: Handle it somehow?
|
|
|
|
|
|
if (ignore)
|
|
|
|
|
|
dbgln("Escape sequence has its ignore flag set.");
|
|
|
|
|
|
|
|
|
|
|
|
if (intermediates.size() == 0) {
|
|
|
|
|
|
switch (last_byte) {
|
|
|
|
|
|
case 'D':
|
|
|
|
|
|
IND();
|
|
|
|
|
|
return;
|
|
|
|
|
|
case 'E':
|
|
|
|
|
|
NEL();
|
|
|
|
|
|
return;
|
|
|
|
|
|
case 'M':
|
|
|
|
|
|
RI();
|
|
|
|
|
|
return;
|
2021-05-16 13:14:37 +00:00
|
|
|
|
case '\\':
|
|
|
|
|
|
// ST (string terminator) -- do nothing
|
2021-05-16 15:16:50 +01:00
|
|
|
|
return;
|
2021-05-23 15:47:41 +02:00
|
|
|
|
case '7':
|
|
|
|
|
|
DECSC();
|
|
|
|
|
|
return;
|
|
|
|
|
|
case '8':
|
|
|
|
|
|
DECRC();
|
|
|
|
|
|
return;
|
2021-05-08 20:37:43 +02:00
|
|
|
|
}
|
|
|
|
|
|
} else if (intermediates[0] == '#') {
|
|
|
|
|
|
switch (last_byte) {
|
|
|
|
|
|
case '8':
|
|
|
|
|
|
// Confidence Test - Fill screen with E's
|
|
|
|
|
|
for (size_t row = 0; row < m_rows; ++row) {
|
|
|
|
|
|
for (size_t column = 0; column < m_columns; ++column) {
|
|
|
|
|
|
put_character_at(row, column, 'E');
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
unimplemented_escape_sequence(intermediates, last_byte);
|
2020-05-16 19:47:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-05-08 20:37:43 +02:00
|
|
|
|
void Terminal::execute_csi_sequence(Parameters parameters, Intermediates intermediates, bool ignore, u8 last_byte)
|
2020-05-16 19:47:49 +02:00
|
|
|
|
{
|
2021-05-08 20:37:43 +02:00
|
|
|
|
// FIXME: Handle it somehow?
|
|
|
|
|
|
if (ignore)
|
|
|
|
|
|
dbgln("CSI sequence has its ignore flag set.");
|
|
|
|
|
|
|
|
|
|
|
|
switch (last_byte) {
|
|
|
|
|
|
case '@':
|
|
|
|
|
|
ICH(parameters);
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 'A':
|
|
|
|
|
|
CUU(parameters);
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 'B':
|
|
|
|
|
|
CUD(parameters);
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 'C':
|
|
|
|
|
|
CUF(parameters);
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 'D':
|
|
|
|
|
|
CUB(parameters);
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 'G':
|
|
|
|
|
|
CHA(parameters);
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 'H':
|
|
|
|
|
|
CUP(parameters);
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 'J':
|
|
|
|
|
|
ED(parameters);
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 'K':
|
|
|
|
|
|
EL(parameters);
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 'L':
|
|
|
|
|
|
IL(parameters);
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 'M':
|
|
|
|
|
|
DL(parameters);
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 'P':
|
|
|
|
|
|
DCH(parameters);
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 'S':
|
|
|
|
|
|
SU(parameters);
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 'T':
|
|
|
|
|
|
SD(parameters);
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 'X':
|
|
|
|
|
|
ECH(parameters);
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 'b':
|
|
|
|
|
|
REP(parameters);
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 'd':
|
|
|
|
|
|
VPA(parameters);
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 'm':
|
|
|
|
|
|
SGR(parameters);
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 's':
|
|
|
|
|
|
SCOSC();
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 'u':
|
2021-05-23 15:47:41 +02:00
|
|
|
|
SCORC();
|
2021-05-08 20:37:43 +02:00
|
|
|
|
break;
|
|
|
|
|
|
case 't':
|
|
|
|
|
|
XTERM_WM(parameters);
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 'r':
|
|
|
|
|
|
DECSTBM(parameters);
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 'l':
|
2021-05-15 14:58:24 +02:00
|
|
|
|
RM(parameters, intermediates);
|
2021-05-08 20:37:43 +02:00
|
|
|
|
break;
|
|
|
|
|
|
case 'h':
|
2021-05-15 14:58:24 +02:00
|
|
|
|
SM(parameters, intermediates);
|
2021-05-08 20:37:43 +02:00
|
|
|
|
break;
|
|
|
|
|
|
case 'c':
|
|
|
|
|
|
DA(parameters);
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 'f':
|
|
|
|
|
|
HVP(parameters);
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 'n':
|
|
|
|
|
|
DSR(parameters);
|
|
|
|
|
|
break;
|
2021-05-24 09:36:41 +02:00
|
|
|
|
case 'q':
|
|
|
|
|
|
if (intermediates.size() >= 1 && intermediates[0] == ' ')
|
|
|
|
|
|
DECSCUSR(parameters);
|
|
|
|
|
|
else
|
|
|
|
|
|
unimplemented_csi_sequence(parameters, intermediates, last_byte);
|
|
|
|
|
|
break;
|
2021-05-08 20:37:43 +02:00
|
|
|
|
default:
|
|
|
|
|
|
unimplemented_csi_sequence(parameters, intermediates, last_byte);
|
2020-05-16 19:47:49 +02:00
|
|
|
|
}
|
2021-05-08 20:37:43 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Terminal::execute_osc_sequence(OscParameters parameters, u8 last_byte)
|
|
|
|
|
|
{
|
|
|
|
|
|
auto stringview_ify = [&](size_t param_idx) {
|
2021-05-17 16:32:11 +02:00
|
|
|
|
return StringView(parameters[param_idx]);
|
2021-05-08 20:37:43 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
2021-05-17 16:32:11 +02:00
|
|
|
|
if (parameters.size() == 0 || parameters[0].is_empty()) {
|
|
|
|
|
|
unimplemented_osc_sequence(parameters, last_byte);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
auto command_number = stringview_ify(0).to_uint();
|
|
|
|
|
|
if (!command_number.has_value()) {
|
|
|
|
|
|
unimplemented_osc_sequence(parameters, last_byte);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
switch (command_number.value()) {
|
|
|
|
|
|
case 0:
|
|
|
|
|
|
case 1:
|
|
|
|
|
|
case 2:
|
2021-05-24 10:40:00 +02:00
|
|
|
|
if (parameters.size() < 2) {
|
2021-05-17 16:32:11 +02:00
|
|
|
|
dbgln("Attempted to set window title without any parameters");
|
2021-05-24 10:40:00 +02:00
|
|
|
|
} else {
|
|
|
|
|
|
// FIXME: the split breaks titles containing semicolons.
|
|
|
|
|
|
// Should we expose the raw OSC string from the parser? Or join by semicolon?
|
|
|
|
|
|
m_current_window_title = stringview_ify(1).to_string();
|
|
|
|
|
|
m_client.set_window_title(m_current_window_title);
|
|
|
|
|
|
}
|
2021-05-17 16:32:11 +02:00
|
|
|
|
break;
|
|
|
|
|
|
case 8:
|
2021-04-16 22:58:51 +03:00
|
|
|
|
#ifndef KERNEL
|
2021-05-17 16:32:11 +02:00
|
|
|
|
if (parameters.size() < 3) {
|
|
|
|
|
|
dbgln("Attempted to set href but gave too few parameters");
|
|
|
|
|
|
} else if (parameters[1].is_empty() && parameters[2].is_empty()) {
|
|
|
|
|
|
// Clear hyperlink
|
2021-05-23 15:47:41 +02:00
|
|
|
|
m_current_state.attribute.href = String();
|
|
|
|
|
|
m_current_state.attribute.href_id = String();
|
2021-05-08 20:37:43 +02:00
|
|
|
|
} else {
|
2021-05-23 15:47:41 +02:00
|
|
|
|
m_current_state.attribute.href = stringview_ify(2);
|
2021-05-17 16:32:11 +02:00
|
|
|
|
// FIXME: Respect the provided ID
|
2021-05-23 15:47:41 +02:00
|
|
|
|
m_current_state.attribute.href_id = String::number(m_next_href_id++);
|
2021-05-08 20:37:43 +02:00
|
|
|
|
}
|
2021-05-17 16:32:11 +02:00
|
|
|
|
#endif
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 9:
|
|
|
|
|
|
if (parameters.size() < 2)
|
|
|
|
|
|
dbgln("Atttempted to set window progress but gave too few parameters");
|
|
|
|
|
|
else if (parameters.size() == 2)
|
|
|
|
|
|
m_client.set_window_progress(stringview_ify(1).to_int().value_or(-1), 0);
|
|
|
|
|
|
else
|
|
|
|
|
|
m_client.set_window_progress(stringview_ify(1).to_int().value_or(-1), stringview_ify(2).to_int().value_or(0));
|
|
|
|
|
|
break;
|
|
|
|
|
|
default:
|
2021-05-08 20:37:43 +02:00
|
|
|
|
unimplemented_osc_sequence(parameters, last_byte);
|
2019-08-12 17:32:16 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-05-17 16:32:11 +02:00
|
|
|
|
void Terminal::dcs_hook(Parameters, Intermediates, bool, u8)
|
2021-05-08 20:37:43 +02:00
|
|
|
|
{
|
|
|
|
|
|
dbgln("Received DCS parameters, but we don't support it yet");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Terminal::receive_dcs_char(u8 byte)
|
|
|
|
|
|
{
|
|
|
|
|
|
dbgln_if(TERMINAL_DEBUG, "DCS string character {:c}", byte);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Terminal::execute_dcs_sequence()
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-10-22 22:14:36 +02:00
|
|
|
|
void Terminal::inject_string(const StringView& str)
|
2019-08-12 17:32:16 +02:00
|
|
|
|
{
|
2019-12-09 17:45:40 +01:00
|
|
|
|
for (size_t i = 0; i < str.length(); ++i)
|
2020-05-16 19:24:24 +02:00
|
|
|
|
on_input(str[i]);
|
2019-08-12 17:32:16 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-05-09 10:38:57 +02:00
|
|
|
|
void Terminal::emit_string(const StringView& string)
|
2020-01-25 19:12:08 +01:00
|
|
|
|
{
|
2020-05-09 10:38:57 +02:00
|
|
|
|
m_client.emit((const u8*)string.characters_without_null_termination(), string.length());
|
2020-01-25 19:12:08 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-06-13 13:56:39 +03:00
|
|
|
|
void Terminal::handle_key_press(KeyCode key, u32 code_point, u8 flags)
|
2020-05-27 00:31:30 +03:00
|
|
|
|
{
|
|
|
|
|
|
bool ctrl = flags & Mod_Ctrl;
|
|
|
|
|
|
bool alt = flags & Mod_Alt;
|
|
|
|
|
|
bool shift = flags & Mod_Shift;
|
2020-09-13 21:08:19 -04:00
|
|
|
|
unsigned modifier_mask = int(shift) + (int(alt) << 1) + (int(ctrl) << 2);
|
|
|
|
|
|
|
|
|
|
|
|
auto emit_final_with_modifier = [this, modifier_mask](char final) {
|
|
|
|
|
|
if (modifier_mask)
|
2021-04-21 22:41:49 +02:00
|
|
|
|
emit_string(String::formatted("\e[1;{}{:c}", modifier_mask + 1, final));
|
2020-09-13 21:08:19 -04:00
|
|
|
|
else
|
2021-04-21 22:41:49 +02:00
|
|
|
|
emit_string(String::formatted("\e[{:c}", final));
|
2020-09-13 21:08:19 -04:00
|
|
|
|
};
|
2020-09-13 21:31:59 -04:00
|
|
|
|
auto emit_tilde_with_modifier = [this, modifier_mask](unsigned num) {
|
|
|
|
|
|
if (modifier_mask)
|
2021-04-21 22:41:49 +02:00
|
|
|
|
emit_string(String::formatted("\e[{};{}~", num, modifier_mask + 1));
|
2020-09-13 21:31:59 -04:00
|
|
|
|
else
|
2021-04-21 22:41:49 +02:00
|
|
|
|
emit_string(String::formatted("\e[{}~", num));
|
2020-09-13 21:31:59 -04:00
|
|
|
|
};
|
2020-05-27 00:31:30 +03:00
|
|
|
|
|
|
|
|
|
|
switch (key) {
|
|
|
|
|
|
case KeyCode::Key_Up:
|
2020-09-13 21:08:19 -04:00
|
|
|
|
emit_final_with_modifier('A');
|
2020-05-27 00:31:30 +03:00
|
|
|
|
return;
|
|
|
|
|
|
case KeyCode::Key_Down:
|
2020-09-13 21:08:19 -04:00
|
|
|
|
emit_final_with_modifier('B');
|
2020-05-27 00:31:30 +03:00
|
|
|
|
return;
|
|
|
|
|
|
case KeyCode::Key_Right:
|
2020-09-13 21:08:19 -04:00
|
|
|
|
emit_final_with_modifier('C');
|
2020-05-27 00:31:30 +03:00
|
|
|
|
return;
|
|
|
|
|
|
case KeyCode::Key_Left:
|
2020-09-13 21:08:19 -04:00
|
|
|
|
emit_final_with_modifier('D');
|
2020-05-27 00:31:30 +03:00
|
|
|
|
return;
|
|
|
|
|
|
case KeyCode::Key_Insert:
|
2020-09-13 21:31:59 -04:00
|
|
|
|
emit_tilde_with_modifier(2);
|
2020-05-27 00:31:30 +03:00
|
|
|
|
return;
|
|
|
|
|
|
case KeyCode::Key_Delete:
|
2020-09-13 21:31:59 -04:00
|
|
|
|
emit_tilde_with_modifier(3);
|
2020-05-27 00:31:30 +03:00
|
|
|
|
return;
|
|
|
|
|
|
case KeyCode::Key_Home:
|
2020-09-13 21:08:19 -04:00
|
|
|
|
emit_final_with_modifier('H');
|
2020-05-27 00:31:30 +03:00
|
|
|
|
return;
|
|
|
|
|
|
case KeyCode::Key_End:
|
2020-09-13 21:08:19 -04:00
|
|
|
|
emit_final_with_modifier('F');
|
2020-05-27 00:31:30 +03:00
|
|
|
|
return;
|
|
|
|
|
|
case KeyCode::Key_PageUp:
|
2020-09-13 21:31:59 -04:00
|
|
|
|
emit_tilde_with_modifier(5);
|
2020-05-27 00:31:30 +03:00
|
|
|
|
return;
|
|
|
|
|
|
case KeyCode::Key_PageDown:
|
2020-09-13 21:31:59 -04:00
|
|
|
|
emit_tilde_with_modifier(6);
|
2020-05-27 00:31:30 +03:00
|
|
|
|
return;
|
2021-05-17 16:25:35 +02:00
|
|
|
|
case KeyCode::Key_Return:
|
|
|
|
|
|
// The standard says that CR should be generated by the return key.
|
|
|
|
|
|
// The TTY will take care of translating it to CR LF for the terminal.
|
|
|
|
|
|
emit_string("\r");
|
|
|
|
|
|
return;
|
2020-05-27 00:31:30 +03:00
|
|
|
|
default:
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-06-13 13:56:39 +03:00
|
|
|
|
if (!code_point) {
|
2020-05-29 22:13:51 +03:00
|
|
|
|
// Probably a modifier being pressed.
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-05-27 00:31:30 +03:00
|
|
|
|
if (shift && key == KeyCode::Key_Tab) {
|
|
|
|
|
|
emit_string("\033[Z");
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Key event was not one of the above special cases,
|
|
|
|
|
|
// attempt to treat it as a character...
|
|
|
|
|
|
if (ctrl) {
|
2020-06-13 13:56:39 +03:00
|
|
|
|
if (code_point >= 'a' && code_point <= 'z') {
|
|
|
|
|
|
code_point = code_point - 'a' + 1;
|
|
|
|
|
|
} else if (code_point == '\\') {
|
|
|
|
|
|
code_point = 0x1c;
|
2020-05-27 00:31:30 +03:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Alt modifier sends escape prefix.
|
|
|
|
|
|
if (alt)
|
|
|
|
|
|
emit_string("\033");
|
|
|
|
|
|
|
2020-06-13 13:56:39 +03:00
|
|
|
|
StringBuilder sb;
|
2020-08-05 16:31:20 -04:00
|
|
|
|
sb.append_code_point(code_point);
|
2020-06-13 13:56:39 +03:00
|
|
|
|
|
|
|
|
|
|
emit_string(sb.to_string());
|
2020-05-27 00:31:30 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-05-08 20:37:43 +02:00
|
|
|
|
void Terminal::unimplemented_control_code(u8 code)
|
|
|
|
|
|
{
|
|
|
|
|
|
dbgln("Unimplemented control code {:02x}", code);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Terminal::unimplemented_escape_sequence(Intermediates intermediates, u8 last_byte)
|
2019-08-12 17:32:16 +02:00
|
|
|
|
{
|
|
|
|
|
|
StringBuilder builder;
|
2021-05-08 20:37:43 +02:00
|
|
|
|
builder.appendff("Unimplemented escape sequence {:c}", last_byte);
|
|
|
|
|
|
if (!intermediates.is_empty()) {
|
|
|
|
|
|
builder.append(", intermediates: ");
|
|
|
|
|
|
for (size_t i = 0; i < intermediates.size(); ++i)
|
|
|
|
|
|
builder.append((char)intermediates[i]);
|
2019-08-12 17:32:16 +02:00
|
|
|
|
}
|
2021-05-08 20:37:43 +02:00
|
|
|
|
dbgln("{}", builder.string_view());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Terminal::unimplemented_csi_sequence(Parameters parameters, Intermediates intermediates, u8 last_byte)
|
|
|
|
|
|
{
|
|
|
|
|
|
StringBuilder builder;
|
|
|
|
|
|
builder.appendff("Unimplemented CSI sequence: {:c}", last_byte);
|
|
|
|
|
|
if (!parameters.is_empty()) {
|
|
|
|
|
|
builder.append(", parameters: [");
|
|
|
|
|
|
for (size_t i = 0; i < parameters.size(); ++i)
|
|
|
|
|
|
builder.appendff("{}{}", (i == 0) ? "" : ", ", parameters[i]);
|
|
|
|
|
|
builder.append("]");
|
|
|
|
|
|
}
|
|
|
|
|
|
if (!intermediates.is_empty()) {
|
2021-02-20 17:03:35 +01:00
|
|
|
|
builder.append(", intermediates:");
|
2021-05-08 20:37:43 +02:00
|
|
|
|
for (size_t i = 0; i < intermediates.size(); ++i)
|
|
|
|
|
|
builder.append((char)intermediates[i]);
|
2019-08-12 17:32:16 +02:00
|
|
|
|
}
|
2021-02-20 17:16:33 +01:00
|
|
|
|
dbgln("{}", builder.string_view());
|
2019-08-12 17:32:16 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-05-08 20:37:43 +02:00
|
|
|
|
void Terminal::unimplemented_osc_sequence(OscParameters parameters, u8 last_byte)
|
2019-08-12 17:32:16 +02:00
|
|
|
|
{
|
2021-05-08 20:37:43 +02:00
|
|
|
|
StringBuilder builder;
|
|
|
|
|
|
builder.appendff("Unimplemented OSC sequence parameters: (bel_terminated={}) [ ", last_byte == '\a');
|
|
|
|
|
|
bool first = true;
|
|
|
|
|
|
for (auto parameter : parameters) {
|
|
|
|
|
|
if (!first)
|
|
|
|
|
|
builder.append(", ");
|
|
|
|
|
|
builder.append("[");
|
|
|
|
|
|
for (auto character : parameter)
|
|
|
|
|
|
builder.append((char)character);
|
|
|
|
|
|
builder.append("]");
|
|
|
|
|
|
first = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
builder.append(" ]");
|
|
|
|
|
|
dbgln("{}", builder.string_view());
|
2019-08-12 17:32:16 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-04-16 22:58:51 +03:00
|
|
|
|
#ifndef KERNEL
|
2019-08-12 17:32:16 +02:00
|
|
|
|
void Terminal::set_size(u16 columns, u16 rows)
|
|
|
|
|
|
{
|
2020-01-15 21:13:54 -06:00
|
|
|
|
if (!columns)
|
|
|
|
|
|
columns = 1;
|
|
|
|
|
|
if (!rows)
|
|
|
|
|
|
rows = 1;
|
|
|
|
|
|
|
2019-08-12 17:32:16 +02:00
|
|
|
|
if (columns == m_columns && rows == m_rows)
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
if (rows > m_rows) {
|
2021-05-23 15:47:41 +02:00
|
|
|
|
while (m_normal_screen_buffer.size() < rows)
|
|
|
|
|
|
m_normal_screen_buffer.append(make<Line>(columns));
|
|
|
|
|
|
while (m_alternate_screen_buffer.size() < rows)
|
|
|
|
|
|
m_alternate_screen_buffer.append(make<Line>(columns));
|
2019-08-12 17:32:16 +02:00
|
|
|
|
} else {
|
2021-05-23 15:47:41 +02:00
|
|
|
|
m_normal_screen_buffer.shrink(rows);
|
|
|
|
|
|
m_alternate_screen_buffer.shrink(rows);
|
2019-08-12 17:32:16 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-05-23 15:47:41 +02:00
|
|
|
|
for (int i = 0; i < rows; ++i) {
|
|
|
|
|
|
m_normal_screen_buffer[i].set_length(columns);
|
|
|
|
|
|
m_alternate_screen_buffer[i].set_length(columns);
|
|
|
|
|
|
}
|
2019-08-12 17:32:16 +02:00
|
|
|
|
|
|
|
|
|
|
m_columns = columns;
|
|
|
|
|
|
m_rows = rows;
|
|
|
|
|
|
|
|
|
|
|
|
m_scroll_region_top = 0;
|
|
|
|
|
|
m_scroll_region_bottom = rows - 1;
|
|
|
|
|
|
|
2021-05-23 15:47:41 +02:00
|
|
|
|
m_current_state.cursor.clamp(m_rows - 1, m_columns - 1);
|
|
|
|
|
|
m_normal_saved_state.cursor.clamp(m_rows - 1, m_columns - 1);
|
|
|
|
|
|
m_alternate_saved_state.cursor.clamp(m_rows - 1, m_columns - 1);
|
|
|
|
|
|
m_saved_cursor_position.clamp(m_rows - 1, m_columns - 1);
|
2019-08-12 17:32:16 +02:00
|
|
|
|
|
|
|
|
|
|
m_horizontal_tabs.resize(columns);
|
|
|
|
|
|
for (unsigned i = 0; i < columns; ++i)
|
|
|
|
|
|
m_horizontal_tabs[i] = (i % 8) == 0;
|
|
|
|
|
|
// Rightmost column is always last tab on line.
|
|
|
|
|
|
m_horizontal_tabs[columns - 1] = 1;
|
|
|
|
|
|
|
|
|
|
|
|
m_client.terminal_did_resize(m_columns, m_rows);
|
|
|
|
|
|
}
|
2021-04-16 22:58:51 +03:00
|
|
|
|
#endif
|
2019-08-12 17:32:16 +02:00
|
|
|
|
|
2021-04-16 22:58:51 +03:00
|
|
|
|
#ifndef KERNEL
|
2019-08-12 17:32:16 +02:00
|
|
|
|
void Terminal::invalidate_cursor()
|
|
|
|
|
|
{
|
2021-05-23 15:47:41 +02:00
|
|
|
|
active_buffer()[cursor_row()].set_dirty(true);
|
2019-08-12 17:32:16 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-05-09 16:16:16 +02:00
|
|
|
|
Attribute Terminal::attribute_at(const Position& position) const
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!position.is_valid())
|
|
|
|
|
|
return {};
|
2020-05-10 16:59:02 +02:00
|
|
|
|
if (position.row() >= static_cast<int>(line_count()))
|
2020-05-09 16:16:16 +02:00
|
|
|
|
return {};
|
|
|
|
|
|
auto& line = this->line(position.row());
|
2021-02-26 20:28:22 +01:00
|
|
|
|
if (static_cast<size_t>(position.column()) >= line.length())
|
2020-05-09 16:16:16 +02:00
|
|
|
|
return {};
|
2021-02-26 20:28:22 +01:00
|
|
|
|
return line.attribute_at(position.column());
|
2020-05-09 16:16:16 +02:00
|
|
|
|
}
|
2021-04-16 22:58:51 +03:00
|
|
|
|
#endif
|
2020-05-09 16:16:16 +02:00
|
|
|
|
|
2019-08-12 17:32:16 +02:00
|
|
|
|
}
|