freedesktop_screensaver.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. CharString app_name_utf8 = app_name_string.utf8();
  50. const char *app_name = app_name_string.is_empty() ? "Godot Engine" : app_name_utf8.get_data();
  51. const char *reason = "Running Godot Engine project";
  52. DBusMessage *message = dbus_message_new_method_call(
  53. BUS_OBJECT_NAME, BUS_OBJECT_PATH, BUS_INTERFACE,
  54. "Inhibit");
  55. dbus_message_append_args(
  56. message,
  57. DBUS_TYPE_STRING, &app_name,
  58. DBUS_TYPE_STRING, &reason,
  59. DBUS_TYPE_INVALID);
  60. DBusMessage *reply = dbus_connection_send_with_reply_and_block(bus, message, 50, &error);
  61. dbus_message_unref(message);
  62. if (dbus_error_is_set(&error)) {
  63. dbus_connection_unref(bus);
  64. unsupported = false;
  65. return;
  66. }
  67. DBusMessageIter reply_iter;
  68. dbus_message_iter_init(reply, &reply_iter);
  69. dbus_message_iter_get_basic(&reply_iter, &cookie);
  70. print_verbose("FreeDesktopScreenSaver: Acquired screensaver inhibition cookie: " + uitos(cookie));
  71. dbus_message_unref(reply);
  72. dbus_connection_unref(bus);
  73. }
  74. void FreeDesktopScreenSaver::uninhibit() {
  75. if (unsupported) {
  76. return;
  77. }
  78. DBusError error;
  79. dbus_error_init(&error);
  80. DBusConnection *bus = dbus_bus_get(DBUS_BUS_SESSION, &error);
  81. if (dbus_error_is_set(&error)) {
  82. unsupported = true;
  83. return;
  84. }
  85. DBusMessage *message = dbus_message_new_method_call(
  86. BUS_OBJECT_NAME, BUS_OBJECT_PATH, BUS_INTERFACE,
  87. "UnInhibit");
  88. dbus_message_append_args(
  89. message,
  90. DBUS_TYPE_UINT32, &cookie,
  91. DBUS_TYPE_INVALID);
  92. DBusMessage *reply = dbus_connection_send_with_reply_and_block(bus, message, 50, &error);
  93. if (dbus_error_is_set(&error)) {
  94. dbus_connection_unref(bus);
  95. unsupported = true;
  96. return;
  97. }
  98. print_verbose("FreeDesktopScreenSaver: Released screensaver inhibition cookie: " + uitos(cookie));
  99. dbus_message_unref(message);
  100. dbus_message_unref(reply);
  101. dbus_connection_unref(bus);
  102. }
  103. #endif // DBUS_ENABLED