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.

592 lines
16KB

  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. #include "juce_win32_DynamicLibraryLoader.h"
  49. extern void juce_updateMultiMonitorInfo(); // from WindowDriver
  50. //==============================================================================
  51. void Logger::outputDebugString (const String& text) throw()
  52. {
  53. OutputDebugString (text + T("\n"));
  54. }
  55. void Logger::outputDebugPrintf (const tchar* format, ...) throw()
  56. {
  57. String text;
  58. va_list args;
  59. va_start (args, format);
  60. text.vprintf(format, args);
  61. outputDebugString (text);
  62. }
  63. //==============================================================================
  64. static int64 hiResTicksPerSecond;
  65. static double hiResTicksScaleFactor;
  66. static SYSTEM_INFO systemInfo;
  67. static struct _LogicalCpuInfo
  68. {
  69. bool htSupported;
  70. bool htAvailable;
  71. int numPackages;
  72. int numLogicalPerPackage;
  73. unsigned long physicalAffinityMask;
  74. } logicalCpuInfo;
  75. //==============================================================================
  76. #if JUCE_USE_INTRINSICS
  77. // CPU info functions using intrinsics...
  78. #pragma intrinsic (__cpuid)
  79. #pragma intrinsic (__rdtsc)
  80. static unsigned int getCPUIDWord (int* familyModel = 0, int* extFeatures = 0) throw()
  81. {
  82. int info [4];
  83. __cpuid (info, 1);
  84. if (familyModel != 0)
  85. *familyModel = info [0];
  86. if (extFeatures != 0)
  87. *extFeatures = info[1];
  88. return info[3];
  89. }
  90. const String SystemStats::getCpuVendor() throw()
  91. {
  92. int info [4];
  93. __cpuid (info, 0);
  94. char v [12];
  95. memcpy (v, info + 1, 4);
  96. memcpy (v + 4, info + 3, 4);
  97. memcpy (v + 8, info + 2, 4);
  98. return String (v, 12);
  99. }
  100. #else
  101. //==============================================================================
  102. // CPU info functions using old fashioned inline asm...
  103. static juce_noinline unsigned int getCPUIDWord (int* familyModel = 0, int* extFeatures = 0)
  104. {
  105. unsigned int cpu = 0;
  106. unsigned int ext = 0;
  107. unsigned int family = 0;
  108. #ifdef JUCE_GCC
  109. unsigned int dummy = 0;
  110. #endif
  111. #ifndef __MINGW32__
  112. __try
  113. #endif
  114. {
  115. #ifdef JUCE_GCC
  116. __asm__ ("cpuid" : "=a" (family), "=b" (ext), "=c" (dummy),"=d" (cpu) : "a" (1));
  117. #else
  118. __asm
  119. {
  120. mov eax, 1
  121. cpuid
  122. mov cpu, edx
  123. mov family, eax
  124. mov ext, ebx
  125. }
  126. #endif
  127. }
  128. #ifndef __MINGW32__
  129. __except (EXCEPTION_EXECUTE_HANDLER)
  130. {
  131. return 0;
  132. }
  133. #endif
  134. if (familyModel != 0)
  135. *familyModel = family;
  136. if (extFeatures != 0)
  137. *extFeatures = ext;
  138. return cpu;
  139. }
  140. static void juce_getCpuVendor (char* const v)
  141. {
  142. int vendor[4];
  143. zeromem (vendor, 16);
  144. #ifdef JUCE_64BIT
  145. #else
  146. #ifndef __MINGW32__
  147. __try
  148. #endif
  149. {
  150. #ifdef JUCE_GCC
  151. unsigned int dummy = 0;
  152. __asm__ ("cpuid" : "=a" (dummy), "=b" (vendor[0]), "=c" (vendor[2]),"=d" (vendor[1]) : "a" (0));
  153. #else
  154. __asm
  155. {
  156. mov eax, 0
  157. cpuid
  158. mov [vendor], ebx
  159. mov [vendor + 4], edx
  160. mov [vendor + 8], ecx
  161. }
  162. #endif
  163. }
  164. #ifndef __MINGW32__
  165. __except (EXCEPTION_EXECUTE_HANDLER)
  166. {
  167. *v = 0;
  168. }
  169. #endif
  170. #endif
  171. memcpy (v, vendor, 16);
  172. }
  173. const String SystemStats::getCpuVendor()
  174. {
  175. char v [16];
  176. juce_getCpuVendor (v);
  177. return String (v, 16);
  178. }
  179. #endif
  180. static void initLogicalCpuInfo() throw()
  181. {
  182. int familyModelWord, extFeaturesWord;
  183. int featuresWord = getCPUIDWord (&familyModelWord, &extFeaturesWord);
  184. HANDLE hCurrentProcessHandle = GetCurrentProcess();
  185. logicalCpuInfo.htSupported = false;
  186. logicalCpuInfo.htAvailable = false;
  187. logicalCpuInfo.numLogicalPerPackage = 1;
  188. logicalCpuInfo.numPackages = 0;
  189. logicalCpuInfo.physicalAffinityMask = 0;
  190. DWORD_PTR processAffinity, systemAffinity;
  191. if (! GetProcessAffinityMask (hCurrentProcessHandle, &processAffinity, &systemAffinity))
  192. return;
  193. // Checks: CPUID supported, model >= Pentium 4, Hyperthreading bit set, logical CPUs per package > 1
  194. if (featuresWord == 0
  195. || ((familyModelWord >> 8) & 0xf) < 15
  196. || (featuresWord & (1 << 28)) == 0
  197. || ((extFeaturesWord >> 16) & 0xff) < 2)
  198. {
  199. logicalCpuInfo.physicalAffinityMask = static_cast <unsigned long> (processAffinity);
  200. return;
  201. }
  202. logicalCpuInfo.htSupported = true;
  203. logicalCpuInfo.numLogicalPerPackage = (extFeaturesWord >> 16) & 0xff;
  204. unsigned int affinityMask;
  205. unsigned int physAff = 0;
  206. unsigned char i = 1;
  207. unsigned char physIdMask = 0xFF;
  208. unsigned char physIdShift = 0;
  209. unsigned char apicId;
  210. unsigned char logId;
  211. unsigned char physId;
  212. while (i < logicalCpuInfo.numLogicalPerPackage)
  213. {
  214. i *= 2;
  215. physIdMask <<= 1;
  216. physIdShift++;
  217. }
  218. affinityMask = 1;
  219. logicalCpuInfo.numPackages = 0;
  220. while ((affinityMask != 0) && (affinityMask <= processAffinity))
  221. {
  222. if (SetProcessAffinityMask (hCurrentProcessHandle, affinityMask))
  223. {
  224. Sleep(0); // schedule onto correct CPU
  225. featuresWord = getCPUIDWord (&familyModelWord, &extFeaturesWord);
  226. apicId = (unsigned char) (extFeaturesWord >> 24);
  227. logId = (unsigned char) (apicId & ~physIdMask);
  228. physId = (unsigned char) (apicId >> physIdShift);
  229. if (logId != 0)
  230. logicalCpuInfo.htAvailable = true;
  231. if ((((int) logId) % logicalCpuInfo.numLogicalPerPackage) == 0)
  232. {
  233. // This is a physical CPU
  234. physAff |= affinityMask;
  235. logicalCpuInfo.numPackages++;
  236. }
  237. }
  238. affinityMask = affinityMask << 1;
  239. }
  240. logicalCpuInfo.physicalAffinityMask = physAff;
  241. SetProcessAffinityMask(hCurrentProcessHandle, processAffinity);
  242. }
  243. //==============================================================================
  244. void juce_initialiseThreadEvents() throw();
  245. void juce_initialiseUnicodeFileFunctions() throw();
  246. static struct JuceCpuProps
  247. {
  248. bool hasMMX : 1, hasSSE : 1, hasSSE2 : 1, has3DNow : 1;
  249. } juce_CpuProps;
  250. bool SystemStats::hasMMX() throw()
  251. {
  252. return juce_CpuProps.hasMMX;
  253. }
  254. bool SystemStats::hasSSE() throw()
  255. {
  256. return juce_CpuProps.hasSSE;
  257. }
  258. bool SystemStats::hasSSE2() throw()
  259. {
  260. return juce_CpuProps.hasSSE2;
  261. }
  262. bool SystemStats::has3DNow() throw()
  263. {
  264. return juce_CpuProps.has3DNow;
  265. }
  266. void SystemStats::initialiseStats() throw()
  267. {
  268. juce_initialiseUnicodeFileFunctions();
  269. juce_initialiseThreadEvents();
  270. juce_CpuProps.hasMMX = IsProcessorFeaturePresent (PF_MMX_INSTRUCTIONS_AVAILABLE) != 0;
  271. juce_CpuProps.hasSSE = IsProcessorFeaturePresent (PF_XMMI_INSTRUCTIONS_AVAILABLE) != 0;
  272. juce_CpuProps.hasSSE2 = IsProcessorFeaturePresent (PF_XMMI64_INSTRUCTIONS_AVAILABLE) != 0;
  273. #ifdef PF_AMD3D_INSTRUCTIONS_AVAILABLE
  274. juce_CpuProps.has3DNow = IsProcessorFeaturePresent (PF_AMD3D_INSTRUCTIONS_AVAILABLE) != 0;
  275. #else
  276. juce_CpuProps.has3DNow = IsProcessorFeaturePresent (PF_3DNOW_INSTRUCTIONS_AVAILABLE) != 0;
  277. #endif
  278. LARGE_INTEGER f;
  279. QueryPerformanceFrequency (&f);
  280. hiResTicksPerSecond = f.QuadPart;
  281. hiResTicksScaleFactor = 1000.0 / hiResTicksPerSecond;
  282. String s (SystemStats::getJUCEVersion());
  283. GetSystemInfo (&systemInfo);
  284. initLogicalCpuInfo();
  285. #ifdef JUCE_DEBUG
  286. const MMRESULT res = timeBeginPeriod (1);
  287. jassert (res == TIMERR_NOERROR);
  288. #else
  289. timeBeginPeriod (1);
  290. #endif
  291. #if JUCE_DEBUG && JUCE_MSVC && JUCE_CHECK_MEMORY_LEAKS
  292. _CrtSetDbgFlag (_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
  293. #endif
  294. }
  295. //==============================================================================
  296. SystemStats::OperatingSystemType SystemStats::getOperatingSystemType() throw()
  297. {
  298. OSVERSIONINFO info;
  299. info.dwOSVersionInfoSize = sizeof (info);
  300. GetVersionEx (&info);
  301. if (info.dwPlatformId == VER_PLATFORM_WIN32_NT)
  302. {
  303. switch (info.dwMajorVersion)
  304. {
  305. case 5:
  306. return (info.dwMinorVersion == 0) ? Win2000 : WinXP;
  307. case 6:
  308. return WinVista;
  309. default:
  310. jassertfalse // !! not a supported OS!
  311. break;
  312. }
  313. }
  314. else if (info.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS)
  315. {
  316. jassert (info.dwMinorVersion != 0); // !! still running on Windows 95??
  317. return Win98;
  318. }
  319. return UnknownOS;
  320. }
  321. const String SystemStats::getOperatingSystemName() throw()
  322. {
  323. const char* name = "Unknown OS";
  324. switch (getOperatingSystemType())
  325. {
  326. case WinVista:
  327. name = "Windows Vista";
  328. break;
  329. case WinXP:
  330. name = "Windows XP";
  331. break;
  332. case Win2000:
  333. name = "Windows 2000";
  334. break;
  335. case Win98:
  336. name = "Windows 98";
  337. break;
  338. default:
  339. jassertfalse // !! new type of OS?
  340. break;
  341. }
  342. return name;
  343. }
  344. //==============================================================================
  345. int SystemStats::getMemorySizeInMegabytes() throw()
  346. {
  347. MEMORYSTATUS mem;
  348. GlobalMemoryStatus (&mem);
  349. return (int) (mem.dwTotalPhys / (1024 * 1024)) + 1;
  350. }
  351. bool SystemStats::hasHyperThreading() throw()
  352. {
  353. return logicalCpuInfo.htAvailable;
  354. }
  355. int SystemStats::getNumPhysicalCpus() throw()
  356. {
  357. if (logicalCpuInfo.numPackages)
  358. return logicalCpuInfo.numPackages;
  359. return getNumLogicalCpus();
  360. }
  361. int SystemStats::getNumLogicalCpus() throw()
  362. {
  363. return systemInfo.dwNumberOfProcessors;
  364. }
  365. uint32 SystemStats::getPhysicalAffinityMask() throw()
  366. {
  367. return logicalCpuInfo.physicalAffinityMask;
  368. }
  369. //==============================================================================
  370. uint32 juce_millisecondsSinceStartup() throw()
  371. {
  372. return (uint32) GetTickCount();
  373. }
  374. int64 Time::getHighResolutionTicks() throw()
  375. {
  376. LARGE_INTEGER ticks;
  377. QueryPerformanceCounter (&ticks);
  378. const int64 mainCounterAsHiResTicks = (GetTickCount() * hiResTicksPerSecond) / 1000;
  379. const int64 newOffset = mainCounterAsHiResTicks - ticks.QuadPart;
  380. // fix for a very obscure PCI hardware bug that can make the counter
  381. // sometimes jump forwards by a few seconds..
  382. static int64 hiResTicksOffset = 0;
  383. const int offsetDrift = abs ((int) (newOffset - hiResTicksOffset));
  384. if (offsetDrift > ((int) hiResTicksPerSecond) >> 1)
  385. hiResTicksOffset = newOffset;
  386. return ticks.QuadPart + hiResTicksOffset;
  387. }
  388. double Time::getMillisecondCounterHiRes() throw()
  389. {
  390. return getHighResolutionTicks() * hiResTicksScaleFactor;
  391. }
  392. int64 Time::getHighResolutionTicksPerSecond() throw()
  393. {
  394. return hiResTicksPerSecond;
  395. }
  396. int64 SystemStats::getClockCycleCounter() throw()
  397. {
  398. #if JUCE_USE_INTRINSICS
  399. // MS intrinsics version...
  400. return __rdtsc();
  401. #elif JUCE_GCC
  402. // GNU inline asm version...
  403. unsigned int hi = 0, lo = 0;
  404. __asm__ __volatile__ (
  405. "xor %%eax, %%eax \n\
  406. xor %%edx, %%edx \n\
  407. rdtsc \n\
  408. movl %%eax, %[lo] \n\
  409. movl %%edx, %[hi]"
  410. :
  411. : [hi] "m" (hi),
  412. [lo] "m" (lo)
  413. : "cc", "eax", "ebx", "ecx", "edx", "memory");
  414. return (int64) ((((uint64) hi) << 32) | lo);
  415. #else
  416. // MSVC inline asm version...
  417. unsigned int hi = 0, lo = 0;
  418. __asm
  419. {
  420. xor eax, eax
  421. xor edx, edx
  422. rdtsc
  423. mov lo, eax
  424. mov hi, edx
  425. }
  426. return (int64) ((((uint64) hi) << 32) | lo);
  427. #endif
  428. }
  429. int SystemStats::getCpuSpeedInMegaherz() throw()
  430. {
  431. const int64 cycles = SystemStats::getClockCycleCounter();
  432. const uint32 millis = Time::getMillisecondCounter();
  433. int lastResult = 0;
  434. for (;;)
  435. {
  436. int n = 1000000;
  437. while (--n > 0) {}
  438. const uint32 millisElapsed = Time::getMillisecondCounter() - millis;
  439. const int64 cyclesNow = SystemStats::getClockCycleCounter();
  440. if (millisElapsed > 80)
  441. {
  442. const int newResult = (int) (((cyclesNow - cycles) / millisElapsed) / 1000);
  443. if (millisElapsed > 500 || (lastResult == newResult && newResult > 100))
  444. return newResult;
  445. lastResult = newResult;
  446. }
  447. }
  448. }
  449. //==============================================================================
  450. bool Time::setSystemTimeToThisTime() const throw()
  451. {
  452. SYSTEMTIME st;
  453. st.wDayOfWeek = 0;
  454. st.wYear = (WORD) getYear();
  455. st.wMonth = (WORD) (getMonth() + 1);
  456. st.wDay = (WORD) getDayOfMonth();
  457. st.wHour = (WORD) getHours();
  458. st.wMinute = (WORD) getMinutes();
  459. st.wSecond = (WORD) getSeconds();
  460. st.wMilliseconds = (WORD) (millisSinceEpoch % 1000);
  461. // do this twice because of daylight saving conversion problems - the
  462. // first one sets it up, the second one kicks it in.
  463. return SetLocalTime (&st) != 0
  464. && SetLocalTime (&st) != 0;
  465. }
  466. int SystemStats::getPageSize() throw()
  467. {
  468. return systemInfo.dwPageSize;
  469. }
  470. END_JUCE_NAMESPACE