ProcessUtils.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Precompiled.h"
  24. #include "Mutex.h"
  25. #include "ProcessUtils.h"
  26. #include <cstdio>
  27. #include <cstdlib>
  28. #include <fcntl.h>
  29. #include <libcpuid.h>
  30. #ifdef WIN32
  31. #include <windows.h>
  32. #include <io.h>
  33. #else
  34. #include <unistd.h>
  35. #endif
  36. #ifdef _MSC_VER
  37. #include <float.h>
  38. #else
  39. // From http://stereopsis.com/FPU.html
  40. #define FPU_CW_PREC_MASK 0x0300
  41. #define FPU_CW_PREC_SINGLE 0x0000
  42. #define FPU_CW_PREC_DOUBLE 0x0200
  43. #define FPU_CW_PREC_EXTENDED 0x0300
  44. #define FPU_CW_ROUND_MASK 0x0c00
  45. #define FPU_CW_ROUND_NEAR 0x0000
  46. #define FPU_CW_ROUND_DOWN 0x0400
  47. #define FPU_CW_ROUND_UP 0x0800
  48. #define FPU_CW_ROUND_CHOP 0x0c00
  49. inline unsigned GetFPUState()
  50. {
  51. unsigned control = 0;
  52. __asm__ __volatile__ ("fnstcw %0" : "=m" (control));
  53. return control;
  54. }
  55. inline void SetFPUState(unsigned control)
  56. {
  57. __asm__ __volatile__ ("fldcw %0" : : "m" (control));
  58. }
  59. #endif
  60. #include "DebugNew.h"
  61. #ifdef WIN32
  62. static bool consoleOpened = false;
  63. #endif
  64. static String currentLine;
  65. static Vector<String> arguments;
  66. static Mutex staticMutex;
  67. void GetCPUData(struct cpu_id_t* data)
  68. {
  69. if (cpu_identify(0, data) < 0)
  70. {
  71. data->num_logical_cpus = 1;
  72. data->num_cores = 1;
  73. }
  74. }
  75. void InitFPU()
  76. {
  77. // Make sure FPU is in round-to-nearest, single precision mode
  78. // This is needed for ODE to behave predictably in float mode
  79. #ifdef _MSC_VER
  80. _controlfp(_RC_NEAR | _PC_24, _MCW_RC | _MCW_PC);
  81. #else
  82. unsigned control = GetFPUState();
  83. control &= ~(FPU_CW_PREC_MASK | FPU_CW_ROUND_MASK);
  84. control |= (FPU_CW_PREC_SINGLE | FPU_CW_ROUND_NEAR);
  85. SetFPUState(control);
  86. #endif
  87. }
  88. void ErrorDialog(const char* title, const char* message)
  89. {
  90. #ifdef WIN32
  91. MessageBox(0, message, title, 0);
  92. #else
  93. printf("%s\n", message);
  94. #endif
  95. }
  96. void ErrorExit(const String& message, int exitCode)
  97. {
  98. printf("%s", message.CString());
  99. exit(exitCode);
  100. }
  101. void OpenConsoleWindow()
  102. {
  103. #ifdef WIN32
  104. if (consoleOpened)
  105. return;
  106. AllocConsole();
  107. HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
  108. int hCrt = _open_osfhandle((long)hOut, _O_TEXT);
  109. FILE* outFile = _fdopen(hCrt, "w");
  110. setvbuf(outFile, NULL, _IONBF, 1);
  111. *stdout = *outFile;
  112. HANDLE hIn = GetStdHandle(STD_INPUT_HANDLE);
  113. hCrt = _open_osfhandle((long)hIn, _O_TEXT);
  114. FILE* inFile = _fdopen(hCrt, "r");
  115. setvbuf(inFile, NULL, _IONBF, 128);
  116. *stdin = *inFile;
  117. consoleOpened = true;
  118. #endif
  119. }
  120. void PrintLine(const String& str)
  121. {
  122. printf("%s\n", str.CString());
  123. }
  124. void PrintLine(const char* str)
  125. {
  126. printf("%s\n", str);
  127. }
  128. const Vector<String>& ParseArguments(const char* cmdLine)
  129. {
  130. arguments.Clear();
  131. if (!cmdLine)
  132. return arguments;
  133. String cmdStr(cmdLine);
  134. unsigned cmdStart = 0, cmdEnd = 0;
  135. bool inCmd = false;
  136. bool inQuote = false;
  137. for (unsigned i = 0; i < cmdStr.Length(); ++i)
  138. {
  139. if (cmdStr[i] == '\"')
  140. inQuote = !inQuote;
  141. if (cmdStr[i] == ' ' && !inQuote)
  142. {
  143. if (inCmd)
  144. {
  145. inCmd = false;
  146. cmdEnd = i;
  147. arguments.Push(cmdStr.Substring(cmdStart, cmdEnd - cmdStart));
  148. }
  149. }
  150. else
  151. {
  152. if (!inCmd)
  153. {
  154. inCmd = true;
  155. cmdStart = i;
  156. }
  157. }
  158. }
  159. if (inCmd)
  160. {
  161. cmdEnd = cmdStr.Length();
  162. arguments.Push(cmdStr.Substring(cmdStart, cmdEnd - cmdStart));
  163. }
  164. return arguments;
  165. }
  166. const Vector<String>& GetArguments()
  167. {
  168. return arguments;
  169. }
  170. String GetConsoleInput()
  171. {
  172. String ret;
  173. #ifdef WIN32
  174. HANDLE input = GetStdHandle(STD_INPUT_HANDLE);
  175. if (input == INVALID_HANDLE_VALUE)
  176. return ret;
  177. // Use char-based input
  178. SetConsoleMode(input, ENABLE_PROCESSED_INPUT);
  179. INPUT_RECORD record;
  180. DWORD events = 0;
  181. DWORD readEvents = 0;
  182. if (!GetNumberOfConsoleInputEvents(input, &events))
  183. return ret;
  184. while (events--)
  185. {
  186. ReadConsoleInput(input, &record, 1, &readEvents);
  187. if (record.EventType == KEY_EVENT && record.Event.KeyEvent.bKeyDown)
  188. {
  189. char c = record.Event.KeyEvent.uChar.AsciiChar;
  190. if (c)
  191. {
  192. if (c == '\b')
  193. {
  194. printf("\b \b");
  195. int length = currentLine.Length();
  196. if (length)
  197. currentLine.Resize(length - 1);
  198. }
  199. else if (c == '\r')
  200. {
  201. printf("\n");
  202. ret = currentLine;
  203. currentLine.Clear();
  204. return ret;
  205. }
  206. else
  207. {
  208. // We have disabled echo, so echo manually
  209. printf("%c", c);
  210. currentLine += c;
  211. }
  212. }
  213. }
  214. }
  215. #else
  216. int flags = fcntl(STDIN_FILENO, F_GETFL);
  217. fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK);
  218. for (;;)
  219. {
  220. int ch = fgetc(stdin);
  221. if (ch >= 0 && ch != '\n')
  222. ret += (char)ch;
  223. else
  224. break;
  225. }
  226. #endif
  227. return ret;
  228. }
  229. unsigned GetNumPhysicalCPUs()
  230. {
  231. struct cpu_id_t data;
  232. GetCPUData(&data);
  233. return data.num_cores;
  234. }
  235. unsigned GetNumLogicalCPUs()
  236. {
  237. struct cpu_id_t data;
  238. GetCPUData(&data);
  239. return data.num_logical_cpus;
  240. }
  241. Mutex& GetStaticMutex()
  242. {
  243. return staticMutex;
  244. }