ProcessUtils.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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) && !defined(RASPI)
  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) && !defined(RASPI)
  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. #if defined(IOS)
  75. 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(RASPI)
  82. 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(ANDROID) && !defined(IOS) && !defined(RASPI) && !defined(__x86_64__) && !defined(_M_AMD64)
  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. #ifndef ENABLE_LUA_JIT
  98. _controlfp(_RC_NEAR | _PC_24, _MCW_RC | _MCW_PC);
  99. #endif
  100. #else
  101. unsigned control = GetFPUState();
  102. control &= ~(FPU_CW_PREC_MASK | FPU_CW_ROUND_MASK);
  103. control |= (FPU_CW_PREC_SINGLE | FPU_CW_ROUND_NEAR);
  104. SetFPUState(control);
  105. #endif
  106. #endif
  107. }
  108. void ErrorDialog(const String& title, const String& message)
  109. {
  110. #ifdef WIN32
  111. MessageBoxW(0, WString(message).CString(), WString(title).CString(), 0);
  112. #else
  113. PrintLine(message, true);
  114. #endif
  115. }
  116. void ErrorExit(const String& message, int exitCode)
  117. {
  118. if (!message.Empty())
  119. PrintLine(message, true);
  120. exit(exitCode);
  121. }
  122. void OpenConsoleWindow()
  123. {
  124. #ifdef WIN32
  125. if (consoleOpened)
  126. return;
  127. AllocConsole();
  128. HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
  129. int hCrt = _open_osfhandle((size_t)hOut, _O_TEXT);
  130. FILE* outFile = _fdopen(hCrt, "w");
  131. setvbuf(outFile, NULL, _IONBF, 1);
  132. *stdout = *outFile;
  133. HANDLE hIn = GetStdHandle(STD_INPUT_HANDLE);
  134. hCrt = _open_osfhandle((size_t)hIn, _O_TEXT);
  135. FILE* inFile = _fdopen(hCrt, "r");
  136. setvbuf(inFile, NULL, _IONBF, 128);
  137. *stdin = *inFile;
  138. consoleOpened = true;
  139. #endif
  140. }
  141. void PrintUnicode(const String& str, bool error)
  142. {
  143. #if !defined(ANDROID) && !defined(IOS)
  144. #ifdef WIN32
  145. HANDLE stream = GetStdHandle(error ? STD_ERROR_HANDLE : STD_OUTPUT_HANDLE);
  146. if (stream == INVALID_HANDLE_VALUE)
  147. return;
  148. WString strW(str);
  149. DWORD charsWritten;
  150. WriteConsoleW(stream, strW.CString(), strW.Length(), &charsWritten, 0);
  151. #else
  152. fprintf(error ? stderr : stdout, "%s", str.CString());
  153. #endif
  154. #endif
  155. }
  156. void PrintUnicodeLine(const String& str, bool error)
  157. {
  158. PrintUnicode(str + '\n', error);
  159. }
  160. void PrintLine(const String& str, bool error)
  161. {
  162. #if !defined(ANDROID) && !defined(IOS)
  163. fprintf(error ? stderr: stdout, "%s\n", str.CString());
  164. #endif
  165. }
  166. const Vector<String>& ParseArguments(const String& cmdLine, bool skipFirstArgument)
  167. {
  168. arguments.Clear();
  169. unsigned cmdStart = 0, cmdEnd = 0;
  170. bool inCmd = false;
  171. bool inQuote = false;
  172. for (unsigned i = 0; i < cmdLine.Length(); ++i)
  173. {
  174. if (cmdLine[i] == '\"')
  175. inQuote = !inQuote;
  176. if (cmdLine[i] == ' ' && !inQuote)
  177. {
  178. if (inCmd)
  179. {
  180. inCmd = false;
  181. cmdEnd = i;
  182. // Do not store the first argument (executable name)
  183. if (!skipFirstArgument)
  184. arguments.Push(cmdLine.Substring(cmdStart, cmdEnd - cmdStart));
  185. skipFirstArgument = false;
  186. }
  187. }
  188. else
  189. {
  190. if (!inCmd)
  191. {
  192. inCmd = true;
  193. cmdStart = i;
  194. }
  195. }
  196. }
  197. if (inCmd)
  198. {
  199. cmdEnd = cmdLine.Length();
  200. if (!skipFirstArgument)
  201. arguments.Push(cmdLine.Substring(cmdStart, cmdEnd - cmdStart));
  202. }
  203. // Strip double quotes from the arguments
  204. for (unsigned i = 0; i < arguments.Size(); ++i)
  205. arguments[i].Replace("\"", "");
  206. return arguments;
  207. }
  208. const Vector<String>& ParseArguments(const char* cmdLine)
  209. {
  210. return ParseArguments(String(cmdLine));
  211. }
  212. const Vector<String>& ParseArguments(const WString& cmdLine)
  213. {
  214. return ParseArguments(String(cmdLine));
  215. }
  216. const Vector<String>& ParseArguments(const wchar_t* cmdLine)
  217. {
  218. return ParseArguments(String(cmdLine));
  219. }
  220. const Vector<String>& ParseArguments(int argc, char** argv)
  221. {
  222. String cmdLine;
  223. for (int i = 0; i < argc; ++i)
  224. cmdLine.AppendWithFormat("\"%s\" ", (const char*)argv[i]);
  225. return ParseArguments(cmdLine);
  226. }
  227. const Vector<String>& GetArguments()
  228. {
  229. return arguments;
  230. }
  231. String GetConsoleInput()
  232. {
  233. String ret;
  234. #ifdef WIN32
  235. HANDLE input = GetStdHandle(STD_INPUT_HANDLE);
  236. HANDLE output = GetStdHandle(STD_OUTPUT_HANDLE);
  237. if (input == INVALID_HANDLE_VALUE || output == INVALID_HANDLE_VALUE)
  238. return ret;
  239. // Use char-based input
  240. SetConsoleMode(input, ENABLE_PROCESSED_INPUT);
  241. INPUT_RECORD record;
  242. DWORD events = 0;
  243. DWORD readEvents = 0;
  244. if (!GetNumberOfConsoleInputEvents(input, &events))
  245. return ret;
  246. while (events--)
  247. {
  248. ReadConsoleInputW(input, &record, 1, &readEvents);
  249. if (record.EventType == KEY_EVENT && record.Event.KeyEvent.bKeyDown)
  250. {
  251. unsigned c = record.Event.KeyEvent.uChar.UnicodeChar;
  252. if (c)
  253. {
  254. if (c == '\b')
  255. {
  256. PrintUnicode("\b \b");
  257. int length = currentLine.LengthUTF8();
  258. if (length)
  259. currentLine = currentLine.SubstringUTF8(0, length - 1);
  260. }
  261. else if (c == '\r')
  262. {
  263. PrintUnicode("\n");
  264. ret = currentLine;
  265. currentLine.Clear();
  266. return ret;
  267. }
  268. else
  269. {
  270. // We have disabled echo, so echo manually
  271. wchar_t out = c;
  272. DWORD charsWritten;
  273. WriteConsoleW(output, &out, 1, &charsWritten, 0);
  274. currentLine.AppendUTF8(c);
  275. }
  276. }
  277. }
  278. }
  279. #elif !defined(ANDROID) && !defined(IOS)
  280. int flags = fcntl(STDIN_FILENO, F_GETFL);
  281. fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK);
  282. for (;;)
  283. {
  284. int ch = fgetc(stdin);
  285. if (ch >= 0 && ch != '\n')
  286. ret += (char)ch;
  287. else
  288. break;
  289. }
  290. #endif
  291. return ret;
  292. }
  293. String GetPlatform()
  294. {
  295. #if defined(ANDROID)
  296. return "Android";
  297. #elif defined(IOS)
  298. return "iOS";
  299. #elif defined(WIN32)
  300. return "Windows";
  301. #elif defined(__APPLE__)
  302. return "Mac OS X";
  303. #elif defined(RASPI)
  304. return "Raspberry Pi";
  305. #elif defined(__linux__)
  306. return "Linux";
  307. #else
  308. return String::EMPTY;
  309. #endif
  310. }
  311. unsigned GetNumPhysicalCPUs()
  312. {
  313. #if defined(IOS)
  314. host_basic_info_data_t data;
  315. GetCPUData(&data);
  316. #if defined(TARGET_IPHONE_SIMULATOR)
  317. // Hardcoded to dual-core on simulator mode even if the host has more
  318. return Min(2, data.physical_cpu);
  319. #else
  320. return data.physical_cpu;
  321. #endif
  322. #elif !defined(ANDROID) && !defined(RASPI)
  323. struct cpu_id_t data;
  324. GetCPUData(&data);
  325. return data.num_cores;
  326. #else
  327. /// \todo Implement properly
  328. return 1;
  329. #endif
  330. }
  331. unsigned GetNumLogicalCPUs()
  332. {
  333. #if defined(IOS)
  334. host_basic_info_data_t data;
  335. GetCPUData(&data);
  336. #if defined(TARGET_IPHONE_SIMULATOR)
  337. return Min(2, data.logical_cpu);
  338. #else
  339. return data.logical_cpu;
  340. #endif
  341. #elif !defined(ANDROID) && !defined(RASPI)
  342. struct cpu_id_t data;
  343. GetCPUData(&data);
  344. return data.num_logical_cpus;
  345. #else
  346. /// \todo Implement properly
  347. return 1;
  348. #endif
  349. }
  350. }