Repository: LadybirdBrowser/ladybird
File: Libraries/LibWeb/Internals/InternalGamepad.h (C/C++)

Code before:
/*
 * Copyright (c) 2025, Luke Wilde <luke@ladybird.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <LibWeb/Bindings/PlatformObject.h>

// NOTE: Unfortunately we have to just manually define these enums given forward declaring is not supported, so updating SDL versions that adjust the content of these enums may break things

typedef enum SDL_GamepadButton {
    SDL_GAMEPAD_BUTTON_INVALID = -1,
    SDL_GAMEPAD_BUTTON_SOUTH, /**< Bottom face button (e.g. Xbox A button) */
    SDL_GAMEPAD_BUTTON_EAST,  /**< Right face button (e.g. Xbox B button) */
    SDL_GAMEPAD_BUTTON_WEST,  /**< Left face button (e.g. Xbox X button) */
    SDL_GAMEPAD_BUTTON_NORTH, /**< Top face button (e.g. Xbox Y button) */
    SDL_GAMEPAD_BUTTON_BACK,
    SDL_GAMEPAD_BUTTON_GUIDE,
    SDL_GAMEPAD_BUTTON_START,
    SDL_GAMEPAD_BUTTON_LEFT_STICK,
    SDL_GAMEPAD_BUTTON_RIGHT_STICK,
    SDL_GAMEPAD_BUTTON_LEFT_SHOULDER,
    SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER,
    SDL_GAMEPAD_BUTTON_DPAD_UP,
    SDL_GAMEPAD_BUTTON_DPAD_DOWN,
    SDL_GAMEPAD_BUTTON_DPAD_LEFT,
    SDL_GAMEPAD_BUTTON_DPAD_RIGHT,
    SDL_GAMEPAD_BUTTON_MISC1,         /**< Additional button (e.g. Xbox Series X share button, PS5 microphone button, Nintendo Switch Pro capture button, Amazon Luna microphone button, Google Stadia capture button) */
    SDL_GAMEPAD_BUTTON_RIGHT_PADDLE1, /**< Upper or primary paddle, under your right hand (e.g. Xbox Elite paddle P1) */
    SDL_GAMEPAD_BUTTON_LEFT_PADDLE1,  /**< Upper or primary paddle, under your left hand (e.g. Xbox Elite paddle P3) */
    SDL_GAMEPAD_BUTTON_RIGHT_PADDLE2, /**< Lower or secondary paddle, under your right hand (e.g. Xbox Elite paddle P2) */
    SDL_GAMEPAD_BUTTON_LEFT_PADDLE2,  /**< Lower or secondary paddle, under your left hand (e.g. Xbox Elite paddle P4) */
    SDL_GAMEPAD_BUTTON_TOUCHPAD,      /**< PS4/PS5 touchpad button */
    SDL_GAMEPAD_BUTTON_MISC2,         /**< Additional button */
    SDL_GAMEPAD_BUTTON_MISC3,         /**< Additional button */
    SDL_GAMEPAD_BUTTON_MISC4,         /**< Additional button */

Review comment (suggestion):
suggestion: It may be better to make buttons, axes and triggers return `Array<i32, [length]>` instead. It doesn't have to be super pretty with being an internal testing class not exposed to the web.

Code after:
/*
 * Copyright (c) 2025, Luke Wilde <luke@ladybird.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <LibWeb/Bindings/PlatformObject.h>
#include <LibWeb/Gamepad/SDLGamepadForward.h>

namespace Web::Internals {

class InternalGamepad : public Bindings::PlatformObject {
    WEB_PLATFORM_OBJECT(InternalGamepad, Bindings::PlatformObject);
    GC_DECLARE_ALLOCATOR(InternalGamepad);

public:
    static GC::Ref<InternalGamepad> create(JS::Realm&);

    virtual ~InternalGamepad() override;

    Array<i32, 15> const& buttons();
    Array<i32, 4> const& axes();
    Array<i32, 2> const& triggers();

    void set_button(int button, bool down);
    void set_axis(int axis, short value);

    GC::RootVector<JS::Object*> get_received_rumble_effects() const;
    GC::RootVector<JS::Object*> get_received_rumble_trigger_effects() const;

    void received_rumble(u16 low_frequency_rumble, u16 high_frequency_rumble);
    void received_rumble_triggers(u16 left_rumble, u16 right_rumble);

    void disconnect();

private: