Skip to content

Commit

Permalink
Implement DisplayServer.beep.
Browse files Browse the repository at this point in the history
  • Loading branch information
bruvzg committed Dec 3, 2024
1 parent 0f20e67 commit 84650f2
Show file tree
Hide file tree
Showing 17 changed files with 146 additions and 0 deletions.
7 changes: 7 additions & 0 deletions doc/classes/DisplayServer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@
<tutorials>
</tutorials>
<methods>
<method name="beep" qualifiers="const">
<return type="void" />
<description>
Plays the beep sound from the operative system, if possible. Because it comes from the OS, the beep sound will be audible even if the application is muted. It may also be disabled for the entire OS by the user.
[b]Note:[/b] This method is implemented on macOS, Linux (X11/Wayland), and Windows.
</description>
</method>
<method name="clipboard_get" qualifiers="const">
<return type="String" />
<description>
Expand Down
11 changes: 11 additions & 0 deletions platform/linuxbsd/wayland/SCsub
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,22 @@ env.WAYLAND_API_CODE(
source="#thirdparty/wayland-protocols/unstable/xdg-foreign/xdg-foreign-unstable-v1.xml",
)

env.WAYLAND_API_HEADER(
target="protocol/xdg_system_bell.gen.h",
source="#thirdparty/wayland-protocols/staging/xdg-system-bell/xdg-system-bell-v1.xml",
)

env.WAYLAND_API_CODE(
target="protocol/xdg_system_bell.gen.c",
source="#thirdparty/wayland-protocols/staging/xdg-system-bell/xdg-system-bell-v1.xml",
)

source_files = [
"protocol/wayland.gen.c",
"protocol/viewporter.gen.c",
"protocol/fractional_scale.gen.c",
"protocol/xdg_shell.gen.c",
"protocol/xdg_system_bell.gen.c",
"protocol/xdg_foreign.gen.c",
"protocol/xdg_decoration.gen.c",
"protocol/xdg_activation.gen.c",
Expand Down
4 changes: 4 additions & 0 deletions platform/linuxbsd/wayland/display_server_wayland.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,10 @@ Error DisplayServerWayland::file_dialog_with_options_show(const String &p_title,

#endif

void DisplayServerWayland::beep() const {
wayland_thread.beep();
}

void DisplayServerWayland::mouse_set_mode(MouseMode p_mode) {
if (p_mode == mouse_mode) {
return;
Expand Down
2 changes: 2 additions & 0 deletions platform/linuxbsd/wayland/display_server_wayland.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ class DisplayServerWayland : public DisplayServer {
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 beep() const override;

virtual void mouse_set_mode(MouseMode p_mode) override;
virtual MouseMode mouse_get_mode() const override;

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

if (strcmp(interface, xdg_system_bell_v1_interface.name) == 0) {
registry->xdg_system_bell = (struct xdg_system_bell_v1 *)wl_registry_bind(wl_registry, name, &xdg_system_bell_v1_interface, 1);
registry->xdg_system_bell_name = name;
return;
}

if (strcmp(interface, xdg_activation_v1_interface.name) == 0) {
registry->xdg_activation = (struct xdg_activation_v1 *)wl_registry_bind(wl_registry, name, &xdg_activation_v1_interface, 1);
registry->xdg_activation_name = name;
Expand Down Expand Up @@ -692,6 +698,17 @@ void WaylandThread::_wl_registry_on_global_remove(void *data, struct wl_registry
return;
}

if (name == registry->xdg_system_bell_name) {
if (registry->xdg_system_bell) {
xdg_system_bell_v1_destroy(registry->xdg_system_bell);
registry->xdg_system_bell = nullptr;
}

registry->xdg_system_bell_name = 0;

return;
}

if (name == registry->xdg_activation_name) {
if (registry->xdg_activation) {
xdg_activation_v1_destroy(registry->xdg_activation);
Expand Down Expand Up @@ -3282,6 +3299,12 @@ struct wl_surface *WaylandThread::window_get_wl_surface(DisplayServer::WindowID
return ws.wl_surface;
}

void WaylandThread::beep() const {
if (registry.xdg_system_bell) {
xdg_system_bell_v1_ring(registry.xdg_system_bell, nullptr);
}
}

void WaylandThread::window_set_max_size(DisplayServer::WindowID p_window_id, const Size2i &p_size) {
// TODO: Use window IDs for multiwindow support.
WindowState &ws = main_window;
Expand Down Expand Up @@ -4364,6 +4387,10 @@ void WaylandThread::destroy() {
xdg_activation_v1_destroy(registry.xdg_activation);
}

if (registry.xdg_system_bell) {
xdg_system_bell_v1_destroy(registry.xdg_system_bell);
}

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

#ifdef LIBDECOR_ENABLED
#ifdef SOWRAP_ENABLED
Expand Down Expand Up @@ -162,6 +163,9 @@ class WaylandThread {
struct zxdg_decoration_manager_v1 *xdg_decoration_manager = nullptr;
uint32_t xdg_decoration_manager_name = 0;

struct xdg_system_bell_v1 *xdg_system_bell = nullptr;
uint32_t xdg_system_bell_name = 0;

struct xdg_activation_v1 *xdg_activation = nullptr;
uint32_t xdg_activation_name = 0;

Expand Down Expand Up @@ -926,6 +930,8 @@ class WaylandThread {
bool has_message();
Ref<Message> pop_message();

void beep() const;

void window_create(DisplayServer::WindowID p_window_id, int p_width, int p_height);

struct wl_surface *window_get_wl_surface(DisplayServer::WindowID p_window_id) const;
Expand Down
4 changes: 4 additions & 0 deletions platform/linuxbsd/x11/display_server_x11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,10 @@ Error DisplayServerX11::file_dialog_with_options_show(const String &p_title, con

#endif

void DisplayServerX11::beep() const {
XBell(x11_display, 0);
}

void DisplayServerX11::mouse_set_mode(MouseMode p_mode) {
_THREAD_SAFE_METHOD_

Expand Down
2 changes: 2 additions & 0 deletions platform/linuxbsd/x11/display_server_x11.h
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,8 @@ class DisplayServerX11 : public DisplayServer {
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 beep() const override;

virtual void mouse_set_mode(MouseMode p_mode) override;
virtual MouseMode mouse_get_mode() const override;

Expand Down
2 changes: 2 additions & 0 deletions platform/macos/display_server_macos.h
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,8 @@ class DisplayServerMacOS : public DisplayServer {
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;

virtual void beep() const override;

virtual void mouse_set_mode(MouseMode p_mode) override;
virtual MouseMode mouse_get_mode() const override;

Expand Down
4 changes: 4 additions & 0 deletions platform/macos/display_server_macos.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1200,6 +1200,10 @@
return OK;
}

void DisplayServerMacOS::beep() const {
NSBeep();
}

Error DisplayServerMacOS::dialog_input_text(String p_title, String p_description, String p_partial, const Callable &p_callback) {
_THREAD_SAFE_METHOD_

Expand Down
4 changes: 4 additions & 0 deletions platform/windows/display_server_windows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -786,6 +786,10 @@ void DisplayServerWindows::process_file_dialog_callbacks() {
}
}

void DisplayServerWindows::beep() const {
MessageBeep(MB_OK);
}

void DisplayServerWindows::mouse_set_mode(MouseMode p_mode) {
_THREAD_SAFE_METHOD_

Expand Down
2 changes: 2 additions & 0 deletions platform/windows/display_server_windows.h
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,8 @@ class DisplayServerWindows : public DisplayServer {
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;

virtual void beep() const override;

virtual void mouse_set_mode(MouseMode p_mode) override;
virtual MouseMode mouse_get_mode() const override;

Expand Down
5 changes: 5 additions & 0 deletions servers/display_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,9 @@ Error DisplayServer::file_dialog_with_options_show(const String &p_title, const
return ERR_UNAVAILABLE;
}

void DisplayServer::beep() const {
}

int DisplayServer::keyboard_get_layout_count() const {
return 0;
}
Expand Down Expand Up @@ -998,6 +1001,8 @@ void DisplayServer::_bind_methods() {
ClassDB::bind_method(D_METHOD("file_dialog_show", "title", "current_directory", "filename", "show_hidden", "mode", "filters", "callback"), &DisplayServer::file_dialog_show);
ClassDB::bind_method(D_METHOD("file_dialog_with_options_show", "title", "current_directory", "root", "filename", "show_hidden", "mode", "filters", "options", "callback"), &DisplayServer::file_dialog_with_options_show);

ClassDB::bind_method(D_METHOD("beep"), &DisplayServer::beep);

ClassDB::bind_method(D_METHOD("keyboard_get_layout_count"), &DisplayServer::keyboard_get_layout_count);
ClassDB::bind_method(D_METHOD("keyboard_get_current_layout"), &DisplayServer::keyboard_get_current_layout);
ClassDB::bind_method(D_METHOD("keyboard_set_current_layout", "index"), &DisplayServer::keyboard_set_current_layout);
Expand Down
2 changes: 2 additions & 0 deletions servers/display_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,8 @@ class DisplayServer : public Object {
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);
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);

virtual void beep() const;

virtual int keyboard_get_layout_count() const;
virtual int keyboard_get_current_layout() const;
virtual void keyboard_set_current_layout(int p_index);
Expand Down
1 change: 1 addition & 0 deletions thirdparty/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1026,6 +1026,7 @@ Files extracted from upstream source:
- `staging/fractional-scale/fractional-scale-v1.xml`
- `staging/xdg-activation/README`
- `staging/xdg-activation/xdg-activation-v1.xml`
- `staging/xdg-system-bell/xdg-system-bell-v1.xml`
- `unstable/idle-inhibit/README`
- `unstable/idle-inhibit/idle-inhibit-unstable-v1.xml`
- `unstable/pointer-constraints/README`
Expand Down
5 changes: 5 additions & 0 deletions thirdparty/wayland-protocols/staging/xdg-system-bell/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
system_bell protocol

Maintainers:
Jonas Ådahl <[email protected]>
Carlos Garnacho <[email protected]>
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?xml version="1.0" encoding="UTF-8"?>
<protocol name="xdg_system_bell_v1">
<copyright>
Copyright © 2016, 2023 Red Hat

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice (including the next
paragraph) shall be included in all copies or substantial portions of the
Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
</copyright>

<interface name="xdg_system_bell_v1" version="1">
<description summary="system bell">
This global interface enables clients to ring the system bell.

Warning! The protocol described in this file is currently in the testing
phase. Backward compatible changes may be added together with the
corresponding interface version bump. Backward incompatible changes can
only be done by creating a new major version of the extension.
</description>

<request name="destroy" type="destructor">
<description summary="destroy the system bell object">
Notify that the object will no longer be used.
</description>
</request>

<request name="ring">
<description summary="ring the system bell">
This requests rings the system bell on behalf of a client. How ringing
the bell is implemented is up to the compositor. It may be an audible
sound, a visual feedback of some kind, or any other thing including
nothing.

The passed surface should correspond to a toplevel like surface role,
or be null, meaning the client doesn't have a particular toplevel it
wants to associate the bell ringing with. See the xdg-shell protocol
extension for a toplevel like surface role.
</description>
<arg name="surface" type="object" interface="wl_surface"
allow-null="true" summary="associated surface"/>
</request>
</interface>
</protocol>

0 comments on commit 84650f2

Please sign in to comment.