ProcessUtils.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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 String& title, const String& message)
  89. {
  90. #ifdef WIN32
  91. MessageBoxW(0, WString(message).CString(), WString(title).CString(), 0);
  92. #else
  93. printf("%s\n", message.CString());
  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 String& cmdLine)
  129. {
  130. arguments.Clear();
  131. unsigned cmdStart = 0, cmdEnd = 0;
  132. bool inCmd = false;
  133. bool inQuote = false;
  134. for (unsigned i = 0; i < cmdLine.Length(); ++i)
  135. {
  136. if (cmdLine[i] == '\"')
  137. inQuote = !inQuote;
  138. if (cmdLine[i] == ' ' && !inQuote)
  139. {
  140. if (inCmd)
  141. {
  142. inCmd = false;
  143. cmdEnd = i;
  144. arguments.Push(cmdLine.Substring(cmdStart, cmdEnd - cmdStart));
  145. }
  146. }
  147. else
  148. {
  149. if (!inCmd)
  150. {
  151. inCmd = true;
  152. cmdStart = i;
  153. }
  154. }
  155. }
  156. if (inCmd)
  157. {
  158. cmdEnd = cmdLine.Length();
  159. arguments.Push(cmdLine.Substring(cmdStart, cmdEnd - cmdStart));
  160. }
  161. return arguments;
  162. }
  163. const Vector<String>& GetArguments()
  164. {
  165. return arguments;
  166. }
  167. String GetConsoleInput()
  168. {
  169. String ret;
  170. #ifdef WIN32
  171. HANDLE input = GetStdHandle(STD_INPUT_HANDLE);
  172. if (input == INVALID_HANDLE_VALUE)
  173. return ret;
  174. // Use char-based input
  175. SetConsoleMode(input, ENABLE_PROCESSED_INPUT);
  176. INPUT_RECORD record;
  177. DWORD events = 0;
  178. DWORD readEvents = 0;
  179. if (!GetNumberOfConsoleInputEvents(input, &events))
  180. return ret;
  181. while (events--)
  182. {
  183. ReadConsoleInput(input, &record, 1, &readEvents);
  184. if (record.EventType == KEY_EVENT && record.Event.KeyEvent.bKeyDown)
  185. {
  186. char c = record.Event.KeyEvent.uChar.AsciiChar;
  187. if (c)
  188. {
  189. if (c == '\b')
  190. {
  191. printf("\b \b");
  192. int length = currentLine.Length();
  193. if (length)
  194. currentLine.Resize(length - 1);
  195. }
  196. else if (c == '\r')
  197. {
  198. printf("\n");
  199. ret = currentLine;
  200. currentLine.Clear();
  201. return ret;
  202. }
  203. else
  204. {
  205. // We have disabled echo, so echo manually
  206. printf("%c", c);
  207. currentLine += c;
  208. }
  209. }
  210. }
  211. }
  212. #else
  213. int flags = fcntl(STDIN_FILENO, F_GETFL);
  214. fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK);
  215. for (;;)
  216. {
  217. int ch = fgetc(stdin);
  218. if (ch >= 0 && ch != '\n')
  219. ret += (char)ch;
  220. else
  221. break;
  222. }
  223. #endif
  224. return ret;
  225. }
  226. unsigned GetNumPhysicalCPUs()
  227. {
  228. struct cpu_id_t data;
  229. GetCPUData(&data);
  230. return data.num_cores;
  231. }
  232. unsigned GetNumLogicalCPUs()
  233. {
  234. struct cpu_id_t data;
  235. GetCPUData(&data);
  236. return data.num_logical_cpus;
  237. }
  238. Mutex& GetStaticMutex()
  239. {
  240. return staticMutex;
  241. }