finalcut/examples/opti-move.cpp

283 lines
7.9 KiB
C++
Raw Normal View History

2017-11-04 07:03:53 +01:00
/***********************************************************************
* opti-move.cpp - Tests the cursor movement optimization *
* *
* This file is part of the FINAL CUT widget toolkit *
2017-11-04 07:03:53 +01:00
* *
* Copyright 2016-2020 Markus Gans *
2017-11-04 07:03:53 +01:00
* *
* FINAL CUT is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
* published by the Free Software Foundation; either version 3 of *
2017-11-04 07:03:53 +01:00
* the License, or (at your option) any later version. *
* *
* FINAL CUT is distributed in the hope that it will be useful, but *
* WITHOUT ANY WARRANTY; without even the implied warranty of *
2017-11-04 07:03:53 +01:00
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with this program. If not, see *
* <http://www.gnu.org/licenses/>. *
***********************************************************************/
2016-11-26 15:18:44 +01:00
#include <iomanip>
2017-09-11 03:06:02 +02:00
#include <string>
#include <final/final.h>
2016-11-26 18:15:31 +01:00
2016-11-26 15:18:44 +01:00
// Global FApplication object
2019-08-25 22:16:00 +02:00
static finalcut::FApplication* app{nullptr};
2016-11-26 15:18:44 +01:00
// function prototype
2016-11-26 18:40:50 +01:00
bool keyPressed();
2016-11-26 15:18:44 +01:00
void term_boundaries (int&, int&);
void move (int, int, int, int);
//----------------------------------------------------------------------
// functions
//----------------------------------------------------------------------
2016-11-26 18:40:50 +01:00
bool keyPressed()
2016-11-26 15:18:44 +01:00
{
// Waiting for keypress
2019-08-25 22:16:00 +02:00
struct termios save{};
bool ret{false};
2016-11-26 15:18:44 +01:00
std::cout << "\nPress any key to continue...";
fflush(stdout);
tcgetattr (STDIN_FILENO, &save);
2019-08-25 22:16:00 +02:00
struct termios t = save;
2016-11-26 15:18:44 +01:00
t.c_lflag &= uInt(~(ICANON | ECHO));
tcsetattr (STDIN_FILENO, TCSANOW, &t);
2016-11-26 18:40:50 +01:00
if ( std::getchar() != EOF )
ret = true;
else
ret = false;
2016-11-27 19:16:52 +01:00
2016-11-26 15:18:44 +01:00
tcsetattr (STDIN_FILENO, TCSADRAIN, &save);
2016-11-26 18:40:50 +01:00
return ret;
2016-11-26 15:18:44 +01:00
}
//----------------------------------------------------------------------
void term_boundaries (int& x, int& y)
{
// checks and corrects the terminal boundaries
const int term_width = int(app->getDesktopWidth());
const int term_height = int(app->getDesktopHeight());
2016-11-26 15:18:44 +01:00
if ( x < 0 )
x = 0;
if ( y < 0 )
y = 0;
2018-10-24 08:51:38 +02:00
if ( x >= term_width && term_width > 0 )
2016-11-26 15:18:44 +01:00
{
y += x / term_width;
x %= term_width;
}
if ( y >= term_height )
y = term_height - 1;
}
//----------------------------------------------------------------------
void move (int xold, int yold, int xnew, int ynew)
{
// Prints the cursor move escape sequence
2020-04-13 12:40:11 +02:00
finalcut::FString buffer{};
finalcut::FString sequence{};
finalcut::FString from{};
finalcut::FString to{};
finalcut::FString byte{};
const std::string ctrl_character[] =
{
"NUL", "SOH", "STX", "ETX", "EOT", "ENQ", "ACK", "BEL",
"BS", "Tab", "LF", "VT", "FF", "CR", "SO", "SI",
"DLE", "DC1", "DC2", "DC3", "DC4", "NAK", "SYN", "ETB",
"CAN", "EM", "SUB", "Esc", "FS", "GS", "RS", "US",
"Space"
};
2016-11-26 15:18:44 +01:00
term_boundaries(xold, yold);
term_boundaries(xnew, ynew);
2019-09-29 22:28:58 +02:00
// Get the move string
2019-09-29 22:28:58 +02:00
buffer = finalcut::FTerm::moveCursorString (xold, yold, xnew, ynew);
2016-11-26 15:18:44 +01:00
2019-09-29 22:28:58 +02:00
for (auto&& ch : buffer)
2016-11-26 15:18:44 +01:00
{
if ( ch < 0x21 )
2019-09-29 22:28:58 +02:00
sequence += ctrl_character[std::size_t(ch)];
else
sequence += ch;
sequence += ' ';
2016-11-26 15:18:44 +01:00
}
2019-09-29 22:28:58 +02:00
from.sprintf ("(%3d;%3d)", xold, yold);
to.sprintf ("(%3d;%3d)", xnew, ynew);
const std::size_t len = buffer.getLength();
2016-11-26 15:18:44 +01:00
if ( len <= 1 )
2019-09-29 22:28:58 +02:00
byte.sprintf ("%d byte ", len);
2016-11-26 15:18:44 +01:00
else
2019-09-29 22:28:58 +02:00
byte.sprintf ("%d bytes", len);
2016-11-26 15:18:44 +01:00
2019-09-29 22:28:58 +02:00
std::cout << std::right << std::setw(10) << from << " -> "
<< std::left << std::setw(10) << to << " "
<< std::setw(21) << sequence << " "
<< std::right << std::setw(10) << byte << "\r\n";
2016-11-26 15:18:44 +01:00
}
//----------------------------------------------------------------------
2020-06-06 21:10:06 +02:00
class DirectLogger final : public finalcut::FLog
{
public:
2020-06-06 21:10:06 +02:00
// Constructor
DirectLogger();
// Destructor
~DirectLogger() override;
void info (const std::string& entry) override
{
output << entry << "\r" << std::endl;
}
void warn (const std::string&) override
2020-06-06 21:10:06 +02:00
{
// An implementation is not required in this context
}
void error (const std::string&) override
2020-06-06 21:10:06 +02:00
{
// An implementation is not required in this context
}
void debug (const std::string&) override
2020-06-06 21:10:06 +02:00
{
// An implementation is not required in this context
}
2020-06-07 18:21:59 +02:00
void flush() override
{
output.flush();
}
void setOutputStream (const std::ostream& os) override
{ output.rdbuf(os.rdbuf()); }
void setLineEnding (LineEnding) override
2020-06-06 21:10:06 +02:00
{
// An implementation is not required in this context
}
void enableTimestamp() override
2020-06-06 21:10:06 +02:00
{
// An implementation is not required in this context
}
void disableTimestamp() override
2020-06-06 21:10:06 +02:00
{
// An implementation is not required in this context
}
private:
// Data member
std::ostream output{std::cerr.rdbuf()};
};
2016-11-26 15:18:44 +01:00
2020-06-06 21:10:06 +02:00
//----------------------------------------------------------------------
DirectLogger::DirectLogger() // constructor
{ }
//----------------------------------------------------------------------
DirectLogger::~DirectLogger() // destructor
{ }
2016-11-26 15:18:44 +01:00
//----------------------------------------------------------------------
// main part
//----------------------------------------------------------------------
int main (int argc, char* argv[])
{
2020-06-06 21:10:06 +02:00
// Disable mouse
finalcut::FStartOptions::getFStartOptions().mouse_support = false;
// Create the application object
2020-06-06 21:10:06 +02:00
finalcut::FApplication term_app{argc, argv};
// Force terminal initialization without calling show()
term_app.initTerminal();
if ( finalcut::FApplication::isQuit() )
2020-06-06 21:10:06 +02:00
return 0;
2020-08-12 22:28:02 +02:00
// Pointer to the global virtual terminal object
app = &term_app;
// Get screen dimension
2020-06-06 21:10:06 +02:00
int xmax = int(term_app.getDesktopWidth() - 1);
int ymax = int(term_app.getDesktopHeight() - 1);
finalcut::FString line{std::size_t(xmax) + 1, '-'};
2016-11-26 15:18:44 +01:00
// Place the cursor in the upper left corner
2020-06-06 21:10:06 +02:00
term_app.setTermXY(0, 0);
// Reset all terminal attributes
2020-06-06 21:10:06 +02:00
term_app.setNormal();
// Clear the screen
2020-06-06 21:10:06 +02:00
term_app.clearArea();
2016-11-26 15:18:44 +01:00
// Show the determined terminal name and text resolution
std::cout << "Terminal: " << finalcut::FTerm::getTermType() << "\r\n";
2016-11-26 15:18:44 +01:00
std::cout << " Columns: 0.." << xmax << "\r\n";
std::cout << " Lines: 0.." << ymax << "\r\n";
// Show the escape sequences for the following cursor movements
2016-11-26 15:18:44 +01:00
std::cout << std::setw(38) << "Cursor move\r\n";
std::cout << " (From) -> (To) ";
std::cout << "escape sequence ";
std::cout << "Length\r\n";
std::cout << line;
move (5, 12, 0, 0);
move (5, ymax, 5, 0);
move (xmax, 1, 0, 1);
move (xmax, 1, 0, 2);
move (xmax + 1, 1, 0, 2);
2016-11-26 15:18:44 +01:00
move (9, 4, 10, 4);
move (10, 4, 9, 4);
move (9, 4, 11, 4);
move (11, 4, 9, 4);
move (1, 0, 8, 0);
move (16, 0, 16, 1);
move (16, 1, 16, 0);
move (16, 0, 16, 2);
move (16, 2, 16, 0);
move (3, 2, xmax, 2);
move (5, 5, xmax - 5, ymax - 5);
2016-11-26 15:18:44 +01:00
// Waiting for keypress
2016-11-26 15:18:44 +01:00
keyPressed();
// Show terminal speed and milliseconds for all cursor movement sequence
std::cout << "\r" << line << std::flush;
// Generation of a logger in a shared_ptr via a pointer
finalcut::FApplication::setLog(std::make_shared<DirectLogger>());
2020-04-13 12:40:11 +02:00
const finalcut::FOptiMove& opti_move = *finalcut::FTerm::getFOptiMove();
finalcut::printDurations(opti_move);
2016-11-26 15:18:44 +01:00
// Waiting for keypress
2016-11-26 15:18:44 +01:00
keyPressed();
2020-06-06 21:10:06 +02:00
app = nullptr; // End of term_app object scope
2020-06-06 21:10:06 +02:00
return 0;
2016-11-26 15:18:44 +01:00
}