gamepad_platform_data_fetcher_mac.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Copyright (c) 2012 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_GAMEPAD_PLATFORM_DATA_FETCHER_MAC_H_
  5. #define DEVICE_GAMEPAD_GAMEPAD_PLATFORM_DATA_FETCHER_MAC_H_
  6. #include <CoreFoundation/CoreFoundation.h>
  7. #include <IOKit/hid/IOHIDManager.h>
  8. #include <stddef.h>
  9. #include <memory>
  10. #include "base/mac/scoped_cftyperef.h"
  11. #include "base/memory/scoped_refptr.h"
  12. #include "device/gamepad/gamepad_data_fetcher.h"
  13. #include "device/gamepad/public/cpp/gamepad.h"
  14. #include "device/gamepad/public/mojom/gamepad.mojom-forward.h"
  15. namespace base {
  16. class SequencedTaskRunner;
  17. } // namespace base
  18. namespace device {
  19. class GamepadDeviceMac;
  20. class GamepadPlatformDataFetcherMac : public GamepadDataFetcher {
  21. public:
  22. using Factory = GamepadDataFetcherFactoryImpl<GamepadPlatformDataFetcherMac,
  23. GAMEPAD_SOURCE_MAC_HID>;
  24. GamepadPlatformDataFetcherMac();
  25. GamepadPlatformDataFetcherMac(const GamepadPlatformDataFetcherMac&) = delete;
  26. GamepadPlatformDataFetcherMac& operator=(
  27. const GamepadPlatformDataFetcherMac&) = delete;
  28. ~GamepadPlatformDataFetcherMac() override;
  29. // GamepadDataFetcher public implementation.
  30. GamepadSource source() override;
  31. void GetGamepadData(bool devices_changed_hint) override;
  32. void PauseHint(bool paused) override;
  33. void PlayEffect(int source_id,
  34. mojom::GamepadHapticEffectType,
  35. mojom::GamepadEffectParametersPtr,
  36. mojom::GamepadHapticsManager::PlayVibrationEffectOnceCallback,
  37. scoped_refptr<base::SequencedTaskRunner>) override;
  38. void ResetVibration(
  39. int source_id,
  40. mojom::GamepadHapticsManager::ResetVibrationActuatorCallback,
  41. scoped_refptr<base::SequencedTaskRunner>) override;
  42. private:
  43. static GamepadPlatformDataFetcherMac* InstanceFromContext(void* context);
  44. static void DeviceAddCallback(void* context,
  45. IOReturn result,
  46. void* sender,
  47. IOHIDDeviceRef ref);
  48. static void DeviceRemoveCallback(void* context,
  49. IOReturn result,
  50. void* sender,
  51. IOHIDDeviceRef ref);
  52. static void ValueChangedCallback(void* context,
  53. IOReturn result,
  54. void* sender,
  55. IOHIDValueRef ref);
  56. // GamepadDataFetcher private implementation.
  57. void OnAddedToProvider() override;
  58. // Returns the GamepadDeviceMac from |devices_| that has the given device
  59. // reference. Returns nullptr if the device is not in |devices_|.
  60. GamepadDeviceMac* GetGamepadFromHidDevice(IOHIDDeviceRef device);
  61. // Query device info for |device| and add it to |devices_| if it is a
  62. // gamepad.
  63. void DeviceAdd(IOHIDDeviceRef device);
  64. // Remove |device| from the set of connected devices.
  65. void DeviceRemove(IOHIDDeviceRef device);
  66. // Update the gamepad state for the button or axis referred to by |value|.
  67. void ValueChanged(IOHIDValueRef value);
  68. // Register for connection events and value change notifications for HID
  69. // devices.
  70. void RegisterForNotifications();
  71. // Unregister from connection events and value change notifications.
  72. void UnregisterFromNotifications();
  73. bool DisconnectUnrecognizedGamepad(int source_id) override;
  74. bool enabled_ = false;
  75. bool paused_ = false;
  76. base::ScopedCFTypeRef<IOHIDManagerRef> hid_manager_ref_;
  77. // A map of all devices using this data fetcher with the source_id as the key.
  78. std::unordered_map<int, std::unique_ptr<GamepadDeviceMac>> devices_;
  79. };
  80. } // namespace device
  81. #endif // DEVICE_GAMEPAD_GAMEPAD_PLATFORM_DATA_FETCHER_MAC_H_