freedesktop_screensaver.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /**************************************************************************/
  2. /* freedesktop_screensaver.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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. #ifdef SOWRAP_ENABLED
  34. #include "dbus-so_wrap.h"
  35. #else
  36. #include <dbus/dbus.h>
  37. #endif
  38. #define BUS_OBJECT_NAME "org.freedesktop.ScreenSaver"
  39. #define BUS_OBJECT_PATH "/org/freedesktop/ScreenSaver"
  40. #define BUS_INTERFACE "org.freedesktop.ScreenSaver"
  41. void FreeDesktopScreenSaver::inhibit() {
  42. if (unsupported) {
  43. return;
  44. }
  45. DBusError error;
  46. dbus_error_init(&error);
  47. DBusConnection *bus = dbus_bus_get(DBUS_BUS_SESSION, &error);
  48. if (dbus_error_is_set(&error)) {
  49. dbus_error_free(&error);
  50. unsupported = true;
  51. return;
  52. }
  53. String app_name_string = GLOBAL_GET("application/config/name");
  54. CharString app_name_utf8 = app_name_string.utf8();
  55. const char *app_name = app_name_string.is_empty() ? "Godot Engine" : app_name_utf8.get_data();
  56. const char *reason = "Running Godot Engine project";
  57. DBusMessage *message = dbus_message_new_method_call(
  58. BUS_OBJECT_NAME, BUS_OBJECT_PATH, BUS_INTERFACE,
  59. "Inhibit");
  60. dbus_message_append_args(
  61. message,
  62. DBUS_TYPE_STRING, &app_name,
  63. DBUS_TYPE_STRING, &reason,
  64. DBUS_TYPE_INVALID);
  65. DBusMessage *reply = dbus_connection_send_with_reply_and_block(bus, message, 50, &error);
  66. dbus_message_unref(message);
  67. if (dbus_error_is_set(&error)) {
  68. dbus_error_free(&error);
  69. dbus_connection_unref(bus);
  70. unsupported = false;
  71. return;
  72. }
  73. DBusMessageIter reply_iter;
  74. dbus_message_iter_init(reply, &reply_iter);
  75. dbus_message_iter_get_basic(&reply_iter, &cookie);
  76. print_verbose("FreeDesktopScreenSaver: Acquired screensaver inhibition cookie: " + uitos(cookie));
  77. dbus_message_unref(reply);
  78. dbus_connection_unref(bus);
  79. }
  80. void FreeDesktopScreenSaver::uninhibit() {
  81. if (unsupported) {
  82. return;
  83. }
  84. DBusError error;
  85. dbus_error_init(&error);
  86. DBusConnection *bus = dbus_bus_get(DBUS_BUS_SESSION, &error);
  87. if (dbus_error_is_set(&error)) {
  88. dbus_error_free(&error);
  89. unsupported = true;
  90. return;
  91. }
  92. DBusMessage *message = dbus_message_new_method_call(
  93. BUS_OBJECT_NAME, BUS_OBJECT_PATH, BUS_INTERFACE,
  94. "UnInhibit");
  95. dbus_message_append_args(
  96. message,
  97. DBUS_TYPE_UINT32, &cookie,
  98. DBUS_TYPE_INVALID);
  99. DBusMessage *reply = dbus_connection_send_with_reply_and_block(bus, message, 50, &error);
  100. if (dbus_error_is_set(&error)) {
  101. dbus_error_free(&error);
  102. dbus_connection_unref(bus);
  103. unsupported = true;
  104. return;
  105. }
  106. print_verbose("FreeDesktopScreenSaver: Released screensaver inhibition cookie: " + uitos(cookie));
  107. dbus_message_unref(message);
  108. dbus_message_unref(reply);
  109. dbus_connection_unref(bus);
  110. }
  111. FreeDesktopScreenSaver::FreeDesktopScreenSaver() {
  112. #ifdef SOWRAP_ENABLED
  113. #ifdef DEBUG_ENABLED
  114. int dylibloader_verbose = 1;
  115. #else
  116. int dylibloader_verbose = 0;
  117. #endif
  118. unsupported = (initialize_dbus(dylibloader_verbose) != 0);
  119. #else
  120. unsupported = false;
  121. #endif
  122. if (unsupported) {
  123. return;
  124. }
  125. bool ver_ok = false;
  126. int version_major = 0;
  127. int version_minor = 0;
  128. int version_rev = 0;
  129. dbus_get_version(&version_major, &version_minor, &version_rev);
  130. ver_ok = (version_major == 1 && version_minor >= 10) || (version_major > 1); // 1.10.0
  131. print_verbose(vformat("ScreenSaver: DBus %d.%d.%d detected.", version_major, version_minor, version_rev));
  132. if (!ver_ok) {
  133. print_verbose("ScreenSaver:: Unsupported DBus library version!");
  134. unsupported = true;
  135. }
  136. }
  137. #endif // DBUS_ENABLED