2020-12-05 13:08:24 +01:00
|
|
|
#include "display_sleep_control.h"
|
2019-10-12 14:40:47 +02:00
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
#include <windows.h>
|
|
|
|
|
|
|
|
|
|
#elif defined(__APPLE__)
|
2024-02-24 16:30:38 +01:00
|
|
|
#pragma GCC diagnostic push
|
|
|
|
|
#pragma GCC diagnostic ignored "-Wold-style-cast"
|
2019-10-12 14:40:47 +02:00
|
|
|
#include <IOKit/pwr_mgt/IOPMLib.h>
|
2024-02-24 16:30:38 +01:00
|
|
|
#pragma GCC diagnostic pop
|
2019-10-12 14:40:47 +02:00
|
|
|
|
|
|
|
|
static IOPMAssertionID s_pm_assertion = kIOPMNullAssertionID;
|
|
|
|
|
|
|
|
|
|
#elif defined(HAVE_QTDBUS)
|
|
|
|
|
#include <QtDBus/QDBusConnection>
|
|
|
|
|
#include <QtDBus/QDBusInterface>
|
|
|
|
|
#include <QtDBus/QDBusMessage>
|
|
|
|
|
#include <QDBusReply>
|
2020-12-12 13:01:29 +01:00
|
|
|
#include "util/types.hpp"
|
2019-10-12 14:40:47 +02:00
|
|
|
static u32 s_dbus_cookie = 0;
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
bool display_sleep_control_supported()
|
|
|
|
|
{
|
|
|
|
|
#if defined(_WIN32) || defined(__APPLE__)
|
|
|
|
|
return true;
|
|
|
|
|
#elif defined(HAVE_QTDBUS)
|
2025-04-05 21:50:45 +02:00
|
|
|
for (const char* service : {"org.freedesktop.ScreenSaver", "org.mate.ScreenSaver"})
|
2021-10-20 01:01:45 +02:00
|
|
|
{
|
|
|
|
|
QDBusInterface interface(service, "/ScreenSaver", service, QDBusConnection::sessionBus());
|
|
|
|
|
if (interface.isValid())
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
2019-10-12 14:40:47 +02:00
|
|
|
#else
|
|
|
|
|
return false;
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-24 21:41:14 +01:00
|
|
|
void enable_display_sleep(bool enabled)
|
2019-10-12 14:40:47 +02:00
|
|
|
{
|
2020-07-03 17:12:58 +02:00
|
|
|
if (!display_sleep_control_supported())
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-12 14:40:47 +02:00
|
|
|
#ifdef _WIN32
|
2025-02-24 21:41:14 +01:00
|
|
|
SetThreadExecutionState(enabled ? ES_CONTINUOUS : (ES_CONTINUOUS | ES_SYSTEM_REQUIRED | ES_DISPLAY_REQUIRED));
|
2019-10-12 14:40:47 +02:00
|
|
|
#elif defined(__APPLE__)
|
2025-02-24 21:41:14 +01:00
|
|
|
if (enabled && s_pm_assertion != kIOPMNullAssertionID)
|
2019-10-12 14:40:47 +02:00
|
|
|
{
|
|
|
|
|
IOPMAssertionRelease(s_pm_assertion);
|
|
|
|
|
s_pm_assertion = kIOPMNullAssertionID;
|
|
|
|
|
}
|
2025-02-24 21:41:14 +01:00
|
|
|
else if (!enabled)
|
|
|
|
|
{
|
|
|
|
|
#pragma GCC diagnostic push
|
|
|
|
|
// Necessary as some of those values are macro using old casts
|
|
|
|
|
#pragma GCC diagnostic ignored "-Wold-style-cast"
|
|
|
|
|
IOPMAssertionCreateWithName(kIOPMAssertionTypePreventUserIdleDisplaySleep, kIOPMAssertionLevelOn, CFSTR("Game running"), &s_pm_assertion);
|
|
|
|
|
#pragma GCC diagnostic pop
|
|
|
|
|
}
|
2019-10-12 14:40:47 +02:00
|
|
|
#elif defined(HAVE_QTDBUS)
|
2025-02-24 21:41:14 +01:00
|
|
|
if (enabled && s_dbus_cookie != 0)
|
2019-10-12 14:40:47 +02:00
|
|
|
{
|
2025-04-05 21:50:45 +02:00
|
|
|
for (const char* service : {"org.freedesktop.ScreenSaver", "org.mate.ScreenSaver"})
|
2021-10-20 01:01:45 +02:00
|
|
|
{
|
|
|
|
|
QDBusInterface interface(service, "/ScreenSaver", service, QDBusConnection::sessionBus());
|
|
|
|
|
if (interface.isValid())
|
|
|
|
|
{
|
|
|
|
|
interface.call("UnInhibit", s_dbus_cookie);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-10-12 14:40:47 +02:00
|
|
|
s_dbus_cookie = 0;
|
|
|
|
|
}
|
2025-02-24 21:41:14 +01:00
|
|
|
else if (!enabled)
|
2020-07-03 17:12:58 +02:00
|
|
|
{
|
2025-04-05 21:50:45 +02:00
|
|
|
for (const char* service : {"org.freedesktop.ScreenSaver", "org.mate.ScreenSaver"})
|
2021-10-20 01:01:45 +02:00
|
|
|
{
|
2025-02-24 21:41:14 +01:00
|
|
|
QDBusInterface interface(service, "/ScreenSaver", service, QDBusConnection::sessionBus());
|
|
|
|
|
if (interface.isValid())
|
2021-10-20 01:01:45 +02:00
|
|
|
{
|
2025-02-24 21:41:14 +01:00
|
|
|
QDBusReply<u32> reply = interface.call("Inhibit", "rpcs3", "Game running");
|
|
|
|
|
if (reply.isValid())
|
|
|
|
|
{
|
|
|
|
|
s_dbus_cookie = reply.value();
|
|
|
|
|
}
|
|
|
|
|
break;
|
2021-10-20 01:01:45 +02:00
|
|
|
}
|
|
|
|
|
}
|
2019-10-12 14:40:47 +02:00
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
}
|