From e51a9dac46526aaa16237b20097fa214bdb5f538 Mon Sep 17 00:00:00 2001 From: Tom Marshall Date: Thu, 24 Aug 2017 12:57:27 +0000 Subject: [PATCH] recovery: ui: Minor cleanup for touch code * Better naming for some touch vars and funcs * Introduce Point class Change-Id: Idfcab54ac356face52efd69fdfdc0a6f4633a3f3 --- minui/events.cpp | 8 ++--- minui/include/minui/minui.h | 4 +-- recovery_ui/include/recovery_ui/ui.h | 54 +++++++++++++++++++++++++--- recovery_ui/ui.cpp | 22 ++++++------ 4 files changed, 66 insertions(+), 22 deletions(-) diff --git a/minui/events.cpp b/minui/events.cpp index b307a4977..b2fe61ced 100644 --- a/minui/events.cpp +++ b/minui/events.cpp @@ -325,7 +325,7 @@ int ev_sync_key_state(const ev_set_key_callback& set_key_cb) { return 0; } -void ev_iterate_available_keys(const std::function& f) { +void ev_iterate_available_keys(const std::function& key_detected) { // Use unsigned long to match ioctl's parameter type. unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)]; // NOLINT unsigned long key_bits[BITS_TO_LONGS(KEY_MAX)]; // NOLINT @@ -348,13 +348,13 @@ void ev_iterate_available_keys(const std::function& f) { for (int key_code = 0; key_code <= KEY_MAX; ++key_code) { if (test_bit(key_code, key_bits)) { - f(key_code); + key_detected(key_code); } } } } -void ev_iterate_touch_inputs(const std::function& action) { +void ev_iterate_touch_inputs(const std::function& key_detected) { for (size_t i = 0; i < g_ev_dev_count; ++i) { // Use unsigned long to match ioctl's parameter type. unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)] = {}; // NOLINT @@ -372,7 +372,7 @@ void ev_iterate_touch_inputs(const std::function& action) { for (int key_code = 0; key_code <= KEY_MAX; ++key_code) { if (test_bit(key_code, key_bits)) { - action(key_code); + key_detected(key_code); } } } diff --git a/minui/include/minui/minui.h b/minui/include/minui/minui.h index 6a71ad3fb..8176f708d 100644 --- a/minui/include/minui/minui.h +++ b/minui/include/minui/minui.h @@ -172,8 +172,8 @@ using ev_set_sw_callback = std::function; int ev_init(ev_callback input_cb, bool allow_touch_inputs = false); void ev_exit(); int ev_add_fd(android::base::unique_fd&& fd, ev_callback cb); -void ev_iterate_available_keys(const std::function& f); -void ev_iterate_touch_inputs(const std::function& action); +void ev_iterate_available_keys(const std::function& key_detected); +void ev_iterate_touch_inputs(const std::function& key_detected); int ev_sync_key_state(const ev_set_key_callback& set_key_cb); int ev_sync_sw_state(const ev_set_sw_callback& set_sw_cb); diff --git a/recovery_ui/include/recovery_ui/ui.h b/recovery_ui/include/recovery_ui/ui.h index 04db868a4..23da7f3ec 100644 --- a/recovery_ui/include/recovery_ui/ui.h +++ b/recovery_ui/include/recovery_ui/ui.h @@ -1,5 +1,6 @@ /* * Copyright (C) 2011 The Android Open Source Project + * Copyright (C) 2019 The LineageOS Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -29,6 +30,51 @@ static constexpr const char* DEFAULT_LOCALE = "en-US"; +/* + * Simple representation of a (x,y) coordinate with convenience operators + */ +class Point { + public: + Point() : x_(0), y_(0) {} + Point(int x, int y) : x_(x), y_(y) {} + int x() const { + return x_; + } + int y() const { + return y_; + } + void x(int x) { + x_ = x; + } + void y(int y) { + y_ = y; + } + + bool operator==(const Point& rhs) const { + return (x() == rhs.x() && y() == rhs.y()); + } + bool operator!=(const Point& rhs) const { + return !(*this == rhs); + } + + Point operator+(const Point& rhs) const { + Point tmp; + tmp.x_ = x_ + rhs.x_; + tmp.y_ = y_ + rhs.y_; + return tmp; + } + Point operator-(const Point& rhs) const { + Point tmp; + tmp.x_ = x_ - rhs.x_; + tmp.y_ = y_ - rhs.y_; + return tmp; + } + + private: + int x_; + int y_; +}; + // Abstract class for controlling the user interface during recovery. class RecoveryUI { public: @@ -227,7 +273,7 @@ class RecoveryUI { const int touch_high_threshold_; void OnKeyDetected(int key_code); - void OnTouchDetected(int dx, int dy); + void OnTouchEvent(); int OnInputEvent(int fd, uint32_t epevents); void ProcessKey(int key_code, int updown); void TimeKey(int key_code, int count); @@ -261,10 +307,8 @@ class RecoveryUI { // Touch event related variables. See the comments in RecoveryUI::OnInputEvent(). int touch_slot_; - int touch_X_; - int touch_Y_; - int touch_start_X_; - int touch_start_Y_; + Point touch_pos_; + Point touch_start_; bool touch_finger_down_; bool touch_swiping_; bool is_bootreason_recovery_ui_; diff --git a/recovery_ui/ui.cpp b/recovery_ui/ui.cpp index 5e35adf4e..18f1d019e 100644 --- a/recovery_ui/ui.cpp +++ b/recovery_ui/ui.cpp @@ -212,18 +212,19 @@ static SwipeDirection FlipSwipeDirection(SwipeDirection direction) { } } -void RecoveryUI::OnTouchDetected(int dx, int dy) { +void RecoveryUI::OnTouchEvent() { + Point delta = touch_pos_ - touch_start_; SwipeDirection direction; // We only consider a valid swipe if: // - the delta along one axis is below touch_low_threshold_; // - and the delta along the other axis is beyond touch_high_threshold_. - if (abs(dy) < touch_low_threshold_ && abs(dx) > touch_high_threshold_) { - direction = dx < 0 ? SwipeDirection::LEFT : SwipeDirection::RIGHT; - } else if (abs(dx) < touch_low_threshold_ && abs(dy) > touch_high_threshold_) { - direction = dy < 0 ? SwipeDirection::UP : SwipeDirection::DOWN; + if (abs(delta.y()) < touch_low_threshold_ && abs(delta.x()) > touch_high_threshold_) { + direction = delta.x() < 0 ? SwipeDirection::LEFT : SwipeDirection::RIGHT; + } else if (abs(delta.x()) < touch_low_threshold_ && abs(delta.y()) > touch_high_threshold_) { + direction = delta.y() < 0 ? SwipeDirection::UP : SwipeDirection::DOWN; } else { - LOG(DEBUG) << "Ignored " << dx << " " << dy << " (low: " << touch_low_threshold_ + LOG(DEBUG) << "Ignored " << delta.x() << " " << delta.y() << " (low: " << touch_low_threshold_ << ", high: " << touch_high_threshold_ << ")"; return; } @@ -290,12 +291,11 @@ int RecoveryUI::OnInputEvent(int fd, uint32_t epevents) { // There might be multiple SYN_REPORT events. We should only detect a swipe after lifting the // contact. if (touch_finger_down_ && !touch_swiping_) { - touch_start_X_ = touch_X_; - touch_start_Y_ = touch_Y_; + touch_start_ = touch_pos_; touch_swiping_ = true; } else if (!touch_finger_down_ && touch_swiping_) { touch_swiping_ = false; - OnTouchDetected(touch_X_ - touch_start_X_, touch_Y_ - touch_start_Y_); + OnTouchEvent(); } } return 0; @@ -331,12 +331,12 @@ int RecoveryUI::OnInputEvent(int fd, uint32_t epevents) { switch (ev.code) { case ABS_MT_POSITION_X: - touch_X_ = ev.value; + touch_pos_.x(ev.value); touch_finger_down_ = true; break; case ABS_MT_POSITION_Y: - touch_Y_ = ev.value; + touch_pos_.y(ev.value); touch_finger_down_ = true; break;