ProcessUtils.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. //
  2. // Copyright (c) 2008-2015 the Urho3D project.
  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 deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // 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 FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "../Core/Mutex.h"
  23. #include "../Core/ProcessUtils.h"
  24. #include "../Math/MathDefs.h"
  25. #include <cstdio>
  26. #include <fcntl.h>
  27. #ifdef __APPLE__
  28. #include "TargetConditionals.h"
  29. #endif
  30. #if defined(IOS)
  31. #include <mach/mach_host.h>
  32. #elif !defined(ANDROID) && !defined(RPI)
  33. #include <LibCpuId/libcpuid.h>
  34. #endif
  35. #ifdef WIN32
  36. #include <windows.h>
  37. #include <io.h>
  38. #else
  39. #include <unistd.h>
  40. #endif
  41. #if defined(_MSC_VER)
  42. #include <float.h>
  43. #elif !defined(ANDROID) && !defined(IOS) && !defined(RPI) && !defined(EMSCRIPTEN)
  44. // From http://stereopsis.com/FPU.html
  45. #define FPU_CW_PREC_MASK 0x0300
  46. #define FPU_CW_PREC_SINGLE 0x0000
  47. #define FPU_CW_PREC_DOUBLE 0x0200
  48. #define FPU_CW_PREC_EXTENDED 0x0300
  49. #define FPU_CW_ROUND_MASK 0x0c00
  50. #define FPU_CW_ROUND_NEAR 0x0000
  51. #define FPU_CW_ROUND_DOWN 0x0400
  52. #define FPU_CW_ROUND_UP 0x0800
  53. #define FPU_CW_ROUND_CHOP 0x0c00
  54. inline unsigned GetFPUState()
  55. {
  56. unsigned control = 0;
  57. __asm__ __volatile__ ("fnstcw %0" : "=m" (control));
  58. return control;
  59. }
  60. inline void SetFPUState(unsigned control)
  61. {
  62. __asm__ __volatile__ ("fldcw %0" : : "m" (control));
  63. }
  64. #endif
  65. #include "../DebugNew.h"
  66. namespace Urho3D
  67. {
  68. #ifdef WIN32
  69. static bool consoleOpened = false;
  70. #endif
  71. static String currentLine;
  72. static Vector<String> arguments;
  73. #if defined(IOS)
  74. static void GetCPUData(host_basic_info_data_t* data)
  75. {
  76. mach_msg_type_number_t infoCount;
  77. infoCount = HOST_BASIC_INFO_COUNT;
  78. host_info(mach_host_self(), HOST_BASIC_INFO, (host_info_t)data, &infoCount);
  79. }
  80. #elif !defined(ANDROID) && !defined(RPI) && !defined(EMSCRIPTEN)
  81. static void GetCPUData(struct cpu_id_t* data)
  82. {
  83. if (cpu_identify(0, data) < 0)
  84. {
  85. data->num_logical_cpus = 1;
  86. data->num_cores = 1;
  87. }
  88. }
  89. #endif
  90. void InitFPU()
  91. {
  92. #if !defined(URHO3D_LUAJIT) && !defined(ANDROID) && !defined(IOS) && !defined(RPI) && !defined(__x86_64__) && !defined(_M_AMD64) && !defined(EMSCRIPTEN)
  93. // Make sure FPU is in round-to-nearest, single precision mode
  94. // This ensures Direct3D and OpenGL behave similarly, and all threads behave similarly
  95. #ifdef _MSC_VER
  96. _controlfp(_RC_NEAR | _PC_24, _MCW_RC | _MCW_PC);
  97. #else
  98. unsigned control = GetFPUState();
  99. control &= ~(FPU_CW_PREC_MASK | FPU_CW_ROUND_MASK);
  100. control |= (FPU_CW_PREC_SINGLE | FPU_CW_ROUND_NEAR);
  101. SetFPUState(control);
  102. #endif
  103. #endif
  104. }
  105. void ErrorDialog(const String& title, const String& message)
  106. {
  107. #ifdef WIN32
  108. MessageBoxW(0, WString(message).CString(), WString(title).CString(), 0);
  109. #else
  110. PrintLine(message, true);
  111. #endif
  112. }
  113. void ErrorExit(const String& message, int exitCode)
  114. {
  115. if (!message.Empty())
  116. PrintLine(message, true);
  117. exit(exitCode);
  118. }
  119. void OpenConsoleWindow()
  120. {
  121. #ifdef WIN32
  122. if (consoleOpened)
  123. return;
  124. AllocConsole();
  125. HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
  126. int hCrt = _open_osfhandle((size_t)hOut, _O_TEXT);
  127. FILE* outFile = _fdopen(hCrt, "w");
  128. setvbuf(outFile, NULL, _IONBF, 1);
  129. *stdout = *outFile;
  130. HANDLE hIn = GetStdHandle(STD_INPUT_HANDLE);
  131. hCrt = _open_osfhandle((size_t)hIn, _O_TEXT);
  132. FILE* inFile = _fdopen(hCrt, "r");
  133. setvbuf(inFile, NULL, _IONBF, 128);
  134. *stdin = *inFile;
  135. consoleOpened = true;
  136. #endif
  137. }
  138. void PrintUnicode(const String& str, bool error)
  139. {
  140. #if !defined(ANDROID) && !defined(IOS)
  141. #ifdef WIN32
  142. HANDLE stream = GetStdHandle(error ? STD_ERROR_HANDLE : STD_OUTPUT_HANDLE);
  143. if (stream == INVALID_HANDLE_VALUE)
  144. return;
  145. WString strW(str);
  146. DWORD charsWritten;
  147. WriteConsoleW(stream, strW.CString(), strW.Length(), &charsWritten, 0);
  148. #else
  149. fprintf(error ? stderr : stdout, "%s", str.CString());
  150. #endif
  151. #endif
  152. }
  153. void PrintUnicodeLine(const String& str, bool error)
  154. {
  155. PrintUnicode(str + '\n', error);
  156. }
  157. void PrintLine(const String& str, bool error)
  158. {
  159. #if !defined(ANDROID) && !defined(IOS)
  160. fprintf(error ? stderr: stdout, "%s\n", str.CString());
  161. #endif
  162. }
  163. const Vector<String>& ParseArguments(const String& cmdLine, bool skipFirstArgument)
  164. {
  165. arguments.Clear();
  166. unsigned cmdStart = 0, cmdEnd = 0;
  167. bool inCmd = false;
  168. bool inQuote = false;
  169. for (unsigned i = 0; i < cmdLine.Length(); ++i)
  170. {
  171. if (cmdLine[i] == '\"')
  172. inQuote = !inQuote;
  173. if (cmdLine[i] == ' ' && !inQuote)
  174. {
  175. if (inCmd)
  176. {
  177. inCmd = false;
  178. cmdEnd = i;
  179. // Do not store the first argument (executable name)
  180. if (!skipFirstArgument)
  181. arguments.Push(cmdLine.Substring(cmdStart, cmdEnd - cmdStart));
  182. skipFirstArgument = false;
  183. }
  184. }
  185. else
  186. {
  187. if (!inCmd)
  188. {
  189. inCmd = true;
  190. cmdStart = i;
  191. }
  192. }
  193. }
  194. if (inCmd)
  195. {
  196. cmdEnd = cmdLine.Length();
  197. if (!skipFirstArgument)
  198. arguments.Push(cmdLine.Substring(cmdStart, cmdEnd - cmdStart));
  199. }
  200. // Strip double quotes from the arguments
  201. for (unsigned i = 0; i < arguments.Size(); ++i)
  202. arguments[i].Replace("\"", "");
  203. return arguments;
  204. }
  205. const Vector<String>& ParseArguments(const char* cmdLine)
  206. {
  207. return ParseArguments(String(cmdLine));
  208. }
  209. const Vector<String>& ParseArguments(const WString& cmdLine)
  210. {
  211. return ParseArguments(String(cmdLine));
  212. }
  213. const Vector<String>& ParseArguments(const wchar_t* cmdLine)
  214. {
  215. return ParseArguments(String(cmdLine));
  216. }
  217. const Vector<String>& ParseArguments(int argc, char** argv)
  218. {
  219. String cmdLine;
  220. for (int i = 0; i < argc; ++i)
  221. cmdLine.AppendWithFormat("\"%s\" ", (const char*)argv[i]);
  222. return ParseArguments(cmdLine);
  223. }
  224. const Vector<String>& GetArguments()
  225. {
  226. return arguments;
  227. }
  228. String GetConsoleInput()
  229. {
  230. String ret;
  231. #ifdef URHO3D_TESTING
  232. // When we are running automated tests, reading the console may block. Just return empty in that case
  233. return ret;
  234. #endif
  235. #ifdef WIN32
  236. HANDLE input = GetStdHandle(STD_INPUT_HANDLE);
  237. HANDLE output = GetStdHandle(STD_OUTPUT_HANDLE);
  238. if (input == INVALID_HANDLE_VALUE || output == INVALID_HANDLE_VALUE)
  239. return ret;
  240. // Use char-based input
  241. SetConsoleMode(input, ENABLE_PROCESSED_INPUT);
  242. INPUT_RECORD record;
  243. DWORD events = 0;
  244. DWORD readEvents = 0;
  245. if (!GetNumberOfConsoleInputEvents(input, &events))
  246. return ret;
  247. while (events--)
  248. {
  249. ReadConsoleInputW(input, &record, 1, &readEvents);
  250. if (record.EventType == KEY_EVENT && record.Event.KeyEvent.bKeyDown)
  251. {
  252. unsigned c = record.Event.KeyEvent.uChar.UnicodeChar;
  253. if (c)
  254. {
  255. if (c == '\b')
  256. {
  257. PrintUnicode("\b \b");
  258. int length = currentLine.LengthUTF8();
  259. if (length)
  260. currentLine = currentLine.SubstringUTF8(0, length - 1);
  261. }
  262. else if (c == '\r')
  263. {
  264. PrintUnicode("\n");
  265. ret = currentLine;
  266. currentLine.Clear();
  267. return ret;
  268. }
  269. else
  270. {
  271. // We have disabled echo, so echo manually
  272. wchar_t out = c;
  273. DWORD charsWritten;
  274. WriteConsoleW(output, &out, 1, &charsWritten, 0);
  275. currentLine.AppendUTF8(c);
  276. }
  277. }
  278. }
  279. }
  280. #elif !defined(ANDROID) && !defined(IOS)
  281. int flags = fcntl(STDIN_FILENO, F_GETFL);
  282. fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK);
  283. for (;;)
  284. {
  285. int ch = fgetc(stdin);
  286. if (ch >= 0 && ch != '\n')
  287. ret += (char)ch;
  288. else
  289. break;
  290. }
  291. #endif
  292. return ret;
  293. }
  294. String GetPlatform()
  295. {
  296. #if defined(ANDROID)
  297. return "Android";
  298. #elif defined(IOS)
  299. return "iOS";
  300. #elif defined(WIN32)
  301. return "Windows";
  302. #elif defined(__APPLE__)
  303. return "Mac OS X";
  304. #elif defined(RPI)
  305. return "Raspberry Pi";
  306. #elif defined(__linux__)
  307. return "Linux";
  308. #else
  309. return String::EMPTY;
  310. #endif
  311. }
  312. #ifdef ANDROID
  313. static unsigned GetAndroidCPUCount()
  314. {
  315. FILE* fp;
  316. int res, i = -1, j = -1;
  317. fp = fopen("/sys/devices/system/cpu/present", "r");
  318. // If failed, return at least 1
  319. if (fp == 0)
  320. return 1;
  321. res = fscanf(fp, "%d-%d", &i, &j);
  322. fclose(fp);
  323. if (res == 1 && i == 0)
  324. return 1;
  325. else if (res == 2 && i == 0)
  326. return j + 1;
  327. // If failed, return at least 1
  328. return 1;
  329. }
  330. #endif
  331. unsigned GetNumPhysicalCPUs()
  332. {
  333. #if defined(IOS)
  334. host_basic_info_data_t data;
  335. GetCPUData(&data);
  336. #if defined(TARGET_IPHONE_SIMULATOR)
  337. // Hardcoded to dual-core on simulator mode even if the host has more
  338. return Min(2, data.physical_cpu);
  339. #else
  340. return data.physical_cpu;
  341. #endif
  342. #elif defined(ANDROID)
  343. return GetAndroidCPUCount();
  344. #elif !defined(ANDROID) && !defined(RPI) && !defined(EMSCRIPTEN)
  345. struct cpu_id_t data;
  346. GetCPUData(&data);
  347. return data.num_cores;
  348. #else
  349. /// \todo Implement properly
  350. return 1;
  351. #endif
  352. }
  353. unsigned GetNumLogicalCPUs()
  354. {
  355. #if defined(IOS)
  356. host_basic_info_data_t data;
  357. GetCPUData(&data);
  358. #if defined(TARGET_IPHONE_SIMULATOR)
  359. return Min(2, data.logical_cpu);
  360. #else
  361. return data.logical_cpu;
  362. #endif
  363. #elif defined(ANDROID)
  364. return GetAndroidCPUCount();
  365. #elif !defined(ANDROID) && !defined(RPI) && !defined(EMSCRIPTEN)
  366. struct cpu_id_t data;
  367. GetCPUData(&data);
  368. return data.num_logical_cpus;
  369. #else
  370. /// \todo Implement properly
  371. return 1;
  372. #endif
  373. }
  374. }