blob: 2fb8ff34b0a31c5d5a252b43ec792ca4ce7c11f9 [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 Schoedel4b64dd42017-03-07 13:06:25 -050024 void dumpInternal(String8& result) override;
Kevin Schoedel89af70b2017-03-03 18:11:37 -050025
26 protected:
Kevin Schoedel0108af72017-03-09 11:45:20 -050027 static constexpr int kTouchpads = 2;
28
Kevin Schoedel89af70b2017-03-03 18:11:37 -050029 VirtualTouchpadEvdev() {}
Kevin Schoedel0108af72017-03-09 11:45:20 -050030 void Reset();
Kevin Schoedel89af70b2017-03-03 18:11:37 -050031
Kevin Schoedel0108af72017-03-09 11:45:20 -050032 // Must be called only between construction (or Detach()) and Attach().
33 inline void SetEvdevInjectorForTesting(int touchpad,
34 EvdevInjector* injector) {
35 touchpad_[touchpad].injector = injector;
Kevin Schoedel89af70b2017-03-03 18:11:37 -050036 }
37
38 private:
Kevin Schoedel0108af72017-03-09 11:45:20 -050039 // Per-touchpad state.
40 struct Touchpad {
41 // Except for testing, the |EvdevInjector| used to inject evdev events.
42 std::unique_ptr<EvdevInjector> owned_injector;
Kevin Schoedel89af70b2017-03-03 18:11:37 -050043
Kevin Schoedel0108af72017-03-09 11:45:20 -050044 // Active pointer to |owned_injector_| or to a testing injector.
45 EvdevInjector* injector = nullptr;
Kevin Schoedel89af70b2017-03-03 18:11:37 -050046
Kevin Schoedel0108af72017-03-09 11:45:20 -050047 // Previous (x, y) position in device space, to suppress redundant events.
48 int32_t last_device_x;
49 int32_t last_device_y;
Kevin Schoedel89af70b2017-03-03 18:11:37 -050050
Kevin Schoedel0108af72017-03-09 11:45:20 -050051 // Records current touch state (0=up 1=down) in bit 0, and previous state
52 // in bit 1, to track transitions.
53 int touches;
Kevin Schoedel89af70b2017-03-03 18:11:37 -050054
Kevin Schoedel0108af72017-03-09 11:45:20 -050055 // Previous injected button state, to detect changes.
56 int32_t last_motion_event_buttons;
57 };
58 Touchpad touchpad_[kTouchpads];
Kevin Schoedel89af70b2017-03-03 18:11:37 -050059
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