暫無描述
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.

Console.cpp 8.1KB

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