gamepad_platform_data_fetcher_linux.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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_LINUX_H_
  5. #define DEVICE_GAMEPAD_GAMEPAD_PLATFORM_DATA_FETCHER_LINUX_H_
  6. #include <stddef.h>
  7. #include <memory>
  8. #include <string>
  9. #include "base/compiler_specific.h"
  10. #include "device/gamepad/gamepad_data_fetcher.h"
  11. #include "device/gamepad/gamepad_device_linux.h"
  12. #include "device/gamepad/public/cpp/gamepads.h"
  13. #include "device/gamepad/udev_gamepad_linux.h"
  14. #include "device/udev_linux/udev_watcher.h"
  15. extern "C" {
  16. struct udev_device;
  17. }
  18. namespace device {
  19. class DEVICE_GAMEPAD_EXPORT GamepadPlatformDataFetcherLinux
  20. : public GamepadDataFetcher,
  21. public UdevWatcher::Observer {
  22. public:
  23. class Factory : public GamepadDataFetcherFactory {
  24. public:
  25. Factory(scoped_refptr<base::SequencedTaskRunner> dbus_runner);
  26. ~Factory() override;
  27. std::unique_ptr<GamepadDataFetcher> CreateDataFetcher() override;
  28. GamepadSource source() override;
  29. static GamepadSource static_source();
  30. private:
  31. scoped_refptr<base::SequencedTaskRunner> dbus_runner_;
  32. };
  33. GamepadPlatformDataFetcherLinux(
  34. scoped_refptr<base::SequencedTaskRunner> dbus_runner);
  35. GamepadPlatformDataFetcherLinux(const GamepadPlatformDataFetcherLinux&) =
  36. delete;
  37. GamepadPlatformDataFetcherLinux& operator=(
  38. const GamepadPlatformDataFetcherLinux&) = delete;
  39. ~GamepadPlatformDataFetcherLinux() override;
  40. GamepadSource source() override;
  41. // GamepadDataFetcher implementation.
  42. void GetGamepadData(bool devices_changed_hint) override;
  43. bool DisconnectUnrecognizedGamepad(int source_id) override;
  44. void PlayEffect(int pad_index,
  45. mojom::GamepadHapticEffectType,
  46. mojom::GamepadEffectParametersPtr,
  47. mojom::GamepadHapticsManager::PlayVibrationEffectOnceCallback,
  48. scoped_refptr<base::SequencedTaskRunner>) override;
  49. void ResetVibration(
  50. int pad_index,
  51. mojom::GamepadHapticsManager::ResetVibrationActuatorCallback,
  52. scoped_refptr<base::SequencedTaskRunner>) override;
  53. private:
  54. void OnAddedToProvider() override;
  55. void RefreshDevice(udev_device* dev);
  56. void RefreshJoydevDevice(udev_device* dev, const UdevGamepadLinux& pad_info);
  57. void RefreshEvdevDevice(udev_device* dev, const UdevGamepadLinux& pad_info);
  58. void RefreshHidrawDevice(udev_device* dev, const UdevGamepadLinux& pad_info);
  59. void ReadDeviceData(size_t index);
  60. void OnHidrawDeviceOpened(GamepadDeviceLinux* device);
  61. GamepadDeviceLinux* GetDeviceWithJoydevIndex(int joydev_index);
  62. GamepadDeviceLinux* GetOrCreateMatchingDevice(
  63. const UdevGamepadLinux& pad_info);
  64. void RemoveDevice(GamepadDeviceLinux* device);
  65. // UdevWatcher::Observer overrides
  66. void OnDeviceAdded(ScopedUdevDevicePtr device) override;
  67. void OnDeviceRemoved(ScopedUdevDevicePtr device) override;
  68. void OnDeviceChanged(ScopedUdevDevicePtr device) override;
  69. std::unordered_set<std::unique_ptr<GamepadDeviceLinux>> devices_;
  70. std::unique_ptr<device::UdevWatcher> udev_watcher_;
  71. scoped_refptr<base::SequencedTaskRunner> dbus_runner_;
  72. base::WeakPtrFactory<GamepadPlatformDataFetcherLinux> weak_factory_{this};
  73. };
  74. } // namespace device
  75. #endif // DEVICE_GAMEPAD_GAMEPAD_PLATFORM_DATA_FETCHER_LINUX_H_