ProcessUtils.cpp 9.3 KB

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