Path: blob/master/platform/linuxbsd/freedesktop_screensaver.cpp
10277 views
/**************************************************************************/1/* freedesktop_screensaver.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "freedesktop_screensaver.h"3132#ifdef DBUS_ENABLED3334#include "core/config/project_settings.h"3536#ifdef SOWRAP_ENABLED37#include "dbus-so_wrap.h"38#else39#include <dbus/dbus.h>40#endif4142#define BUS_OBJECT_NAME "org.freedesktop.ScreenSaver"43#define BUS_OBJECT_PATH "/org/freedesktop/ScreenSaver"44#define BUS_INTERFACE "org.freedesktop.ScreenSaver"4546void FreeDesktopScreenSaver::inhibit() {47if (unsupported) {48return;49}5051DBusError error;52dbus_error_init(&error);5354DBusConnection *bus = dbus_bus_get(DBUS_BUS_SESSION, &error);55if (dbus_error_is_set(&error)) {56dbus_error_free(&error);57unsupported = true;58return;59}6061String app_name_string = GLOBAL_GET("application/config/name");62CharString app_name_utf8 = app_name_string.utf8();63const char *app_name = app_name_string.is_empty() ? "Godot Engine" : app_name_utf8.get_data();6465const char *reason = "Running Godot Engine project";6667DBusMessage *message = dbus_message_new_method_call(68BUS_OBJECT_NAME, BUS_OBJECT_PATH, BUS_INTERFACE,69"Inhibit");70dbus_message_append_args(71message,72DBUS_TYPE_STRING, &app_name,73DBUS_TYPE_STRING, &reason,74DBUS_TYPE_INVALID);7576DBusMessage *reply = dbus_connection_send_with_reply_and_block(bus, message, 50, &error);77dbus_message_unref(message);78if (dbus_error_is_set(&error)) {79dbus_error_free(&error);80dbus_connection_unref(bus);81unsupported = true;82return;83}8485DBusMessageIter reply_iter;86dbus_message_iter_init(reply, &reply_iter);87dbus_message_iter_get_basic(&reply_iter, &cookie);88print_verbose("FreeDesktopScreenSaver: Acquired screensaver inhibition cookie: " + uitos(cookie));8990dbus_message_unref(reply);91dbus_connection_unref(bus);92}9394void FreeDesktopScreenSaver::uninhibit() {95if (unsupported) {96return;97}9899DBusError error;100dbus_error_init(&error);101102DBusConnection *bus = dbus_bus_get(DBUS_BUS_SESSION, &error);103if (dbus_error_is_set(&error)) {104dbus_error_free(&error);105unsupported = true;106return;107}108109DBusMessage *message = dbus_message_new_method_call(110BUS_OBJECT_NAME, BUS_OBJECT_PATH, BUS_INTERFACE,111"UnInhibit");112dbus_message_append_args(113message,114DBUS_TYPE_UINT32, &cookie,115DBUS_TYPE_INVALID);116117DBusMessage *reply = dbus_connection_send_with_reply_and_block(bus, message, 50, &error);118dbus_message_unref(message);119if (dbus_error_is_set(&error)) {120dbus_error_free(&error);121dbus_connection_unref(bus);122unsupported = true;123return;124}125126print_verbose("FreeDesktopScreenSaver: Released screensaver inhibition cookie: " + uitos(cookie));127128dbus_message_unref(reply);129dbus_connection_unref(bus);130}131132FreeDesktopScreenSaver::FreeDesktopScreenSaver() {133}134135#endif // DBUS_ENABLED136137138