Line data Source code
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 file,
5 : * You can obtain one at http://mozilla.org/MPL/2.0/. */
6 :
7 : #ifndef mozilla_dom_GamepadPlatformService_h_
8 : #define mozilla_dom_GamepadPlatformService_h_
9 :
10 : #include "mozilla/dom/GamepadBinding.h"
11 :
12 : #include "mozilla/Mutex.h"
13 : #include "mozilla/StaticPtr.h"
14 :
15 : namespace mozilla {
16 : namespace dom {
17 :
18 : class GamepadEventChannelParent;
19 :
20 : // Platform Service for building and transmitting IPDL messages
21 : // through the HAL sandbox. Used by platform specific
22 : // Gamepad implementations
23 : //
24 : // This class can be accessed by the following 2 threads :
25 : // 1. Background thread:
26 : // This thread takes charge of IPDL communications
27 : // between here and DOM side
28 : //
29 : // 2. Monitor Thread:
30 : // This thread is populated in platform-dependent backends, which
31 : // is in charge of processing gamepad hardware events from OS
32 : class GamepadPlatformService final
33 : {
34 0 : NS_INLINE_DECL_THREADSAFE_REFCOUNTING(GamepadPlatformService)
35 : public:
36 : //Get the singleton service
37 : static already_AddRefed<GamepadPlatformService> GetParentService();
38 :
39 : // Add a gamepad to the list of known gamepads, and return its index.
40 : uint32_t AddGamepad(const char* aID, GamepadMappingType aMapping,
41 : GamepadHand aHand, uint32_t aNumButtons,
42 : uint32_t aNumAxes, uint32_t aNumHaptics);
43 : // Remove the gamepad at |aIndex| from the list of known gamepads.
44 : void RemoveGamepad(uint32_t aIndex);
45 :
46 : // Update the state of |aButton| for the gamepad at |aIndex| for all
47 : // windows that are listening and visible, and fire one of
48 : // a gamepadbutton{up,down} event at them as well.
49 : // aPressed is used for digital buttons, aTouched is for detecting touched
50 : // events, aValue is for analog buttons.
51 : void NewButtonEvent(uint32_t aIndex, uint32_t aButton, bool aPressed,
52 : bool aTouched, double aValue);
53 : // When only a digital button is available the value will be synthesized.
54 : void NewButtonEvent(uint32_t aIndex, uint32_t aButton, bool aPressed);
55 : // When only a digital button are available the value will be synthesized.
56 : void NewButtonEvent(uint32_t aIndex, uint32_t aButton, bool aPressed,
57 : bool aTouched);
58 :
59 : // Update the state of |aAxis| for the gamepad at |aIndex| for all
60 : // windows that are listening and visible, and fire a gamepadaxismove
61 : // event at them as well.
62 : void NewAxisMoveEvent(uint32_t aIndex, uint32_t aAxis, double aValue);
63 : // Update the state of |aState| for the gamepad at |aIndex| for all
64 : // windows that are listening and visible.
65 : void NewPoseEvent(uint32_t aIndex, const GamepadPoseState& aState);
66 :
67 : // When shutting down the platform communications for gamepad, also reset the
68 : // indexes.
69 : void ResetGamepadIndexes();
70 :
71 : //Add IPDL parent instance
72 : void AddChannelParent(GamepadEventChannelParent* aParent);
73 :
74 : //Remove IPDL parent instance
75 : void RemoveChannelParent(GamepadEventChannelParent* aParent);
76 :
77 : bool HasGamepadListeners();
78 :
79 : void MaybeShutdown();
80 :
81 : private:
82 : GamepadPlatformService();
83 : ~GamepadPlatformService();
84 : template<class T> void NotifyGamepadChange(const T& aInfo);
85 :
86 : // Flush all pending events buffered in mPendingEvents, must be called
87 : // with mMutex held
88 : void FlushPendingEvents();
89 : void Cleanup();
90 :
91 : // mGamepadIndex can only be accessed by monitor thread
92 : uint32_t mGamepadIndex;
93 :
94 : // mChannelParents stores all the GamepadEventChannelParent instances
95 : // which may be accessed by both background thread and monitor thread
96 : // simultaneously, so we have a mutex to prevent race condition
97 : nsTArray<RefPtr<GamepadEventChannelParent>> mChannelParents;
98 :
99 : // This mutex protects mChannelParents from race condition
100 : // between background and monitor thread
101 : Mutex mMutex;
102 :
103 : // In mochitest, it is possible that the test Events is synthesized
104 : // before GamepadEventChannel created, we need to buffer all events
105 : // until the channel is created if that happens.
106 : nsTArray<GamepadChangeEvent> mPendingEvents;
107 : };
108 :
109 : } // namespace dom
110 : } // namespace mozilla
111 :
112 : #endif
|