diff --git a/tools/rime_api_console.cc b/tools/rime_api_console.cc index 5360ad9f7..f0492cdd4 100644 --- a/tools/rime_api_console.cc +++ b/tools/rime_api_console.cc @@ -207,6 +207,14 @@ void on_message(void* context_object, } } +RimeSessionId ensure_session(RimeApi* rime) { + RimeSessionId id = rime->create_session(); + if (!id) { + fprintf(stderr, "Error creating rime session.\n"); + } + return id; +} + int main(int argc, char* argv[]) { unsigned int codepage = SetConsoleOutputCodePage(); RimeApi* rime = rime_get_api(); @@ -225,13 +233,7 @@ 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"); - SetConsoleOutputCodePage(codepage); - return 1; - } - + RimeSessionId session_id = 0; const int kMaxLength = 99; char line[kMaxLength + 1] = {0}; while (fgets(line, kMaxLength, stdin) != NULL) { @@ -241,6 +243,11 @@ int main(int argc, char* argv[]) { break; } } + if (!rime->find_session(session_id) && + !(session_id = ensure_session(rime))) { + SetConsoleOutputCodePage(codepage); + return 1; + } if (!strcmp(line, "exit")) break; else if (!strcmp(line, "reload")) {