Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/platform/linuxbsd/freedesktop_portal_desktop.h
21760 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
65
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);
66
static bool color_picker_parse_response(DBusMessageIter *p_iter, bool &r_cancel, Color &r_color);
67
68
static Error make_request_token(String &r_token);
69
bool send_request(DBusMessage *p_message, const String &r_token, String &r_response_path, String &r_response_filter);
70
71
struct ColorPickerData {
72
Callable callback;
73
String filter;
74
String path;
75
};
76
77
struct ColorPickerCallback {
78
Callable callback;
79
Variant status;
80
Variant color;
81
};
82
List<ColorPickerCallback> pending_color_cbs;
83
84
Mutex color_picker_mutex;
85
Vector<ColorPickerData> color_pickers;
86
87
struct FileDialogData {
88
Vector<String> filter_names;
89
HashMap<String, String> option_ids;
90
DisplayServer::WindowID prev_focus = DisplayServer::INVALID_WINDOW_ID;
91
Callable callback;
92
String filter;
93
String path;
94
bool opt_in_cb = false;
95
};
96
97
struct FileDialogCallback {
98
Callable callback;
99
Variant status;
100
Variant files;
101
Variant index;
102
Variant options;
103
bool opt_in_cb = false;
104
};
105
List<FileDialogCallback> pending_file_cbs;
106
107
Mutex file_dialog_mutex;
108
Vector<FileDialogData> file_dialogs;
109
Thread monitor_thread;
110
SafeFlag monitor_thread_abort;
111
DBusConnection *monitor_connection = nullptr;
112
113
String theme_path;
114
Callable system_theme_changed;
115
void _system_theme_changed_callback();
116
bool _is_interface_supported(const char *p_iface, uint32_t p_minimum_version);
117
118
Mutex inhibit_mutex;
119
String inhibit_path;
120
String inhibit_filter;
121
122
static void _thread_monitor(void *p_ud);
123
124
public:
125
FreeDesktopPortalDesktop();
126
~FreeDesktopPortalDesktop();
127
128
bool is_supported() { return !unsupported; }
129
bool is_file_chooser_supported();
130
bool is_settings_supported();
131
bool is_screenshot_supported();
132
bool is_inhibit_supported();
133
134
// org.freedesktop.portal.FileChooser methods.
135
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);
136
void process_callbacks();
137
138
// org.freedesktop.portal.Settings methods.
139
140
// Retrieve the system's preferred color scheme.
141
// 0: No preference or unknown.
142
// 1: Prefer dark appearance.
143
// 2: Prefer light appearance.
144
uint32_t get_appearance_color_scheme();
145
Color get_appearance_accent_color();
146
void set_system_theme_change_callback(const Callable &p_system_theme_changed) {
147
system_theme_changed = p_system_theme_changed;
148
}
149
150
// Retrieve high-contrast setting.
151
// -1: Unknown.
152
// 0: Disabled.
153
// 1: Enabled.
154
uint32_t get_high_contrast();
155
156
// org.freedesktop.portal.Screenshot methods.
157
bool color_picker(const String &p_xid, const Callable &p_callback);
158
159
// org.freedesktop.portal.Inhibit methods.
160
bool inhibit(const String &p_xid);
161
void uninhibit();
162
};
163
164
#endif // DBUS_ENABLED
165
166