ProcessUtils.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. //
  2. // Copyright (c) 2008-2014 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(ENABLE_LUAJIT) && !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. _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. #ifdef WIN32
  109. MessageBoxW(0, WString(message).CString(), WString(title).CString(), 0);
  110. #else
  111. PrintLine(message, true);
  112. #endif
  113. }
  114. void ErrorExit(const String& message, int exitCode)
  115. {
  116. if (!message.Empty())
  117. PrintLine(message, true);
  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((size_t)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((size_t)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, bool error)
  159. {
  160. #if !defined(ANDROID) && !defined(IOS)
  161. fprintf(error ? stderr: stdout, "%s\n", str.CString());
  162. #endif
  163. }
  164. const Vector<String>& ParseArguments(const String& cmdLine, bool skipFirstArgument)
  165. {
  166. arguments.Clear();
  167. unsigned cmdStart = 0, cmdEnd = 0;
  168. bool inCmd = false;
  169. bool inQuote = false;
  170. for (unsigned i = 0; i < cmdLine.Length(); ++i)
  171. {
  172. if (cmdLine[i] == '\"')
  173. inQuote = !inQuote;
  174. if (cmdLine[i] == ' ' && !inQuote)
  175. {
  176. if (inCmd)
  177. {
  178. inCmd = false;
  179. cmdEnd = i;
  180. // Do not store the first argument (executable name)
  181. if (!skipFirstArgument)
  182. arguments.Push(cmdLine.Substring(cmdStart, cmdEnd - cmdStart));
  183. skipFirstArgument = false;
  184. }
  185. }
  186. else
  187. {
  188. if (!inCmd)
  189. {
  190. inCmd = true;
  191. cmdStart = i;
  192. }
  193. }
  194. }
  195. if (inCmd)
  196. {
  197. cmdEnd = cmdLine.Length();
  198. if (!skipFirstArgument)
  199. arguments.Push(cmdLine.Substring(cmdStart, cmdEnd - cmdStart));
  200. }
  201. // Strip double quotes from the arguments
  202. for (unsigned i = 0; i < arguments.Size(); ++i)
  203. arguments[i].Replace("\"", "");
  204. return arguments;
  205. }
  206. const Vector<String>& ParseArguments(const char* cmdLine)
  207. {
  208. return ParseArguments(String(cmdLine));
  209. }
  210. const Vector<String>& ParseArguments(const WString& cmdLine)
  211. {
  212. return ParseArguments(String(cmdLine));
  213. }
  214. const Vector<String>& ParseArguments(const wchar_t* cmdLine)
  215. {
  216. return ParseArguments(String(cmdLine));
  217. }
  218. const Vector<String>& ParseArguments(int argc, char** argv)
  219. {
  220. String cmdLine;
  221. for (int i = 0; i < argc; ++i)
  222. cmdLine.AppendWithFormat("\"%s\" ", (const char*)argv[i]);
  223. return ParseArguments(cmdLine);
  224. }
  225. const Vector<String>& GetArguments()
  226. {
  227. return arguments;
  228. }
  229. String GetConsoleInput()
  230. {
  231. String ret;
  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(RASPI)
  302. return "Raspberry Pi";
  303. #elif defined(__linux__)
  304. return "Linux";
  305. #else
  306. return String::EMPTY;
  307. #endif
  308. }
  309. unsigned GetNumPhysicalCPUs()
  310. {
  311. #if defined(IOS)
  312. host_basic_info_data_t data;
  313. GetCPUData(&data);
  314. #if defined(TARGET_IPHONE_SIMULATOR)
  315. // Hardcoded to dual-core on simulator mode even if the host has more
  316. return Min(2, data.physical_cpu);
  317. #else
  318. return data.physical_cpu;
  319. #endif
  320. #elif !defined(ANDROID) && !defined(RASPI)
  321. struct cpu_id_t data;
  322. GetCPUData(&data);
  323. return data.num_cores;
  324. #else
  325. /// \todo Implement properly
  326. return 1;
  327. #endif
  328. }
  329. unsigned GetNumLogicalCPUs()
  330. {
  331. #if defined(IOS)
  332. host_basic_info_data_t data;
  333. GetCPUData(&data);
  334. #if defined(TARGET_IPHONE_SIMULATOR)
  335. return Min(2, data.logical_cpu);
  336. #else
  337. return data.logical_cpu;
  338. #endif
  339. #elif !defined(ANDROID) && !defined(RASPI)
  340. struct cpu_id_t data;
  341. GetCPUData(&data);
  342. return data.num_logical_cpus;
  343. #else
  344. /// \todo Implement properly
  345. return 1;
  346. #endif
  347. }
  348. }