diff --git a/src/MeoAssistant/Assistant.cpp b/src/MeoAssistant/Assistant.cpp index 3614bc3a98..3f6507bb24 100644 --- a/src/MeoAssistant/Assistant.cpp +++ b/src/MeoAssistant/Assistant.cpp @@ -255,7 +255,7 @@ void Assistant::working_proc() // minitouch 的适配工作太多了,短期弄不完,默认关掉 // 适配好了的任务每个自己单独开 - m_ctrler->set_minitouch_enabled(true); + 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 efa841d2bc..6bded47ef6 100644 --- a/src/MeoAssistant/Controller.cpp +++ b/src/MeoAssistant/Controller.cpp @@ -505,17 +505,20 @@ bool asst::Controller::call_and_hup_minitouch(const std::string& cmd) Log.info(cmd); m_minitouch_avaiable = false; - constexpr int PipeBuffSize = 4096ULL; + constexpr int PipeReadBuffSize = 4096ULL; + constexpr int PipeWriteBuffSize = 64 * 1024ULL; std::string pipe_str; #ifdef _WIN32 - SECURITY_ATTRIBUTES sa_attr_wr { + SECURITY_ATTRIBUTES sa_attr_inherit { .nLength = sizeof(SECURITY_ATTRIBUTES), .lpSecurityDescriptor = nullptr, .bInheritHandle = TRUE, }; - - if (!asst::win32::CreateOverlappablePipe(&m_minitouch_parent_wr, &m_minitouch_child_wr, nullptr, &sa_attr_wr, - PipeBuffSize, true, false)) { + HANDLE pipe_child_write = INVALID_HANDLE_VALUE, pipe_parent_read = 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)) { DWORD err = GetLastError(); Log.error("Failed to create pipe for minitouch, err", err); return false; @@ -525,9 +528,9 @@ 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_parent_wr; - si.hStdOutput = m_minitouch_child_wr; - si.hStdError = m_minitouch_child_wr; + si.hStdInput = m_minitouch_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, @@ -538,7 +541,10 @@ bool asst::Controller::call_and_hup_minitouch(const std::string& cmd) return false; } - auto pipe_buffer = std::make_unique(PipeBuffSize); + 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 { @@ -548,20 +554,23 @@ bool asst::Controller::call_and_hup_minitouch(const std::string& cmd) std::vector wait_handles; OVERLAPPED pipeov { .hEvent = CreateEventW(nullptr, TRUE, FALSE, nullptr) }; - std::ignore = ReadFile(m_minitouch_parent_wr, pipe_buffer.get(), PipeBuffSize, nullptr, &pipeov); + std::ignore = ReadFile(pipe_parent_read, pipe_buffer.get(), PipeReadBuffSize, nullptr, &pipeov); while (!need_exit() && check_timeout()) { if (pipe_str.find('$') != std::string::npos) { break; } DWORD len = 0; - if (!GetOverlappedResult(m_minitouch_parent_wr, &pipeov, &len, FALSE)) { + if (!GetOverlappedResult(pipe_parent_read, &pipeov, &len, FALSE)) { continue; } pipe_str.insert(pipe_str.end(), pipe_buffer.get(), pipe_buffer.get() + len); - std::ignore = ReadFile(m_minitouch_parent_wr, pipe_buffer.get(), PipeBuffSize, nullptr, &pipeov); + std::ignore = ReadFile(pipe_parent_read, pipe_buffer.get(), PipeReadBuffSize, nullptr, &pipeov); } + CloseHandle(pipe_parent_read); + pipe_parent_read = INVALID_HANDLE_VALUE; + #else // !_WIN32 // TODO std::ignore = pipe_str; @@ -602,8 +611,8 @@ bool asst::Controller::input_to_minitouch(const std::string& cmd, int delay_ms) #ifdef _WIN32 DWORD written = 0; - if (!WriteFile(m_minitouch_child_wr, cmd.c_str(), static_cast(cmd.size() * sizeof(std::string::value_type)), - &written, NULL)) { + if (!WriteFile(m_minitouch_parent_write, cmd.c_str(), + static_cast(cmd.size() * sizeof(std::string::value_type)), &written, NULL)) { auto err = GetLastError(); Log.error("Failed to write to minitouch, err", err); return false; @@ -637,13 +646,13 @@ void asst::Controller::release_minitouch(bool force) CloseHandle(m_minitouch_process_info.hThread); m_minitouch_process_info.hThread = nullptr; } - if (m_minitouch_parent_wr) { - CloseHandle(m_minitouch_parent_wr); - m_minitouch_parent_wr = nullptr; + if (m_minitouch_child_read) { + CloseHandle(m_minitouch_child_read); + m_minitouch_child_read = nullptr; } - if (m_minitouch_child_wr) { - CloseHandle(m_minitouch_child_wr); - m_minitouch_child_wr = nullptr; + if (m_minitouch_parent_write) { + CloseHandle(m_minitouch_parent_write); + m_minitouch_parent_write = nullptr; } #endif // _WIN32 diff --git a/src/MeoAssistant/Controller.h b/src/MeoAssistant/Controller.h index 0eb5578037..1737ca8300 100644 --- a/src/MeoAssistant/Controller.h +++ b/src/MeoAssistant/Controller.h @@ -164,8 +164,8 @@ namespace asst bool m_minitouch_avaiable = false; // 状态 #ifdef _WIN32 - HANDLE m_minitouch_parent_wr = nullptr; - HANDLE m_minitouch_child_wr = nullptr; + 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 }; ASST_AUTO_DEDUCED_ZERO_INIT_END diff --git a/src/MeoAssistant/Utils/Platform/AsstPlatformWin32.cpp b/src/MeoAssistant/Utils/Platform/AsstPlatformWin32.cpp index 588af965fb..09b51f4c30 100644 --- a/src/MeoAssistant/Utils/Platform/AsstPlatformWin32.cpp +++ b/src/MeoAssistant/Utils/Platform/AsstPlatformWin32.cpp @@ -34,9 +34,9 @@ bool asst::win32::CreateOverlappablePipe(HANDLE* read, HANDLE* write, SECURITY_A { static std::atomic pipeid {}; auto pipename = std::format(L"\\\\.\\pipe\\maa-pipe-{}-{}", GetCurrentProcessId(), pipeid++); - DWORD read_flag = PIPE_ACCESS_DUPLEX; + DWORD read_flag = PIPE_ACCESS_INBOUND; if (overlapped_read) read_flag |= FILE_FLAG_OVERLAPPED; - DWORD write_flag = GENERIC_WRITE | GENERIC_READ; + DWORD write_flag = GENERIC_WRITE; if (overlapped_write) write_flag |= FILE_FLAG_OVERLAPPED; auto pipe_read = CreateNamedPipeW(pipename.c_str(), read_flag, PIPE_TYPE_BYTE | PIPE_WAIT, 1, bufsize, bufsize, 0, secattr_read);