ProcessUtils.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. PrintLine(message);
  94. #endif
  95. }
  96. void ErrorExit(const String& message, int exitCode)
  97. {
  98. Print(message);
  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 Print(const String& str)
  121. {
  122. /// \todo Handle Unicode
  123. printf("%s", str.CString());
  124. }
  125. void PrintLine(const String& str)
  126. {
  127. /// \todo Handle Unicode
  128. printf("%s\n", str.CString());
  129. }
  130. const Vector<String>& ParseArguments(const String& cmdLine)
  131. {
  132. arguments.Clear();
  133. unsigned cmdStart = 0, cmdEnd = 0;
  134. bool inCmd = false;
  135. bool inQuote = false;
  136. bool firstArgument = true;
  137. for (unsigned i = 0; i < cmdLine.Length(); ++i)
  138. {
  139. if (cmdLine[i] == '\"')
  140. inQuote = !inQuote;
  141. if (cmdLine[i] == ' ' && !inQuote)
  142. {
  143. if (inCmd)
  144. {
  145. inCmd = false;
  146. cmdEnd = i;
  147. // Do not store the first argument (executable name)
  148. if (!firstArgument)
  149. arguments.Push(cmdLine.Substring(cmdStart, cmdEnd - cmdStart));
  150. firstArgument = false;
  151. }
  152. }
  153. else
  154. {
  155. if (!inCmd)
  156. {
  157. inCmd = true;
  158. cmdStart = i;
  159. }
  160. }
  161. }
  162. if (inCmd)
  163. {
  164. cmdEnd = cmdLine.Length();
  165. if (!firstArgument)
  166. arguments.Push(cmdLine.Substring(cmdStart, cmdEnd - cmdStart));
  167. }
  168. return arguments;
  169. }
  170. const Vector<String>& ParseArguments(const char* cmdLine)
  171. {
  172. return ParseArguments(String(cmdLine));
  173. }
  174. const Vector<String>& ParseArguments(const WString& cmdLine)
  175. {
  176. return ParseArguments(String(cmdLine));
  177. }
  178. const Vector<String>& ParseArguments(const wchar_t* cmdLine)
  179. {
  180. return ParseArguments(String(cmdLine));
  181. }
  182. const Vector<String>& ParseArguments(int argc, char** argv)
  183. {
  184. String cmdLine;
  185. for (int i = 0; i < argc; ++i)
  186. {
  187. cmdLine += (const char*)argv[i];
  188. if (i < argc - 1)
  189. cmdLine += ' ';
  190. }
  191. return ParseArguments(cmdLine);
  192. }
  193. const Vector<String>& GetArguments()
  194. {
  195. return arguments;
  196. }
  197. String GetConsoleInput()
  198. {
  199. String ret;
  200. #ifdef WIN32
  201. HANDLE input = GetStdHandle(STD_INPUT_HANDLE);
  202. HANDLE output = GetStdHandle(STD_OUTPUT_HANDLE);
  203. if (input == INVALID_HANDLE_VALUE || output == INVALID_HANDLE_VALUE)
  204. return ret;
  205. // Use char-based input
  206. SetConsoleMode(input, ENABLE_PROCESSED_INPUT);
  207. INPUT_RECORD record;
  208. DWORD events = 0;
  209. DWORD readEvents = 0;
  210. if (!GetNumberOfConsoleInputEvents(input, &events))
  211. return ret;
  212. while (events--)
  213. {
  214. ReadConsoleInputW(input, &record, 1, &readEvents);
  215. if (record.EventType == KEY_EVENT && record.Event.KeyEvent.bKeyDown)
  216. {
  217. unsigned c = record.Event.KeyEvent.uChar.UnicodeChar;
  218. if (c)
  219. {
  220. if (c == '\b')
  221. {
  222. printf("\b \b");
  223. int length = currentLine.LengthUTF8();
  224. if (length)
  225. currentLine = currentLine.SubstringUTF8(0, length - 1);
  226. }
  227. else if (c == '\r')
  228. {
  229. printf("\n");
  230. ret = currentLine;
  231. currentLine.Clear();
  232. return ret;
  233. }
  234. else
  235. {
  236. // We have disabled echo, so echo manually
  237. wchar_t out = c;
  238. DWORD charsWritten;
  239. WriteConsoleW(output, &out, 1, &charsWritten, 0);
  240. currentLine.AppendUTF8(c);
  241. }
  242. }
  243. }
  244. }
  245. #else
  246. int flags = fcntl(STDIN_FILENO, F_GETFL);
  247. fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK);
  248. for (;;)
  249. {
  250. int ch = fgetc(stdin);
  251. if (ch >= 0 && ch != '\n')
  252. ret += (char)ch;
  253. else
  254. break;
  255. }
  256. #endif
  257. return ret;
  258. }
  259. unsigned GetNumPhysicalCPUs()
  260. {
  261. struct cpu_id_t data;
  262. GetCPUData(&data);
  263. return data.num_cores;
  264. }
  265. unsigned GetNumLogicalCPUs()
  266. {
  267. struct cpu_id_t data;
  268. GetCPUData(&data);
  269. return data.num_logical_cpus;
  270. }
  271. Mutex& GetStaticMutex()
  272. {
  273. return staticMutex;
  274. }