ProcessUtils.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Oorni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Precompiled.h"
  24. #include "Mutex.h"
  25. #include "ProcessUtils.h"
  26. #include "MathDefs.h"
  27. #include <cstdio>
  28. #include <cstdlib>
  29. #include <fcntl.h>
  30. #ifdef __APPLE__
  31. #include "TargetConditionals.h"
  32. #endif
  33. #if defined(IOS)
  34. #include <mach/mach_host.h>
  35. #elif !defined(ANDROID)
  36. #include <libcpuid.h>
  37. #endif
  38. #ifdef WIN32
  39. #include <windows.h>
  40. #include <io.h>
  41. #else
  42. #include <unistd.h>
  43. #endif
  44. #if defined(_MSC_VER)
  45. #include <float.h>
  46. #elif !defined(ANDROID) && !defined(IOS)
  47. // From http://stereopsis.com/FPU.html
  48. #define FPU_CW_PREC_MASK 0x0300
  49. #define FPU_CW_PREC_SINGLE 0x0000
  50. #define FPU_CW_PREC_DOUBLE 0x0200
  51. #define FPU_CW_PREC_EXTENDED 0x0300
  52. #define FPU_CW_ROUND_MASK 0x0c00
  53. #define FPU_CW_ROUND_NEAR 0x0000
  54. #define FPU_CW_ROUND_DOWN 0x0400
  55. #define FPU_CW_ROUND_UP 0x0800
  56. #define FPU_CW_ROUND_CHOP 0x0c00
  57. inline unsigned GetFPUState()
  58. {
  59. unsigned control = 0;
  60. __asm__ __volatile__ ("fnstcw %0" : "=m" (control));
  61. return control;
  62. }
  63. inline void SetFPUState(unsigned control)
  64. {
  65. __asm__ __volatile__ ("fldcw %0" : : "m" (control));
  66. }
  67. #endif
  68. #include "DebugNew.h"
  69. namespace Urho3D
  70. {
  71. #ifdef WIN32
  72. static bool consoleOpened = false;
  73. #endif
  74. static String currentLine;
  75. static Vector<String> arguments;
  76. static Mutex staticMutex;
  77. #if defined(IOS)
  78. void GetCPUData(host_basic_info_data_t* data)
  79. {
  80. mach_msg_type_number_t infoCount;
  81. infoCount = HOST_BASIC_INFO_COUNT;
  82. host_info(mach_host_self(), HOST_BASIC_INFO, (host_info_t)data, &infoCount);
  83. }
  84. #elif !defined(ANDROID)
  85. void GetCPUData(struct cpu_id_t* data)
  86. {
  87. if (cpu_identify(0, data) < 0)
  88. {
  89. data->num_logical_cpus = 1;
  90. data->num_cores = 1;
  91. }
  92. }
  93. #endif
  94. void InitFPU()
  95. {
  96. #if !defined(ANDROID) && !defined(IOS)
  97. // Make sure FPU is in round-to-nearest, single precision mode
  98. // This ensures Direct3D and OpenGL behave similarly, and all threads behave similarly
  99. #ifdef _MSC_VER
  100. _controlfp(_RC_NEAR | _PC_24, _MCW_RC | _MCW_PC);
  101. #else
  102. unsigned control = GetFPUState();
  103. control &= ~(FPU_CW_PREC_MASK | FPU_CW_ROUND_MASK);
  104. control |= (FPU_CW_PREC_SINGLE | FPU_CW_ROUND_NEAR);
  105. SetFPUState(control);
  106. #endif
  107. #endif
  108. }
  109. void ErrorDialog(const String& title, const String& message)
  110. {
  111. #ifdef WIN32
  112. MessageBoxW(0, WString(message).CString(), WString(title).CString(), 0);
  113. #else
  114. PrintLine(message);
  115. #endif
  116. }
  117. void ErrorExit(const String& message, int exitCode)
  118. {
  119. PrintLine(message);
  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((long)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((long)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)
  142. {
  143. #if !defined(ANDROID) && !defined(IOS)
  144. #ifdef WIN32
  145. HANDLE output = GetStdHandle(STD_OUTPUT_HANDLE);
  146. if (output == INVALID_HANDLE_VALUE)
  147. return;
  148. WString strW(str);
  149. DWORD charsWritten;
  150. WriteConsoleW(output, strW.CString(), strW.Length(), &charsWritten, 0);
  151. #else
  152. printf("%s", str.CString());
  153. #endif
  154. #endif
  155. }
  156. void PrintUnicodeLine(const String& str)
  157. {
  158. PrintUnicode(str + '\n');
  159. }
  160. void PrintLine(const String& str)
  161. {
  162. #if !defined(ANDROID) && !defined(IOS)
  163. printf("%s\n", str.CString());
  164. #endif
  165. }
  166. const Vector<String>& ParseArguments(const String& cmdLine)
  167. {
  168. arguments.Clear();
  169. unsigned cmdStart = 0, cmdEnd = 0;
  170. bool inCmd = false;
  171. bool inQuote = false;
  172. bool firstArgument = true;
  173. for (unsigned i = 0; i < cmdLine.Length(); ++i)
  174. {
  175. if (cmdLine[i] == '\"')
  176. inQuote = !inQuote;
  177. if (cmdLine[i] == ' ' && !inQuote)
  178. {
  179. if (inCmd)
  180. {
  181. inCmd = false;
  182. cmdEnd = i;
  183. // Do not store the first argument (executable name)
  184. if (!firstArgument)
  185. arguments.Push(cmdLine.Substring(cmdStart, cmdEnd - cmdStart));
  186. firstArgument = false;
  187. }
  188. }
  189. else
  190. {
  191. if (!inCmd)
  192. {
  193. inCmd = true;
  194. cmdStart = i;
  195. }
  196. }
  197. }
  198. if (inCmd)
  199. {
  200. cmdEnd = cmdLine.Length();
  201. if (!firstArgument)
  202. arguments.Push(cmdLine.Substring(cmdStart, cmdEnd - cmdStart));
  203. }
  204. // Strip double quotes from the arguments
  205. for (unsigned i = 0; i < arguments.Size(); ++i)
  206. arguments[i].Replace("\"", "");
  207. return arguments;
  208. }
  209. const Vector<String>& ParseArguments(const char* cmdLine)
  210. {
  211. return ParseArguments(String(cmdLine));
  212. }
  213. const Vector<String>& ParseArguments(const WString& cmdLine)
  214. {
  215. return ParseArguments(String(cmdLine));
  216. }
  217. const Vector<String>& ParseArguments(const wchar_t* cmdLine)
  218. {
  219. return ParseArguments(String(cmdLine));
  220. }
  221. const Vector<String>& ParseArguments(int argc, char** argv)
  222. {
  223. String cmdLine;
  224. for (int i = 0; i < argc; ++i)
  225. cmdLine.AppendWithFormat("\"%s\" ", (const char*)argv[i]);
  226. return ParseArguments(cmdLine);
  227. }
  228. const Vector<String>& GetArguments()
  229. {
  230. return arguments;
  231. }
  232. String GetConsoleInput()
  233. {
  234. String ret;
  235. #ifdef WIN32
  236. HANDLE input = GetStdHandle(STD_INPUT_HANDLE);
  237. HANDLE output = GetStdHandle(STD_OUTPUT_HANDLE);
  238. if (input == INVALID_HANDLE_VALUE || output == INVALID_HANDLE_VALUE)
  239. return ret;
  240. // Use char-based input
  241. SetConsoleMode(input, ENABLE_PROCESSED_INPUT);
  242. INPUT_RECORD record;
  243. DWORD events = 0;
  244. DWORD readEvents = 0;
  245. if (!GetNumberOfConsoleInputEvents(input, &events))
  246. return ret;
  247. while (events--)
  248. {
  249. ReadConsoleInputW(input, &record, 1, &readEvents);
  250. if (record.EventType == KEY_EVENT && record.Event.KeyEvent.bKeyDown)
  251. {
  252. unsigned c = record.Event.KeyEvent.uChar.UnicodeChar;
  253. if (c)
  254. {
  255. if (c == '\b')
  256. {
  257. PrintUnicode("\b \b");
  258. int length = currentLine.LengthUTF8();
  259. if (length)
  260. currentLine = currentLine.SubstringUTF8(0, length - 1);
  261. }
  262. else if (c == '\r')
  263. {
  264. PrintUnicode("\n");
  265. ret = currentLine;
  266. currentLine.Clear();
  267. return ret;
  268. }
  269. else
  270. {
  271. // We have disabled echo, so echo manually
  272. wchar_t out = c;
  273. DWORD charsWritten;
  274. WriteConsoleW(output, &out, 1, &charsWritten, 0);
  275. currentLine.AppendUTF8(c);
  276. }
  277. }
  278. }
  279. }
  280. #elif !defined(ANDROID) && !defined(IOS)
  281. int flags = fcntl(STDIN_FILENO, F_GETFL);
  282. fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK);
  283. for (;;)
  284. {
  285. int ch = fgetc(stdin);
  286. if (ch >= 0 && ch != '\n')
  287. ret += (char)ch;
  288. else
  289. break;
  290. }
  291. #endif
  292. return ret;
  293. }
  294. String GetPlatform()
  295. {
  296. #if defined(ANDROID)
  297. return "Android";
  298. #elif defined(IOS)
  299. return "iOS";
  300. #elif defined(WIN32)
  301. return "Windows";
  302. #elif defined(__APPLE__)
  303. return "Mac OS X";
  304. #elif defined(__linux__)
  305. return "Linux";
  306. #else
  307. return String();
  308. #endif
  309. }
  310. unsigned GetNumPhysicalCPUs()
  311. {
  312. #if defined(IOS)
  313. host_basic_info_data_t data;
  314. GetCPUData(&data);
  315. #if defined(TARGET_IPHONE_SIMULATOR)
  316. // Hardcoded to dual-core on simulator mode even if the host has more
  317. return Min(2, data.physical_cpu);
  318. #else
  319. return data.physical_cpu;
  320. #endif
  321. #elif !defined(ANDROID)
  322. struct cpu_id_t data;
  323. GetCPUData(&data);
  324. return data.num_cores;
  325. #else
  326. /// \todo Implement properly
  327. return 1;
  328. #endif
  329. }
  330. unsigned GetNumLogicalCPUs()
  331. {
  332. #if defined(IOS)
  333. host_basic_info_data_t data;
  334. GetCPUData(&data);
  335. #if defined(TARGET_IPHONE_SIMULATOR)
  336. return Min(2, data.logical_cpu);
  337. #else
  338. return data.logical_cpu;
  339. #endif
  340. #elif !defined(ANDROID)
  341. struct cpu_id_t data;
  342. GetCPUData(&data);
  343. return data.num_logical_cpus;
  344. #else
  345. /// \todo Implement properly
  346. return 1;
  347. #endif
  348. }
  349. Mutex& GetStaticMutex()
  350. {
  351. return staticMutex;
  352. }
  353. }