From 4f8bad32bf53d20a83eb3c5338bf1cb04ab9d9f4 Mon Sep 17 00:00:00 2001 From: ksqsf Date: Wed, 12 Jun 2024 18:34:35 +0200 Subject: [PATCH] fix(tools): recreate session when necessary --- tools/rime_api_console.cc | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/tools/rime_api_console.cc b/tools/rime_api_console.cc index b6df28a31..1f8b4852e 100644 --- a/tools/rime_api_console.cc +++ b/tools/rime_api_console.cc @@ -187,6 +187,15 @@ void on_message(void* context_object, } } +bool ensure_session(RimeApi* rime, RimeSessionId& session_id) { + session_id = rime->create_session(); + if (!session_id) { + fprintf(stderr, "Error creating rime session.\n"); + return false; + } + return true; +} + int main(int argc, char* argv[]) { unsigned int codepage = SetConsoleOutputCodePage(); RimeApi* rime = rime_get_api(); @@ -205,9 +214,8 @@ int main(int argc, char* argv[]) { rime->join_maintenance_thread(); fprintf(stderr, "ready.\n"); - RimeSessionId session_id = rime->create_session(); - if (!session_id) { - fprintf(stderr, "Error creating rime session.\n"); + RimeSessionId session_id; + if (!ensure_session(rime, session_id)) { SetConsoleOutputCodePage(codepage); return 1; } @@ -221,6 +229,10 @@ int main(int argc, char* argv[]) { break; } } + if (!rime->find_session(session_id) && !ensure_session(rime, session_id)) { + SetConsoleOutputCodePage(codepage); + return 1; + } if (!strcmp(line, "exit")) break; else if (!strcmp(line, "reload")) {