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