POSIXConsole.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platformPOSIX/platformPOSIX.h"
  23. #include "platformPOSIX/POSIXStdConsole.h"
  24. #include "platformPOSIX/POSIXUtils.h"
  25. #include "platform/input/event.h"
  26. #include "platform/platform.h"
  27. #include "core/util/rawData.h"
  28. #include "core/strings/stringFunctions.h"
  29. #include "core/util/journal/process.h"
  30. #include <signal.h>
  31. #include <unistd.h>
  32. #include <stdarg.h>
  33. #include <fcntl.h>
  34. #include "console/engineAPI.h"
  35. StdConsole *stdConsole = NULL;
  36. DefineEngineFunction(enableWinConsole, void, (bool _enable),, "enableWinConsole(bool);")
  37. {
  38. if (stdConsole)
  39. {
  40. stdConsole->enable(_enable);
  41. stdConsole->enableInput(_enable);
  42. }
  43. }
  44. void StdConsole::create()
  45. {
  46. if (stdConsole == NULL)
  47. stdConsole = new StdConsole();
  48. }
  49. void StdConsole::destroy()
  50. {
  51. if (stdConsole && stdConsole->isEnabled())
  52. stdConsole->enable(false);
  53. delete stdConsole;
  54. stdConsole = NULL;
  55. }
  56. static void signalHandler(int sigtype)
  57. {
  58. if (sigtype == SIGCONT && stdConsole != NULL)
  59. stdConsole->resetTerminal();
  60. }
  61. void StdConsole::resetTerminal()
  62. {
  63. if (stdConsoleEnabled)
  64. {
  65. /* setup the proper terminal modes */
  66. struct termios termModes;
  67. tcgetattr(stdIn, &termModes);
  68. termModes.c_lflag &= ~ICANON; // enable non-canonical mode
  69. termModes.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHOKE);
  70. termModes.c_cc[VMIN] = 0;
  71. termModes.c_cc[VTIME] = 5;
  72. tcsetattr(stdIn, TCSAFLUSH, &termModes);
  73. }
  74. }
  75. void StdConsole::enable(bool enabled)
  76. {
  77. if (enabled && !stdConsoleEnabled)
  78. {
  79. stdConsoleEnabled = true;
  80. // install signal handler for sigcont
  81. signal(SIGCONT, &signalHandler);
  82. // save the terminal state
  83. if (originalTermState == NULL)
  84. originalTermState = new termios;
  85. tcgetattr(stdIn, originalTermState);
  86. // put the terminal into our preferred mode
  87. resetTerminal();
  88. printf("%s", Con::getVariable("Con::Prompt"));
  89. }
  90. else if (!enabled && stdConsoleEnabled)
  91. {
  92. stdConsoleEnabled = false;
  93. // uninstall signal handler
  94. signal(SIGCONT, SIG_DFL);
  95. // reset the original terminal state
  96. if (originalTermState != NULL)
  97. tcsetattr(stdIn, TCSANOW, originalTermState);
  98. }
  99. }
  100. void StdConsole::enableInput( bool enabled )
  101. {
  102. stdConsoleInputEnabled = enabled;
  103. }
  104. bool StdConsole::isEnabled()
  105. {
  106. if ( stdConsole )
  107. return stdConsole->stdConsoleEnabled;
  108. return false;
  109. }
  110. static void stdConsoleConsumer(/*ConsoleLogEntry::Level*/ U32, const char *line)
  111. {
  112. stdConsole->processConsoleLine(line);
  113. }
  114. StdConsole::StdConsole()
  115. {
  116. for (S32 iIndex = 0; iIndex < MAX_CMDS; iIndex ++)
  117. rgCmds[iIndex][0] = '\0';
  118. stdOut = dup(1);
  119. stdIn = dup(0);
  120. stdErr = dup(2);
  121. // Ensure in buffer is null terminated after allocation
  122. inbuf[0] = 0x00;
  123. iCmdIndex = 0;
  124. stdConsoleEnabled = false;
  125. stdConsoleInputEnabled = false;
  126. Con::addConsumer(stdConsoleConsumer);
  127. inpos = 0;
  128. lineOutput = false;
  129. inBackground = false;
  130. originalTermState = NULL;
  131. Process::notify(this, &StdConsole::process, PROCESS_LAST_ORDER);
  132. }
  133. StdConsole::~StdConsole()
  134. {
  135. Con::removeConsumer(stdConsoleConsumer);
  136. if (stdConsoleEnabled)
  137. enable(false);
  138. if (originalTermState != NULL)
  139. {
  140. delete originalTermState;
  141. originalTermState = NULL;
  142. }
  143. }
  144. void StdConsole::printf(const char *s, ...)
  145. {
  146. // Get the line into a buffer.
  147. static const int BufSize = 4096;
  148. static char buffer[BufSize];
  149. va_list args;
  150. va_start(args, s);
  151. vsnprintf(buffer, BufSize, s, args);
  152. va_end(args);
  153. // Replace tabs with carats, like the "real" console does.
  154. char *pos = buffer;
  155. while (*pos) {
  156. if (*pos == '\t') {
  157. *pos = '^';
  158. }
  159. pos++;
  160. }
  161. // Axe the color characters.
  162. Con::stripColorChars(buffer);
  163. // Print it.
  164. write(stdOut, buffer, strlen(buffer));
  165. fflush(stdout);
  166. }
  167. void StdConsole::processConsoleLine(const char *consoleLine)
  168. {
  169. if(stdConsoleEnabled)
  170. {
  171. if(lineOutput)
  172. printf("%s\n", consoleLine);
  173. else
  174. {
  175. // Clear current line before outputting the console line. This prevents prompt text from overflowing onto normal output.
  176. printf("%c[2K", 27);
  177. printf("%c%s\n%s%s", '\r', consoleLine, Con::getVariable("Con::Prompt"), inbuf);
  178. }
  179. }
  180. }
  181. void StdConsole::process()
  182. {
  183. if(stdConsoleEnabled)
  184. {
  185. //U16 key;
  186. char typedData[64]; // damn, if you can type this fast... :-D
  187. if (UUtils->inBackground())
  188. // we don't have the terminal
  189. inBackground = true;
  190. else
  191. {
  192. // if we were in the background, reset the terminal
  193. if (inBackground)
  194. resetTerminal();
  195. inBackground = false;
  196. }
  197. // see if stdIn has any input waiting
  198. // mojo for select call
  199. fd_set rfds;
  200. struct timeval tv;
  201. FD_ZERO(&rfds);
  202. FD_SET(stdIn, &rfds);
  203. // don't wait at all in select
  204. tv.tv_sec = 0;
  205. tv.tv_usec = 0;
  206. int numEvents = select(stdIn+1, &rfds, NULL, NULL, &tv);
  207. if (numEvents <= 0)
  208. // no data available
  209. return;
  210. numEvents = read(stdIn, typedData, 64);
  211. if (numEvents == -1)
  212. return;
  213. // TODO LINUX, when debug in qtCreator some times we get false console inputs.
  214. if( !stdConsoleInputEnabled )
  215. return;
  216. typedData[numEvents] = '\0';
  217. if (numEvents > 0)
  218. {
  219. char outbuf[512];
  220. S32 outpos = 0;
  221. for (int i = 0; i < numEvents; i++)
  222. {
  223. switch(typedData[i])
  224. {
  225. case 8:
  226. case 127:
  227. /* backspace */
  228. if (inpos > 0)
  229. {
  230. outbuf[outpos++] = '\b';
  231. outbuf[outpos++] = ' ';
  232. outbuf[outpos++] = '\b';
  233. inpos--;
  234. }
  235. break;
  236. // XXX Don't know if we can detect shift-TAB. So, only handling
  237. // TAB for now.
  238. case '\t':
  239. // In the output buffer, we're going to have to erase the current line (in case
  240. // we're cycling through various completions) and write out the whole input
  241. // buffer, so (inpos * 3) + complen <= 512. Should be OK. The input buffer is
  242. // also 512 chars long so that constraint will also be fine for the input buffer.
  243. {
  244. // Erase the current line.
  245. U32 i;
  246. for (i = 0; i < inpos; i++) {
  247. outbuf[outpos++] = '\b';
  248. outbuf[outpos++] = ' ';
  249. outbuf[outpos++] = '\b';
  250. }
  251. // Modify the input buffer with the completion.
  252. U32 maxlen = 512 - (inpos * 3);
  253. inpos = Con::tabComplete(inbuf, inpos, maxlen, true);
  254. // Copy the input buffer to the output.
  255. for (i = 0; i < inpos; i++) {
  256. outbuf[outpos++] = inbuf[i];
  257. }
  258. }
  259. break;
  260. case '\n':
  261. case '\r':
  262. /* new line */
  263. outbuf[outpos++] = '\n';
  264. inbuf[inpos] = 0;
  265. outbuf[outpos] = 0;
  266. printf("%s", outbuf);
  267. S32 eventSize;
  268. eventSize = 1;
  269. {
  270. RawData rd;
  271. rd.size = inpos + 1;
  272. rd.data = (S8*) inbuf;
  273. Con::smConsoleInput.trigger(rd);
  274. }
  275. // If we've gone off the end of our array, wrap
  276. // back to the beginning
  277. if (iCmdIndex >= MAX_CMDS)
  278. iCmdIndex %= MAX_CMDS;
  279. // Put the new command into the array
  280. strcpy(rgCmds[iCmdIndex ++], inbuf);
  281. printf("%s", Con::getVariable("Con::Prompt"));
  282. inpos = outpos = 0;
  283. inbuf[0] = 0x00; // Ensure inbuf is NULL terminated after sending out command
  284. break;
  285. case 27:
  286. // JMQTODO: are these magic numbers keyboard map specific?
  287. if (typedData[i+1] == 91 || typedData[i+1] == 79)
  288. {
  289. i += 2;
  290. // an arrow key was pressed.
  291. switch(typedData[i])
  292. {
  293. case 'A':
  294. /* up arrow */
  295. // Go to the previous command in the cyclic array
  296. if ((-- iCmdIndex) < 0)
  297. iCmdIndex = MAX_CMDS - 1;
  298. // If this command isn't empty ...
  299. if (rgCmds[iCmdIndex][0] != '\0')
  300. {
  301. // Obliterate current displayed text
  302. for (S32 i = outpos = 0; i < inpos; i ++)
  303. {
  304. outbuf[outpos++] = '\b';
  305. outbuf[outpos++] = ' ';
  306. outbuf[outpos++] = '\b';
  307. }
  308. // Copy command into command and display buffers
  309. for (inpos = 0; inpos < (S32)strlen(rgCmds[iCmdIndex]); inpos++, outpos++)
  310. {
  311. outbuf[outpos] = rgCmds[iCmdIndex][inpos];
  312. inbuf [inpos ] = rgCmds[iCmdIndex][inpos];
  313. }
  314. }
  315. // If previous is empty, stay on current command
  316. else if ((++ iCmdIndex) >= MAX_CMDS)
  317. {
  318. iCmdIndex = 0;
  319. }
  320. break;
  321. case 'B':
  322. /* down arrow */
  323. // Go to the next command in the command array, if
  324. // it isn't empty
  325. if (rgCmds[iCmdIndex][0] != '\0' && (++ iCmdIndex) >= MAX_CMDS)
  326. iCmdIndex = 0;
  327. // Obliterate current displayed text
  328. for (S32 i = outpos = 0; i < inpos; i ++)
  329. {
  330. outbuf[outpos++] = '\b';
  331. outbuf[outpos++] = ' ';
  332. outbuf[outpos++] = '\b';
  333. }
  334. // Copy command into command and display buffers
  335. for (inpos = 0; inpos < (S32)strlen(rgCmds[iCmdIndex]); inpos++, outpos++)
  336. {
  337. outbuf[outpos] = rgCmds[iCmdIndex][inpos];
  338. inbuf [inpos ] = rgCmds[iCmdIndex][inpos];
  339. }
  340. break;
  341. case 'C':
  342. /* right arrow */
  343. break;
  344. case 'D':
  345. /* left arrow */
  346. break;
  347. }
  348. // read again to get rid of a bad char.
  349. //read(stdIn, &key, sizeof(char));
  350. break;
  351. } else {
  352. inbuf[inpos++] = typedData[i];
  353. outbuf[outpos++] = typedData[i];
  354. break;
  355. }
  356. break;
  357. default:
  358. inbuf[inpos++] = typedData[i];
  359. outbuf[outpos++] = typedData[i];
  360. break;
  361. }
  362. }
  363. if (outpos)
  364. {
  365. outbuf[outpos] = 0;
  366. printf("%s", outbuf);
  367. }
  368. }
  369. }
  370. }