AndroidConsole.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 "platformAndroid/platformAndroid.h"
  23. #include "platformAndroid/AndroidConsole.h"
  24. #include "platform/event.h"
  25. #include "game/gameInterface.h"
  26. #include "platform/threads/thread.h"
  27. #include <stdio.h>
  28. // TODO: convert this to use ncurses.
  29. AndroidConsole *gConsole = NULL;
  30. ConsoleFunction(enableWinConsole, void, 2, 2, "(bool enable)")
  31. {
  32. if (gConsole)
  33. gConsole->enable(dAtob(argv[1]));
  34. }
  35. ConsoleFunction(enableDebugOutput, void, 2, 2, "(bool enable)")
  36. {
  37. if (gConsole)
  38. gConsole->enableDebugOutput(dAtob(argv[1]));
  39. }
  40. static void AndroidConsoleConsumer(ConsoleLogEntry::Level, const char *line)
  41. {
  42. if (gConsole)
  43. gConsole->processConsoleLine(line);
  44. }
  45. static void AndroidConsoleInputLoopThread(S32 *arg)
  46. {
  47. if(!gConsole)
  48. return;
  49. gConsole->inputLoop();
  50. }
  51. void AndroidConsole::create()
  52. {
  53. gConsole = new AndroidConsole();
  54. }
  55. void AndroidConsole::destroy()
  56. {
  57. if (gConsole)
  58. delete gConsole;
  59. gConsole = NULL;
  60. }
  61. void AndroidConsole::enable(bool enabled)
  62. {
  63. if (gConsole == NULL) return;
  64. consoleEnabled = enabled;
  65. if(consoleEnabled)
  66. {
  67. printf("Initializing Console...\n");
  68. new Thread((ThreadRunFunction)AndroidConsoleInputLoopThread,0,true);
  69. printf("Console Initialized.\n");
  70. printf("%s", Con::getVariable("Con::Prompt"));
  71. }
  72. else
  73. {
  74. printf("Deactivating Console.");
  75. }
  76. }
  77. //%PUAP%
  78. void AndroidConsole::enableDebugOutput(bool enabled)
  79. {
  80. if (gConsole == NULL) return;
  81. debugOutputEnabled = enabled;
  82. }
  83. bool AndroidConsole::isEnabled()
  84. {
  85. if ( !gConsole )
  86. return false;
  87. return gConsole->consoleEnabled;
  88. }
  89. AndroidConsole::AndroidConsole()
  90. {
  91. consoleEnabled = false;
  92. clearInBuf();
  93. Con::addConsumer(AndroidConsoleConsumer);
  94. }
  95. AndroidConsole::~AndroidConsole()
  96. {
  97. Con::removeConsumer(AndroidConsoleConsumer);
  98. }
  99. void AndroidConsole::processConsoleLine(const char *consoleLine)
  100. {
  101. if(consoleEnabled)
  102. {
  103. adprintf("%s\n", consoleLine);
  104. }
  105. else if(debugOutputEnabled)
  106. {
  107. adprintf("%s\n", consoleLine);
  108. }
  109. }
  110. void AndroidConsole::clearInBuf()
  111. {
  112. dMemset(inBuf, 0, MaxConsoleLineSize);
  113. inBufPos=0;
  114. }
  115. void AndroidConsole::inputLoop()
  116. {
  117. Con::printf("Console Input Thread Started");
  118. unsigned char c;
  119. while(consoleEnabled)
  120. {
  121. c = fgetc(stdin);
  122. if(c == '\n')
  123. {
  124. // exec the line
  125. dStrcpy(postEvent.data, inBuf);
  126. postEvent.size = ConsoleEventHeaderSize + dStrlen(inBuf) + 1;
  127. Con::printf("=> %s",postEvent.data);
  128. Game->postEvent(postEvent);
  129. // clear the buffer
  130. clearInBuf();
  131. // display the prompt. Note that we're using real printf, not Con::printf...
  132. printf("=> ");
  133. }
  134. else
  135. {
  136. // add it to the buffer.
  137. inBuf[inBufPos++] = c;
  138. // if we're full, clear & warn.
  139. if(inBufPos >= MaxConsoleLineSize-1)
  140. {
  141. clearInBuf();
  142. Con::warnf("Line to long, discarding the last 512 bytes...");
  143. }
  144. }
  145. }
  146. Con::printf("Console Input Thread Stopped");
  147. }