Skip to content

Commit

Permalink
refactor: 整理minitouch相关参数
Browse files Browse the repository at this point in the history
  • Loading branch information
MistEO committed Nov 28, 2023
1 parent aba3623 commit 1343aac
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 12 deletions.
8 changes: 6 additions & 2 deletions source/MaaAdbControlUnit/Input/MaatouchInput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,12 @@ bool MaatouchInput::press_key(int key)
return false;
}

bool ret = shell_handler_->write(MAA_FMT::format("k {} d\nc\n", key)) &&
shell_handler_->write(MAA_FMT::format("k {} u\nc\n", key));
// https://github.com/openstf/minitouch#writable-to-the-socket
static constexpr std::string_view kKeyDownFormat = "k {} d\nc\n";
static constexpr std::string_view kKeyUpFormat = "k {} u\nc\n";

bool ret = shell_handler_->write(MAA_FMT::format(kKeyDownFormat, key)) &&
shell_handler_->write(MAA_FMT::format(kKeyUpFormat, key));

if (!ret) {
LogError << "failed to write";
Expand Down
4 changes: 3 additions & 1 deletion source/MaaAdbControlUnit/Input/MinitouchInput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ bool MinitouchInput::set_wh(int swidth, int sheight, int orientation)
{
LogFunc << VAR(swidth) << VAR(sheight) << VAR(orientation);

shell_handler_ = invoke_app_->invoke_bin("-i");
// https://github.com/openstf/minitouch#running
static const std::string kMinitouchUseStdin = "-i";
shell_handler_ = invoke_app_->invoke_bin(kMinitouchUseStdin);
if (!shell_handler_) {
return false;
}
Expand Down
18 changes: 9 additions & 9 deletions source/MaaAdbControlUnit/Input/MtouchHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ bool MtouchHelper::click(int x, int y)

LogInfo << VAR(x) << VAR(y) << VAR(touch_x) << VAR(touch_y);

bool ret = shell_handler_->write(MAA_FMT::format("d {} {} {} {}\nc\n", 0, touch_x, touch_y, press_)) &&
shell_handler_->write(MAA_FMT::format("u {}\nc\n", 0));
bool ret = shell_handler_->write(MAA_FMT::format(kDownFormat, 0, touch_x, touch_y, press_)) &&
shell_handler_->write(MAA_FMT::format(kUpFormat, 0));

if (!ret) {
LogError << "failed to write";
Expand Down Expand Up @@ -130,7 +130,7 @@ bool MtouchHelper::swipe(int x1, int y1, int x2, int y2, int duration)
auto start = std::chrono::steady_clock::now();
auto now = start;
bool ret = true;
ret &= shell_handler_->write(MAA_FMT::format("d {} {} {} {}\nc\n", 0, touch_x1, touch_y1, press_));
ret &= shell_handler_->write(MAA_FMT::format(kDownFormat, 0, touch_x1, touch_y1, press_));
if (!ret) {
LogError << "write error";
return false;
Expand All @@ -147,19 +147,19 @@ bool MtouchHelper::swipe(int x1, int y1, int x2, int y2, int duration)
std::this_thread::sleep_until(now + delay);
now = std::chrono::steady_clock::now();

ret &= shell_handler_->write(MAA_FMT::format("m {} {} {} {}\nc\n", 0, tx, ty, press_));
ret &= shell_handler_->write(MAA_FMT::format(kMoveFormat, 0, tx, ty, press_));
if (!ret) {
LogWarn << "write error";
}
}

std::this_thread::sleep_until(now + delay);
now = std::chrono::steady_clock::now();
ret &= shell_handler_->write(MAA_FMT::format("m {} {} {} {}\nc\n", 0, touch_x2, touch_y2, press_));
ret &= shell_handler_->write(MAA_FMT::format(kMoveFormat, 0, touch_x2, touch_y2, press_));

std::this_thread::sleep_until(now + delay);
now = std::chrono::steady_clock::now();
ret &= shell_handler_->write(MAA_FMT::format("u {}\nc\n", 0));
ret &= shell_handler_->write(MAA_FMT::format(kUpFormat, 0));

if (!ret) {
LogError << "failed to write";
Expand All @@ -180,7 +180,7 @@ bool MtouchHelper::touch_down(int contact, int x, int y, int pressure)

LogInfo << VAR(contact) << VAR(x) << VAR(y) << VAR(touch_x) << VAR(touch_y);

bool ret = shell_handler_->write(MAA_FMT::format("d {} {} {} {}\nc\n", contact, touch_x, touch_y, pressure));
bool ret = shell_handler_->write(MAA_FMT::format(kDownFormat, contact, touch_x, touch_y, pressure));

if (!ret) {
LogError << "failed to write";
Expand All @@ -201,7 +201,7 @@ bool MtouchHelper::touch_move(int contact, int x, int y, int pressure)

LogInfo << VAR(contact) << VAR(x) << VAR(y) << VAR(touch_x) << VAR(touch_y);

bool ret = shell_handler_->write(MAA_FMT::format("m {} {} {} {}\nc\n", contact, touch_x, touch_y, pressure));
bool ret = shell_handler_->write(MAA_FMT::format(kMoveFormat, contact, touch_x, touch_y, pressure));

if (!ret) {
LogError << "failed to write";
Expand All @@ -220,7 +220,7 @@ bool MtouchHelper::touch_up(int contact)

LogInfo << VAR(contact);

bool ret = shell_handler_->write(MAA_FMT::format("u {}\nc\n", contact));
bool ret = shell_handler_->write(MAA_FMT::format(kUpFormat, contact));

if (!ret) {
LogError << "failed to write";
Expand Down
5 changes: 5 additions & 0 deletions source/MaaAdbControlUnit/Input/MtouchHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ class MtouchHelper : public TouchInputBase
virtual std::pair<int, int> screen_to_touch(int x, int y) = 0;
virtual std::pair<int, int> screen_to_touch(double x, double y) = 0;

// https://github.com/openstf/minitouch#writable-to-the-socket
static constexpr std::string_view kDownFormat = "d {} {} {} {}\nc\n";
static constexpr std::string_view kMoveFormat = "m {} {} {} {}\nc\n";
static constexpr std::string_view kUpFormat = "u {}\nc\n";

std::shared_ptr<IOHandler> shell_handler_ = nullptr;

int screen_width_ = 0;
Expand Down

0 comments on commit 1343aac

Please sign in to comment.