freedesktop_portal_desktop.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /**************************************************************************/
  2. /* freedesktop_portal_desktop.h */
  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. #pragma once
  31. #ifdef DBUS_ENABLED
  32. #include "core/os/thread.h"
  33. #include "core/os/thread_safe.h"
  34. #include "servers/display/display_server.h"
  35. struct DBusMessage;
  36. struct DBusConnection;
  37. struct DBusMessageIter;
  38. class FreeDesktopPortalDesktop : public Object {
  39. GDSOFTCLASS(FreeDesktopPortalDesktop, Object);
  40. private:
  41. bool unsupported = false;
  42. enum ReadVariantType {
  43. VAR_TYPE_UINT32, // u
  44. VAR_TYPE_BOOL, // b
  45. VAR_TYPE_COLOR, // (ddd)
  46. };
  47. static bool try_parse_variant(DBusMessage *p_reply_message, ReadVariantType p_type, void *r_value);
  48. // Read a setting from org.freekdesktop.portal.Settings
  49. bool read_setting(const char *p_namespace, const char *p_key, ReadVariantType p_type, void *r_value);
  50. static void append_dbus_string(DBusMessageIter *p_iter, const String &p_string);
  51. static void append_dbus_dict_options(DBusMessageIter *p_iter, const TypedArray<Dictionary> &p_options, HashMap<String, String> &r_ids);
  52. static void append_dbus_dict_filters(DBusMessageIter *p_iter, const Vector<String> &p_filter_names, const Vector<String> &p_filter_exts, const Vector<String> &p_filter_mimes);
  53. static void append_dbus_dict_string(DBusMessageIter *p_iter, const String &p_key, const String &p_value, bool p_as_byte_array = false);
  54. static void append_dbus_dict_bool(DBusMessageIter *p_iter, const String &p_key, bool p_value);
  55. static bool file_chooser_parse_response(DBusMessageIter *p_iter, const Vector<String> &p_names, const HashMap<String, String> &p_ids, bool &r_cancel, Vector<String> &r_urls, int &r_index, Dictionary &r_options);
  56. static bool color_picker_parse_response(DBusMessageIter *p_iter, bool &r_cancel, Color &r_color);
  57. static Error make_request_token(String &r_token);
  58. bool send_request(DBusMessage *p_message, const String &r_token, String &r_response_path, String &r_response_filter);
  59. struct ColorPickerData {
  60. Callable callback;
  61. String filter;
  62. String path;
  63. };
  64. struct ColorPickerCallback {
  65. Callable callback;
  66. Variant status;
  67. Variant color;
  68. };
  69. List<ColorPickerCallback> pending_color_cbs;
  70. Mutex color_picker_mutex;
  71. Vector<ColorPickerData> color_pickers;
  72. struct FileDialogData {
  73. Vector<String> filter_names;
  74. HashMap<String, String> option_ids;
  75. DisplayServer::WindowID prev_focus = DisplayServer::INVALID_WINDOW_ID;
  76. Callable callback;
  77. String filter;
  78. String path;
  79. bool opt_in_cb = false;
  80. };
  81. struct FileDialogCallback {
  82. Callable callback;
  83. Variant status;
  84. Variant files;
  85. Variant index;
  86. Variant options;
  87. bool opt_in_cb = false;
  88. };
  89. List<FileDialogCallback> pending_file_cbs;
  90. Mutex file_dialog_mutex;
  91. Vector<FileDialogData> file_dialogs;
  92. Thread monitor_thread;
  93. SafeFlag monitor_thread_abort;
  94. DBusConnection *monitor_connection = nullptr;
  95. String theme_path;
  96. Callable system_theme_changed;
  97. void _system_theme_changed_callback();
  98. bool _is_interface_supported(const char *p_iface, uint32_t p_minimum_version);
  99. Mutex inhibit_mutex;
  100. String inhibit_path;
  101. String inhibit_filter;
  102. static void _thread_monitor(void *p_ud);
  103. public:
  104. FreeDesktopPortalDesktop();
  105. ~FreeDesktopPortalDesktop();
  106. bool is_supported() { return !unsupported; }
  107. bool is_file_chooser_supported();
  108. bool is_settings_supported();
  109. bool is_screenshot_supported();
  110. bool is_inhibit_supported();
  111. // org.freedesktop.portal.FileChooser methods.
  112. Error file_dialog_show(DisplayServer::WindowID p_window_id, const String &p_xid, const String &p_title, const String &p_current_directory, const String &p_root, const String &p_filename, DisplayServer::FileDialogMode p_mode, const Vector<String> &p_filters, const TypedArray<Dictionary> &p_options, const Callable &p_callback, bool p_options_in_cb);
  113. void process_callbacks();
  114. // org.freedesktop.portal.Settings methods.
  115. // Retrieve the system's preferred color scheme.
  116. // 0: No preference or unknown.
  117. // 1: Prefer dark appearance.
  118. // 2: Prefer light appearance.
  119. uint32_t get_appearance_color_scheme();
  120. Color get_appearance_accent_color();
  121. void set_system_theme_change_callback(const Callable &p_system_theme_changed) {
  122. system_theme_changed = p_system_theme_changed;
  123. }
  124. // Retrieve high-contrast setting.
  125. // -1: Unknown.
  126. // 0: Disabled.
  127. // 1: Enabled.
  128. uint32_t get_high_contrast();
  129. // org.freedesktop.portal.Screenshot methods.
  130. bool color_picker(const String &p_xid, const Callable &p_callback);
  131. // org.freedesktop.portal.Inhibit methods.
  132. bool inhibit(const String &p_xid);
  133. void uninhibit();
  134. };
  135. #endif // DBUS_ENABLED