duckstation

duckstation, but archived from the revision just before upstream changed it to a proprietary software project, this version is the libre one
git clone https://git.neptards.moe/u3shit/duckstation.git
Log | Files | Refs | README | LICENSE

platform_misc_win32.cpp (2107B)


      1 // SPDX-FileCopyrightText: 2019-2024 Connor McLaughlin <stenzek@gmail.com>
      2 // SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0)
      3 
      4 #include "platform_misc.h"
      5 
      6 #include "common/error.h"
      7 #include "common/file_system.h"
      8 #include "common/log.h"
      9 #include "common/small_string.h"
     10 #include "common/string_util.h"
     11 
     12 #include <algorithm>
     13 #include <cinttypes>
     14 #include <memory>
     15 
     16 #include "common/windows_headers.h"
     17 #include <Psapi.h>
     18 #include <WinSock2.h>
     19 #include <mmsystem.h>
     20 
     21 Log_SetChannel(PlatformMisc);
     22 
     23 static bool s_screensaver_suspended = false;
     24 static bool s_winsock_initialized = false;
     25 static std::once_flag s_winsock_initializer;
     26 
     27 bool PlatformMisc::InitializeSocketSupport(Error* error)
     28 {
     29   std::call_once(s_winsock_initializer, [](Error* error) {
     30     WSADATA wsa = {};
     31     if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0)
     32     {
     33       Error::SetSocket(error, "WSAStartup() failed: ", WSAGetLastError());
     34       return false;
     35     }
     36 
     37     s_winsock_initialized = true;
     38     std::atexit([]() { WSACleanup(); });
     39     return true;
     40   }, error);
     41 
     42   return s_winsock_initialized;
     43 }
     44 
     45 static bool SetScreensaverInhibitWin32(bool inhibit)
     46 {
     47   if (SetThreadExecutionState(ES_CONTINUOUS | (inhibit ? (ES_DISPLAY_REQUIRED | ES_SYSTEM_REQUIRED) : 0)) == NULL)
     48   {
     49     ERROR_LOG("SetThreadExecutionState() failed: {}", GetLastError());
     50     return false;
     51   }
     52 
     53   return true;
     54 }
     55 
     56 void PlatformMisc::SuspendScreensaver()
     57 {
     58   if (s_screensaver_suspended)
     59     return;
     60 
     61   if (!SetScreensaverInhibitWin32(true))
     62   {
     63     ERROR_LOG("Failed to suspend screensaver.");
     64     return;
     65   }
     66 
     67   s_screensaver_suspended = true;
     68 }
     69 
     70 void PlatformMisc::ResumeScreensaver()
     71 {
     72   if (!s_screensaver_suspended)
     73     return;
     74 
     75   if (!SetScreensaverInhibitWin32(false))
     76     ERROR_LOG("Failed to resume screensaver.");
     77 
     78   s_screensaver_suspended = false;
     79 }
     80 
     81 size_t PlatformMisc::GetRuntimePageSize()
     82 {
     83   SYSTEM_INFO si = {};
     84   GetSystemInfo(&si);
     85   return si.dwPageSize;
     86 }
     87 
     88 bool PlatformMisc::PlaySoundAsync(const char* path)
     89 {
     90   const std::wstring wpath(FileSystem::GetWin32Path(path));
     91   return PlaySoundW(wpath.c_str(), NULL, SND_ASYNC | SND_NODEFAULT);
     92 }