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 *
|
|
|
|
* *
|
2018-01-14 21:21:08 +01:00
|
|
|
* Copyright 2016-2018 Markus Gans *
|
2017-11-04 07:03:53 +01:00
|
|
|
* *
|
|
|
|
* The 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 *
|
|
|
|
* the License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* The Final Cut is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* 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>
|
|
|
|
|
2017-10-31 00:41:59 +01:00
|
|
|
#include <final/final.h>
|
2016-11-26 18:15:31 +01:00
|
|
|
|
2016-11-26 15:18:44 +01:00
|
|
|
|
2017-12-30 21:27:17 +01:00
|
|
|
// Global FVTerm object
|
2018-09-20 23:59:01 +02:00
|
|
|
static finalcut::FVTerm* terminal;
|
2016-11-26 15:18:44 +01:00
|
|
|
|
2017-12-30 21:27:17 +01:00
|
|
|
// Global FApplication object
|
2018-09-20 23:59:01 +02:00
|
|
|
static finalcut::FApplication* app;
|
2017-12-30 21:27:17 +01:00
|
|
|
|
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
|
|
|
|
struct termios save, t;
|
2016-11-26 18:40:50 +01:00
|
|
|
bool ret;
|
2016-11-26 15:18:44 +01:00
|
|
|
std::cout << "\nPress any key to continue...";
|
|
|
|
fflush(stdout);
|
|
|
|
tcgetattr (STDIN_FILENO, &save);
|
|
|
|
t = save;
|
|
|
|
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
|
2018-10-14 06:25:33 +02:00
|
|
|
int term_width = int(app->getDesktopWidth());
|
|
|
|
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
|
|
|
|
std::string sequence;
|
|
|
|
char* buffer;
|
2018-09-02 22:46:01 +02:00
|
|
|
char from[26], to[26], byte[20];
|
2016-11-26 15:18:44 +01:00
|
|
|
uInt len;
|
2018-07-01 14:48:53 +02:00
|
|
|
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",
|
2018-07-08 14:48:45 +02:00
|
|
|
"CAN", "EM", "SUB", "Esc", "FS", "GS", "RS", "US",
|
|
|
|
"Space"
|
2018-07-01 14:48:53 +02:00
|
|
|
};
|
2016-11-26 15:18:44 +01:00
|
|
|
|
|
|
|
term_boundaries(xold, yold);
|
|
|
|
term_boundaries(xnew, ynew);
|
2018-09-02 22:46:01 +02:00
|
|
|
snprintf (from, sizeof(from), "(%3d;%3d)", xold, yold);
|
|
|
|
snprintf (to, sizeof(to), "(%3d;%3d)", xnew, ynew);
|
2016-11-26 15:18:44 +01:00
|
|
|
std::cout << std::right << std::setw(10) << from
|
|
|
|
<< " -> "
|
|
|
|
<< std::left << std::setw(10) << to
|
|
|
|
<< " ";
|
|
|
|
// get the move string
|
2018-10-29 23:57:35 +01:00
|
|
|
buffer = terminal->moveCursor (xold, yold, xnew, ynew);
|
2016-11-26 15:18:44 +01:00
|
|
|
len = uInt(std::strlen(buffer));
|
|
|
|
|
2017-08-27 09:50:30 +02:00
|
|
|
for (uInt i = 0; i < len; i++)
|
2016-11-26 15:18:44 +01:00
|
|
|
{
|
2018-07-01 14:48:53 +02:00
|
|
|
char ch = buffer[i];
|
|
|
|
|
2018-07-08 14:48:45 +02:00
|
|
|
if ( ch < 0x21 )
|
2018-07-01 14:48:53 +02:00
|
|
|
sequence += ctrl_character[uInt(ch)];
|
|
|
|
else
|
|
|
|
sequence += ch;
|
|
|
|
|
|
|
|
sequence += ' ';
|
2016-11-26 15:18:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
std::cout << std::setw(21) << sequence << " ";
|
|
|
|
|
|
|
|
if ( len <= 1 )
|
|
|
|
snprintf (byte, sizeof(byte), "%d byte ", len);
|
|
|
|
else
|
|
|
|
snprintf (byte, sizeof(byte), "%d bytes", len);
|
|
|
|
|
|
|
|
std::cout << std::right << std::setw(10) << byte << "\r\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// main part
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
int main (int argc, char* argv[])
|
|
|
|
{
|
|
|
|
int xmax, ymax;
|
|
|
|
|
2017-09-19 06:18:03 +02:00
|
|
|
// Create the application object
|
2018-09-20 23:59:01 +02:00
|
|
|
finalcut::FApplication TermApp(argc, argv);
|
2017-09-19 06:18:03 +02:00
|
|
|
|
2017-10-31 00:41:59 +01:00
|
|
|
// Pointer to the global virtual terminal object
|
2018-09-20 23:59:01 +02:00
|
|
|
terminal = static_cast<finalcut::FVTerm*>(&TermApp);
|
2017-12-30 21:27:17 +01:00
|
|
|
app = &TermApp;
|
2017-10-31 00:41:59 +01:00
|
|
|
|
|
|
|
// Get screen dimension
|
2018-10-14 06:25:33 +02:00
|
|
|
xmax = int(TermApp.getDesktopWidth() - 1);
|
|
|
|
ymax = int(TermApp.getDesktopHeight() - 1);
|
|
|
|
finalcut::FString line(std::size_t(xmax) + 1, '-');
|
2016-11-26 15:18:44 +01:00
|
|
|
|
2017-09-19 06:18:03 +02:00
|
|
|
// Place the cursor in the upper left corner
|
2017-10-31 00:41:59 +01:00
|
|
|
TermApp.setTermXY(0,0);
|
2017-09-19 06:18:03 +02:00
|
|
|
// Reset all terminal attributes
|
2017-10-31 00:41:59 +01:00
|
|
|
TermApp.setNormal();
|
2017-09-19 06:18:03 +02:00
|
|
|
// Clear the screen
|
2017-10-31 00:41:59 +01:00
|
|
|
TermApp.clearArea();
|
2016-11-26 15:18:44 +01:00
|
|
|
|
2017-09-19 06:18:03 +02:00
|
|
|
// Show the determined terminal name and text resolution
|
2018-10-29 23:57:35 +01:00
|
|
|
std::cout << "Terminal: " << TermApp.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";
|
|
|
|
|
2017-09-19 06:18:03 +02:00
|
|
|
// 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);
|
2017-08-27 09:50:30 +02:00
|
|
|
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);
|
2017-08-27 09:50:30 +02:00
|
|
|
move (5, 5, xmax - 5, ymax - 5);
|
2016-11-26 15:18:44 +01:00
|
|
|
|
2017-09-19 06:18:03 +02:00
|
|
|
// Waiting for keypress
|
2016-11-26 15:18:44 +01:00
|
|
|
keyPressed();
|
|
|
|
|
2017-09-19 06:18:03 +02:00
|
|
|
// Show terminal speed and milliseconds for all cursor movement sequence
|
2016-11-26 15:18:44 +01:00
|
|
|
std::cout << "\r" << line;
|
2018-10-29 23:57:35 +01:00
|
|
|
TermApp.printMoveDurations();
|
2016-11-26 15:18:44 +01:00
|
|
|
|
2017-09-19 06:18:03 +02:00
|
|
|
// Waiting for keypress
|
2016-11-26 15:18:44 +01:00
|
|
|
keyPressed();
|
2018-01-14 21:21:08 +01:00
|
|
|
app = 0; // End of TermApp object scope
|
2016-11-26 15:18:44 +01:00
|
|
|
}
|