finalcut/src/fterm.cpp

3282 lines
84 KiB
C++
Raw Normal View History

// File: fterm.cpp
// Provides: class FTerm
2015-05-23 13:35:12 +02:00
#include "fterm.h"
2015-05-23 13:35:12 +02:00
#include "fcharmap.h"
#include "fkey_map.h"
#include "ftcap_map.h"
#include "fonts/newfont.h"
#include "fonts/unicodemap.h"
#include "fonts/vgafont.h"
// global FTerm object
static FTerm* init_term_object = 0;
2015-05-23 13:35:12 +02:00
// global init state
static bool term_initialized = false;
// function pointer
int (*FTerm::Fputchar)(int);
// static class attributes
int FTerm::stdin_no;
int FTerm::stdout_no;
int FTerm::fd_tty;
int FTerm::max_color;
int FTerm::stdin_status_flags;
uInt FTerm::baudrate;
uInt FTerm::tabstop;
uInt FTerm::attr_without_color;
2015-05-23 13:35:12 +02:00
bool FTerm::resize_term;
bool FTerm::mouse_support;
bool FTerm::raw_mode;
bool FTerm::input_data_pending;
2015-05-23 13:35:12 +02:00
bool FTerm::non_blocking_stdin;
bool FTerm::gpm_mouse_enabled;
bool FTerm::color256;
bool FTerm::monochron;
bool FTerm::background_color_erase;
bool FTerm::automatic_left_margin;
bool FTerm::automatic_right_margin;
bool FTerm::eat_nl_glitch;
2016-01-10 00:56:52 +01:00
bool FTerm::ansi_default_color;
2016-10-09 02:06:06 +02:00
bool FTerm::xterm_terminal;
2015-05-23 13:35:12 +02:00
bool FTerm::rxvt_terminal;
bool FTerm::urxvt_terminal;
bool FTerm::mlterm_terminal;
bool FTerm::putty_terminal;
bool FTerm::kde_konsole;
bool FTerm::gnome_terminal;
bool FTerm::kterm_terminal;
bool FTerm::tera_terminal;
bool FTerm::cygwin_terminal;
bool FTerm::mintty_terminal;
bool FTerm::linux_terminal;
bool FTerm::screen_terminal;
bool FTerm::tmux_terminal;
bool FTerm::pc_charset_console;
bool FTerm::utf8_input;
bool FTerm::utf8_state;
bool FTerm::utf8_console;
bool FTerm::utf8_linux_terminal;
bool FTerm::force_vt100;
bool FTerm::vt100_console;
bool FTerm::ascii_console;
bool FTerm::NewFont;
bool FTerm::VGAFont;
bool FTerm::cursor_optimisation;
2016-01-10 00:56:52 +01:00
bool FTerm::osc_support;
2015-05-23 13:35:12 +02:00
uChar FTerm::x11_button_state;
termios FTerm::term_init;
2015-10-03 21:58:41 +02:00
char FTerm::termtype[30] = "";
2015-05-23 13:35:12 +02:00
char* FTerm::term_name = 0;
char* FTerm::locale_name = 0;
char* FTerm::locale_xterm = 0;
FPoint* FTerm::mouse = 0;
FRect* FTerm::term = 0;
char FTerm::exit_message[8192] = "";
2015-05-23 13:35:12 +02:00
fc::encoding FTerm::Encoding;
const FString* FTerm::xterm_font = 0;
const FString* FTerm::xterm_title = 0;
const FString* FTerm::answer_back = 0;
const FString* FTerm::sec_da = 0;
FOptiMove* FTerm::opti_move = 0;
FOptiAttr* FTerm::opti_attr = 0;
FTerm::modifier_key FTerm::mod_key;
2015-05-23 13:35:12 +02:00
std::map<uChar,uChar>* FTerm::vt100_alt_char = 0;
std::map<std::string,fc::encoding>* \
2015-05-23 13:35:12 +02:00
FTerm::encoding_set = 0;
FTermcap::tcap_map* FTermcap::tcap = 0;
FTermcap::tcap_map* FTerm::tcap = term_caps;
console_font_op FTerm::screen_font;
unimapdesc FTerm::screen_unicode_map;
fc::consoleCursorStyle FTerm::console_cursor_style;
2015-05-23 13:35:12 +02:00
//----------------------------------------------------------------------
// class FTerm
//----------------------------------------------------------------------
// constructors and destructor
//----------------------------------------------------------------------
FTerm::FTerm()
2015-09-22 04:18:20 +02:00
: map()
2015-05-23 13:35:12 +02:00
{
if ( ! term_initialized )
{
init();
}
}
//----------------------------------------------------------------------
FTerm::~FTerm() // destructor
{
if ( init_term_object == this )
2015-05-23 13:35:12 +02:00
{
finish();
if ( exit_message[0] )
{
std::fprintf (stderr, "Warning: %s\n", exit_message);
}
2015-05-23 13:35:12 +02:00
}
}
// private methods of FTerm
//----------------------------------------------------------------------
void FTerm::outb_Attribute_Controller (int index, int data)
{
inb (AttrC_DataSwitch);
outb (index & 0x1F, AttrC_Index);
outb (uChar(data), AttrC_DataW);
inb (AttrC_DataSwitch);
2015-09-20 05:44:50 +02:00
outb (uChar((index & 0x1F) | 0x20), AttrC_Index);
2015-05-23 13:35:12 +02:00
outb (uChar(data), AttrC_DataW);
}
//----------------------------------------------------------------------
int FTerm::inb_Attribute_Controller (int index)
{
int res;
inb (AttrC_DataSwitch);
outb (index & 0x1F, AttrC_Index);
res = inb (AttrC_DataR);
inb (AttrC_DataSwitch);
2015-09-20 05:44:50 +02:00
outb (uChar((index & 0x1F) | 0x20), AttrC_Index);
2015-05-23 13:35:12 +02:00
inb (AttrC_DataR);
return res;
}
//----------------------------------------------------------------------
int FTerm::getFramebuffer_bpp ()
{
int fd = -1;
struct fb_var_screeninfo fb_var;
struct fb_fix_screeninfo fb_fix;
const char* fb = const_cast<char*>("/dev/fb/0");
2015-10-01 03:48:58 +02:00
if ( (fd = open(fb, O_RDWR)) < 0 )
2015-05-23 13:35:12 +02:00
{
if ( errno != ENOENT && errno != ENOTDIR )
return -1;
2015-10-01 03:48:58 +02:00
2015-05-23 13:35:12 +02:00
fb = const_cast<char*>("/dev/fb0");
2015-10-01 03:48:58 +02:00
if ( (fd = open(fb, O_RDWR)) < 0 )
2015-05-23 13:35:12 +02:00
return -1;
}
if (fd >= 0)
{
if ( ! ioctl(fd, FBIOGET_VSCREENINFO, &fb_var)
&& ! ioctl(fd, FBIOGET_FSCREENINFO, &fb_fix) )
{
::close(fd);
return int(fb_var.bits_per_pixel);
}
else
{
::close(fd);
}
}
2015-10-01 04:44:26 +02:00
return -1;
2015-05-23 13:35:12 +02:00
}
//----------------------------------------------------------------------
int FTerm::openConsole()
{
if ( fd_tty >= 0 ) // console is already opened
return 0;
if ( term_name && (fd_tty = open (term_name, O_RDWR, 0)) < 0)
if ( (fd_tty = open("/proc/self/fd/0", O_RDWR, 0)) < 0)
if ( (fd_tty = open("/dev/tty", O_RDWR, 0)) < 0)
if ( (fd_tty = open("/dev/tty0", O_RDWR, 0)) < 0)
if ( (fd_tty = open("/dev/vc/0", O_RDWR, 0)) < 0)
if ( (fd_tty = open("/dev/systty", O_RDWR, 0)) < 0)
if ( (fd_tty = open("/dev/console", O_RDWR, 0)) < 0)
return -1; // No file descriptor referring to the console
return 0;
}
//----------------------------------------------------------------------
int FTerm::closeConsole()
{
if ( fd_tty < 0 ) // console is already closed
return 0;
int ret = ::close (fd_tty); // use 'close' from the global namespace
fd_tty = -1;
2015-05-23 13:35:12 +02:00
if ( ret == 0 )
return 0;
else
return -1;
}
//----------------------------------------------------------------------
int FTerm::isConsole()
{
char arg = 0;
// get keyboard type an compare
return ( isatty (fd_tty)
&& ioctl(fd_tty, KDGKBTYPE, &arg) == 0
&& ((arg == KB_101) || (arg == KB_84)) );
}
2016-05-01 21:45:27 +02:00
//----------------------------------------------------------------------
void FTerm::identifyTermType()
{
// Import the untrusted environment variable TERM
const char* term_env = std::getenv(const_cast<char*>("TERM"));
2016-05-01 21:45:27 +02:00
if ( term_env )
{
std::strncpy (termtype, term_env, sizeof(termtype) - 1);
2016-05-01 21:45:27 +02:00
return;
}
else if ( term_name )
{
// fallback: look into /etc/ttytype or /etc/ttys
2016-05-16 21:11:32 +02:00
//
// file format:
// <terminal type> <whitespace> <tty name>
//
// Example:
// linux tty1
// vt100 ttys0
2016-05-16 21:11:32 +02:00
std::FILE *fp;
2016-05-16 21:11:32 +02:00
if ( (fp = std::fopen("/etc/ttytype", "r")) != 0
|| (fp = std::fopen("/etc/ttys", "r")) != 0 )
2016-05-01 21:45:27 +02:00
{
2016-05-01 22:18:54 +02:00
char* p;
char* type;
char* name;
char str[BUFSIZ];
2016-05-01 21:45:27 +02:00
// get term basename
const char* term_basename = std::strrchr(term_name, '/');
2016-05-01 21:45:27 +02:00
if ( term_basename == 0 )
term_basename = term_name;
2016-05-01 21:45:27 +02:00
else
term_basename++;
2016-05-01 21:45:27 +02:00
// read and parse the file
2016-05-01 22:18:54 +02:00
while ( fgets(str, sizeof(str)-1, fp) != 0 )
2016-05-01 21:45:27 +02:00
{
type = name = 0; // 0 == not found
p = str;
2016-05-01 21:45:27 +02:00
while ( *p )
{
if ( std::isspace(uChar(*p)) )
2016-05-01 21:45:27 +02:00
*p = '\0';
else if ( type == 0 )
type = p;
else if ( name == 0 && p != str && p[-1] == '\0' )
name = p;
2016-05-01 21:45:27 +02:00
p++;
}
if ( type != 0 && name != 0 && ! std::strcmp(name, term_basename) )
2016-05-01 21:45:27 +02:00
{
std::strncpy (termtype, type, sizeof(termtype) - 1);
std::fclose(fp);
2016-05-01 21:45:27 +02:00
return;
}
}
std::fclose(fp);
2016-05-01 21:45:27 +02:00
}
}
2016-05-01 21:45:27 +02:00
// use vt100 if not found
std::strncpy (termtype, const_cast<char*>("vt100"), 6);
2016-05-01 21:45:27 +02:00
}
2015-05-23 13:35:12 +02:00
//----------------------------------------------------------------------
int FTerm::getScreenFont()
{
struct console_font_op font;
const size_t data_size = 4 * 32 * 512;
int ret;
if ( fd_tty < 0 )
return -1;
// initialize unused padding bytes in struct
memset(&font, 0, sizeof(console_font_op));
font.op = KD_FONT_OP_GET;
font.flags = 0;
font.width = 32;
font.height = 32;
font.charcount = 512;
// initialize with 0
2015-10-17 19:40:43 +02:00
font.data = new uChar[data_size]();
2015-05-23 13:35:12 +02:00
// font operation
ret = ioctl (fd_tty, KDFONTOP, &font);
if ( ret == 0 )
{
screen_font.width = font.width;
screen_font.height = font.height;
screen_font.charcount = font.charcount;
screen_font.data = font.data;
2015-05-23 13:35:12 +02:00
return 0;
}
else
return -1;
}
//----------------------------------------------------------------------
2015-10-10 04:01:22 +02:00
int FTerm::setScreenFont ( uChar* fontdata, uInt count
, uInt fontwidth, uInt fontheight
, bool direct)
2015-05-23 13:35:12 +02:00
{
struct console_font_op font;
int ret;
if ( fd_tty < 0 )
return -1;
// initialize unused padding bytes in struct
memset(&font, 0x00, sizeof(console_font_op));
2015-05-23 13:35:12 +02:00
font.op = KD_FONT_OP_SET;
font.flags = 0;
font.width = fontwidth;
font.height = fontheight;
font.charcount = count;
if ( direct )
font.data = fontdata;
else
{
2015-09-30 22:39:02 +02:00
const uInt bytes_per_line = font.width / 8;
2015-05-23 13:35:12 +02:00
const size_t data_size = bytes_per_line * 32 * font.charcount;
try
{
2015-10-17 19:40:43 +02:00
font.data = new uChar[data_size](); // initialize with 0
2015-05-23 13:35:12 +02:00
}
catch (const std::bad_alloc& ex)
{
std::cerr << "not enough memory to alloc " << ex.what() << std::endl;
return -1;
}
for (uInt i=0; i < count; i++)
std::memcpy ( const_cast<uChar*>(font.data + bytes_per_line*32*i)
, &fontdata[i * font.height]
, font.height);
2015-05-23 13:35:12 +02:00
}
// font operation
ret = ioctl (fd_tty, KDFONTOP, &font);
if ( ret != 0 && errno != ENOSYS && errno != EINVAL )
{
if ( ! direct )
delete[] font.data;
2015-05-23 13:35:12 +02:00
return -1;
}
if ( ! direct )
delete[] font.data;
if ( ret == 0 )
return 0;
else
return -1;
}
//----------------------------------------------------------------------
int FTerm::getUnicodeMap()
{
struct unimapdesc unimap;
int ret;
if ( fd_tty < 0 )
return -1;
unimap.entry_ct = 0;
unimap.entries = 0;
// get count
ret = ioctl (fd_tty, GIO_UNIMAP, &unimap);
2015-05-23 13:35:12 +02:00
if ( ret != 0 )
{
int count;
if ( errno != ENOMEM || unimap.entry_ct == 0 )
return -1;
count = unimap.entry_ct;
try
{
2015-10-17 19:40:43 +02:00
unimap.entries = new struct unipair[count]();
2015-05-23 13:35:12 +02:00
}
catch (const std::bad_alloc& ex)
{
std::cerr << "not enough memory to alloc " << ex.what() << std::endl;
return -1;
}
// get unicode-to-font mapping from kernel
ret = ioctl(fd_tty, GIO_UNIMAP, &unimap);
if ( ret == 0 )
{
screen_unicode_map.entry_ct = unimap.entry_ct;
screen_unicode_map.entries = unimap.entries;
2015-05-23 13:35:12 +02:00
}
else
return -1;
}
return 0;
}
//----------------------------------------------------------------------
int FTerm::setUnicodeMap (struct unimapdesc* unimap)
{
struct unimapinit advice;
int ret;
if ( fd_tty < 0 )
return -1;
advice.advised_hashsize = 0;
advice.advised_hashstep = 0;
advice.advised_hashlevel = 0;
do
{
// clear the unicode-to-font table
ret = ioctl(fd_tty, PIO_UNIMAPCLR, &advice);
2015-05-23 13:35:12 +02:00
if ( ret != 0 )
return -1;
// put the new unicode-to-font mapping in kernel
ret = ioctl(fd_tty, PIO_UNIMAP, unimap);
2015-05-23 13:35:12 +02:00
if ( ret != 0 )
advice.advised_hashlevel++;
}
while ( ret != 0 && errno == ENOMEM && advice.advised_hashlevel < 100);
if ( ret == 0 )
return 0;
else
return -1;
}
//----------------------------------------------------------------------
int FTerm::setBlinkAsIntensity (bool on)
2015-05-23 13:35:12 +02:00
{
// Uses blink-bit as background intensity.
// That permits 16 colors for background
if ( getuid() != 0 ) // Direct hardware access requires root privileges
2015-05-23 13:35:12 +02:00
return -2;
if ( fd_tty < 0 )
return -1;
// Enable access to VGA I/O ports (from 0x3B4 with num = 0x2C)
if ( ioctl(fd_tty, KDENABIO, 0) < 0 )
return -1; // error on KDENABIO
if ( on )
outb_Attribute_Controller (0x10, inb_Attribute_Controller(0x10) & 0xF7);
else
outb_Attribute_Controller (0x10, inb_Attribute_Controller(0x10) | 0x08);
// Disable access to VGA I/O ports
if ( ioctl(fd_tty, KDDISABIO, 0) < 0 )
return -1; // error on KDDISABIO
return 0;
}
//----------------------------------------------------------------------
bool FTerm::isKeyTimeout (timeval* time, register long timeout)
{
register long diff_usec;
struct timeval now;
struct timeval diff;
FObject::getCurrentTime(now);
diff.tv_sec = now.tv_sec - time->tv_sec;
diff.tv_usec = now.tv_usec - time->tv_usec;
2015-05-23 13:35:12 +02:00
if ( diff.tv_usec < 0 )
{
diff.tv_sec--;
diff.tv_usec += 1000000;
}
2015-05-23 13:35:12 +02:00
diff_usec = (diff.tv_sec * 1000000) + diff.tv_usec;
return (diff_usec > timeout);
}
//----------------------------------------------------------------------
2015-09-22 04:18:20 +02:00
int FTerm::parseKeyString ( char* buffer
, int buf_size
, timeval* time_keypressed )
2015-05-23 13:35:12 +02:00
{
register uChar firstchar = uChar(buffer[0]);
register size_t buf_len = std::strlen(buffer);
2015-05-23 13:35:12 +02:00
const long key_timeout = 100000; // 100 ms
int key, len, n;
if ( firstchar == ESC[0] )
2015-05-23 13:35:12 +02:00
{
// x11 mouse tracking
if ( buf_len >= 6 && buffer[1] == '[' && buffer[2] == 'M' )
return fc::Fkey_mouse;
2015-05-23 13:35:12 +02:00
// SGR mouse tracking
if ( buffer[1] == '[' && buffer[2] == '<' && buf_len >= 9
&& (buffer[buf_len-1] == 'M' || buffer[buf_len-1] == 'm') )
return fc::Fkey_extended_mouse;
2015-05-23 13:35:12 +02:00
// urxvt mouse tracking
if ( buffer[1] == '[' && buffer[2] >= '1' && buffer[2] <= '9'
&& buffer[3] >= '0' && buffer[3] <= '9' && buf_len >= 9
&& buffer[buf_len-1] == 'M' )
return fc::Fkey_urxvt_mouse;
// look for termcap keys
for (int i=0; Fkey[i].tname[0] != 0; i++)
{
char* k = Fkey[i].string;
len = (k) ? int(std::strlen(k)) : 0;
2015-05-23 13:35:12 +02:00
if ( k && std::strncmp(k, buffer, uInt(len)) == 0 ) // found
2015-05-23 13:35:12 +02:00
{
n = len;
for (; n < buf_size; n++) // Remove founded entry
buffer[n-len] = buffer[n];
for (; n-len < len; n++) // Fill rest with '\0'
2015-05-23 13:35:12 +02:00
buffer[n-len] = '\0';
input_data_pending = bool(buffer[0] != '\0');
2015-05-23 13:35:12 +02:00
return Fkey[i].num;
}
}
// look for meta keys
for (int i=0; Fmetakey[i].string[0] != 0; i++)
{
2015-09-30 22:39:02 +02:00
char* kmeta = Fmetakey[i].string; // The string is never null
len = int(std::strlen(kmeta));
if ( std::strncmp(kmeta, buffer, uInt(len)) == 0 ) // found
2015-05-23 13:35:12 +02:00
{
if ( len == 2 && ( buffer[1] == 'O'
|| buffer[1] == '['
|| buffer[1] == ']') )
{
if ( ! isKeyTimeout(time_keypressed, key_timeout) )
return NEED_MORE_DATA;
}
n = len;
for (; n < buf_size; n++) // Remove founded entry
buffer[n-len] = buffer[n];
for (; n-len < len; n++) // Fill rest with '\0'
2015-05-23 13:35:12 +02:00
buffer[n-len] = '\0';
input_data_pending = bool(buffer[0] != '\0');
2015-05-23 13:35:12 +02:00
return Fmetakey[i].num;
}
}
2015-05-23 13:35:12 +02:00
if ( ! isKeyTimeout(time_keypressed, key_timeout) )
return NEED_MORE_DATA;
}
// look for utf-8 character
len = 1;
if ( utf8_input && (firstchar & 0xc0) == 0xc0 )
{
2015-09-30 22:39:02 +02:00
char utf8char[4] = {}; // init array with '\0'
2015-05-23 13:35:12 +02:00
if ((firstchar & 0xe0) == 0xc0)
len = 2;
else if ((firstchar & 0xf0) == 0xe0)
len = 3;
else if ((firstchar & 0xf8) == 0xf0)
len = 4;
2015-05-23 13:35:12 +02:00
for (n=0; n < len ; n++)
utf8char[n] = char(buffer[n] & 0xff);
key = UTF8decode(utf8char);
}
else
key = uChar(buffer[0] & 0xff);
n = len;
2015-09-24 00:41:43 +02:00
for (; n < buf_size; n++) // remove the key from the buffer front
buffer[n-len] = buffer[n];
2015-09-24 00:41:43 +02:00
for (n=n-len; n < buf_size; n++) // fill the rest with '\0' bytes
buffer[n] = '\0';
input_data_pending = bool(buffer[0] != '\0');
2015-05-23 13:35:12 +02:00
if ( key == 0 ) // Ctrl+Space or Ctrl+@
key = fc::Fckey_space;
2015-05-23 13:35:12 +02:00
return int(key == 127 ? fc::Fkey_backspace : key);
}
//----------------------------------------------------------------------
bool& FTerm::unprocessedInput()
{
return input_data_pending;
}
//----------------------------------------------------------------------
int FTerm::getLineNumber()
{
if ( term->getHeight() == 0 )
getTermSize();
return term->getHeight();
}
//----------------------------------------------------------------------
int FTerm::getColumnNumber()
{
if ( term->getWidth() == 0 )
getTermSize();
return term->getWidth();
}
2015-05-23 13:35:12 +02:00
//----------------------------------------------------------------------
FString FTerm::getKeyName (int keynum)
2015-05-23 13:35:12 +02:00
{
for (int i=0; FkeyName[i].string[0] != 0; i++)
if ( FkeyName[i].num && FkeyName[i].num == keynum )
return FString(FkeyName[i].string);
2015-05-23 13:35:12 +02:00
if ( keynum > 32 && keynum < 127 )
return FString(char(keynum));
2015-05-23 13:35:12 +02:00
return FString("");
}
//----------------------------------------------------------------------
void FTerm::getModifierKey()
{
char subcode = 6;
// fill bit field with 0
memset (&mod_key, 0x00, sizeof(mod_key));
// TIOCLINUX, subcode=6
if ( ioctl(0, TIOCLINUX, &subcode) >= 0 )
{
if ( subcode & (1 << KG_SHIFT) )
mod_key.shift = true;
if ( subcode & (1 << KG_ALTGR) )
mod_key.alt_gr = true;
if ( subcode & (1 << KG_CTRL) )
mod_key.ctrl = true;
if ( subcode & (1 << KG_ALT) )
mod_key.alt = true;
}
}
2015-05-23 13:35:12 +02:00
//----------------------------------------------------------------------
2015-10-07 02:36:38 +02:00
void FTerm::init_console()
2015-05-23 13:35:12 +02:00
{
2015-10-07 02:36:38 +02:00
fd_tty = -1;
screen_unicode_map.entries = 0;
screen_font.data = 0;
2015-10-07 02:36:38 +02:00
if ( openConsole() == 0 )
2015-05-23 13:35:12 +02:00
{
2015-10-07 02:36:38 +02:00
if ( isConsole() )
2015-05-23 13:35:12 +02:00
{
2015-10-07 02:36:38 +02:00
getUnicodeMap();
getScreenFont();
2015-05-23 13:35:12 +02:00
}
2015-10-07 02:36:38 +02:00
getTermSize();
closeConsole();
2015-05-23 13:35:12 +02:00
}
2015-10-07 02:36:38 +02:00
else
2015-05-23 13:35:12 +02:00
{
2015-10-07 02:36:38 +02:00
std::cerr << "can not open the console.\n";
std::abort();
}
}
2015-05-23 13:35:12 +02:00
2015-10-07 02:36:38 +02:00
//----------------------------------------------------------------------
uInt FTerm::getBaudRate (const struct termios* termios_p)
{
std::map<speed_t,uInt> ospeed;
ospeed[B0] = 0; // hang up
ospeed[B50] = 50; // 50 baud
ospeed[B75] = 75; // 75 baud
ospeed[B110] = 110; // 110 baud
ospeed[B134] = 134; // 134.5 baud
ospeed[B150] = 150; // 150 baud
ospeed[B200] = 200; // 200 baud
ospeed[B300] = 300; // 300 baud
ospeed[B600] = 600; // 600 baud
ospeed[B1200] = 1200; // 1,200 baud
ospeed[B1800] = 1800; // 1,800 baud
ospeed[B2400] = 2400; // 2,400 baud
ospeed[B4800] = 4800; // 4,800 baud
ospeed[B9600] = 9600; // 9,600 baud
ospeed[B19200] = 19200; // 19,200 baud
ospeed[B38400] = 38400; // 38,400 baud
ospeed[B57600] = 57600; // 57,600 baud
ospeed[B115200] = 115200; // 115,200 baud
ospeed[B230400] = 230400; // 230,400 baud
2015-05-23 13:35:12 +02:00
2015-10-07 02:36:38 +02:00
return ospeed[cfgetospeed(termios_p)];
2015-05-23 13:35:12 +02:00
}
//----------------------------------------------------------------------
void FTerm::init_consoleCharMap()
{
if ( screen_unicode_map.entry_ct != 0 )
2015-05-23 13:35:12 +02:00
{
for (int i=0; i <= lastCharItem; i++ )
{
bool found = false;
for (uInt n=0; n < screen_unicode_map.entry_ct; n++)
2015-05-23 13:35:12 +02:00
{
if ( character[i][fc::UTF8] == screen_unicode_map.entries[n].unicode )
2015-05-23 13:35:12 +02:00
{
found = true;
break;
}
}
2015-05-23 13:35:12 +02:00
if ( ! found )
character[i][fc::PC] = character[i][fc::ASCII];
}
}
}
//----------------------------------------------------------------------
void FTerm::signal_handler (int signum)
{
switch (signum)
{
case SIGWINCH:
if ( resize_term )
break;
// initialize a resize event to the root element
resize_term = true;
break;
2015-09-20 05:44:50 +02:00
2015-05-23 13:35:12 +02:00
case SIGTERM:
case SIGQUIT:
case SIGINT:
case SIGABRT:
case SIGILL:
case SIGSEGV:
init_term_object->finish();
std::fflush (stderr);
std::fflush (stdout);
std::fprintf ( stderr
, "\nProgram stopped: signal %d (%s)\n"
, signum
, strsignal(signum) );
2015-09-22 04:18:20 +02:00
std::terminate();
2015-05-23 13:35:12 +02:00
}
}
//----------------------------------------------------------------------
2015-10-07 02:50:30 +02:00
char* FTerm::init_256colorTerminal()
2015-05-23 13:35:12 +02:00
{
char local256[80] = "";
2015-10-07 02:50:30 +02:00
char* new_termtype = 0;
2015-10-03 22:41:30 +02:00
char *s1, *s2, *s3, *s4, *s5, *s6;
2015-05-23 13:35:12 +02:00
// Enable 256 color capabilities
s1 = std::getenv("COLORTERM");
s2 = std::getenv("VTE_VERSION");
s3 = std::getenv("XTERM_VERSION");
s4 = std::getenv("ROXTERM_ID");
s5 = std::getenv("KONSOLE_DBUS_SESSION");
s6 = std::getenv("KONSOLE_DCOP");
2015-05-23 13:35:12 +02:00
if ( s1 != 0 )
std::strncat (local256, s1, sizeof(local256) - std::strlen(local256) - 1);
2015-05-23 13:35:12 +02:00
if ( s2 != 0 )
std::strncat (local256, s2, sizeof(local256) - std::strlen(local256) - 1);
2015-05-23 13:35:12 +02:00
if ( s3 != 0 )
std::strncat (local256, s3, sizeof(local256) - std::strlen(local256) - 1);
2015-05-23 13:35:12 +02:00
if ( s4 != 0 )
std::strncat (local256, s4, sizeof(local256) - std::strlen(local256) - 1);
2015-05-23 13:35:12 +02:00
if ( s5 != 0 )
std::strncat (local256, s5, sizeof(local256) - std::strlen(local256) - 1);
2015-05-23 13:35:12 +02:00
if ( s6 != 0 )
std::strncat (local256, s6, sizeof(local256) - std::strlen(local256) - 1);
2015-05-23 13:35:12 +02:00
if ( std::strlen(local256) > 0 )
2015-05-23 13:35:12 +02:00
{
if ( std::strncmp(termtype, "xterm", 5) == 0 )
2015-10-03 23:45:33 +02:00
new_termtype = const_cast<char*>("xterm-256color");
2015-05-23 13:35:12 +02:00
if ( std::strncmp(termtype, "screen", 6) == 0 )
2015-05-23 13:35:12 +02:00
{
2015-10-03 23:45:33 +02:00
new_termtype = const_cast<char*>("screen-256color");
2015-05-23 13:35:12 +02:00
screen_terminal = true;
char* tmux = std::getenv("TMUX");
if ( tmux && std::strlen(tmux) != 0 )
2015-05-23 13:35:12 +02:00
tmux_terminal = true;
}
if ( std::strncmp(termtype, "Eterm", 5) == 0 )
2015-10-03 23:45:33 +02:00
new_termtype = const_cast<char*>("Eterm-256color");
2015-05-23 13:35:12 +02:00
if ( std::strncmp(termtype, "mlterm", 6) == 0 )
2015-05-23 13:35:12 +02:00
{
2015-10-03 23:45:33 +02:00
new_termtype = const_cast<char*>("mlterm-256color");
2015-05-23 13:35:12 +02:00
mlterm_terminal = true;
}
if ( std::strncmp(termtype, "rxvt", 4) != 0
2015-05-23 13:35:12 +02:00
&& s1
&& std::strncmp(s1, "rxvt-xpm", 8) == 0 )
2015-05-23 13:35:12 +02:00
{
2015-10-03 23:45:33 +02:00
new_termtype = const_cast<char*>("rxvt-256color");
2015-05-23 13:35:12 +02:00
rxvt_terminal = true;
}
2015-05-23 13:35:12 +02:00
color256 = true;
}
else if ( std::strstr (termtype, "256color") )
2016-04-30 12:52:18 +02:00
color256 = true;
2015-05-23 13:35:12 +02:00
else
color256 = false;
if ( (s5 && std::strlen(s5) > 0) || (s6 && std::strlen(s6) > 0) )
2015-05-23 13:35:12 +02:00
kde_konsole = true;
if ( (s1 && std::strncmp(s1, "gnome-terminal", 14) == 0) || s2 )
2015-05-23 13:35:12 +02:00
{
gnome_terminal = true;
// Each gnome-terminal should be able to use 256 colors
color256 = true;
new_termtype = const_cast<char*>("gnome-256color");
2015-05-23 13:35:12 +02:00
}
2015-10-07 02:36:38 +02:00
return new_termtype;
}
//----------------------------------------------------------------------
char* FTerm::parseAnswerbackMsg (char*& current_termtype)
{
char* new_termtype = current_termtype;
2015-05-23 13:35:12 +02:00
// send ENQ and read the answerback message
answer_back = new FString(getAnswerbackMsg());
2015-10-07 02:36:38 +02:00
if ( answer_back && *answer_back == FString("PuTTY") )
2015-05-23 13:35:12 +02:00
{
putty_terminal = true;
2015-05-23 13:35:12 +02:00
if ( color256 )
2015-10-03 23:45:33 +02:00
new_termtype = const_cast<char*>("putty-256color");
2015-05-23 13:35:12 +02:00
else
2015-10-03 23:45:33 +02:00
new_termtype = const_cast<char*>("putty");
2015-05-23 13:35:12 +02:00
}
else
putty_terminal = false;
if ( cygwin_terminal )
std::putchar (BS[0]); // cygwin needs a backspace to delete the '♣' char
2015-05-23 13:35:12 +02:00
2015-10-07 02:36:38 +02:00
return new_termtype;
}
//----------------------------------------------------------------------
char* FTerm::parseSecDA (char*& current_termtype)
{
char* new_termtype = current_termtype;
bool sec_da_supported = false;
2015-10-07 02:36:38 +02:00
2015-12-11 06:04:29 +01:00
// The Linux console knows no Sec_DA
if ( linux_terminal )
return new_termtype;
2015-10-07 02:36:38 +02:00
// secondary device attributes (SEC_DA) <- decTerminalID string
sec_da = new FString(getSecDA());
2015-10-07 02:36:38 +02:00
if ( sec_da && sec_da->getLength() > 5 )
2015-05-23 13:35:12 +02:00
{
uLong num_components;
// remove the first 3 bytes ("\033[>")
FString temp = sec_da->right(sec_da->getLength() - 3);
// remove the last byte ("c")
2015-05-23 13:35:12 +02:00
temp.remove(temp.getLength()-1, 1);
// split into components
std::vector<FString> sec_da_split = temp.split(';');
2015-05-23 13:35:12 +02:00
num_components = sec_da_split.size();
if ( num_components == 3 )
sec_da_supported = true;
if ( num_components >= 2 )
2015-05-23 13:35:12 +02:00
{
FString* sec_da_components = &sec_da_split[0];
2015-05-23 13:35:12 +02:00
if ( ! sec_da_components[0].isEmpty() )
2015-09-30 22:39:02 +02:00
{
2015-10-07 02:36:38 +02:00
int terminal_id_type, terminal_id_version;
// Read the terminal type
try
{
terminal_id_type = sec_da_components[0].toInt();
2015-10-07 02:36:38 +02:00
}
catch (const std::exception&)
{
terminal_id_type = -1;
}
2015-10-01 03:48:58 +02:00
2015-10-07 02:36:38 +02:00
// Read the terminal (firmware) version
try
{
if ( sec_da_components[1] )
terminal_id_version = sec_da_components[1].toInt();
2015-05-23 13:35:12 +02:00
else
2015-10-07 02:36:38 +02:00
terminal_id_version = -1;
}
catch (const std::exception&)
{
terminal_id_version = -1;
}
2015-10-01 03:48:58 +02:00
2015-10-07 02:36:38 +02:00
switch ( terminal_id_type )
{
case 0: // DEC VT100
if ( terminal_id_version == 115 )
kde_konsole = true;
else if ( terminal_id_version == 136 )
2015-10-07 02:36:38 +02:00
putty_terminal = true; // PuTTY
break;
2015-10-07 21:32:30 +02:00
case 1: // DEC VT220
if ( ! sec_da_supported )
{
if ( terminal_id_version == 2 ) // also used by apple terminal
kterm_terminal = true; // kterm
}
else if ( terminal_id_version > 1000 )
2015-10-07 02:36:38 +02:00
{
gnome_terminal = true;
// Each gnome-terminal should be able to use 256 colors
color256 = true;
new_termtype = const_cast<char*>("gnome-256color");
2015-10-07 02:36:38 +02:00
}
break;
2015-10-07 21:32:30 +02:00
case 2: // DEC VT240
case 18: // DEC VT330
case 19: // DEC VT340
case 24: // DEC VT320
case 41: // DEC VT420
case 61: // DEC VT510
case 64: // DEC VT520
case 65: // DEC VT525
break;
2015-10-07 02:36:38 +02:00
case 32: // Tera Term
tera_terminal = true;
new_termtype = const_cast<char*>("teraterm");
break;
2015-10-07 21:32:30 +02:00
2015-10-07 02:36:38 +02:00
case 77: // mintty
mintty_terminal = true;
new_termtype = const_cast<char*>("xterm-256color");
// switch to application escape key mode
2016-01-31 21:06:29 +01:00
putstring (CSI "?7727h");
std::fflush(stdout);
2015-10-07 02:36:38 +02:00
break;
2015-10-07 21:32:30 +02:00
2015-10-07 02:36:38 +02:00
case 83: // screen
screen_terminal = true;
break;
2015-10-07 21:32:30 +02:00
2015-10-07 02:36:38 +02:00
case 82: // rxvt
rxvt_terminal = true;
force_vt100 = true; // this rxvt terminal support on utf-8
if ( std::strncmp(termtype, "rxvt-", 5) != 0
|| std::strncmp(termtype, "rxvt-cygwin-native", 5) == 0 )
2015-10-07 02:36:38 +02:00
new_termtype = const_cast<char*>("rxvt-16color");
break;
2015-10-07 21:32:30 +02:00
2015-10-07 02:36:38 +02:00
case 85: // rxvt-unicode
rxvt_terminal = true;
urxvt_terminal = true;
if ( std::strncmp(termtype, "rxvt-", 5) != 0 )
2015-10-07 02:36:38 +02:00
{
if ( color256 )
new_termtype = const_cast<char*>("rxvt-256color");
else
new_termtype = const_cast<char*>("rxvt");
}
break;
2015-10-07 21:32:30 +02:00
2015-10-07 02:36:38 +02:00
default:
break;
}
2015-09-30 22:39:02 +02:00
}
2015-05-23 13:35:12 +02:00
}
}
2016-10-09 02:06:06 +02:00
2015-10-07 02:36:38 +02:00
return new_termtype;
}
2015-05-23 13:35:12 +02:00
//----------------------------------------------------------------------
void FTerm::oscPrefix()
{
if ( tmux_terminal )
{
// tmux device control string
putstring (ESC "Ptmux;" ESC);
}
else if ( screen_terminal )
{
// GNU Screen device control string
putstring (ESC "P");
}
}
//----------------------------------------------------------------------
void FTerm::oscPostfix()
{
if ( screen_terminal || tmux_terminal )
{
// GNU Screen/tmux string terminator
putstring (ESC "\\");
}
}
2015-10-07 02:36:38 +02:00
//----------------------------------------------------------------------
void FTerm::init_alt_charset()
2015-10-07 02:36:38 +02:00
{
// read the used vt100 pairs
if ( tcap[fc::t_acs_chars].string )
2015-10-03 23:45:33 +02:00
{
for (int n=0; tcap[fc::t_acs_chars].string[n]; tcap[fc::t_acs_chars].string += 2)
2015-10-07 02:36:38 +02:00
{
// insert the vt100 key/value pairs into a map
uChar p1 = uChar(tcap[fc::t_acs_chars].string[n]);
uChar p2 = uChar(tcap[fc::t_acs_chars].string[n+1]);
2015-10-07 02:36:38 +02:00
(*vt100_alt_char)[p1] = p2;
}
2015-10-03 23:45:33 +02:00
}
2015-10-07 21:32:30 +02:00
2015-10-07 02:36:38 +02:00
enum column
2015-05-23 13:35:12 +02:00
{
2015-10-07 02:36:38 +02:00
vt100_key = 0,
utf8_char = 1
};
2015-10-07 21:32:30 +02:00
2015-10-07 02:36:38 +02:00
// update array 'character' with discovered vt100 pairs
for (int n=0; n <= lastKeyItem; n++ )
2015-05-23 13:35:12 +02:00
{
2015-10-07 02:36:38 +02:00
uChar keyChar = uChar(vt100_key_to_utf8[n][vt100_key]);
uChar altChar = uChar((*vt100_alt_char)[ keyChar ]);
uInt utf8char = uInt(vt100_key_to_utf8[n][utf8_char]);
2015-10-07 21:32:30 +02:00
fc::encoding num = fc::NUM_OF_ENCODINGS;
2015-10-07 02:36:38 +02:00
uInt* p = std::find ( character[0]
2015-10-07 21:32:30 +02:00
, character[lastCharItem] + num
2015-10-07 02:36:38 +02:00
, utf8char );
2015-10-07 21:32:30 +02:00
if ( p != character[lastCharItem] + num ) // found in character
2015-05-23 13:35:12 +02:00
{
2015-10-07 21:32:30 +02:00
int item = int(std::distance(character[0], p) / num);
2015-10-07 02:36:38 +02:00
if ( altChar )
character[item][fc::VT100] = altChar; // update alternate character set
else
character[item][fc::VT100] = 0; // delete vt100 char in character
2015-05-23 13:35:12 +02:00
}
}
2015-10-07 02:36:38 +02:00
}
//----------------------------------------------------------------------
void FTerm::init_pc_charset()
{
bool reinit = false;
// rxvt does not support pc charset
2016-01-17 02:57:08 +01:00
if ( rxvt_terminal || urxvt_terminal )
return;
if ( gnome_terminal || linux_terminal )
{
// fallback if tcap "S2" is not found
if ( ! tcap[fc::t_enter_pc_charset_mode].string )
{
if ( utf8_console )
{
// Select iso8859-1 + null mapping
tcap[fc::t_enter_pc_charset_mode].string = \
const_cast<char*>(ESC "%@" ESC "(U");
}
else
{
// Select null mapping
tcap[fc::t_enter_pc_charset_mode].string = \
const_cast<char*>(ESC "(U");
}
opti_attr->set_enter_pc_charset_mode (tcap[fc::t_enter_pc_charset_mode].string);
reinit = true;
}
// fallback if tcap "S3" is not found
if ( ! tcap[fc::t_exit_pc_charset_mode].string )
{
if ( utf8_console )
{
// Select ascii mapping + utf8
tcap[fc::t_exit_pc_charset_mode].string = \
const_cast<char*>(ESC "(B" ESC "%G");
}
else
{
// Select ascii mapping
tcap[fc::t_enter_pc_charset_mode].string = \
const_cast<char*>(ESC "(B");
}
opti_attr->set_exit_pc_charset_mode (tcap[fc::t_exit_pc_charset_mode].string);
reinit = true;
}
}
if ( reinit )
opti_attr->init();
}
2015-10-07 02:36:38 +02:00
//----------------------------------------------------------------------
void FTerm::init_termcaps()
{
/* Terminal capability data base
* -----------------------------
* Info under: man 5 terminfo
*
* Importent shell commands:
* captoinfo - convert all termcap descriptions into terminfo descriptions
* infocmp - print out terminfo description from the current terminal
*/
static char term_buffer[2048];
static char string_buf[2048];
char* buffer = string_buf;
// share the terminal capabilities
FTermcap().setTermcapMap(tcap);
2015-10-07 02:36:38 +02:00
// open the termcap file + load entry for termtype
if ( tgetent(term_buffer, termtype) == 1 )
{
char* key_up_string;
// screen erased with the background color
background_color_erase = tgetflag(const_cast<char*>("ut"));
// t_cursor_left wraps from column 0 to last column
automatic_left_margin = tgetflag(const_cast<char*>("bw"));
// terminal has auto-matic margins
automatic_right_margin = tgetflag(const_cast<char*>("am"));
// newline ignored after 80 cols
eat_nl_glitch = tgetflag(const_cast<char*>("xn"));
2016-01-10 00:56:52 +01:00
// terminal supports ANSI set default fg and bg color
ansi_default_color = tgetflag(const_cast<char*>("AX"));
// terminal supports operating system commands (OSC)
// OSC = Esc + ']'
2016-01-10 00:56:52 +01:00
osc_support = tgetflag(const_cast<char*>("XT"));
if ( isTeraTerm() )
eat_nl_glitch = true;
2015-10-07 02:36:38 +02:00
// maximum number of colors on screen
max_color = tgetnum(const_cast<char*>("Co"));
if ( max_color < 0 )
max_color = 1;
2015-10-07 02:36:38 +02:00
if ( max_color < 8 )
monochron = true;
else
monochron = false;
tabstop = uInt(tgetnum(const_cast<char*>("it")));
attr_without_color = uInt(tgetnum(const_cast<char*>("NC")));
2016-01-17 02:57:08 +01:00
// gnome-terminal has NC=16 however, it can use the dim attribute
if ( gnome_terminal )
attr_without_color = 0;
2016-01-17 02:57:08 +01:00
// PuTTY has NC=22 however, it can show underline and reverse
if ( putty_terminal )
attr_without_color = 16;
2015-10-07 02:36:38 +02:00
// read termcap output strings
for (int i=0; tcap[i].tname[0] != 0; i++)
tcap[i].string = tgetstr(tcap[i].tname, &buffer);
// set invisible cursor for cygwin terminal
if ( cygwin_terminal && ! tcap[fc::t_cursor_invisible].string )
tcap[fc::t_cursor_invisible].string = \
2016-01-31 21:06:29 +01:00
const_cast<char*>(CSI "?25l");
// set visible cursor for cygwin terminal
if ( cygwin_terminal && ! tcap[fc::t_cursor_visible].string )
tcap[fc::t_cursor_visible].string = \
2016-01-31 21:06:29 +01:00
const_cast<char*>(CSI "?25h");
2015-10-07 02:36:38 +02:00
// set ansi blink for cygwin terminal
if ( cygwin_terminal && ! tcap[fc::t_enter_blink_mode].string )
tcap[fc::t_enter_blink_mode].string = \
2016-01-31 21:06:29 +01:00
const_cast<char*>(CSI "5m");
2015-10-07 02:36:38 +02:00
// set enter/exit alternative charset mode for rxvt terminal
if ( rxvt_terminal && std::strncmp(termtype, "rxvt-16color", 12) == 0 )
2015-10-07 02:36:38 +02:00
{
tcap[fc::t_enter_alt_charset_mode].string = \
2016-01-31 21:06:29 +01:00
const_cast<char*>(ESC "(0");
tcap[fc::t_exit_alt_charset_mode].string = \
2016-01-31 21:06:29 +01:00
const_cast<char*>(ESC "(B");
2015-10-07 02:36:38 +02:00
}
// set exit underline for gnome terminal
if ( gnome_terminal )
tcap[fc::t_exit_underline_mode].string = \
2016-01-31 21:06:29 +01:00
const_cast<char*>(CSI "24m");
2015-10-07 02:36:38 +02:00
// set ansi foreground and background color
if ( linux_terminal || cygwin_terminal )
{
tcap[fc::t_set_a_foreground].string = \
2016-01-31 21:06:29 +01:00
const_cast<char*>(CSI "3%p1%{8}%m%d%?%p1%{7}%>%t;1%e;21%;m");
tcap[fc::t_set_a_background].string = \
2016-01-31 21:06:29 +01:00
const_cast<char*>(CSI "4%p1%{8}%m%d%?%p1%{7}%>%t;5%e;25%;m");
tcap[fc::t_orig_pair].string = \
2016-01-31 21:06:29 +01:00
const_cast<char*>(CSI "39;49;25m");
2015-10-07 02:36:38 +02:00
}
else if ( rxvt_terminal && ! urxvt_terminal )
{
tcap[fc::t_set_a_foreground].string = \
2016-01-31 21:06:29 +01:00
const_cast<char*>(CSI "%?%p1%{8}%<%t%p1%{30}%+%e%p1%'R'%+%;%dm");
tcap[fc::t_set_a_background].string = \
2016-01-31 21:06:29 +01:00
const_cast<char*>(CSI "%?%p1%{8}%<%t%p1%'('%+%e%p1%{92}%+%;%dm");
}
2015-10-23 23:57:00 +02:00
else if ( tera_terminal )
2015-10-07 02:36:38 +02:00
{
tcap[fc::t_set_a_foreground].string = \
2016-01-31 21:06:29 +01:00
const_cast<char*>(CSI "38;5;%p1%dm");
tcap[fc::t_set_a_background].string = \
2016-01-31 21:06:29 +01:00
const_cast<char*>(CSI "48;5;%p1%dm");
tcap[fc::t_exit_attribute_mode].string = \
2016-01-31 21:06:29 +01:00
const_cast<char*>(CSI "0m" SI);
2015-10-07 02:36:38 +02:00
}
else if ( putty_terminal )
{
tcap[fc::t_set_a_foreground].string = \
2016-01-31 21:06:29 +01:00
const_cast<char*>(CSI "%?%p1%{8}%<"
2015-10-07 02:36:38 +02:00
"%t3%p1%d"
"%e%p1%{16}%<"
"%t9%p1%{8}%-%d"
"%e38;5;%p1%d%;m");
tcap[fc::t_set_a_background].string = \
2016-01-31 21:06:29 +01:00
const_cast<char*>(CSI "%?%p1%{8}%<"
2015-10-07 02:36:38 +02:00
"%t4%p1%d"
"%e%p1%{16}%<"
"%t10%p1%{8}%-%d"
"%e48;5;%p1%d%;m");
}
// fallback if "AF" is not found
if ( ! tcap[fc::t_set_a_foreground].string )
tcap[fc::t_set_a_foreground].string = \
2016-01-31 21:06:29 +01:00
const_cast<char*>(CSI "3%p1%dm");
2015-10-07 02:36:38 +02:00
// fallback if "AB" is not found
if ( ! tcap[fc::t_set_a_background].string )
tcap[fc::t_set_a_background].string = \
2016-01-31 21:06:29 +01:00
const_cast<char*>(CSI "4%p1%dm");
2015-10-07 02:36:38 +02:00
// fallback if "Ic" is not found
if ( ! tcap[fc::t_initialize_color].string )
{
if ( screen_terminal )
{
if ( tmux_terminal )
{
tcap[fc::t_initialize_color].string = \
const_cast<char*>(ESC "Ptmux;" ESC OSC "4;%p1%d;rgb:"
"%p2%{255}%*%{1000}%/%2.2X/"
"%p3%{255}%*%{1000}%/%2.2X/"
"%p4%{255}%*%{1000}%/%2.2X" BEL ESC "\\");
}
else
{
tcap[fc::t_initialize_color].string = \
const_cast<char*>(ESC "P" OSC "4;%p1%d;rgb:"
"%p2%{255}%*%{1000}%/%2.2X/"
"%p3%{255}%*%{1000}%/%2.2X/"
"%p4%{255}%*%{1000}%/%2.2X" BEL ESC "\\");
}
}
else
{
tcap[fc::t_initialize_color].string = \
const_cast<char*>(OSC "P%p1%x"
"%p2%{255}%*%{1000}%/%02x"
"%p3%{255}%*%{1000}%/%02x"
"%p4%{255}%*%{1000}%/%02x");
}
}
2015-10-07 02:36:38 +02:00
// fallback if "ti" is not found
if ( ! tcap[fc::t_enter_ca_mode].string )
tcap[fc::t_enter_ca_mode].string = \
2016-01-31 21:06:29 +01:00
const_cast<char*>(ESC "7" CSI "?47h");
2015-10-07 02:36:38 +02:00
// fallback if "te" is not found
if ( ! tcap[fc::t_exit_ca_mode].string )
tcap[fc::t_exit_ca_mode].string = \
2016-01-31 21:06:29 +01:00
const_cast<char*>(CSI "?47l" ESC "8" CSI "m");
2015-10-07 02:36:38 +02:00
// set ansi move if "cm" is not found
if ( ! tcap[fc::t_cursor_address].string )
tcap[fc::t_cursor_address].string = \
2016-01-31 21:06:29 +01:00
const_cast<char*>(CSI "%i%p1%d;%p2%dH");
2015-10-07 02:36:38 +02:00
// test for standard ECMA-48 (ANSI X3.64) terminal
if ( tcap[fc::t_exit_underline_mode].string
&& std::strncmp(tcap[fc::t_exit_underline_mode].string, CSI "24m", 5) == 0 )
{
// seems to be a ECMA-48 (ANSI X3.64) compatible terminal
tcap[fc::t_enter_dbl_underline_mode].string = \
2016-01-31 21:06:29 +01:00
const_cast<char*>(CSI "21m"); // Exit single underline, too
tcap[fc::t_exit_dbl_underline_mode].string = \
2016-01-31 21:06:29 +01:00
const_cast<char*>(CSI "24m");
tcap[fc::t_exit_bold_mode].string = \
2016-01-31 21:06:29 +01:00
const_cast<char*>(CSI "22m"); // Exit dim, too
tcap[fc::t_exit_dim_mode].string = \
2016-01-31 21:06:29 +01:00
const_cast<char*>(CSI "22m");
tcap[fc::t_exit_underline_mode].string = \
2016-01-31 21:06:29 +01:00
const_cast<char*>(CSI "24m");
tcap[fc::t_exit_blink_mode].string = \
2016-01-31 21:06:29 +01:00
const_cast<char*>(CSI "25m");
tcap[fc::t_exit_reverse_mode].string = \
2016-01-31 21:06:29 +01:00
const_cast<char*>(CSI "27m");
tcap[fc::t_exit_secure_mode].string = \
2016-01-31 21:06:29 +01:00
const_cast<char*>(CSI "28m");
tcap[fc::t_enter_crossed_out_mode].string = \
2016-01-31 21:06:29 +01:00
const_cast<char*>(CSI "9m");
tcap[fc::t_exit_crossed_out_mode].string = \
2016-01-31 21:06:29 +01:00
const_cast<char*>(CSI "29m");
}
2015-10-07 02:36:38 +02:00
// read termcap key strings
for (int i=0; Fkey[i].tname[0] != 0; i++)
{
Fkey[i].string = tgetstr(Fkey[i].tname, &buffer);
// fallback for rxvt with TERM=xterm
if ( std::strncmp(Fkey[i].tname, "khx", 3) == 0 )
2016-01-31 21:06:29 +01:00
Fkey[i].string = const_cast<char*>(CSI "7~"); // home key
2015-10-07 02:36:38 +02:00
if ( std::strncmp(Fkey[i].tname, "@7x", 3) == 0 )
2016-01-31 21:06:29 +01:00
Fkey[i].string = const_cast<char*>(CSI "8~"); // end key
2015-10-07 02:36:38 +02:00
if ( std::strncmp(Fkey[i].tname, "k1x", 3) == 0 )
2016-01-31 21:06:29 +01:00
Fkey[i].string = const_cast<char*>(CSI "11~"); // F1
2015-10-07 02:36:38 +02:00
if ( std::strncmp(Fkey[i].tname, "k2x", 3) == 0 )
2016-01-31 21:06:29 +01:00
Fkey[i].string = const_cast<char*>(CSI "12~"); // F2
2015-10-07 02:36:38 +02:00
if ( std::strncmp(Fkey[i].tname, "k3x", 3) == 0 )
2016-01-31 21:06:29 +01:00
Fkey[i].string = const_cast<char*>(CSI "13~"); // F3
2015-10-07 02:36:38 +02:00
if ( std::strncmp(Fkey[i].tname, "k4x", 3) == 0 )
2016-01-31 21:06:29 +01:00
Fkey[i].string = const_cast<char*>(CSI "14~"); // F4
// fallback for TERM=ansi
if ( std::strncmp(Fkey[i].tname, "@7X", 3) == 0 )
2016-01-31 21:06:29 +01:00
Fkey[i].string = const_cast<char*>(CSI "K"); // end key
2015-10-07 02:36:38 +02:00
}
// Some terminals (e.g. PuTTY) send the wrong code for the arrow keys
// http://www.unix.com/shell-programming-scripting/..
// ..110380-using-arrow-keys-shell-scripts.html
key_up_string = tgetstr(const_cast<char*>("ku"), &buffer);
if ( (key_up_string && (std::strcmp(key_up_string, CSI "A") == 0))
|| ( tcap[fc::t_cursor_up].string
&& (std::strcmp(tcap[fc::t_cursor_up].string, CSI "A") == 0)) )
2015-10-07 02:36:38 +02:00
{
for (int i=0; Fkey[i].tname[0] != 0; i++)
{
if ( std::strncmp(Fkey[i].tname, "kux", 3) == 0 )
2016-01-31 21:06:29 +01:00
Fkey[i].string = const_cast<char*>(CSI "A"); // key up
2015-10-07 02:36:38 +02:00
if ( std::strncmp(Fkey[i].tname, "kdx", 3) == 0 )
2016-01-31 21:06:29 +01:00
Fkey[i].string = const_cast<char*>(CSI "B"); // key down
2015-10-07 02:36:38 +02:00
if ( std::strncmp(Fkey[i].tname, "krx", 3) == 0 )
2016-01-31 21:06:29 +01:00
Fkey[i].string = const_cast<char*>(CSI "C"); // key right
2015-10-07 02:36:38 +02:00
if ( std::strncmp(Fkey[i].tname, "klx", 3) == 0 )
2016-01-31 21:06:29 +01:00
Fkey[i].string = const_cast<char*>(CSI "D"); // key left
2015-05-23 13:35:12 +02:00
if ( std::strncmp(Fkey[i].tname, "k1X", 3) == 0 )
2016-01-31 21:06:29 +01:00
Fkey[i].string = const_cast<char*>(ESC "OP"); // PF1
2015-10-07 02:36:38 +02:00
if ( std::strncmp(Fkey[i].tname, "k2X", 3) == 0 )
2016-01-31 21:06:29 +01:00
Fkey[i].string = const_cast<char*>(ESC "OQ"); // PF2
2015-10-07 02:36:38 +02:00
if ( std::strncmp(Fkey[i].tname, "k3X", 3) == 0 )
2016-01-31 21:06:29 +01:00
Fkey[i].string = const_cast<char*>(ESC "OR"); // PF3
2015-10-07 02:36:38 +02:00
if ( std::strncmp(Fkey[i].tname, "k4X", 3) == 0 )
2016-01-31 21:06:29 +01:00
Fkey[i].string = const_cast<char*>(ESC "OS"); // PF4
2015-10-07 02:36:38 +02:00
}
}
}
else
{
std::cerr << "Unknown terminal: " << termtype << "\n"
<< "Check the TERM environment variable\n"
<< "Also make sure that the terminal"
<< "is defined in the terminfo database.\n";
std::abort();
}
// duration precalculation of the cursor movement strings
opti_move->setTabStop(int(tabstop));
opti_move->set_cursor_home (tcap[fc::t_cursor_home].string);
opti_move->set_cursor_to_ll (tcap[fc::t_cursor_to_ll].string);
opti_move->set_carriage_return (tcap[fc::t_carriage_return].string);
opti_move->set_tabular (tcap[fc::t_tab].string);
opti_move->set_back_tab (tcap[fc::t_back_tab].string);
opti_move->set_cursor_up (tcap[fc::t_cursor_up].string);
opti_move->set_cursor_down (tcap[fc::t_cursor_down].string);
opti_move->set_cursor_left (tcap[fc::t_cursor_left].string);
opti_move->set_cursor_right (tcap[fc::t_cursor_right].string);
opti_move->set_cursor_address (tcap[fc::t_cursor_address].string);
opti_move->set_column_address (tcap[fc::t_column_address].string);
opti_move->set_row_address (tcap[fc::t_row_address].string);
opti_move->set_parm_up_cursor (tcap[fc::t_parm_up_cursor].string);
opti_move->set_parm_down_cursor (tcap[fc::t_parm_down_cursor].string);
opti_move->set_parm_left_cursor (tcap[fc::t_parm_left_cursor].string);
opti_move->set_parm_right_cursor (tcap[fc::t_parm_right_cursor].string);
opti_move->set_auto_left_margin (automatic_left_margin);
opti_move->set_eat_newline_glitch (eat_nl_glitch);
//opti_move->printDurations();
// attribute settings
opti_attr->setNoColorVideo (int(attr_without_color));
opti_attr->set_enter_bold_mode (tcap[fc::t_enter_bold_mode].string);
opti_attr->set_exit_bold_mode (tcap[fc::t_exit_bold_mode].string);
opti_attr->set_enter_dim_mode (tcap[fc::t_enter_dim_mode].string);
opti_attr->set_exit_dim_mode (tcap[fc::t_exit_dim_mode].string);
opti_attr->set_enter_italics_mode (tcap[fc::t_enter_italics_mode].string);
opti_attr->set_exit_italics_mode (tcap[fc::t_exit_italics_mode].string);
opti_attr->set_enter_underline_mode (tcap[fc::t_enter_underline_mode].string);
opti_attr->set_exit_underline_mode (tcap[fc::t_exit_underline_mode].string);
opti_attr->set_enter_blink_mode (tcap[fc::t_enter_blink_mode].string);
opti_attr->set_exit_blink_mode (tcap[fc::t_exit_blink_mode].string);
opti_attr->set_enter_reverse_mode (tcap[fc::t_enter_reverse_mode].string);
opti_attr->set_exit_reverse_mode (tcap[fc::t_exit_reverse_mode].string);
opti_attr->set_enter_standout_mode (tcap[fc::t_enter_standout_mode].string);
opti_attr->set_exit_standout_mode (tcap[fc::t_exit_standout_mode].string);
opti_attr->set_enter_secure_mode (tcap[fc::t_enter_secure_mode].string);
opti_attr->set_exit_secure_mode (tcap[fc::t_exit_secure_mode].string);
opti_attr->set_enter_protected_mode (tcap[fc::t_enter_protected_mode].string);
opti_attr->set_exit_protected_mode (tcap[fc::t_exit_protected_mode].string);
opti_attr->set_enter_crossed_out_mode (tcap[fc::t_enter_crossed_out_mode].string);
opti_attr->set_exit_crossed_out_mode (tcap[fc::t_exit_crossed_out_mode].string);
opti_attr->set_enter_dbl_underline_mode (tcap[fc::t_enter_dbl_underline_mode].string);
opti_attr->set_exit_dbl_underline_mode (tcap[fc::t_exit_dbl_underline_mode].string);
opti_attr->set_set_attributes (tcap[fc::t_set_attributes].string);
opti_attr->set_exit_attribute_mode (tcap[fc::t_exit_attribute_mode].string);
opti_attr->set_enter_alt_charset_mode (tcap[fc::t_enter_alt_charset_mode].string);
opti_attr->set_exit_alt_charset_mode (tcap[fc::t_exit_alt_charset_mode].string);
opti_attr->set_enter_pc_charset_mode (tcap[fc::t_enter_pc_charset_mode].string);
opti_attr->set_exit_pc_charset_mode (tcap[fc::t_exit_pc_charset_mode].string);
opti_attr->set_a_foreground_color (tcap[fc::t_set_a_foreground].string);
opti_attr->set_a_background_color (tcap[fc::t_set_a_background].string);
opti_attr->set_foreground_color (tcap[fc::t_set_foreground].string);
opti_attr->set_background_color (tcap[fc::t_set_background].string);
opti_attr->set_term_color_pair (tcap[fc::t_set_color_pair].string);
opti_attr->set_orig_pair (tcap[fc::t_orig_pair].string);
opti_attr->set_orig_orig_colors (tcap[fc::t_orig_colors].string);
2016-01-10 00:56:52 +01:00
opti_attr->setMaxColor (max_color);
2016-01-10 00:56:52 +01:00
if ( ansi_default_color )
opti_attr->setDefaultColorSupport();
if ( cygwin_terminal )
opti_attr->setCygwinTerminal();
opti_attr->init();
2015-10-07 02:36:38 +02:00
}
//----------------------------------------------------------------------
void FTerm::init_encoding()
{
// detect encoding and set the Fputchar function pointer
if ( isatty(stdout_no) && ! std::strcmp(nl_langinfo(CODESET), "UTF-8") )
2015-05-23 13:35:12 +02:00
{
utf8_console = true;
Encoding = fc::UTF8;
Fputchar = &FTerm::putchar_UTF8; // function pointer
utf8_state = true;
utf8_input = true;
setUTF8(true);
}
2015-10-03 17:01:00 +02:00
else if ( isatty(stdout_no)
&& (std::strlen(termtype) > 0)
&& (tcap[fc::t_exit_alt_charset_mode].string != 0) )
2015-05-23 13:35:12 +02:00
{
vt100_console = true;
Encoding = fc::VT100;
Fputchar = &FTerm::putchar_ASCII; // function pointer
2015-05-23 13:35:12 +02:00
}
else
{
ascii_console = true;
Encoding = fc::ASCII;
Fputchar = &FTerm::putchar_ASCII; // function pointer
}
init_pc_charset();
2015-05-23 13:35:12 +02:00
if ( linux_terminal
|| cygwin_terminal
|| NewFont
|| (putty_terminal && ! utf8_state) )
{
pc_charset_console = true;
Encoding = fc::PC;
Fputchar = &FTerm::putchar_ASCII; // function pointer
2015-05-23 13:35:12 +02:00
if ( linux_terminal && utf8_console )
{
utf8_linux_terminal = true;
setUTF8(false);
}
2016-10-09 02:06:06 +02:00
else if ( xterm_terminal && utf8_console )
{
Fputchar = &FTerm::putchar_UTF8; // function pointer
}
2015-05-23 13:35:12 +02:00
}
if ( force_vt100 )
{
vt100_console = true;
Encoding = fc::VT100;
Fputchar = &FTerm::putchar_ASCII; // function pointer
2015-05-23 13:35:12 +02:00
}
2015-10-07 02:36:38 +02:00
}
//----------------------------------------------------------------------
void FTerm::init()
{
char* new_termtype = 0;
term_initialized = true;
init_term_object = this;
2015-10-07 02:36:38 +02:00
x11_button_state = 0x03;
max_color = 1;
fd_tty = -1;
opti_move = new FOptiMove();
opti_attr = new FOptiAttr();
term = new FRect(0,0,0,0);
mouse = new FPoint(0,0);
2015-10-07 02:36:38 +02:00
vt100_alt_char = new std::map<uChar,uChar>;
encoding_set = new std::map<std::string,fc::encoding>;
// Define the encoding set
(*encoding_set)["UTF8"] = fc::UTF8;
(*encoding_set)["UTF-8"] = fc::UTF8;
(*encoding_set)["VT100"] = fc::VT100;
(*encoding_set)["PC"] = fc::PC;
(*encoding_set)["ASCII"] = fc::ASCII;
// Preset to false
utf8_console = \
utf8_input = \
utf8_state = \
utf8_linux_terminal = \
pc_charset_console = \
2015-10-07 02:36:38 +02:00
vt100_console = \
NewFont = \
VGAFont = \
ascii_console = \
mouse_support = \
force_vt100 = \
tera_terminal = \
kterm_terminal = \
gnome_terminal = \
kde_konsole = \
2015-10-07 02:36:38 +02:00
rxvt_terminal = \
urxvt_terminal = \
mlterm_terminal = \
mintty_terminal = \
screen_terminal = \
tmux_terminal = \
background_color_erase = false;
// Preset to true
cursor_optimisation = true;
2015-10-07 02:36:38 +02:00
// assertion: programm start in cooked mode
raw_mode = \
input_data_pending = \
non_blocking_stdin = false;
// init arrays with '\0'
std::fill_n (exit_message, sizeof(exit_message), '\0');
2015-10-07 02:36:38 +02:00
stdin_no = fileno(stdin);
stdout_no = fileno(stdout);
stdin_status_flags = fcntl(stdin_no, F_GETFL);
if ( stdin_status_flags == -1 )
std::abort();
term_name = ttyname(stdout_no);
// initialize terminal and Linux console
init_console();
// make stdin non-blocking
setNonBlockingInput();
// save termios settings
tcgetattr (stdin_no, &term_init);
// get output baud rate
baudrate = getBaudRate(&term_init);
if ( isatty(stdout_no) )
opti_move->setBaudRate(int(baudrate));
2015-10-07 21:32:30 +02:00
2016-05-01 21:45:27 +02:00
// detect the type of the terminal
identifyTermType();
2015-10-07 02:36:38 +02:00
// initialize 256 colors terminals
2015-10-07 02:50:30 +02:00
new_termtype = init_256colorTerminal();
2015-10-07 02:36:38 +02:00
if ( std::strncmp(termtype, "cygwin", 6) == 0 )
2015-10-07 02:36:38 +02:00
cygwin_terminal = true;
else
cygwin_terminal = false;
if ( std::strncmp(termtype, "rxvt-cygwin-native", 18) == 0 )
2015-10-07 02:36:38 +02:00
rxvt_terminal = true;
2015-12-11 06:04:29 +01:00
// Test for Linux console
if ( std::strncmp(termtype, const_cast<char*>("linux"), 5) == 0
|| std::strncmp(termtype, const_cast<char*>("con"), 3) == 0 )
2015-12-11 06:04:29 +01:00
linux_terminal = true;
else
linux_terminal = false;
// start terminal detection...
2015-10-07 02:36:38 +02:00
setRawMode();
// Identify the terminal via the answerback-message
2015-10-07 02:36:38 +02:00
new_termtype = parseAnswerbackMsg (new_termtype);
// Identify the terminal via the secondary device attributes (SEC_DA)
2015-10-07 02:36:38 +02:00
new_termtype = parseSecDA (new_termtype);
unsetRawMode();
// ...end of terminal detection
// stop non-blocking stdin
unsetNonBlockingInput();
2015-10-24 00:49:56 +02:00
// Test if the terminal is a xterm
if ( std::strncmp(termtype, const_cast<char*>("xterm"), 5) == 0
|| std::strncmp(termtype, const_cast<char*>("Eterm"), 4) == 0 )
2016-10-09 02:06:06 +02:00
xterm_terminal = true;
2015-10-07 02:36:38 +02:00
else
2016-10-09 02:06:06 +02:00
xterm_terminal = false;
2015-10-29 21:10:50 +01:00
2015-10-24 00:49:56 +02:00
// set the new environment variable TERM
if ( new_termtype )
{
setenv(const_cast<char*>("TERM"), new_termtype, 1);
std::strncpy (termtype, new_termtype, std::strlen(new_termtype)+1);
2015-10-24 00:49:56 +02:00
}
2015-10-07 02:36:38 +02:00
// Initializes variables for the current terminal
init_termcaps();
init_alt_charset();
2015-10-07 02:36:38 +02:00
// init current locale
locale_name = std::setlocale (LC_ALL, "");
locale_name = std::setlocale (LC_NUMERIC, "");
2015-10-07 02:36:38 +02:00
// get XTERM_LOCALE
locale_xterm = std::getenv("XTERM_LOCALE");
2015-10-07 02:36:38 +02:00
// set LC_ALL to XTERM_LOCALE
if ( locale_xterm )
locale_name = std::setlocale (LC_ALL, locale_xterm);
2015-10-07 02:36:38 +02:00
// TeraTerm can not show UTF-8 character
if ( tera_terminal && ! std::strcmp(nl_langinfo(CODESET), "UTF-8") )
locale_name = std::setlocale (LC_ALL, "en_US");
2015-10-07 21:32:30 +02:00
2015-10-07 02:36:38 +02:00
// if locale C => switch from 7bit ascii -> latin1
if ( isatty(stdout_no) && ! std::strcmp(nl_langinfo(CODESET), "ANSI_X3.4-1968") )
locale_name = std::setlocale (LC_ALL, "en_US");
2015-10-07 02:36:38 +02:00
// try to found a meaningful content for locale_name
if ( locale_name )
locale_name = std::setlocale (LC_CTYPE, 0);
2015-10-07 02:36:38 +02:00
else
{
locale_name = std::getenv("LC_ALL");
2015-10-07 02:36:38 +02:00
if ( ! locale_name )
{
locale_name = std::getenv("LC_CTYPE");
2015-10-07 02:36:38 +02:00
if ( ! locale_name )
locale_name = std::getenv("LANG");
2015-10-07 02:36:38 +02:00
}
}
2015-10-07 02:36:38 +02:00
// Fallback to C
if ( ! locale_name )
locale_name = const_cast<char*>("C");
// Detect environment and set encoding
init_encoding();
2015-05-23 13:35:12 +02:00
2015-08-22 18:53:52 +02:00
#ifdef F_HAVE_LIBGPM
2015-05-23 13:35:12 +02:00
// Enable the linux general purpose mouse (gpm) server
gpm_mouse_enabled = enableGpmMouse();
2015-05-23 13:35:12 +02:00
#endif
// xterm mouse support
if ( tcap[fc::t_key_mouse].string != 0 )
2015-05-23 13:35:12 +02:00
{
mouse_support = true;
enableXTermMouse();
}
2015-10-07 21:32:30 +02:00
// enter 'keyboard_transmit' mode
if ( tcap[fc::t_keypad_xmit].string )
2015-05-23 13:35:12 +02:00
{
putstring (tcap[fc::t_keypad_xmit].string);
std::fflush(stdout);
2015-05-23 13:35:12 +02:00
}
2015-10-07 21:32:30 +02:00
// save current cursor position
if ( tcap[fc::t_save_cursor].string )
2015-05-23 13:35:12 +02:00
{
putstring (tcap[fc::t_save_cursor].string);
std::fflush(stdout);
2015-05-23 13:35:12 +02:00
}
2015-10-07 21:32:30 +02:00
// saves the screen and the cursor position
if ( tcap[fc::t_enter_ca_mode].string )
2015-05-23 13:35:12 +02:00
{
putstring (tcap[fc::t_enter_ca_mode].string);
std::fflush(stdout);
2015-05-23 13:35:12 +02:00
}
2015-10-07 21:32:30 +02:00
// enable alternate charset
if ( tcap[fc::t_enable_acs].string )
2015-05-23 13:35:12 +02:00
{
putstring (tcap[fc::t_enable_acs].string);
std::fflush(stdout);
2015-05-23 13:35:12 +02:00
}
setXTermCursorStyle(fc::blinking_underline);
setXTermMouseBackground("rgb:ffff/ffff/ffff");
setXTermMouseForeground ("rgb:0000/0000/0000");
2015-05-23 13:35:12 +02:00
if ( ! gnome_terminal )
2015-10-23 23:57:00 +02:00
setXTermCursorColor("rgb:ffff/ffff/ffff");
if ( ! (mintty_terminal || rxvt_terminal || screen_terminal) )
2015-05-23 13:35:12 +02:00
{
2016-01-17 02:57:08 +01:00
// mintty and rxvt can't reset these settings
2015-05-23 13:35:12 +02:00
setXTermBackground("rgb:8080/a4a4/ecec");
setXTermForeground("rgb:0000/0000/0000");
setXTermHighlightBackground("rgb:8686/8686/8686");
2015-05-23 13:35:12 +02:00
}
2015-09-22 04:18:20 +02:00
setRawMode();
2015-05-23 13:35:12 +02:00
2016-10-09 02:06:06 +02:00
if ( (xterm_terminal || urxvt_terminal) && ! rxvt_terminal )
2015-05-23 13:35:12 +02:00
{
setNonBlockingInput();
xterm_font = new FString(getXTermFont());
xterm_title = new FString(getXTermTitle());
unsetNonBlockingInput();
}
if ( (max_color == 8)
&& ( linux_terminal
|| cygwin_terminal
|| putty_terminal
|| tera_terminal
|| rxvt_terminal) )
{
2015-05-23 13:35:12 +02:00
max_color = 16;
}
2015-05-23 13:35:12 +02:00
if ( linux_terminal && openConsole() == 0 )
{
if ( isConsole() )
if ( setBlinkAsIntensity(true) != 0 )
2015-05-23 13:35:12 +02:00
max_color = 8;
2015-05-23 13:35:12 +02:00
closeConsole();
setConsoleCursor(fc::underscore_cursor, true);
2015-05-23 13:35:12 +02:00
}
if ( linux_terminal && getFramebuffer_bpp() >= 4 )
max_color = 16;
if ( kde_konsole )
setKDECursor(fc::UnderlineCursor);
2015-10-29 21:10:50 +01:00
if ( max_color >= 16
&& ! cygwin_terminal
&& ! kde_konsole
&& ! tera_terminal )
2015-05-23 13:35:12 +02:00
{
resetColorMap();
saveColorMap();
2015-10-24 00:49:56 +02:00
setPalette (fc::Black, 0x00, 0x00, 0x00);
2015-08-09 23:39:12 +02:00
setPalette (fc::Blue, 0x22, 0x22, 0xb2);
2015-11-07 23:16:09 +01:00
setPalette (fc::Green, 0x18, 0xb2, 0x18);
2015-05-23 13:35:12 +02:00
setPalette (fc::Cyan, 0x4a, 0x4a, 0xe4);
setPalette (fc::Red, 0xb2, 0x18, 0x18);
2015-11-07 23:16:09 +01:00
setPalette (fc::Magenta, 0xb2, 0x18, 0xb2);
setPalette (fc::Brown, 0xe8, 0x87, 0x1f);
2015-05-23 13:35:12 +02:00
setPalette (fc::LightGray, 0xbc, 0xbc, 0xbc);
setPalette (fc::DarkGray, 0x50, 0x50, 0x50);
setPalette (fc::LightBlue, 0x80, 0xa4, 0xec);
2015-11-07 23:16:09 +01:00
setPalette (fc::LightGreen, 0x54, 0xff, 0x54);
setPalette (fc::LightCyan, 0x49, 0xc9, 0xe3);
2015-05-23 13:35:12 +02:00
setPalette (fc::LightRed, 0xff, 0x54, 0x54);
2015-11-07 23:16:09 +01:00
setPalette (fc::LightMagenta, 0xff, 0x54, 0xff);
2015-05-23 13:35:12 +02:00
setPalette (fc::Yellow, 0xff, 0xff, 0x54);
setPalette (fc::White, 0xff, 0xff, 0xff);
}
2016-01-17 02:57:08 +01:00
// set 200 Hz beep (100 ms)
2015-05-23 13:35:12 +02:00
setBeep(200, 100);
signal(SIGTERM, FTerm::signal_handler); // Termination signal
signal(SIGQUIT, FTerm::signal_handler); // Quit from keyboard (Ctrl-\)
signal(SIGINT, FTerm::signal_handler); // Keyboard interrupt (Ctrl-C)
signal(SIGABRT, FTerm::signal_handler); // Abort signal from abort(3)
signal(SIGILL, FTerm::signal_handler); // Illegal Instruction
signal(SIGSEGV, FTerm::signal_handler); // Invalid memory reference
signal(SIGWINCH, FTerm::signal_handler); // Window resize signal
}
//----------------------------------------------------------------------
void FTerm::finish()
{
// set default signal handler
signal(SIGWINCH, SIG_DFL); // Window resize signal
signal(SIGSEGV, SIG_DFL); // Invalid memory reference
signal(SIGILL, SIG_DFL); // Illegal Instruction
signal(SIGABRT, SIG_DFL); // Abort signal from abort(3)
signal(SIGINT, SIG_DFL); // Keyboard interrupt (Ctrl-C)
signal(SIGQUIT, SIG_DFL); // Quit from keyboard (Ctrl-\)
signal(SIGTERM, SIG_DFL); // Termination signal
2016-10-09 02:06:06 +02:00
if ( xterm_title && xterm_terminal && ! rxvt_terminal )
2015-05-23 13:35:12 +02:00
setXTermTitle (*xterm_title);
2015-09-22 04:18:20 +02:00
setCookedMode(); // leave raw mode
2015-05-23 13:35:12 +02:00
2015-10-07 21:32:30 +02:00
// turn off all attributes
if ( tcap[fc::t_exit_attribute_mode].string )
2015-05-23 13:35:12 +02:00
{
putstring (tcap[fc::t_exit_attribute_mode].string);
std::fflush(stdout);
2015-05-23 13:35:12 +02:00
}
2015-10-23 23:57:00 +02:00
// turn off pc charset mode
if ( tcap[fc::t_exit_pc_charset_mode].string )
{
putstring (tcap[fc::t_exit_pc_charset_mode].string);
std::fflush(stdout);
}
2015-10-23 23:57:00 +02:00
// reset xterm color settings to default
setXTermCursorColor("rgb:b1b1/b1b1/b1b1");
resetXTermMouseForeground();
resetXTermMouseBackground();
resetXTermCursorColor();
resetXTermForeground();
resetXTermBackground();
2015-11-22 21:41:18 +01:00
resetXTermHighlightBackground();
2015-05-23 13:35:12 +02:00
setXTermCursorStyle(fc::steady_block);
if ( max_color >= 16 && ! (kde_konsole || tera_terminal) )
2015-05-23 13:35:12 +02:00
{
// reset screen settings
setPalette (fc::Cyan, 0x18, 0xb2, 0xb2);
setPalette (fc::LightGray, 0xb2, 0xb2, 0xb2);
setPalette (fc::DarkGray, 0x68, 0x68, 0x68);
setPalette (fc::LightBlue, 0x54, 0x54, 0xff);
setPalette (fc::LightGreen, 0x54, 0xff, 0x54);
2015-10-24 00:49:56 +02:00
resetXTermColors();
2015-05-23 13:35:12 +02:00
resetColorMap();
}
2015-05-23 13:35:12 +02:00
if ( mintty_terminal )
{
// switch to normal escape key mode
2016-01-31 21:06:29 +01:00
putstring (CSI "?7727l");
std::fflush(stdout);
2015-05-23 13:35:12 +02:00
}
2015-05-23 13:35:12 +02:00
if ( linux_terminal )
{
setBlinkAsIntensity (false);
setConsoleCursor(fc::default_cursor, false);
2015-05-23 13:35:12 +02:00
}
2015-05-23 13:35:12 +02:00
if ( kde_konsole )
setKDECursor(fc::BlockCursor);
resetBeep();
if ( linux_terminal
&& background_color_erase
&& tcap[fc::t_clear_screen].string )
2015-05-23 13:35:12 +02:00
{
int rows = term->getHeight();
putstring (tcap[fc::t_clear_screen].string, rows);
2015-05-23 13:35:12 +02:00
}
if ( mouse_support )
disableXTermMouse();
2015-08-22 18:53:52 +02:00
#ifdef F_HAVE_LIBGPM
2015-05-23 13:35:12 +02:00
if ( gpm_mouse_enabled )
disableGpmMouse(); // Disable gpm server
2015-05-23 13:35:12 +02:00
#endif
2015-10-07 21:32:30 +02:00
// restores the screen and the cursor position
if ( tcap[fc::t_exit_ca_mode].string )
2015-05-23 13:35:12 +02:00
{
putstring (tcap[fc::t_exit_ca_mode].string);
std::fflush(stdout);
2015-05-23 13:35:12 +02:00
}
2015-10-07 21:32:30 +02:00
// restore cursor to position of last save_cursor
if ( tcap[fc::t_restore_cursor].string )
2015-05-23 13:35:12 +02:00
{
putstring (tcap[fc::t_restore_cursor].string);
std::fflush(stdout);
2015-05-23 13:35:12 +02:00
}
2015-10-07 21:32:30 +02:00
// leave 'keyboard_transmit' mode
if ( tcap[fc::t_keypad_local].string )
2015-05-23 13:35:12 +02:00
{
putstring (tcap[fc::t_keypad_local].string);
std::fflush(stdout);
2015-05-23 13:35:12 +02:00
}
if ( linux_terminal && utf8_console )
setUTF8(true);
if ( NewFont || VGAFont )
setOldFont();
else
{
if ( screen_font.data != 0 )
delete[] screen_font.data;
if ( screen_unicode_map.entries != 0 )
delete[] screen_unicode_map.entries;
2015-05-23 13:35:12 +02:00
}
if ( encoding_set )
delete encoding_set;
2015-05-23 13:35:12 +02:00
if ( vt100_alt_char )
delete vt100_alt_char;
if ( sec_da )
delete sec_da;
if ( answer_back )
delete answer_back;
2015-05-23 13:35:12 +02:00
if ( xterm_title )
delete xterm_title;
2015-05-23 13:35:12 +02:00
if ( xterm_font )
delete xterm_font;
if ( mouse )
delete mouse;
if ( term )
delete term;
if ( opti_attr )
delete opti_attr;
if ( opti_move )
delete opti_move;
2015-05-23 13:35:12 +02:00
}
//----------------------------------------------------------------------
inline uInt FTerm::charEncode (uInt c)
2015-05-23 13:35:12 +02:00
{
return charEncode (c, Encoding);
}
2015-10-07 21:32:30 +02:00
//----------------------------------------------------------------------
uInt FTerm::charEncode (uInt c, fc::encoding enc)
{
for (uInt i=0; i <= uInt(lastCharItem); i++)
2015-05-23 13:35:12 +02:00
{
if ( character[i][0] == c )
{
c = character[i][enc];
break;
}
2015-05-23 13:35:12 +02:00
}
2015-05-23 13:35:12 +02:00
return c;
}
//----------------------------------------------------------------------
uInt FTerm::cp437_to_unicode (uChar c)
{
register uInt ucs = uInt(c);
for (register uInt i=0; i <= lastCP437Item; i++)
{
if ( cp437_to_ucs[i][0] == c ) // found
{
ucs = cp437_to_ucs[i][1];
break;
}
}
2015-05-23 13:35:12 +02:00
return ucs;
}
// protected methods of FTerm
//----------------------------------------------------------------------
bool FTerm::charEncodable (uInt c)
{
uInt ch = charEncode(c);
return bool(ch > 0 && ch != c);
}
2016-10-09 02:06:06 +02:00
// public methods of FTerm
2016-10-09 02:06:06 +02:00
//----------------------------------------------------------------------
bool FTerm::setVGAFont()
2016-10-09 02:06:06 +02:00
{
if ( VGAFont )
return VGAFont;
2015-05-23 13:35:12 +02:00
if ( gnome_terminal
|| kde_konsole
|| putty_terminal
|| tera_terminal
|| cygwin_terminal
|| mintty_terminal )
return false;
2015-05-23 13:35:12 +02:00
VGAFont = true;
2015-05-23 13:35:12 +02:00
if ( xterm_terminal || screen_terminal || osc_support )
2015-05-23 13:35:12 +02:00
{
// Set font in xterm to vga
oscPrefix();
putstring (OSC "50;vga" BEL);
oscPostfix();
std::fflush(stdout);
NewFont = false;
pc_charset_console = true;
Encoding = fc::PC;
if ( xterm_terminal && utf8_console )
Fputchar = &FTerm::putchar_UTF8;
else
Fputchar = &FTerm::putchar_ASCII;
2015-05-23 13:35:12 +02:00
}
else if ( linux_terminal )
2015-05-23 13:35:12 +02:00
{
if ( openConsole() == 0 )
{
if ( isConsole() )
{
// standard vga font 8x16
int ret = setScreenFont(__8x16std, 256, 8, 16);
2015-05-23 13:35:12 +02:00
if ( ret != 0 )
VGAFont = false;
2015-05-23 13:35:12 +02:00
// unicode character mapping
struct unimapdesc unimap;
unimap.entry_ct = uChar ( sizeof(unicode_cp437_pairs)
/ sizeof(unipair) );
unimap.entries = &unicode_cp437_pairs[0];
setUnicodeMap(&unimap);
}
else
VGAFont = false;
getTermSize();
closeConsole();
}
else
VGAFont = false;
pc_charset_console = true;
Encoding = fc::PC;
Fputchar = &FTerm::putchar_ASCII;
}
else
VGAFont = false;
return VGAFont;
2015-05-23 13:35:12 +02:00
}
//----------------------------------------------------------------------
bool FTerm::setNewFont()
2015-05-23 13:35:12 +02:00
{
if ( NewFont )
return true;
if ( gnome_terminal
|| kde_konsole
|| putty_terminal
|| tera_terminal
|| cygwin_terminal
|| mintty_terminal )
return false;
2015-05-23 13:35:12 +02:00
if ( xterm_terminal || screen_terminal || urxvt_terminal || osc_support )
{
NewFont = true;
// Set font in xterm to 8x16graph
oscPrefix();
putstring (OSC "50;8x16graph" BEL);
oscPostfix();
std::fflush(stdout);
pc_charset_console = true;
Encoding = fc::PC;
2015-08-09 23:39:12 +02:00
if ( xterm_terminal && utf8_console )
Fputchar = &FTerm::putchar_UTF8;
else
Fputchar = &FTerm::putchar_ASCII;
}
else if ( linux_terminal )
2015-05-23 13:35:12 +02:00
{
NewFont = true;
2015-05-23 13:35:12 +02:00
if ( openConsole() == 0 )
{
if ( isConsole() )
2015-05-23 13:35:12 +02:00
{
struct unimapdesc unimap;
int ret;
// Set the graphical font 8x16
ret = setScreenFont(__8x16graph, 256, 8, 16);
2015-08-09 23:39:12 +02:00
if ( ret != 0 )
NewFont = false;
2015-05-23 13:35:12 +02:00
// unicode character mapping
unimap.entry_ct = uInt16 ( sizeof(unicode_cp437_pairs)
/ sizeof(unipair) );
unimap.entries = &unicode_cp437_pairs[0];
setUnicodeMap(&unimap);
}
getTermSize();
closeConsole();
2015-05-23 13:35:12 +02:00
}
pc_charset_console = true;
Encoding = fc::PC;
Fputchar = &FTerm::putchar_ASCII; // function pointer
2015-05-23 13:35:12 +02:00
}
else
NewFont = false;
2015-05-23 13:35:12 +02:00
return NewFont;
2016-10-09 02:06:06 +02:00
}
2015-05-23 13:35:12 +02:00
//----------------------------------------------------------------------
bool FTerm::setOldFont()
2015-05-23 13:35:12 +02:00
{
bool retval;
2015-05-23 13:35:12 +02:00
if ( ! (NewFont || VGAFont) )
return false;
2015-05-23 13:35:12 +02:00
retval = \
NewFont = \
VGAFont = false;
2015-05-23 13:35:12 +02:00
if ( xterm_terminal || screen_terminal || urxvt_terminal || osc_support )
2015-05-23 13:35:12 +02:00
{
if ( xterm_font && xterm_font->getLength() > 2 )
{
// restore saved xterm font
oscPrefix();
putstringf (OSC "50;%s" BEL, xterm_font->c_str() );
oscPostfix();
}
else
2015-05-23 13:35:12 +02:00
{
// Set font in xterm to vga
oscPrefix();
putstring (OSC "50;vga" BEL);
oscPostfix();
}
std::fflush(stdout);
retval = true;
}
else if ( linux_terminal )
{
if ( openConsole() == 0 )
{
if ( isConsole() )
2015-05-23 13:35:12 +02:00
{
if ( screen_font.data )
{
int ret = setScreenFont ( screen_font.data
, screen_font.charcount
, screen_font.width
, screen_font.height
, true );
delete[] screen_font.data;
if ( ret == 0 )
retval = true;
}
if ( screen_unicode_map.entries )
{
setUnicodeMap (&screen_unicode_map);
delete[] screen_unicode_map.entries;
}
}
getTermSize();
closeConsole();
2015-05-23 13:35:12 +02:00
}
}
return retval;
2015-05-23 13:35:12 +02:00
}
//----------------------------------------------------------------------
void FTerm::setConsoleCursor (fc::consoleCursorStyle style, bool hidden)
{
// Set cursor style in linux console
if ( ! linux_terminal )
return;
console_cursor_style = style;
if ( hidden )
return;
putstringf (CSI "?%dc", style);
std::fflush(stdout);
}
2015-05-23 13:35:12 +02:00
//----------------------------------------------------------------------
char* FTerm::moveCursor (int xold, int yold, int xnew, int ynew)
2015-05-23 13:35:12 +02:00
{
// returns the cursor move string
if ( cursor_optimisation )
return opti_move->moveCursor (xold, yold, xnew, ynew);
else
return tgoto(tcap[fc::t_cursor_address].string, xnew, ynew);
}
2015-05-23 13:35:12 +02:00
//----------------------------------------------------------------------
char* FTerm::enableCursor()
2015-05-23 13:35:12 +02:00
{
char*& vs = tcap[fc::t_cursor_visible].string;
char*& ve = tcap[fc::t_cursor_normal].string;
if ( ve )
return ve;
else if ( vs )
return vs;
return 0;
2015-05-23 13:35:12 +02:00
}
//----------------------------------------------------------------------
char* FTerm::disableCursor()
2015-05-23 13:35:12 +02:00
{
char*& vi = tcap[fc::t_cursor_invisible].string;
if ( vi )
return vi;
return 0;
2015-05-23 13:35:12 +02:00
}
//----------------------------------------------------------------------
void FTerm::getTermSize()
{
struct winsize win_size;
int ret;
if ( fd_tty < 0 )
return;
ret = ioctl (fd_tty, TIOCGWINSZ, &win_size);
if ( ret != 0 || win_size.ws_col == 0 || win_size.ws_row == 0 )
{
char* str;
2016-07-31 20:25:25 +02:00
term->setPos(1,1);
str = std::getenv("COLUMNS");
term->setWidth(str ? std::atoi(str) : 80);
str = std::getenv("LINES");
term->setHeight(str ? std::atoi(str) : 25);
2015-05-23 13:35:12 +02:00
}
else
{
term->setRect(1, 1, win_size.ws_col, win_size.ws_row);
}
opti_move->setTermSize (term->getWidth(), term->getHeight());
2015-05-23 13:35:12 +02:00
}
//----------------------------------------------------------------------
void FTerm::setTermSize (int term_width, int term_height)
{
// Set xterm size to {term_width} x {term_height}
2016-10-09 02:06:06 +02:00
if ( xterm_terminal )
2015-05-23 13:35:12 +02:00
{
2016-01-31 21:06:29 +01:00
putstringf (CSI "8;%d;%dt", term_height, term_width);
std::fflush(stdout);
2015-05-23 13:35:12 +02:00
}
}
//----------------------------------------------------------------------
void FTerm::setKDECursor (fc::kdeKonsoleCursorShape style)
2015-05-23 13:35:12 +02:00
{
// Set cursor style in KDE konsole
if ( kde_konsole )
2015-05-23 13:35:12 +02:00
{
oscPrefix();
putstringf (OSC "50;CursorShape=%d" BEL, style);
oscPostfix();
std::fflush(stdout);
2015-05-23 13:35:12 +02:00
}
}
//----------------------------------------------------------------------
FString FTerm::getXTermFont()
{
2015-10-17 19:40:43 +02:00
FString font("");
2016-10-09 02:06:06 +02:00
if ( xterm_terminal || screen_terminal || osc_support )
2015-05-23 13:35:12 +02:00
{
if ( raw_mode && non_blocking_stdin )
2015-09-30 22:39:02 +02:00
{
int n;
char temp[150] = {};
oscPrefix();
putstring (OSC "50;?" BEL); // get font
oscPostfix();
std::fflush(stdout);
usleep(150000); // wait 150 ms
// read the terminal answer
n = int(read(fileno(stdin), &temp, sizeof(temp)-1));
// BEL + '\0' = string terminator
if ( n >= 6 && temp[n-1] == BEL[0] && temp[n] == '\0' )
{
temp[n-1] = '\0';
font = static_cast<char*>(temp + 5);
}
2015-09-30 22:39:02 +02:00
}
2015-05-23 13:35:12 +02:00
}
2015-05-23 13:35:12 +02:00
return font;
}
//----------------------------------------------------------------------
FString FTerm::getXTermTitle()
{
2015-10-17 19:40:43 +02:00
FString title("");
2015-05-23 13:35:12 +02:00
if ( kde_konsole )
return title;
if ( raw_mode && non_blocking_stdin )
{
int n;
2015-09-30 22:39:02 +02:00
char temp[512] = {};
2016-01-31 21:06:29 +01:00
putstring (CSI "21t"); // get title
std::fflush(stdout);
2015-05-23 13:35:12 +02:00
usleep(150000); // wait 150 ms
// read the terminal answer
2015-05-23 13:35:12 +02:00
n = int(read(fileno(stdin), &temp, sizeof(temp)-1));
2015-10-17 19:40:43 +02:00
// Esc + \ = OSC string terminator
if ( n >= 5 && temp[n-1] == '\\' && temp[n-2] == ESC[0] )
2015-09-30 22:39:02 +02:00
{
2015-05-23 13:35:12 +02:00
temp[n-2] = '\0';
title = static_cast<char*>(temp + 3);
2015-09-30 22:39:02 +02:00
}
2015-05-23 13:35:12 +02:00
}
2015-05-23 13:35:12 +02:00
return title;
}
//----------------------------------------------------------------------
void FTerm::setXTermCursorStyle (fc::xtermCursorStyle style)
2015-05-23 13:35:12 +02:00
{
// Set the xterm cursor style
2016-10-09 02:06:06 +02:00
if ( (xterm_terminal || mintty_terminal) && ! (gnome_terminal || kde_konsole) )
2015-05-23 13:35:12 +02:00
{
2016-01-31 21:06:29 +01:00
putstringf (CSI "%d q", style);
std::fflush(stdout);
2015-05-23 13:35:12 +02:00
}
}
//----------------------------------------------------------------------
void FTerm::setXTermTitle (const FString& title)
{
// Set the xterm title
2016-10-09 02:06:06 +02:00
if ( xterm_terminal || screen_terminal
|| mintty_terminal || putty_terminal
|| osc_support )
2015-05-23 13:35:12 +02:00
{
oscPrefix();
2016-01-31 21:06:29 +01:00
putstringf (OSC "0;%s" BEL, title.c_str());
oscPostfix();
std::fflush(stdout);
2015-05-23 13:35:12 +02:00
}
}
//----------------------------------------------------------------------
void FTerm::setXTermForeground (const FString& fg)
{
// Set the VT100 text foreground color
2016-10-09 02:06:06 +02:00
if ( xterm_terminal || screen_terminal
|| mintty_terminal || mlterm_terminal
|| osc_support )
2015-05-23 13:35:12 +02:00
{
oscPrefix();
2016-01-31 21:06:29 +01:00
putstringf (OSC "10;%s" BEL, fg.c_str());
oscPostfix();
std::fflush(stdout);
2015-05-23 13:35:12 +02:00
}
}
//----------------------------------------------------------------------
void FTerm::setXTermBackground (const FString& bg)
{
// Set the VT100 text background color
2016-10-09 02:06:06 +02:00
if ( xterm_terminal || screen_terminal
|| mintty_terminal || mlterm_terminal
|| osc_support )
2015-05-23 13:35:12 +02:00
{
oscPrefix();
2016-01-31 21:06:29 +01:00
putstringf (OSC "11;%s" BEL, bg.c_str());
oscPostfix();
std::fflush(stdout);
2015-05-23 13:35:12 +02:00
}
}
//----------------------------------------------------------------------
2015-10-23 23:57:00 +02:00
void FTerm::setXTermCursorColor (const FString& cc)
2015-05-23 13:35:12 +02:00
{
// Set the text cursor color
2016-10-09 02:06:06 +02:00
if ( xterm_terminal || screen_terminal
|| mintty_terminal || urxvt_terminal
|| osc_support )
2015-05-23 13:35:12 +02:00
{
oscPrefix();
2016-01-31 21:06:29 +01:00
putstringf (OSC "12;%s" BEL, cc.c_str());
oscPostfix();
std::fflush(stdout);
2015-05-23 13:35:12 +02:00
}
}
//----------------------------------------------------------------------
void FTerm::setXTermMouseForeground (const FString& mfg)
{
// Set the mouse foreground color
2016-10-09 02:06:06 +02:00
if ( xterm_terminal || screen_terminal || urxvt_terminal || osc_support )
2015-05-23 13:35:12 +02:00
{
oscPrefix();
2016-01-31 21:06:29 +01:00
putstringf (OSC "13;%s" BEL, mfg.c_str());
oscPostfix();
std::fflush(stdout);
2015-05-23 13:35:12 +02:00
}
}
//----------------------------------------------------------------------
void FTerm::setXTermMouseBackground (const FString& mbg)
{
// Set the mouse background color
2016-10-09 02:06:06 +02:00
if ( xterm_terminal || screen_terminal || osc_support )
2015-05-23 13:35:12 +02:00
{
oscPrefix();
2016-01-31 21:06:29 +01:00
putstringf (OSC "14;%s" BEL, mbg.c_str());
oscPostfix();
std::fflush(stdout);
2015-05-23 13:35:12 +02:00
}
}
//----------------------------------------------------------------------
void FTerm::setXTermHighlightBackground (const FString& hbg)
{
// Set the highlight background color
2016-10-09 02:06:06 +02:00
if ( xterm_terminal || screen_terminal || urxvt_terminal || osc_support )
2015-05-23 13:35:12 +02:00
{
oscPrefix();
2016-01-31 21:06:29 +01:00
putstringf (OSC "17;%s" BEL, hbg.c_str());
oscPostfix();
std::fflush(stdout);
2015-05-23 13:35:12 +02:00
}
}
2015-10-24 00:49:56 +02:00
//----------------------------------------------------------------------
void FTerm::resetXTermColors()
{
// Reset the entire color table
2016-10-09 02:06:06 +02:00
if ( xterm_terminal || screen_terminal || osc_support )
2015-10-24 00:49:56 +02:00
{
oscPrefix();
2016-01-31 21:06:29 +01:00
putstringf (OSC "104" BEL);
oscPostfix();
std::fflush(stdout);
2015-10-24 00:49:56 +02:00
}
}
2015-05-23 13:35:12 +02:00
//----------------------------------------------------------------------
void FTerm::resetXTermForeground()
{
// Reset the VT100 text foreground color
2016-10-09 02:06:06 +02:00
if ( xterm_terminal || screen_terminal || osc_support )
2015-05-23 13:35:12 +02:00
{
oscPrefix();
2016-01-31 21:06:29 +01:00
putstring (OSC "110" BEL);
oscPostfix();
std::fflush(stdout);
2015-05-23 13:35:12 +02:00
}
}
//----------------------------------------------------------------------
void FTerm::resetXTermBackground()
{
// Reset the VT100 text background color
2016-10-09 02:06:06 +02:00
if ( xterm_terminal || screen_terminal || osc_support )
2015-05-23 13:35:12 +02:00
{
oscPrefix();
2016-01-31 21:06:29 +01:00
putstring (OSC "111" BEL);
oscPostfix();
std::fflush(stdout);
2015-05-23 13:35:12 +02:00
}
}
//----------------------------------------------------------------------
2015-10-23 23:57:00 +02:00
void FTerm::resetXTermCursorColor()
2015-05-23 13:35:12 +02:00
{
// Reset the text cursor color
2016-10-09 02:06:06 +02:00
if ( xterm_terminal || screen_terminal || osc_support )
2015-05-23 13:35:12 +02:00
{
oscPrefix();
2016-01-31 21:06:29 +01:00
putstring (OSC "112" BEL);
oscPostfix();
std::fflush(stdout);
2015-05-23 13:35:12 +02:00
}
}
//----------------------------------------------------------------------
void FTerm::resetXTermMouseForeground()
{
// Reset the mouse foreground color
2016-10-09 02:06:06 +02:00
if ( xterm_terminal || screen_terminal || osc_support )
2015-05-23 13:35:12 +02:00
{
oscPrefix();
2016-01-31 21:06:29 +01:00
putstring (OSC "113" BEL);
oscPostfix();
std::fflush(stdout);
2015-05-23 13:35:12 +02:00
}
}
//----------------------------------------------------------------------
void FTerm::resetXTermMouseBackground()
{
// Reset the mouse background color
2016-10-09 02:06:06 +02:00
if ( xterm_terminal || screen_terminal || osc_support )
2015-05-23 13:35:12 +02:00
{
oscPrefix();
2016-01-31 21:06:29 +01:00
putstring (OSC "114" BEL);
oscPostfix();
std::fflush(stdout);
2015-05-23 13:35:12 +02:00
}
}
2015-11-22 21:41:18 +01:00
//----------------------------------------------------------------------
void FTerm::resetXTermHighlightBackground()
{
// Reset the highlight background color
2016-10-09 02:06:06 +02:00
if ( xterm_terminal || screen_terminal || urxvt_terminal || osc_support )
2015-11-22 21:41:18 +01:00
{
oscPrefix();
2016-01-31 21:06:29 +01:00
putstringf (OSC "117" BEL);
oscPostfix();
std::fflush(stdout);
2015-11-22 21:41:18 +01:00
}
}
2015-05-23 13:35:12 +02:00
//----------------------------------------------------------------------
void FTerm::saveColorMap()
{
// ioctl (0, GIO_CMAP, &map);
}
//----------------------------------------------------------------------
void FTerm::resetColorMap()
{
char*& op = tcap[fc::t_orig_pair].string;
char*& oc = tcap[fc::t_orig_colors].string;
2015-10-07 21:32:30 +02:00
if ( op )
putstring (op);
else if ( oc )
putstring (oc);
2015-05-23 13:35:12 +02:00
/*else
{
dacreg CurrentColors[16] =
{
{0x00, 0x00, 0x00}, {0xAA, 0x00, 0x00},
{0x00, 0xAA, 0x00}, {0xAA, 0x55, 0x00},
{0x00, 0x00, 0xAA}, {0xAA, 0x00, 0xAA},
{0x00, 0xAA, 0xAA}, {0xAA, 0xAA, 0xAA},
{0x55, 0x55, 0x55}, {0xFF, 0x55, 0x55},
{0x55, 0xFF, 0x55}, {0xFF, 0xFF, 0x55},
{0x55, 0x55, 0xFF}, {0xFF, 0x55, 0xFF},
{0x55, 0xFF, 0xFF}, {0xFF, 0xFF, 0xFF}
};
for (int x=0; x<16; x++)
{
map.d[x].red = CurrentColors[x].red;
map.d[x].green = CurrentColors[x].green;
map.d[x].blue = CurrentColors[x].blue;
}
ioctl (0, PIO_CMAP, &map);
}*/
std::fflush(stdout);
2015-05-23 13:35:12 +02:00
}
//----------------------------------------------------------------------
void FTerm::setPalette (short index, int r, int g, int b)
2015-05-23 13:35:12 +02:00
{
char*& Ic = tcap[fc::t_initialize_color].string;
char*& Ip = tcap[fc::t_initialize_pair].string;
2015-10-07 21:32:30 +02:00
index = FOptiAttr::vga2ansi(index);
2015-05-23 13:35:12 +02:00
2015-10-07 21:32:30 +02:00
if ( Ic || Ip )
2015-05-23 13:35:12 +02:00
{
int rr, gg, bb;
2015-10-07 21:32:30 +02:00
const char* color_str = "";
2015-05-23 13:35:12 +02:00
rr = (r * 1001) / 256;
gg = (g * 1001) / 256;
bb = (b * 1001) / 256;
2015-10-07 21:32:30 +02:00
if ( Ic )
color_str = tparm(Ic, index, rr, gg, bb, 0, 0, 0, 0, 0);
else if ( Ip )
color_str = tparm(Ip, index, 0, 0, 0, rr, gg, bb, 0, 0);
2015-10-07 21:32:30 +02:00
putstring (color_str);
2015-05-23 13:35:12 +02:00
}
else if ( linux_terminal )
{
/* // direct vga-register set
if ( r>=0 && r<256
&& g>=0 && g<256
&& b>=0 && b<256 )
{
map.d[index].red = r;
map.d[index].green = g;
map.d[index].blue = b;
}
ioctl (0, PIO_CMAP, &map); */
}
std::fflush(stdout);
2015-05-23 13:35:12 +02:00
}
//----------------------------------------------------------------------
void FTerm::xtermMouse (bool on)
{
// activate/deactivate the xterm mouse support
2015-05-23 13:35:12 +02:00
if ( ! mouse_support )
return;
2015-05-23 13:35:12 +02:00
if ( on )
2016-01-31 21:06:29 +01:00
putstring (CSI "?1001s" // save old highlight mouse tracking
CSI "?1000h" // enable x11 mouse tracking
CSI "?1002h" // enable cell motion mouse tracking
CSI "?1015h" // enable urxvt mouse mode
CSI "?1006h"); // enable SGR mouse mode
2015-05-23 13:35:12 +02:00
else
2016-01-31 21:06:29 +01:00
putstring (CSI "?1006l" // disable SGR mouse mode
CSI "?1015l" // disable urxvt mouse mode
CSI "?1002l" // disable cell motion mouse tracking
CSI "?1000l" // disable x11 mouse tracking
CSI "?1001r"); // restore old highlight mouse tracking
std::fflush(stdout);
2015-05-23 13:35:12 +02:00
}
2015-08-22 18:53:52 +02:00
#ifdef F_HAVE_LIBGPM
2015-05-23 13:35:12 +02:00
//----------------------------------------------------------------------
bool FTerm::gpmMouse (bool on)
{
// activate/deactivate the gpm mouse support
2015-05-23 13:35:12 +02:00
if ( ! linux_terminal )
return false;
if ( openConsole() == 0 )
{
if ( ! isConsole() )
return false;
2015-05-23 13:35:12 +02:00
closeConsole();
}
if ( on )
{
Gpm_Connect conn;
conn.eventMask = uInt16(~GPM_MOVE);
conn.defaultMask = GPM_MOVE;
conn.maxMod = uInt16(~0);
conn.minMod = 0;
Gpm_Open(&conn, 0);
2015-05-23 13:35:12 +02:00
switch ( gpm_fd )
{
case -1:
return false;
2015-09-20 05:44:50 +02:00
2015-05-23 13:35:12 +02:00
case -2:
Gpm_Close();
return false;
2015-09-20 05:44:50 +02:00
default:
break;
2015-05-23 13:35:12 +02:00
}
}
else
{
Gpm_Close();
}
2015-05-23 13:35:12 +02:00
return on;
}
2015-08-22 18:53:52 +02:00
#endif // F_HAVE_LIBGPM
2015-05-23 13:35:12 +02:00
//----------------------------------------------------------------------
void FTerm::setBeep (int Hz, int ms)
2015-05-23 13:35:12 +02:00
{
if ( ! linux_terminal )
return;
2015-05-23 13:35:12 +02:00
// range for frequency: 21-32766
if ( Hz < 21 || Hz > 32766 )
return;
2015-05-23 13:35:12 +02:00
// range for duration: 0-1999
if ( ms < 0 || ms > 1999 )
return;
2016-01-31 21:06:29 +01:00
putstringf ( CSI "10;%d]"
CSI "11;%d]"
, Hz, ms );
std::fflush(stdout);
2015-05-23 13:35:12 +02:00
}
//----------------------------------------------------------------------
void FTerm::resetBeep()
{
if ( ! linux_terminal )
return;
2015-05-23 13:35:12 +02:00
// default frequency: 750 Hz
// default duration: 125 ms
2016-01-31 21:06:29 +01:00
putstring ( CSI "10;750]"
CSI "11;125]" );
std::fflush(stdout);
2015-05-23 13:35:12 +02:00
}
//----------------------------------------------------------------------
void FTerm::beep()
2015-05-23 13:35:12 +02:00
{
if ( tcap[fc::t_bell].string )
2015-05-23 13:35:12 +02:00
{
putstring (tcap[fc::t_bell].string);
std::fflush(stdout);
2015-05-23 13:35:12 +02:00
}
}
//----------------------------------------------------------------------
void FTerm::setEncoding (std::string enc)
2015-05-23 13:35:12 +02:00
{
std::map<std::string,fc::encoding>::const_iterator it;
// available encodings: "UTF8", "VT100", "PC" and "ASCII"
it = encoding_set->find(enc);
if ( it != encoding_set->end() ) // found
{
Encoding = it->second;
assert ( Encoding == fc::UTF8
|| Encoding == fc::VT100
|| Encoding == fc::PC
|| Encoding == fc::ASCII );
// set the new Fputchar function pointer
switch ( int(Encoding) )
{
case fc::UTF8:
Fputchar = &FTerm::putchar_UTF8;
break;
2015-05-23 13:35:12 +02:00
case fc::VT100:
case fc::PC:
2016-10-09 02:06:06 +02:00
if ( xterm_terminal && utf8_console )
Fputchar = &FTerm::putchar_UTF8;
// fall through
2015-05-23 13:35:12 +02:00
case fc::ASCII:
default:
Fputchar = &FTerm::putchar_ASCII;
}
}
}
//----------------------------------------------------------------------
std::string FTerm::getEncoding()
{
std::map<std::string,fc::encoding>::const_iterator it, end;
end = encoding_set->end();
for (it = encoding_set->begin(); it != end; ++it )
if ( it->second == Encoding )
return it->first;
2015-05-23 13:35:12 +02:00
return "";
}
//----------------------------------------------------------------------
bool FTerm::setNonBlockingInput (bool on)
2015-05-23 13:35:12 +02:00
{
if ( on == non_blocking_stdin )
return non_blocking_stdin;
if ( on ) // make stdin non-blocking
{
stdin_status_flags |= O_NONBLOCK;
2015-09-30 22:39:02 +02:00
if ( fcntl (stdin_no, F_SETFL, stdin_status_flags) != -1 )
non_blocking_stdin = true;
2015-05-23 13:35:12 +02:00
}
else
{
stdin_status_flags &= ~O_NONBLOCK;
2015-09-30 22:39:02 +02:00
if ( fcntl (stdin_no, F_SETFL, stdin_status_flags) != -1 )
non_blocking_stdin = false;
2015-05-23 13:35:12 +02:00
}
2015-05-23 13:35:12 +02:00
return non_blocking_stdin;
}
//----------------------------------------------------------------------
bool FTerm::scrollTermForward()
{
if ( tcap[fc::t_scroll_forward].string )
{
putstring (tcap[fc::t_scroll_forward].string);
std::fflush(stdout);
return true;
}
return false;
}
//----------------------------------------------------------------------
bool FTerm::scrollTermReverse()
{
if ( tcap[fc::t_scroll_reverse].string )
{
putstring (tcap[fc::t_scroll_reverse].string);
std::fflush(stdout);
return true;
}
return false;
}
2015-05-23 13:35:12 +02:00
//----------------------------------------------------------------------
bool FTerm::setUTF8 (bool on) // UTF-8 (Unicode)
2015-05-23 13:35:12 +02:00
{
if ( on == utf8_state )
return utf8_state;
if ( on )
utf8_state = true;
else
utf8_state = false;
if ( linux_terminal )
{
if ( on )
2016-01-31 21:06:29 +01:00
putstring (ESC "%G");
2015-05-23 13:35:12 +02:00
else
2016-01-31 21:06:29 +01:00
putstring (ESC "%@");
2015-05-23 13:35:12 +02:00
}
std::fflush(stdout);
2015-05-23 13:35:12 +02:00
return utf8_state;
}
//----------------------------------------------------------------------
bool FTerm::setRawMode (bool on)
2015-05-23 13:35:12 +02:00
{
if ( on == raw_mode )
return raw_mode;
std::fflush(stdout);
2015-05-23 13:35:12 +02:00
if ( on )
{
// Info under: man 3 termios
struct termios t;
tcgetattr (stdin_no, &t);
/* set + unset flags for raw mode */
// input mode
t.c_iflag &= uInt(~(IGNBRK | BRKINT | PARMRK | ISTRIP
| INLCR | IGNCR | ICRNL | IXON));
// output mode
t.c_oflag &= uInt(~OPOST);
// local mode
#if DEBUG
// Exit with ctrl-c only if compiled with "DEBUG" option
t.c_lflag &= uInt(~(ECHO | ECHONL | ICANON | IEXTEN));
#else
// Plus disable signals.
t.c_lflag &= uInt(~(ECHO | ECHONL | ICANON | IEXTEN | ISIG));
#endif
// control mode
t.c_cflag &= uInt(~(CSIZE | PARENB));
t.c_cflag |= uInt(CS8);
// defines the terminal special characters for noncanonical read
t.c_cc[VTIME] = 0; // Timeout in deciseconds
t.c_cc[VMIN] = 1; // Minimum number of characters
// set the new termios settings
tcsetattr (stdin_no, TCSAFLUSH, &t);
raw_mode = true;
}
else
{
// restore termios settings
tcsetattr (stdin_no, TCSAFLUSH, &term_init);
raw_mode = false;
}
2015-05-23 13:35:12 +02:00
return raw_mode;
}
//----------------------------------------------------------------------
FString FTerm::getAnswerbackMsg()
{
FString answerback = "";
if ( raw_mode )
{
2015-10-04 19:01:34 +02:00
ssize_t n;
fd_set ifds;
struct timeval tv;
2015-09-30 22:39:02 +02:00
char temp[10] = {};
FD_ZERO(&ifds);
FD_SET(stdin_no, &ifds);
tv.tv_sec = 0;
tv.tv_usec = 150000; // 150 ms
2015-10-07 21:32:30 +02:00
std::putchar (ENQ[0]); // send enquiry character
std::fflush(stdout);
2015-05-23 13:35:12 +02:00
// read the answerback message
if ( select (stdin_no+1, &ifds, 0, 0, &tv) > 0)
2015-10-01 03:48:58 +02:00
{
n = read(fileno(stdin), &temp, sizeof(temp)-1);
if ( n > 0 )
{
temp[n] = '\0';
answerback = temp;
}
2015-10-01 03:48:58 +02:00
}
2015-05-23 13:35:12 +02:00
}
2015-05-23 13:35:12 +02:00
return answerback;
}
//----------------------------------------------------------------------
FString FTerm::getSecDA()
{
FString sec_da_str = "";
2015-05-23 13:35:12 +02:00
if ( raw_mode )
{
2015-10-04 19:01:34 +02:00
ssize_t n;
fd_set ifds;
struct timeval tv;
2015-09-30 22:39:02 +02:00
char temp[16] = {};
FD_ZERO(&ifds);
FD_SET(stdin_no, &ifds);
tv.tv_sec = 0;
tv.tv_usec = 550000; // 150 ms
2015-05-23 13:35:12 +02:00
// get the secondary device attributes
std::putchar (SECDA[0]);
std::putchar (SECDA[1]);
std::putchar (SECDA[2]);
std::putchar (SECDA[3]);
2015-12-11 06:04:29 +01:00
std::fflush(stdout);
usleep(150000); // min. wait time 150 ms (need for mintty)
2015-05-23 13:35:12 +02:00
// read the answer
if ( select (stdin_no+1, &ifds, 0, 0, &tv) > 0 )
2015-10-01 03:48:58 +02:00
{
n = read(fileno(stdin), &temp, sizeof(temp)-1);
if ( n > 0 )
{
temp[n] = '\0';
sec_da_str = temp;
}
2015-10-01 03:48:58 +02:00
}
2015-05-23 13:35:12 +02:00
}
return sec_da_str;
2015-05-23 13:35:12 +02:00
}
2015-10-07 21:32:30 +02:00
//----------------------------------------------------------------------
void FTerm::putstringf (const char* format, ...)
{
assert ( format != 0 );
char buf[512];
char* buffer;
va_list args;
buffer = buf;
va_start (args, format);
std::vsnprintf (buffer, sizeof(buf), format, args);
2015-10-07 21:32:30 +02:00
va_end (args);
tputs (buffer, 1, std::putchar);
2015-10-07 21:32:30 +02:00
}
//----------------------------------------------------------------------
inline void FTerm::putstring (const char* s, int affcnt)
2015-10-07 21:32:30 +02:00
{
tputs (s, affcnt, std::putchar);
2015-10-07 21:32:30 +02:00
}
2015-05-23 13:35:12 +02:00
//----------------------------------------------------------------------
int FTerm::putchar_ASCII (register int c)
{
if ( std::putchar(char(c)) == EOF )
2015-05-23 13:35:12 +02:00
return 0;
else
return 1;
}
//----------------------------------------------------------------------
int FTerm::putchar_UTF8 (register int c)
{
if (c < 0x80)
{
// 1 Byte (7-bit): 0xxxxxxx
std::putchar (c);
2015-05-23 13:35:12 +02:00
return 1;
}
else if (c < 0x800)
{
// 2 byte (11-bit): 110xxxxx 10xxxxxx
std::putchar (0xc0 | (c >> 6) );
std::putchar (0x80 | (c & 0x3f) );
2015-05-23 13:35:12 +02:00
return 2;
}
else if (c < 0x10000)
{
// 3 byte (16-bit): 1110xxxx 10xxxxxx 10xxxxxx
std::putchar (0xe0 | (c >> 12) );
std::putchar (0x80 | ((c >> 6) & 0x3f) );
std::putchar (0x80 | (c & 0x3f) );
2015-05-23 13:35:12 +02:00
return 3;
}
else if (c < 0x200000)
{
// 4 byte (21-bit): 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
std::putchar (0xf0 | (c >> 18) );
std::putchar (0x80 | ((c >> 12) & 0x3f) );
std::putchar (0x80 | ((c >> 6) & 0x3f) );
std::putchar (0x80 | (c & 0x3f));
2015-05-23 13:35:12 +02:00
return 4;
}
else
return EOF;
}
//----------------------------------------------------------------------
int FTerm::UTF8decode(char* utf8)
{
register int ucs=0;
for (register int i=0; i < int(std::strlen(utf8)); ++i)
2015-05-23 13:35:12 +02:00
{
register uChar ch = uChar(utf8[i]);
if ((ch & 0xc0) == 0x80)
{
// byte 2..4 = 10xxxxxx
ucs = (ucs << 6) | (ch & 0x3f);
}
else if (ch < 128)
{
// byte 1 = 0xxxxxxx (1 byte mapping)
ucs = ch & 0xff;
}
else if ((ch & 0xe0) == 0xc0)
{
// byte 1 = 110xxxxx (2 byte mapping)
ucs = ch & 0x1f;
}
else if ((ch & 0xf0) == 0xe0)
{
// byte 1 = 1110xxxx (3 byte mapping)
ucs = ch & 0x0f;
}
else if ((ch & 0xf8) == 0xf0)
{
// byte 1 = 11110xxx (4 byte mapping)
ucs = ch & 0x07;
}
else
{
// error
ucs = EOF;
}
}
2015-05-23 13:35:12 +02:00
return ucs;
}