GaspardTerminal/src/main.cpp¶
GaspardTerminal source module. Keep its repository-relative directory layout alongside the shared bindings.
Source files · Download main.cpp
#include "console.hpp"
#include "native.hpp"
#include "../../bindings/cpp/desktop_gui.hpp"
#include <cstdio>
#include <cstring>
#include <exception>
namespace
{
class Terminal
{
gaspardos::Window window_;
gaspardos::gui::NativePainter painter_;
gaspardos::desktop::PaintQueue paint_;
gaspardos::desktop::TextInput input_;
terminal::Console console_;
terminal::NativeBackend backend_;
terminal::Engine engine_;
std::string draft_, lastDirectory_;
int history_;
unsigned scroll_, revision_, children_;
bool dirty_, lastBusy_;
Terminal(const Terminal&);
Terminal& operator=(const Terminal&);
void loadEnvironment()
{
gaspardos::NativeFile file;
int64_t result = -11;
unsigned attempts = 0;
while (result == -11 && attempts++ < 4096)
{
result = file.open(L"\\AHCIDISK0\\Vars.com", GO64_RDONLY);
if (result == -11) GO64_yield();
}
if (result == -2) return; // Optional on a fresh installation.
if (result < 0)
{
console_.print("Cannot open /AHCIDISK0/Vars.com\n");
return;
}
std::string contents;
char bytes[1024];
attempts = 0;
do
{
result = file.read(bytes, sizeof(bytes));
if (result > 0) contents.append(bytes, static_cast<std::size_t>(result));
if (result == -11) GO64_yield();
} while ((result > 0 || result == -11) && contents.size() <= 65536
&& ++attempts < 4096);
int64_t closed = -11;
for (unsigned i = 0; closed == -11 && i < 4096; ++i)
{
closed = file.close();
if (closed == -11) GO64_yield();
}
std::string error;
terminal::EnvironmentFile parser;
if (result != 0 || closed != 0)
console_.print("Cannot read /AHCIDISK0/Vars.com (I/O or size limit)\n");
else if (!parser.parse(engine_, contents, error))
console_.print(error + "\n");
}
void history(bool previous)
{
const std::vector<terminal::HistoryEntry>& entries = engine_.history();
if (previous)
{
if (history_ < 0)
{
draft_ = input_.value;
history_ = static_cast<int>(entries.size());
}
if (history_ > 0)
--history_;
}
else if (history_ >= 0)
{
if (++history_ >= static_cast<int>(entries.size()))
{
history_ = -1;
input_.set(draft_);
dirty_ = true;
return;
}
}
if (history_ >= 0 && history_ < static_cast<int>(entries.size()))
input_.set(entries[history_].line);
dirty_ = true;
}
// Fixed 80 x 24 character grid, with the prompt in the output stream.
void snapshot()
{
const unsigned rows = 24;
paint_.clear();
dirty_ = false;
paint_.fill(0, 0, 644, 388, 0x00ffffff);
const std::deque<std::string>& lines = console_.lines();
std::vector<std::string> wrapped(1, "");
unsigned column = 0;
for (std::size_t i = 0; i < lines.size(); ++i)
{
if (i) { wrapped.push_back(""); column = 0; }
appendCells(wrapped, column, lines[i]);
}
const bool prompt = !engine_.busy() && !backend_.busy();
unsigned cursorRow = 0, cursorColumn = 0;
std::string cursorGlyph = " ";
if (prompt)
{
appendCells(wrapped, column, "$ ");
appendCells(wrapped, column, input_.value.substr(0, input_.cursor));
cursorRow = static_cast<unsigned>(wrapped.size() - 1);
cursorColumn = column;
if (input_.cursor < input_.value.size())
{
std::size_t end = input_.cursor + 1;
while (end < input_.value.size()
&& (static_cast<unsigned char>(input_.value[end]) & 0xc0) == 0x80)
++end;
cursorGlyph = input_.value.substr(input_.cursor, end - input_.cursor);
}
appendCells(wrapped, column, input_.value.substr(input_.cursor));
}
unsigned maximum = wrapped.size() > rows
? static_cast<unsigned>(wrapped.size() - rows) : 0;
if (scroll_ > maximum) scroll_ = maximum;
unsigned first = maximum - scroll_;
// Keep an insertion point visible even inside a multi-line command.
if (prompt && !scroll_ && cursorRow < first) first = cursorRow;
for (unsigned row = 0; row < rows && first + row < wrapped.size(); ++row)
{
const std::string& text = wrapped[first + row];
unsigned x = 2;
for (std::size_t pos = 0; pos < text.size();)
{
std::size_t end = pos + 1;
while (end < text.size()
&& (static_cast<unsigned char>(text[end]) & 0xc0) == 0x80) ++end;
// Place every glyph on its cell: the native GUI font has
// proportional advances and must not determine the grid.
paint_.text(x, 2 + row * 16, text.substr(pos, end - pos), 0x00000000);
x += 8;
pos = end;
}
}
if (prompt && !scroll_ && cursorRow >= first && cursorRow < first + rows)
{
const int x = 2 + cursorColumn * 8, y = 2 + (cursorRow - first) * 16;
paint_.fill(x, y, 8, 16, 0x00000000);
paint_.text(x, y, cursorGlyph, 0x00ffffff);
}
}
static void appendCells(std::vector<std::string>& lines, unsigned& column,
const std::string& text)
{
for (std::size_t pos = 0; pos < text.size();)
{
std::size_t end = pos + 1;
while (end < text.size()
&& (static_cast<unsigned char>(text[end]) & 0xc0) == 0x80) ++end;
lines.back().append(text, pos, end - pos);
if (++column == 80) { lines.push_back(""); column = 0; }
pos = end;
}
}
bool event(const GO64_Event& e)
{
if (e.type == 15 || e.type == GO64_ERROR)
return false;
if (e.type == GO64_READY || e.type == 1 || e.type == GO64_OVERFLOW)
{
paint_.clear();
dirty_ = true;
}
if (e.type == 8)
{
if (e.key == 13)
{
if (engine_.busy() || backend_.busy()) return true;
console_.print("$ " + input_.value + "\n");
engine_.submit(input_.value);
input_.set("");
history_ = -1;
scroll_ = 0;
}
else if (e.key == 0xF802)
history(true);
else if (e.key == 0xF803)
history(false);
else if (e.key == 0xF808)
scroll_ += 24;
else if (e.key == 0xF809)
scroll_ = scroll_ > 24 ? scroll_ - 24 : 0;
else if (e.key == 27)
{
input_.set("");
history_ = -1;
}
else if (e.key == 9)
{
for (unsigned i = 0; i < 4; ++i)
input_.key(' ');
}
else
input_.key(e.key);
if (e.key != 0xF808 && e.key != 0xF809) scroll_ = 0;
dirty_ = true;
}
return true;
}
public:
Terminal()
: painter_(window_)
, backend_(console_)
, engine_(backend_, console_)
, history_(-1)
, scroll_(0)
, revision_(0)
, children_(0)
, dirty_(true)
, lastBusy_(false)
{
terminal::registerDefaultCommands(engine_);
loadEnvironment();
}
int run()
{
GO64_WindowOptions options;
std::memset(&options, 0, sizeof(options));
options.width = 644;
options.height = 388;
options.background = 0x00ffffff;
std::strcpy(options.title, "xterm");
if (window_.create(options) < 0)
return 1;
#ifdef GASPARD_TERMINAL_SMOKE_TEST
bool submitted = false, reported = false;
#endif
while (!engine_.exiting())
{
GO64_Event e;
int64_t result = window_.event(e);
if (result == 0)
{
if (!event(e))
break;
}
else if (result != -11)
break;
#ifdef GASPARD_TERMINAL_SMOKE_TEST
if (window_.ready() && !submitted)
{
const char batch[] =
"set APP /AHCIDISK0/apps/argv.elf; cd /AHCIDISK0/apps; echo $PWD; repeat 2 "
"'echo language ready'; run $APP one 'two words'";
console_.print(std::string("> ") + batch + "\n");
if (!engine_.submit(batch))
return 4;
submitted = true;
}
#endif
const bool wasBusy = engine_.busy() || backend_.busy();
engine_.tick();
backend_.tick();
if (wasBusy != (engine_.busy() || backend_.busy())) dirty_ = true;
if (revision_ != console_.revision() || children_ != backend_.children()
|| lastBusy_ != backend_.busy() || lastDirectory_ != engine_.cwd())
{
revision_ = console_.revision();
children_ = backend_.children();
lastBusy_ = backend_.busy();
lastDirectory_ = engine_.cwd();
dirty_ = true;
}
if (window_.ready())
{
if (paint_.finished() && dirty_)
snapshot();
result = paint_.paint(painter_);
if (result < 0 && result != -11)
break;
}
#ifdef GASPARD_TERMINAL_SMOKE_TEST
if (submitted && !reported && !engine_.busy() && !backend_.busy()
&& !backend_.children() && paint_.finished() && !dirty_)
{
bool child = false;
for (std::size_t i = 0; i < console_.lines().size(); ++i)
if (console_.lines()[i].find("CHILD ARGV OK") != std::string::npos)
child = true;
if (child)
{
const char passed[] = "GASPARDTERMINAL REAL RUN: PASS\n";
GO64_console_write(passed, sizeof(passed) - 1);
reported = true;
}
}
#endif
GO64_yield();
}
backend_.cancel();
for (unsigned i = 0; i < 1000 && backend_.busy(); ++i)
{
backend_.tick();
GO64_yield();
}
for (unsigned i = 0; i < 1000; ++i)
{
int64_t result = window_.close();
if (!result)
return 0;
if (result != -11)
break;
GO64_yield();
}
return 2;
}
};
} // namespace
int main()
{
try
{
Terminal terminal;
return terminal.run();
}
catch (const std::exception& e)
{
GO64_console_write(e.what(), std::strlen(e.what()));
return 3;
}
}