recovery: ui: Minor cleanup for touch code
* Better naming for some touch vars and funcs
* Introduce Point class
Change-Id: Idfcab54ac356face52efd69fdfdc0a6f4633a3f3
diff --git a/minui/events.cpp b/minui/events.cpp
index b307a49..b2fe61c 100644
--- a/minui/events.cpp
+++ b/minui/events.cpp
@@ -325,7 +325,7 @@
return 0;
}
-void ev_iterate_available_keys(const std::function<void(int)>& f) {
+void ev_iterate_available_keys(const std::function<void(int)>& 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 @@
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<void(int)>& action) {
+void ev_iterate_touch_inputs(const std::function<void(int)>& 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 @@
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 6a71ad3..8176f70 100644
--- a/minui/include/minui/minui.h
+++ b/minui/include/minui/minui.h
@@ -172,8 +172,8 @@
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<void(int)>& f);
-void ev_iterate_touch_inputs(const std::function<void(int)>& action);
+void ev_iterate_available_keys(const std::function<void(int)>& key_detected);
+void ev_iterate_touch_inputs(const std::function<void(int)>& 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 04db868..23da7f3 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 @@
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 @@
// 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 5e35adf..6b97ab5 100644
--- a/recovery_ui/ui.cpp
+++ b/recovery_ui/ui.cpp
@@ -218,12 +218,12 @@
// 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 +290,11 @@
// 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 +330,12 @@
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;