Console.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. #include "il2cpp-config.h"
  2. #if IL2CPP_TARGET_POSIX && !IL2CPP_USE_PLATFORM_SPECIFIC_CONSOLE
  3. #include "os/Console.h"
  4. #include "os/File.h"
  5. #include <stdlib.h>
  6. #include <errno.h>
  7. #include <fcntl.h>
  8. #include <signal.h>
  9. #include <stdio.h>
  10. #include <termios.h>
  11. #include <unistd.h>
  12. #include <sys/ioctl.h>
  13. #include <sys/time.h>
  14. #include <sys/types.h>
  15. namespace il2cpp
  16. {
  17. namespace os
  18. {
  19. namespace Console
  20. {
  21. #if !RUNTIME_TINY
  22. static bool setupComplete = false;
  23. static int32_t s_terminalSize;
  24. static struct termios s_initialAttr;
  25. static struct termios s_il2cppAttr;
  26. static std::string s_keypadXmit;
  27. static std::string s_teardown;
  28. static struct sigaction s_saveSigcont, s_saveSigint, s_saveSigwinch;
  29. static int32_t GetTerminalSize()
  30. {
  31. struct winsize ws;
  32. if (ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) == 0)
  33. return (ws.ws_col << 16) | ws.ws_row;
  34. return -1;
  35. }
  36. static bool SetProperty(int32_t property, bool value)
  37. {
  38. struct termios attr;
  39. bool callset = false;
  40. bool check;
  41. if (tcgetattr(STDIN_FILENO, &attr) == -1)
  42. return false;
  43. check = (attr.c_lflag & property) != 0;
  44. if ((value || check) && !(value && check))
  45. {
  46. callset = true;
  47. if (value)
  48. attr.c_lflag |= property;
  49. else
  50. attr.c_lflag &= ~property;
  51. }
  52. if (!callset)
  53. return true;
  54. if (tcsetattr(STDIN_FILENO, TCSANOW, &attr) == -1)
  55. return true;
  56. s_il2cppAttr = attr;
  57. return true;
  58. }
  59. static void SetControlChars(uint8_t* control_chars, const uint8_t *cc)
  60. {
  61. // The index into the array comes from corlib/System/ControlCharacters.cs
  62. #ifdef VINTR
  63. control_chars[0] = cc[VINTR];
  64. #endif
  65. #ifdef VQUIT
  66. control_chars[1] = cc[VQUIT];
  67. #endif
  68. #ifdef VERASE
  69. control_chars[2] = cc[VERASE];
  70. #endif
  71. #ifdef VKILL
  72. control_chars[3] = cc[VKILL];
  73. #endif
  74. #ifdef VEOF
  75. control_chars[4] = cc[VEOF];
  76. #endif
  77. #ifdef VTIME
  78. control_chars[5] = cc[VTIME];
  79. #endif
  80. #ifdef VMIN
  81. control_chars[6] = cc[VMIN];
  82. #endif
  83. #ifdef VSWTC
  84. control_chars[7] = cc[VSWTC];
  85. #endif
  86. #ifdef VSTART
  87. control_chars[8] = cc[VSTART];
  88. #endif
  89. #ifdef VSTOP
  90. control_chars[9] = cc[VSTOP];
  91. #endif
  92. #ifdef VSUSP
  93. control_chars[10] = cc[VSUSP];
  94. #endif
  95. #ifdef VEOL
  96. control_chars[11] = cc[VEOL];
  97. #endif
  98. #ifdef VREPRINT
  99. control_chars[12] = cc[VREPRINT];
  100. #endif
  101. #ifdef VDISCARD
  102. control_chars[13] = cc[VDISCARD];
  103. #endif
  104. #ifdef VWERASE
  105. control_chars[14] = cc[VWERASE];
  106. #endif
  107. #ifdef VLNEXT
  108. control_chars[15] = cc[VLNEXT];
  109. #endif
  110. #ifdef VEOL2
  111. control_chars[16] = cc[VEOL2];
  112. #endif
  113. }
  114. static void CallDoConsoleCancelEvent()
  115. {
  116. // TODO: Call Console.cancel_handler delegate from another thread.
  117. }
  118. static void SigintHandler(int signo)
  119. {
  120. static bool insideSigint = false;
  121. if (insideSigint)
  122. return;
  123. insideSigint = true;
  124. CallDoConsoleCancelEvent();
  125. insideSigint = false;
  126. }
  127. static void SigcontHandler(int signo, siginfo_t *the_siginfo, void *data)
  128. {
  129. // Ignore error, there is not much we can do in the sigcont handler.
  130. tcsetattr(STDIN_FILENO, TCSANOW, &s_il2cppAttr);
  131. if (!s_keypadXmit.empty())
  132. write(STDOUT_FILENO, s_keypadXmit.c_str(), s_keypadXmit.length());
  133. // Call previous handler
  134. if (s_saveSigcont.sa_sigaction != NULL &&
  135. s_saveSigcont.sa_sigaction != (void*)SIG_DFL &&
  136. s_saveSigcont.sa_sigaction != (void*)SIG_IGN)
  137. (*s_saveSigcont.sa_sigaction)(signo, the_siginfo, data);
  138. }
  139. static void SigwinchHandler(int signo, siginfo_t *the_siginfo, void *data)
  140. {
  141. const int32_t size = GetTerminalSize();
  142. if (size != -1)
  143. s_terminalSize = size;
  144. // Call previous handler
  145. if (s_saveSigwinch.sa_sigaction != NULL &&
  146. s_saveSigwinch.sa_sigaction != (void*)SIG_DFL &&
  147. s_saveSigwinch.sa_sigaction != (void*)SIG_IGN)
  148. (*s_saveSigwinch.sa_sigaction)(signo, the_siginfo, data);
  149. }
  150. static void ConsoleSetupSignalHandler()
  151. {
  152. struct sigaction sigcont, sigint, sigwinch;
  153. memset(&sigcont, 0, sizeof(struct sigaction));
  154. memset(&sigint, 0, sizeof(struct sigaction));
  155. memset(&sigwinch, 0, sizeof(struct sigaction));
  156. // Continuing
  157. sigcont.sa_sigaction = SigcontHandler;
  158. sigcont.sa_flags = SA_SIGINFO;
  159. sigemptyset(&sigcont.sa_mask);
  160. sigaction(SIGCONT, &sigcont, &s_saveSigcont);
  161. // Interrupt handler
  162. sigint.sa_handler = SigintHandler;
  163. sigint.sa_flags = 0;
  164. sigemptyset(&sigint.sa_mask);
  165. sigaction(SIGINT, &sigint, &s_saveSigint);
  166. // Window size changed
  167. sigwinch.sa_sigaction = SigwinchHandler;
  168. sigwinch.sa_flags = SA_SIGINFO;
  169. sigemptyset(&sigwinch.sa_mask);
  170. sigaction(SIGWINCH, &sigwinch, &s_saveSigwinch);
  171. }
  172. // Exists in Mono, but is unused.
  173. static void ConsoleRestoreSignalHandlers()
  174. {
  175. sigaction(SIGCONT, &s_saveSigcont, NULL);
  176. sigaction(SIGINT, &s_saveSigint, NULL);
  177. sigaction(SIGWINCH, &s_saveSigwinch, NULL);
  178. }
  179. int32_t InternalKeyAvailable(int32_t ms_timeout)
  180. {
  181. fd_set rfds;
  182. struct timeval tv;
  183. struct timeval *tvptr;
  184. div_t divvy;
  185. int32_t ret, nbytes;
  186. do
  187. {
  188. FD_ZERO(&rfds);
  189. FD_SET(STDIN_FILENO, &rfds);
  190. if (ms_timeout >= 0)
  191. {
  192. divvy = div(ms_timeout, 1000);
  193. tv.tv_sec = divvy.quot;
  194. tv.tv_usec = divvy.rem;
  195. tvptr = &tv;
  196. }
  197. else
  198. {
  199. tvptr = NULL;
  200. }
  201. ret = select(STDIN_FILENO + 1, &rfds, NULL, NULL, tvptr);
  202. }
  203. while (ret == -1 && errno == EINTR);
  204. if (ret > 0)
  205. {
  206. nbytes = 0;
  207. ret = ioctl(STDIN_FILENO, FIONREAD, &nbytes);
  208. if (ret >= 0)
  209. ret = nbytes;
  210. }
  211. return (ret > 0) ? ret : 0;
  212. }
  213. bool SetBreak(bool wantBreak)
  214. {
  215. return SetProperty(IGNBRK, !wantBreak);
  216. }
  217. bool SetEcho(bool wantEcho)
  218. {
  219. return SetProperty(ECHO, wantEcho);
  220. }
  221. static void TtyShutdown()
  222. {
  223. if (!setupComplete)
  224. return;
  225. if (!s_teardown.empty())
  226. write(STDOUT_FILENO, s_teardown.c_str(), s_teardown.length());
  227. tcflush(STDIN_FILENO, TCIFLUSH);
  228. tcsetattr(STDIN_FILENO, TCSANOW, &s_initialAttr);
  229. SetProperty(ECHO, true);
  230. setupComplete = false;
  231. }
  232. bool TtySetup(const std::string& keypadXmit, const std::string& teardown, uint8_t* control_characters, int32_t** size)
  233. {
  234. s_terminalSize = GetTerminalSize();
  235. if (s_terminalSize == -1)
  236. {
  237. int32_t cols = 0, rows = 0;
  238. const char *colsValue = getenv("COLUMNS");
  239. if (colsValue != NULL)
  240. cols = atoi(colsValue);
  241. const char *linesValue = getenv("LINES");
  242. if (linesValue != NULL)
  243. rows = atoi(linesValue);
  244. if (cols != 0 && rows != 0)
  245. s_terminalSize = (cols << 16) | rows;
  246. else
  247. s_terminalSize = -1;
  248. }
  249. *size = &s_terminalSize;
  250. if (tcgetattr(STDIN_FILENO, &s_initialAttr) == -1)
  251. return false;
  252. s_il2cppAttr = s_initialAttr;
  253. s_il2cppAttr.c_lflag &= ~(ICANON);
  254. s_il2cppAttr.c_iflag &= ~(IXON | IXOFF);
  255. s_il2cppAttr.c_cc[VMIN] = 1;
  256. s_il2cppAttr.c_cc[VTIME] = 0;
  257. if (tcsetattr(STDIN_FILENO, TCSANOW, &s_il2cppAttr) == -1)
  258. return false;
  259. s_keypadXmit = keypadXmit;
  260. SetControlChars(control_characters, s_il2cppAttr.c_cc);
  261. if (setupComplete)
  262. return true;
  263. ConsoleSetupSignalHandler();
  264. setupComplete = true;
  265. s_teardown = teardown;
  266. atexit(TtyShutdown);
  267. return true;
  268. }
  269. #endif
  270. const char* NewLine()
  271. {
  272. return "\n";
  273. }
  274. }
  275. }
  276. }
  277. #endif