x86UNIXConsole.cc 12 KB

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