ProcessUtils.cpp 11 KB

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