The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

514 lines
13KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-7 by Raw Material Software ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the
  7. GNU General Public License, as published by the Free Software Foundation;
  8. either version 2 of the License, or (at your option) any later version.
  9. JUCE is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with JUCE; if not, visit www.gnu.org/licenses or write to the
  15. Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  16. Boston, MA 02111-1307 USA
  17. ------------------------------------------------------------------------------
  18. If you'd like to release a closed-source product which uses JUCE, commercial
  19. licenses are also available: visit www.rawmaterialsoftware.com/juce for
  20. more information.
  21. ==============================================================================
  22. */
  23. #include "win32_headers.h"
  24. #include "../../../src/juce_core/basics/juce_StandardHeader.h"
  25. //==============================================================================
  26. // Auto-link the other win32 libs that are needed by library calls..
  27. #if defined (JUCE_DLL_BUILD) && JUCE_MSVC
  28. #pragma comment(lib, "kernel32.lib")
  29. #pragma comment(lib, "user32.lib")
  30. #pragma comment(lib, "shell32.lib")
  31. #pragma comment(lib, "gdi32.lib")
  32. #pragma comment(lib, "vfw32.lib")
  33. #pragma comment(lib, "comdlg32.lib")
  34. #pragma comment(lib, "winmm.lib")
  35. #pragma comment(lib, "wininet.lib")
  36. #pragma comment(lib, "ole32.lib")
  37. #pragma comment(lib, "advapi32.lib")
  38. #pragma comment(lib, "ws2_32.lib")
  39. #if JUCE_OPENGL
  40. #pragma comment(lib, "OpenGL32.Lib")
  41. #pragma comment(lib, "GlU32.Lib")
  42. #endif
  43. #endif
  44. //==============================================================================
  45. BEGIN_JUCE_NAMESPACE
  46. #include "../../../src/juce_core/io/files/juce_File.h"
  47. #include "../../../src/juce_core/basics/juce_SystemStats.h"
  48. extern void juce_updateMultiMonitorInfo() throw();
  49. extern void juce_initialiseThreadEvents() throw();
  50. #if JUCE_ENABLE_WIN98_COMPATIBILITY
  51. extern void juce_initialiseUnicodeFileFunctions() throw();
  52. #endif
  53. //==============================================================================
  54. void Logger::outputDebugString (const String& text) throw()
  55. {
  56. OutputDebugString (text + T("\n"));
  57. }
  58. void Logger::outputDebugPrintf (const tchar* format, ...) throw()
  59. {
  60. String text;
  61. va_list args;
  62. va_start (args, format);
  63. text.vprintf(format, args);
  64. outputDebugString (text);
  65. }
  66. //==============================================================================
  67. static int64 hiResTicksPerSecond;
  68. static double hiResTicksScaleFactor;
  69. //==============================================================================
  70. #if JUCE_USE_INTRINSICS
  71. // CPU info functions using intrinsics...
  72. #pragma intrinsic (__cpuid)
  73. #pragma intrinsic (__rdtsc)
  74. /*static unsigned int getCPUIDWord (int* familyModel = 0, int* extFeatures = 0) throw()
  75. {
  76. int info [4];
  77. __cpuid (info, 1);
  78. if (familyModel != 0)
  79. *familyModel = info [0];
  80. if (extFeatures != 0)
  81. *extFeatures = info[1];
  82. return info[3];
  83. }*/
  84. const String SystemStats::getCpuVendor() throw()
  85. {
  86. int info [4];
  87. __cpuid (info, 0);
  88. char v [12];
  89. memcpy (v, info + 1, 4);
  90. memcpy (v + 4, info + 3, 4);
  91. memcpy (v + 8, info + 2, 4);
  92. return String (v, 12);
  93. }
  94. #else
  95. //==============================================================================
  96. // CPU info functions using old fashioned inline asm...
  97. /*static juce_noinline unsigned int getCPUIDWord (int* familyModel = 0, int* extFeatures = 0)
  98. {
  99. unsigned int cpu = 0;
  100. unsigned int ext = 0;
  101. unsigned int family = 0;
  102. #if JUCE_GCC
  103. unsigned int dummy = 0;
  104. #endif
  105. #ifndef __MINGW32__
  106. __try
  107. #endif
  108. {
  109. #if JUCE_GCC
  110. __asm__ ("cpuid" : "=a" (family), "=b" (ext), "=c" (dummy),"=d" (cpu) : "a" (1));
  111. #else
  112. __asm
  113. {
  114. mov eax, 1
  115. cpuid
  116. mov cpu, edx
  117. mov family, eax
  118. mov ext, ebx
  119. }
  120. #endif
  121. }
  122. #ifndef __MINGW32__
  123. __except (EXCEPTION_EXECUTE_HANDLER)
  124. {
  125. return 0;
  126. }
  127. #endif
  128. if (familyModel != 0)
  129. *familyModel = family;
  130. if (extFeatures != 0)
  131. *extFeatures = ext;
  132. return cpu;
  133. }*/
  134. static void juce_getCpuVendor (char* const v)
  135. {
  136. int vendor[4];
  137. zeromem (vendor, 16);
  138. #ifdef JUCE_64BIT
  139. #else
  140. #ifndef __MINGW32__
  141. __try
  142. #endif
  143. {
  144. #if JUCE_GCC
  145. unsigned int dummy = 0;
  146. __asm__ ("cpuid" : "=a" (dummy), "=b" (vendor[0]), "=c" (vendor[2]),"=d" (vendor[1]) : "a" (0));
  147. #else
  148. __asm
  149. {
  150. mov eax, 0
  151. cpuid
  152. mov [vendor], ebx
  153. mov [vendor + 4], edx
  154. mov [vendor + 8], ecx
  155. }
  156. #endif
  157. }
  158. #ifndef __MINGW32__
  159. __except (EXCEPTION_EXECUTE_HANDLER)
  160. {
  161. *v = 0;
  162. }
  163. #endif
  164. #endif
  165. memcpy (v, vendor, 16);
  166. }
  167. const String SystemStats::getCpuVendor() throw()
  168. {
  169. char v [16];
  170. juce_getCpuVendor (v);
  171. return String (v, 16);
  172. }
  173. #endif
  174. //==============================================================================
  175. struct CPUFlags
  176. {
  177. bool hasMMX : 1;
  178. bool hasSSE : 1;
  179. bool hasSSE2 : 1;
  180. bool has3DNow : 1;
  181. };
  182. static CPUFlags cpuFlags;
  183. bool SystemStats::hasMMX() throw()
  184. {
  185. return cpuFlags.hasMMX;
  186. }
  187. bool SystemStats::hasSSE() throw()
  188. {
  189. return cpuFlags.hasSSE;
  190. }
  191. bool SystemStats::hasSSE2() throw()
  192. {
  193. return cpuFlags.hasSSE2;
  194. }
  195. bool SystemStats::has3DNow() throw()
  196. {
  197. return cpuFlags.has3DNow;
  198. }
  199. void SystemStats::initialiseStats() throw()
  200. {
  201. #if JUCE_ENABLE_WIN98_COMPATIBILITY
  202. juce_initialiseUnicodeFileFunctions();
  203. #endif
  204. juce_initialiseThreadEvents();
  205. cpuFlags.hasMMX = IsProcessorFeaturePresent (PF_MMX_INSTRUCTIONS_AVAILABLE) != 0;
  206. cpuFlags.hasSSE = IsProcessorFeaturePresent (PF_XMMI_INSTRUCTIONS_AVAILABLE) != 0;
  207. cpuFlags.hasSSE2 = IsProcessorFeaturePresent (PF_XMMI64_INSTRUCTIONS_AVAILABLE) != 0;
  208. #ifdef PF_AMD3D_INSTRUCTIONS_AVAILABLE
  209. cpuFlags.has3DNow = IsProcessorFeaturePresent (PF_AMD3D_INSTRUCTIONS_AVAILABLE) != 0;
  210. #else
  211. cpuFlags.has3DNow = IsProcessorFeaturePresent (PF_3DNOW_INSTRUCTIONS_AVAILABLE) != 0;
  212. #endif
  213. LARGE_INTEGER f;
  214. QueryPerformanceFrequency (&f);
  215. hiResTicksPerSecond = f.QuadPart;
  216. hiResTicksScaleFactor = 1000.0 / hiResTicksPerSecond;
  217. String s (SystemStats::getJUCEVersion());
  218. #ifdef JUCE_DEBUG
  219. const MMRESULT res = timeBeginPeriod (1);
  220. jassert (res == TIMERR_NOERROR);
  221. #else
  222. timeBeginPeriod (1);
  223. #endif
  224. #if defined (JUCE_DEBUG) && JUCE_MSVC && JUCE_CHECK_MEMORY_LEAKS
  225. _CrtSetDbgFlag (_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
  226. #endif
  227. }
  228. //==============================================================================
  229. SystemStats::OperatingSystemType SystemStats::getOperatingSystemType() throw()
  230. {
  231. OSVERSIONINFO info;
  232. info.dwOSVersionInfoSize = sizeof (info);
  233. GetVersionEx (&info);
  234. if (info.dwPlatformId == VER_PLATFORM_WIN32_NT)
  235. {
  236. switch (info.dwMajorVersion)
  237. {
  238. case 5:
  239. return (info.dwMinorVersion == 0) ? Win2000 : WinXP;
  240. case 6:
  241. return WinVista;
  242. default:
  243. jassertfalse // !! not a supported OS!
  244. break;
  245. }
  246. }
  247. else if (info.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS)
  248. {
  249. jassert (info.dwMinorVersion != 0); // !! still running on Windows 95??
  250. return Win98;
  251. }
  252. return UnknownOS;
  253. }
  254. const String SystemStats::getOperatingSystemName() throw()
  255. {
  256. const char* name = "Unknown OS";
  257. switch (getOperatingSystemType())
  258. {
  259. case WinVista:
  260. name = "Windows Vista";
  261. break;
  262. case WinXP:
  263. name = "Windows XP";
  264. break;
  265. case Win2000:
  266. name = "Windows 2000";
  267. break;
  268. case Win98:
  269. name = "Windows 98";
  270. break;
  271. default:
  272. jassertfalse // !! new type of OS?
  273. break;
  274. }
  275. return name;
  276. }
  277. bool SystemStats::isOperatingSystem64Bit() throw()
  278. {
  279. #ifdef _WIN64
  280. return true;
  281. #else
  282. typedef BOOL (WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL);
  283. LPFN_ISWOW64PROCESS fnIsWow64Process = (LPFN_ISWOW64PROCESS) GetProcAddress (GetModuleHandle ("kernel32"), "IsWow64Process");
  284. BOOL isWow64 = FALSE;
  285. return (fnIsWow64Process != 0)
  286. && fnIsWow64Process (GetCurrentProcess(), &isWow64)
  287. && (isWow64 != FALSE);
  288. #endif
  289. }
  290. //==============================================================================
  291. int SystemStats::getMemorySizeInMegabytes() throw()
  292. {
  293. MEMORYSTATUS mem;
  294. GlobalMemoryStatus (&mem);
  295. return (int) (mem.dwTotalPhys / (1024 * 1024)) + 1;
  296. }
  297. int SystemStats::getNumCpus() throw()
  298. {
  299. SYSTEM_INFO systemInfo;
  300. GetSystemInfo (&systemInfo);
  301. return systemInfo.dwNumberOfProcessors;
  302. }
  303. //==============================================================================
  304. uint32 juce_millisecondsSinceStartup() throw()
  305. {
  306. return (uint32) GetTickCount();
  307. }
  308. int64 Time::getHighResolutionTicks() throw()
  309. {
  310. LARGE_INTEGER ticks;
  311. QueryPerformanceCounter (&ticks);
  312. const int64 mainCounterAsHiResTicks = (GetTickCount() * hiResTicksPerSecond) / 1000;
  313. const int64 newOffset = mainCounterAsHiResTicks - ticks.QuadPart;
  314. // fix for a very obscure PCI hardware bug that can make the counter
  315. // sometimes jump forwards by a few seconds..
  316. static int64 hiResTicksOffset = 0;
  317. const int64 offsetDrift = abs64 (newOffset - hiResTicksOffset);
  318. if (offsetDrift > (hiResTicksPerSecond >> 1))
  319. hiResTicksOffset = newOffset;
  320. return ticks.QuadPart + hiResTicksOffset;
  321. }
  322. double Time::getMillisecondCounterHiRes() throw()
  323. {
  324. return getHighResolutionTicks() * hiResTicksScaleFactor;
  325. }
  326. int64 Time::getHighResolutionTicksPerSecond() throw()
  327. {
  328. return hiResTicksPerSecond;
  329. }
  330. int64 SystemStats::getClockCycleCounter() throw()
  331. {
  332. #if JUCE_USE_INTRINSICS
  333. // MS intrinsics version...
  334. return __rdtsc();
  335. #elif JUCE_GCC
  336. // GNU inline asm version...
  337. unsigned int hi = 0, lo = 0;
  338. __asm__ __volatile__ (
  339. "xor %%eax, %%eax \n\
  340. xor %%edx, %%edx \n\
  341. rdtsc \n\
  342. movl %%eax, %[lo] \n\
  343. movl %%edx, %[hi]"
  344. :
  345. : [hi] "m" (hi),
  346. [lo] "m" (lo)
  347. : "cc", "eax", "ebx", "ecx", "edx", "memory");
  348. return (int64) ((((uint64) hi) << 32) | lo);
  349. #else
  350. // MSVC inline asm version...
  351. unsigned int hi = 0, lo = 0;
  352. __asm
  353. {
  354. xor eax, eax
  355. xor edx, edx
  356. rdtsc
  357. mov lo, eax
  358. mov hi, edx
  359. }
  360. return (int64) ((((uint64) hi) << 32) | lo);
  361. #endif
  362. }
  363. int SystemStats::getCpuSpeedInMegaherz() throw()
  364. {
  365. const int64 cycles = SystemStats::getClockCycleCounter();
  366. const uint32 millis = Time::getMillisecondCounter();
  367. int lastResult = 0;
  368. for (;;)
  369. {
  370. int n = 1000000;
  371. while (--n > 0) {}
  372. const uint32 millisElapsed = Time::getMillisecondCounter() - millis;
  373. const int64 cyclesNow = SystemStats::getClockCycleCounter();
  374. if (millisElapsed > 80)
  375. {
  376. const int newResult = (int) (((cyclesNow - cycles) / millisElapsed) / 1000);
  377. if (millisElapsed > 500 || (lastResult == newResult && newResult > 100))
  378. return newResult;
  379. lastResult = newResult;
  380. }
  381. }
  382. }
  383. //==============================================================================
  384. bool Time::setSystemTimeToThisTime() const throw()
  385. {
  386. SYSTEMTIME st;
  387. st.wDayOfWeek = 0;
  388. st.wYear = (WORD) getYear();
  389. st.wMonth = (WORD) (getMonth() + 1);
  390. st.wDay = (WORD) getDayOfMonth();
  391. st.wHour = (WORD) getHours();
  392. st.wMinute = (WORD) getMinutes();
  393. st.wSecond = (WORD) getSeconds();
  394. st.wMilliseconds = (WORD) (millisSinceEpoch % 1000);
  395. // do this twice because of daylight saving conversion problems - the
  396. // first one sets it up, the second one kicks it in.
  397. return SetLocalTime (&st) != 0
  398. && SetLocalTime (&st) != 0;
  399. }
  400. int SystemStats::getPageSize() throw()
  401. {
  402. SYSTEM_INFO systemInfo;
  403. GetSystemInfo (&systemInfo);
  404. return systemInfo.dwPageSize;
  405. }
  406. END_JUCE_NAMESPACE