freedesktop_portal_desktop.h 4.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. #ifndef FREEDESKTOP_PORTAL_DESKTOP_H
  31. #define FREEDESKTOP_PORTAL_DESKTOP_H
  32. #ifdef DBUS_ENABLED
  33. #include "core/os/thread.h"
  34. #include "servers/display_server.h"
  35. struct DBusMessage;
  36. struct DBusConnection;
  37. struct DBusMessageIter;
  38. class FreeDesktopPortalDesktop : public Object {
  39. private:
  40. bool unsupported = false;
  41. static bool try_parse_variant(DBusMessage *p_reply_message, int p_type, void *r_value);
  42. // Read a setting from org.freekdesktop.portal.Settings
  43. bool read_setting(const char *p_namespace, const char *p_key, int p_type, void *r_value);
  44. static void append_dbus_string(DBusMessageIter *p_iter, const String &p_string);
  45. static void append_dbus_dict_filters(DBusMessageIter *p_iter, const Vector<String> &p_filter_names, const Vector<String> &p_filter_exts);
  46. static void append_dbus_dict_string(DBusMessageIter *p_iter, const String &p_key, const String &p_value, bool p_as_byte_array = false);
  47. static void append_dbus_dict_bool(DBusMessageIter *p_iter, const String &p_key, bool p_value);
  48. static bool file_chooser_parse_response(DBusMessageIter *p_iter, const Vector<String> &p_names, bool &r_cancel, Vector<String> &r_urls, int &r_index);
  49. void _file_dialog_callback(const Callable &p_callable, const Variant &p_status, const Variant &p_list, const Variant &p_index);
  50. struct FileDialogData {
  51. Vector<String> filter_names;
  52. DBusConnection *connection = nullptr;
  53. DisplayServer::WindowID prev_focus = DisplayServer::INVALID_WINDOW_ID;
  54. Callable callback;
  55. String path;
  56. };
  57. Mutex file_dialog_mutex;
  58. Vector<FileDialogData> file_dialogs;
  59. Thread file_dialog_thread;
  60. SafeFlag file_dialog_thread_abort;
  61. static void _thread_file_dialog_monitor(void *p_ud);
  62. public:
  63. FreeDesktopPortalDesktop();
  64. ~FreeDesktopPortalDesktop();
  65. bool is_supported() { return !unsupported; }
  66. 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_filename, DisplayServer::FileDialogMode p_mode, const Vector<String> &p_filters, const Callable &p_callback);
  67. // Retrieve the system's preferred color scheme.
  68. // 0: No preference or unknown.
  69. // 1: Prefer dark appearance.
  70. // 2: Prefer light appearance.
  71. uint32_t get_appearance_color_scheme();
  72. };
  73. #endif // DBUS_ENABLED
  74. #endif // FREEDESKTOP_PORTAL_DESKTOP_H