pcsx2/common/Windows/WinMisc.cpp

142 lines
3.4 KiB
C++
Raw Normal View History

/* PCSX2 - PS2 Emulator for PCs
* Copyright (C) 2002-2021 PCSX2 Dev Team
*
* PCSX2 is free software: you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* PCSX2 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with PCSX2.
* If not, see <http://www.gnu.org/licenses/>.
*/
2021-09-03 10:43:33 +00:00
#if defined(_WIN32)
2021-09-01 20:31:46 +00:00
#include "common/Pcsx2Defs.h"
#include "common/RedtapeWindows.h"
#include "common/StringUtil.h"
2022-12-04 04:52:26 +00:00
#include "common/Threading.h"
#include "common/General.h"
2022-10-02 13:05:05 +00:00
#include "common/WindowInfo.h"
#include "fmt/core.h"
#include <mmsystem.h>
#include <timeapi.h>
#include <VersionHelpers.h>
// 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;
}();
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;
}
u64 GetTickFrequency()
{
2021-07-06 22:53:34 +00:00
return lfreq.QuadPart;
}
u64 GetCPUTicks()
{
2021-07-06 22:53:34 +00:00
LARGE_INTEGER count;
QueryPerformanceCounter(&count);
return count.QuadPart;
}
u64 GetPhysicalMemory()
{
2021-07-06 22:53:34 +00:00
MEMORYSTATUSEX status;
status.dwLength = sizeof(status);
GlobalMemoryStatusEx(&status);
return status.ullTotalPhys;
}
// Calculates the Windows OS Version and processor architecture, and returns it as a
// human-readable string. :)
std::string GetOSVersionString()
{
std::string retval;
2021-07-06 22:53:34 +00:00
SYSTEM_INFO si;
GetNativeSystemInfo(&si);
if (IsWindows10OrGreater())
{
retval = "Microsoft ";
retval += IsWindowsServer() ? "Windows Server 2016+" : "Windows 10+";
}
else
retval = "Unsupported Operating System!";
2021-07-06 22:53:34 +00:00
return retval;
}
2022-10-02 13:05:05 +00:00
bool WindowInfo::InhibitScreensaver(const WindowInfo& wi, bool inhibit)
{
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;
}
bool Common::PlaySoundAsync(const char* path)
{
const std::wstring wpath(StringUtil::UTF8StringToWideString(path));
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;
}
}
2021-09-03 10:43:33 +00:00
#endif