1 // Copyright 2018 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 
5 #ifndef DEVICE_GAMEPAD_DUALSHOCK4_CONTROLLER_H_
6 #define DEVICE_GAMEPAD_DUALSHOCK4_CONTROLLER_H_
7 
8 #include <stdint.h>
9 #include <memory>
10 
11 #include "base/memory/weak_ptr.h"
12 #include "device/gamepad/abstract_haptic_gamepad.h"
13 #include "device/gamepad/gamepad_export.h"
14 #include "device/gamepad/gamepad_standard_mappings.h"
15 
16 namespace device {
17 
18 class HidWriter;
19 
20 class DEVICE_GAMEPAD_EXPORT Dualshock4Controller final
21     : public AbstractHapticGamepad {
22  public:
23   Dualshock4Controller(uint16_t vendor_id,
24                        uint16_t product_id,
25                        GamepadBusType bus_type,
26                        std::unique_ptr<HidWriter> hid_writer);
27   ~Dualshock4Controller() override;
28 
29   // Returns true if |vendor_id| and |product_id| match the device IDs for
30   // a Dualshock4 gamepad.
31   static bool IsDualshock4(uint16_t vendor_id, uint16_t product_id);
32 
33   // Detects the transport in use (USB or Bluetooth) given the bcdVersion value
34   // reported by the device. Used on Windows where the platform HID API does not
35   // expose the transport type.
36   static GamepadBusType BusTypeFromVersionNumber(uint32_t version_number);
37 
38   // Extracts gamepad inputs from an input report and updates the gamepad state
39   // in |pad|. |report_id| is first byte of the report, |report| contains the
40   // remaining bytes. Returns true if |pad| was modified.
41   bool ProcessInputReport(uint8_t report_id,
42                           base::span<const uint8_t> report,
43                           Gamepad* pad);
44 
45   // AbstractHapticGamepad public implementation.
46   void SetVibration(double strong_magnitude, double weak_magnitude) override;
47   base::WeakPtr<AbstractHapticGamepad> GetWeakPtr() override;
48 
49  private:
50   // AbstractHapticGamepad private implementation.
51   void DoShutdown() override;
52 
53   // Sends a vibration output report suitable for a USB-connected Dualshock4.
54   void SetVibrationUsb(double strong_magnitude, double weak_magnitude);
55 
56   // Sends a vibration output report suitable for a Bluetooth-connected
57   // Dualshock4.
58   void SetVibrationBluetooth(double strong_magnitude, double weak_magnitude);
59 
60   uint16_t vendor_id_;
61   uint16_t product_id_;
62   GamepadBusType bus_type_;
63   std::unique_ptr<HidWriter> writer_;
64   base::WeakPtrFactory<Dualshock4Controller> weak_factory_{this};
65 };
66 
67 }  // namespace device
68 
69 #endif  // DEVICE_GAMEPAD_DUALSHOCK4_CONTROLLER_H_
70