Add option for preventing display sleep (#5783)

Adds support for preventing the display from sleeping while a game is
running. Supports Windows, Linux (with the org.freedesktop.ScreenSaver
D-Bus service), and macOS.
This commit is contained in:
Alex James 2019-10-12 07:40:47 -05:00 committed by Ivan
parent 07022fd3b6
commit 3ad743ecaa
11 changed files with 118 additions and 16 deletions

View file

@ -0,0 +1,70 @@
#include "display_sleep_control.h"
#include "Emu/System.h"
#include "Utilities/Log.h"
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#elif defined(__APPLE__)
#include <IOKit/pwr_mgt/IOPMLib.h>
static IOPMAssertionID s_pm_assertion = kIOPMNullAssertionID;
#elif defined(HAVE_QTDBUS)
#include <QtDBus/QDBusConnection>
#include <QtDBus/QDBusInterface>
#include <QtDBus/QDBusMessage>
#include <QDBusReply>
static u32 s_dbus_cookie = 0;
#endif
bool display_sleep_control_supported()
{
#if defined(_WIN32) || defined(__APPLE__)
return true;
#elif defined(HAVE_QTDBUS)
QDBusInterface interface("org.freedesktop.ScreenSaver", "/ScreenSaver", "org.freedesktop.ScreenSaver", QDBusConnection::sessionBus());
return interface.isValid();
#else
return false;
#endif
}
void enable_display_sleep()
{
#ifdef _WIN32
SetThreadExecutionState(ES_CONTINUOUS);
#elif defined(__APPLE__)
if (s_pm_assertion != kIOPMNullAssertionID)
{
IOPMAssertionRelease(s_pm_assertion);
s_pm_assertion = kIOPMNullAssertionID;
}
#elif defined(HAVE_QTDBUS)
if (s_dbus_cookie != 0)
{
QDBusInterface interface("org.freedesktop.ScreenSaver", "/ScreenSaver", "org.freedesktop.ScreenSaver", QDBusConnection::sessionBus());
interface.call("UnInhibit", s_dbus_cookie);
s_dbus_cookie = 0;
}
#endif
}
void disable_display_sleep()
{
#ifdef _WIN32
SetThreadExecutionState(ES_CONTINUOUS | ES_SYSTEM_REQUIRED | ES_DISPLAY_REQUIRED);
#elif defined(__APPLE__)
IOPMAssertionCreateWithName(kIOPMAssertionTypePreventUserIdleDisplaySleep, kIOPMAssertionLevelOn, CFSTR("Game running"), &s_pm_assertion);
#elif defined(HAVE_QTDBUS)
QDBusInterface interface("org.freedesktop.ScreenSaver", "/ScreenSaver", "org.freedesktop.ScreenSaver", QDBusConnection::sessionBus());
QDBusReply<u32> reply = interface.call("Inhibit", "rpcs3", "Game running");
if (reply.isValid())
{
s_dbus_cookie = reply.value();
}
#endif
}