From 526615bc10fa9e1e468bfafc13950792323a5dc2 Mon Sep 17 00:00:00 2001 From: Jelle Raaijmakers Date: Tue, 22 Jul 2025 15:11:18 +0200 Subject: [PATCH] LibWeb: Stub Navigator.getGamepads() --- Libraries/LibWeb/CMakeLists.txt | 2 + Libraries/LibWeb/Forward.h | 6 +++ Libraries/LibWeb/Gamepad/Gamepad.cpp | 24 +++++++++ Libraries/LibWeb/Gamepad/Gamepad.h | 24 +++++++++ Libraries/LibWeb/Gamepad/Gamepad.idl | 19 +++++++ Libraries/LibWeb/Gamepad/NavigatorGamepad.cpp | 49 +++++++++++++++++++ Libraries/LibWeb/Gamepad/NavigatorGamepad.h | 26 ++++++++++ Libraries/LibWeb/HTML/Navigator.h | 2 + Libraries/LibWeb/HTML/Navigator.idl | 1 + Libraries/LibWeb/idl_files.cmake | 1 + .../BindingsGenerator/IDLGenerators.cpp | 1 + .../LibWeb/BindingsGenerator/Namespaces.h | 1 + .../Text/expected/all-window-properties.txt | 1 + 13 files changed, 157 insertions(+) create mode 100644 Libraries/LibWeb/Gamepad/Gamepad.cpp create mode 100644 Libraries/LibWeb/Gamepad/Gamepad.h create mode 100644 Libraries/LibWeb/Gamepad/Gamepad.idl create mode 100644 Libraries/LibWeb/Gamepad/NavigatorGamepad.cpp create mode 100644 Libraries/LibWeb/Gamepad/NavigatorGamepad.h diff --git a/Libraries/LibWeb/CMakeLists.txt b/Libraries/LibWeb/CMakeLists.txt index b04be4db6b2..3a07161b4aa 100644 --- a/Libraries/LibWeb/CMakeLists.txt +++ b/Libraries/LibWeb/CMakeLists.txt @@ -346,6 +346,8 @@ set(SOURCES FileAPI/FileList.cpp FileAPI/FileReader.cpp FileAPI/FileReaderSync.cpp + Gamepad/Gamepad.cpp + Gamepad/NavigatorGamepad.cpp Geolocation/Geolocation.cpp Geolocation/GeolocationCoordinates.cpp Geolocation/GeolocationPosition.cpp diff --git a/Libraries/LibWeb/Forward.h b/Libraries/LibWeb/Forward.h index a0090df82ed..91118d7ce94 100644 --- a/Libraries/LibWeb/Forward.h +++ b/Libraries/LibWeb/Forward.h @@ -489,6 +489,12 @@ class FileList; } +namespace Web::Gamepad { + +class Gamepad; + +} + namespace Web::Geolocation { class Geolocation; diff --git a/Libraries/LibWeb/Gamepad/Gamepad.cpp b/Libraries/LibWeb/Gamepad/Gamepad.cpp new file mode 100644 index 00000000000..b619b315d65 --- /dev/null +++ b/Libraries/LibWeb/Gamepad/Gamepad.cpp @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2025, Jelle Raaijmakers + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include +#include + +namespace Web::Gamepad { + +Gamepad::Gamepad(JS::Realm& realm) + : PlatformObject(realm) +{ +} + +void Gamepad::initialize(JS::Realm& realm) +{ + WEB_SET_PROTOTYPE_FOR_INTERFACE(Gamepad); + Base::initialize(realm); +} + +} diff --git a/Libraries/LibWeb/Gamepad/Gamepad.h b/Libraries/LibWeb/Gamepad/Gamepad.h new file mode 100644 index 00000000000..b5173a58df5 --- /dev/null +++ b/Libraries/LibWeb/Gamepad/Gamepad.h @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2025, Jelle Raaijmakers + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include + +namespace Web::Gamepad { + +// https://w3c.github.io/gamepad/#dom-gamepad +class Gamepad : public Bindings::PlatformObject { + WEB_PLATFORM_OBJECT(Gamepad, Bindings::PlatformObject); + GC_DECLARE_ALLOCATOR(Gamepad); + +private: + explicit Gamepad(JS::Realm&); + + virtual void initialize(JS::Realm&) override; +}; + +} diff --git a/Libraries/LibWeb/Gamepad/Gamepad.idl b/Libraries/LibWeb/Gamepad/Gamepad.idl new file mode 100644 index 00000000000..d5fa34e6bd4 --- /dev/null +++ b/Libraries/LibWeb/Gamepad/Gamepad.idl @@ -0,0 +1,19 @@ +// https://w3c.github.io/gamepad/#dom-gamepad +[Exposed=Window] +interface Gamepad { + [FIXME] readonly attribute DOMString id; + [FIXME] readonly attribute long index; + [FIXME] readonly attribute boolean connected; + [FIXME] readonly attribute DOMHighResTimeStamp timestamp; + [FIXME] readonly attribute GamepadMappingType mapping; + [FIXME] readonly attribute FrozenArray axes; + [FIXME] readonly attribute FrozenArray buttons; + [FIXME] readonly attribute FrozenArray touches; + [FIXME, SameObject] readonly attribute GamepadHapticActuator vibrationActuator; +}; + +// https://w3c.github.io/gamepad/#idl-def-navigator-partial-1 +[Exposed=Window] +partial interface Navigator { + sequence getGamepads(); +}; diff --git a/Libraries/LibWeb/Gamepad/NavigatorGamepad.cpp b/Libraries/LibWeb/Gamepad/NavigatorGamepad.cpp new file mode 100644 index 00000000000..e0a43245024 --- /dev/null +++ b/Libraries/LibWeb/Gamepad/NavigatorGamepad.cpp @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2025, Jelle Raaijmakers + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include +#include +#include + +namespace Web::Gamepad { + +// https://w3c.github.io/gamepad/#dom-navigator-getgamepads +WebIDL::ExceptionOr>> NavigatorGamepadPartial::get_gamepads() +{ + auto& navigator = as(*this); + + // FIXME: 1. Let doc be the current global object's associated Document. + + // FIXME: 2. If doc is null or doc is not fully active, then return an empty list. + + // FIXME: 3. If doc is not allowed to use the "gamepad" permission, then throw a "SecurityError" DOMException and abort + // these steps. + + // FIXME: 4. If this.[[hasGamepadGesture]] is false, then return an empty list. + + // FIXME: 5. Let now be the current high resolution time given the current global object. + + // FIXME: 6. Let gamepads be an empty list. + + // FIXME: 7. For each gamepad of this.[[gamepads]]: + { + // FIXME: 1. If gamepad is not null and gamepad.[[exposed]] is false: + if (false) { + // FIXME: 1. Set gamepad.[[exposed]] to true. + + // FIXME: 2. Set gamepad.[[timestamp]] to now. + } + + // FIXME: 2. Append gamepad to gamepads. + } + + // FIXME: 8. Return gamepads. + dbgln("FIXME: Unimplemented NavigatorGamepadPartial::get_gamepads()"); + return GC::RootVector> { navigator.heap() }; +} + +} diff --git a/Libraries/LibWeb/Gamepad/NavigatorGamepad.h b/Libraries/LibWeb/Gamepad/NavigatorGamepad.h new file mode 100644 index 00000000000..4d2e05122fc --- /dev/null +++ b/Libraries/LibWeb/Gamepad/NavigatorGamepad.h @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025, Jelle Raaijmakers + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include +#include +#include +#include + +namespace Web::Gamepad { + +class NavigatorGamepadPartial { +public: + WebIDL::ExceptionOr>> get_gamepads(); + +private: + virtual ~NavigatorGamepadPartial() = default; + + friend class HTML::Navigator; +}; + +} diff --git a/Libraries/LibWeb/HTML/Navigator.h b/Libraries/LibWeb/HTML/Navigator.h index 9c1e22bec1f..87258658459 100644 --- a/Libraries/LibWeb/HTML/Navigator.h +++ b/Libraries/LibWeb/HTML/Navigator.h @@ -7,6 +7,7 @@ #pragma once #include +#include #include #include #include @@ -25,6 +26,7 @@ class Navigator : public Bindings::PlatformObject , public NavigatorBeaconPartial , public NavigatorConcurrentHardwareMixin , public NavigatorDeviceMemoryMixin + , public Gamepad::NavigatorGamepadPartial , public NavigatorIDMixin , public NavigatorLanguageMixin , public NavigatorOnLineMixin diff --git a/Libraries/LibWeb/HTML/Navigator.idl b/Libraries/LibWeb/HTML/Navigator.idl index 264bfbc102f..770e5d1555c 100644 --- a/Libraries/LibWeb/HTML/Navigator.idl +++ b/Libraries/LibWeb/HTML/Navigator.idl @@ -1,5 +1,6 @@ #import #import +#import #import #import #import diff --git a/Libraries/LibWeb/idl_files.cmake b/Libraries/LibWeb/idl_files.cmake index 0d1515449e7..22cfc20ba1c 100644 --- a/Libraries/LibWeb/idl_files.cmake +++ b/Libraries/LibWeb/idl_files.cmake @@ -105,6 +105,7 @@ libweb_js_bindings(FileAPI/File) libweb_js_bindings(FileAPI/FileList) libweb_js_bindings(FileAPI/FileReader) libweb_js_bindings(FileAPI/FileReaderSync) +libweb_js_bindings(Gamepad/Gamepad) libweb_js_bindings(Geolocation/Geolocation) libweb_js_bindings(Geolocation/GeolocationCoordinates) libweb_js_bindings(Geolocation/GeolocationPosition) diff --git a/Meta/Lagom/Tools/CodeGenerators/LibWeb/BindingsGenerator/IDLGenerators.cpp b/Meta/Lagom/Tools/CodeGenerators/LibWeb/BindingsGenerator/IDLGenerators.cpp index dcb8d270a0e..ba80431d5c6 100644 --- a/Meta/Lagom/Tools/CodeGenerators/LibWeb/BindingsGenerator/IDLGenerators.cpp +++ b/Meta/Lagom/Tools/CodeGenerators/LibWeb/BindingsGenerator/IDLGenerators.cpp @@ -4784,6 +4784,7 @@ using namespace Web::EntriesAPI; using namespace Web::EventTiming; using namespace Web::Fetch; using namespace Web::FileAPI; +using namespace Web::Gamepad; using namespace Web::Geolocation; using namespace Web::Geometry; using namespace Web::HighResolutionTime; diff --git a/Meta/Lagom/Tools/CodeGenerators/LibWeb/BindingsGenerator/Namespaces.h b/Meta/Lagom/Tools/CodeGenerators/LibWeb/BindingsGenerator/Namespaces.h index 90327ac77f8..99beb0e11ed 100644 --- a/Meta/Lagom/Tools/CodeGenerators/LibWeb/BindingsGenerator/Namespaces.h +++ b/Meta/Lagom/Tools/CodeGenerators/LibWeb/BindingsGenerator/Namespaces.h @@ -23,6 +23,7 @@ static constexpr Array libweb_interface_namespaces = { "Encoding"sv, "Fetch"sv, "FileAPI"sv, + "Gamepad"sv, "Geolocation"sv, "Geometry"sv, "HTML"sv, diff --git a/Tests/LibWeb/Text/expected/all-window-properties.txt b/Tests/LibWeb/Text/expected/all-window-properties.txt index 97089a31a4d..fa40dd46d83 100644 --- a/Tests/LibWeb/Text/expected/all-window-properties.txt +++ b/Tests/LibWeb/Text/expected/all-window-properties.txt @@ -139,6 +139,7 @@ FormData FormDataEvent Function GainNode +Gamepad Geolocation GeolocationCoordinates GeolocationPosition