finalcut/include/final/fterm.h

730 lines
25 KiB
C
Raw Normal View History

2017-11-04 07:03:53 +01:00
/***********************************************************************
* fterm.h - Base class for terminal detection and control *
* *
* This file is part of the Final Cut widget toolkit *
* *
* Copyright 2012-2017 Markus Gans *
* *
* The Final Cut is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Lesser General Public License *
* as published by the Free Software Foundation; either version 3 of *
* the License, or (at your option) any later version. *
* *
* The Final Cut is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with this program. If not, see *
* <http://www.gnu.org/licenses/>. *
***********************************************************************/
/* Base class
*
*
* 1 1
* FTerm -- - - - FOptiMove
* :
* :
* : 1
* :- - - - FOptiAttr
* :
* :
* : *
* :- - - - FString
* :
* :
* : *
* :- - - - FPoint
* :
* :
* : *
* - - - - FRect
*
*/
2015-05-23 13:35:12 +02:00
#ifndef FTERM_H
#define FTERM_H
2015-05-23 13:35:12 +02:00
#if !defined (USE_FINAL_H) && !defined (COMPILE_FINAL_CUT)
#error "Only <final/final.h> can be included directly."
#endif
#include "final/fconfig.h"
2015-08-22 18:53:52 +02:00
#ifdef F_HAVE_LIBGPM
2015-05-23 13:35:12 +02:00
#include <gpm.h>
#endif
2017-03-26 20:40:04 +02:00
#if defined(__linux__)
2017-09-11 03:06:02 +02:00
#include <linux/fb.h> // Linux framebuffer console
#include <linux/keyboard.h> // need for gpm keyboard modifiers
#include <sys/io.h> // <asm/io.h> is deprecated
2017-03-26 20:40:04 +02:00
#include <sys/kd.h>
#endif
2015-05-23 13:35:12 +02:00
#if defined(__FreeBSD__) || defined(__DragonFly__)
#include <sys/consio.h>
#include <sys/kbio.h>
#endif
#if defined(__NetBSD__) || defined(__OpenBSD__)
#include <sys/time.h>
#include <dev/wscons/wsconsio.h>
#endif
2015-08-09 23:39:12 +02:00
#include <sys/ioctl.h>
#include <sys/stat.h>
2015-05-23 13:35:12 +02:00
#include <fcntl.h>
#include <langinfo.h>
2017-09-11 03:06:02 +02:00
#include <term.h> // termcap
2015-05-23 13:35:12 +02:00
#include <termios.h>
#include <ttyent.h>
2015-05-23 13:35:12 +02:00
#include <unistd.h>
#include <clocale>
2015-05-23 13:35:12 +02:00
#include <cmath>
#include <csignal>
#include <map>
#include <queue>
2017-09-11 03:06:02 +02:00
#include <string>
2015-05-23 13:35:12 +02:00
#include "final/fc.h"
#include "final/fobject.h"
#include "final/foptiattr.h"
#include "final/foptimove.h"
#include "final/fpoint.h"
#include "final/frect.h"
#include "final/fstring.h"
#include "final/ftermcap.h"
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
#undef buttons // from term.h
#endif
//----------------------------------------------------------------------
// class FTerm
//----------------------------------------------------------------------
#pragma pack(push)
#pragma pack(1)
class FTerm
{
2017-09-11 03:06:02 +02:00
public:
// Typedefs
typedef FOptiAttr::char_data char_data;
static struct modifier_key // bit field
{
uChar shift : 1; // 0..1
uChar alt_gr : 1; // 0..1
uChar ctrl : 1; // 0..1
uChar alt : 1; // 0..1
uChar : 4; // padding bits
} mod_key;
// Constructor
explicit FTerm (bool = false);
// Destructor
virtual ~FTerm();
// Accessors
virtual const char* getClassName() const;
static termios getTTY();
static int getLineNumber();
static int getColumnNumber();
static const FString getKeyName (int);
2017-03-26 20:40:04 +02:00
#if defined(__linux__)
2017-09-11 03:06:02 +02:00
static modifier_key& getLinuxModifierKey();
2017-03-26 20:40:04 +02:00
#endif
2017-09-11 03:06:02 +02:00
static char* getTermType();
static char* getTermName();
static int getTabstop();
static int getMaxColor();
#if defined(__linux__)
2017-09-11 03:06:02 +02:00
static fc::linuxConsoleCursorStyle getLinuxConsoleCursorStyle();
#endif
#if defined(__FreeBSD__) || defined(__DragonFly__)
2017-09-11 03:06:02 +02:00
static fc::freebsdConsoleCursorStyle getFreeBSDConsoleCursorStyle();
#endif
2017-03-26 20:40:04 +02:00
#if DEBUG
2017-09-11 03:06:02 +02:00
static const FString& getAnswerbackString();
static const FString& getSecDAString();
2017-03-26 20:40:04 +02:00
#endif
2017-09-11 03:06:02 +02:00
// Inquiries
static bool isKeyTimeout (timeval*, register long);
static bool isNormal (char_data*&);
static bool isRaw();
static bool hasPCcharset();
static bool hasUTF8();
static bool hasVT100();
static bool hasASCII();
static bool isMonochron();
static bool isXTerminal();
static bool isAnsiTerminal();
static bool isRxvtTerminal();
static bool isUrxvtTerminal();
static bool isMltermTerminal();
static bool isPuttyTerminal();
static bool isKdeTerminal();
static bool isGnomeTerminal();
static bool isKtermTerminal();
static bool isTeraTerm();
static bool isCygwinTerminal();
static bool isMinttyTerm();
static bool isLinuxTerm();
static bool isScreenTerm();
static bool isTmuxTerm();
static bool isInputDataPending();
static bool isNewFont();
static bool isUTF8();
static bool isUTF8_linux_terminal();
// Mutators
static bool setCursorOptimisation (bool);
static void setXTermDefaultColors (bool);
#if defined(__linux__)
2017-09-11 03:06:02 +02:00
static void setLinuxConsoleCursorStyle \
(fc::linuxConsoleCursorStyle, bool);
#endif
#if defined(__FreeBSD__) || defined(__DragonFly__)
2017-09-11 03:06:02 +02:00
static void setFreeBSDConsoleCursorStyle \
(fc::freebsdConsoleCursorStyle, bool);
#endif
2017-09-11 03:06:02 +02:00
static void setTTY (const termios&);
static void noHardwareEcho();
static bool setRawMode (bool);
static bool setRawMode();
static bool unsetRawMode();
static bool setCookedMode();
static void disableAltScreen();
static bool setUTF8 (bool);
static bool setUTF8();
static bool unsetUTF8();
static bool setNonBlockingInput (bool);
static bool setNonBlockingInput();
static bool unsetNonBlockingInput();
// Methods
static int parseKeyString (char[], int, timeval*);
static bool& unprocessedInput();
static bool setVGAFont();
static bool setNewFont();
static bool setOldFont();
static char* moveCursor (int, int, int, int);
static void printMoveDurations();
static char* enableCursor();
static char* disableCursor();
static void detectTermSize();
static void setTermSize (int, int);
static void setKDECursor (fc::kdeKonsoleCursorShape);
static const FString* getXTermFont();
static const FString* getXTermTitle();
static const FString getXTermColorName (int);
static void setXTermCursorStyle (fc::xtermCursorStyle);
static void setXTermTitle (const FString&);
static void setXTermForeground (const FString&);
static void setXTermBackground (const FString&);
static void setXTermCursorColor (const FString&);
static void setXTermMouseForeground (const FString&);
static void setXTermMouseBackground (const FString&);
static void setXTermHighlightBackground (const FString&);
static void setXTermDefaults();
static void resetXTermColors();
static void resetXTermForeground();
static void resetXTermBackground();
static void resetXTermCursorColor();
static void resetXTermMouseForeground();
static void resetXTermMouseBackground();
static void resetXTermHighlightBackground();
static void resetXTermDefaults();
static void saveColorMap();
static void resetColorMap();
static void setPalette (short, int, int, int);
static void setBeep (int, int);
static void resetBeep();
static void beep();
static void setEncoding (std::string);
static std::string getEncoding();
static bool scrollTermForward();
static bool scrollTermReverse();
static const FString getAnswerbackMsg();
static const FString getSecDA();
// function pointer -> static function
static int (*Fputchar)(int);
static void putstringf (const char* const, ...)
#if defined(__clang__)
__attribute__((__format__ (__printf__, 1, 2)))
#elif defined(__GNUC__)
__attribute__ ((format (printf, 1, 2)))
#endif
;
static void putstring (const char* const, int = 1);
static int putchar_ASCII (register int);
static int putchar_UTF8 (register int);
static int UTF8decode (const char[]);
2015-05-23 13:35:12 +02:00
2017-03-26 20:40:04 +02:00
#if DEBUG
2017-09-11 03:06:02 +02:00
static char termtype_256color[256];
static char termtype_Answerback[256];
static char termtype_SecDA[256];
2017-03-26 20:40:04 +02:00
#endif
2017-09-11 03:06:02 +02:00
protected:
// Methods
2017-03-26 20:40:04 +02:00
#if defined(__linux__)
2017-09-11 03:06:02 +02:00
static void initLinuxConsoleCharMap();
2017-03-26 20:40:04 +02:00
#endif
#if defined(__FreeBSD__) || defined(__DragonFly__)
2017-09-11 03:06:02 +02:00
static void initFreeBSDConsoleCharMap();
#endif
2017-09-11 03:06:02 +02:00
static void initCygwinCharMap();
static void initTeraTermCharMap();
static bool charEncodable (uInt);
static uInt charEncode (uInt);
static uInt charEncode (uInt, fc::encoding);
static char* changeAttribute ( char_data*&
, char_data*& );
static bool hasChangedTermSize();
static void changeTermSizeFinished();
static void xtermMetaSendsESC (bool);
static void xtermMouse (bool);
static void enableXTermMouse();
static void disableXTermMouse();
2015-05-23 13:35:12 +02:00
#ifdef F_HAVE_LIBGPM
2017-09-11 03:06:02 +02:00
static bool gpmMouse (bool);
static bool enableGpmMouse();
static bool disableGpmMouse();
static bool isGpmMouseEnabled();
#endif // F_HAVE_LIBGPM
2017-09-11 03:06:02 +02:00
static FPoint& getMousePos();
static void setMousePos (const FPoint&);
static void setMousePos (short, short);
// Data Members
static int stdin_no;
static int stdout_no;
static int erase_ch_length;
static int repeat_char_length;
static int clr_bol_length;
static int clr_eol_length;
static int cursor_addres_lengths;
static bool NewFont;
static bool VGAFont;
static bool no_shadow_character;
static bool no_half_block_character;
static bool cursor_optimisation;
static bool xterm_default_colors;
static bool use_alternate_screen;
static fc::encoding Encoding;
static char exit_message[8192];
private:
// Typedefs
typedef FTermcap::tcap_map termcap_map;
typedef struct
{
uChar red;
uChar green;
uChar blue;
} dacreg;
// Constants
static const int NEED_MORE_DATA = -1; // parseKeyString return value
// Disable copy constructor
FTerm (const FTerm&);
// Disable assignment operator (=)
FTerm& operator = (const FTerm&);
// Inquiries
#if defined(__linux__)
2017-09-11 03:06:02 +02:00
static int isLinuxConsole();
#endif
#if defined(__FreeBSD__) || defined(__DragonFly__)
2017-09-11 03:06:02 +02:00
static bool isFreeBSDConsole();
#endif
#if defined(__NetBSD__) || defined(__OpenBSD__)
2017-09-11 03:06:02 +02:00
static bool isWSConsConsole();
#endif
2017-09-11 03:06:02 +02:00
// Methods
#if defined(__linux__)
2017-09-11 03:06:02 +02:00
static uInt16 getInputStatusRegisterOne();
static uChar readAttributeController (uChar);
static void writeAttributeController (uChar, uChar);
static uChar getAttributeMode();
static void setAttributeMode (uChar);
static int setBlinkAsIntensity (bool);
static int getFramebuffer_bpp();
2017-03-26 20:40:04 +02:00
#endif
2017-09-11 03:06:02 +02:00
static int openConsole();
static int closeConsole();
static void getSystemTermType();
static void storeTTYsettings();
static void restoreTTYsettings();
2017-03-26 20:40:04 +02:00
#if defined(__linux__)
2017-09-11 03:06:02 +02:00
static int getScreenFont();
static int setScreenFont ( uChar*, uInt, uInt, uInt
, bool = false );
static int setUnicodeMap (struct unimapdesc*);
static int getUnicodeMap ();
static void initLinuxConsole();
#endif
#if defined(__FreeBSD__) || defined(__DragonFly__)
2017-09-11 03:06:02 +02:00
static bool saveFreeBSDAltKey();
static bool setFreeBSDAltKey (uInt);
static bool setFreeBSDAlt2Meta();
static bool resetFreeBSDAlt2Meta();
static void initFreeBSDConsole();
2017-03-26 20:40:04 +02:00
#endif
#if defined(__NetBSD__) || defined(__OpenBSD__)
2017-09-11 03:06:02 +02:00
static bool saveWSConsEncoding();
static bool setWSConsEncoding (kbd_t);
static bool setWSConsMetaEsc();
static bool resetWSConsEncoding();
static void initWSConsConsole();
#endif
2017-09-11 03:06:02 +02:00
static uInt getBaudRate (const struct termios*);
static char* init_256colorTerminal();
static char* parseAnswerbackMsg (char*&);
static char* parseSecDA (char*&);
static void oscPrefix();
static void oscPostfix();
static void init_alt_charset();
static void init_pc_charset();
static void init_termcaps();
static void init_encoding();
void init();
void finish();
static uInt cp437_to_unicode (uChar);
static void signal_handler (int);
// Data Members
static std::map <uChar,uChar>* vt100_alt_char;
static std::map <std::string,fc::encoding>* encoding_set;
static FTermcap::tcap_map* tcap;
static bool mouse_support;
static bool decscusr_support;
2017-09-11 03:06:02 +02:00
static bool terminal_detection;
static bool raw_mode;
static bool input_data_pending;
static bool non_blocking_stdin;
static bool gpm_mouse_enabled;
static bool pc_charset_console;
static bool utf8_input;
static bool utf8_state;
static bool utf8_console;
static bool utf8_linux_terminal;
static bool force_vt100;
static bool vt100_console;
static bool ascii_console;
static bool color256;
static bool monochron;
static bool xterm_terminal;
static bool ansi_terminal;
static bool rxvt_terminal;
static bool urxvt_terminal;
static bool mlterm_terminal;
static bool putty_terminal;
static bool kde_konsole;
static bool gnome_terminal;
static bool kterm_terminal;
static bool tera_terminal;
static bool cygwin_terminal;
static bool mintty_terminal;
static bool linux_terminal;
static bool netbsd_terminal;
static bool openbsd_terminal;
static bool screen_terminal;
static bool tmux_terminal;
static char termtype[256];
static char term_name[256];
static char* locale_name;
static char* locale_xterm;
static FRect* term; // current terminal geometry
static FPoint* mouse; // mouse click position
static int stdin_status_flags;
static int fd_tty;
static uInt baudrate;
static bool resize_term;
static struct termios term_init;
static fc::linuxConsoleCursorStyle linux_console_cursor_style;
static fc::freebsdConsoleCursorStyle freebsd_console_cursor_style;
static struct console_font_op screen_font;
static struct unimapdesc screen_unicode_map;
#if defined(__FreeBSD__) || defined(__DragonFly__)
2017-09-11 03:06:02 +02:00
static uInt bsd_alt_keymap;
#endif
#if defined(__NetBSD__) || defined(__OpenBSD__)
2017-09-11 03:06:02 +02:00
static kbd_t wscons_keyboard_encoding;
#endif
2017-09-11 03:06:02 +02:00
static FOptiMove* opti_move;
static FOptiAttr* opti_attr;
static const FString* xterm_font;
static const FString* xterm_title;
static const FString* answer_back;
static const FString* sec_da;
struct
{
dacreg d[16];
} color_map;
};
#pragma pack(pop)
2015-10-17 19:40:43 +02:00
// FTerm inline functions
2015-05-23 13:35:12 +02:00
//----------------------------------------------------------------------
inline const char* FTerm::getClassName() const
{ return "FTerm"; }
//----------------------------------------------------------------------
inline char* FTerm::getTermType()
{ return termtype; }
//----------------------------------------------------------------------
inline char* FTerm::getTermName()
{ return term_name; }
//----------------------------------------------------------------------
2017-03-26 20:40:04 +02:00
inline int FTerm::getTabstop()
{ return FTermcap::tabstop; }
//----------------------------------------------------------------------
inline int FTerm::getMaxColor()
{ return FTermcap::max_color; }
2017-03-26 20:40:04 +02:00
#if DEBUG
//----------------------------------------------------------------------
inline const FString& FTerm::getAnswerbackString()
{ return ( answer_back ) ? *answer_back : *fc::empty_string; }
2017-03-26 20:40:04 +02:00
//----------------------------------------------------------------------
inline const FString& FTerm::getSecDAString()
{ return ( sec_da ) ? *sec_da : *fc::empty_string; }
2017-03-26 20:40:04 +02:00
#endif
//----------------------------------------------------------------------
inline bool FTerm::isRaw()
{ return raw_mode; }
2015-05-23 13:35:12 +02:00
//----------------------------------------------------------------------
inline bool FTerm::hasPCcharset()
{ return pc_charset_console; }
//----------------------------------------------------------------------
inline bool FTerm::hasUTF8()
{ return utf8_console; }
//----------------------------------------------------------------------
inline bool FTerm::hasVT100()
{ return vt100_console; }
//----------------------------------------------------------------------
inline bool FTerm::hasASCII()
{ return ascii_console; }
//----------------------------------------------------------------------
inline bool FTerm::isMonochron()
{ return monochron; }
//----------------------------------------------------------------------
2016-10-09 02:06:06 +02:00
inline bool FTerm::isXTerminal()
{ return xterm_terminal; }
2015-05-23 13:35:12 +02:00
//----------------------------------------------------------------------
inline bool FTerm::isAnsiTerminal()
{ return ansi_terminal; }
//----------------------------------------------------------------------
2016-10-09 02:06:06 +02:00
inline bool FTerm::isRxvtTerminal()
{ return rxvt_terminal; }
2015-05-23 13:35:12 +02:00
//----------------------------------------------------------------------
inline bool FTerm::isUrxvtTerminal()
{ return urxvt_terminal; }
2016-10-09 02:06:06 +02:00
//----------------------------------------------------------------------
inline bool FTerm::isMltermTerminal()
{ return mlterm_terminal; }
//----------------------------------------------------------------------
inline bool FTerm::isPuttyTerminal()
{ return putty_terminal; }
//----------------------------------------------------------------------
inline bool FTerm::isKdeTerminal()
{ return kde_konsole; }
//----------------------------------------------------------------------
inline bool FTerm::isGnomeTerminal()
{ return gnome_terminal; }
//----------------------------------------------------------------------
inline bool FTerm::isKtermTerminal()
{ return kterm_terminal; }
//----------------------------------------------------------------------
inline bool FTerm::isTeraTerm()
{ return tera_terminal; }
//----------------------------------------------------------------------
inline bool FTerm::isCygwinTerminal()
{ return cygwin_terminal; }
//----------------------------------------------------------------------
inline bool FTerm::isMinttyTerm()
{ return mintty_terminal; }
//----------------------------------------------------------------------
inline bool FTerm::isLinuxTerm()
{ return linux_terminal; }
//----------------------------------------------------------------------
inline bool FTerm::isScreenTerm()
{ return screen_terminal; }
//----------------------------------------------------------------------
inline bool FTerm::isTmuxTerm()
{ return tmux_terminal; }
//----------------------------------------------------------------------
inline bool FTerm::isInputDataPending()
{ return input_data_pending; }
//----------------------------------------------------------------------
inline bool FTerm::isNewFont()
{ return NewFont; }
//----------------------------------------------------------------------
inline bool FTerm::isUTF8()
{ return utf8_state; }
//----------------------------------------------------------------------
inline bool FTerm::isUTF8_linux_terminal()
{ return utf8_linux_terminal; }
//----------------------------------------------------------------------
inline bool FTerm::setCursorOptimisation (bool on)
{ return cursor_optimisation = ( on ) ? true : false; }
//----------------------------------------------------------------------
inline void FTerm::setXTermDefaultColors (bool on)
{ xterm_default_colors = on; }
2015-05-23 13:35:12 +02:00
//----------------------------------------------------------------------
inline bool FTerm::setRawMode()
{ return setRawMode(true); }
2015-05-23 13:35:12 +02:00
//----------------------------------------------------------------------
inline bool FTerm::unsetRawMode()
{ return setRawMode(false); }
2015-05-23 13:35:12 +02:00
//----------------------------------------------------------------------
inline bool FTerm::setCookedMode()
{ return setRawMode(false); }
2015-05-23 13:35:12 +02:00
2017-03-26 20:40:04 +02:00
//----------------------------------------------------------------------
inline void FTerm::disableAltScreen()
{ use_alternate_screen = false; }
2015-05-23 13:35:12 +02:00
//----------------------------------------------------------------------
inline bool FTerm::setUTF8()
{ return setUTF8(true); }
//----------------------------------------------------------------------
inline bool FTerm::unsetUTF8()
{ return setUTF8(false); }
//----------------------------------------------------------------------
inline bool FTerm::setNonBlockingInput()
{ return setNonBlockingInput(true); }
2015-05-23 13:35:12 +02:00
//----------------------------------------------------------------------
inline bool FTerm::unsetNonBlockingInput()
{ return setNonBlockingInput(false); }
2015-05-23 13:35:12 +02:00
//----------------------------------------------------------------------
inline bool FTerm::hasChangedTermSize()
{ return resize_term; }
2015-05-23 13:35:12 +02:00
//----------------------------------------------------------------------
inline void FTerm::changeTermSizeFinished()
{ resize_term = false; }
2015-05-23 13:35:12 +02:00
//----------------------------------------------------------------------
inline void FTerm::enableXTermMouse()
{ xtermMouse(true); }
//----------------------------------------------------------------------
inline void FTerm::disableXTermMouse()
{ xtermMouse(false); }
#ifdef F_HAVE_LIBGPM
//----------------------------------------------------------------------
inline bool FTerm::enableGpmMouse()
{ return gpmMouse(true); }
//----------------------------------------------------------------------
inline bool FTerm::disableGpmMouse()
{ return gpmMouse(false); }
//----------------------------------------------------------------------
inline bool FTerm::isGpmMouseEnabled()
{ return gpm_mouse_enabled; }
#endif // F_HAVE_LIBGPM
//----------------------------------------------------------------------
inline FPoint& FTerm::getMousePos()
{ return *mouse; }
//----------------------------------------------------------------------
2017-08-12 20:10:27 +02:00
inline void FTerm::setMousePos (const FPoint& m)
{ *mouse = m; }
//----------------------------------------------------------------------
inline void FTerm::setMousePos (short x, short y)
{ mouse->setPoint (x, y); }
2015-05-23 13:35:12 +02:00
#endif // FTERM_H