2019-01-07 00:10:35 +08:00
|
|
|
#include "ftxui/component/screen_interactive.hpp"
|
2019-01-05 09:03:49 +08:00
|
|
|
|
2018-10-10 01:06:03 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <termios.h>
|
|
|
|
#include <unistd.h>
|
2019-01-07 00:10:35 +08:00
|
|
|
#include <iostream>
|
|
|
|
#include "ftxui/component/component.hpp"
|
2019-01-12 22:00:08 +08:00
|
|
|
#include "ftxui/screen/terminal.hpp"
|
2019-01-27 09:33:06 +08:00
|
|
|
#include <thread>
|
2018-10-10 01:06:03 +08:00
|
|
|
|
2019-01-12 22:00:08 +08:00
|
|
|
namespace ftxui {
|
2018-10-10 01:06:03 +08:00
|
|
|
|
2018-10-19 04:58:38 +08:00
|
|
|
namespace {
|
2019-01-07 00:10:35 +08:00
|
|
|
constexpr int ESC = 27;
|
|
|
|
constexpr int WAT = 195;
|
|
|
|
constexpr int WAT2 = 194;
|
|
|
|
constexpr int WATWAIT = 91;
|
|
|
|
|
|
|
|
Event GetEvent() {
|
|
|
|
int v1 = getchar();
|
|
|
|
if (v1 == ESC) {
|
|
|
|
int v2 = getchar();
|
|
|
|
int v3 = getchar();
|
|
|
|
|
|
|
|
// if (v2 == WATWAIT) {
|
|
|
|
// int v4 = getchar();
|
|
|
|
// int v5 = getchar();
|
|
|
|
// return Event{v1, v2, v3, v4, v5};
|
|
|
|
//}
|
|
|
|
return Event{v1, v2, v3};
|
|
|
|
}
|
2018-10-21 20:18:11 +08:00
|
|
|
|
2019-01-07 00:10:35 +08:00
|
|
|
if (v1 == WAT) {
|
|
|
|
int v2 = getchar();
|
|
|
|
return Event{v1, v2};
|
|
|
|
}
|
2018-10-21 20:18:11 +08:00
|
|
|
|
2019-01-07 00:10:35 +08:00
|
|
|
if (v1 == WAT2) {
|
|
|
|
int v2 = getchar();
|
|
|
|
return Event{v1, v2};
|
|
|
|
}
|
2019-01-03 05:33:59 +08:00
|
|
|
|
2019-01-07 00:10:35 +08:00
|
|
|
return Event{v1};
|
2018-10-19 04:58:38 +08:00
|
|
|
};
|
2019-01-12 22:00:08 +08:00
|
|
|
|
2019-01-07 00:10:35 +08:00
|
|
|
}; // namespace
|
2018-10-19 04:58:38 +08:00
|
|
|
|
2019-01-27 04:52:55 +08:00
|
|
|
ScreenInteractive::ScreenInteractive(int dimx,
|
|
|
|
int dimy,
|
2019-01-05 09:03:49 +08:00
|
|
|
Dimension dimension)
|
2019-01-13 01:24:46 +08:00
|
|
|
: Screen(dimx, dimy), dimension_(dimension) {}
|
2018-10-10 01:06:03 +08:00
|
|
|
ScreenInteractive::~ScreenInteractive() {}
|
|
|
|
|
2019-01-05 09:03:49 +08:00
|
|
|
// static
|
2019-01-27 04:52:55 +08:00
|
|
|
ScreenInteractive ScreenInteractive::FixedSize(int dimx, int dimy) {
|
2019-01-05 09:03:49 +08:00
|
|
|
return ScreenInteractive(dimx, dimy, Dimension::Fixed);
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
ScreenInteractive ScreenInteractive::Fullscreen() {
|
|
|
|
return ScreenInteractive(0, 0, Dimension::Fullscreen);
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
ScreenInteractive ScreenInteractive::TerminalOutput() {
|
|
|
|
return ScreenInteractive(0, 0, Dimension::TerminalOutput);
|
|
|
|
}
|
|
|
|
|
2019-01-19 07:20:29 +08:00
|
|
|
// static
|
|
|
|
ScreenInteractive ScreenInteractive::FitComponent() {
|
|
|
|
return ScreenInteractive(0, 0, Dimension::FitComponent);
|
|
|
|
}
|
|
|
|
|
2019-01-27 09:33:06 +08:00
|
|
|
void ScreenInteractive::PostEvent(Event event) {
|
|
|
|
std::unique_lock<std::mutex> lock(events_queue_mutex);
|
|
|
|
events_queue.push(event);
|
|
|
|
events_queue_wait.notify_one();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScreenInteractive::EventLoop(Component* component) {
|
|
|
|
bool handled = 0;
|
|
|
|
for (;;) {
|
|
|
|
std::unique_lock<std::mutex> lock(events_queue_mutex);
|
|
|
|
while (!events_queue.empty()) {
|
|
|
|
component->OnEvent(events_queue.front());
|
|
|
|
events_queue.pop();
|
|
|
|
handled = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (handled)
|
|
|
|
return;
|
|
|
|
events_queue_wait.wait(lock);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-13 01:24:46 +08:00
|
|
|
void ScreenInteractive::Loop(Component* component) {
|
|
|
|
//std::cout << "\033[?9h"; [> Send Mouse Row & Column on Button Press <]
|
|
|
|
//std::cout << "\033[?1000h"; [> Send Mouse X & Y on button press and release <]
|
|
|
|
//std::cout << std::flush;
|
2018-10-10 01:06:03 +08:00
|
|
|
|
|
|
|
// Save the old terminal configuration.
|
|
|
|
struct termios terminal_configuration_old;
|
|
|
|
tcgetattr(STDIN_FILENO, &terminal_configuration_old);
|
|
|
|
|
|
|
|
// Set the new terminal configuration
|
|
|
|
struct termios terminal_configuration_new;
|
|
|
|
terminal_configuration_new = terminal_configuration_old;
|
|
|
|
|
|
|
|
// Non canonique terminal.
|
|
|
|
terminal_configuration_new.c_lflag &= ~ICANON;
|
|
|
|
// Do not print after a key press.
|
|
|
|
terminal_configuration_new.c_lflag &= ~ECHO;
|
|
|
|
tcsetattr(STDIN_FILENO, TCSANOW, &terminal_configuration_new);
|
|
|
|
|
2019-01-27 09:33:06 +08:00
|
|
|
std::thread read_char([this]() {
|
|
|
|
while (!quit_)
|
|
|
|
PostEvent(GetEvent());
|
|
|
|
});
|
|
|
|
|
2019-01-07 06:24:58 +08:00
|
|
|
std::string reset_position;
|
2019-01-07 00:10:35 +08:00
|
|
|
while (!quit_) {
|
2019-01-13 05:25:49 +08:00
|
|
|
reset_position = ResetPosition();
|
2019-01-13 01:24:46 +08:00
|
|
|
Draw(component);
|
2019-01-07 06:24:58 +08:00
|
|
|
std::cout << reset_position << ToString() << std::flush;
|
2018-10-10 01:06:03 +08:00
|
|
|
Clear();
|
2019-01-27 09:33:06 +08:00
|
|
|
EventLoop(component);
|
2019-01-07 00:10:35 +08:00
|
|
|
}
|
2018-10-10 01:06:03 +08:00
|
|
|
|
|
|
|
// Restore the old terminal configuration.
|
|
|
|
tcsetattr(STDIN_FILENO, TCSANOW, &terminal_configuration_old);
|
2019-01-13 01:24:46 +08:00
|
|
|
|
2019-01-27 09:33:06 +08:00
|
|
|
read_char.join();
|
|
|
|
|
2019-01-13 01:24:46 +08:00
|
|
|
std::cout << std::endl;
|
2018-10-10 01:06:03 +08:00
|
|
|
}
|
|
|
|
|
2019-01-13 01:24:46 +08:00
|
|
|
void ScreenInteractive::Draw(Component* component) {
|
|
|
|
auto document = component->Render();
|
2019-01-27 04:52:55 +08:00
|
|
|
int dimx;
|
|
|
|
int dimy;
|
2019-01-07 00:10:35 +08:00
|
|
|
switch (dimension_) {
|
2019-01-05 09:03:49 +08:00
|
|
|
case Dimension::Fixed:
|
2019-01-13 01:24:46 +08:00
|
|
|
dimx = dimx_;
|
|
|
|
dimy = dimy_;
|
2019-01-05 09:03:49 +08:00
|
|
|
break;
|
|
|
|
case Dimension::TerminalOutput:
|
|
|
|
document->ComputeRequirement();
|
|
|
|
dimx = Terminal::Size().dimx;
|
|
|
|
dimy = document->requirement().min.y;
|
|
|
|
break;
|
|
|
|
case Dimension::Fullscreen:
|
|
|
|
dimx = Terminal::Size().dimx;
|
|
|
|
dimy = Terminal::Size().dimy;
|
|
|
|
break;
|
2019-01-19 07:20:29 +08:00
|
|
|
case Dimension::FitComponent:
|
2019-01-21 06:04:10 +08:00
|
|
|
auto terminal = Terminal::Size();
|
2019-01-19 07:20:29 +08:00
|
|
|
document->ComputeRequirement();
|
2019-01-21 06:04:10 +08:00
|
|
|
dimx = std::min(document->requirement().min.x, terminal.dimx);
|
|
|
|
dimy = std::min(document->requirement().min.y, terminal.dimy);
|
2019-01-19 07:20:29 +08:00
|
|
|
break;
|
2019-01-05 09:03:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (dimx != dimx_ || dimy != dimy_) {
|
|
|
|
dimx_ = dimx;
|
|
|
|
dimy_ = dimy;
|
2019-01-12 22:00:08 +08:00
|
|
|
pixels_ = std::vector<std::vector<Pixel>>(
|
|
|
|
dimy, std::vector<Pixel>(dimx));
|
2019-01-05 09:03:49 +08:00
|
|
|
}
|
|
|
|
|
2018-10-10 01:06:03 +08:00
|
|
|
Render(*this, document.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
std::function<void()> ScreenInteractive::ExitLoopClosure() {
|
|
|
|
return [this]() { quit_ = true; };
|
|
|
|
}
|
|
|
|
|
2019-01-12 22:00:08 +08:00
|
|
|
} // namespace ftxui.
|