Skip to content

Commit

Permalink
recovery: ui: Minor cleanup for touch code
Browse files Browse the repository at this point in the history
 * Better naming for some touch vars and funcs

 * Introduce Point class

Change-Id: Idfcab54ac356face52efd69fdfdc0a6f4633a3f3
  • Loading branch information
tdm authored and xenxynon committed Dec 28, 2024
1 parent 9c5414f commit e51a9da
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 22 deletions.
8 changes: 4 additions & 4 deletions minui/events.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<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
Expand All @@ -348,13 +348,13 @@ void ev_iterate_available_keys(const std::function<void(int)>& 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<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
Expand All @@ -372,7 +372,7 @@ void ev_iterate_touch_inputs(const std::function<void(int)>& 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);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions minui/include/minui/minui.h
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,8 @@ using ev_set_sw_callback = std::function<int(int code, int value)>;
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);

Expand Down
54 changes: 49 additions & 5 deletions recovery_ui/include/recovery_ui/ui.h
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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_;
Expand Down
22 changes: 11 additions & 11 deletions recovery_ui/ui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down

0 comments on commit e51a9da

Please sign in to comment.