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