1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 // This file defines a strongly-typed opaque gamepad handle
9 // The handle is designed to be copied around and passed over IPC. It keeps
10 // track of which "provider" created the handle so it can ensure that
11 // providers are never mixed. It also allows each provider to have its own
12 // algorithm for generating gamepad IDs, since the VR and Platform services
15 #ifndef mozilla_dom_gamepad_GamepadHandle_h
16 #define mozilla_dom_gamepad_GamepadHandle_h
18 #include "PLDHashTable.h"
19 #include <type_traits>
29 namespace mozilla::gfx
{
31 class VRDisplayClient
;
34 } // namespace mozilla::gfx
36 namespace mozilla::dom
{
38 class GamepadPlatformService
;
39 class GamepadServiceTest
;
42 // The "kind" of a gamepad handle is based on which provider created it
43 enum class GamepadHandleKind
: uint8_t {
44 GamepadPlatformManager
,
50 // Allow handle to be passed around as a simple object
51 GamepadHandle() = default;
52 GamepadHandle(const GamepadHandle
&) = default;
53 GamepadHandle
& operator=(const GamepadHandle
&) = default;
55 // Helps code know which manager to send requests to
56 GamepadHandleKind
GetKind() const;
58 // Define operators so the handle can compared and stored in maps
59 friend bool operator==(const GamepadHandle
& a
, const GamepadHandle
& b
);
60 friend bool operator!=(const GamepadHandle
& a
, const GamepadHandle
& b
);
61 friend bool operator<(const GamepadHandle
& a
, const GamepadHandle
& b
);
63 PLDHashNumber
Hash() const;
66 explicit GamepadHandle(uint32_t aValue
, GamepadHandleKind aKind
);
67 uint32_t GetValue() const { return mValue
; }
70 GamepadHandleKind mKind
{GamepadHandleKind::GamepadPlatformManager
};
72 // These are the classes that are "gamepad managers". They are allowed to
73 // create new handles and inspect their actual value
74 friend class mozilla::dom::GamepadPlatformService
;
75 friend class mozilla::dom::GamepadServiceTest
;
76 friend class mozilla::dom::XRInputSource
;
77 friend class mozilla::gfx::VRDisplayClient
;
78 friend class mozilla::gfx::VRManager
;
80 // Allow IPDL to serialize us
81 friend struct IPC::ParamTraits
<mozilla::dom::GamepadHandle
>;
84 static_assert(std::is_trivially_copyable
<GamepadHandle
>::value
,
85 "GamepadHandle must be trivially copyable");
87 } // namespace mozilla::dom
89 #endif // mozilla_dom_gamepad_GamepadHandle_h