freedesktop_portal_desktop.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /**************************************************************************/
  2. /* freedesktop_portal_desktop.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_portal_desktop.h"
  31. #ifdef DBUS_ENABLED
  32. #include "core/error/error_macros.h"
  33. #include "core/os/os.h"
  34. #include "core/string/ustring.h"
  35. #include "core/variant/variant.h"
  36. #ifdef SOWRAP_ENABLED
  37. #include "dbus-so_wrap.h"
  38. #else
  39. #include <dbus/dbus.h>
  40. #endif
  41. #define BUS_OBJECT_NAME "org.freedesktop.portal.Desktop"
  42. #define BUS_OBJECT_PATH "/org/freedesktop/portal/desktop"
  43. #define BUS_INTERFACE_SETTINGS "org.freedesktop.portal.Settings"
  44. static bool try_parse_variant(DBusMessage *p_reply_message, int p_type, void *r_value) {
  45. DBusMessageIter iter[3];
  46. dbus_message_iter_init(p_reply_message, &iter[0]);
  47. if (dbus_message_iter_get_arg_type(&iter[0]) != DBUS_TYPE_VARIANT) {
  48. return false;
  49. }
  50. dbus_message_iter_recurse(&iter[0], &iter[1]);
  51. if (dbus_message_iter_get_arg_type(&iter[1]) != DBUS_TYPE_VARIANT) {
  52. return false;
  53. }
  54. dbus_message_iter_recurse(&iter[1], &iter[2]);
  55. if (dbus_message_iter_get_arg_type(&iter[2]) != p_type) {
  56. return false;
  57. }
  58. dbus_message_iter_get_basic(&iter[2], r_value);
  59. return true;
  60. }
  61. bool FreeDesktopPortalDesktop::read_setting(const char *p_namespace, const char *p_key, int p_type, void *r_value) {
  62. if (unsupported) {
  63. return false;
  64. }
  65. DBusError error;
  66. dbus_error_init(&error);
  67. DBusConnection *bus = dbus_bus_get(DBUS_BUS_SESSION, &error);
  68. if (dbus_error_is_set(&error)) {
  69. dbus_error_free(&error);
  70. unsupported = true;
  71. if (OS::get_singleton()->is_stdout_verbose()) {
  72. ERR_PRINT(String() + "Error opening D-Bus connection: " + error.message);
  73. }
  74. return false;
  75. }
  76. DBusMessage *message = dbus_message_new_method_call(
  77. BUS_OBJECT_NAME, BUS_OBJECT_PATH, BUS_INTERFACE_SETTINGS,
  78. "Read");
  79. dbus_message_append_args(
  80. message,
  81. DBUS_TYPE_STRING, &p_namespace,
  82. DBUS_TYPE_STRING, &p_key,
  83. DBUS_TYPE_INVALID);
  84. DBusMessage *reply = dbus_connection_send_with_reply_and_block(bus, message, 50, &error);
  85. dbus_message_unref(message);
  86. if (dbus_error_is_set(&error)) {
  87. dbus_error_free(&error);
  88. dbus_connection_unref(bus);
  89. if (OS::get_singleton()->is_stdout_verbose()) {
  90. ERR_PRINT(String() + "Error on D-Bus communication: " + error.message);
  91. }
  92. return false;
  93. }
  94. bool success = try_parse_variant(reply, p_type, r_value);
  95. dbus_message_unref(reply);
  96. dbus_connection_unref(bus);
  97. return success;
  98. }
  99. uint32_t FreeDesktopPortalDesktop::get_appearance_color_scheme() {
  100. if (unsupported) {
  101. return 0;
  102. }
  103. uint32_t value = 0;
  104. read_setting("org.freedesktop.appearance", "color-scheme", DBUS_TYPE_UINT32, &value);
  105. return value;
  106. }
  107. FreeDesktopPortalDesktop::FreeDesktopPortalDesktop() {
  108. #ifdef SOWRAP_ENABLED
  109. #ifdef DEBUG_ENABLED
  110. int dylibloader_verbose = 1;
  111. #else
  112. int dylibloader_verbose = 0;
  113. #endif
  114. unsupported = (initialize_dbus(dylibloader_verbose) != 0);
  115. #else
  116. unsupported = false;
  117. #endif
  118. if (unsupported) {
  119. return;
  120. }
  121. bool ver_ok = false;
  122. int version_major = 0;
  123. int version_minor = 0;
  124. int version_rev = 0;
  125. dbus_get_version(&version_major, &version_minor, &version_rev);
  126. ver_ok = (version_major == 1 && version_minor >= 10) || (version_major > 1); // 1.10.0
  127. print_verbose(vformat("PortalDesktop: DBus %d.%d.%d detected.", version_major, version_minor, version_rev));
  128. if (!ver_ok) {
  129. print_verbose("PortalDesktop: Unsupported DBus library version!");
  130. unsupported = true;
  131. }
  132. }
  133. #endif // DBUS_ENABLED