From 3a7a00d490c683be3f0041b3f75774d7fe4e9954 Mon Sep 17 00:00:00 2001 From: MistEO Date: Wed, 16 Nov 2022 00:11:00 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20adb=20=E5=B0=81=E8=A3=85=E7=9A=84?= =?UTF-8?q?=E4=B8=80=E4=BA=9B=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/MeoAssistant/Controller.cpp | 50 +++++++++++++-------------- src/MeoAssistant/Controller.h | 61 +++++++++++++++++++++------------ 2 files changed, 64 insertions(+), 47 deletions(-) diff --git a/src/MeoAssistant/Controller.cpp b/src/MeoAssistant/Controller.cpp index 11034a3b1c..3b190656b4 100644 --- a/src/MeoAssistant/Controller.cpp +++ b/src/MeoAssistant/Controller.cpp @@ -459,11 +459,12 @@ bool asst::Controller::call_and_hup_minitouch(const std::string& cmd) .lpSecurityDescriptor = nullptr, .bInheritHandle = TRUE, }; - HANDLE pipe_child_write = INVALID_HANDLE_VALUE, pipe_parent_read = INVALID_HANDLE_VALUE; + HANDLE pipe_parent_read = INVALID_HANDLE_VALUE, pipe_child_write = INVALID_HANDLE_VALUE; + HANDLE pipe_child_read = INVALID_HANDLE_VALUE, pipe_parent_write = INVALID_HANDLE_VALUE; if (!asst::win32::CreateOverlappablePipe(&pipe_parent_read, &pipe_child_write, nullptr, &sa_attr_inherit, PipeReadBuffSize, true, false) || - !asst::win32::CreateOverlappablePipe(&m_minitouch_child_read, &m_minitouch_parent_write, &sa_attr_inherit, - nullptr, PipeWriteBuffSize, false, false)) { + !asst::win32::CreateOverlappablePipe(&pipe_child_read, &pipe_parent_write, &sa_attr_inherit, nullptr, + PipeWriteBuffSize, false, false)) { DWORD err = GetLastError(); Log.error("Failed to create pipe for minitouch, err", err); return false; @@ -473,31 +474,31 @@ bool asst::Controller::call_and_hup_minitouch(const std::string& cmd) si.cb = sizeof(STARTUPINFOW); si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW; si.wShowWindow = SW_HIDE; - si.hStdInput = m_minitouch_child_read; + si.hStdInput = pipe_child_read; si.hStdOutput = pipe_child_write; si.hStdError = pipe_child_write; auto cmd_osstr = utils::to_osstring(cmd); - if (!CreateProcessW(NULL, cmd_osstr.data(), nullptr, nullptr, TRUE, 0, nullptr, nullptr, &si, - &m_minitouch_process_info)) { + BOOL create_ret = CreateProcessW(NULL, cmd_osstr.data(), nullptr, nullptr, TRUE, 0, nullptr, nullptr, &si, + &m_minitouch_process_info); + CloseHandle(pipe_child_write); + CloseHandle(pipe_child_read); + pipe_child_write = INVALID_HANDLE_VALUE; + pipe_child_read = INVALID_HANDLE_VALUE; + + if (!create_ret) { DWORD err = GetLastError(); Log.error("Failed to create process for minitouch, err", err); - release_minitouch(true); return false; } - CloseHandle(pipe_child_write); - pipe_child_write = INVALID_HANDLE_VALUE; - - auto pipe_buffer = std::make_unique(PipeReadBuffSize); - auto start_time = std::chrono::steady_clock::now(); auto check_timeout = [&]() -> bool { using namespace std::chrono_literals; return std::chrono::steady_clock::now() - start_time < 3s; }; - std::vector wait_handles; + auto pipe_buffer = std::make_unique(PipeReadBuffSize); OVERLAPPED pipeov { .hEvent = CreateEventW(nullptr, TRUE, FALSE, nullptr) }; std::ignore = ReadFile(pipe_parent_read, pipe_buffer.get(), PipeReadBuffSize, nullptr, &pipeov); @@ -516,6 +517,7 @@ bool asst::Controller::call_and_hup_minitouch(const std::string& cmd) CloseHandle(pipe_parent_read); pipe_parent_read = INVALID_HANDLE_VALUE; + m_minitouch_parent_write = pipe_parent_write; #else // !_WIN32 // TODO std::ignore = pipe_str; @@ -588,10 +590,6 @@ void asst::Controller::release_minitouch(bool force) CloseHandle(m_minitouch_process_info.hThread); m_minitouch_process_info.hThread = nullptr; } - if (m_minitouch_child_read) { - CloseHandle(m_minitouch_child_read); - m_minitouch_child_read = nullptr; - } if (m_minitouch_parent_write) { CloseHandle(m_minitouch_parent_write); m_minitouch_parent_write = nullptr; @@ -1003,8 +1001,8 @@ bool asst::Controller::click_without_scale(const Point& p) } if (m_minitouch_enabled && m_minitouch_avaiable) { - MinitouchHelper minitouch_helper(m_minitouch_props); - return input_to_minitouch(minitouch_helper.down(p.x, p.y) + minitouch_helper.up()); + Minitoucher toucher(std::bind(&Controller::input_to_minitouch, this, std::placeholders::_1), m_minitouch_props); + return toucher.down(p.x, p.y) && toucher.up(); } else { std::string cur_cmd = @@ -1049,8 +1047,8 @@ bool asst::Controller::swipe_without_scale(const Point& p1, const Point& p2, int const auto& opt = Configer.get_options(); if (m_minitouch_enabled && m_minitouch_avaiable) { - MinitouchHelper minitouch_helper(m_minitouch_props); - input_to_minitouch(minitouch_helper.down(x1, y1)); + Minitoucher toucher(std::bind(&Controller::input_to_minitouch, this, std::placeholders::_1), m_minitouch_props); + toucher.down(x1, y1); if (duration == 0) { duration = 200; } @@ -1060,28 +1058,28 @@ bool asst::Controller::swipe_without_scale(const Point& p1, const Point& p2, int double v0x = static_cast(_x2 - _x1) / _duration - accelerationx * _duration; double v0y = static_cast(_y2 - _y1) / _duration - accelerationy * _duration; - constexpr int TimeInterval = MinitouchHelper::DefaultSwipeDelay; + constexpr int TimeInterval = Minitoucher::DefaultSwipeDelay; for (int cur_time = TimeInterval; cur_time < _duration; cur_time += TimeInterval) { int cur_x = _x1 + static_cast(v0x * cur_time + accelerationx * cur_time * cur_time); int cur_y = _y1 + static_cast(v0y * cur_time + accelerationy * cur_time * cur_time); if (cur_x < 0 || cur_x > m_minitouch_props.max_x || cur_y < 0 || cur_y > m_minitouch_props.max_y) { continue; } - input_to_minitouch(minitouch_helper.move(cur_x, cur_y)); + toucher.move(cur_x, cur_y); } if (_x2 >= 0 && _x2 <= m_minitouch_props.max_x && _y2 >= 0 && _y2 <= m_minitouch_props.max_y) { - input_to_minitouch(minitouch_helper.move(_x2, _y2)); + toucher.move(_x2, _y2); } }; minitouch_move(x1, y1, x2, y2, duration); constexpr int ExtraEndDelay = 100; // 停留终点 - input_to_minitouch(minitouch_helper.wait(ExtraEndDelay)); + toucher.wait(ExtraEndDelay); if (extra_swipe && opt.minitouch_extra_swipe_duration > 0) { minitouch_move(x2, y2, x2, y2 - opt.minitouch_extra_swipe_dist, opt.minitouch_extra_swipe_duration); duration += opt.minitouch_extra_swipe_duration; } - return input_to_minitouch(minitouch_helper.up()); + return toucher.up(); } else { std::string cur_cmd = diff --git a/src/MeoAssistant/Controller.h b/src/MeoAssistant/Controller.h index b61bc27292..2d145b26e7 100644 --- a/src/MeoAssistant/Controller.h +++ b/src/MeoAssistant/Controller.h @@ -168,7 +168,6 @@ namespace asst bool m_minitouch_avaiable = false; // 状态 #ifdef _WIN32 - HANDLE m_minitouch_child_read = nullptr; HANDLE m_minitouch_parent_write = nullptr; ASST_AUTO_DEDUCED_ZERO_INIT_START PROCESS_INFORMATION m_minitouch_process_info = { nullptr }; @@ -207,32 +206,53 @@ namespace asst double y_scaling = 0; } m_minitouch_props; - class MinitouchHelper + class Minitoucher { + public: + using InputFunc = std::function; + public: static constexpr int DefaultClickDelay = 50; static constexpr int DefaultSwipeDelay = 2; static constexpr int ExtraDelay = 100; - MinitouchHelper(const MinitouchProps& props, bool auto_sleep = true) - : m_props(props), m_auto_sleep(auto_sleep) + Minitoucher(InputFunc func, const MinitouchProps& props, bool auto_sleep = true) + : m_input_func(func), m_props(props), m_auto_sleep(auto_sleep) {} - ~MinitouchHelper() + ~Minitoucher() { if (m_auto_sleep) { sleep(); } } - [[nodiscard]] inline std::string reset() { return "r\n"; } - [[nodiscard]] inline std::string commit() { return "c\n"; } + bool reset() { return m_input_func(reset_cmd()); } + bool commit() { return m_input_func(commit_cmd()); } + bool down(int x, int y, int wait_ms = DefaultClickDelay, bool with_commit = true, int contact = 0) + { + return m_input_func(down_cmd(x, y, wait_ms, with_commit, contact)); + } + bool move(int x, int y, int wait_ms = DefaultSwipeDelay, bool with_commit = true, int contact = 0) + { + return m_input_func(move_cmd(x, y, wait_ms, with_commit, contact)); + } + bool up(int wait_ms = DefaultClickDelay, bool with_commit = true, int contact = 0) + { + return m_input_func(up_cmd(wait_ms, with_commit, contact)); + } + bool wait(int ms) { return m_input_func(wait_cmd(ms)); } + void clear() noexcept { m_wait_ms_count = 0; } + + private: + [[nodiscard]] std::string reset_cmd() const noexcept { return "r\n"; } + [[nodiscard]] std::string commit_cmd() const noexcept { return "c\n"; } #ifdef _MSC_VER #pragma warning(push) #pragma warning(disable : 4996) #endif - [[nodiscard]] std::string down(int x, int y, int wait_ms = DefaultClickDelay, bool with_commit = true, - int contact = 0) + [[nodiscard]] std::string down_cmd(int x, int y, int wait_ms = DefaultClickDelay, bool with_commit = true, + int contact = 0) { // mac 编不过 // std::string str = std::format("d {} {} {} {}\n", contact, x, y, pressure); @@ -241,38 +261,37 @@ namespace asst sprintf(buff, "d %d %d %d %d\n", contact, scale_x(x), scale_y(y), m_props.max_pressure); std::string str = buff; - if (with_commit) str += commit(); - if (wait_ms) str += wait(wait_ms); + if (with_commit) str += commit_cmd(); + if (wait_ms) str += wait_cmd(wait_ms); return str; } - [[nodiscard]] std::string move(int x, int y, int wait_ms = DefaultSwipeDelay, bool with_commit = true, - int contact = 0) + [[nodiscard]] std::string move_cmd(int x, int y, int wait_ms = DefaultSwipeDelay, bool with_commit = true, + int contact = 0) { char buff[64] = { 0 }; sprintf(buff, "m %d %d %d %d\n", contact, scale_x(x), scale_y(y), m_props.max_pressure); std::string str = buff; - if (with_commit) str += commit(); - if (wait_ms) str += wait(wait_ms); + if (with_commit) str += commit_cmd(); + if (wait_ms) str += wait_cmd(wait_ms); return str; } - [[nodiscard]] std::string up(int wait_ms = DefaultClickDelay, bool with_commit = true, int contact = 0) + [[nodiscard]] std::string up_cmd(int wait_ms = DefaultClickDelay, bool with_commit = true, int contact = 0) { char buff[16] = { 0 }; sprintf(buff, "u %d\n", contact); std::string str = buff; - if (with_commit) str += commit(); - if (wait_ms) str += wait(wait_ms); + if (with_commit) str += commit_cmd(); + if (wait_ms) str += wait_cmd(wait_ms); return str; } - [[nodiscard]] std::string wait(int ms) + [[nodiscard]] std::string wait_cmd(int ms) { m_wait_ms_count += ms; - char buff[16] = { 0 }; sprintf(buff, "w %d\n", ms); return buff; @@ -280,7 +299,6 @@ namespace asst #ifdef _MSC_VER #pragma warning(pop) #endif - void clear() noexcept { m_wait_ms_count = 0; } void sleep() { using namespace std::chrono_literals; @@ -292,6 +310,7 @@ namespace asst int scale_x(int x) const noexcept { return static_cast(x * m_props.x_scaling); } int scale_y(int y) const noexcept { return static_cast(y * m_props.y_scaling); } + const std::function m_input_func = nullptr; const MinitouchProps& m_props; int m_wait_ms_count = ExtraDelay; bool m_auto_sleep = false;