Duckstation/src/util/platform_misc_win32.cpp

93 lines
2.1 KiB
C++
Raw Normal View History

// SPDX-FileCopyrightText: 2019-2024 Connor McLaughlin <stenzek@gmail.com>
// SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0)
#include "platform_misc.h"
2024-05-25 13:49:14 +00:00
#include "common/error.h"
#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"
#include <algorithm>
2022-09-21 12:44:52 +00:00
#include <cinttypes>
#include <memory>
2022-09-21 12:44:52 +00:00
#include "common/windows_headers.h"
#include <Psapi.h>
2024-05-25 13:49:14 +00:00
#include <WinSock2.h>
2022-09-21 12:44:52 +00:00
#include <mmsystem.h>
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
{
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;
}
size_t PlatformMisc::GetRuntimePageSize()
{
SYSTEM_INFO si = {};
GetSystemInfo(&si);
return si.dwPageSize;
}
2023-08-23 12:06:48 +00:00
bool PlatformMisc::PlaySoundAsync(const char* path)
2022-09-21 12:44:52 +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);
}