Host: Add GetTopLevelWindowInfo()

And use it for screensaver inhibiting on Linux.
This commit is contained in:
Connor McLaughlin
2022-11-18 18:14:39 +10:00
parent 3d4d2336a9
commit 8d7aea5e19
25 changed files with 133 additions and 126 deletions

View File

@ -243,7 +243,7 @@ void CommonHost::OnSystemStarted()
FullscreenUI::OnSystemStarted();
if (g_settings.inhibit_screensaver)
FrontendCommon::SuspendScreensaver(g_host_display->GetWindowInfo());
FrontendCommon::SuspendScreensaver();
}
void CommonHost::OnSystemPaused()
@ -261,7 +261,7 @@ void CommonHost::OnSystemResumed()
FullscreenUI::OnSystemResumed();
if (g_settings.inhibit_screensaver)
FrontendCommon::SuspendScreensaver(g_host_display->GetWindowInfo());
FrontendCommon::SuspendScreensaver();
}
void CommonHost::OnSystemDestroyed()
@ -361,7 +361,7 @@ void CommonHost::CheckForSettingsChanges(const Settings& old_settings)
if (g_settings.inhibit_screensaver != old_settings.inhibit_screensaver)
{
if (g_settings.inhibit_screensaver)
FrontendCommon::SuspendScreensaver(g_host_display->GetWindowInfo());
FrontendCommon::SuspendScreensaver();
else
FrontendCommon::ResumeScreensaver();
}

View File

@ -49,8 +49,3 @@ std::unique_ptr<AudioStream> CreateXAudio2Stream(u32 sample_rate, u32 channels,
namespace ImGuiManager {
void RenderDebugWindows();
}
namespace Host {
/// Return the current window handle. Needed for DInput.
void* GetTopLevelWindowHandle();
} // namespace Host

View File

@ -85,9 +85,15 @@ bool DInputSource::Initialize(SettingsInterface& si, std::unique_lock<std::mutex
// need to release the lock while we're enumerating, because we call winId().
settings_lock.unlock();
m_toplevel_window = static_cast<HWND>(Host::GetTopLevelWindowHandle());
const std::optional<WindowInfo> toplevel_wi(Host::GetTopLevelWindowInfo());
if (!toplevel_wi.has_value() || toplevel_wi->type != WindowInfo::Type::Win32)
{
Log_ErrorPrintf("Missing top level window, cannot add DInput devices.");
return false;
}
settings_lock.lock();
m_toplevel_window = static_cast<HWND>(toplevel_wi->window_handle);
ReloadDevices();
return true;
}

View File

@ -9,6 +9,7 @@
#include "common/settings_interface.h"
#include "common/types.h"
#include "common/window_info.h"
/// Class, or source of an input event.
enum class InputSourceType : u32
@ -308,6 +309,9 @@ std::vector<std::string> GetInputProfileNames();
} // namespace InputManager
namespace Host {
/// Return the current window handle. Needed for DInput.
std::optional<WindowInfo> GetTopLevelWindowInfo();
/// Called when a new input device is connected.
void OnInputDeviceConnected(const std::string_view& identifier, const std::string_view& device_name);

View File

@ -1,7 +1,7 @@
#include "common/window_info.h"
namespace FrontendCommon {
void SuspendScreensaver(const WindowInfo& wi);
void SuspendScreensaver();
void ResumeScreensaver();
/// Abstracts platform-specific code for asynchronously playing a sound.

View File

@ -32,13 +32,10 @@ static bool SetScreensaverInhibitMacOS(bool inhibit)
}
static bool s_screensaver_suspended;
static WindowInfo s_screensaver_suspender;
void FrontendCommon::SuspendScreensaver(const WindowInfo& wi)
void FrontendCommon::SuspendScreensaver()
{
if (s_screensaver_suspended &&
(s_screensaver_suspender.type != wi.type || s_screensaver_suspender.window_handle != wi.window_handle))
ResumeScreensaver();
if (s_screensaver_suspended)
if (!SetScreensaverInhibitMacOS(true))
{
@ -46,10 +43,7 @@ void FrontendCommon::SuspendScreensaver(const WindowInfo& wi)
return;
}
Log_InfoPrintf("Screensaver suspended by 0x%" PRIx64 ".",
static_cast<u64>(reinterpret_cast<uintptr_t>(wi.window_handle)));
s_screensaver_suspended = true;
s_screensaver_suspender = wi;
}
void FrontendCommon::ResumeScreensaver()
@ -59,11 +53,8 @@ void FrontendCommon::ResumeScreensaver()
if (!SetScreensaverInhibitMacOS(false))
Log_ErrorPrint("Failed to resume screensaver.");
else
Log_InfoPrint("Screensaver resumed.");
s_screensaver_suspended = false;
s_screensaver_suspender = {};
}
bool FrontendCommon::PlaySoundAsync(const char* path)

View File

@ -1,6 +1,7 @@
#include "common/log.h"
#include "common/string.h"
#include "platform_misc.h"
#include "input_manager.h"
#include <cinttypes>
Log_SetChannel(FrontendCommon);
@ -36,40 +37,42 @@ static bool SetScreensaverInhibitX11(bool inhibit, const WindowInfo& wi)
#endif // USE_X11
static bool SetScreensaverInhibit(bool inhibit, const WindowInfo& wi)
static bool SetScreensaverInhibit(bool inhibit)
{
switch (wi.type)
std::optional<WindowInfo> wi(Host::GetTopLevelWindowInfo());
if (!wi.has_value())
{
Log_ErrorPrintf("No top-level window.");
return false;
}
switch (wi->type)
{
#ifdef USE_X11
case WindowInfo::Type::X11:
return SetScreensaverInhibitX11(inhibit, wi);
return SetScreensaverInhibitX11(inhibit, wi.value());
#endif
default:
Log_ErrorPrintf("Unknown type: %u", static_cast<unsigned>(wi.type));
Log_ErrorPrintf("Unknown type: %u", static_cast<unsigned>(wi->type));
return false;
}
}
static bool s_screensaver_suspended;
static WindowInfo s_screensaver_suspender;
void FrontendCommon::SuspendScreensaver(const WindowInfo& wi)
void FrontendCommon::SuspendScreensaver()
{
if (s_screensaver_suspended &&
(s_screensaver_suspender.type != wi.type || s_screensaver_suspender.window_handle != wi.window_handle))
ResumeScreensaver();
if (s_screensaver_suspended)
return;
if (!SetScreensaverInhibit(true, wi))
if (!SetScreensaverInhibit(true))
{
Log_ErrorPrintf("Failed to suspend screensaver.");
return;
}
Log_InfoPrintf("Screensaver suspended by 0x%" PRIx64 ".",
static_cast<u64>(reinterpret_cast<uintptr_t>(wi.window_handle)));
s_screensaver_suspended = true;
s_screensaver_suspender = wi;
}
void FrontendCommon::ResumeScreensaver()
@ -77,13 +80,10 @@ void FrontendCommon::ResumeScreensaver()
if (!s_screensaver_suspended)
return;
if (!SetScreensaverInhibit(false, s_screensaver_suspender))
if (!SetScreensaverInhibit(false))
Log_ErrorPrint("Failed to resume screensaver.");
else
Log_InfoPrint("Screensaver resumed.");
s_screensaver_suspended = false;
s_screensaver_suspender = {};
}
bool FrontendCommon::PlaySoundAsync(const char* path)

View File

@ -20,13 +20,11 @@ static bool SetScreensaverInhibitWin32(bool inhibit)
}
static bool s_screensaver_suspended;
static WindowInfo s_screensaver_suspender;
void FrontendCommon::SuspendScreensaver(const WindowInfo& wi)
void FrontendCommon::SuspendScreensaver()
{
if (s_screensaver_suspended &&
(s_screensaver_suspender.type != wi.type || s_screensaver_suspender.window_handle != wi.window_handle))
ResumeScreensaver();
if (s_screensaver_suspended)
return;
if (!SetScreensaverInhibitWin32(true))
{
@ -34,10 +32,7 @@ void FrontendCommon::SuspendScreensaver(const WindowInfo& wi)
return;
}
Log_InfoPrintf("Screensaver suspended by 0x%" PRIx64 ".",
static_cast<u64>(reinterpret_cast<uintptr_t>(wi.window_handle)));
s_screensaver_suspended = true;
s_screensaver_suspender = wi;
}
void FrontendCommon::ResumeScreensaver()
@ -47,11 +42,8 @@ void FrontendCommon::ResumeScreensaver()
if (!SetScreensaverInhibitWin32(false))
Log_ErrorPrint("Failed to resume screensaver.");
else
Log_InfoPrint("Screensaver resumed.");
s_screensaver_suspended = false;
s_screensaver_suspender = {};
}
bool FrontendCommon::PlaySoundAsync(const char* path)