freedesktop_screensaver.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*************************************************************************/
  2. /* freedesktop_screensaver.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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-so_wrap.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. dbus_error_free(&error);
  46. unsupported = true;
  47. return;
  48. }
  49. String app_name_string = GLOBAL_GET("application/config/name");
  50. CharString app_name_utf8 = app_name_string.utf8();
  51. const char *app_name = app_name_string.is_empty() ? "Godot Engine" : app_name_utf8.get_data();
  52. const char *reason = "Running Godot Engine project";
  53. DBusMessage *message = dbus_message_new_method_call(
  54. BUS_OBJECT_NAME, BUS_OBJECT_PATH, BUS_INTERFACE,
  55. "Inhibit");
  56. dbus_message_append_args(
  57. message,
  58. DBUS_TYPE_STRING, &app_name,
  59. DBUS_TYPE_STRING, &reason,
  60. DBUS_TYPE_INVALID);
  61. DBusMessage *reply = dbus_connection_send_with_reply_and_block(bus, message, 50, &error);
  62. dbus_message_unref(message);
  63. if (dbus_error_is_set(&error)) {
  64. dbus_error_free(&error);
  65. dbus_connection_unref(bus);
  66. unsupported = false;
  67. return;
  68. }
  69. DBusMessageIter reply_iter;
  70. dbus_message_iter_init(reply, &reply_iter);
  71. dbus_message_iter_get_basic(&reply_iter, &cookie);
  72. print_verbose("FreeDesktopScreenSaver: Acquired screensaver inhibition cookie: " + uitos(cookie));
  73. dbus_message_unref(reply);
  74. dbus_connection_unref(bus);
  75. }
  76. void FreeDesktopScreenSaver::uninhibit() {
  77. if (unsupported) {
  78. return;
  79. }
  80. DBusError error;
  81. dbus_error_init(&error);
  82. DBusConnection *bus = dbus_bus_get(DBUS_BUS_SESSION, &error);
  83. if (dbus_error_is_set(&error)) {
  84. dbus_error_free(&error);
  85. unsupported = true;
  86. return;
  87. }
  88. DBusMessage *message = dbus_message_new_method_call(
  89. BUS_OBJECT_NAME, BUS_OBJECT_PATH, BUS_INTERFACE,
  90. "UnInhibit");
  91. dbus_message_append_args(
  92. message,
  93. DBUS_TYPE_UINT32, &cookie,
  94. DBUS_TYPE_INVALID);
  95. DBusMessage *reply = dbus_connection_send_with_reply_and_block(bus, message, 50, &error);
  96. if (dbus_error_is_set(&error)) {
  97. dbus_error_free(&error);
  98. dbus_connection_unref(bus);
  99. unsupported = true;
  100. return;
  101. }
  102. print_verbose("FreeDesktopScreenSaver: Released screensaver inhibition cookie: " + uitos(cookie));
  103. dbus_message_unref(message);
  104. dbus_message_unref(reply);
  105. dbus_connection_unref(bus);
  106. }
  107. FreeDesktopScreenSaver::FreeDesktopScreenSaver() {
  108. #ifdef DEBUG_ENABLED
  109. int dylibloader_verbose = 1;
  110. #else
  111. int dylibloader_verbose = 0;
  112. #endif
  113. unsupported = (initialize_dbus(dylibloader_verbose) != 0);
  114. }
  115. #endif // DBUS_ENABLED