ProcessUtils.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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 <cstdlib>
  28. #include <fcntl.h>
  29. #ifdef __APPLE__
  30. #include "TargetConditionals.h"
  31. #endif
  32. #if defined(IOS)
  33. #include <mach/mach_host.h>
  34. #elif !defined(ANDROID)
  35. #include <libcpuid.h>
  36. #endif
  37. #ifdef WIN32
  38. #include <windows.h>
  39. #include <io.h>
  40. #else
  41. #include <unistd.h>
  42. #endif
  43. #if defined(_MSC_VER)
  44. #include <float.h>
  45. #elif !defined(ANDROID) && !defined(IOS)
  46. // From http://stereopsis.com/FPU.html
  47. #define FPU_CW_PREC_MASK 0x0300
  48. #define FPU_CW_PREC_SINGLE 0x0000
  49. #define FPU_CW_PREC_DOUBLE 0x0200
  50. #define FPU_CW_PREC_EXTENDED 0x0300
  51. #define FPU_CW_ROUND_MASK 0x0c00
  52. #define FPU_CW_ROUND_NEAR 0x0000
  53. #define FPU_CW_ROUND_DOWN 0x0400
  54. #define FPU_CW_ROUND_UP 0x0800
  55. #define FPU_CW_ROUND_CHOP 0x0c00
  56. inline unsigned GetFPUState()
  57. {
  58. unsigned control = 0;
  59. __asm__ __volatile__ ("fnstcw %0" : "=m" (control));
  60. return control;
  61. }
  62. inline void SetFPUState(unsigned control)
  63. {
  64. __asm__ __volatile__ ("fldcw %0" : : "m" (control));
  65. }
  66. #endif
  67. #include "DebugNew.h"
  68. namespace Urho3D
  69. {
  70. #ifdef WIN32
  71. static bool consoleOpened = false;
  72. #endif
  73. static String currentLine;
  74. static Vector<String> arguments;
  75. static Mutex staticMutex;
  76. #if defined(IOS)
  77. void GetCPUData(host_basic_info_data_t* data)
  78. {
  79. mach_msg_type_number_t infoCount;
  80. infoCount = HOST_BASIC_INFO_COUNT;
  81. host_info(mach_host_self(), HOST_BASIC_INFO, (host_info_t)data, &infoCount);
  82. }
  83. #elif !defined(ANDROID)
  84. void GetCPUData(struct cpu_id_t* data)
  85. {
  86. if (cpu_identify(0, data) < 0)
  87. {
  88. data->num_logical_cpus = 1;
  89. data->num_cores = 1;
  90. }
  91. }
  92. #endif
  93. void InitFPU()
  94. {
  95. #if !defined(ANDROID) && !defined(IOS)
  96. // Make sure FPU is in round-to-nearest, single precision mode
  97. // This ensures Direct3D and OpenGL behave similarly, and all threads behave similarly
  98. #ifdef _MSC_VER
  99. _controlfp(_RC_NEAR | _PC_24, _MCW_RC | _MCW_PC);
  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);
  114. #endif
  115. }
  116. void ErrorExit(const String& message, int exitCode)
  117. {
  118. PrintLine(message);
  119. exit(exitCode);
  120. }
  121. void OpenConsoleWindow()
  122. {
  123. #ifdef WIN32
  124. if (consoleOpened)
  125. return;
  126. AllocConsole();
  127. HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
  128. int hCrt = _open_osfhandle((long)hOut, _O_TEXT);
  129. FILE* outFile = _fdopen(hCrt, "w");
  130. setvbuf(outFile, NULL, _IONBF, 1);
  131. *stdout = *outFile;
  132. HANDLE hIn = GetStdHandle(STD_INPUT_HANDLE);
  133. hCrt = _open_osfhandle((long)hIn, _O_TEXT);
  134. FILE* inFile = _fdopen(hCrt, "r");
  135. setvbuf(inFile, NULL, _IONBF, 128);
  136. *stdin = *inFile;
  137. consoleOpened = true;
  138. #endif
  139. }
  140. void PrintUnicode(const String& str)
  141. {
  142. #if !defined(ANDROID) && !defined(IOS)
  143. #ifdef WIN32
  144. HANDLE output = GetStdHandle(STD_OUTPUT_HANDLE);
  145. if (output == INVALID_HANDLE_VALUE)
  146. return;
  147. WString strW(str);
  148. DWORD charsWritten;
  149. WriteConsoleW(output, strW.CString(), strW.Length(), &charsWritten, 0);
  150. #else
  151. printf("%s", str.CString());
  152. #endif
  153. #endif
  154. }
  155. void PrintUnicodeLine(const String& str)
  156. {
  157. PrintUnicode(str + '\n');
  158. }
  159. void PrintLine(const String& str)
  160. {
  161. #if !defined(ANDROID) && !defined(IOS)
  162. printf("%s\n", str.CString());
  163. #endif
  164. }
  165. const Vector<String>& ParseArguments(const String& cmdLine)
  166. {
  167. arguments.Clear();
  168. unsigned cmdStart = 0, cmdEnd = 0;
  169. bool inCmd = false;
  170. bool inQuote = false;
  171. bool firstArgument = true;
  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 (!firstArgument)
  184. arguments.Push(cmdLine.Substring(cmdStart, cmdEnd - cmdStart));
  185. firstArgument = 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 (!firstArgument)
  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(__linux__)
  304. return "Linux";
  305. #else
  306. return String();
  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)
  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)
  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. Mutex& GetStaticMutex()
  349. {
  350. return staticMutex;
  351. }
  352. }