VirtualTouchpadEvdev.h revision de1cdaebc9b8fdbc5348e6c07f849b74bacc485d
1#ifndef ANDROID_DVR_VIRTUAL_TOUCHPAD_EVDEV_H
2#define ANDROID_DVR_VIRTUAL_TOUCHPAD_EVDEV_H
3
4#include "EvdevInjector.h"
5#include "VirtualTouchpad.h"
6
7namespace android {
8namespace dvr {
9
10class EvdevInjector;
11
12// VirtualTouchpadEvdev implements a VirtualTouchpad by injecting evdev events.
13//
14class VirtualTouchpadEvdev : public VirtualTouchpad {
15 public:
16  static std::unique_ptr<VirtualTouchpad> Create();
17  ~VirtualTouchpadEvdev() override {}
18
19  // VirtualTouchpad implementation:
20  status_t Attach() override;
21  status_t Detach() override;
22  status_t Touch(int touchpad, float x, float y, float pressure) override;
23  status_t ButtonState(int touchpad, int buttons) override;
24  void dumpInternal(String8& result) override;
25
26 protected:
27  static constexpr int kTouchpads = 2;
28
29  VirtualTouchpadEvdev() {}
30  void Reset();
31
32  // Must be called only between construction (or Detach()) and Attach().
33  inline void SetEvdevInjectorForTesting(int touchpad,
34                                         EvdevInjector* injector) {
35    touchpad_[touchpad].injector = injector;
36  }
37
38 private:
39  // Per-touchpad state.
40  struct Touchpad {
41    // Except for testing, the |EvdevInjector| used to inject evdev events.
42    std::unique_ptr<EvdevInjector> owned_injector;
43
44    // Active pointer to |owned_injector_| or to a testing injector.
45    EvdevInjector* injector = nullptr;
46
47    // Previous (x, y) position in device space, to suppress redundant events.
48    int32_t last_device_x;
49    int32_t last_device_y;
50
51    // Records current touch state (0=up 1=down) in bit 0, and previous state
52    // in bit 1, to track transitions.
53    int touches;
54
55    // Previous injected button state, to detect changes.
56    int32_t last_motion_event_buttons;
57  };
58  Touchpad touchpad_[kTouchpads];
59
60  VirtualTouchpadEvdev(const VirtualTouchpadEvdev&) = delete;
61  void operator=(const VirtualTouchpadEvdev&) = delete;
62};
63
64}  // namespace dvr
65}  // namespace android
66
67#endif  // ANDROID_DVR_VIRTUAL_TOUCHPAD_EVDEV_H
68