x86UNIXConsole.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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 "platformX86UNIX/platformX86UNIX.h"
  23. #include "platformX86UNIX/x86UNIXStdConsole.h"
  24. #include "platformX86UNIX/x86UNIXUtils.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. StdConsole *stdConsole = NULL;
  35. ConsoleFunction(enableWinConsole, void, 2, 2, "enableWinConsole(bool);")
  36. {
  37. argc;
  38. if (stdConsole)
  39. stdConsole->enable(dAtob(argv[1]));
  40. }
  41. void StdConsole::create()
  42. {
  43. if (stdConsole == NULL)
  44. stdConsole = new StdConsole();
  45. }
  46. void StdConsole::destroy()
  47. {
  48. if (stdConsole && stdConsole->isEnabled())
  49. stdConsole->enable(false);
  50. delete stdConsole;
  51. stdConsole = NULL;
  52. }
  53. static void signalHandler(int sigtype)
  54. {
  55. if (sigtype == SIGCONT && stdConsole != NULL)
  56. stdConsole->resetTerminal();
  57. }
  58. void StdConsole::resetTerminal()
  59. {
  60. if (stdConsoleEnabled)
  61. {
  62. /* setup the proper terminal modes */
  63. struct termios termModes;
  64. tcgetattr(stdIn, &termModes);
  65. termModes.c_lflag &= ~ICANON; // enable non-canonical mode
  66. termModes.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHOKE);
  67. termModes.c_cc[VMIN] = 0;
  68. termModes.c_cc[VTIME] = 5;
  69. tcsetattr(stdIn, TCSAFLUSH, &termModes);
  70. }
  71. }
  72. void StdConsole::enable(bool enabled)
  73. {
  74. if (enabled && !stdConsoleEnabled)
  75. {
  76. stdConsoleEnabled = true;
  77. // install signal handler for sigcont
  78. signal(SIGCONT, &signalHandler);
  79. // save the terminal state
  80. if (originalTermState == NULL)
  81. originalTermState = new termios;
  82. tcgetattr(stdIn, originalTermState);
  83. // put the terminal into our preferred mode
  84. resetTerminal();
  85. printf("%s", Con::getVariable("Con::Prompt"));
  86. }
  87. else if (!enabled && stdConsoleEnabled)
  88. {
  89. stdConsoleEnabled = false;
  90. // uninstall signal handler
  91. signal(SIGCONT, SIG_DFL);
  92. // reset the original terminal state
  93. if (originalTermState != NULL)
  94. tcsetattr(stdIn, TCSANOW, originalTermState);
  95. }
  96. }
  97. void StdConsole::enableInput( bool enabled )
  98. {
  99. stdConsoleInputEnabled = enabled;
  100. }
  101. bool StdConsole::isEnabled()
  102. {
  103. if ( stdConsole )
  104. return stdConsole->stdConsoleEnabled;
  105. return false;
  106. }
  107. static void stdConsoleConsumer(/*ConsoleLogEntry::Level*/ U32, const char *line)
  108. {
  109. stdConsole->processConsoleLine(line);
  110. }
  111. StdConsole::StdConsole()
  112. {
  113. for (S32 iIndex = 0; iIndex < MAX_CMDS; iIndex ++)
  114. rgCmds[iIndex][0] = '\0';
  115. stdOut = dup(1);
  116. stdIn = dup(0);
  117. stdErr = dup(2);
  118. iCmdIndex = 0;
  119. stdConsoleEnabled = false;
  120. stdConsoleInputEnabled = false;
  121. Con::addConsumer(stdConsoleConsumer);
  122. inpos = 0;
  123. lineOutput = false;
  124. inBackground = false;
  125. originalTermState = NULL;
  126. Process::notify(this, &StdConsole::process, PROCESS_LAST_ORDER);
  127. }
  128. StdConsole::~StdConsole()
  129. {
  130. Con::removeConsumer(stdConsoleConsumer);
  131. if (stdConsoleEnabled)
  132. enable(false);
  133. if (originalTermState != NULL)
  134. {
  135. delete originalTermState;
  136. originalTermState = NULL;
  137. }
  138. }
  139. void StdConsole::printf(const char *s, ...)
  140. {
  141. // Get the line into a buffer.
  142. static const int BufSize = 4096;
  143. static char buffer[BufSize];
  144. va_list args;
  145. va_start(args, s);
  146. vsnprintf(buffer, BufSize, s, args);
  147. va_end(args);
  148. // Replace tabs with carats, like the "real" console does.
  149. char *pos = buffer;
  150. while (*pos) {
  151. if (*pos == '\t') {
  152. *pos = '^';
  153. }
  154. pos++;
  155. }
  156. // Axe the color characters.
  157. Con::stripColorChars(buffer);
  158. // Print it.
  159. write(stdOut, buffer, strlen(buffer));
  160. fflush(stdout);
  161. }
  162. void StdConsole::processConsoleLine(const char *consoleLine)
  163. {
  164. if(stdConsoleEnabled)
  165. {
  166. inbuf[inpos] = 0;
  167. if(lineOutput)
  168. printf("%s\n", consoleLine);
  169. else
  170. printf("%c%s\n%s%s", '\r', consoleLine, Con::getVariable("Con::Prompt"), inbuf);
  171. }
  172. }
  173. void StdConsole::process()
  174. {
  175. if(stdConsoleEnabled)
  176. {
  177. //U16 key;
  178. char typedData[64]; // damn, if you can type this fast... :-D
  179. if (UUtils->inBackground())
  180. // we don't have the terminal
  181. inBackground = true;
  182. else
  183. {
  184. // if we were in the background, reset the terminal
  185. if (inBackground)
  186. resetTerminal();
  187. inBackground = false;
  188. }
  189. // see if stdIn has any input waiting
  190. // mojo for select call
  191. fd_set rfds;
  192. struct timeval tv;
  193. FD_ZERO(&rfds);
  194. FD_SET(stdIn, &rfds);
  195. // don't wait at all in select
  196. tv.tv_sec = 0;
  197. tv.tv_usec = 0;
  198. int numEvents = select(stdIn+1, &rfds, NULL, NULL, &tv);
  199. if (numEvents <= 0)
  200. // no data available
  201. return;
  202. numEvents = read(stdIn, typedData, 64);
  203. if (numEvents == -1)
  204. return;
  205. // TODO LINUX, when debug in qtCreator some times we get false console inputs.
  206. if( !stdConsoleInputEnabled )
  207. return;
  208. typedData[numEvents] = '\0';
  209. if (numEvents > 0)
  210. {
  211. char outbuf[512];
  212. S32 outpos = 0;
  213. for (int i = 0; i < numEvents; i++)
  214. {
  215. switch(typedData[i])
  216. {
  217. case 8:
  218. case 127:
  219. /* backspace */
  220. if (inpos > 0)
  221. {
  222. outbuf[outpos++] = '\b';
  223. outbuf[outpos++] = ' ';
  224. outbuf[outpos++] = '\b';
  225. inpos--;
  226. }
  227. break;
  228. // XXX Don't know if we can detect shift-TAB. So, only handling
  229. // TAB for now.
  230. case '\t':
  231. // In the output buffer, we're going to have to erase the current line (in case
  232. // we're cycling through various completions) and write out the whole input
  233. // buffer, so (inpos * 3) + complen <= 512. Should be OK. The input buffer is
  234. // also 512 chars long so that constraint will also be fine for the input buffer.
  235. {
  236. // Erase the current line.
  237. U32 i;
  238. for (i = 0; i < inpos; i++) {
  239. outbuf[outpos++] = '\b';
  240. outbuf[outpos++] = ' ';
  241. outbuf[outpos++] = '\b';
  242. }
  243. // Modify the input buffer with the completion.
  244. U32 maxlen = 512 - (inpos * 3);
  245. inpos = Con::tabComplete(inbuf, inpos, maxlen, true);
  246. // Copy the input buffer to the output.
  247. for (i = 0; i < inpos; i++) {
  248. outbuf[outpos++] = inbuf[i];
  249. }
  250. }
  251. break;
  252. case '\n':
  253. case '\r':
  254. /* new line */
  255. outbuf[outpos++] = '\n';
  256. inbuf[inpos] = 0;
  257. outbuf[outpos] = 0;
  258. printf("%s", outbuf);
  259. S32 eventSize;
  260. eventSize = 1;
  261. {
  262. RawData rd;
  263. rd.size = inpos + 1;
  264. rd.data = (S8*) inbuf;
  265. Con::smConsoleInput.trigger(rd);
  266. }
  267. // If we've gone off the end of our array, wrap
  268. // back to the beginning
  269. if (iCmdIndex >= MAX_CMDS)
  270. iCmdIndex %= MAX_CMDS;
  271. // Put the new command into the array
  272. strcpy(rgCmds[iCmdIndex ++], inbuf);
  273. printf("%s", Con::getVariable("Con::Prompt"));
  274. inpos = outpos = 0;
  275. break;
  276. case 27:
  277. // JMQTODO: are these magic numbers keyboard map specific?
  278. if (typedData[i+1] == 91 || typedData[i+1] == 79)
  279. {
  280. i += 2;
  281. // an arrow key was pressed.
  282. switch(typedData[i])
  283. {
  284. case 'A':
  285. /* up arrow */
  286. // Go to the previous command in the cyclic array
  287. if ((-- iCmdIndex) < 0)
  288. iCmdIndex = MAX_CMDS - 1;
  289. // If this command isn't empty ...
  290. if (rgCmds[iCmdIndex][0] != '\0')
  291. {
  292. // Obliterate current displayed text
  293. for (S32 i = outpos = 0; i < inpos; i ++)
  294. {
  295. outbuf[outpos++] = '\b';
  296. outbuf[outpos++] = ' ';
  297. outbuf[outpos++] = '\b';
  298. }
  299. // Copy command into command and display buffers
  300. for (inpos = 0; inpos < (S32)strlen(rgCmds[iCmdIndex]); inpos++, outpos++)
  301. {
  302. outbuf[outpos] = rgCmds[iCmdIndex][inpos];
  303. inbuf [inpos ] = rgCmds[iCmdIndex][inpos];
  304. }
  305. }
  306. // If previous is empty, stay on current command
  307. else if ((++ iCmdIndex) >= MAX_CMDS)
  308. {
  309. iCmdIndex = 0;
  310. }
  311. break;
  312. case 'B':
  313. /* down arrow */
  314. // Go to the next command in the command array, if
  315. // it isn't empty
  316. if (rgCmds[iCmdIndex][0] != '\0' && (++ iCmdIndex) >= MAX_CMDS)
  317. iCmdIndex = 0;
  318. // Obliterate current displayed text
  319. for (S32 i = outpos = 0; i < inpos; i ++)
  320. {
  321. outbuf[outpos++] = '\b';
  322. outbuf[outpos++] = ' ';
  323. outbuf[outpos++] = '\b';
  324. }
  325. // Copy command into command and display buffers
  326. for (inpos = 0; inpos < (S32)strlen(rgCmds[iCmdIndex]); inpos++, outpos++)
  327. {
  328. outbuf[outpos] = rgCmds[iCmdIndex][inpos];
  329. inbuf [inpos ] = rgCmds[iCmdIndex][inpos];
  330. }
  331. break;
  332. case 'C':
  333. /* right arrow */
  334. break;
  335. case 'D':
  336. /* left arrow */
  337. break;
  338. }
  339. // read again to get rid of a bad char.
  340. //read(stdIn, &key, sizeof(char));
  341. break;
  342. } else {
  343. inbuf[inpos++] = typedData[i];
  344. outbuf[outpos++] = typedData[i];
  345. break;
  346. }
  347. break;
  348. default:
  349. inbuf[inpos++] = typedData[i];
  350. outbuf[outpos++] = typedData[i];
  351. break;
  352. }
  353. }
  354. if (outpos)
  355. {
  356. outbuf[outpos] = 0;
  357. printf("%s", outbuf);
  358. }
  359. }
  360. }
  361. }