ProcessUtils.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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. #ifndef ANDROID
  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)
  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. #ifndef ANDROID
  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. #ifndef ANDROID
  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. #ifdef WIN32
  129. HANDLE output = GetStdHandle(STD_OUTPUT_HANDLE);
  130. if (output == INVALID_HANDLE_VALUE)
  131. return;
  132. WString strW(str);
  133. DWORD charsWritten;
  134. WriteConsoleW(output, strW.CString(), strW.Length(), &charsWritten, 0);
  135. #else
  136. printf("%s", str.CString());
  137. #endif
  138. }
  139. void PrintUnicodeLine(const String& str)
  140. {
  141. PrintUnicode(str + '\n');
  142. }
  143. void PrintLine(const String& str)
  144. {
  145. printf("%s\n", str.CString());
  146. }
  147. const Vector<String>& ParseArguments(const String& cmdLine)
  148. {
  149. arguments.Clear();
  150. unsigned cmdStart = 0, cmdEnd = 0;
  151. bool inCmd = false;
  152. bool inQuote = false;
  153. bool firstArgument = true;
  154. for (unsigned i = 0; i < cmdLine.Length(); ++i)
  155. {
  156. if (cmdLine[i] == '\"')
  157. inQuote = !inQuote;
  158. if (cmdLine[i] == ' ' && !inQuote)
  159. {
  160. if (inCmd)
  161. {
  162. inCmd = false;
  163. cmdEnd = i;
  164. // Do not store the first argument (executable name)
  165. if (!firstArgument)
  166. arguments.Push(cmdLine.Substring(cmdStart, cmdEnd - cmdStart));
  167. firstArgument = false;
  168. }
  169. }
  170. else
  171. {
  172. if (!inCmd)
  173. {
  174. inCmd = true;
  175. cmdStart = i;
  176. }
  177. }
  178. }
  179. if (inCmd)
  180. {
  181. cmdEnd = cmdLine.Length();
  182. if (!firstArgument)
  183. arguments.Push(cmdLine.Substring(cmdStart, cmdEnd - cmdStart));
  184. }
  185. // Strip double quotes from the arguments
  186. for (unsigned i = 0; i < arguments.Size(); ++i)
  187. arguments[i].Replace("\"", "");
  188. return arguments;
  189. }
  190. const Vector<String>& ParseArguments(const char* cmdLine)
  191. {
  192. return ParseArguments(String(cmdLine));
  193. }
  194. const Vector<String>& ParseArguments(const WString& cmdLine)
  195. {
  196. return ParseArguments(String(cmdLine));
  197. }
  198. const Vector<String>& ParseArguments(const wchar_t* cmdLine)
  199. {
  200. return ParseArguments(String(cmdLine));
  201. }
  202. const Vector<String>& ParseArguments(int argc, char** argv)
  203. {
  204. String cmdLine;
  205. for (int i = 0; i < argc; ++i)
  206. {
  207. cmdLine += (const char*)argv[i];
  208. if (i < argc - 1)
  209. cmdLine += ' ';
  210. }
  211. return ParseArguments(cmdLine);
  212. }
  213. const Vector<String>& GetArguments()
  214. {
  215. return arguments;
  216. }
  217. String GetConsoleInput()
  218. {
  219. String ret;
  220. #ifdef WIN32
  221. HANDLE input = GetStdHandle(STD_INPUT_HANDLE);
  222. HANDLE output = GetStdHandle(STD_OUTPUT_HANDLE);
  223. if (input == INVALID_HANDLE_VALUE || output == INVALID_HANDLE_VALUE)
  224. return ret;
  225. // Use char-based input
  226. SetConsoleMode(input, ENABLE_PROCESSED_INPUT);
  227. INPUT_RECORD record;
  228. DWORD events = 0;
  229. DWORD readEvents = 0;
  230. if (!GetNumberOfConsoleInputEvents(input, &events))
  231. return ret;
  232. while (events--)
  233. {
  234. ReadConsoleInputW(input, &record, 1, &readEvents);
  235. if (record.EventType == KEY_EVENT && record.Event.KeyEvent.bKeyDown)
  236. {
  237. unsigned c = record.Event.KeyEvent.uChar.UnicodeChar;
  238. if (c)
  239. {
  240. if (c == '\b')
  241. {
  242. PrintUnicode("\b \b");
  243. int length = currentLine.LengthUTF8();
  244. if (length)
  245. currentLine = currentLine.SubstringUTF8(0, length - 1);
  246. }
  247. else if (c == '\r')
  248. {
  249. PrintUnicode("\n");
  250. ret = currentLine;
  251. currentLine.Clear();
  252. return ret;
  253. }
  254. else
  255. {
  256. // We have disabled echo, so echo manually
  257. wchar_t out = c;
  258. DWORD charsWritten;
  259. WriteConsoleW(output, &out, 1, &charsWritten, 0);
  260. currentLine.AppendUTF8(c);
  261. }
  262. }
  263. }
  264. }
  265. #else
  266. int flags = fcntl(STDIN_FILENO, F_GETFL);
  267. fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK);
  268. for (;;)
  269. {
  270. int ch = fgetc(stdin);
  271. if (ch >= 0 && ch != '\n')
  272. ret += (char)ch;
  273. else
  274. break;
  275. }
  276. #endif
  277. return ret;
  278. }
  279. String GetPlatform()
  280. {
  281. #if defined(WIN32)
  282. return "Windows";
  283. #elif defined(__APPLE__)
  284. return "Mac OS X";
  285. #elif defined(ANDROID)
  286. return "Android";
  287. #elif defined(__linux__)
  288. return "Linux";
  289. #else
  290. return String();
  291. #endif
  292. }
  293. unsigned GetNumPhysicalCPUs()
  294. {
  295. #ifndef ANDROID
  296. struct cpu_id_t data;
  297. GetCPUData(&data);
  298. return data.num_cores;
  299. #else
  300. /// \todo Implement properly
  301. return 1;
  302. #endif
  303. }
  304. unsigned GetNumLogicalCPUs()
  305. {
  306. #ifndef ANDROID
  307. struct cpu_id_t data;
  308. GetCPUData(&data);
  309. return data.num_logical_cpus;
  310. #else
  311. /// \todo Implement properly
  312. return 1;
  313. #endif
  314. }
  315. Mutex& GetStaticMutex()
  316. {
  317. return staticMutex;
  318. }