Skip to content

Commit 504de8b

Browse files
committed
Merge pull request #91071 from bruvzg/dbus_fd_cb_process
[DBus] Process file dialog callback in the main event loop instead of using deferred call.
2 parents 17d9c52 + 67d6be3 commit 504de8b

File tree

4 files changed

+54
-20
lines changed

4 files changed

+54
-20
lines changed

platform/linuxbsd/freedesktop_portal_desktop.cpp

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -496,24 +496,30 @@ Error FreeDesktopPortalDesktop::file_dialog_show(DisplayServer::WindowID p_windo
496496
return OK;
497497
}
498498

499-
void FreeDesktopPortalDesktop::_file_dialog_callback(const Callable &p_callable, const Variant &p_status, const Variant &p_list, const Variant &p_index, const Variant &p_options, bool p_opt_in_cb) {
500-
if (p_opt_in_cb) {
501-
Variant ret;
502-
Callable::CallError ce;
503-
const Variant *args[4] = { &p_status, &p_list, &p_index, &p_options };
504-
505-
p_callable.callp(args, 4, ret, ce);
506-
if (ce.error != Callable::CallError::CALL_OK) {
507-
ERR_PRINT(vformat("Failed to execute file dialogs callback: %s.", Variant::get_callable_error_text(p_callable, args, 4, ce)));
508-
}
509-
} else {
510-
Variant ret;
511-
Callable::CallError ce;
512-
const Variant *args[3] = { &p_status, &p_list, &p_index };
499+
void FreeDesktopPortalDesktop::process_file_dialog_callbacks() {
500+
MutexLock lock(file_dialog_mutex);
501+
while (!pending_cbs.is_empty()) {
502+
FileDialogCallback cb = pending_cbs.front()->get();
503+
pending_cbs.pop_front();
504+
505+
if (cb.opt_in_cb) {
506+
Variant ret;
507+
Callable::CallError ce;
508+
const Variant *args[4] = { &cb.status, &cb.files, &cb.index, &cb.options };
509+
510+
cb.callback.callp(args, 4, ret, ce);
511+
if (ce.error != Callable::CallError::CALL_OK) {
512+
ERR_PRINT(vformat("Failed to execute file dialog callback: %s.", Variant::get_callable_error_text(cb.callback, args, 4, ce)));
513+
}
514+
} else {
515+
Variant ret;
516+
Callable::CallError ce;
517+
const Variant *args[3] = { &cb.status, &cb.files, &cb.index };
513518

514-
p_callable.callp(args, 3, ret, ce);
515-
if (ce.error != Callable::CallError::CALL_OK) {
516-
ERR_PRINT(vformat("Failed to execute file dialogs callback: %s.", Variant::get_callable_error_text(p_callable, args, 3, ce)));
519+
cb.callback.callp(args, 3, ret, ce);
520+
if (ce.error != Callable::CallError::CALL_OK) {
521+
ERR_PRINT(vformat("Failed to execute file dialog callback: %s.", Variant::get_callable_error_text(cb.callback, args, 3, ce)));
522+
}
517523
}
518524
}
519525
}
@@ -556,7 +562,14 @@ void FreeDesktopPortalDesktop::_thread_monitor(void *p_ud) {
556562
file_chooser_parse_response(&iter, fd.filter_names, cancel, uris, index, options);
557563

558564
if (fd.callback.is_valid()) {
559-
callable_mp(portal, &FreeDesktopPortalDesktop::_file_dialog_callback).call_deferred(fd.callback, !cancel, uris, index, options, fd.opt_in_cb);
565+
FileDialogCallback cb;
566+
cb.callback = fd.callback;
567+
cb.status = !cancel;
568+
cb.files = uris;
569+
cb.index = index;
570+
cb.options = options;
571+
cb.opt_in_cb = fd.opt_in_cb;
572+
portal->pending_cbs.push_back(cb);
560573
}
561574
if (fd.prev_focus != DisplayServer::INVALID_WINDOW_ID) {
562575
callable_mp(DisplayServer::get_singleton(), &DisplayServer::window_move_to_foreground).call_deferred(fd.prev_focus);

platform/linuxbsd/freedesktop_portal_desktop.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,6 @@ class FreeDesktopPortalDesktop : public Object {
5656
static void append_dbus_dict_bool(DBusMessageIter *p_iter, const String &p_key, bool p_value);
5757
static bool file_chooser_parse_response(DBusMessageIter *p_iter, const Vector<String> &p_names, bool &r_cancel, Vector<String> &r_urls, int &r_index, Dictionary &r_options);
5858

59-
void _file_dialog_callback(const Callable &p_callable, const Variant &p_status, const Variant &p_list, const Variant &p_index, const Variant &p_options, bool p_opt_in_cb);
60-
6159
struct FileDialogData {
6260
Vector<String> filter_names;
6361
DisplayServer::WindowID prev_focus = DisplayServer::INVALID_WINDOW_ID;
@@ -67,6 +65,16 @@ class FreeDesktopPortalDesktop : public Object {
6765
bool opt_in_cb = false;
6866
};
6967

68+
struct FileDialogCallback {
69+
Callable callback;
70+
Variant status;
71+
Variant files;
72+
Variant index;
73+
Variant options;
74+
bool opt_in_cb = false;
75+
};
76+
List<FileDialogCallback> pending_cbs;
77+
7078
Mutex file_dialog_mutex;
7179
Vector<FileDialogData> file_dialogs;
7280
Thread monitor_thread;
@@ -86,6 +94,7 @@ class FreeDesktopPortalDesktop : public Object {
8694
bool is_supported() { return !unsupported; }
8795

8896
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);
97+
void process_file_dialog_callbacks();
8998

9099
// Retrieve the system's preferred color scheme.
91100
// 0: No preference or unknown.

platform/linuxbsd/wayland/display_server_wayland.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1161,6 +1161,12 @@ void DisplayServerWayland::process_events() {
11611161
}
11621162
}
11631163

1164+
#ifdef DBUS_ENABLED
1165+
if (portal_desktop) {
1166+
portal_desktop->process_file_dialog_callbacks();
1167+
}
1168+
#endif
1169+
11641170
wayland_thread.mutex.unlock();
11651171

11661172
Input::get_singleton()->flush_buffered_events();

platform/linuxbsd/x11/display_server_x11.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5097,6 +5097,12 @@ void DisplayServerX11::process_events() {
50975097
*/
50985098
}
50995099

5100+
#ifdef DBUS_ENABLED
5101+
if (portal_desktop) {
5102+
portal_desktop->process_file_dialog_callbacks();
5103+
}
5104+
#endif
5105+
51005106
_THREAD_SAFE_UNLOCK_
51015107

51025108
Input::get_singleton()->flush_buffered_events();

0 commit comments

Comments
 (0)