x86UNIXConsole.cpp 12 KB

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