Skip to content

Commit

Permalink
Merge pull request #87776 from bruvzg/wl_nfd
Browse files Browse the repository at this point in the history
[Wayland] Add support for native file dialogs.
  • Loading branch information
akien-mga authored Jan 31, 2024
2 parents f8a039e + edb21e0 commit f23fda3
Show file tree
Hide file tree
Showing 9 changed files with 271 additions and 3 deletions.
4 changes: 2 additions & 2 deletions doc/classes/DisplayServer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@
<description>
Displays OS native dialog for selecting files or directories in the file system.
Callbacks have the following arguments: [code]status: bool, selected_paths: PackedStringArray, selected_filter_index: int[/code].
[b]Note:[/b] This method is implemented if the display server has the [constant FEATURE_NATIVE_DIALOG] feature, i.e. Linux (X11), Windows, and macOS.
[b]Note:[/b] This method is implemented if the display server has the [constant FEATURE_NATIVE_DIALOG] feature. Supported platforms include Linux (X11 and Wayland), Windows, and macOS.
[b]Note:[/b] [param current_directory] might be ignored.
[b]Note:[/b] On Linux, [param show_hidden] is ignored.
[b]Note:[/b] On macOS, native file dialogs have no title.
Expand All @@ -145,7 +145,7 @@
- [code]"values"[/code] - [PackedStringArray] of values. If empty, boolean option (check box) is used.
- [code]"default"[/code] - default selected option index ([int]) or default boolean value ([bool]).
Callbacks have the following arguments: [code]status: bool, selected_paths: PackedStringArray, selected_filter_index: int, selected_option: Dictionary[/code].
[b]Note:[/b] This method is implemented if the display server has the [constant FEATURE_NATIVE_DIALOG] feature, i.e. Linux, Windows, and macOS.
[b]Note:[/b] This method is implemented if the display server has the [constant FEATURE_NATIVE_DIALOG] feature. Supported platforms include Linux (X11 and Wayland), Windows, and macOS.
[b]Note:[/b] [param current_directory] might be ignored.
[b]Note:[/b] On Linux (X11), [param show_hidden] is ignored.
[b]Note:[/b] On macOS, native file dialogs have no title.
Expand Down
11 changes: 11 additions & 0 deletions platform/linuxbsd/wayland/SCsub
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,22 @@ env.WAYLAND_API_CODE(
source="#thirdparty/wayland-protocols/unstable/tablet/tablet-unstable-v2.xml",
)

env.WAYLAND_API_HEADER(
target="protocol/xdg_foreign.gen.h",
source="#thirdparty/wayland-protocols/unstable/xdg-foreign/xdg-foreign-unstable-v1.xml",
)

env.WAYLAND_API_CODE(
target="protocol/xdg_foreign.gen.c",
source="#thirdparty/wayland-protocols/unstable/xdg-foreign/xdg-foreign-unstable-v1.xml",
)

source_files = [
"protocol/wayland.gen.c",
"protocol/viewporter.gen.c",
"protocol/fractional_scale.gen.c",
"protocol/xdg_shell.gen.c",
"protocol/xdg_foreign.gen.c",
"protocol/xdg_decoration.gen.c",
"protocol/xdg_activation.gen.c",
"protocol/relative_pointer.gen.c",
Expand Down
22 changes: 21 additions & 1 deletion platform/linuxbsd/wayland/display_server_wayland.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,9 @@ bool DisplayServerWayland::has_feature(Feature p_feature) const {
case FEATURE_SWAP_BUFFERS:
case FEATURE_KEEP_SCREEN_ON:
case FEATURE_CLIPBOARD_PRIMARY:
#ifdef DBUS_ENABLED
case FEATURE_NATIVE_DIALOG:
#endif
case FEATURE_HIDPI: {
return true;
} break;
Expand Down Expand Up @@ -269,6 +272,22 @@ bool DisplayServerWayland::is_dark_mode() const {
}
}

Error DisplayServerWayland::file_dialog_show(const String &p_title, const String &p_current_directory, const String &p_filename, bool p_show_hidden, FileDialogMode p_mode, const Vector<String> &p_filters, const Callable &p_callback) {
WindowID window_id = MAIN_WINDOW_ID;
// TODO: Use window IDs for multiwindow support.

WaylandThread::WindowState *ws = wayland_thread.wl_surface_get_window_state(wayland_thread.window_get_wl_surface(window_id));
return portal_desktop->file_dialog_show(window_id, (ws ? ws->exported_handle : String()), p_title, p_current_directory, String(), p_filename, p_mode, p_filters, TypedArray<Dictionary>(), p_callback, false);
}

Error DisplayServerWayland::file_dialog_with_options_show(const String &p_title, const String &p_current_directory, const String &p_root, const String &p_filename, bool p_show_hidden, FileDialogMode p_mode, const Vector<String> &p_filters, const TypedArray<Dictionary> &p_options, const Callable &p_callback) {
WindowID window_id = MAIN_WINDOW_ID;
// TODO: Use window IDs for multiwindow support.

WaylandThread::WindowState *ws = wayland_thread.wl_surface_get_window_state(wayland_thread.window_get_wl_surface(window_id));
return portal_desktop->file_dialog_show(window_id, (ws ? ws->exported_handle : String()), p_title, p_current_directory, p_root, p_filename, p_mode, p_filters, p_options, p_callback, true);
}

#endif

void DisplayServerWayland::mouse_set_mode(MouseMode p_mode) {
Expand Down Expand Up @@ -1192,10 +1211,11 @@ DisplayServerWayland::DisplayServerWayland(const String &p_rendering_driver, Win

if (context_rd) {
if (context_rd->initialize() != OK) {
ERR_PRINT(vformat("Could not initialize %s", context_rd->get_api_name()));
memdelete(context_rd);
context_rd = nullptr;
r_error = ERR_CANT_CREATE;
ERR_FAIL_MSG(vformat("Could not initialize %s", context_rd->get_api_name()));
return;
}
}
#endif
Expand Down
3 changes: 3 additions & 0 deletions platform/linuxbsd/wayland/display_server_wayland.h
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,9 @@ class DisplayServerWayland : public DisplayServer {
#ifdef DBUS_ENABLED
virtual bool is_dark_mode_supported() const override;
virtual bool is_dark_mode() const override;

virtual Error file_dialog_show(const String &p_title, const String &p_current_directory, const String &p_filename, bool p_show_hidden, FileDialogMode p_mode, const Vector<String> &p_filters, const Callable &p_callback) override;
virtual Error file_dialog_with_options_show(const String &p_title, const String &p_current_directory, const String &p_root, const String &p_filename, bool p_show_hidden, FileDialogMode p_mode, const Vector<String> &p_filters, const TypedArray<Dictionary> &p_options, const Callable &p_callback) override;
#endif

virtual void mouse_set_mode(MouseMode p_mode) override;
Expand Down
33 changes: 33 additions & 0 deletions platform/linuxbsd/wayland/wayland_thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,12 @@ void WaylandThread::_wl_registry_on_global(void *data, struct wl_registry *wl_re
return;
}

if (strcmp(interface, zxdg_exporter_v1_interface.name) == 0) {
registry->wl_exporter = (struct zxdg_exporter_v1 *)wl_registry_bind(wl_registry, name, &zxdg_exporter_v1_interface, 1);
registry->wl_exporter_name = name;
return;
}

if (strcmp(interface, wl_compositor_interface.name) == 0) {
registry->wl_compositor = (struct wl_compositor *)wl_registry_bind(wl_registry, name, &wl_compositor_interface, 4);
registry->wl_compositor_name = name;
Expand Down Expand Up @@ -570,6 +576,17 @@ void WaylandThread::_wl_registry_on_global_remove(void *data, struct wl_registry
return;
}

if (name == registry->wl_exporter_name) {
if (registry->wl_exporter) {
zxdg_exporter_v1_destroy(registry->wl_exporter);
registry->wl_exporter = nullptr;
}

registry->wl_exporter_name = 0;

return;
}

if (name == registry->wl_compositor_name) {
if (registry->wl_compositor) {
wl_compositor_destroy(registry->wl_compositor);
Expand Down Expand Up @@ -1107,6 +1124,13 @@ void WaylandThread::_xdg_toplevel_on_wm_capabilities(void *data, struct xdg_topl
}
}

void WaylandThread::_xdg_exported_on_exported(void *data, zxdg_exported_v1 *exported, const char *handle) {
WindowState *ws = (WindowState *)data;
ERR_FAIL_NULL(ws);

ws->exported_handle = vformat("wayland:%s", String::utf8(handle));
}

void WaylandThread::_xdg_toplevel_decoration_on_configure(void *data, struct zxdg_toplevel_decoration_v1 *xdg_toplevel_decoration, uint32_t mode) {
if (mode == ZXDG_TOPLEVEL_DECORATION_V1_MODE_CLIENT_SIDE) {
#ifdef LIBDECOR_ENABLED
Expand Down Expand Up @@ -2975,6 +2999,11 @@ void WaylandThread::window_create(DisplayServer::WindowID p_window_id, int p_wid
// "loop".
wl_surface_commit(ws.wl_surface);

if (registry.wl_exporter) {
ws.xdg_exported = zxdg_exporter_v1_export(registry.wl_exporter, ws.wl_surface);
zxdg_exported_v1_add_listener(ws.xdg_exported, &xdg_exported_listener, &ws);
}

// Wait for the surface to be configured before continuing.
wl_display_roundtrip(wl_display);
}
Expand Down Expand Up @@ -3980,6 +4009,10 @@ void WaylandThread::destroy() {
xdg_wm_base_destroy(registry.xdg_wm_base);
}

if (registry.wl_exporter) {
zxdg_exporter_v1_destroy(registry.wl_exporter);
}

if (registry.wl_shm) {
wl_shm_destroy(registry.wl_shm);
}
Expand Down
13 changes: 13 additions & 0 deletions platform/linuxbsd/wayland/wayland_thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
#include "wayland/protocol/wayland.gen.h"
#include "wayland/protocol/xdg_activation.gen.h"
#include "wayland/protocol/xdg_decoration.gen.h"
#include "wayland/protocol/xdg_foreign.gen.h"
#include "wayland/protocol/xdg_shell.gen.h"

#ifdef LIBDECOR_ENABLED
Expand Down Expand Up @@ -132,6 +133,9 @@ class WaylandThread {
struct xdg_wm_base *xdg_wm_base = nullptr;
uint32_t xdg_wm_base_name = 0;

struct zxdg_exporter_v1 *wl_exporter = nullptr;
uint32_t wl_exporter_name = 0;

// wayland-protocols globals.

struct wp_viewporter *wp_viewporter = nullptr;
Expand Down Expand Up @@ -197,6 +201,9 @@ class WaylandThread {

struct wp_viewport *wp_viewport = nullptr;
struct wp_fractional_scale_v1 *wp_fractional_scale = nullptr;
struct zxdg_exported_v1 *xdg_exported = nullptr;

String exported_handle;

// Currently applied buffer scale.
int buffer_scale = 1;
Expand Down Expand Up @@ -599,6 +606,8 @@ class WaylandThread {

static void _xdg_toplevel_decoration_on_configure(void *data, struct zxdg_toplevel_decoration_v1 *xdg_toplevel_decoration, uint32_t mode);

static void _xdg_exported_on_exported(void *data, zxdg_exported_v1 *exported, const char *handle);

static void _xdg_activation_token_on_done(void *data, struct xdg_activation_token_v1 *xdg_activation_token, const char *token);

// Core Wayland event listeners.
Expand Down Expand Up @@ -753,6 +762,10 @@ class WaylandThread {
.frame = _wp_tablet_tool_on_frame,
};

static constexpr struct zxdg_exported_v1_listener xdg_exported_listener = {
.handle = _xdg_exported_on_exported
};

static constexpr struct zxdg_toplevel_decoration_v1_listener xdg_toplevel_decoration_listener = {
.configure = _xdg_toplevel_decoration_on_configure,
};
Expand Down
2 changes: 2 additions & 0 deletions thirdparty/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -971,6 +971,8 @@ Files extracted from upstream source:
- `unstable/tablet/tablet-unstable-v2.xml`
- `unstable/xdg-decoration/README`
- `unstable/xdg-decoration/xdg-decoration-unstable-v1.xml`
- `unstable/xdg-foreign/README`
- `unstable/xdg-foreign/xdg-foreign-unstable-v1.xml`
- `COPYING`


Expand Down
4 changes: 4 additions & 0 deletions thirdparty/wayland-protocols/unstable/xdg-foreign/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
xdg foreign protocol

Maintainers:
Jonas Ådahl <[email protected]>
Loading

0 comments on commit f23fda3

Please sign in to comment.