GamepadPlatformService.h 3.7 KB

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