From 26a029d407be480d791972afb5975cf62c9360a6 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Fri, 19 Apr 2024 02:47:55 +0200 Subject: Adding upstream version 124.0.1. Signed-off-by: Daniel Baumann --- dom/gamepad/GamepadHandle.h | 89 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 dom/gamepad/GamepadHandle.h (limited to 'dom/gamepad/GamepadHandle.h') diff --git a/dom/gamepad/GamepadHandle.h b/dom/gamepad/GamepadHandle.h new file mode 100644 index 0000000000..c13e2b912e --- /dev/null +++ b/dom/gamepad/GamepadHandle.h @@ -0,0 +1,89 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +// This file defines a strongly-typed opaque gamepad handle +// +// The handle is designed to be copied around and passed over IPC. It keeps +// track of which "provider" created the handle so it can ensure that +// providers are never mixed. It also allows each provider to have its own +// algorithm for generating gamepad IDs, since the VR and Platform services +// do it differently. + +#ifndef mozilla_dom_gamepad_GamepadHandle_h +#define mozilla_dom_gamepad_GamepadHandle_h + +#include "PLDHashTable.h" +#include +#include + +namespace IPC { + +template +struct ParamTraits; + +} // namespace IPC + +namespace mozilla::gfx { + +class VRDisplayClient; +class VRManager; + +} // namespace mozilla::gfx + +namespace mozilla::dom { + +class GamepadPlatformService; +class GamepadServiceTest; +class XRInputSource; + +// The "kind" of a gamepad handle is based on which provider created it +enum class GamepadHandleKind : uint8_t { + GamepadPlatformManager, + VR, +}; + +class GamepadHandle { + public: + // Allow handle to be passed around as a simple object + GamepadHandle() = default; + GamepadHandle(const GamepadHandle&) = default; + GamepadHandle& operator=(const GamepadHandle&) = default; + + // Helps code know which manager to send requests to + GamepadHandleKind GetKind() const; + + // Define operators so the handle can compared and stored in maps + friend bool operator==(const GamepadHandle& a, const GamepadHandle& b); + friend bool operator!=(const GamepadHandle& a, const GamepadHandle& b); + friend bool operator<(const GamepadHandle& a, const GamepadHandle& b); + + PLDHashNumber Hash() const; + + private: + explicit GamepadHandle(uint32_t aValue, GamepadHandleKind aKind); + uint32_t GetValue() const { return mValue; } + + uint32_t mValue{0}; + GamepadHandleKind mKind{GamepadHandleKind::GamepadPlatformManager}; + + // These are the classes that are "gamepad managers". They are allowed to + // create new handles and inspect their actual value + friend class mozilla::dom::GamepadPlatformService; + friend class mozilla::dom::GamepadServiceTest; + friend class mozilla::dom::XRInputSource; + friend class mozilla::gfx::VRDisplayClient; + friend class mozilla::gfx::VRManager; + + // Allow IPDL to serialize us + friend struct IPC::ParamTraits; +}; + +static_assert(std::is_trivially_copyable::value, + "GamepadHandle must be trivially copyable"); + +} // namespace mozilla::dom + +#endif // mozilla_dom_gamepad_GamepadHandle_h -- cgit v1.2.3