diff --git a/RimeWithWeasel/RimeWithWeasel.cpp b/RimeWithWeasel/RimeWithWeasel.cpp index 9fbdd8ef5..d57686c89 100644 --- a/RimeWithWeasel/RimeWithWeasel.cpp +++ b/RimeWithWeasel/RimeWithWeasel.cpp @@ -549,6 +549,15 @@ void RimeWithWeaselHandler::EndMaintenance() { m_session_status_map.clear(); } +bool RimeWithWeaselHandler::GetStatus(EatLine eat) { + if (m_active_session) { + _Respond(m_active_session, eat); + return true; + } else { + return false; + } +} + void RimeWithWeaselHandler::SetOption(WeaselSessionId ipc_id, const std::string& opt, bool val) { diff --git a/WeaselIPC/WeaselClientImpl.cpp b/WeaselIPC/WeaselClientImpl.cpp index 26122ad50..2612ec05f 100644 --- a/WeaselIPC/WeaselClientImpl.cpp +++ b/WeaselIPC/WeaselClientImpl.cpp @@ -174,6 +174,11 @@ bool ClientImpl::Echo() { return (serverEcho == session_id); } +bool ClientImpl::GetStatus() { + LRESULT ret = _SendMessage(WEASEL_IPC_GET_STATUS, 0, session_id); + return ret != 0; +} + bool ClientImpl::GetResponseData(ResponseHandler const& handler) { if (!handler) { return false; @@ -280,6 +285,10 @@ bool Client::Echo() { return m_pImpl->Echo(); } +bool Client::GetStatus() { + return m_pImpl->GetStatus(); +} + bool Client::GetResponseData(ResponseHandler handler) { return m_pImpl->GetResponseData(handler); } diff --git a/WeaselIPC/WeaselClientImpl.h b/WeaselIPC/WeaselClientImpl.h index 760279286..b7217552b 100644 --- a/WeaselIPC/WeaselClientImpl.h +++ b/WeaselIPC/WeaselClientImpl.h @@ -27,6 +27,7 @@ class ClientImpl { void FocusIn(); void FocusOut(); void TrayCommand(UINT menuId); + bool GetStatus(); bool GetResponseData(ResponseHandler const& handler); protected: @@ -46,4 +47,4 @@ class ClientImpl { PipeChannel channel; }; -} // namespace weasel \ No newline at end of file +} // namespace weasel diff --git a/WeaselIPCServer/WeaselServerImpl.cpp b/WeaselIPCServer/WeaselServerImpl.cpp index 3b5075b42..ebb4636c0 100644 --- a/WeaselIPCServer/WeaselServerImpl.cpp +++ b/WeaselIPCServer/WeaselServerImpl.cpp @@ -355,6 +355,20 @@ DWORD ServerImpl::OnChangePage(WEASEL_IPC_COMMAND uMsg, return 0; } +DWORD ServerImpl::OnGetStatus(WEASEL_IPC_COMMAND uMsg, + DWORD wParam, + DWORD lParam) { + if (m_pRequestHandler) { + auto eat = [this](std::wstring& msg) -> bool { + *channel << msg; + return true; + }; + m_pRequestHandler->GetStatus(eat); + return 1; + } + return 0; +} + #define MAP_PIPE_MSG_HANDLE(__msg, __wParam, __lParam) \ { \ auto lParam = __lParam; \ @@ -394,6 +408,7 @@ void ServerImpl::HandlePipeMessage(PipeMessage pipe_msg, _Resp resp) { OnHighlightCandidateOnCurrentPage); PIPE_MSG_HANDLE(WEASEL_IPC_CHANGE_PAGE, OnChangePage); PIPE_MSG_HANDLE(WEASEL_IPC_TRAY_COMMAND, OnCommand); + PIPE_MSG_HANDLE(WEASEL_IPC_GET_STATUS, OnGetStatus); END_MAP_PIPE_MSG_HANDLE(result); resp(result); diff --git a/WeaselIPCServer/WeaselServerImpl.h b/WeaselIPCServer/WeaselServerImpl.h index a4d123ddb..28ead381b 100644 --- a/WeaselIPCServer/WeaselServerImpl.h +++ b/WeaselIPCServer/WeaselServerImpl.h @@ -70,6 +70,7 @@ class ServerImpl : public CWindowImpl DWORD wParam, DWORD lParam); DWORD OnChangePage(WEASEL_IPC_COMMAND uMsg, DWORD wParam, DWORD lParam); + DWORD OnGetStatus(WEASEL_IPC_COMMAND uMsg, DWORD wParam, DWORD lParam); public: ServerImpl(); diff --git a/WeaselServer/WeaselServer.cpp b/WeaselServer/WeaselServer.cpp index b4cf8b2f1..fbb830dd7 100644 --- a/WeaselServer/WeaselServer.cpp +++ b/WeaselServer/WeaselServer.cpp @@ -5,6 +5,7 @@ #include "stdafx.h" #include "resource.h" #include "WeaselService.h" +#include #include #include #include @@ -15,6 +16,7 @@ #include #include #include +#include #pragma comment(lib, "Shcore.lib") CAppModule _Module; @@ -71,6 +73,30 @@ int WINAPI _tWinMain(HINSTANCE hInstance, WeaselServerApp::explore(WeaselServerApp::install_dir()); return 0; } + if (!wcscmp(L"/isascii", lpstrCmdLine)) { + // https://speedyleion.github.io/c/c++/windows/2021/07/11/WinMain-and-stdout.html + if (!GetStdHandle(STD_OUTPUT_HANDLE)) { + if (AttachConsole(ATTACH_PARENT_PROCESS)) { + freopen("CONOUT$", "wb", stdout); + } + } + + weasel::Client client; + if (client.Connect()) // try to connect to running server + { + if (client.GetStatus()) { + std::wstring commit; + weasel::Status status; + weasel::ResponseParser parser(&commit, NULL, &status); + bool ok = client.GetResponseData(std::ref(parser)); + if (ok) { + int out = status.ascii_mode ? 1 : 0; + std::cout << out << std::endl; + } + } + } + return 0; + } if (!wcscmp(L"/ascii", lpstrCmdLine) || !wcscmp(L"/nascii", lpstrCmdLine)) { weasel::Client client; bool ascii = !wcscmp(L"/ascii", lpstrCmdLine); diff --git a/include/RimeWithWeasel.h b/include/RimeWithWeasel.h index 6fb0a4cab..c94bfeed8 100644 --- a/include/RimeWithWeasel.h +++ b/include/RimeWithWeasel.h @@ -57,6 +57,7 @@ class RimeWithWeaselHandler : public weasel::RequestHandler { virtual void UpdateInputPosition(RECT const& rc, WeaselSessionId ipc_id); virtual void StartMaintenance(); virtual void EndMaintenance(); + virtual bool GetStatus(EatLine eat); virtual void SetOption(WeaselSessionId ipc_id, const std::string& opt, bool val); diff --git a/include/WeaselIPC.h b/include/WeaselIPC.h index 79cc99fa5..2459c7164 100644 --- a/include/WeaselIPC.h +++ b/include/WeaselIPC.h @@ -31,6 +31,7 @@ enum WEASEL_IPC_COMMAND { WEASEL_IPC_SELECT_CANDIDATE_ON_CURRENT_PAGE, WEASEL_IPC_HIGHLIGHT_CANDIDATE_ON_CURRENT_PAGE, WEASEL_IPC_CHANGE_PAGE, + WEASEL_IPC_GET_STATUS, WEASEL_IPC_LAST_COMMAND }; @@ -91,6 +92,7 @@ struct RequestHandler { virtual void EndMaintenance() {} virtual void SetOption(DWORD session_id, const std::string& opt, bool val) {} virtual void UpdateColorTheme(BOOL darkMode) {} + virtual bool GetStatus(EatLine eat) { return false; } }; // 處理server端回應之物件 @@ -150,6 +152,8 @@ class Client { void FocusOut(); // 托盤菜單 void TrayCommand(UINT menuId); + // 獲取當前輸入法狀態 + bool GetStatus(); // 读取server返回的数据 bool GetResponseData(ResponseHandler handler);