blob: c9578bf00149ea1ff9820628b562517d2fcc34bc [file] [log] [blame]
Kevin Schoedel89af70b2017-03-03 18:11:37 -05001#ifndef ANDROID_DVR_VIRTUAL_TOUCHPAD_EVDEV_H
2#define ANDROID_DVR_VIRTUAL_TOUCHPAD_EVDEV_H
3
Kevin Schoedel89af70b2017-03-03 18:11:37 -05004#include "EvdevInjector.h"
Kevin Schoedel0108af72017-03-09 11:45:20 -05005#include "VirtualTouchpad.h"
Kevin Schoedel89af70b2017-03-03 18:11:37 -05006
7namespace android {
8namespace dvr {
9
10class EvdevInjector;
11
12// VirtualTouchpadEvdev implements a VirtualTouchpad by injecting evdev events.
13//
14class VirtualTouchpadEvdev : public VirtualTouchpad {
15 public:
Kevin Schoedelde1cdae2017-03-17 11:07:06 -040016 static std::unique_ptr<VirtualTouchpad> Create();
17 ~VirtualTouchpadEvdev() override {}
Kevin Schoedel89af70b2017-03-03 18:11:37 -050018
19 // VirtualTouchpad implementation:
Kevin Schoedel4b64dd42017-03-07 13:06:25 -050020 status_t Attach() override;
21 status_t Detach() override;
Kevin Schoedel3002b8a2017-03-06 14:34:39 -050022 status_t Touch(int touchpad, float x, float y, float pressure) override;
23 status_t ButtonState(int touchpad, int buttons) override;
Kevin Schoedeld8fccf02017-06-05 11:13:20 -040024 status_t Scroll(int touchpad, float x, float y) override;
Kevin Schoedel4b64dd42017-03-07 13:06:25 -050025 void dumpInternal(String8& result) override;
Kevin Schoedel89af70b2017-03-03 18:11:37 -050026
27 protected:
Kevin Schoedel0108af72017-03-09 11:45:20 -050028 static constexpr int kTouchpads = 2;
29
Kevin Schoedel89af70b2017-03-03 18:11:37 -050030 VirtualTouchpadEvdev() {}
Kevin Schoedel0108af72017-03-09 11:45:20 -050031 void Reset();
Kevin Schoedel89af70b2017-03-03 18:11:37 -050032
Kevin Schoedel0108af72017-03-09 11:45:20 -050033 // Must be called only between construction (or Detach()) and Attach().
34 inline void SetEvdevInjectorForTesting(int touchpad,
35 EvdevInjector* injector) {
36 touchpad_[touchpad].injector = injector;
Kevin Schoedel89af70b2017-03-03 18:11:37 -050037 }
38
39 private:
Kevin Schoedel0108af72017-03-09 11:45:20 -050040 // Per-touchpad state.
41 struct Touchpad {
42 // Except for testing, the |EvdevInjector| used to inject evdev events.
43 std::unique_ptr<EvdevInjector> owned_injector;
Kevin Schoedel89af70b2017-03-03 18:11:37 -050044
Kevin Schoedel0108af72017-03-09 11:45:20 -050045 // Active pointer to |owned_injector_| or to a testing injector.
46 EvdevInjector* injector = nullptr;
Kevin Schoedel89af70b2017-03-03 18:11:37 -050047
Kevin Schoedel0108af72017-03-09 11:45:20 -050048 // Previous (x, y) position in device space, to suppress redundant events.
49 int32_t last_device_x;
50 int32_t last_device_y;
Kevin Schoedel89af70b2017-03-03 18:11:37 -050051
Kevin Schoedel0108af72017-03-09 11:45:20 -050052 // Records current touch state (0=up 1=down) in bit 0, and previous state
53 // in bit 1, to track transitions.
54 int touches;
Kevin Schoedel89af70b2017-03-03 18:11:37 -050055
Kevin Schoedel0108af72017-03-09 11:45:20 -050056 // Previous injected button state, to detect changes.
57 int32_t last_motion_event_buttons;
58 };
59 Touchpad touchpad_[kTouchpads];
Kevin Schoedel89af70b2017-03-03 18:11:37 -050060
61 VirtualTouchpadEvdev(const VirtualTouchpadEvdev&) = delete;
62 void operator=(const VirtualTouchpadEvdev&) = delete;
63};
64
65} // namespace dvr
66} // namespace android
67
68#endif // ANDROID_DVR_VIRTUAL_TOUCHPAD_EVDEV_H