Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/platform/linuxbsd/freedesktop_portal_desktop.h
11352 views
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
31
#pragma once
32
33
#ifdef DBUS_ENABLED
34
35
#include "core/os/thread.h"
36
#include "core/os/thread_safe.h"
37
#include "servers/display/display_server.h"
38
39
struct DBusMessage;
40
struct DBusConnection;
41
struct DBusMessageIter;
42
43
class FreeDesktopPortalDesktop : public Object {
44
GDSOFTCLASS(FreeDesktopPortalDesktop, Object);
45
46
private:
47
bool unsupported = false;
48
49
enum ReadVariantType {
50
VAR_TYPE_UINT32, // u
51
VAR_TYPE_BOOL, // b
52
VAR_TYPE_COLOR, // (ddd)
53
};
54
55
static bool try_parse_variant(DBusMessage *p_reply_message, ReadVariantType p_type, void *r_value);
56
// Read a setting from org.freekdesktop.portal.Settings
57
bool read_setting(const char *p_namespace, const char *p_key, ReadVariantType p_type, void *r_value);
58
59
static void append_dbus_string(DBusMessageIter *p_iter, const String &p_string);
60
static void append_dbus_dict_options(DBusMessageIter *p_iter, const TypedArray<Dictionary> &p_options, HashMap<String, String> &r_ids);
61
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);
62
static void append_dbus_dict_string(DBusMessageIter *p_iter, const String &p_key, const String &p_value, bool p_as_byte_array = false);
63
static void append_dbus_dict_bool(DBusMessageIter *p_iter, const String &p_key, bool p_value);
64
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);
65
static bool color_picker_parse_response(DBusMessageIter *p_iter, bool &r_cancel, Color &r_color);
66
67
struct ColorPickerData {
68
Callable callback;
69
String filter;
70
String path;
71
};
72
73
struct ColorPickerCallback {
74
Callable callback;
75
Variant status;
76
Variant color;
77
};
78
List<ColorPickerCallback> pending_color_cbs;
79
80
Mutex color_picker_mutex;
81
Vector<ColorPickerData> color_pickers;
82
83
struct FileDialogData {
84
Vector<String> filter_names;
85
HashMap<String, String> option_ids;
86
DisplayServer::WindowID prev_focus = DisplayServer::INVALID_WINDOW_ID;
87
Callable callback;
88
String filter;
89
String path;
90
bool opt_in_cb = false;
91
};
92
93
struct FileDialogCallback {
94
Callable callback;
95
Variant status;
96
Variant files;
97
Variant index;
98
Variant options;
99
bool opt_in_cb = false;
100
};
101
List<FileDialogCallback> pending_file_cbs;
102
103
Mutex file_dialog_mutex;
104
Vector<FileDialogData> file_dialogs;
105
Thread monitor_thread;
106
SafeFlag monitor_thread_abort;
107
DBusConnection *monitor_connection = nullptr;
108
109
String theme_path;
110
Callable system_theme_changed;
111
void _system_theme_changed_callback();
112
bool _is_interface_supported(const char *p_iface);
113
114
static void _thread_monitor(void *p_ud);
115
116
public:
117
FreeDesktopPortalDesktop();
118
~FreeDesktopPortalDesktop();
119
120
bool is_supported() { return !unsupported; }
121
bool is_file_chooser_supported();
122
bool is_settings_supported();
123
bool is_screenshot_supported();
124
125
// org.freedesktop.portal.FileChooser methods.
126
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);
127
void process_callbacks();
128
129
// org.freedesktop.portal.Settings methods.
130
131
// Retrieve the system's preferred color scheme.
132
// 0: No preference or unknown.
133
// 1: Prefer dark appearance.
134
// 2: Prefer light appearance.
135
uint32_t get_appearance_color_scheme();
136
Color get_appearance_accent_color();
137
void set_system_theme_change_callback(const Callable &p_system_theme_changed) {
138
system_theme_changed = p_system_theme_changed;
139
}
140
141
// Retrieve high-contrast setting.
142
// -1: Unknown.
143
// 0: Disabled.
144
// 1: Enabled.
145
uint32_t get_high_contrast();
146
147
// org.freedesktop.portal.Screenshot methods.
148
bool color_picker(const String &p_xid, const Callable &p_callback);
149
};
150
151
#endif // DBUS_ENABLED
152
153