2024-03-29 06:07:54 +00:00
|
|
|
// SPDX-FileCopyrightText: 2019-2024 Connor McLaughlin <stenzek@gmail.com>
|
2022-12-04 11:03:45 +00:00
|
|
|
// SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0)
|
|
|
|
|
2024-03-29 06:07:54 +00:00
|
|
|
#include "platform_misc.h"
|
|
|
|
|
2024-05-25 13:49:14 +00:00
|
|
|
#include "common/error.h"
|
2024-03-29 06:07:54 +00:00
|
|
|
#include "common/file_system.h"
|
2022-09-21 12:44:52 +00:00
|
|
|
#include "common/log.h"
|
2023-09-20 13:49:14 +00:00
|
|
|
#include "common/small_string.h"
|
2022-09-21 12:44:52 +00:00
|
|
|
#include "common/string_util.h"
|
2024-03-29 06:07:54 +00:00
|
|
|
|
2024-05-16 07:24:42 +00:00
|
|
|
#include <algorithm>
|
2022-09-21 12:44:52 +00:00
|
|
|
#include <cinttypes>
|
2024-05-16 07:24:42 +00:00
|
|
|
#include <memory>
|
2022-09-21 12:44:52 +00:00
|
|
|
|
|
|
|
#include "common/windows_headers.h"
|
2024-05-25 13:49:14 +00:00
|
|
|
#include <WinSock2.h>
|
2022-09-21 12:44:52 +00:00
|
|
|
#include <mmsystem.h>
|
|
|
|
|
2024-03-29 06:07:54 +00:00
|
|
|
Log_SetChannel(PlatformMisc);
|
|
|
|
|
2024-05-25 13:49:14 +00:00
|
|
|
static bool s_screensaver_suspended = false;
|
|
|
|
static bool s_winsock_initialized = false;
|
|
|
|
static std::once_flag s_winsock_initializer;
|
|
|
|
|
|
|
|
bool PlatformMisc::InitializeSocketSupport(Error* error)
|
|
|
|
{
|
|
|
|
std::call_once(s_winsock_initializer, [](Error* error) {
|
|
|
|
WSADATA wsa = {};
|
|
|
|
if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0)
|
|
|
|
{
|
|
|
|
Error::SetSocket(error, "WSAStartup() failed: ", WSAGetLastError());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
s_winsock_initialized = true;
|
|
|
|
std::atexit([]() { WSACleanup(); });
|
|
|
|
return true;
|
|
|
|
}, error);
|
|
|
|
|
|
|
|
return s_winsock_initialized;
|
|
|
|
}
|
|
|
|
|
2022-09-21 12:44:52 +00:00
|
|
|
static bool SetScreensaverInhibitWin32(bool inhibit)
|
|
|
|
{
|
|
|
|
if (SetThreadExecutionState(ES_CONTINUOUS | (inhibit ? (ES_DISPLAY_REQUIRED | ES_SYSTEM_REQUIRED) : 0)) == NULL)
|
|
|
|
{
|
2024-05-23 10:55:28 +00:00
|
|
|
ERROR_LOG("SetThreadExecutionState() failed: {}", GetLastError());
|
2022-09-21 12:44:52 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-08-23 12:06:48 +00:00
|
|
|
void PlatformMisc::SuspendScreensaver()
|
2022-09-21 12:44:52 +00:00
|
|
|
{
|
2022-11-18 08:14:39 +00:00
|
|
|
if (s_screensaver_suspended)
|
|
|
|
return;
|
2022-09-21 12:44:52 +00:00
|
|
|
|
|
|
|
if (!SetScreensaverInhibitWin32(true))
|
|
|
|
{
|
2024-05-23 10:55:28 +00:00
|
|
|
ERROR_LOG("Failed to suspend screensaver.");
|
2022-09-21 12:44:52 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
s_screensaver_suspended = true;
|
|
|
|
}
|
|
|
|
|
2023-08-23 12:06:48 +00:00
|
|
|
void PlatformMisc::ResumeScreensaver()
|
2022-09-21 12:44:52 +00:00
|
|
|
{
|
|
|
|
if (!s_screensaver_suspended)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!SetScreensaverInhibitWin32(false))
|
2024-05-23 10:55:28 +00:00
|
|
|
ERROR_LOG("Failed to resume screensaver.");
|
2022-09-21 12:44:52 +00:00
|
|
|
|
|
|
|
s_screensaver_suspended = false;
|
|
|
|
}
|
|
|
|
|
2024-05-16 07:24:42 +00:00
|
|
|
size_t PlatformMisc::GetRuntimePageSize()
|
|
|
|
{
|
|
|
|
SYSTEM_INFO si = {};
|
|
|
|
GetSystemInfo(&si);
|
|
|
|
return si.dwPageSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t PlatformMisc::GetRuntimeCacheLineSize()
|
|
|
|
{
|
|
|
|
DWORD size = 0;
|
|
|
|
if (!GetLogicalProcessorInformation(nullptr, &size) && GetLastError() != ERROR_INSUFFICIENT_BUFFER)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
std::unique_ptr<SYSTEM_LOGICAL_PROCESSOR_INFORMATION[]> lpi =
|
|
|
|
std::make_unique<SYSTEM_LOGICAL_PROCESSOR_INFORMATION[]>(
|
|
|
|
(size + (sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION) - 1)) / sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION));
|
|
|
|
if (!GetLogicalProcessorInformation(lpi.get(), &size))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
u32 max_line_size = 0;
|
|
|
|
for (u32 i = 0; i < size / sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION); i++)
|
|
|
|
{
|
|
|
|
if (lpi[i].Relationship == RelationCache)
|
|
|
|
max_line_size = std::max<u32>(max_line_size, lpi[i].Cache.LineSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
return max_line_size;
|
|
|
|
}
|
|
|
|
|
2023-08-23 12:06:48 +00:00
|
|
|
bool PlatformMisc::PlaySoundAsync(const char* path)
|
2022-09-21 12:44:52 +00:00
|
|
|
{
|
2024-03-29 06:07:54 +00:00
|
|
|
const std::wstring wpath(FileSystem::GetWin32Path(path));
|
2022-09-21 12:44:52 +00:00
|
|
|
return PlaySoundW(wpath.c_str(), NULL, SND_ASYNC | SND_NODEFAULT);
|
|
|
|
}
|