ProcessUtils.cpp 10 KB

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