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

@ -964,9 +964,9 @@ void Host::SetFullscreen(bool enabled)
g_nogui_window->SetFullscreen(enabled); g_nogui_window->SetFullscreen(enabled);
} }
void* Host::GetTopLevelWindowHandle() std::optional<WindowInfo> Host::GetTopLevelWindowInfo()
{ {
return g_nogui_window->GetPlatformWindowHandle(); return g_nogui_window->GetPlatformWindowInfo();
} }
void Host::RequestExit(bool save_state_if_running) void Host::RequestExit(bool save_state_if_running)

View file

@ -25,7 +25,6 @@ public:
virtual std::optional<WindowInfo> GetPlatformWindowInfo() = 0; virtual std::optional<WindowInfo> GetPlatformWindowInfo() = 0;
virtual void SetPlatformWindowTitle(std::string title) = 0; virtual void SetPlatformWindowTitle(std::string title) = 0;
virtual void* GetPlatformWindowHandle() = 0;
virtual std::optional<u32> ConvertHostKeyboardStringToCode(const std::string_view& str) = 0; virtual std::optional<u32> ConvertHostKeyboardStringToCode(const std::string_view& str) = 0;
virtual std::optional<std::string> ConvertHostKeyboardCodeToString(u32 code) = 0; virtual std::optional<std::string> ConvertHostKeyboardCodeToString(u32 code) = 0;

View file

@ -130,11 +130,6 @@ void VTYNoGUIPlatform::SetPlatformWindowTitle(std::string title)
Log_InfoPrintf("Window Title: %s", title.c_str()); Log_InfoPrintf("Window Title: %s", title.c_str());
} }
void* VTYNoGUIPlatform::GetPlatformWindowHandle()
{
return nullptr;
}
void VTYNoGUIPlatform::RunMessageLoop() void VTYNoGUIPlatform::RunMessageLoop()
{ {
while (m_message_loop_running.load(std::memory_order_acquire)) while (m_message_loop_running.load(std::memory_order_acquire))

View file

@ -24,7 +24,6 @@ public:
void DestroyPlatformWindow() override; void DestroyPlatformWindow() override;
std::optional<WindowInfo> GetPlatformWindowInfo() override; std::optional<WindowInfo> GetPlatformWindowInfo() override;
void SetPlatformWindowTitle(std::string title) override; void SetPlatformWindowTitle(std::string title) override;
void* GetPlatformWindowHandle() override;
std::optional<u32> ConvertHostKeyboardStringToCode(const std::string_view& str) override; std::optional<u32> ConvertHostKeyboardStringToCode(const std::string_view& str) override;
std::optional<std::string> ConvertHostKeyboardCodeToString(u32 code) override; std::optional<std::string> ConvertHostKeyboardCodeToString(u32 code) override;

View file

@ -193,11 +193,6 @@ void WaylandNoGUIPlatform::SetPlatformWindowTitle(std::string title)
xdg_toplevel_set_title(m_xdg_toplevel, title.c_str()); xdg_toplevel_set_title(m_xdg_toplevel, title.c_str());
} }
void* WaylandNoGUIPlatform::GetPlatformWindowHandle()
{
return m_surface;
}
std::optional<u32> WaylandNoGUIPlatform::ConvertHostKeyboardStringToCode(const std::string_view& str) std::optional<u32> WaylandNoGUIPlatform::ConvertHostKeyboardStringToCode(const std::string_view& str)
{ {
std::unique_lock lock(m_key_map_mutex); std::unique_lock lock(m_key_map_mutex);

View file

@ -29,7 +29,6 @@ public:
void DestroyPlatformWindow() override; void DestroyPlatformWindow() override;
std::optional<WindowInfo> GetPlatformWindowInfo() override; std::optional<WindowInfo> GetPlatformWindowInfo() override;
void SetPlatformWindowTitle(std::string title) override; void SetPlatformWindowTitle(std::string title) override;
void* GetPlatformWindowHandle() override;
std::optional<u32> ConvertHostKeyboardStringToCode(const std::string_view& str) override; std::optional<u32> ConvertHostKeyboardStringToCode(const std::string_view& str) override;
std::optional<std::string> ConvertHostKeyboardCodeToString(u32 code) override; std::optional<std::string> ConvertHostKeyboardCodeToString(u32 code) override;

View file

@ -177,11 +177,6 @@ void Win32NoGUIPlatform::SetPlatformWindowTitle(std::string title)
SetWindowTextW(m_hwnd, StringUtil::UTF8StringToWideString(title).c_str()); SetWindowTextW(m_hwnd, StringUtil::UTF8StringToWideString(title).c_str());
} }
void* Win32NoGUIPlatform::GetPlatformWindowHandle()
{
return m_hwnd;
}
std::optional<u32> Win32NoGUIPlatform::ConvertHostKeyboardStringToCode(const std::string_view& str) std::optional<u32> Win32NoGUIPlatform::ConvertHostKeyboardStringToCode(const std::string_view& str)
{ {
std::optional<DWORD> converted(Win32KeyNames::GetKeyCodeForName(str)); std::optional<DWORD> converted(Win32KeyNames::GetKeyCodeForName(str));

View file

@ -23,7 +23,6 @@ public:
void DestroyPlatformWindow() override; void DestroyPlatformWindow() override;
std::optional<WindowInfo> GetPlatformWindowInfo() override; std::optional<WindowInfo> GetPlatformWindowInfo() override;
void SetPlatformWindowTitle(std::string title) override; void SetPlatformWindowTitle(std::string title) override;
void* GetPlatformWindowHandle() override;
std::optional<u32> ConvertHostKeyboardStringToCode(const std::string_view& str) override; std::optional<u32> ConvertHostKeyboardStringToCode(const std::string_view& str) override;
std::optional<std::string> ConvertHostKeyboardCodeToString(u32 code) override; std::optional<std::string> ConvertHostKeyboardCodeToString(u32 code) override;

View file

@ -129,11 +129,6 @@ void X11NoGUIPlatform::SetPlatformWindowTitle(std::string title)
}); });
} }
void* X11NoGUIPlatform::GetPlatformWindowHandle()
{
return reinterpret_cast<void*>(m_window);
}
void X11NoGUIPlatform::InitializeKeyMap() void X11NoGUIPlatform::InitializeKeyMap()
{ {
int min_keycode = 0, max_keycode = -1; int min_keycode = 0, max_keycode = -1;

View file

@ -48,7 +48,6 @@ public:
void DestroyPlatformWindow() override; void DestroyPlatformWindow() override;
std::optional<WindowInfo> GetPlatformWindowInfo() override; std::optional<WindowInfo> GetPlatformWindowInfo() override;
void SetPlatformWindowTitle(std::string title) override; void SetPlatformWindowTitle(std::string title) override;
void* GetPlatformWindowHandle() override;
std::optional<u32> ConvertHostKeyboardStringToCode(const std::string_view& str) override; std::optional<u32> ConvertHostKeyboardStringToCode(const std::string_view& str) override;
std::optional<std::string> ConvertHostKeyboardCodeToString(u32 code) override; std::optional<std::string> ConvertHostKeyboardCodeToString(u32 code) override;

View file

@ -44,62 +44,28 @@ DisplayWidget::~DisplayWidget()
#endif #endif
} }
qreal DisplayWidget::devicePixelRatioFromScreen() const
{
const QScreen* screen_for_ratio = screen();
if (!screen_for_ratio)
screen_for_ratio = QGuiApplication::primaryScreen();
return screen_for_ratio ? screen_for_ratio->devicePixelRatio() : static_cast<qreal>(1);
}
int DisplayWidget::scaledWindowWidth() const int DisplayWidget::scaledWindowWidth() const
{ {
return std::max(static_cast<int>(std::ceil(static_cast<qreal>(width()) * devicePixelRatioFromScreen())), 1); return std::max(
static_cast<int>(std::ceil(static_cast<qreal>(width()) * QtUtils::GetDevicePixelRatioForWidget(this))), 1);
} }
int DisplayWidget::scaledWindowHeight() const int DisplayWidget::scaledWindowHeight() const
{ {
return std::max(static_cast<int>(std::ceil(static_cast<qreal>(height()) * devicePixelRatioFromScreen())), 1); return std::max(
static_cast<int>(std::ceil(static_cast<qreal>(height()) * QtUtils::GetDevicePixelRatioForWidget(this))), 1);
} }
std::optional<WindowInfo> DisplayWidget::getWindowInfo() std::optional<WindowInfo> DisplayWidget::getWindowInfo()
{ {
WindowInfo wi; std::optional<WindowInfo> ret(QtUtils::GetWindowInfoForWidget(this));
if (ret.has_value())
// Windows and Apple are easy here since there's no display connection.
#if defined(_WIN32)
wi.type = WindowInfo::Type::Win32;
wi.window_handle = reinterpret_cast<void*>(winId());
#elif defined(__APPLE__)
wi.type = WindowInfo::Type::MacOS;
wi.window_handle = reinterpret_cast<void*>(winId());
#else
QPlatformNativeInterface* pni = QGuiApplication::platformNativeInterface();
const QString platform_name = QGuiApplication::platformName();
if (platform_name == QStringLiteral("xcb"))
{ {
wi.type = WindowInfo::Type::X11; m_last_window_width = ret->surface_width;
wi.display_connection = pni->nativeResourceForWindow("display", windowHandle()); m_last_window_height = ret->surface_height;
wi.window_handle = reinterpret_cast<void*>(winId()); m_last_window_scale = ret->surface_scale;
} }
else if (platform_name == QStringLiteral("wayland")) return ret;
{
wi.type = WindowInfo::Type::Wayland;
wi.display_connection = pni->nativeResourceForWindow("display", windowHandle());
wi.window_handle = pni->nativeResourceForWindow("surface", windowHandle());
}
else
{
qCritical() << "Unknown PNI platform " << platform_name;
return std::nullopt;
}
#endif
m_last_window_width = wi.surface_width = static_cast<u32>(scaledWindowWidth());
m_last_window_height = wi.surface_height = static_cast<u32>(scaledWindowHeight());
m_last_window_scale = wi.surface_scale = static_cast<float>(devicePixelRatioFromScreen());
return wi;
} }
void DisplayWidget::updateRelativeMode(bool enabled) void DisplayWidget::updateRelativeMode(bool enabled)
@ -270,7 +236,7 @@ bool DisplayWidget::event(QEvent* event)
if (!m_relative_mouse_enabled) if (!m_relative_mouse_enabled)
{ {
const qreal dpr = devicePixelRatioFromScreen(); const qreal dpr = QtUtils::GetDevicePixelRatioForWidget(this);
const QPoint mouse_pos = mouse_event->pos(); const QPoint mouse_pos = mouse_event->pos();
const float scaled_x = static_cast<float>(static_cast<qreal>(mouse_pos.x()) * dpr); const float scaled_x = static_cast<float>(static_cast<qreal>(mouse_pos.x()) * dpr);
@ -341,7 +307,7 @@ bool DisplayWidget::event(QEvent* event)
{ {
QWidget::event(event); QWidget::event(event);
const float dpr = devicePixelRatioFromScreen(); const float dpr = QtUtils::GetDevicePixelRatioForWidget(this);
const u32 scaled_width = const u32 scaled_width =
static_cast<u32>(std::max(static_cast<int>(std::ceil(static_cast<qreal>(width()) * dpr)), 1)); static_cast<u32>(std::max(static_cast<int>(std::ceil(static_cast<qreal>(width()) * dpr)), 1));
const u32 scaled_height = const u32 scaled_height =

View file

@ -19,7 +19,6 @@ public:
int scaledWindowWidth() const; int scaledWindowWidth() const;
int scaledWindowHeight() const; int scaledWindowHeight() const;
qreal devicePixelRatioFromScreen() const;
std::optional<WindowInfo> getWindowInfo(); std::optional<WindowInfo> getWindowInfo();

View file

@ -2520,6 +2520,13 @@ void MainWindow::checkForSettingChanges()
updateWindowState(); updateWindowState();
} }
void MainWindow::getWindowInfo(WindowInfo* wi)
{
std::optional<WindowInfo> opt_wi(QtUtils::GetWindowInfoForWidget(this));
if (opt_wi.has_value())
*wi = opt_wi.value();
}
void MainWindow::onCheckForUpdatesActionTriggered() void MainWindow::onCheckForUpdatesActionTriggered()
{ {
// Wipe out the last version, that way it displays the update if we've previously skipped it. // Wipe out the last version, that way it displays the update if we've previously skipped it.

View file

@ -4,8 +4,10 @@
#include <QtWidgets/QMainWindow> #include <QtWidgets/QMainWindow>
#include <QtWidgets/QStackedWidget> #include <QtWidgets/QStackedWidget>
#include <memory> #include <memory>
#include <optional>
#include "controllersettingsdialog.h" #include "controllersettingsdialog.h"
#include "common/window_info.h"
#include "core/types.h" #include "core/types.h"
#include "displaywidget.h" #include "displaywidget.h"
#include "settingsdialog.h" #include "settingsdialog.h"
@ -97,6 +99,7 @@ public Q_SLOTS:
bool requestShutdown(bool allow_confirm = true, bool allow_save_to_state = true, bool save_state = true, bool block_until_done = false); bool requestShutdown(bool allow_confirm = true, bool allow_save_to_state = true, bool save_state = true, bool block_until_done = false);
void requestExit(bool allow_save_to_state = true); void requestExit(bool allow_save_to_state = true);
void checkForSettingChanges(); void checkForSettingChanges();
void getWindowInfo(WindowInfo* wi);
void checkForUpdates(bool display_message); void checkForUpdates(bool display_message);

View file

@ -6,6 +6,7 @@
#include "common/log.h" #include "common/log.h"
#include "common/path.h" #include "common/path.h"
#include "common/string_util.h" #include "common/string_util.h"
#include "common/window_info.h"
#include "core/cheats.h" #include "core/cheats.h"
#include "core/controller.h" #include "core/controller.h"
#include "core/game_database.h" #include "core/game_database.h"
@ -1757,10 +1758,13 @@ void Host::RequestExit(bool save_state_if_running)
QMetaObject::invokeMethod(g_main_window, "requestExit", Qt::QueuedConnection, Q_ARG(bool, save_state_if_running)); QMetaObject::invokeMethod(g_main_window, "requestExit", Qt::QueuedConnection, Q_ARG(bool, save_state_if_running));
} }
void* Host::GetTopLevelWindowHandle() std::optional<WindowInfo> Host::GetTopLevelWindowInfo()
{ {
void* ret = nullptr; // Normally we'd just feed the std::optional all the way through here. But that won't work because of some bug
QMetaObject::invokeMethod(g_main_window, &MainWindow::getNativeWindowId, Qt::BlockingQueuedConnection, &ret); // in Qt 6.1, and we can't upgrade that because of raging/abusive Win7 users... to anyone still using that dead
// OS, this is a passive-aggressive "screw you".
WindowInfo ret;
QMetaObject::invokeMethod(g_main_window, "getWindowInfo", Qt::BlockingQueuedConnection, Q_ARG(WindowInfo*, &ret));
return ret; return ret;
} }

View file

@ -6,6 +6,7 @@
#include <QtCore/QCoreApplication> #include <QtCore/QCoreApplication>
#include <QtCore/QMetaObject> #include <QtCore/QMetaObject>
#include <QtGui/QDesktopServices> #include <QtGui/QDesktopServices>
#include <QtGui/QGuiApplication>
#include <QtGui/QKeyEvent> #include <QtGui/QKeyEvent>
#include <QtWidgets/QComboBox> #include <QtWidgets/QComboBox>
#include <QtWidgets/QDialog> #include <QtWidgets/QDialog>
@ -22,6 +23,14 @@
#include <array> #include <array>
#include <map> #include <map>
#if !defined(_WIN32) && !defined(APPLE)
#include <qpa/qplatformnativeinterface.h>
#endif
#ifdef _WIN32
#include "common/windows_headers.h"
#endif
namespace QtUtils { namespace QtUtils {
QFrame* CreateHorizontalLine(QWidget* parent) QFrame* CreateHorizontalLine(QWidget* parent)
@ -862,4 +871,53 @@ QIcon GetIconForCompatibility(GameDatabase::CompatibilityRating rating)
return QIcon(QStringLiteral(":/icons/star-%1.png").arg(static_cast<u32>(rating))); return QIcon(QStringLiteral(":/icons/star-%1.png").arg(static_cast<u32>(rating)));
} }
qreal GetDevicePixelRatioForWidget(const QWidget* widget)
{
const QScreen* screen_for_ratio = widget->screen();
if (!screen_for_ratio)
screen_for_ratio = QGuiApplication::primaryScreen();
return screen_for_ratio ? screen_for_ratio->devicePixelRatio() : static_cast<qreal>(1);
}
std::optional<WindowInfo> GetWindowInfoForWidget(QWidget* widget)
{
WindowInfo wi;
// Windows and Apple are easy here since there's no display connection.
#if defined(_WIN32)
wi.type = WindowInfo::Type::Win32;
wi.window_handle = reinterpret_cast<void*>(widget->winId());
#elif defined(__APPLE__)
wi.type = WindowInfo::Type::MacOS;
wi.window_handle = reinterpret_cast<void*>(widget->winId());
#else
QPlatformNativeInterface* pni = QGuiApplication::platformNativeInterface();
const QString platform_name = QGuiApplication::platformName();
if (platform_name == QStringLiteral("xcb"))
{
wi.type = WindowInfo::Type::X11;
wi.display_connection = pni->nativeResourceForWindow("display", widget->windowHandle());
wi.window_handle = reinterpret_cast<void*>(widget->winId());
}
else if (platform_name == QStringLiteral("wayland"))
{
wi.type = WindowInfo::Type::Wayland;
wi.display_connection = pni->nativeResourceForWindow("display", widget->windowHandle());
wi.window_handle = pni->nativeResourceForWindow("surface", widget->windowHandle());
}
else
{
qCritical() << "Unknown PNI platform " << platform_name;
return std::nullopt;
}
#endif
const qreal dpr = GetDevicePixelRatioForWidget(widget);
wi.surface_width = static_cast<u32>(static_cast<qreal>(widget->width()) * dpr);
wi.surface_height = static_cast<u32>(static_cast<qreal>(widget->height()) * dpr);
wi.surface_scale = static_cast<float>(dpr);
return wi;
}
} // namespace QtUtils } // namespace QtUtils

View file

@ -1,4 +1,5 @@
#pragma once #pragma once
#include "common/window_info.h"
#include "common/types.h" #include "common/types.h"
#include <QtCore/QByteArray> #include <QtCore/QByteArray>
#include <QtCore/QMetaType> #include <QtCore/QMetaType>
@ -91,4 +92,10 @@ QIcon GetIconForRegion(DiscRegion region);
QIcon GetIconForEntryType(GameList::EntryType type); QIcon GetIconForEntryType(GameList::EntryType type);
QIcon GetIconForCompatibility(GameDatabase::CompatibilityRating rating); QIcon GetIconForCompatibility(GameDatabase::CompatibilityRating rating);
/// Returns the pixel ratio/scaling factor for a widget.
qreal GetDevicePixelRatioForWidget(const QWidget* widget);
/// Returns the common window info structure for a Qt widget.
std::optional<WindowInfo> GetWindowInfoForWidget(QWidget* widget);
} // namespace QtUtils } // namespace QtUtils

View file

@ -243,7 +243,7 @@ void CommonHost::OnSystemStarted()
FullscreenUI::OnSystemStarted(); FullscreenUI::OnSystemStarted();
if (g_settings.inhibit_screensaver) if (g_settings.inhibit_screensaver)
FrontendCommon::SuspendScreensaver(g_host_display->GetWindowInfo()); FrontendCommon::SuspendScreensaver();
} }
void CommonHost::OnSystemPaused() void CommonHost::OnSystemPaused()
@ -261,7 +261,7 @@ void CommonHost::OnSystemResumed()
FullscreenUI::OnSystemResumed(); FullscreenUI::OnSystemResumed();
if (g_settings.inhibit_screensaver) if (g_settings.inhibit_screensaver)
FrontendCommon::SuspendScreensaver(g_host_display->GetWindowInfo()); FrontendCommon::SuspendScreensaver();
} }
void CommonHost::OnSystemDestroyed() 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 != old_settings.inhibit_screensaver)
{ {
if (g_settings.inhibit_screensaver) if (g_settings.inhibit_screensaver)
FrontendCommon::SuspendScreensaver(g_host_display->GetWindowInfo()); FrontendCommon::SuspendScreensaver();
else else
FrontendCommon::ResumeScreensaver(); FrontendCommon::ResumeScreensaver();
} }

View file

@ -49,8 +49,3 @@ std::unique_ptr<AudioStream> CreateXAudio2Stream(u32 sample_rate, u32 channels,
namespace ImGuiManager { namespace ImGuiManager {
void RenderDebugWindows(); 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(). // need to release the lock while we're enumerating, because we call winId().
settings_lock.unlock(); 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(); settings_lock.lock();
m_toplevel_window = static_cast<HWND>(toplevel_wi->window_handle);
ReloadDevices(); ReloadDevices();
return true; return true;
} }

View file

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

View file

@ -1,7 +1,7 @@
#include "common/window_info.h" #include "common/window_info.h"
namespace FrontendCommon { namespace FrontendCommon {
void SuspendScreensaver(const WindowInfo& wi); void SuspendScreensaver();
void ResumeScreensaver(); void ResumeScreensaver();
/// Abstracts platform-specific code for asynchronously playing a sound. /// 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 bool s_screensaver_suspended;
static WindowInfo s_screensaver_suspender;
void FrontendCommon::SuspendScreensaver(const WindowInfo& wi) void FrontendCommon::SuspendScreensaver()
{ {
if (s_screensaver_suspended && if (s_screensaver_suspended)
(s_screensaver_suspender.type != wi.type || s_screensaver_suspender.window_handle != wi.window_handle))
ResumeScreensaver();
if (!SetScreensaverInhibitMacOS(true)) if (!SetScreensaverInhibitMacOS(true))
{ {
@ -46,10 +43,7 @@ void FrontendCommon::SuspendScreensaver(const WindowInfo& wi)
return; return;
} }
Log_InfoPrintf("Screensaver suspended by 0x%" PRIx64 ".",
static_cast<u64>(reinterpret_cast<uintptr_t>(wi.window_handle)));
s_screensaver_suspended = true; s_screensaver_suspended = true;
s_screensaver_suspender = wi;
} }
void FrontendCommon::ResumeScreensaver() void FrontendCommon::ResumeScreensaver()
@ -59,11 +53,8 @@ void FrontendCommon::ResumeScreensaver()
if (!SetScreensaverInhibitMacOS(false)) if (!SetScreensaverInhibitMacOS(false))
Log_ErrorPrint("Failed to resume screensaver."); Log_ErrorPrint("Failed to resume screensaver.");
else
Log_InfoPrint("Screensaver resumed.");
s_screensaver_suspended = false; s_screensaver_suspended = false;
s_screensaver_suspender = {};
} }
bool FrontendCommon::PlaySoundAsync(const char* path) bool FrontendCommon::PlaySoundAsync(const char* path)

View file

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

View file

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