freedesktop_screensaver.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*************************************************************************/
  2. /* freedesktop_screensaver.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "freedesktop_screensaver.h"
  31. #ifdef DBUS_ENABLED
  32. #include "core/config/project_settings.h"
  33. #include <dbus/dbus.h>
  34. #define BUS_OBJECT_NAME "org.freedesktop.ScreenSaver"
  35. #define BUS_OBJECT_PATH "/org/freedesktop/ScreenSaver"
  36. #define BUS_INTERFACE "org.freedesktop.ScreenSaver"
  37. void FreeDesktopScreenSaver::inhibit() {
  38. if (unsupported) {
  39. return;
  40. }
  41. DBusError error;
  42. dbus_error_init(&error);
  43. DBusConnection *bus = dbus_bus_get(DBUS_BUS_SESSION, &error);
  44. if (dbus_error_is_set(&error)) {
  45. unsupported = true;
  46. return;
  47. }
  48. String app_name_string = ProjectSettings::get_singleton()->get("application/config/name");
  49. const char *app_name = app_name_string.is_empty() ? "Godot Engine" : app_name_string.utf8().get_data();
  50. const char *reason = "Running Godot Engine project";
  51. DBusMessage *message = dbus_message_new_method_call(
  52. BUS_OBJECT_NAME, BUS_OBJECT_PATH, BUS_INTERFACE,
  53. "Inhibit");
  54. dbus_message_append_args(
  55. message,
  56. DBUS_TYPE_STRING, &app_name,
  57. DBUS_TYPE_STRING, &reason,
  58. DBUS_TYPE_INVALID);
  59. DBusMessage *reply = dbus_connection_send_with_reply_and_block(bus, message, 50, &error);
  60. dbus_message_unref(message);
  61. if (dbus_error_is_set(&error)) {
  62. dbus_connection_unref(bus);
  63. unsupported = false;
  64. return;
  65. }
  66. DBusMessageIter reply_iter;
  67. dbus_message_iter_init(reply, &reply_iter);
  68. dbus_message_iter_get_basic(&reply_iter, &cookie);
  69. print_verbose("FreeDesktopScreenSaver: Acquired screensaver inhibition cookie: " + uitos(cookie));
  70. dbus_message_unref(reply);
  71. dbus_connection_unref(bus);
  72. }
  73. void FreeDesktopScreenSaver::uninhibit() {
  74. if (unsupported) {
  75. return;
  76. }
  77. DBusError error;
  78. dbus_error_init(&error);
  79. DBusConnection *bus = dbus_bus_get(DBUS_BUS_SESSION, &error);
  80. if (dbus_error_is_set(&error)) {
  81. unsupported = true;
  82. return;
  83. }
  84. DBusMessage *message = dbus_message_new_method_call(
  85. BUS_OBJECT_NAME, BUS_OBJECT_PATH, BUS_INTERFACE,
  86. "UnInhibit");
  87. dbus_message_append_args(
  88. message,
  89. DBUS_TYPE_UINT32, &cookie,
  90. DBUS_TYPE_INVALID);
  91. DBusMessage *reply = dbus_connection_send_with_reply_and_block(bus, message, 50, &error);
  92. if (dbus_error_is_set(&error)) {
  93. dbus_connection_unref(bus);
  94. unsupported = true;
  95. return;
  96. }
  97. print_verbose("FreeDesktopScreenSaver: Released screensaver inhibition cookie: " + uitos(cookie));
  98. dbus_message_unref(message);
  99. dbus_message_unref(reply);
  100. dbus_connection_unref(bus);
  101. }
  102. #endif // DBUS_ENABLED