You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

236 lines
8.3 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /* Linux keyboard handler routines header.
  2. Copyright by Salvador E. Tropea (SET) (2001-2003)
  3. Covered by the GPL license. */
  4. #if defined(TVOSf_Linux) && !defined(LINUXSCREEN_HEADER_INCLUDED)
  5. #define LINUXSCREEN_HEADER_INCLUDED
  6. // Modes you can pass to Init
  7. const int lnxInitVCSrw=0, lnxInitVCSwo=1, lnxInitSimple=2, lnxInitMDA=3;
  8. struct stCodePageCk;
  9. struct stCodePageLang;
  10. struct CodePage;
  11. // virtual to avoid problems with multiple inheritance
  12. class TDisplayLinux : virtual public TDisplay
  13. {
  14. protected:
  15. TDisplayLinux() {};
  16. // We will use casts to base classes, destructors must be pointers
  17. virtual ~TDisplayLinux();
  18. // This sets the pointers of TDisplay to point to this class
  19. static void Init(int mode);
  20. // Behaviors to replace TDisplay
  21. // Monochrome (MDA, Hercules, etc.) members are marked with MDA
  22. // /dev/vcs* members are marked with VCS
  23. static void SetCursorPos(unsigned x, unsigned y);
  24. static void SetCursorPosMDA(unsigned x, unsigned y);
  25. static void SetCursorPosVCS(unsigned x, unsigned y);
  26. static void GetCursorPos(unsigned &x, unsigned &y);
  27. static void GetCursorPosVCS(unsigned &x, unsigned &y);
  28. static void GetCursorPosGeneric(unsigned &x, unsigned &y);
  29. static void SetCursorShape(unsigned start, unsigned end);
  30. static void SetCursorShapeMDA(unsigned start, unsigned end);
  31. static void GetCursorShape(unsigned &start, unsigned &end);
  32. static void GetCursorShapeMDA(unsigned &start, unsigned &end);
  33. // Not available static void clearScreen(uchar, uchar);
  34. static ushort GetRows();
  35. //static ushort GetRowsMDA(); is ushort TDisplay::defaultGetRows()
  36. static ushort GetCols();
  37. //static ushort GetColsMDA(); is ushort TDisplay::defaultGetCols()
  38. // Not available static void SetCrtMode(ushort mode);
  39. // Not available static ushort getCrtMode();
  40. // Not available static void SetCrtModeExt(char *mode);
  41. static int CheckForWindowSize(void);
  42. static const char *GetWindowTitle(void);
  43. static int SetWindowTitle(const char *name);
  44. static int SetDisPaletteColors(int from, int number, TScreenColor *colors);
  45. static void GetDisPaletteColors(int from, int number, TScreenColor *colors);
  46. // Functions and members specific for this driver
  47. // Helpers to make the code easy to read
  48. inline static int canWriteVCS();
  49. inline static int canReadVCS();
  50. static void setUpEnviron();
  51. // Current cursor position
  52. static int curX, curY;
  53. // Position to restore
  54. static unsigned oldCurX, oldCurY;
  55. // Current cursor shape
  56. static int cursorStart,cursorEnd;
  57. // 1 when the size of the window where the program is running changed
  58. static volatile sig_atomic_t windowSizeChanged;
  59. static int vcsWfd; // virtual console system descriptor
  60. static int vcsRfd; // Same for reading
  61. static int hOut; // Handle for the console output
  62. static FILE *fOut; // Stream for the console output
  63. // Original environment of the application, here we put the title
  64. static char *origEnvir;
  65. // New environment, a copy of the original
  66. static char *newEnvir;
  67. // How much space we have
  68. static int maxLenTit;
  69. // Can we use TIOCLINUX? Initialized but TScreen, but also used here
  70. static char tioclinuxOK;
  71. // Color translation table
  72. static char cMap[16];
  73. // The code page we detected (or just guess) is installed
  74. static int installedSFM;
  75. static int installedACM;
  76. };
  77. inline
  78. int TDisplayLinux::canWriteVCS()
  79. {
  80. return vcsWfd>=0;
  81. }
  82. inline
  83. int TDisplayLinux::canReadVCS()
  84. {
  85. return vcsRfd>=0;
  86. }
  87. // With this order the destructor will be called first for TScreen,
  88. // TScreenLinux, TScreen, TScreenLinux::suspend, TDisplayLinux and
  89. // finally TDisplay (twice).
  90. class TScreenLinux : public TDisplayLinux, public TScreen
  91. {
  92. public:
  93. TScreenLinux();
  94. // We will use casts to base classes, destructors must be pointers
  95. virtual ~TScreenLinux();
  96. friend class TDisplayLinux;
  97. friend class TGKeyLinux; // Suspend/ResumeFont on VT switch
  98. protected:
  99. static void Suspend();
  100. static void Resume();
  101. // Default: ushort fixCrtMode( ushort mode )
  102. // Default: void setCrtData();
  103. // Default: void clearScreen()
  104. static void SetVideoMode(ushort mode);
  105. static void SetVideoModeExt(char *mode);
  106. static void GetCharactersMDA(unsigned offset,ushort *buf,unsigned count);
  107. static void GetCharactersVCS(unsigned offset,ushort *buf,unsigned count);
  108. static ushort GetCharacter(unsigned dst);
  109. static void SetCharacter(unsigned offset,uint32 value);
  110. static void SetCharactersTerm(unsigned dst,ushort *src,unsigned len);
  111. static void SetCharactersVCS(unsigned dst,ushort *src,unsigned len);
  112. static void SetCharactersMDA(unsigned dst,ushort *src,unsigned len);
  113. static int System(const char *command, pid_t *pidChild, int in, int out,
  114. int err);
  115. static int GetFontGeometry(unsigned &w, unsigned &h);
  116. static int SetFont(int changeP, TScreenFont256 *fontP,
  117. int changeS, TScreenFont256 *fontS,
  118. int fontCP=-1, int appCP=-1);
  119. static void RestoreFonts();
  120. // Initialization code executed just once
  121. static int InitOnce();
  122. // Initialize member pointers
  123. static void Init(int mode);
  124. // Detects if we can access /dev/vcs* device. Initializes vcsWfd and vcsRfd.
  125. static void DetectVCS();
  126. // Detects a MDA board. Indicated in secondaryAvailable.
  127. static void DetectSecondaryDisplay();
  128. // Window size change signal handler
  129. static void sigWindowSizeChanged(int sig);
  130. // Helper function to save the current content of the screen
  131. static void SaveScreen();
  132. static void RestoreScreen();
  133. static void SaveScreenReleaseMemory(void);
  134. // Helper used to resize screenBuffer
  135. static void CheckSizeBuffer(int oldWidth, int oldHeight);
  136. // Helper to write the text and escape sequences
  137. static void writeBlock(int dst, int len, ushort *old, ushort *src);
  138. // Helper to set the color escape sequence
  139. static void mapColor(int col);
  140. // Closes and releases all, called from TDisplayLinux
  141. static void DeallocateResources();
  142. // Analyzes the console code page
  143. static int AnalyzeCodePage();
  144. static void CreateSFMFromTable(ushort *table);
  145. static void CreateACMFromTable(ushort *table);
  146. // A guess about the code page using the language
  147. static int GuessCodePageFromLANG();
  148. // Tries to find the geometry of loaded fonts
  149. static int GetLinuxFontGeometry();
  150. static void FreeFontsMemory();
  151. static int AllocateFontsMemory();
  152. static int GetLinuxFont();
  153. static void ExpandFont(uchar *dest, TScreenFont256 *font);
  154. static void SuspendFont();
  155. static void ResumeFont();
  156. static void AvoidUTF8Mode();
  157. static int isInUTF8();
  158. // Terminal state before starting
  159. static struct termios outTermiosOrig;
  160. // Our terminal state
  161. static struct termios outTermiosNew;
  162. // Last error
  163. static const char *error;
  164. // Is MDA accesible?
  165. static char secondaryAvailable;
  166. // File handle for mmaped memory of MDA board
  167. static int mdaMemH;
  168. // Mmaped MDA memory
  169. static ushort *mdaMem;
  170. // Kind of palette
  171. static int palette;
  172. enum
  173. {
  174. PAL_MONO, PAL_LOW, PAL_HIGH, PAL_LOW2
  175. };
  176. // Buffer containing the screen before we started
  177. static ushort *userBuffer;
  178. // Size of the buffer
  179. static unsigned userBufferSize;
  180. // Current colors, for escape sequences
  181. static int oldCol, oldBack, oldFore;
  182. // List of known code pages
  183. static struct stCodePageCk knownFonts[];
  184. static struct stCodePageCk knownScreenMaps[];
  185. // List of known languages and their code pages
  186. static struct stCodePageLang langCodePages[];
  187. // Used when the loaded maps are unknown
  188. static CodePage unknownACM, unknownSFM;
  189. // Fonts stuff
  190. static uchar canSetFonts,primaryFontSet,secondaryFontSet;
  191. static struct console_font_op linuxFont;
  192. static struct console_font_op ourFont;
  193. static int origCPScr, origCPApp, origCPInp;
  194. // Original UTF-8 state
  195. static int origUTF8Mode;
  196. // We detected the user is using a 512 symbols font
  197. static int font512Detected;
  198. // Related to font512Detected, it means we have to reduce colors to 8
  199. static int reduceTo8Colors;
  200. };
  201. // SET: Enclosed all the I/O stuff in "__i386__ defined" because I don't
  202. // think it have much sense in non-Intel PCs. In fact looks like it gives
  203. // some problems when compiling for Alpha (__alpha__).
  204. // Also make it only for Linux until I know how to do it for FreeBSD.
  205. #if defined(TVCPU_x86)
  206. // Needed for ioperm, used only by i386.
  207. // I also noted that glibc 2.1.3 for Alpha, SPARC and PPC doesn't have
  208. // this header
  209. #include <sys/perm.h>
  210. #define h386LowLevel
  211. #endif
  212. #endif // LINUXSCREEN_HEADER_INCLUDED