xbox_data_fetcher_mac.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // Copyright 2013 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef DEVICE_GAMEPAD_XBOX_DATA_FETCHER_MAC_H_
  5. #define DEVICE_GAMEPAD_XBOX_DATA_FETCHER_MAC_H_
  6. #include <stdint.h>
  7. #include <set>
  8. #include <vector>
  9. #include <IOKit/IOMessage.h>
  10. #include "base/containers/unique_ptr_adapters.h"
  11. #include "base/mac/scoped_ionotificationportref.h"
  12. #include "base/mac/scoped_ioobject.h"
  13. #include "base/memory/raw_ptr.h"
  14. #include "device/gamepad/gamepad_data_fetcher.h"
  15. #include "device/gamepad/xbox_controller_mac.h"
  16. namespace device {
  17. class XboxDataFetcher : public GamepadDataFetcher,
  18. public XboxControllerMac::Delegate {
  19. public:
  20. typedef GamepadDataFetcherFactoryImpl<XboxDataFetcher,
  21. GAMEPAD_SOURCE_MAC_XBOX>
  22. Factory;
  23. XboxDataFetcher();
  24. XboxDataFetcher(const XboxDataFetcher& entry) = delete;
  25. XboxDataFetcher& operator=(const XboxDataFetcher& entry) = delete;
  26. ~XboxDataFetcher() override;
  27. GamepadSource source() override;
  28. // GamepadDataFetcher overrides
  29. void GetGamepadData(bool devices_changed_hint) override;
  30. void PlayEffect(int source_id,
  31. mojom::GamepadHapticEffectType,
  32. mojom::GamepadEffectParametersPtr,
  33. mojom::GamepadHapticsManager::PlayVibrationEffectOnceCallback,
  34. scoped_refptr<base::SequencedTaskRunner>) override;
  35. void ResetVibration(
  36. int source_id,
  37. mojom::GamepadHapticsManager::ResetVibrationActuatorCallback,
  38. scoped_refptr<base::SequencedTaskRunner>) override;
  39. XboxControllerMac* ControllerForLocation(UInt32 location_id);
  40. private:
  41. struct PendingController {
  42. public:
  43. PendingController(XboxDataFetcher*, std::unique_ptr<XboxControllerMac>);
  44. PendingController(const PendingController& entry);
  45. PendingController& operator=(const PendingController& entry);
  46. ~PendingController();
  47. raw_ptr<XboxDataFetcher> fetcher;
  48. std::unique_ptr<XboxControllerMac> controller;
  49. base::mac::ScopedIOObject<io_iterator_t> notify;
  50. };
  51. static void DeviceAdded(void* context, io_iterator_t iterator);
  52. static void DeviceRemoved(void* context, io_iterator_t iterator);
  53. static void InterestCallback(void* context,
  54. io_service_t service,
  55. IOMessage message_type,
  56. void* message_argument);
  57. bool TryOpenDevice(io_service_t iterator);
  58. bool RegisterForNotifications();
  59. bool RegisterForDeviceNotifications(int vendor_id, int product_id);
  60. bool RegisterForInterestNotifications(io_service_t service,
  61. PendingController* pending);
  62. void PendingControllerBecameAvailable(io_service_t service,
  63. PendingController* pending);
  64. void UnregisterFromNotifications();
  65. void OnAddedToProvider() override;
  66. void AddController(XboxControllerMac* controller);
  67. void RemoveController(XboxControllerMac* controller);
  68. void RemoveControllerByLocationID(uint32_t id);
  69. // XboxControllerMac::Delegate methods.
  70. void XboxControllerGotData(XboxControllerMac* controller,
  71. const XboxControllerMac::Data& data) override;
  72. void XboxControllerGotGuideData(XboxControllerMac* controller,
  73. bool guide) override;
  74. void XboxControllerError(XboxControllerMac* controller) override;
  75. // The set of connected controllers.
  76. std::set<XboxControllerMac*> controllers_;
  77. // The set of enumerated controllers that received an exclusive access error
  78. // on opening the device. The data fetcher is notified when these devices
  79. // become available so we can try opening them again.
  80. std::set<std::unique_ptr<PendingController>, base::UniquePtrComparator>
  81. pending_controllers_;
  82. bool listening_ = false;
  83. // port_ owns source_, so this doesn't need to be a ScopedCFTypeRef, but we
  84. // do need to maintain a reference to it so we can invalidate it.
  85. CFRunLoopSourceRef source_ = nullptr;
  86. base::mac::ScopedIONotificationPortRef port_;
  87. // Iterators returned by calls to IOServiceAddMatchingNotification for
  88. // kIOFirstMatchNotification (connection) and kIOTerminatedNotification
  89. // (disconnection) events. These iterators are not referenced directly but
  90. // must be kept alive in order to continue to receive notifications.
  91. std::vector<base::mac::ScopedIOObject<io_iterator_t>> device_event_iterators_;
  92. };
  93. } // namespace device
  94. #endif // DEVICE_GAMEPAD_XBOX_DATA_FETCHER_MAC_H_