diff --git a/source/MaaControlUnit/Input/MaatouchInput.cpp b/source/MaaControlUnit/Input/MaatouchInput.cpp index 83d809362..a304bfdde 100644 --- a/source/MaaControlUnit/Input/MaatouchInput.cpp +++ b/source/MaaControlUnit/Input/MaatouchInput.cpp @@ -57,6 +57,11 @@ bool MaatouchInput::init(int swidth, int sheight, int orientation) return false; } + return set_wh(swidth, sheight, orientation); +} + +bool MaatouchInput::set_wh(int swidth, int sheight, int orientation) +{ auto start_time = std::chrono::steady_clock::now(); bool timeout = false; auto check_time = [&]() { @@ -69,159 +74,7 @@ bool MaatouchInput::init(int swidth, int sheight, int orientation) return false; } - std::string prev; - std::string info; - while (check_time()) { - auto str = prev + shell_handler_->read(5); - LogDebug << "output:" << str; - if (str.empty()) { - std::this_thread::sleep_for(std::chrono::seconds(2)); - continue; - } - auto pos = str.find('^'); - if (pos == std::string::npos) { - continue; - } - auto rpos = str.find('\n', pos); - if (rpos == std::string::npos) { - prev = str; // 也许还得再读点? - continue; - } - - info = str.substr(pos + 1, rpos - pos - 1); - break; - } - if (timeout) { - LogError << "read maatouch info timeout"; - return false; - } - - LogInfo << "maatouch info:" << info; - string_trim_(info); - - int contact = 0; - int x = 0; - int y = 0; - int pressure = 0; - - std::istringstream ins(std::move(info)); - if (!(ins >> contact >> x >> y >> pressure)) { - return false; - } - - screen_width_ = swidth; - screen_height_ = sheight; - touch_width_ = x; - touch_height_ = y; - xscale_ = double(touch_width_) / swidth; - yscale_ = double(touch_height_) / sheight; - press_ = pressure; - orientation_ = orientation; - - LogInfo << VAR(screen_width_) << VAR(screen_height_) << VAR(touch_width_) << VAR(touch_height_) << VAR(xscale_) - << VAR(yscale_) << VAR(press_) << VAR(orientation_); - - return true; -} - -void MaatouchInput::set_wh(int swidth, int sheight, int orientation) -{ - init(swidth, sheight, orientation); -} - -bool MaatouchInput::click(int x, int y) -{ - if (!shell_handler_) { - LogError << "shell handler not ready"; - return false; - } - - if (x < 0 || x >= screen_width_ || y < 0 || y >= screen_height_) { - LogWarn << "click point out of range" << VAR(x) << VAR(y); - x = std::clamp(x, 0, screen_width_ - 1); - y = std::clamp(y, 0, screen_height_ - 1); - } - - auto [touch_x, touch_y] = screen_to_touch(x, 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)); - - if (!ret) { - LogError << "failed to write"; - return false; - } - - return ret; -} - -bool MaatouchInput::swipe(int x1, int y1, int x2, int y2, int duration) -{ - if (!shell_handler_) { - return false; - } - - if (x1 < 0 || x1 >= screen_width_ || y1 < 0 || y1 >= screen_height_ || x2 < 0 || x2 >= screen_width_ || y2 < 0 || - y2 >= screen_height_) { - LogWarn << "swipe point out of range" << VAR(x1) << VAR(y1) << VAR(x2) << VAR(y2); - x1 = std::clamp(x1, 0, screen_width_ - 1); - y1 = std::clamp(y1, 0, screen_height_ - 1); - x2 = std::clamp(x2, 0, screen_width_ - 1); - y2 = std::clamp(y2, 0, screen_height_ - 1); - } - if (duration <= 0) { - LogWarn << "duration out of range" << VAR(duration); - duration = 500; - } - - auto [touch_x1, touch_y1] = screen_to_touch(x1, y1); - auto [touch_x2, touch_y2] = screen_to_touch(x2, y2); - - LogInfo << VAR(x1) << VAR(y1) << VAR(touch_x1) << VAR(touch_y1) << VAR(x2) << VAR(y2) << VAR(touch_x2) - << VAR(touch_y2) << VAR(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_)); - if (!ret) { - LogError << "write error"; - return false; - } - - constexpr double kInterval = 10; // ms - const double steps = duration / kInterval; - const double x_step_len = (x2 - x1) / steps; - const double y_step_len = (y2 - y1) / steps; - const std::chrono::milliseconds delay(static_cast(kInterval)); - - for (int i = 0; i < steps; ++i) { - auto [tx, ty] = screen_to_touch(x1 + steps * x_step_len, y1 + steps * y_step_len); - 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_)); - 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_)); - - std::this_thread::sleep_until(now + delay); - now = std::chrono::steady_clock::now(); - ret &= shell_handler_->write(MAA_FMT::format("u {}\nc\n", 0)); - - if (!ret) { - LogError << "failed to write"; - return false; - } - - return ret; + return read_info(swidth, sheight, orientation); } bool MaatouchInput::press_key(int key) @@ -241,63 +94,4 @@ bool MaatouchInput::press_key(int key) return ret; } -bool MaatouchInput::touch_down(int contact, int x, int y, int pressure) -{ - if (!shell_handler_) { - LogError << "shell handler not ready"; - return false; - } - - auto [touch_x, touch_y] = screen_to_touch(x, y); - - 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)); - - if (!ret) { - LogError << "failed to write"; - return false; - } - - return ret; -} - -bool MaatouchInput::touch_move(int contact, int x, int y, int pressure) -{ - if (!shell_handler_) { - LogError << "shell handler not ready"; - return false; - } - - auto [touch_x, touch_y] = screen_to_touch(x, y); - - 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)); - if (!ret) { - LogError << "failed to write"; - return false; - } - - return ret; -} - -bool MaatouchInput::touch_up(int contact) -{ - if (!shell_handler_) { - LogError << "shell handler not ready"; - return false; - } - - LogInfo << VAR(contact); - - bool ret = shell_handler_->write(MAA_FMT::format("u {}\nc\n", contact)); - if (!ret) { - LogError << "failed to write"; - return false; - } - - return ret; -} - MAA_CTRL_UNIT_NS_END diff --git a/source/MaaControlUnit/Input/MaatouchInput.h b/source/MaaControlUnit/Input/MaatouchInput.h index 7ecae0195..b19e2ecef 100644 --- a/source/MaaControlUnit/Input/MaatouchInput.h +++ b/source/MaaControlUnit/Input/MaatouchInput.h @@ -1,5 +1,6 @@ #pragma once +#include "MtouchHelper.h" #include "UnitBase.h" #include @@ -8,12 +9,11 @@ MAA_CTRL_UNIT_NS_BEGIN -class MaatouchInput : public TouchInputBase, public KeyInputBase +class MaatouchInput : public MtouchHelper, public KeyInputBase { public: MaatouchInput(std::filesystem::path agent_path) : agent_path_(std::move(agent_path)) { - TouchInputBase::children_.emplace_back(invoke_app_); KeyInputBase::children_.emplace_back(invoke_app_); } virtual ~MaatouchInput() override = default; @@ -23,55 +23,41 @@ class MaatouchInput : public TouchInputBase, public KeyInputBase virtual void set_io(std::shared_ptr io_ptr) override { - TouchInputBase::set_io(io_ptr); + MtouchHelper::set_io(io_ptr); KeyInputBase::set_io(io_ptr); } virtual void set_replacement(Argv::replacement argv_replace) override { - TouchInputBase::set_replacement(argv_replace); + MtouchHelper::set_replacement(argv_replace); KeyInputBase::set_replacement(argv_replace); } virtual void merge_replacement(Argv::replacement argv_replace, bool _override = true) override { - TouchInputBase::merge_replacement(argv_replace, _override); + MtouchHelper::merge_replacement(argv_replace, _override); KeyInputBase::merge_replacement(argv_replace, _override); } public: // from TouchInputAPI virtual bool init(int swidth, int sheight, int orientation) override; virtual void deinit() override {} - virtual void set_wh(int swidth, int sheight, int orientation) override; - - virtual bool click(int x, int y) override; - virtual bool swipe(int x1, int y1, int x2, int y2, int duration) override; - - virtual bool touch_down(int contact, int x, int y, int pressure) override; - virtual bool touch_move(int contact, int x, int y, int pressure) override; - virtual bool touch_up(int contact) override; + virtual bool set_wh(int swidth, int sheight, int orientation) override; public: // from KeyInputAPI virtual bool press_key(int key) override; +protected: // from MtouchHelper + virtual std::pair screen_to_touch(int x, int y) override { return _screen_to_touch(x, y); } + virtual std::pair screen_to_touch(double x, double y) override { return _screen_to_touch(x, y); } + private: template - inline std::pair screen_to_touch(T1 x, T2 y) + inline std::pair _screen_to_touch(T1 x, T2 y) { return std::make_pair(static_cast(round(x * xscale_)), static_cast(round(y * yscale_))); } - std::shared_ptr invoke_app_ = std::make_shared(); - std::shared_ptr shell_handler_ = nullptr; - std::filesystem::path agent_path_; std::string package_name_; - int screen_width_ = 0; - int screen_height_ = 0; - int touch_width_ = 0; - int touch_height_ = 0; - double xscale_ = 0; - double yscale_ = 0; - int press_ = 0; - int orientation_ = 0; }; MAA_CTRL_UNIT_NS_END diff --git a/source/MaaControlUnit/Input/MinitouchInput.cpp b/source/MaaControlUnit/Input/MinitouchInput.cpp index 146874f72..9e4d8c5c0 100644 --- a/source/MaaControlUnit/Input/MinitouchInput.cpp +++ b/source/MaaControlUnit/Input/MinitouchInput.cpp @@ -78,6 +78,11 @@ bool MinitouchInput::init(int swidth, int sheight, int orientation) return false; } + return set_wh(swidth, sheight, orientation); +} + +bool MinitouchInput::set_wh(int swidth, int sheight, int orientation) +{ auto start_time = std::chrono::steady_clock::now(); bool timeout = false; auto check_time = [&]() { @@ -91,221 +96,7 @@ bool MinitouchInput::init(int swidth, int sheight, int orientation) return false; } - std::string prev; - std::string info; - - while (check_time()) { - auto str = prev + shell_handler_->read(5); - LogDebug << "output:" << str; - if (str.empty()) { - std::this_thread::sleep_for(std::chrono::seconds(2)); - continue; - } - auto pos = str.find('^'); - if (pos == std::string::npos) { - continue; - } - auto rpos = str.find('\n', pos); - if (rpos == std::string::npos) { - prev = str; // 也许还得再读点? - continue; - } - - info = str.substr(pos + 1, rpos - pos - 1); - break; - } - if (timeout) { - LogError << "read minitouch info timeout"; - return false; - } - - LogInfo << "minitouch info:" << info; - string_trim_(info); - - int contact = 0; - int x = 0; - int y = 0; - int pressure = 0; - - std::istringstream ins(std::move(info)); - if (!(ins >> contact >> x >> y >> pressure)) { - return false; - } - - screen_width_ = swidth; - screen_height_ = sheight; - touch_width_ = x; - touch_height_ = y; - xscale_ = double(touch_width_) / swidth; - yscale_ = double(touch_height_) / sheight; - press_ = pressure; - orientation_ = orientation; - - LogInfo << VAR(screen_width_) << VAR(screen_height_) << VAR(touch_width_) << VAR(touch_height_) << VAR(xscale_) - << VAR(yscale_) << VAR(press_) << VAR(orientation_); - - return true; -} - -void MinitouchInput::set_wh(int swidth, int sheight, int orientation) -{ - init(swidth, sheight, orientation); -} - -bool MinitouchInput::click(int x, int y) -{ - if (!shell_handler_) { - LogError << "shell handler not ready"; - return false; - } - - if (x < 0 || x >= screen_width_ || y < 0 || y >= screen_height_) { - LogWarn << "click point out of range" << VAR(x) << VAR(y); - x = std::clamp(x, 0, screen_width_ - 1); - y = std::clamp(y, 0, screen_height_ - 1); - } - - auto [touch_x, touch_y] = screen_to_touch(x, 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)); - - if (!ret) { - LogError << "failed to write"; - return false; - } - - return ret; -} - -bool MinitouchInput::swipe(int x1, int y1, int x2, int y2, int duration) -{ - if (!shell_handler_) { - return false; - } - - if (x1 < 0 || x1 >= screen_width_ || y1 < 0 || y1 >= screen_height_ || x2 < 0 || x2 >= screen_width_ || y2 < 0 || - y2 >= screen_height_) { - LogWarn << "swipe point out of range" << VAR(x1) << VAR(y1) << VAR(x2) << VAR(y2); - x1 = std::clamp(x1, 0, screen_width_ - 1); - y1 = std::clamp(y1, 0, screen_height_ - 1); - x2 = std::clamp(x2, 0, screen_width_ - 1); - y2 = std::clamp(y2, 0, screen_height_ - 1); - } - if (duration <= 0) { - LogWarn << "duration out of range" << VAR(duration); - duration = 500; - } - - auto [touch_x1, touch_y1] = screen_to_touch(x1, y1); - auto [touch_x2, touch_y2] = screen_to_touch(x2, y2); - - LogInfo << VAR(x1) << VAR(y1) << VAR(touch_x1) << VAR(touch_y1) << VAR(x2) << VAR(y2) << VAR(touch_x2) - << VAR(touch_y2) << VAR(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_)); - if (!ret) { - LogError << "write error"; - return false; - } - - constexpr double kInterval = 10; // ms - const double steps = duration / kInterval; - const double x_step_len = (x2 - x1) / steps; - const double y_step_len = (y2 - y1) / steps; - const std::chrono::milliseconds delay(static_cast(kInterval)); - - for (int i = 0; i < steps; ++i) { - auto [tx, ty] = screen_to_touch(x1 + steps * x_step_len, y1 + steps * y_step_len); - 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_)); - 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_)); - - std::this_thread::sleep_until(now + delay); - now = std::chrono::steady_clock::now(); - ret &= shell_handler_->write(MAA_FMT::format("u {}\nc\n", 0)); - - if (!ret) { - LogError << "failed to write"; - return false; - } - - return ret; -} - -bool MinitouchInput::touch_down(int contact, int x, int y, int pressure) -{ - if (!shell_handler_) { - LogError << "shell handler not ready"; - return false; - } - - auto [touch_x, touch_y] = screen_to_touch(x, y); - - 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)); - - if (!ret) { - LogError << "failed to write"; - return false; - } - - return ret; -} - -bool MinitouchInput::touch_move(int contact, int x, int y, int pressure) -{ - if (!shell_handler_) { - LogError << "shell handler not ready"; - return false; - } - - auto [touch_x, touch_y] = screen_to_touch(x, y); - - 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)); - - if (!ret) { - LogError << "failed to write"; - return false; - } - - return ret; -} - -bool MinitouchInput::touch_up(int contact) -{ - if (!shell_handler_) { - LogError << "shell handler not ready"; - return false; - } - - LogInfo << VAR(contact); - - bool ret = shell_handler_->write(MAA_FMT::format("u {}\nc\n", contact)); - - if (!ret) { - LogError << "failed to write"; - return false; - } - - return ret; + return read_info(swidth, sheight, orientation); } MAA_CTRL_UNIT_NS_END diff --git a/source/MaaControlUnit/Input/MinitouchInput.h b/source/MaaControlUnit/Input/MinitouchInput.h index f901f218c..e69b3a389 100644 --- a/source/MaaControlUnit/Input/MinitouchInput.h +++ b/source/MaaControlUnit/Input/MinitouchInput.h @@ -1,6 +1,6 @@ #pragma once -#include "UnitBase.h" +#include "MtouchHelper.h" #include @@ -8,7 +8,7 @@ MAA_CTRL_UNIT_NS_BEGIN -class MinitouchInput : public TouchInputBase +class MinitouchInput : public MtouchHelper { public: MinitouchInput(std::filesystem::path agent_path) : agent_path_(std::move(agent_path)) @@ -23,18 +23,15 @@ class MinitouchInput : public TouchInputBase public: // from TouchInputAPI virtual bool init(int swidth, int sheight, int orientation) override; virtual void deinit() override {} - virtual void set_wh(int swidth, int sheight, int orientation) override; + virtual bool set_wh(int swidth, int sheight, int orientation) override; - virtual bool click(int x, int y) override; - virtual bool swipe(int x1, int y1, int x2, int y2, int duration) override; - - virtual bool touch_down(int contact, int x, int y, int pressure) override; - virtual bool touch_move(int contact, int x, int y, int pressure) override; - virtual bool touch_up(int contact) override; +protected: // from MtouchHelper + virtual std::pair screen_to_touch(int x, int y) override { return _screen_to_touch(x, y); } + virtual std::pair screen_to_touch(double x, double y) override { return _screen_to_touch(x, y); } private: template - inline std::pair screen_to_touch(T1 x, T2 y) + inline std::pair _screen_to_touch(T1 x, T2 y) { auto make_pair = [](double x, double y) { return std::make_pair(static_cast(round(x)), static_cast(round(y))); @@ -53,19 +50,8 @@ class MinitouchInput : public TouchInputBase } } - std::shared_ptr invoke_app_ = std::make_shared(); - std::shared_ptr shell_handler_ = nullptr; - std::filesystem::path agent_path_; std::vector arch_list_; - int screen_width_ = 0; - int screen_height_ = 0; - int touch_width_ = 0; - int touch_height_ = 0; - double xscale_ = 0; - double yscale_ = 0; - int press_ = 0; - int orientation_ = 0; }; MAA_CTRL_UNIT_NS_END diff --git a/source/MaaControlUnit/Input/MtouchHelper.cpp b/source/MaaControlUnit/Input/MtouchHelper.cpp new file mode 100644 index 000000000..48d4b1c02 --- /dev/null +++ b/source/MaaControlUnit/Input/MtouchHelper.cpp @@ -0,0 +1,233 @@ +#include "MtouchHelper.h" + +#include "Utils/Format.hpp" +#include "Utils/Logger.h" +#include "Utils/Ranges.hpp" + +#include +#include + +MAA_CTRL_UNIT_NS_BEGIN + +bool MtouchHelper::read_info(int swidth, int sheight, int orientation) +{ + auto start_time = std::chrono::steady_clock::now(); + bool timeout = false; + auto check_time = [&]() { + timeout = duration_since(start_time) > std::chrono::seconds(10); + return !timeout; + }; + + std::string prev; + std::string info; + + while (check_time()) { + auto str = prev + shell_handler_->read(5); + LogDebug << "output:" << str; + if (str.empty()) { + std::this_thread::sleep_for(std::chrono::seconds(2)); + continue; + } + auto pos = str.find('^'); + if (pos == std::string::npos) { + continue; + } + auto rpos = str.find('\n', pos); + if (rpos == std::string::npos) { + prev = str; // 也许还得再读点? + continue; + } + + info = str.substr(pos + 1, rpos - pos - 1); + break; + } + if (timeout) { + LogError << "read info timeout"; + return false; + } + + LogInfo << "info:" << info; + string_trim_(info); + + int contact = 0; + int x = 0; + int y = 0; + int pressure = 0; + + std::istringstream ins(std::move(info)); + if (!(ins >> contact >> x >> y >> pressure)) { + return false; + } + + screen_width_ = swidth; + screen_height_ = sheight; + touch_width_ = x; + touch_height_ = y; + xscale_ = double(touch_width_) / swidth; + yscale_ = double(touch_height_) / sheight; + press_ = pressure; + orientation_ = orientation; + + LogInfo << VAR(screen_width_) << VAR(screen_height_) << VAR(touch_width_) << VAR(touch_height_) << VAR(xscale_) + << VAR(yscale_) << VAR(press_) << VAR(orientation_); + + return true; +} + +bool MtouchHelper::click(int x, int y) +{ + if (!shell_handler_) { + LogError << "shell handler not ready"; + return false; + } + + if (x < 0 || x >= screen_width_ || y < 0 || y >= screen_height_) { + LogWarn << "click point out of range" << VAR(x) << VAR(y); + x = std::clamp(x, 0, screen_width_ - 1); + y = std::clamp(y, 0, screen_height_ - 1); + } + + auto [touch_x, touch_y] = screen_to_touch(x, 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)); + + if (!ret) { + LogError << "failed to write"; + return false; + } + + return ret; +} + +bool MtouchHelper::swipe(int x1, int y1, int x2, int y2, int duration) +{ + if (!shell_handler_) { + return false; + } + + if (x1 < 0 || x1 >= screen_width_ || y1 < 0 || y1 >= screen_height_ || x2 < 0 || x2 >= screen_width_ || y2 < 0 || + y2 >= screen_height_) { + LogWarn << "swipe point out of range" << VAR(x1) << VAR(y1) << VAR(x2) << VAR(y2); + x1 = std::clamp(x1, 0, screen_width_ - 1); + y1 = std::clamp(y1, 0, screen_height_ - 1); + x2 = std::clamp(x2, 0, screen_width_ - 1); + y2 = std::clamp(y2, 0, screen_height_ - 1); + } + if (duration <= 0) { + LogWarn << "duration out of range" << VAR(duration); + duration = 500; + } + + auto [touch_x1, touch_y1] = screen_to_touch(x1, y1); + auto [touch_x2, touch_y2] = screen_to_touch(x2, y2); + + LogInfo << VAR(x1) << VAR(y1) << VAR(touch_x1) << VAR(touch_y1) << VAR(x2) << VAR(y2) << VAR(touch_x2) + << VAR(touch_y2) << VAR(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_)); + if (!ret) { + LogError << "write error"; + return false; + } + + constexpr double kInterval = 10; // ms + const double steps = duration / kInterval; + const double x_step_len = (x2 - x1) / steps; + const double y_step_len = (y2 - y1) / steps; + const std::chrono::milliseconds delay(static_cast(kInterval)); + + for (int i = 0; i < steps; ++i) { + auto [tx, ty] = screen_to_touch(x1 + i * x_step_len, y1 + i * y_step_len); + 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_)); + 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_)); + + std::this_thread::sleep_until(now + delay); + now = std::chrono::steady_clock::now(); + ret &= shell_handler_->write(MAA_FMT::format("u {}\nc\n", 0)); + + if (!ret) { + LogError << "failed to write"; + return false; + } + + return ret; +} + +bool MtouchHelper::touch_down(int contact, int x, int y, int pressure) +{ + if (!shell_handler_) { + LogError << "shell handler not ready"; + return false; + } + + auto [touch_x, touch_y] = screen_to_touch(x, y); + + 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)); + + if (!ret) { + LogError << "failed to write"; + return false; + } + + return ret; +} + +bool MtouchHelper::touch_move(int contact, int x, int y, int pressure) +{ + if (!shell_handler_) { + LogError << "shell handler not ready"; + return false; + } + + auto [touch_x, touch_y] = screen_to_touch(x, y); + + 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)); + + if (!ret) { + LogError << "failed to write"; + return false; + } + + return ret; +} + +bool MtouchHelper::touch_up(int contact) +{ + if (!shell_handler_) { + LogError << "shell handler not ready"; + return false; + } + + LogInfo << VAR(contact); + + bool ret = shell_handler_->write(MAA_FMT::format("u {}\nc\n", contact)); + + if (!ret) { + LogError << "failed to write"; + return false; + } + + return ret; +} + +MAA_CTRL_UNIT_NS_END diff --git a/source/MaaControlUnit/Input/MtouchHelper.h b/source/MaaControlUnit/Input/MtouchHelper.h new file mode 100644 index 000000000..1f81c892e --- /dev/null +++ b/source/MaaControlUnit/Input/MtouchHelper.h @@ -0,0 +1,41 @@ +#pragma once + +#include "UnitBase.h" + +#include "Invoke/InvokeApp.h" + +MAA_CTRL_UNIT_NS_BEGIN + +class MtouchHelper : public TouchInputBase +{ +public: + MtouchHelper() { children_.emplace_back(invoke_app_); } + virtual ~MtouchHelper() override = default; + +public: // from TouchInputAPI + virtual bool click(int x, int y) override; + virtual bool swipe(int x1, int y1, int x2, int y2, int duration) override; + + virtual bool touch_down(int contact, int x, int y, int pressure) override; + virtual bool touch_move(int contact, int x, int y, int pressure) override; + virtual bool touch_up(int contact) override; + +protected: + bool read_info(int swidth, int sheight, int orientation); + virtual std::pair screen_to_touch(int x, int y) = 0; + virtual std::pair screen_to_touch(double x, double y) = 0; + + std::shared_ptr invoke_app_ = std::make_shared(); + std::shared_ptr shell_handler_ = nullptr; + + int screen_width_ = 0; + int screen_height_ = 0; + int touch_width_ = 0; + int touch_height_ = 0; + double xscale_ = 0; + double yscale_ = 0; + int press_ = 0; + int orientation_ = 0; +}; + +MAA_CTRL_UNIT_NS_END diff --git a/source/MaaControlUnit/Input/TapInput.cpp b/source/MaaControlUnit/Input/TapInput.cpp index 353f05fee..38f9cb157 100644 --- a/source/MaaControlUnit/Input/TapInput.cpp +++ b/source/MaaControlUnit/Input/TapInput.cpp @@ -12,16 +12,17 @@ bool TapTouchInput::parse(const json::value& config) } bool TapTouchInput::init(int swidth, int sheight, int orientation) +{ + return set_wh(swidth, sheight, orientation); +} + +bool TapTouchInput::set_wh(int swidth, int sheight, int orientation) { std::ignore = swidth; std::ignore = sheight; std::ignore = orientation; - return true; -} -void TapTouchInput::set_wh(int swidth, int sheight, int orientation) -{ - init(swidth, sheight, orientation); + return true; } bool TapTouchInput::click(int x, int y) diff --git a/source/MaaControlUnit/Input/TapInput.h b/source/MaaControlUnit/Input/TapInput.h index 86ccaf140..9cabade31 100644 --- a/source/MaaControlUnit/Input/TapInput.h +++ b/source/MaaControlUnit/Input/TapInput.h @@ -15,7 +15,7 @@ class TapTouchInput : public TouchInputBase public: // from TouchInputAPI virtual bool init(int swidth, int sheight, int orientation) override; virtual void deinit() override {} - virtual void set_wh(int swidth, int sheight, int orientation) override; + virtual bool set_wh(int swidth, int sheight, int orientation) override; virtual bool click(int x, int y) override; virtual bool swipe(int x1, int y1, int x2, int y2, int duration) override; diff --git a/source/MaaControlUnit/MaaControlUnit.vcxproj b/source/MaaControlUnit/MaaControlUnit.vcxproj index 04d24f44c..6c502fdc8 100644 --- a/source/MaaControlUnit/MaaControlUnit.vcxproj +++ b/source/MaaControlUnit/MaaControlUnit.vcxproj @@ -23,6 +23,7 @@ + @@ -48,6 +49,7 @@ + diff --git a/source/MaaControlUnit/Screencap/Encode.cpp b/source/MaaControlUnit/Screencap/Encode.cpp index acb1264f6..1091f5432 100644 --- a/source/MaaControlUnit/Screencap/Encode.cpp +++ b/source/MaaControlUnit/Screencap/Encode.cpp @@ -11,8 +11,7 @@ bool ScreencapEncode::parse(const json::value& config) bool ScreencapEncode::init(int swidth, int sheight) { - set_wh(swidth, sheight); - return true; + return set_wh(swidth, sheight); } std::optional ScreencapEncode::screencap() diff --git a/source/MaaControlUnit/Screencap/EncodeToFile.cpp b/source/MaaControlUnit/Screencap/EncodeToFile.cpp index 86e961d1d..0de81fa63 100644 --- a/source/MaaControlUnit/Screencap/EncodeToFile.cpp +++ b/source/MaaControlUnit/Screencap/EncodeToFile.cpp @@ -15,11 +15,8 @@ bool ScreencapEncodeToFileAndPull::parse(const json::value& config) bool ScreencapEncodeToFileAndPull::init(int swidth, int sheight) { - set_wh(swidth, sheight); - tempname_ = now_filestem(); - - return true; + return set_wh(swidth, sheight); } std::optional ScreencapEncodeToFileAndPull::screencap() diff --git a/source/MaaControlUnit/Screencap/FastestWay.cpp b/source/MaaControlUnit/Screencap/FastestWay.cpp index 43fabf6fe..cd6a93ca7 100644 --- a/source/MaaControlUnit/Screencap/FastestWay.cpp +++ b/source/MaaControlUnit/Screencap/FastestWay.cpp @@ -56,12 +56,15 @@ void ScreencapFastestWay::deinit() method_ = Method::UnknownYet; } -void ScreencapFastestWay::set_wh(int swidth, int sheight) +bool ScreencapFastestWay::set_wh(int swidth, int sheight) { LogFunc; + for (auto pair : units_) { pair.second->set_wh(swidth, sheight); } + + return true; } std::optional ScreencapFastestWay::screencap() diff --git a/source/MaaControlUnit/Screencap/FastestWay.h b/source/MaaControlUnit/Screencap/FastestWay.h index 60d9069d6..74ee7a2e1 100644 --- a/source/MaaControlUnit/Screencap/FastestWay.h +++ b/source/MaaControlUnit/Screencap/FastestWay.h @@ -33,7 +33,7 @@ class ScreencapFastestWay : public ScreencapBase public: // from ScreencapAPI virtual bool init(int swidth, int sheight) override; virtual void deinit() override; - virtual void set_wh(int swidth, int sheight) override; + virtual bool set_wh(int swidth, int sheight) override; virtual std::optional screencap() override; diff --git a/source/MaaControlUnit/Screencap/Minicap/MinicapBase.cpp b/source/MaaControlUnit/Screencap/Minicap/MinicapBase.cpp index 7898bcd9d..1b7911157 100644 --- a/source/MaaControlUnit/Screencap/Minicap/MinicapBase.cpp +++ b/source/MaaControlUnit/Screencap/Minicap/MinicapBase.cpp @@ -108,8 +108,8 @@ bool MinicapBase::init(int swidth, int sheight) // TODO: 确认低版本是否使用minicap-nopie const auto bin_path = agent_path_ / path(target_arch) / path("bin") / path("minicap"); - const auto lib_path = agent_path_ / path(target_arch) / path("lib") / - path(MAA_FMT::format("android-{}", fit_sdk)) / path("minicap.so"); + const auto lib_path = agent_path_ / path(target_arch) / path("lib") / path(MAA_FMT::format("android-{}", fit_sdk)) / + path("minicap.so"); if (!binary_->push(bin_path) || !library_->push(lib_path)) { return false; } @@ -118,8 +118,7 @@ bool MinicapBase::init(int swidth, int sheight) return false; } - set_wh(swidth, sheight); - return true; + return set_wh(swidth, sheight); } MAA_CTRL_UNIT_NS_END diff --git a/source/MaaControlUnit/Screencap/RawByNetcat.cpp b/source/MaaControlUnit/Screencap/RawByNetcat.cpp index e9c317be8..b37b62095 100644 --- a/source/MaaControlUnit/Screencap/RawByNetcat.cpp +++ b/source/MaaControlUnit/Screencap/RawByNetcat.cpp @@ -12,7 +12,7 @@ bool ScreencapRawByNetcat::parse(const json::value& config) bool ScreencapRawByNetcat::init(int swidth, int sheight) { - set_wh(swidth, sheight); + LogFunc; if (!io_ptr_) { LogError << "io_ptr is nullptr"; @@ -38,7 +38,7 @@ bool ScreencapRawByNetcat::init(int swidth, int sheight) } netcat_port_ = prt.value(); - return true; + return set_wh(swidth, sheight); } void ScreencapRawByNetcat::deinit() diff --git a/source/MaaControlUnit/Screencap/RawWithGzip.cpp b/source/MaaControlUnit/Screencap/RawWithGzip.cpp index 435bfb5a8..4eefe7835 100644 --- a/source/MaaControlUnit/Screencap/RawWithGzip.cpp +++ b/source/MaaControlUnit/Screencap/RawWithGzip.cpp @@ -11,9 +11,7 @@ bool ScreencapRawWithGzip::parse(const json::value& config) bool ScreencapRawWithGzip::init(int swidth, int sheight) { - set_wh(swidth, sheight); - - return true; + return set_wh(swidth, sheight); } std::optional ScreencapRawWithGzip::screencap() diff --git a/source/MaaControlUnit/Screencap/ScreencapHelper.cpp b/source/MaaControlUnit/Screencap/ScreencapHelper.cpp index f94df98e5..a0bbda346 100644 --- a/source/MaaControlUnit/Screencap/ScreencapHelper.cpp +++ b/source/MaaControlUnit/Screencap/ScreencapHelper.cpp @@ -14,10 +14,12 @@ MAA_CTRL_UNIT_NS_BEGIN -void ScreencapHelper::set_wh(int w, int h) +bool ScreencapHelper::set_wh(int w, int h) { width_ = w; height_ = h; + + return true; } std::optional ScreencapHelper::process_data( diff --git a/source/MaaControlUnit/Screencap/ScreencapHelper.h b/source/MaaControlUnit/Screencap/ScreencapHelper.h index 3a4c5c295..a6169a3ad 100644 --- a/source/MaaControlUnit/Screencap/ScreencapHelper.h +++ b/source/MaaControlUnit/Screencap/ScreencapHelper.h @@ -12,7 +12,7 @@ MAA_CTRL_UNIT_NS_BEGIN class ScreencapHelper { public: - void set_wh(int w, int h); + bool set_wh(int w, int h); int get_w() const { return width_; } int get_h() const { return height_; } diff --git a/source/MaaControlUnit/UnitBase.h b/source/MaaControlUnit/UnitBase.h index 102218c06..039ed2815 100644 --- a/source/MaaControlUnit/UnitBase.h +++ b/source/MaaControlUnit/UnitBase.h @@ -64,7 +64,7 @@ class ScreencapBase : public ScreencapAPI, public UnitBase virtual ~ScreencapBase() override = default; public: - virtual void set_wh(int swidth, int sheight) override { screencap_helper_.set_wh(swidth, sheight); } + virtual bool set_wh(int swidth, int sheight) override { return screencap_helper_.set_wh(swidth, sheight); } protected: ScreencapHelper screencap_helper_; diff --git a/source/include/ControlUnit/ControlUnitAPI.h b/source/include/ControlUnit/ControlUnitAPI.h index f9be5ddd9..27de21e8d 100644 --- a/source/include/ControlUnit/ControlUnitAPI.h +++ b/source/include/ControlUnit/ControlUnitAPI.h @@ -58,7 +58,7 @@ class TouchInputAPI virtual bool init(int swidth, int sheight, int orientation) = 0; virtual void deinit() = 0; - virtual void set_wh(int swidth, int sheight, int orientation) = 0; + virtual bool set_wh(int swidth, int sheight, int orientation) = 0; virtual bool click(int x, int y) = 0; virtual bool swipe(int x1, int y1, int x2, int y2, int duration) = 0; @@ -85,7 +85,7 @@ class ScreencapAPI virtual bool init(int swidth, int sheight) = 0; virtual void deinit() = 0; - virtual void set_wh(int swidth, int sheight) = 0; + virtual bool set_wh(int swidth, int sheight) = 0; virtual std::optional screencap() = 0; };