From 111df267196b39a1a5d28c01653d34acd219efe4 Mon Sep 17 00:00:00 2001 From: fanlumaster <1730976608@qq.com> Date: Fri, 9 Jan 2026 21:47:14 +0800 Subject: [PATCH] [ipc]: Init/Uninit ipc in SetFocus instead of SetThreadFocus --- src/IME/MetasequoiaIME.cpp | 29 +++++++---------------------- src/IME/MetasequoiaIME.h | 29 ++++++++++++++++------------- src/IPC/Ipc.cpp | 14 +++++++++++--- src/IPC/Ipc.h | 4 ++++ src/Thread/ThreadFocusSink.cpp | 4 ---- src/Thread/ThreadMgrEventSink.cpp | 13 +++++++++++++ 6 files changed, 51 insertions(+), 42 deletions(-) diff --git a/src/IME/MetasequoiaIME.cpp b/src/IME/MetasequoiaIME.cpp index c681641f..875a8c7f 100644 --- a/src/IME/MetasequoiaIME.cpp +++ b/src/IME/MetasequoiaIME.cpp @@ -482,24 +482,6 @@ void CMetasequoiaIME::IpcWorkerThread(CMetasequoiaIME *pIME) { while (!pIME->_shouldStopIpcThread) { - /* 重新建立连接 */ - if (Global::hToTsfWorkerThreadPipe == nullptr || Global::hToTsfWorkerThreadPipe == INVALID_HANDLE_VALUE) - { - if (!WaitNamedPipe(FANY_IME_TO_TSF_WORKER_THREAD_NAMED_PIPE, 100)) - { - continue; - } - Global::hToTsfWorkerThreadPipe = CreateFile( // - FANY_IME_TO_TSF_WORKER_THREAD_NAMED_PIPE, // - GENERIC_READ | GENERIC_WRITE, // - 0, // - nullptr, // - OPEN_EXISTING, // - 0, // - nullptr // - ); - } - /* 阻塞读 */ while (!pIME->_shouldStopIpcThread) { @@ -515,9 +497,7 @@ void CMetasequoiaIME::IpcWorkerThread(CMetasequoiaIME *pIME) if (!readResult || bytesRead == 0) { - CloseHandle(Global::hToTsfWorkerThreadPipe); - Global::hToTsfWorkerThreadPipe = nullptr; - break; // 断线,回到外层重连 + break; } if (buf.msg_type == Global::DataToTsfWorkerThreadMsgType::SwitchToEnglish) @@ -539,6 +519,7 @@ void CMetasequoiaIME::IpcWorkerThread(CMetasequoiaIME *pIME) OutputDebugString(fmt::format(L"Switch to Chinese").c_str()); } } + Sleep(100); } } @@ -557,7 +538,7 @@ LRESULT CALLBACK CMetasequoiaIME_WindowProc(HWND hWnd, UINT message, WPARAM wPar switch (message) { - case WM_CheckGlobalCompartment: + case WM_CheckGlobalCompartment: { if (wParam == Global::DataToTsfWorkerThreadMsgType::SwitchToEnglish) { OutputDebugString(fmt::format(L"Wnd Switch to English").c_str()); @@ -570,6 +551,10 @@ LRESULT CALLBACK CMetasequoiaIME_WindowProc(HWND hWnd, UINT message, WPARAM wPar OutputDebugString(fmt::format(L"Switch to Chinese").c_str()); } break; + } + case WM_ConnectNamedpipe: { + break; + } default: return DefWindowProc(hWnd, message, wParam, lParam); } diff --git a/src/IME/MetasequoiaIME.h b/src/IME/MetasequoiaIME.h index f424580b..9721619c 100644 --- a/src/IME/MetasequoiaIME.h +++ b/src/IME/MetasequoiaIME.h @@ -10,18 +10,19 @@ class CCandidateListUIPresenter; class CCompositionProcessorEngine; const DWORD WM_CheckGlobalCompartment = WM_USER; +const DWORD WM_ConnectNamedpipe = WM_USER + 1; LRESULT CALLBACK CMetasequoiaIME_WindowProc(HWND wndHandle, UINT uMsg, WPARAM wParam, LPARAM lParam); class CMetasequoiaIME : public ITfTextInputProcessorEx, - public ITfThreadMgrEventSink, - public ITfTextEditSink, - public ITfKeyEventSink, - public ITfCompositionSink, - public ITfDisplayAttributeProvider, - public ITfActiveLanguageProfileNotifySink, - public ITfThreadFocusSink, - public ITfFunctionProvider, - public ITfFnGetPreferredTouchKeyboardLayout + public ITfThreadMgrEventSink, + public ITfTextEditSink, + public ITfKeyEventSink, + public ITfCompositionSink, + public ITfDisplayAttributeProvider, + public ITfActiveLanguageProfileNotifySink, + public ITfThreadFocusSink, + public ITfFunctionProvider, + public ITfFnGetPreferredTouchKeyboardLayout { public: CMetasequoiaIME(); @@ -150,10 +151,12 @@ class CMetasequoiaIME : public ITfTextInputProcessorEx, // comless helpers static HRESULT CMetasequoiaIME::CreateInstance(REFCLSID rclsid, REFIID riid, _Outptr_result_maybenull_ LPVOID *ppv, - _Out_opt_ HINSTANCE *phInst, BOOL isComLessMode); - static HRESULT CMetasequoiaIME::ComLessCreateInstance(REFGUID rclsid, REFIID riid, _Outptr_result_maybenull_ void **ppv, - _Out_opt_ HINSTANCE *phInst); - static HRESULT CMetasequoiaIME::GetComModuleName(REFGUID rclsid, _Out_writes_(cchPath) WCHAR *wchPath, DWORD cchPath); + _Out_opt_ HINSTANCE *phInst, BOOL isComLessMode); + static HRESULT CMetasequoiaIME::ComLessCreateInstance(REFGUID rclsid, REFIID riid, + _Outptr_result_maybenull_ void **ppv, + _Out_opt_ HINSTANCE *phInst); + static HRESULT CMetasequoiaIME::GetComModuleName(REFGUID rclsid, _Out_writes_(cchPath) WCHAR *wchPath, + DWORD cchPath); static void IpcWorkerThread(CMetasequoiaIME *pIME); diff --git a/src/IPC/Ipc.cpp b/src/IPC/Ipc.cpp index 8a4b0ace..6bb25f98 100644 --- a/src/IPC/Ipc.cpp +++ b/src/IPC/Ipc.cpp @@ -22,8 +22,6 @@ FanyImeNamedpipeDataToTsf namedpipeDataFromServer = {}; /* Data size transfered from Server process */ static const int ServerDtPipeDataSize = 512; -void SendToAuxNamedpipe(std::wstring pipeData); - int InitIpc() { // @@ -149,7 +147,17 @@ int InitNamedpipe() } } - return 0; + if (hPipe == nullptr || hPipe == INVALID_HANDLE_VALUE || hFromServerPipe == nullptr || + hFromServerPipe == INVALID_HANDLE_VALUE || Global::hToTsfWorkerThreadPipe == nullptr || + Global::hToTsfWorkerThreadPipe == INVALID_HANDLE_VALUE) + { + hPipe = nullptr; + hFromServerPipe = nullptr; + Global::hToTsfWorkerThreadPipe = nullptr; + return 0; + } + + return 1; } int CloseIpc() diff --git a/src/IPC/Ipc.h b/src/IPC/Ipc.h index 0f432e07..3ac27aca 100644 --- a/src/IPC/Ipc.h +++ b/src/IPC/Ipc.h @@ -117,6 +117,8 @@ int SendIMEActivationEventToUIProcessViaNamedPipe(); int SendIMEDeactivationEventToUIProcessViaNamedPipe(); int SendIMESwitchEventToUIProcessViaNamedPipe(UINT uImeStatus); +void SendToAuxNamedpipe(std::wstring pipeData); + // // For named pipe // @@ -175,4 +177,6 @@ constexpr UINT SwitchToChinese = 1; inline HANDLE hToTsfWorkerThreadPipe = nullptr; +inline bool g_connected = false; + } // namespace Global \ No newline at end of file diff --git a/src/Thread/ThreadFocusSink.cpp b/src/Thread/ThreadFocusSink.cpp index 2f9b26e2..fe4d5cf7 100644 --- a/src/Thread/ThreadFocusSink.cpp +++ b/src/Thread/ThreadFocusSink.cpp @@ -12,8 +12,6 @@ STDAPI CMetasequoiaIME::OnSetThreadFocus() { - // Connect to namedpipe - InitNamedpipe(); if (_pCandidateListUIPresenter) { ITfDocumentMgr *pCandidateListDocumentMgr = nullptr; @@ -40,8 +38,6 @@ STDAPI CMetasequoiaIME::OnSetThreadFocus() STDAPI CMetasequoiaIME::OnKillThreadFocus() { - // Disconnect from namedpipe - CloseNamedpipe(); if (_pCandidateListUIPresenter) { ITfDocumentMgr *pCandidateListDocumentMgr = nullptr; diff --git a/src/Thread/ThreadMgrEventSink.cpp b/src/Thread/ThreadMgrEventSink.cpp index e7b1264c..fb866d01 100644 --- a/src/Thread/ThreadMgrEventSink.cpp +++ b/src/Thread/ThreadMgrEventSink.cpp @@ -2,6 +2,7 @@ #include "Globals.h" #include "MetasequoiaIME.h" #include "CandidateListUIPresenter.h" +#include "Ipc.h" //+--------------------------------------------------------------------------- // @@ -44,6 +45,18 @@ STDAPI CMetasequoiaIME::OnSetFocus(_In_ ITfDocumentMgr *pDocMgrFocus, _In_ ITfDo { pDocMgrPrevFocus; + if (pDocMgrFocus && !Global::g_connected) + { + + InitNamedpipe(); + Global::g_connected = true; + } + else if (!pDocMgrFocus && Global::g_connected) + { + CloseNamedpipe(); + Global::g_connected = false; + } + _InitTextEditSink(pDocMgrFocus); _UpdateLanguageBarOnSetFocus(pDocMgrFocus);