diff --git a/source/MaaAdbControlUnit/Input/MaatouchInput.cpp b/source/MaaAdbControlUnit/Input/MaatouchInput.cpp index 6acc0db88..0785e0071 100644 --- a/source/MaaAdbControlUnit/Input/MaatouchInput.cpp +++ b/source/MaaAdbControlUnit/Input/MaatouchInput.cpp @@ -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"; diff --git a/source/MaaAdbControlUnit/Input/MinitouchInput.cpp b/source/MaaAdbControlUnit/Input/MinitouchInput.cpp index b73a4765b..b5851dbdb 100644 --- a/source/MaaAdbControlUnit/Input/MinitouchInput.cpp +++ b/source/MaaAdbControlUnit/Input/MinitouchInput.cpp @@ -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; } diff --git a/source/MaaAdbControlUnit/Input/MtouchHelper.cpp b/source/MaaAdbControlUnit/Input/MtouchHelper.cpp index 48d4b1c02..8f006c51f 100644 --- a/source/MaaAdbControlUnit/Input/MtouchHelper.cpp +++ b/source/MaaAdbControlUnit/Input/MtouchHelper.cpp @@ -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"; @@ -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; @@ -147,7 +147,7 @@ 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"; } @@ -155,11 +155,11 @@ 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, 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"; @@ -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"; @@ -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"; @@ -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"; diff --git a/source/MaaAdbControlUnit/Input/MtouchHelper.h b/source/MaaAdbControlUnit/Input/MtouchHelper.h index 0ee1b3b25..fd86eb91b 100644 --- a/source/MaaAdbControlUnit/Input/MtouchHelper.h +++ b/source/MaaAdbControlUnit/Input/MtouchHelper.h @@ -24,6 +24,11 @@ class MtouchHelper : public TouchInputBase virtual std::pair screen_to_touch(int x, int y) = 0; virtual std::pair 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 shell_handler_ = nullptr; int screen_width_ = 0;