GamepadHandle.h (2821B)
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/. */ 6 7 // This file defines a strongly-typed opaque gamepad handle 8 // 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 13 // do it differently. 14 15 #ifndef mozilla_dom_gamepad_GamepadHandle_h 16 #define mozilla_dom_gamepad_GamepadHandle_h 17 18 #include <cinttypes> 19 #include <type_traits> 20 21 #include "PLDHashTable.h" 22 23 namespace IPC { 24 25 template <class> 26 struct ParamTraits; 27 28 } // namespace IPC 29 30 namespace mozilla::gfx { 31 32 class VRDisplayClient; 33 class VRManager; 34 35 } // namespace mozilla::gfx 36 37 namespace mozilla::dom { 38 39 class GamepadPlatformService; 40 class GamepadServiceTest; 41 class XRInputSource; 42 43 // The "kind" of a gamepad handle is based on which provider created it 44 enum class GamepadHandleKind : uint8_t { 45 GamepadPlatformManager, 46 VR, 47 }; 48 49 class GamepadHandle { 50 public: 51 // Allow handle to be passed around as a simple object 52 GamepadHandle() = default; 53 GamepadHandle(const GamepadHandle&) = default; 54 GamepadHandle& operator=(const GamepadHandle&) = default; 55 56 // Helps code know which manager to send requests to 57 GamepadHandleKind GetKind() const; 58 59 // Define operators so the handle can compared and stored in maps 60 friend bool operator==(const GamepadHandle& a, const GamepadHandle& b); 61 friend bool operator!=(const GamepadHandle& a, const GamepadHandle& b); 62 friend bool operator<(const GamepadHandle& a, const GamepadHandle& b); 63 64 PLDHashNumber Hash() const; 65 66 private: 67 explicit GamepadHandle(uint32_t aValue, GamepadHandleKind aKind); 68 uint32_t GetValue() const { return mValue; } 69 70 uint32_t mValue{0}; 71 GamepadHandleKind mKind{GamepadHandleKind::GamepadPlatformManager}; 72 73 // These are the classes that are "gamepad managers". They are allowed to 74 // create new handles and inspect their actual value 75 friend class mozilla::dom::GamepadPlatformService; 76 friend class mozilla::dom::GamepadServiceTest; 77 friend class mozilla::dom::XRInputSource; 78 friend class mozilla::gfx::VRDisplayClient; 79 friend class mozilla::gfx::VRManager; 80 81 // Allow IPDL to serialize us 82 friend struct IPC::ParamTraits<mozilla::dom::GamepadHandle>; 83 }; 84 85 static_assert(std::is_trivially_copyable<GamepadHandle>::value, 86 "GamepadHandle must be trivially copyable"); 87 88 } // namespace mozilla::dom 89 90 #endif // mozilla_dom_gamepad_GamepadHandle_h