2024-05-29 10:30:53 +00:00
|
|
|
// SPDX-FileCopyrightText: 2002-2024 PCSX2 Dev Team
|
2024-07-30 11:42:36 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0+
|
2009-11-26 03:37:10 +00:00
|
|
|
|
2024-04-01 11:24:33 +00:00
|
|
|
#include "common/FileSystem.h"
|
2023-12-26 11:25:10 +00:00
|
|
|
#include "common/HostSys.h"
|
2021-09-01 20:31:46 +00:00
|
|
|
#include "common/RedtapeWindows.h"
|
2022-05-18 13:27:23 +00:00
|
|
|
#include "common/StringUtil.h"
|
2022-12-04 04:52:26 +00:00
|
|
|
#include "common/Threading.h"
|
2022-10-02 13:05:05 +00:00
|
|
|
#include "common/WindowInfo.h"
|
2022-05-18 13:27:23 +00:00
|
|
|
|
|
|
|
#include "fmt/core.h"
|
2009-07-11 08:31:38 +00:00
|
|
|
|
2022-04-18 13:35:14 +00:00
|
|
|
#include <mmsystem.h>
|
2022-11-27 02:51:57 +00:00
|
|
|
#include <timeapi.h>
|
|
|
|
#include <VersionHelpers.h>
|
2009-03-01 03:30:19 +00:00
|
|
|
|
2023-06-14 15:25:48 +00:00
|
|
|
// If anything tries to read this as an initializer, we're in trouble.
|
|
|
|
static const LARGE_INTEGER lfreq = []() {
|
|
|
|
LARGE_INTEGER ret = {};
|
|
|
|
QueryPerformanceFrequency(&ret);
|
|
|
|
return ret;
|
|
|
|
}();
|
2009-03-01 03:30:19 +00:00
|
|
|
|
2022-12-04 04:52:26 +00:00
|
|
|
// This gets leaked... oh well.
|
|
|
|
static thread_local HANDLE s_sleep_timer;
|
|
|
|
static thread_local bool s_sleep_timer_created = false;
|
|
|
|
|
|
|
|
static HANDLE GetSleepTimer()
|
|
|
|
{
|
|
|
|
if (s_sleep_timer_created)
|
|
|
|
return s_sleep_timer;
|
|
|
|
|
|
|
|
s_sleep_timer_created = true;
|
|
|
|
s_sleep_timer = CreateWaitableTimerEx(nullptr, nullptr, CREATE_WAITABLE_TIMER_HIGH_RESOLUTION, TIMER_ALL_ACCESS);
|
|
|
|
if (!s_sleep_timer)
|
|
|
|
s_sleep_timer = CreateWaitableTimer(nullptr, TRUE, nullptr);
|
|
|
|
|
|
|
|
return s_sleep_timer;
|
|
|
|
}
|
|
|
|
|
2009-03-01 03:30:19 +00:00
|
|
|
u64 GetTickFrequency()
|
|
|
|
{
|
2021-07-06 22:53:34 +00:00
|
|
|
return lfreq.QuadPart;
|
2009-03-01 03:30:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
u64 GetCPUTicks()
|
|
|
|
{
|
2021-07-06 22:53:34 +00:00
|
|
|
LARGE_INTEGER count;
|
|
|
|
QueryPerformanceCounter(&count);
|
|
|
|
return count.QuadPart;
|
2009-03-01 03:30:19 +00:00
|
|
|
}
|
2009-07-11 08:31:38 +00:00
|
|
|
|
2010-11-23 21:32:52 +00:00
|
|
|
u64 GetPhysicalMemory()
|
|
|
|
{
|
2021-07-06 22:53:34 +00:00
|
|
|
MEMORYSTATUSEX status;
|
|
|
|
status.dwLength = sizeof(status);
|
|
|
|
GlobalMemoryStatusEx(&status);
|
|
|
|
return status.ullTotalPhys;
|
2010-11-23 21:32:52 +00:00
|
|
|
}
|
|
|
|
|
2021-07-06 19:25:17 +00:00
|
|
|
// Calculates the Windows OS Version and processor architecture, and returns it as a
|
2009-11-26 03:37:10 +00:00
|
|
|
// human-readable string. :)
|
2022-05-18 13:27:23 +00:00
|
|
|
std::string GetOSVersionString()
|
2009-07-11 08:31:38 +00:00
|
|
|
{
|
2022-05-18 13:27:23 +00:00
|
|
|
std::string retval;
|
2016-11-12 15:28:37 +00:00
|
|
|
|
2021-07-06 22:53:34 +00:00
|
|
|
SYSTEM_INFO si;
|
2021-07-06 19:25:17 +00:00
|
|
|
GetNativeSystemInfo(&si);
|
2016-11-12 15:28:37 +00:00
|
|
|
|
2022-11-19 23:07:09 +00:00
|
|
|
if (IsWindows10OrGreater())
|
2022-05-18 13:27:23 +00:00
|
|
|
{
|
|
|
|
retval = "Microsoft ";
|
2022-11-27 02:51:57 +00:00
|
|
|
retval += IsWindowsServer() ? "Windows Server 2016+" : "Windows 10+";
|
2022-11-19 23:07:09 +00:00
|
|
|
|
2022-05-18 13:27:23 +00:00
|
|
|
}
|
2022-11-19 23:07:09 +00:00
|
|
|
else
|
|
|
|
retval = "Unsupported Operating System!";
|
2016-11-12 15:28:37 +00:00
|
|
|
|
2021-07-06 22:53:34 +00:00
|
|
|
return retval;
|
2009-11-26 03:37:10 +00:00
|
|
|
}
|
2010-06-28 18:03:54 +00:00
|
|
|
|
2024-05-23 13:35:34 +00:00
|
|
|
bool Common::InhibitScreensaver(bool inhibit)
|
2015-02-26 02:08:58 +00:00
|
|
|
{
|
2021-07-06 22:53:34 +00:00
|
|
|
EXECUTION_STATE flags = ES_CONTINUOUS;
|
2022-10-02 13:05:05 +00:00
|
|
|
if (inhibit)
|
2021-07-06 22:53:34 +00:00
|
|
|
flags |= ES_DISPLAY_REQUIRED;
|
|
|
|
SetThreadExecutionState(flags);
|
2022-10-02 13:05:05 +00:00
|
|
|
return true;
|
2015-02-26 02:08:58 +00:00
|
|
|
}
|
2022-04-18 13:35:14 +00:00
|
|
|
|
|
|
|
bool Common::PlaySoundAsync(const char* path)
|
|
|
|
{
|
2024-04-01 11:24:33 +00:00
|
|
|
const std::wstring wpath = FileSystem::GetWin32Path(path);
|
2022-04-18 13:35:14 +00:00
|
|
|
return PlaySoundW(wpath.c_str(), NULL, SND_ASYNC | SND_NODEFAULT);
|
|
|
|
}
|
|
|
|
|
2022-12-04 04:52:26 +00:00
|
|
|
void Threading::Sleep(int ms)
|
|
|
|
{
|
|
|
|
::Sleep(ms);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Threading::SleepUntil(u64 ticks)
|
|
|
|
{
|
|
|
|
// This is definitely sub-optimal, but there's no way to sleep until a QPC timestamp on Win32.
|
|
|
|
const s64 diff = static_cast<s64>(ticks - GetCPUTicks());
|
|
|
|
if (diff <= 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
const HANDLE hTimer = GetSleepTimer();
|
|
|
|
if (!hTimer)
|
|
|
|
return;
|
|
|
|
|
|
|
|
const u64 one_hundred_nanos_diff = (static_cast<u64>(diff) * 10000000ULL) / GetTickFrequency();
|
|
|
|
if (one_hundred_nanos_diff == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
LARGE_INTEGER fti;
|
|
|
|
fti.QuadPart = -static_cast<s64>(one_hundred_nanos_diff);
|
|
|
|
|
|
|
|
if (SetWaitableTimer(hTimer, &fti, 0, nullptr, nullptr, FALSE))
|
|
|
|
{
|
|
|
|
WaitForSingleObject(hTimer, INFINITE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|