Skip to content

Commit cab75df

Browse files
committed
feat: get ascii mode by command line
1 parent 75fcbef commit cab75df

8 files changed

Lines changed: 67 additions & 1 deletion

File tree

RimeWithWeasel/RimeWithWeasel.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,15 @@ void RimeWithWeaselHandler::EndMaintenance() {
549549
m_session_status_map.clear();
550550
}
551551

552+
bool RimeWithWeaselHandler::GetStatus(EatLine eat) {
553+
if (m_active_session) {
554+
_Respond(m_active_session, eat);
555+
return true;
556+
} else {
557+
return false;
558+
}
559+
}
560+
552561
void RimeWithWeaselHandler::SetOption(WeaselSessionId ipc_id,
553562
const std::string& opt,
554563
bool val) {

WeaselIPC/WeaselClientImpl.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,11 @@ bool ClientImpl::Echo() {
176176
return (serverEcho == session_id);
177177
}
178178

179+
bool ClientImpl::GetStatus() {
180+
LRESULT ret = _SendMessage(WEASEL_IPC_GET_STATUS, 0, session_id);
181+
return ret != 0;
182+
}
183+
179184
bool ClientImpl::GetResponseData(ResponseHandler const& handler) {
180185
if (!handler) {
181186
return false;
@@ -293,6 +298,10 @@ bool Client::Echo() {
293298
return m_pImpl->Echo();
294299
}
295300

301+
bool Client::GetStatus() {
302+
return m_pImpl->GetStatus();
303+
}
304+
296305
bool Client::GetResponseData(ResponseHandler handler) {
297306
return m_pImpl->GetResponseData(handler);
298307
}

WeaselIPC/WeaselClientImpl.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class ClientImpl {
2727
void FocusIn();
2828
void FocusOut();
2929
void TrayCommand(UINT menuId);
30+
bool GetStatus();
3031
bool GetResponseData(ResponseHandler const& handler);
3132

3233
protected:
@@ -46,4 +47,4 @@ class ClientImpl {
4647
PipeChannel<PipeMessage> channel;
4748
};
4849

49-
} // namespace weasel
50+
} // namespace weasel

WeaselIPCServer/WeaselServerImpl.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,20 @@ DWORD ServerImpl::OnChangePage(WEASEL_IPC_COMMAND uMsg,
355355
return 0;
356356
}
357357

358+
DWORD ServerImpl::OnGetStatus(WEASEL_IPC_COMMAND uMsg,
359+
DWORD wParam,
360+
DWORD lParam) {
361+
if (m_pRequestHandler) {
362+
auto eat = [this](std::wstring& msg) -> bool {
363+
*channel << msg;
364+
return true;
365+
};
366+
m_pRequestHandler->GetStatus(eat);
367+
return 1;
368+
}
369+
return 0;
370+
}
371+
358372
#define MAP_PIPE_MSG_HANDLE(__msg, __wParam, __lParam) \
359373
{ \
360374
auto lParam = __lParam; \
@@ -394,6 +408,7 @@ void ServerImpl::HandlePipeMessage(PipeMessage pipe_msg, _Resp resp) {
394408
OnHighlightCandidateOnCurrentPage);
395409
PIPE_MSG_HANDLE(WEASEL_IPC_CHANGE_PAGE, OnChangePage);
396410
PIPE_MSG_HANDLE(WEASEL_IPC_TRAY_COMMAND, OnCommand);
411+
PIPE_MSG_HANDLE(WEASEL_IPC_GET_STATUS, OnGetStatus);
397412
END_MAP_PIPE_MSG_HANDLE(result);
398413

399414
resp(result);

WeaselIPCServer/WeaselServerImpl.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ class ServerImpl : public CWindowImpl<ServerImpl, CWindow, ServerWinTraits>
7070
DWORD wParam,
7171
DWORD lParam);
7272
DWORD OnChangePage(WEASEL_IPC_COMMAND uMsg, DWORD wParam, DWORD lParam);
73+
DWORD OnGetStatus(WEASEL_IPC_COMMAND uMsg, DWORD wParam, DWORD lParam);
7374

7475
public:
7576
ServerImpl();

WeaselServer/WeaselServer.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "stdafx.h"
66
#include "resource.h"
77
#include "WeaselService.h"
8+
#include <ResponseParser.h>
89
#include <WeaselIPC.h>
910
#include <WeaselUI.h>
1011
#include <RimeWithWeasel.h>
@@ -15,6 +16,7 @@
1516
#include <WinUser.h>
1617
#include <memory>
1718
#include <atlstr.h>
19+
#include <iostream>
1820
#pragma comment(lib, "Shcore.lib")
1921
CAppModule _Module;
2022

@@ -71,6 +73,30 @@ int WINAPI _tWinMain(HINSTANCE hInstance,
7173
WeaselServerApp::explore(WeaselServerApp::install_dir());
7274
return 0;
7375
}
76+
if (!wcscmp(L"/isascii", lpstrCmdLine)) {
77+
// https://speedyleion.github.io/c/c++/windows/2021/07/11/WinMain-and-stdout.html
78+
if (!GetStdHandle(STD_OUTPUT_HANDLE)) {
79+
if (AttachConsole(ATTACH_PARENT_PROCESS)) {
80+
freopen("CONOUT$", "wb", stdout);
81+
}
82+
}
83+
84+
weasel::Client client;
85+
if (client.Connect()) // try to connect to running server
86+
{
87+
if (client.GetStatus()) {
88+
std::wstring commit;
89+
weasel::Status status;
90+
weasel::ResponseParser parser(&commit, NULL, &status);
91+
bool ok = client.GetResponseData(std::ref(parser));
92+
if (ok) {
93+
int out = status.ascii_mode ? 1 : 0;
94+
std::cout << out << std::endl;
95+
}
96+
}
97+
}
98+
return 0;
99+
}
74100
if (!wcscmp(L"/ascii", lpstrCmdLine) || !wcscmp(L"/nascii", lpstrCmdLine)) {
75101
weasel::Client client;
76102
bool ascii = !wcscmp(L"/ascii", lpstrCmdLine);

include/RimeWithWeasel.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ class RimeWithWeaselHandler : public weasel::RequestHandler {
5757
virtual void UpdateInputPosition(RECT const& rc, WeaselSessionId ipc_id);
5858
virtual void StartMaintenance();
5959
virtual void EndMaintenance();
60+
virtual bool GetStatus(EatLine eat);
6061
virtual void SetOption(WeaselSessionId ipc_id,
6162
const std::string& opt,
6263
bool val);

include/WeaselIPC.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ enum WEASEL_IPC_COMMAND {
3131
WEASEL_IPC_SELECT_CANDIDATE_ON_CURRENT_PAGE,
3232
WEASEL_IPC_HIGHLIGHT_CANDIDATE_ON_CURRENT_PAGE,
3333
WEASEL_IPC_CHANGE_PAGE,
34+
WEASEL_IPC_GET_STATUS,
3435
WEASEL_IPC_LAST_COMMAND
3536
};
3637

@@ -91,6 +92,7 @@ struct RequestHandler {
9192
virtual void EndMaintenance() {}
9293
virtual void SetOption(DWORD session_id, const std::string& opt, bool val) {}
9394
virtual void UpdateColorTheme(BOOL darkMode) {}
95+
virtual bool GetStatus(EatLine eat) { return false; }
9496
};
9597

9698
// 處理server端回應之物件
@@ -150,6 +152,8 @@ class Client {
150152
void FocusOut();
151153
// 托盤菜單
152154
void TrayCommand(UINT menuId);
155+
// 獲取當前輸入法狀態
156+
bool GetStatus();
153157
// 读取server返回的数据
154158
bool GetResponseData(ResponseHandler handler);
155159

0 commit comments

Comments
 (0)