mirror of
https://github.com/AxioDL/boo.git
synced 2025-12-14 07:36:26 +00:00
Inhibit screensaver using dbus or xdg-screensaver
This commit is contained in:
@@ -3,11 +3,87 @@
|
||||
*/
|
||||
|
||||
#define APPLICATION_UNIX_CPP
|
||||
|
||||
#include <dbus/dbus.h>
|
||||
#include <cstdint>
|
||||
#include <unistd.h>
|
||||
#include "logvisor/logvisor.hpp"
|
||||
#include "boo/IApplication.hpp"
|
||||
|
||||
namespace boo {
|
||||
static logvisor::Module Log("boo::ApplicationUnix");
|
||||
IApplication* APP = nullptr;
|
||||
|
||||
class ScreenSaverInhibitor {
|
||||
DBusConnection* m_dbus;
|
||||
uint64_t m_wid;
|
||||
DBusPendingCall* m_pending = nullptr;
|
||||
uint32_t m_cookie = UINT32_MAX;
|
||||
|
||||
static void Callback(DBusPendingCall* pending, ScreenSaverInhibitor* user_data) {
|
||||
user_data->HandleReply();
|
||||
}
|
||||
|
||||
void HandleReply() {
|
||||
DBusMessage* msg = nullptr;
|
||||
DBusError err = DBUS_ERROR_INIT;
|
||||
if ((msg = dbus_pending_call_steal_reply(m_pending)) &&
|
||||
dbus_message_get_args(msg, &err, DBUS_TYPE_UINT32, &m_cookie, DBUS_TYPE_INVALID)) {
|
||||
Log.report(logvisor::Info, "Screen saver inhibited");
|
||||
} else {
|
||||
/* Fallback to xdg-screensaver */
|
||||
dbus_error_free(&err);
|
||||
Log.report(logvisor::Info, "Falling back to xdg-screensaver inhibit");
|
||||
if (!fork()) {
|
||||
char win_id[32];
|
||||
snprintf(win_id, 32, "0x%lX", m_wid);
|
||||
execlp("xdg-screensaver", "xdg-screensaver", "suspend", win_id, nullptr);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
if (msg)
|
||||
dbus_message_unref(msg);
|
||||
dbus_pending_call_unref(m_pending);
|
||||
m_pending = nullptr;
|
||||
}
|
||||
|
||||
public:
|
||||
ScreenSaverInhibitor(DBusConnection* dbus, uint64_t wid) : m_dbus(dbus), m_wid(wid) {
|
||||
#ifndef BOO_MSAN
|
||||
DBusMessage* msg =
|
||||
dbus_message_new_method_call("org.freedesktop.ScreenSaver", "/org/freedesktop/ScreenSaver",
|
||||
"org.freedesktop.ScreenSaver", "Inhibit");
|
||||
const char* appName = APP->getUniqueName().data();
|
||||
const char* reason = "Game Active";
|
||||
dbus_message_append_args(msg, DBUS_TYPE_STRING, &appName, DBUS_TYPE_STRING, &reason, DBUS_TYPE_INVALID);
|
||||
dbus_connection_send_with_reply(m_dbus, msg, &m_pending, -1);
|
||||
dbus_pending_call_set_notify(m_pending, DBusPendingCallNotifyFunction(Callback), this, nullptr);
|
||||
dbus_message_unref(msg);
|
||||
dbus_connection_flush(m_dbus);
|
||||
#endif
|
||||
}
|
||||
|
||||
~ScreenSaverInhibitor() {
|
||||
#ifndef BOO_MSAN
|
||||
if (m_cookie != UINT32_MAX) {
|
||||
DBusMessage* msg =
|
||||
dbus_message_new_method_call("org.freedesktop.ScreenSaver", "/org/freedesktop/ScreenSaver",
|
||||
"org.freedesktop.ScreenSaver", "UnInhibit");
|
||||
dbus_message_append_args(msg, DBUS_TYPE_UINT32, &m_cookie, DBUS_TYPE_INVALID);
|
||||
dbus_connection_send(m_dbus, msg, nullptr);
|
||||
dbus_message_unref(msg);
|
||||
dbus_connection_flush(m_dbus);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#include "ApplicationXlib.hpp"
|
||||
#include "ApplicationWayland.hpp"
|
||||
|
||||
#include <memory>
|
||||
#include <dbus/dbus.h>
|
||||
#include <cstdio>
|
||||
|
||||
/* No icon by default */
|
||||
@@ -46,7 +122,6 @@ DBusConnection* RegisterDBus(const char* appName, bool& isFirst) {
|
||||
|
||||
namespace boo {
|
||||
|
||||
IApplication* APP = nullptr;
|
||||
int ApplicationRun(IApplication::EPlatformType platform, IApplicationCallback& cb, std::string_view uniqueName,
|
||||
std::string_view friendlyName, std::string_view pname, const std::vector<std::string>& args,
|
||||
std::string_view gfxApi, uint32_t samples, uint32_t anisotropy, bool deepColor,
|
||||
|
||||
Reference in New Issue
Block a user