freedesktop_portal_desktop.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*************************************************************************/
  2. /* freedesktop_portal_desktop.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_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 "dbus-so_wrap.h"
  36. #include "core/variant/variant.h"
  37. #define BUS_OBJECT_NAME "org.freedesktop.portal.Desktop"
  38. #define BUS_OBJECT_PATH "/org/freedesktop/portal/desktop"
  39. #define BUS_INTERFACE_SETTINGS "org.freedesktop.portal.Settings"
  40. static bool try_parse_variant(DBusMessage *p_reply_message, int p_type, void *r_value) {
  41. DBusMessageIter iter[3];
  42. dbus_message_iter_init(p_reply_message, &iter[0]);
  43. if (dbus_message_iter_get_arg_type(&iter[0]) != DBUS_TYPE_VARIANT) {
  44. return false;
  45. }
  46. dbus_message_iter_recurse(&iter[0], &iter[1]);
  47. if (dbus_message_iter_get_arg_type(&iter[1]) != DBUS_TYPE_VARIANT) {
  48. return false;
  49. }
  50. dbus_message_iter_recurse(&iter[1], &iter[2]);
  51. if (dbus_message_iter_get_arg_type(&iter[2]) != p_type) {
  52. return false;
  53. }
  54. dbus_message_iter_get_basic(&iter[2], r_value);
  55. return true;
  56. }
  57. bool FreeDesktopPortalDesktop::read_setting(const char *p_namespace, const char *p_key, int p_type, void *r_value) {
  58. if (unsupported) {
  59. return false;
  60. }
  61. DBusError error;
  62. dbus_error_init(&error);
  63. DBusConnection *bus = dbus_bus_get(DBUS_BUS_SESSION, &error);
  64. if (dbus_error_is_set(&error)) {
  65. dbus_error_free(&error);
  66. unsupported = true;
  67. if (OS::get_singleton()->is_stdout_verbose()) {
  68. ERR_PRINT(String() + "Error opening D-Bus connection: " + error.message);
  69. }
  70. return false;
  71. }
  72. DBusMessage *message = dbus_message_new_method_call(
  73. BUS_OBJECT_NAME, BUS_OBJECT_PATH, BUS_INTERFACE_SETTINGS,
  74. "Read");
  75. dbus_message_append_args(
  76. message,
  77. DBUS_TYPE_STRING, &p_namespace,
  78. DBUS_TYPE_STRING, &p_key,
  79. DBUS_TYPE_INVALID);
  80. DBusMessage *reply = dbus_connection_send_with_reply_and_block(bus, message, 50, &error);
  81. dbus_message_unref(message);
  82. if (dbus_error_is_set(&error)) {
  83. dbus_error_free(&error);
  84. dbus_connection_unref(bus);
  85. if (OS::get_singleton()->is_stdout_verbose()) {
  86. ERR_PRINT(String() + "Error on D-Bus communication: " + error.message);
  87. }
  88. return false;
  89. }
  90. bool success = try_parse_variant(reply, p_type, r_value);
  91. dbus_message_unref(reply);
  92. dbus_connection_unref(bus);
  93. return success;
  94. }
  95. uint32_t FreeDesktopPortalDesktop::get_appearance_color_scheme() {
  96. if (unsupported) {
  97. return 0;
  98. }
  99. uint32_t value = 0;
  100. read_setting("org.freedesktop.appearance", "color-scheme", DBUS_TYPE_UINT32, &value);
  101. return value;
  102. }
  103. FreeDesktopPortalDesktop::FreeDesktopPortalDesktop() {
  104. #ifdef DEBUG_ENABLED
  105. int dylibloader_verbose = 1;
  106. #else
  107. int dylibloader_verbose = 0;
  108. #endif
  109. unsupported = (initialize_dbus(dylibloader_verbose) != 0);
  110. }
  111. #endif // DBUS_ENABLED