From 8316c63d4c4b202a04d2c7f4562f32baadfbf9dc Mon Sep 17 00:00:00 2001 From: MistEO Date: Sun, 13 Nov 2022 16:13:51 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=9A=82=E6=97=B6=E7=BB=9F=E4=B8=80?= =?UTF-8?q?=E5=85=B3=E9=97=ADminitouch=EF=BC=8C=E5=90=8E=E7=BB=AD=E9=80=90?= =?UTF-8?q?=E6=AD=A5=E9=80=82=E9=85=8D=E5=B9=B6=E5=BC=80=E5=90=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/MeoAssistant/Assistant.cpp | 4 +++ src/MeoAssistant/Controller.cpp | 58 +++++++++++++++++---------------- src/MeoAssistant/Controller.h | 6 ++-- 3 files changed, 38 insertions(+), 30 deletions(-) diff --git a/src/MeoAssistant/Assistant.cpp b/src/MeoAssistant/Assistant.cpp index ce52b131b3..3f6507bb24 100644 --- a/src/MeoAssistant/Assistant.cpp +++ b/src/MeoAssistant/Assistant.cpp @@ -253,6 +253,10 @@ void Assistant::working_proc() }; task_callback(AsstMsg::TaskChainStart, callback_json, this); + // minitouch 的适配工作太多了,短期弄不完,默认关掉 + // 适配好了的任务每个自己单独开 + m_ctrler->set_minitouch_enabled(false); + bool ret = task_ptr->run(); finished_tasks.emplace_back(id); diff --git a/src/MeoAssistant/Controller.cpp b/src/MeoAssistant/Controller.cpp index 3c5791c2c5..166b0b29d5 100644 --- a/src/MeoAssistant/Controller.cpp +++ b/src/MeoAssistant/Controller.cpp @@ -801,7 +801,7 @@ std::optional asst::Controller::init_socket(const std::string& l int listen_ret = ::listen(m_server_sock, 3); server_start = bind_ret == 0 && getname_ret == 0 && listen_ret == 0; #else - m_server_sock_addr.sin_port = ::htons(0); + m_server_sock_addr.sin_port = htons(0); int bind_ret = ::bind(m_server_sock, reinterpret_cast(&m_server_sock_addr), sizeof(::sockaddr_in)); socklen_t addrlen = sizeof(m_server_sock_addr); int getname_ret = ::getsockname(m_server_sock, reinterpret_cast(&m_server_sock_addr), &addrlen); @@ -817,7 +817,7 @@ std::optional asst::Controller::init_socket(const std::string& l #ifdef _WIN32 port_result = ::ntohs(m_server_sock_addr.sin_port); #else - port_result = ::ntohs(m_server_sock_addr.sin_port); + port_result = ntohs(m_server_sock_addr.sin_port); #endif Log.info("command server start", local_address, port_result); @@ -1055,17 +1055,18 @@ bool asst::Controller::click_without_scale(const Point& p) Log.error("click point out of range"); } - if (m_minitouch_avaiable) { + if (m_minitouch_enabled && m_minitouch_avaiable) { int x = static_cast(p.x * m_minitouch_props.x_scaling); int y = static_cast(p.y * m_minitouch_props.y_scaling); constexpr int WaitMs = 50; std::string minitouch_cmd = MinitouchCmd::down(x, y, true, WaitMs) + MinitouchCmd::up(); return input_to_minitouch(minitouch_cmd, WaitMs); } - - std::string cur_cmd = - utils::string_replace_all(m_adb.click, { { "[x]", std::to_string(p.x) }, { "[y]", std::to_string(p.y) } }); - return call_command(cur_cmd).has_value(); + else { + std::string cur_cmd = + utils::string_replace_all(m_adb.click, { { "[x]", std::to_string(p.x) }, { "[y]", std::to_string(p.y) } }); + return call_command(cur_cmd).has_value(); + } } bool asst::Controller::click_without_scale(const Rect& rect) @@ -1102,7 +1103,7 @@ bool asst::Controller::swipe_without_scale(const Point& p1, const Point& p2, int } const auto& opt = Configer.get_options(); - if (m_minitouch_avaiable) { + if (m_minitouch_enabled && m_minitouch_avaiable) { constexpr int MoveInterval = 1; std::string minitouch_cmd = MinitouchCmd::down(static_cast(x1 * m_minitouch_props.x_scaling), @@ -1122,9 +1123,16 @@ bool asst::Controller::swipe_without_scale(const Point& p1, const Point& p2, int // TODO: 加点随机因子,或者改成中间快两头慢 std::string minitouch_cmd; for (int times = 1; times < move_times; ++times) { - minitouch_cmd += MinitouchCmd::move(_x1 + x_step * times, _y1 + y_step * times, true, MoveInterval); + int cur_x = _x1 + x_step * times; + int cur_y = _y1 + y_step * times; + if (cur_x < 0 || cur_x > m_minitouch_props.max_x || cur_y < 0 || cur_y > m_minitouch_props.max_y) { + continue; + } + minitouch_cmd += MinitouchCmd::move(cur_x, cur_y, true, MoveInterval); + } + if (_x2 >= 0 && _x2 <= m_minitouch_props.max_x && _y2 >= 0 && _y2 <= m_minitouch_props.max_y) { + minitouch_cmd += MinitouchCmd::move(_x2, _y2); } - minitouch_cmd += MinitouchCmd::move(_x2, _y2); return minitouch_cmd; }; minitouch_cmd += move_cmd(x1, y1, x2, y2, duration); @@ -1436,26 +1444,20 @@ bool asst::Controller::connect(const std::string& adb_path, const std::string& a return false; } - if (m_minitouch_enabled) { - auto minitouch_cmd_rep = [&](const std::string& cfg_cmd) -> std::string { - return utils::string_replace_all( - cmd_replace(cfg_cmd), - { - // TODO: 用 adb shell getprop ro.product.cpu.abilist 来判断该用哪个文件夹里的 - { "[minitouchLocalPath]", (m_resource_path / "minitouch" / "armeabi-v7a" / "minitouch").string() }, - { "[minitouchWorkingFile]", m_uuid }, - }); - }; + auto minitouch_cmd_rep = [&](const std::string& cfg_cmd) -> std::string { + return utils::string_replace_all( + cmd_replace(cfg_cmd), + { + // TODO: 用 adb shell getprop ro.product.cpu.abilist 来判断该用哪个文件夹里的 + { "[minitouchLocalPath]", (m_resource_path / "minitouch" / "armeabi-v7a" / "minitouch").string() }, + { "[minitouchWorkingFile]", m_uuid }, + }); + }; - call_command(minitouch_cmd_rep(adb_cfg.push_minitouch)); - call_command(minitouch_cmd_rep(adb_cfg.chmod_minitouch)); + call_command(minitouch_cmd_rep(adb_cfg.push_minitouch)); + call_command(minitouch_cmd_rep(adb_cfg.chmod_minitouch)); - std::string call_mt_cmd = minitouch_cmd_rep(adb_cfg.call_minitouch); - // 不知道为啥有时候读不到pipe,重试几次,再不行拉到 - for (int i = 0; i != 5; ++i) { - if (call_and_hup_minitouch(call_mt_cmd)) break; - } - } + call_and_hup_minitouch(minitouch_cmd_rep(adb_cfg.call_minitouch)); // try to find the fastest way if (!screencap()) { diff --git a/src/MeoAssistant/Controller.h b/src/MeoAssistant/Controller.h index 39baab3b6b..1cf731f6cf 100644 --- a/src/MeoAssistant/Controller.h +++ b/src/MeoAssistant/Controller.h @@ -38,10 +38,12 @@ namespace asst Controller(Controller&&) = delete; ~Controller(); + inline static void set_resource_path(const std::filesystem::path& path) { m_resource_path = path; } + bool connect(const std::string& adb_path, const std::string& address, const std::string& config); bool inited() const noexcept; void set_exit_flag(bool* flag); - inline static void set_resource_path(const std::filesystem::path& path) { m_resource_path = path; } + void set_minitouch_enabled(bool enable) noexcept { m_minitouch_enabled = enable; } const std::string& get_uuid() const; cv::Mat get_image(bool raw = false); @@ -158,7 +160,7 @@ namespace asst } screencap_method = ScreencapMethod::UnknownYet; } m_adb; - bool m_minitouch_enabled = true; // 开关 + bool m_minitouch_enabled = false; // 开关 bool m_minitouch_avaiable = false; // 状态 #ifdef _WIN32