blob: dbaca9a17e3acc1573f63a2b2d54b684076a8514 [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
4#include <memory>
5
Kevin Schoedel89af70b2017-03-03 18:11:37 -05006#include "EvdevInjector.h"
Kevin Schoedel0108af72017-03-09 11:45:20 -05007#include "VirtualTouchpad.h"
Kevin Schoedel89af70b2017-03-03 18:11:37 -05008
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:
Kevin Schoedel4b64dd42017-03-07 13:06:25 -050021 status_t Attach() override;
22 status_t Detach() override;
Kevin Schoedel3002b8a2017-03-06 14:34:39 -050023 status_t Touch(int touchpad, float x, float y, float pressure) override;
24 status_t ButtonState(int touchpad, int buttons) 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 Schoedel3002b8a2017-03-06 14:34:39 -050031 ~VirtualTouchpadEvdev() override {}
Kevin Schoedel0108af72017-03-09 11:45:20 -050032 void Reset();
Kevin Schoedel89af70b2017-03-03 18:11:37 -050033
Kevin Schoedel0108af72017-03-09 11:45:20 -050034 // Must be called only between construction (or Detach()) and Attach().
35 inline void SetEvdevInjectorForTesting(int touchpad,
36 EvdevInjector* injector) {
37 touchpad_[touchpad].injector = injector;
Kevin Schoedel89af70b2017-03-03 18:11:37 -050038 }
39
40 private:
Kevin Schoedel0108af72017-03-09 11:45:20 -050041 // Per-touchpad state.
42 struct Touchpad {
43 // Except for testing, the |EvdevInjector| used to inject evdev events.
44 std::unique_ptr<EvdevInjector> owned_injector;
Kevin Schoedel89af70b2017-03-03 18:11:37 -050045
Kevin Schoedel0108af72017-03-09 11:45:20 -050046 // Active pointer to |owned_injector_| or to a testing injector.
47 EvdevInjector* injector = nullptr;
Kevin Schoedel89af70b2017-03-03 18:11:37 -050048
Kevin Schoedel0108af72017-03-09 11:45:20 -050049 // Previous (x, y) position in device space, to suppress redundant events.
50 int32_t last_device_x;
51 int32_t last_device_y;
Kevin Schoedel89af70b2017-03-03 18:11:37 -050052
Kevin Schoedel0108af72017-03-09 11:45:20 -050053 // Records current touch state (0=up 1=down) in bit 0, and previous state
54 // in bit 1, to track transitions.
55 int touches;
Kevin Schoedel89af70b2017-03-03 18:11:37 -050056
Kevin Schoedel0108af72017-03-09 11:45:20 -050057 // Previous injected button state, to detect changes.
58 int32_t last_motion_event_buttons;
59 };
60 Touchpad touchpad_[kTouchpads];
Kevin Schoedel89af70b2017-03-03 18:11:37 -050061
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