x86UNIXConsole.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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. // Replace tabs with carats, like the "real" console does.
  148. char *pos = buffer;
  149. while (*pos) {
  150. if (*pos == '\t') {
  151. *pos = '^';
  152. }
  153. pos++;
  154. }
  155. // Axe the color characters.
  156. Con::stripColorChars(buffer);
  157. // Print it.
  158. write(stdOut, buffer, strlen(buffer));
  159. fflush(stdout);
  160. }
  161. void StdConsole::processConsoleLine(const char *consoleLine)
  162. {
  163. if(stdConsoleEnabled)
  164. {
  165. inbuf[inpos] = 0;
  166. if(lineOutput)
  167. printf("%s\n", consoleLine);
  168. else
  169. printf("%c%s\n%s%s", '\r', consoleLine, Con::getVariable("Con::Prompt"), inbuf);
  170. }
  171. }
  172. void StdConsole::process()
  173. {
  174. if(stdConsoleEnabled)
  175. {
  176. //U16 key;
  177. char typedData[64]; // damn, if you can type this fast... :-D
  178. if (UUtils->inBackground())
  179. // we don't have the terminal
  180. inBackground = true;
  181. else
  182. {
  183. // if we were in the background, reset the terminal
  184. if (inBackground)
  185. resetTerminal();
  186. inBackground = false;
  187. }
  188. // see if stdIn has any input waiting
  189. // mojo for select call
  190. fd_set rfds;
  191. struct timeval tv;
  192. FD_ZERO(&rfds);
  193. FD_SET(stdIn, &rfds);
  194. // don't wait at all in select
  195. tv.tv_sec = 0;
  196. tv.tv_usec = 0;
  197. int numEvents = select(stdIn+1, &rfds, NULL, NULL, &tv);
  198. if (numEvents <= 0)
  199. // no data available
  200. return;
  201. numEvents = read(stdIn, typedData, 64);
  202. if (numEvents == -1)
  203. return;
  204. // TODO LINUX, when debug in qtCreator some times we get false console inputs.
  205. if( !stdConsoleInputEnabled )
  206. return;
  207. typedData[numEvents] = '\0';
  208. if (numEvents > 0)
  209. {
  210. char outbuf[512];
  211. S32 outpos = 0;
  212. for (int i = 0; i < numEvents; i++)
  213. {
  214. switch(typedData[i])
  215. {
  216. case 8:
  217. case 127:
  218. /* backspace */
  219. if (inpos > 0)
  220. {
  221. outbuf[outpos++] = '\b';
  222. outbuf[outpos++] = ' ';
  223. outbuf[outpos++] = '\b';
  224. inpos--;
  225. }
  226. break;
  227. // XXX Don't know if we can detect shift-TAB. So, only handling
  228. // TAB for now.
  229. case '\t':
  230. // In the output buffer, we're going to have to erase the current line (in case
  231. // we're cycling through various completions) and write out the whole input
  232. // buffer, so (inpos * 3) + complen <= 512. Should be OK. The input buffer is
  233. // also 512 chars long so that constraint will also be fine for the input buffer.
  234. {
  235. // Erase the current line.
  236. U32 i;
  237. for (i = 0; i < inpos; i++) {
  238. outbuf[outpos++] = '\b';
  239. outbuf[outpos++] = ' ';
  240. outbuf[outpos++] = '\b';
  241. }
  242. // Modify the input buffer with the completion.
  243. U32 maxlen = 512 - (inpos * 3);
  244. inpos = Con::tabComplete(inbuf, inpos, maxlen, true);
  245. // Copy the input buffer to the output.
  246. for (i = 0; i < inpos; i++) {
  247. outbuf[outpos++] = inbuf[i];
  248. }
  249. }
  250. break;
  251. case '\n':
  252. case '\r':
  253. /* new line */
  254. outbuf[outpos++] = '\n';
  255. inbuf[inpos] = 0;
  256. outbuf[outpos] = 0;
  257. printf("%s", outbuf);
  258. S32 eventSize;
  259. eventSize = 1;
  260. {
  261. RawData rd;
  262. rd.size = inpos + 1;
  263. rd.data = (S8*) inbuf;
  264. Con::smConsoleInput.trigger(rd);
  265. }
  266. // If we've gone off the end of our array, wrap
  267. // back to the beginning
  268. if (iCmdIndex >= MAX_CMDS)
  269. iCmdIndex %= MAX_CMDS;
  270. // Put the new command into the array
  271. strcpy(rgCmds[iCmdIndex ++], inbuf);
  272. printf("%s", Con::getVariable("Con::Prompt"));
  273. inpos = outpos = 0;
  274. break;
  275. case 27:
  276. // JMQTODO: are these magic numbers keyboard map specific?
  277. if (typedData[i+1] == 91 || typedData[i+1] == 79)
  278. {
  279. i += 2;
  280. // an arrow key was pressed.
  281. switch(typedData[i])
  282. {
  283. case 'A':
  284. /* up arrow */
  285. // Go to the previous command in the cyclic array
  286. if ((-- iCmdIndex) < 0)
  287. iCmdIndex = MAX_CMDS - 1;
  288. // If this command isn't empty ...
  289. if (rgCmds[iCmdIndex][0] != '\0')
  290. {
  291. // Obliterate current displayed text
  292. for (S32 i = outpos = 0; i < inpos; i ++)
  293. {
  294. outbuf[outpos++] = '\b';
  295. outbuf[outpos++] = ' ';
  296. outbuf[outpos++] = '\b';
  297. }
  298. // Copy command into command and display buffers
  299. for (inpos = 0; inpos < (S32)strlen(rgCmds[iCmdIndex]); inpos++, outpos++)
  300. {
  301. outbuf[outpos] = rgCmds[iCmdIndex][inpos];
  302. inbuf [inpos ] = rgCmds[iCmdIndex][inpos];
  303. }
  304. }
  305. // If previous is empty, stay on current command
  306. else if ((++ iCmdIndex) >= MAX_CMDS)
  307. {
  308. iCmdIndex = 0;
  309. }
  310. break;
  311. case 'B':
  312. /* down arrow */
  313. // Go to the next command in the command array, if
  314. // it isn't empty
  315. if (rgCmds[iCmdIndex][0] != '\0' && (++ iCmdIndex) >= MAX_CMDS)
  316. iCmdIndex = 0;
  317. // Obliterate current displayed text
  318. for (S32 i = outpos = 0; i < inpos; i ++)
  319. {
  320. outbuf[outpos++] = '\b';
  321. outbuf[outpos++] = ' ';
  322. outbuf[outpos++] = '\b';
  323. }
  324. // Copy command into command and display buffers
  325. for (inpos = 0; inpos < (S32)strlen(rgCmds[iCmdIndex]); inpos++, outpos++)
  326. {
  327. outbuf[outpos] = rgCmds[iCmdIndex][inpos];
  328. inbuf [inpos ] = rgCmds[iCmdIndex][inpos];
  329. }
  330. break;
  331. case 'C':
  332. /* right arrow */
  333. break;
  334. case 'D':
  335. /* left arrow */
  336. break;
  337. }
  338. // read again to get rid of a bad char.
  339. //read(stdIn, &key, sizeof(char));
  340. break;
  341. } else {
  342. inbuf[inpos++] = typedData[i];
  343. outbuf[outpos++] = typedData[i];
  344. break;
  345. }
  346. break;
  347. default:
  348. inbuf[inpos++] = typedData[i];
  349. outbuf[outpos++] = typedData[i];
  350. break;
  351. }
  352. }
  353. if (outpos)
  354. {
  355. outbuf[outpos] = 0;
  356. printf("%s", outbuf);
  357. }
  358. }
  359. }
  360. }