Kevin Schoedel | 89af70b | 2017-03-03 18:11:37 -0500 | [diff] [blame] | 1 | #ifndef ANDROID_DVR_VIRTUAL_TOUCHPAD_EVDEV_H |
| 2 | #define ANDROID_DVR_VIRTUAL_TOUCHPAD_EVDEV_H |
| 3 | |
| 4 | #include <memory> |
| 5 | |
Kevin Schoedel | 89af70b | 2017-03-03 18:11:37 -0500 | [diff] [blame] | 6 | #include "EvdevInjector.h" |
Kevin Schoedel | 0108af7 | 2017-03-09 11:45:20 -0500 | [diff] [blame] | 7 | #include "VirtualTouchpad.h" |
Kevin Schoedel | 89af70b | 2017-03-03 18:11:37 -0500 | [diff] [blame] | 8 | |
| 9 | namespace android { |
| 10 | namespace dvr { |
| 11 | |
| 12 | class EvdevInjector; |
| 13 | |
| 14 | // VirtualTouchpadEvdev implements a VirtualTouchpad by injecting evdev events. |
| 15 | // |
| 16 | class VirtualTouchpadEvdev : public VirtualTouchpad { |
| 17 | public: |
| 18 | static sp<VirtualTouchpad> Create(); |
| 19 | |
| 20 | // VirtualTouchpad implementation: |
Kevin Schoedel | 4b64dd4 | 2017-03-07 13:06:25 -0500 | [diff] [blame] | 21 | status_t Attach() override; |
| 22 | status_t Detach() override; |
Kevin Schoedel | 3002b8a | 2017-03-06 14:34:39 -0500 | [diff] [blame] | 23 | status_t Touch(int touchpad, float x, float y, float pressure) override; |
| 24 | status_t ButtonState(int touchpad, int buttons) override; |
Kevin Schoedel | 4b64dd4 | 2017-03-07 13:06:25 -0500 | [diff] [blame] | 25 | void dumpInternal(String8& result) override; |
Kevin Schoedel | 89af70b | 2017-03-03 18:11:37 -0500 | [diff] [blame] | 26 | |
| 27 | protected: |
Kevin Schoedel | 0108af7 | 2017-03-09 11:45:20 -0500 | [diff] [blame] | 28 | static constexpr int kTouchpads = 2; |
| 29 | |
Kevin Schoedel | 89af70b | 2017-03-03 18:11:37 -0500 | [diff] [blame] | 30 | VirtualTouchpadEvdev() {} |
Kevin Schoedel | 3002b8a | 2017-03-06 14:34:39 -0500 | [diff] [blame] | 31 | ~VirtualTouchpadEvdev() override {} |
Kevin Schoedel | 0108af7 | 2017-03-09 11:45:20 -0500 | [diff] [blame] | 32 | void Reset(); |
Kevin Schoedel | 89af70b | 2017-03-03 18:11:37 -0500 | [diff] [blame] | 33 | |
Kevin Schoedel | 0108af7 | 2017-03-09 11:45:20 -0500 | [diff] [blame] | 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; |
Kevin Schoedel | 89af70b | 2017-03-03 18:11:37 -0500 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | private: |
Kevin Schoedel | 0108af7 | 2017-03-09 11:45:20 -0500 | [diff] [blame] | 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; |
Kevin Schoedel | 89af70b | 2017-03-03 18:11:37 -0500 | [diff] [blame] | 45 | |
Kevin Schoedel | 0108af7 | 2017-03-09 11:45:20 -0500 | [diff] [blame] | 46 | // Active pointer to |owned_injector_| or to a testing injector. |
| 47 | EvdevInjector* injector = nullptr; |
Kevin Schoedel | 89af70b | 2017-03-03 18:11:37 -0500 | [diff] [blame] | 48 | |
Kevin Schoedel | 0108af7 | 2017-03-09 11:45:20 -0500 | [diff] [blame] | 49 | // Previous (x, y) position in device space, to suppress redundant events. |
| 50 | int32_t last_device_x; |
| 51 | int32_t last_device_y; |
Kevin Schoedel | 89af70b | 2017-03-03 18:11:37 -0500 | [diff] [blame] | 52 | |
Kevin Schoedel | 0108af7 | 2017-03-09 11:45:20 -0500 | [diff] [blame] | 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; |
Kevin Schoedel | 89af70b | 2017-03-03 18:11:37 -0500 | [diff] [blame] | 56 | |
Kevin Schoedel | 0108af7 | 2017-03-09 11:45:20 -0500 | [diff] [blame] | 57 | // Previous injected button state, to detect changes. |
| 58 | int32_t last_motion_event_buttons; |
| 59 | }; |
| 60 | Touchpad touchpad_[kTouchpads]; |
Kevin Schoedel | 89af70b | 2017-03-03 18:11:37 -0500 | [diff] [blame] | 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 |