2020-01-18 11:38:21 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2018-10-30 15:59:29 +03:00
|
|
|
#include "VirtualConsole.h"
|
2018-11-10 18:25:59 +03:00
|
|
|
#include "IO.h"
|
2018-10-30 15:59:29 +03:00
|
|
|
#include "StdLib.h"
|
2019-11-23 19:27:09 +03:00
|
|
|
#include <Kernel/Heap/kmalloc.h>
|
2019-09-06 16:34:26 +03:00
|
|
|
#include <AK/String.h>
|
2019-06-07 21:02:01 +03:00
|
|
|
#include <Kernel/Arch/i386/CPU.h>
|
2019-08-12 14:30:27 +03:00
|
|
|
#include <Kernel/Devices/KeyboardDevice.h>
|
2018-10-30 15:59:29 +03:00
|
|
|
|
2019-07-03 22:17:35 +03:00
|
|
|
static u8* s_vga_buffer;
|
2018-10-30 15:59:29 +03:00
|
|
|
static VirtualConsole* s_consoles[6];
|
2018-11-01 16:09:21 +03:00
|
|
|
static int s_active_console;
|
2018-10-30 15:59:29 +03:00
|
|
|
|
2019-07-03 22:17:35 +03:00
|
|
|
void VirtualConsole::get_vga_cursor(u8& row, u8& column)
|
2018-11-10 18:25:59 +03:00
|
|
|
{
|
2019-07-03 22:17:35 +03:00
|
|
|
u16 value;
|
2018-11-10 18:25:59 +03:00
|
|
|
IO::out8(0x3d4, 0x0e);
|
|
|
|
value = IO::in8(0x3d5) << 8;
|
|
|
|
IO::out8(0x3d4, 0x0f);
|
|
|
|
value |= IO::in8(0x3d5);
|
2018-11-29 05:45:23 +03:00
|
|
|
row = value / columns();
|
|
|
|
column = value % columns();
|
2018-11-10 18:25:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void VirtualConsole::flush_vga_cursor()
|
|
|
|
{
|
2019-07-03 22:17:35 +03:00
|
|
|
u16 value = m_current_vga_start_address + (m_cursor_row * columns() + m_cursor_column);
|
2018-11-10 18:25:59 +03:00
|
|
|
IO::out8(0x3d4, 0x0e);
|
|
|
|
IO::out8(0x3d5, MSB(value));
|
|
|
|
IO::out8(0x3d4, 0x0f);
|
|
|
|
IO::out8(0x3d5, LSB(value));
|
|
|
|
}
|
|
|
|
|
2018-10-30 15:59:29 +03:00
|
|
|
void VirtualConsole::initialize()
|
|
|
|
{
|
2020-01-17 21:59:20 +03:00
|
|
|
s_vga_buffer = (u8*)0xc00b8000;
|
2018-10-30 15:59:29 +03:00
|
|
|
memset(s_consoles, 0, sizeof(s_consoles));
|
2018-11-01 16:09:21 +03:00
|
|
|
s_active_console = -1;
|
2018-10-30 15:59:29 +03:00
|
|
|
}
|
|
|
|
|
2018-11-01 16:09:21 +03:00
|
|
|
VirtualConsole::VirtualConsole(unsigned index, InitialContents initial_contents)
|
2018-10-30 15:59:29 +03:00
|
|
|
: TTY(4, index)
|
|
|
|
, m_index(index)
|
|
|
|
{
|
2019-11-27 16:06:24 +03:00
|
|
|
sprintf(m_tty_name, "/dev/tty%u", m_index);
|
2018-11-29 05:45:23 +03:00
|
|
|
set_size(80, 25);
|
2019-10-18 15:14:28 +03:00
|
|
|
m_horizontal_tabs = static_cast<u8*>(kmalloc_eternal(columns()));
|
2018-12-07 02:17:23 +03:00
|
|
|
for (unsigned i = 0; i < columns(); ++i)
|
|
|
|
m_horizontal_tabs[i] = (i % 8) == 0;
|
2018-12-07 02:26:12 +03:00
|
|
|
// Rightmost column is always last tab on line.
|
|
|
|
m_horizontal_tabs[columns() - 1] = 1;
|
2018-12-07 02:17:23 +03:00
|
|
|
|
2018-10-30 15:59:29 +03:00
|
|
|
s_consoles[index] = this;
|
2019-07-03 22:17:35 +03:00
|
|
|
m_buffer = (u8*)kmalloc_eternal(rows() * columns() * 2);
|
2018-11-01 16:09:21 +03:00
|
|
|
if (initial_contents == AdoptCurrentVGABuffer) {
|
2018-11-29 05:45:23 +03:00
|
|
|
memcpy(m_buffer, s_vga_buffer, rows() * columns() * 2);
|
2018-11-10 18:25:59 +03:00
|
|
|
get_vga_cursor(m_cursor_row, m_cursor_column);
|
2018-10-30 15:59:29 +03:00
|
|
|
} else {
|
2019-07-03 22:17:35 +03:00
|
|
|
u16* line_mem = reinterpret_cast<u16*>(m_buffer);
|
|
|
|
for (u16 i = 0; i < rows() * columns(); ++i)
|
2018-11-01 16:09:21 +03:00
|
|
|
line_mem[i] = 0x0720;
|
2018-10-30 15:59:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
VirtualConsole::~VirtualConsole()
|
|
|
|
{
|
2019-10-18 15:14:28 +03:00
|
|
|
ASSERT_NOT_REACHED();
|
2018-10-30 15:59:29 +03:00
|
|
|
}
|
|
|
|
|
2018-11-10 02:56:10 +03:00
|
|
|
void VirtualConsole::clear()
|
|
|
|
{
|
2019-07-03 22:17:35 +03:00
|
|
|
u16* linemem = m_active ? (u16*)s_vga_buffer : (u16*)m_buffer;
|
|
|
|
for (u16 i = 0; i < rows() * columns(); ++i)
|
2018-11-10 02:56:10 +03:00
|
|
|
linemem[i] = 0x0720;
|
|
|
|
if (m_active)
|
|
|
|
set_vga_start_row(0);
|
|
|
|
set_cursor(0, 0);
|
|
|
|
}
|
|
|
|
|
2018-11-01 16:09:21 +03:00
|
|
|
void VirtualConsole::switch_to(unsigned index)
|
2018-10-30 15:59:29 +03:00
|
|
|
{
|
2018-11-01 16:09:21 +03:00
|
|
|
if ((int)index == s_active_console)
|
2018-10-30 15:59:29 +03:00
|
|
|
return;
|
|
|
|
ASSERT(index < 6);
|
|
|
|
ASSERT(s_consoles[index]);
|
2019-08-18 05:04:09 +03:00
|
|
|
|
2018-10-30 15:59:29 +03:00
|
|
|
InterruptDisabler disabler;
|
2019-08-18 05:04:09 +03:00
|
|
|
if (s_active_console != -1) {
|
|
|
|
auto* active_console = s_consoles[s_active_console];
|
|
|
|
// We won't know how to switch away from a graphical console until we
|
|
|
|
// can set the video mode on our own. Just stop anyone from trying for
|
|
|
|
// now.
|
|
|
|
if (active_console->is_graphical())
|
|
|
|
return;
|
|
|
|
active_console->set_active(false);
|
|
|
|
}
|
|
|
|
dbgprintf("VC: Switch to %u (%p)\n", index, s_consoles[index]);
|
2018-11-01 16:09:21 +03:00
|
|
|
s_active_console = index;
|
|
|
|
s_consoles[s_active_console]->set_active(true);
|
2019-01-31 19:31:23 +03:00
|
|
|
Console::the().set_implementation(s_consoles[s_active_console]);
|
2018-10-30 15:59:29 +03:00
|
|
|
}
|
|
|
|
|
2018-11-01 16:09:21 +03:00
|
|
|
void VirtualConsole::set_active(bool b)
|
2018-10-30 15:59:29 +03:00
|
|
|
{
|
|
|
|
if (b == m_active)
|
|
|
|
return;
|
|
|
|
|
|
|
|
InterruptDisabler disabler;
|
|
|
|
|
|
|
|
m_active = b;
|
|
|
|
if (!m_active) {
|
2018-11-29 05:45:23 +03:00
|
|
|
memcpy(m_buffer, m_current_vga_window, rows() * columns() * 2);
|
2019-08-12 14:30:27 +03:00
|
|
|
KeyboardDevice::the().set_client(nullptr);
|
2018-10-30 15:59:29 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-11-29 05:45:23 +03:00
|
|
|
memcpy(s_vga_buffer, m_buffer, rows() * columns() * 2);
|
2018-11-09 23:18:03 +03:00
|
|
|
set_vga_start_row(0);
|
2018-11-10 18:25:59 +03:00
|
|
|
flush_vga_cursor();
|
2018-10-30 17:33:37 +03:00
|
|
|
|
2019-08-12 14:30:27 +03:00
|
|
|
KeyboardDevice::the().set_client(this);
|
2018-10-30 15:59:29 +03:00
|
|
|
}
|
|
|
|
|
2019-07-03 22:17:35 +03:00
|
|
|
inline bool is_valid_parameter_character(u8 ch)
|
2018-10-30 15:59:29 +03:00
|
|
|
{
|
|
|
|
return ch >= 0x30 && ch <= 0x3f;
|
|
|
|
}
|
|
|
|
|
2019-07-03 22:17:35 +03:00
|
|
|
inline bool is_valid_intermediate_character(u8 ch)
|
2018-10-30 15:59:29 +03:00
|
|
|
{
|
|
|
|
return ch >= 0x20 && ch <= 0x2f;
|
|
|
|
}
|
|
|
|
|
2019-07-03 22:17:35 +03:00
|
|
|
inline bool is_valid_final_character(u8 ch)
|
2018-10-30 15:59:29 +03:00
|
|
|
{
|
|
|
|
return ch >= 0x40 && ch <= 0x7e;
|
|
|
|
}
|
|
|
|
|
2019-07-03 22:17:35 +03:00
|
|
|
enum class VGAColor : u8 {
|
2018-10-30 15:59:29 +03:00
|
|
|
Black = 0,
|
|
|
|
Blue,
|
|
|
|
Green,
|
|
|
|
Cyan,
|
|
|
|
Red,
|
|
|
|
Magenta,
|
|
|
|
Brown,
|
|
|
|
LightGray,
|
|
|
|
DarkGray,
|
|
|
|
BrightBlue,
|
|
|
|
BrightGreen,
|
|
|
|
BrightCyan,
|
|
|
|
BrightRed,
|
|
|
|
BrightMagenta,
|
|
|
|
Yellow,
|
|
|
|
White,
|
|
|
|
};
|
|
|
|
|
2019-07-03 22:17:35 +03:00
|
|
|
enum class ANSIColor : u8 {
|
2018-10-30 15:59:29 +03:00
|
|
|
Black = 0,
|
|
|
|
Red,
|
|
|
|
Green,
|
|
|
|
Brown,
|
|
|
|
Blue,
|
|
|
|
Magenta,
|
|
|
|
Cyan,
|
|
|
|
LightGray,
|
|
|
|
DarkGray,
|
|
|
|
BrightRed,
|
|
|
|
BrightGreen,
|
|
|
|
Yellow,
|
|
|
|
BrightBlue,
|
|
|
|
BrightMagenta,
|
|
|
|
BrightCyan,
|
|
|
|
White,
|
|
|
|
};
|
|
|
|
|
2018-11-01 16:09:21 +03:00
|
|
|
static inline VGAColor ansi_color_to_vga(ANSIColor color)
|
2018-10-30 15:59:29 +03:00
|
|
|
{
|
|
|
|
switch (color) {
|
2019-06-07 12:43:58 +03:00
|
|
|
case ANSIColor::Black:
|
|
|
|
return VGAColor::Black;
|
|
|
|
case ANSIColor::Red:
|
|
|
|
return VGAColor::Red;
|
|
|
|
case ANSIColor::Brown:
|
|
|
|
return VGAColor::Brown;
|
|
|
|
case ANSIColor::Blue:
|
|
|
|
return VGAColor::Blue;
|
|
|
|
case ANSIColor::Magenta:
|
|
|
|
return VGAColor::Magenta;
|
|
|
|
case ANSIColor::Green:
|
|
|
|
return VGAColor::Green;
|
|
|
|
case ANSIColor::Cyan:
|
|
|
|
return VGAColor::Cyan;
|
|
|
|
case ANSIColor::LightGray:
|
|
|
|
return VGAColor::LightGray;
|
|
|
|
case ANSIColor::DarkGray:
|
|
|
|
return VGAColor::DarkGray;
|
|
|
|
case ANSIColor::BrightRed:
|
|
|
|
return VGAColor::BrightRed;
|
|
|
|
case ANSIColor::BrightGreen:
|
|
|
|
return VGAColor::BrightGreen;
|
|
|
|
case ANSIColor::Yellow:
|
|
|
|
return VGAColor::Yellow;
|
|
|
|
case ANSIColor::BrightBlue:
|
|
|
|
return VGAColor::BrightBlue;
|
|
|
|
case ANSIColor::BrightMagenta:
|
|
|
|
return VGAColor::BrightMagenta;
|
|
|
|
case ANSIColor::BrightCyan:
|
|
|
|
return VGAColor::BrightCyan;
|
|
|
|
case ANSIColor::White:
|
|
|
|
return VGAColor::White;
|
2018-10-30 15:59:29 +03:00
|
|
|
}
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
return VGAColor::LightGray;
|
|
|
|
}
|
|
|
|
|
2019-07-03 22:17:35 +03:00
|
|
|
static inline u8 ansi_color_to_vga(u8 color)
|
2018-10-30 15:59:29 +03:00
|
|
|
{
|
2019-07-03 22:17:35 +03:00
|
|
|
return (u8)ansi_color_to_vga((ANSIColor)color);
|
2018-10-30 15:59:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void VirtualConsole::escape$m(const Vector<unsigned>& params)
|
|
|
|
{
|
|
|
|
for (auto param : params) {
|
|
|
|
switch (param) {
|
|
|
|
case 0:
|
|
|
|
// Reset
|
2018-11-01 16:09:21 +03:00
|
|
|
m_current_attribute = 0x07;
|
2018-10-30 15:59:29 +03:00
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
// Bold
|
2018-11-01 16:09:21 +03:00
|
|
|
m_current_attribute |= 8;
|
2018-10-30 15:59:29 +03:00
|
|
|
break;
|
|
|
|
case 30:
|
|
|
|
case 31:
|
|
|
|
case 32:
|
|
|
|
case 33:
|
|
|
|
case 34:
|
|
|
|
case 35:
|
|
|
|
case 36:
|
|
|
|
case 37:
|
|
|
|
// Foreground color
|
2018-11-01 16:09:21 +03:00
|
|
|
m_current_attribute &= ~0x7;
|
|
|
|
m_current_attribute |= ansi_color_to_vga(param - 30);
|
2018-10-30 15:59:29 +03:00
|
|
|
break;
|
|
|
|
case 40:
|
|
|
|
case 41:
|
|
|
|
case 42:
|
|
|
|
case 43:
|
|
|
|
case 44:
|
|
|
|
case 45:
|
|
|
|
case 46:
|
|
|
|
case 47:
|
|
|
|
// Background color
|
2018-11-01 16:09:21 +03:00
|
|
|
m_current_attribute &= ~0x70;
|
|
|
|
m_current_attribute |= ansi_color_to_vga(param - 30) << 8;
|
2018-10-30 15:59:29 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void VirtualConsole::escape$s(const Vector<unsigned>&)
|
|
|
|
{
|
2018-11-01 16:09:21 +03:00
|
|
|
m_saved_cursor_row = m_cursor_row;
|
|
|
|
m_saved_cursor_column = m_cursor_column;
|
2018-10-30 15:59:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void VirtualConsole::escape$u(const Vector<unsigned>&)
|
|
|
|
{
|
2018-11-01 16:09:21 +03:00
|
|
|
set_cursor(m_saved_cursor_row, m_saved_cursor_column);
|
2018-10-30 15:59:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void VirtualConsole::escape$H(const Vector<unsigned>& params)
|
|
|
|
{
|
|
|
|
unsigned row = 1;
|
|
|
|
unsigned col = 1;
|
|
|
|
if (params.size() >= 1)
|
|
|
|
row = params[0];
|
|
|
|
if (params.size() >= 2)
|
|
|
|
col = params[1];
|
2018-11-01 16:09:21 +03:00
|
|
|
set_cursor(row - 1, col - 1);
|
2018-10-30 15:59:29 +03:00
|
|
|
}
|
|
|
|
|
2018-11-11 17:36:40 +03:00
|
|
|
void VirtualConsole::escape$A(const Vector<unsigned>& params)
|
|
|
|
{
|
|
|
|
int num = 1;
|
|
|
|
if (params.size() >= 1)
|
|
|
|
num = params[0];
|
|
|
|
int new_row = (int)m_cursor_row - num;
|
|
|
|
if (new_row < 0)
|
|
|
|
new_row = 0;
|
|
|
|
set_cursor(new_row, m_cursor_column);
|
|
|
|
}
|
|
|
|
|
2018-12-07 03:19:02 +03:00
|
|
|
void VirtualConsole::escape$D(const Vector<unsigned>& params)
|
|
|
|
{
|
|
|
|
int num = 1;
|
|
|
|
if (params.size() >= 1)
|
|
|
|
num = params[0];
|
|
|
|
int new_column = (int)m_cursor_column - num;
|
|
|
|
if (new_column < 0)
|
|
|
|
new_column = 0;
|
|
|
|
set_cursor(m_cursor_row, new_column);
|
|
|
|
}
|
|
|
|
|
2018-10-30 15:59:29 +03:00
|
|
|
void VirtualConsole::escape$J(const Vector<unsigned>& params)
|
|
|
|
{
|
|
|
|
int mode = 0;
|
|
|
|
if (params.size() >= 1)
|
|
|
|
mode = params[0];
|
|
|
|
switch (mode) {
|
|
|
|
case 0:
|
|
|
|
// FIXME: Clear from cursor to end of screen.
|
2019-11-06 15:56:11 +03:00
|
|
|
ASSERT_NOT_REACHED();
|
2018-10-30 15:59:29 +03:00
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
// FIXME: Clear from cursor to beginning of screen.
|
2019-11-06 15:56:11 +03:00
|
|
|
ASSERT_NOT_REACHED();
|
2018-10-30 15:59:29 +03:00
|
|
|
break;
|
|
|
|
case 2:
|
2018-11-10 02:56:10 +03:00
|
|
|
clear();
|
2018-10-30 15:59:29 +03:00
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
// FIXME: <esc>[3J should also clear the scrollback buffer.
|
2018-11-10 02:56:10 +03:00
|
|
|
clear();
|
2018-10-30 15:59:29 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-03 22:17:35 +03:00
|
|
|
void VirtualConsole::execute_escape_sequence(u8 final)
|
2018-10-30 15:59:29 +03:00
|
|
|
{
|
2019-04-20 15:13:40 +03:00
|
|
|
auto paramparts = String::copy(m_parameters).split(';');
|
2018-10-30 15:59:29 +03:00
|
|
|
Vector<unsigned> params;
|
|
|
|
for (auto& parampart : paramparts) {
|
|
|
|
bool ok;
|
2019-05-08 20:21:51 +03:00
|
|
|
unsigned value = parampart.to_uint(ok);
|
2018-10-30 15:59:29 +03:00
|
|
|
if (!ok) {
|
|
|
|
// FIXME: Should we do something else?
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
params.append(value);
|
|
|
|
}
|
|
|
|
switch (final) {
|
2019-06-07 12:43:58 +03:00
|
|
|
case 'A':
|
|
|
|
escape$A(params);
|
|
|
|
break;
|
|
|
|
case 'D':
|
|
|
|
escape$D(params);
|
|
|
|
break;
|
|
|
|
case 'H':
|
|
|
|
escape$H(params);
|
|
|
|
break;
|
|
|
|
case 'J':
|
|
|
|
escape$J(params);
|
|
|
|
break;
|
|
|
|
case 'm':
|
|
|
|
escape$m(params);
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
escape$s(params);
|
|
|
|
break;
|
|
|
|
case 'u':
|
|
|
|
escape$u(params);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2018-10-30 15:59:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
m_parameters.clear();
|
|
|
|
m_intermediates.clear();
|
|
|
|
}
|
|
|
|
|
2019-07-03 22:17:35 +03:00
|
|
|
void VirtualConsole::clear_vga_row(u16 row)
|
2018-11-09 23:18:03 +03:00
|
|
|
{
|
2019-07-03 22:17:35 +03:00
|
|
|
u16* linemem = (u16*)&m_current_vga_window[row * 160];
|
|
|
|
for (u16 i = 0; i < columns(); ++i)
|
2018-11-09 23:18:03 +03:00
|
|
|
linemem[i] = 0x0720;
|
|
|
|
}
|
|
|
|
|
2018-11-01 16:09:21 +03:00
|
|
|
void VirtualConsole::scroll_up()
|
2018-10-30 15:59:29 +03:00
|
|
|
{
|
2018-11-29 05:45:23 +03:00
|
|
|
if (m_cursor_row == (rows() - 1)) {
|
2018-11-09 23:18:03 +03:00
|
|
|
if (m_active) {
|
|
|
|
if (m_vga_start_row >= 160) {
|
2018-11-29 05:45:23 +03:00
|
|
|
memcpy(s_vga_buffer, m_current_vga_window + 160, (rows() - 1) * columns() * 2);
|
2018-11-09 23:18:03 +03:00
|
|
|
set_vga_start_row(0);
|
|
|
|
clear_vga_row(24);
|
|
|
|
} else {
|
|
|
|
set_vga_start_row(m_vga_start_row + 1);
|
|
|
|
clear_vga_row(24);
|
|
|
|
}
|
|
|
|
} else {
|
2019-12-20 21:41:31 +03:00
|
|
|
memmove(m_buffer, m_buffer + 160, 160 * 24);
|
2019-07-03 22:17:35 +03:00
|
|
|
u16* linemem = (u16*)&m_buffer[24 * 160];
|
|
|
|
for (u16 i = 0; i < columns(); ++i)
|
2018-11-09 23:18:03 +03:00
|
|
|
linemem[i] = 0x0720;
|
|
|
|
}
|
2018-10-30 17:33:37 +03:00
|
|
|
} else {
|
2018-11-01 16:09:21 +03:00
|
|
|
++m_cursor_row;
|
2018-10-30 17:33:37 +03:00
|
|
|
}
|
2018-11-01 16:09:21 +03:00
|
|
|
m_cursor_column = 0;
|
2018-10-30 17:33:37 +03:00
|
|
|
}
|
|
|
|
|
2018-11-01 16:09:21 +03:00
|
|
|
void VirtualConsole::set_cursor(unsigned row, unsigned column)
|
2018-10-30 17:33:37 +03:00
|
|
|
{
|
2018-11-29 05:45:23 +03:00
|
|
|
ASSERT(row < rows());
|
|
|
|
ASSERT(column < columns());
|
2018-11-01 16:09:21 +03:00
|
|
|
m_cursor_row = row;
|
|
|
|
m_cursor_column = column;
|
2018-10-30 17:33:37 +03:00
|
|
|
if (m_active)
|
2018-11-10 18:25:59 +03:00
|
|
|
flush_vga_cursor();
|
2018-10-30 17:33:37 +03:00
|
|
|
}
|
|
|
|
|
2019-07-03 22:17:35 +03:00
|
|
|
void VirtualConsole::put_character_at(unsigned row, unsigned column, u8 ch)
|
2018-10-30 17:33:37 +03:00
|
|
|
{
|
2018-11-29 05:45:23 +03:00
|
|
|
ASSERT(row < rows());
|
|
|
|
ASSERT(column < columns());
|
2019-07-03 22:17:35 +03:00
|
|
|
u16 cur = (row * 160) + (column * 2);
|
2018-11-09 23:18:03 +03:00
|
|
|
if (m_active) {
|
2019-07-03 22:17:35 +03:00
|
|
|
u16 cur = (row * 160) + (column * 2);
|
2018-11-09 23:18:03 +03:00
|
|
|
m_current_vga_window[cur] = ch;
|
|
|
|
m_current_vga_window[cur + 1] = m_current_attribute;
|
|
|
|
} else {
|
|
|
|
m_buffer[cur] = ch;
|
|
|
|
m_buffer[cur + 1] = m_current_attribute;
|
|
|
|
}
|
2018-10-30 17:33:37 +03:00
|
|
|
}
|
|
|
|
|
2019-07-03 22:17:35 +03:00
|
|
|
void VirtualConsole::on_char(u8 ch)
|
2018-10-30 17:33:37 +03:00
|
|
|
{
|
2019-08-18 05:04:09 +03:00
|
|
|
// ignore writes in graphical mode
|
|
|
|
if (m_graphical)
|
|
|
|
return;
|
|
|
|
|
2018-11-01 16:09:21 +03:00
|
|
|
switch (m_escape_state) {
|
2018-10-30 15:59:29 +03:00
|
|
|
case ExpectBracket:
|
|
|
|
if (ch == '[')
|
2018-11-01 16:09:21 +03:00
|
|
|
m_escape_state = ExpectParameter;
|
2018-10-30 15:59:29 +03:00
|
|
|
else
|
2018-11-01 16:09:21 +03:00
|
|
|
m_escape_state = Normal;
|
2018-10-30 15:59:29 +03:00
|
|
|
return;
|
|
|
|
case ExpectParameter:
|
2018-11-01 16:09:21 +03:00
|
|
|
if (is_valid_parameter_character(ch)) {
|
2018-10-30 15:59:29 +03:00
|
|
|
m_parameters.append(ch);
|
|
|
|
return;
|
|
|
|
}
|
2018-11-01 16:09:21 +03:00
|
|
|
m_escape_state = ExpectIntermediate;
|
2019-02-15 14:39:16 +03:00
|
|
|
[[fallthrough]];
|
2018-10-30 15:59:29 +03:00
|
|
|
case ExpectIntermediate:
|
2018-11-01 16:09:21 +03:00
|
|
|
if (is_valid_intermediate_character(ch)) {
|
2018-10-30 15:59:29 +03:00
|
|
|
m_intermediates.append(ch);
|
|
|
|
return;
|
|
|
|
}
|
2018-11-01 16:09:21 +03:00
|
|
|
m_escape_state = ExpectFinal;
|
2019-02-15 14:39:16 +03:00
|
|
|
[[fallthrough]];
|
2018-10-30 15:59:29 +03:00
|
|
|
case ExpectFinal:
|
2018-11-01 16:09:21 +03:00
|
|
|
if (is_valid_final_character(ch)) {
|
|
|
|
m_escape_state = Normal;
|
|
|
|
execute_escape_sequence(ch);
|
2018-10-30 15:59:29 +03:00
|
|
|
return;
|
|
|
|
}
|
2018-11-01 16:09:21 +03:00
|
|
|
m_escape_state = Normal;
|
2018-10-30 15:59:29 +03:00
|
|
|
return;
|
|
|
|
case Normal:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (ch) {
|
|
|
|
case '\0':
|
|
|
|
return;
|
|
|
|
case '\033':
|
2018-11-01 16:09:21 +03:00
|
|
|
m_escape_state = ExpectBracket;
|
2018-10-30 15:59:29 +03:00
|
|
|
return;
|
|
|
|
case 8: // Backspace
|
2018-11-01 16:09:21 +03:00
|
|
|
if (m_cursor_column) {
|
|
|
|
set_cursor(m_cursor_row, m_cursor_column - 1);
|
|
|
|
put_character_at(m_cursor_row, m_cursor_column, ' ');
|
2018-10-30 15:59:29 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
break;
|
2018-12-07 02:21:25 +03:00
|
|
|
case '\a':
|
|
|
|
// FIXME: Bell!
|
|
|
|
return;
|
2018-12-07 02:17:23 +03:00
|
|
|
case '\t': {
|
|
|
|
for (unsigned i = m_cursor_column; i < columns(); ++i) {
|
|
|
|
if (m_horizontal_tabs[i]) {
|
|
|
|
set_cursor(m_cursor_row, i);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2018-10-30 15:59:29 +03:00
|
|
|
case '\n':
|
2018-11-01 16:09:21 +03:00
|
|
|
scroll_up();
|
|
|
|
set_cursor(m_cursor_row, m_cursor_column);
|
2018-10-30 15:59:29 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-11-01 16:09:21 +03:00
|
|
|
put_character_at(m_cursor_row, m_cursor_column, ch);
|
2018-10-30 15:59:29 +03:00
|
|
|
|
2018-11-01 16:09:21 +03:00
|
|
|
++m_cursor_column;
|
2018-11-29 05:45:23 +03:00
|
|
|
if (m_cursor_column >= columns())
|
2018-11-01 16:09:21 +03:00
|
|
|
scroll_up();
|
|
|
|
set_cursor(m_cursor_row, m_cursor_column);
|
2018-10-30 15:59:29 +03:00
|
|
|
}
|
|
|
|
|
2019-02-17 10:39:09 +03:00
|
|
|
void VirtualConsole::on_key_pressed(KeyboardDevice::Event key)
|
2018-10-30 15:59:29 +03:00
|
|
|
{
|
2019-08-18 05:04:09 +03:00
|
|
|
// ignore keyboard in graphical mode
|
|
|
|
if (m_graphical)
|
|
|
|
return;
|
|
|
|
|
2019-08-12 14:31:43 +03:00
|
|
|
if (!key.is_press())
|
|
|
|
return;
|
2018-11-16 22:18:58 +03:00
|
|
|
if (key.ctrl()) {
|
|
|
|
if (key.character >= 'a' && key.character <= 'z') {
|
|
|
|
emit(key.character - 'a' + 1);
|
|
|
|
return;
|
|
|
|
} else if (key.character == '\\') {
|
|
|
|
emit(0x1c);
|
|
|
|
return;
|
|
|
|
}
|
2018-11-02 16:06:48 +03:00
|
|
|
}
|
|
|
|
emit(key.character);
|
2018-10-30 15:59:29 +03:00
|
|
|
}
|
|
|
|
|
2019-07-03 22:17:35 +03:00
|
|
|
void VirtualConsole::on_sysconsole_receive(u8 ch)
|
2018-10-30 15:59:29 +03:00
|
|
|
{
|
2018-11-08 03:23:23 +03:00
|
|
|
InterruptDisabler disabler;
|
2018-11-01 16:09:21 +03:00
|
|
|
auto old_attribute = m_current_attribute;
|
|
|
|
m_current_attribute = 0x03;
|
2018-11-16 22:18:58 +03:00
|
|
|
on_char(ch);
|
2018-11-01 16:09:21 +03:00
|
|
|
m_current_attribute = old_attribute;
|
2018-10-30 17:33:37 +03:00
|
|
|
}
|
|
|
|
|
2019-07-03 22:17:35 +03:00
|
|
|
ssize_t VirtualConsole::on_tty_write(const u8* data, ssize_t size)
|
2018-10-30 17:33:37 +03:00
|
|
|
{
|
2018-11-08 03:23:23 +03:00
|
|
|
InterruptDisabler disabler;
|
2019-02-25 23:19:57 +03:00
|
|
|
for (ssize_t i = 0; i < size; ++i)
|
2018-11-16 22:18:58 +03:00
|
|
|
on_char(data[i]);
|
2019-02-05 15:09:01 +03:00
|
|
|
return size;
|
2018-10-30 17:33:37 +03:00
|
|
|
}
|
|
|
|
|
2019-10-18 15:13:43 +03:00
|
|
|
StringView VirtualConsole::tty_name() const
|
2018-10-30 17:33:37 +03:00
|
|
|
{
|
2019-04-16 01:35:02 +03:00
|
|
|
return m_tty_name;
|
2018-10-30 15:59:29 +03:00
|
|
|
}
|
2018-11-09 23:18:03 +03:00
|
|
|
|
2019-10-30 15:11:41 +03:00
|
|
|
void VirtualConsole::echo(u8 ch)
|
|
|
|
{
|
|
|
|
if (should_echo_input()) {
|
|
|
|
on_tty_write(&ch, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-03 22:17:35 +03:00
|
|
|
void VirtualConsole::set_vga_start_row(u16 row)
|
2018-11-09 23:18:03 +03:00
|
|
|
{
|
|
|
|
m_vga_start_row = row;
|
2018-11-29 05:45:23 +03:00
|
|
|
m_current_vga_start_address = row * columns();
|
2018-11-09 23:18:03 +03:00
|
|
|
m_current_vga_window = s_vga_buffer + row * 160;
|
2018-11-10 18:25:59 +03:00
|
|
|
IO::out8(0x3d4, 0x0c);
|
|
|
|
IO::out8(0x3d5, MSB(m_current_vga_start_address));
|
|
|
|
IO::out8(0x3d4, 0x0d);
|
|
|
|
IO::out8(0x3d5, LSB(m_current_vga_start_address));
|
2018-11-09 23:18:03 +03:00
|
|
|
}
|