2
0

ProcessUtils.cpp 7.9 KB

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