finalcut/src/include/final/fterm.h

461 lines
17 KiB
C
Raw Normal View History

2017-11-04 07:03:53 +01:00
/***********************************************************************
* fterm.h - Base class for terminal control *
2017-11-04 07:03:53 +01:00
* *
* This file is part of the Final Cut widget toolkit *
* *
* Copyright 2012-2019 Markus Gans *
2017-11-04 07:03:53 +01:00
* *
* The Final Cut is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Lesser General Public License *
* as published by the Free Software Foundation; either version 3 of *
* the License, or (at your option) any later version. *
* *
* The Final Cut is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with this program. If not, see *
* <http://www.gnu.org/licenses/>. *
***********************************************************************/
/* Base class
*
*
* 1 1
* FTerm -- - - - FTermios
* :
* :
* : 1
* :- - - - FTermcap
* :
* :
* : 1
* :- - - - FTermcapQuirks
* :
* :
* : 1
* :- - - - FTermDetection
* :
* :
* : 1
* :- - - - FTermXTerminal
* :
* :
* : 1
* :- - - - FTermLinux
* :
* :
* : 1
* :- - - - FTermFreeBSD
* :
* :
* : 1
* :- - - - FTermOpenBSD
* :
* :
* : 1
* :- - - - FColorPalette
* :
* :
* : 1
* :- - - - FOptiAttr
* :
* :
* : 1
* :- - - - FOptiMove
* :
* :
* : 1
* :- - - - FKeyboard
* :
* :
2018-07-22 23:07:49 +02:00
* : 1
* :- - - - FMouseControl
* :
* :
* : *
* :- - - - 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
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>
#if F_HAVE_GETTTYNAM && F_HAVE_TTYENT_H
#include <ttyent.h>
#endif
#include <clocale>
2015-05-23 13:35:12 +02:00
#include <cmath>
#include <csignal>
2019-10-05 23:20:07 +02:00
#include <functional>
2015-05-23 13:35:12 +02:00
#include <map>
#include <queue>
2019-10-05 23:20:07 +02:00
#include <utility>
2017-09-11 03:06:02 +02:00
#include <string>
2019-10-05 23:20:07 +02:00
#include <vector>
2015-05-23 13:35:12 +02:00
#include "final/fc.h"
#include "final/fstring.h"
#include "final/fsystem.h"
namespace finalcut
{
// class forward declaration
class FKeyboard;
class FMouseControl;
class FOptiAttr;
class FOptiMove;
class FStartOptions;
class FSize;
class FString;
class FTermBuffer;
class FTermData;
class FTermDebugData;
class FTermDetection;
class FTermXTerminal;
2019-07-14 18:30:35 +02:00
#if defined(UNIT_TEST)
class FTermLinux;
class FTermFreeBSD;
class FTermOpenBSD;
2019-07-14 18:30:35 +02:00
#elif defined(__linux__)
class FTermLinux;
2018-10-08 04:14:20 +02:00
#elif defined(__FreeBSD__) || defined(__DragonFly__)
class FTermFreeBSD;
2018-10-08 04:14:20 +02:00
#elif defined(__NetBSD__) || defined(__OpenBSD__)
class FTermOpenBSD;
#endif
2015-05-23 13:35:12 +02:00
//----------------------------------------------------------------------
// class FTerm
//----------------------------------------------------------------------
2019-01-04 02:18:54 +01:00
class FTerm final
2015-05-23 13:35:12 +02:00
{
2017-09-11 03:06:02 +02:00
public:
2019-10-05 23:20:07 +02:00
// Typedef
typedef std::function<int(int)> defaultPutChar;
2018-10-29 00:45:45 +01:00
2017-09-11 03:06:02 +02:00
// Constructor
explicit FTerm (bool = false);
// Disable copy constructor
FTerm (const FTerm&) = delete;
2017-09-11 03:06:02 +02:00
// Destructor
virtual ~FTerm();
// Disable assignment operator (=)
FTerm& operator = (const FTerm&) = delete;
2017-09-11 03:06:02 +02:00
// Accessors
2019-10-05 23:20:07 +02:00
virtual const FString getClassName() const;
static std::size_t getLineNumber();
static std::size_t getColumnNumber();
2018-11-21 20:07:08 +01:00
static const FString getKeyName (FKey);
static int getTTYFileDescriptor();
static char* getTermType();
static char* getTermFileName();
static int getTabstop();
static int getMaxColor();
charSubstitution& getCharSubstitutionMap();
static FTermData* getFTermData();
static FSystem* getFSystem();
static FOptiMove* getFOptiMove();
static FOptiAttr* getFOptiAttr();
static FTermDetection* getFTermDetection();
static FTermXTerminal* getFTermXTerminal();
static FKeyboard* getFKeyboard();
static FMouseControl* getFMouseControl();
2019-07-14 18:30:35 +02:00
#if defined(UNIT_TEST)
static FTermLinux* getFTermLinux();
static FTermFreeBSD* getFTermFreeBSD();
static FTermOpenBSD* getFTermOpenBSD();
#elif defined(__linux__)
static FTermLinux* getFTermLinux();
#elif defined(__FreeBSD__) || defined(__DragonFly__)
static FTermFreeBSD* getFTermFreeBSD();
#elif defined(__NetBSD__) || defined(__OpenBSD__)
static FTermOpenBSD* getFTermOpenBSD();
#endif
#if DEBUG
static FTermDebugData& getFTermDebugData();
#endif
2017-03-26 20:40:04 +02:00
2017-09-11 03:06:02 +02:00
// Inquiries
static bool isNormal (charData*&);
static bool isRaw();
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 isSunTerminal();
static bool isCygwinTerminal();
static bool isMinttyTerm();
static bool isLinuxTerm();
static bool isFreeBSDTerm();
static bool isNetBSDTerm();
static bool isOpenBSDTerm();
static bool isScreenTerm();
static bool isTmuxTerm();
static bool isNewFont();
2018-10-29 00:45:45 +01:00
static bool isCursorHideable();
static bool hasChangedTermSize();
static bool hasShadowCharacter();
static bool hasHalfBlockCharacter();
static bool hasAlternateScreen();
static bool canChangeColorPalette();
2017-09-11 03:06:02 +02:00
// Mutators
static void setFSystem (FSystem*);
static void setTermType (const char[]);
2018-12-22 23:50:10 +01:00
static void setInsertCursor (bool);
static void redefineDefaultColors (bool);
2018-12-28 22:57:43 +01:00
static void setDblclickInterval (const uInt64);
static bool setUTF8 (bool);
static bool setUTF8();
static bool unsetUTF8();
2017-09-11 03:06:02 +02:00
// Methods
static bool setVGAFont();
static bool setNewFont();
static bool setOldFont();
static int openConsole();
static int closeConsole();
static char* moveCursorString (int, int, int, int);
static char* cursorsVisibilityString (bool);
static void detectTermSize();
static void setTermSize (const FSize&);
2018-10-29 00:45:45 +01:00
static void setTermTitle (const FString&);
static void setKDECursor (fc::kdeKonsoleCursorShape);
static void saveColorMap();
static void resetColorMap();
2018-11-07 22:06:58 +01:00
static void setPalette (FColor, int, int, int);
static void setBeep (int, int);
static void resetBeep();
static void beep();
static void setEncoding (fc::encoding);
static fc::encoding getEncoding();
static std::string getEncodingString();
static bool charEncodable (wchar_t);
static wchar_t charEncode (wchar_t);
static wchar_t charEncode (wchar_t, fc::encoding);
static bool scrollTermForward();
static bool scrollTermReverse();
2017-09-11 03:06:02 +02:00
2019-10-05 23:20:07 +02:00
static defaultPutChar& putchar(); // function pointer
template<typename... Args>
static void putstringf (const char[], Args&&...);
static void putstring (const char[], int = 1);
static int putchar_ASCII (int);
static int putchar_UTF8 (int);
2017-03-26 20:40:04 +02:00
static void initScreenSettings();
static char* changeAttribute ( charData*&
, charData*& );
static void changeTermSizeFinished();
static void exitWithMessage (const FString&)
#if defined(__clang__) || defined(__GNUC__)
__attribute__((noreturn))
#endif
;
2017-09-11 03:06:02 +02:00
private:
// Methods
static FStartOptions& getStartOptions();
static void init_global_values (bool);
static void init_terminal_device_path();
static void oscPrefix();
static void oscPostfix();
static void init_alt_charset();
static void init_pc_charset();
static void init_cygwin_charmap();
static void init_teraterm_charmap();
static void init_fixed_max_color();
static void init_keyboard();
static void init_termcap();
2018-10-08 04:14:20 +02:00
static void init_quirks();
static void init_optiMove();
static void init_optiAttr();
static void init_font();
static void init_locale();
static void init_encoding();
static void init_encoding_set();
static void init_term_encoding();
static void init_individual_term_encoding();
static void init_force_vt100_encoding();
static void init_utf8_without_alt_charset();
static void init_tab_quirks();
static void init_captureFontAndTitle();
2018-10-09 00:33:26 +02:00
static bool hasNoFontSettingOption();
static void redefineColorPalette();
static void restoreColorPalette();
static void setInsertCursorStyle();
static void setOverwriteCursorStyle();
static char* enableCursorString();
static char* disableCursorString();
static void enableMouse();
static void disableMouse();
2018-10-09 00:33:26 +02:00
static void enableApplicationEscKey();
static void disableApplicationEscKey();
static void enableKeypad();
static void disableKeypad();
static void enableAlternateCharset();
static void useAlternateScreenBuffer();
static void useNormalScreenBuffer();
void allocationValues();
void deallocationValues();
void init (bool);
void initOSspecifics();
2018-10-02 01:03:44 +02:00
void initTermspecifics();
2018-10-09 00:33:26 +02:00
void initBaudRate();
void finish();
void finishOSspecifics1();
void finish_encoding();
static void setSignalHandler();
static void resetSignalHandler();
static void signal_handler (int);
2017-09-11 03:06:02 +02:00
// Data members
static FTermData* data;
static FSystem* fsys;
static FOptiMove* opti_move;
static FOptiAttr* opti_attr;
static FTermDetection* term_detection;
static FTermXTerminal* xterm;
static FKeyboard* keyboard;
static FMouseControl* mouse;
2019-07-14 18:30:35 +02:00
#if defined(UNIT_TEST)
#undef linux
static FTermLinux* linux;
static FTermFreeBSD* freebsd;
static FTermOpenBSD* openbsd;
#elif defined(__linux__)
#undef linux
static FTermLinux* linux;
2018-10-08 04:14:20 +02:00
#elif defined(__FreeBSD__) || defined(__DragonFly__)
static FTermFreeBSD* freebsd;
2018-10-08 04:14:20 +02:00
#elif defined(__NetBSD__) || defined(__OpenBSD__)
static FTermOpenBSD* openbsd;
#endif
#if DEBUG
static FTermDebugData* debug_data;
#endif
};
2015-10-17 19:40:43 +02:00
// non-member function forward declarations
//----------------------------------------------------------------------
wchar_t cp437_to_unicode (uChar);
uChar unicode_to_cp437 (wchar_t);
FString getFullWidth (const FString&);
FString getHalfWidth (const FString&);
std::size_t getColumnWidthToLength (const FString&, std::size_t);
FString getColumnSubString (const FString&, std::size_t, std::size_t);
std::size_t getColumnWidth (const FString&, std::size_t);
std::size_t getColumnWidth (const FString&);
std::size_t getColumnWidth (const wchar_t);
std::size_t getColumnWidth (charData&);
std::size_t getColumnWidth (const FTermBuffer&);
// FTerm inline functions
2015-05-23 13:35:12 +02:00
//----------------------------------------------------------------------
2019-10-05 23:20:07 +02:00
inline const FString FTerm::getClassName() const
2015-05-23 13:35:12 +02:00
{ return "FTerm"; }
//----------------------------------------------------------------------
inline void FTerm::setFSystem (FSystem* fsystem)
{ fsys = fsystem; }
2018-10-29 00:45:45 +01:00
//----------------------------------------------------------------------
inline bool FTerm::setUTF8()
{ return setUTF8(true); }
//----------------------------------------------------------------------
inline bool FTerm::unsetUTF8()
{ return setUTF8(false); }
//----------------------------------------------------------------------
template<typename... Args>
inline void FTerm::putstringf (const char format[], Args&&... args)
{
2019-09-29 22:28:58 +02:00
int size = std::snprintf ( nullptr, 0, format
, std::forward<Args>(args)... ) + 1;
if ( size == -1 )
return;
if ( ! fsys )
getFSystem();
2019-10-05 23:20:07 +02:00
std::size_t count = std::size_t(size);
std::vector<char> buf(count);
std::snprintf (&buf[0], count, format, std::forward<Args>(args)...);
2019-09-29 22:28:58 +02:00
fsys->tputs (&buf[0], 1, FTerm::putchar_ASCII);
}
} // namespace finalcut
namespace
{
//----------------------------------------------------------------------
inline std::ostream& operator << ( std::ostream& os
, finalcut::fc::SpecialCharacter c )
{
return os << static_cast<char>(c);
}
//----------------------------------------------------------------------
inline std::wostream& operator << ( std::wostream& os
, finalcut::fc::SpecialCharacter c )
{
return os << static_cast<wchar_t>(c);
}
} // namespace
#endif // FTERM_H