forked from fxliang/rimeac.lua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
368 lines (358 loc) · 10.2 KB
/
main.cpp
File metadata and controls
368 lines (358 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
#ifndef _WIN32
#include <lua5.4/lua.hpp>
#else
extern "C" {
#include "include/lua.h"
#include "include/lualib.h"
#include "include/lauxlib.h"
}
#endif
#include "include/LuaBridge/LuaBridge.h"
#include <stdio.h>
#include <rime_api.h>
#include <rime_levers_api.h>
#include <filesystem>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef _WIN32
#include <windows.h>
inline unsigned int SetConsoleOutputCodePage(unsigned int codepage = CP_UTF8) {
unsigned int cp = GetConsoleOutputCP();
SetConsoleOutputCP(codepage);
return cp;
}
#else
inline unsigned int SetConsoleOutputCodePage(unsigned int codepage = 65001) {
return 0;
}
#endif /* _WIN32 */
using SessionsMap = std::map<int, RimeSessionId>;
namespace fs = std::filesystem;
unsigned int codepage;
lua_State* L;
RimeApi* rime;
SessionsMap sessions_map;
RimeSessionId current_session;
void print_status(RimeStatus* status) {
printf("schema: %s / %s\n", status->schema_id, status->schema_name);
printf("status: ");
if (status->is_disabled)
printf("disabled ");
if (status->is_composing)
printf("composing ");
if (status->is_ascii_mode)
printf("ascii ");
if (status->is_full_shape)
printf("full_shape ");
if (status->is_simplified)
printf("simplified ");
printf("\n");
}
void print_composition(RimeComposition* composition) {
const char* preedit = composition->preedit;
if (!preedit)
return;
size_t len = strlen(preedit);
size_t start = composition->sel_start;
size_t end = composition->sel_end;
size_t cursor = composition->cursor_pos;
for (size_t i = 0; i <= len; ++i) {
if (start < end) {
if (i == start) {
putchar('[');
} else if (i == end) {
putchar(']');
}
}
if (i == cursor)
putchar('|');
if (i < len)
putchar(preedit[i]);
}
printf("\n");
}
void print_menu(RimeMenu* menu) {
if (menu->num_candidates == 0)
return;
printf("page: %d%c (of size %d)\n", menu->page_no + 1,
menu->is_last_page ? '$' : ' ', menu->page_size);
for (int i = 0; i < menu->num_candidates; ++i) {
bool highlighted = i == menu->highlighted_candidate_index;
printf("%d. %c%s%c%s\n", i + 1, highlighted ? '[' : ' ',
menu->candidates[i].text, highlighted ? ']' : ' ',
menu->candidates[i].comment ? menu->candidates[i].comment : "");
}
}
void print_context(RimeContext* context) {
if (context->composition.length > 0 || context->menu.num_candidates > 0) {
print_composition(&context->composition);
} else {
printf("(not composing)\n");
}
print_menu(&context->menu);
}
void print_session() {
RimeApi* rime = rime_get_api();
RimeSessionId session_id = current_session;
RIME_STRUCT(RimeCommit, commit);
RIME_STRUCT(RimeStatus, status);
RIME_STRUCT(RimeContext, context);
if (rime->get_commit(session_id, &commit)) {
printf("commit: %s\n", commit.text);
rime->free_commit(&commit);
}
if (rime->get_status(session_id, &status)) {
print_status(&status);
rime->free_status(&status);
}
if (rime->get_context(session_id, &context)) {
print_context(&context);
rime->free_context(&context);
}
}
void on_message(void* context_object,
RimeSessionId session_id,
const char* message_type,
const char* message_value) {
printf("message: [%p] [%s] %s\n", (void*)session_id, message_type, message_value);
RimeApi* rime = rime_get_api();
if (RIME_API_AVAILABLE(rime, get_state_label) &&
!strcmp(message_type, "option")) {
Bool state = message_value[0] != '!';
const char* option_name = message_value + !state;
const char* state_label =
rime->get_state_label(session_id, option_name, state);
if (state_label) {
printf("updated option: %s = %d // %s\n", option_name, state,
state_label);
}
}
}
inline void init_env() {
L = luaL_newstate();
luaL_openlibs(L);
codepage = SetConsoleOutputCodePage();
}
inline void finalize_lua(){
lua_close(L);
}
inline void finalize_env(){
finalize_lua();
if (!rime)
rime = rime_get_api();
rime->finalize();
SetConsoleOutputCodePage(codepage);
}
void setup_rime(const char* app_name, const char* shared, const char* usr,
const char* log) {
if (!rime)
rime = rime_get_api();
RIME_STRUCT(RimeTraits, traits);
traits.app_name = app_name;
traits.shared_data_dir = shared;
traits.user_data_dir = usr;
traits.log_dir = log;
rime->setup(&traits);
rime->set_notification_handler(&on_message, NULL);
if (!fs::exists(log)) {
fs::create_directories(log);
}
}
void init_rime() {
fprintf(stderr, "initializing...\n");
if (!rime)
rime = rime_get_api();
rime->initialize(NULL);
if (rime->start_maintenance(True))
rime->join_maintenance_thread();
fprintf(stderr, "ready.\n");
}
void finalize_rime() {
if (!rime)
rime = rime_get_api();
rime->finalize();
}
bool simulate_keys(const char* keys) {
if (!rime) {
fprintf(stderr, "Please init rime first!\n");
return false;
}
//printf("simulate keys on session: %p, %s\n", (void*)current_session, keys);
return rime->simulate_key_sequence(current_session, keys);
}
bool select_schema(const char* schema_id) {
if (!rime) {
fprintf(stderr, "Please init rime first!\n");
return false;
}
return rime->select_schema(current_session, schema_id);
}
void set_option(const char* option_name, bool value) {
if (!rime) {
fprintf(stderr, "Please init rime first!\n");
return;
}
rime->set_option(current_session, option_name, value);
}
bool select_candidate(int index) {
if (!rime) {
fprintf(stderr, "Please init rime first!\n");
return false;
}
return (index > 0 &&
rime->select_candidate_on_current_page(current_session, (index-1)));
}
void print_sessions() {
printf("current sessions list:\n");
for (const auto& p : sessions_map) {
char schema_id[256] = {0};
rime->get_current_schema(p.second, schema_id, sizeof(schema_id));
char mk = (p.second == current_session) ? '>' : ' ';
printf("%c %d. session_id: %p, schema_id: %s\n", mk, p.first, (void*)p.second,
schema_id);
}
}
bool add_session() {
if (!rime) {
fprintf(stderr, "Please init rime first!\n");
return false;
}
RimeSessionId id = rime->create_session();
if (!id) {
fprintf(stderr, "Error creating new rime session.\n");
return false;
}
if (sessions_map.size())
sessions_map[sessions_map.rbegin()->first + 1] = id;
else
sessions_map[1] = id;
current_session = id;
return true;
}
void destroy_sessions() {
if (!rime) {
fprintf(stderr, "Please init rime first!\n");
return;
}
for(const auto& s : sessions_map) {
printf("destroy session: %p\n", (void*)s.second);
rime->destroy_session(s.second);
}
sessions_map.clear();
}
void kill_session(int id) {
if (!rime) {
fprintf(stderr, "Please init rime first!\n");
return;
}
auto it = sessions_map.find(id);
if (it == sessions_map.end()) {
fprintf(stderr, "no session by this index!\n");
return;
}
if (it != sessions_map.begin())
it--;
else
it++;
printf("destroy session: %p\n", (void*)sessions_map[id]);
rime->destroy_session(sessions_map[id]);
sessions_map.erase(id);
current_session = it->second;
}
RimeSessionId get_session(int index) {
if (index == 0)
return current_session;
else if (sessions_map.find(index) != sessions_map.end())
return (RimeSessionId)sessions_map[index];
else
return 0;
}
bool switch_session(int index) {
RimeSessionId id = get_session(index);
if (!id)
return false;
else
current_session = id;
return true;
}
int get_index_of_current_session() {
for(auto& p : sessions_map) {
if(p.second == current_session)
return p.first;
}
return 0;
}
int get_index_of_session(RimeSessionId id) {
for(auto& p : sessions_map) {
if(p.second == id)
return p.first;
}
return 0;
}
std::vector<std::string> get_candidates() {
std::vector<std::string> ret;
RIME_STRUCT(RimeContext, context);
if (rime->get_context(current_session, &context)) {
if(context.menu.num_candidates) {
for (int i = 0; i < context.menu.num_candidates; ++i) {
ret.push_back(context.menu.candidates[i].text);
}
}
rime->free_context(&context);
}
return ret;
}
std::vector<std::string> get_comments() {
std::vector<std::string> ret;
RIME_STRUCT(RimeContext, context);
if (rime->get_context(current_session, &context)) {
if(context.menu.num_candidates) {
for (int i = 0; i < context.menu.num_candidates; ++i) {
ret.push_back(context.menu.candidates[i].comment ?
context.menu.candidates[i].comment : "");
}
}
rime->free_context(&context);
}
return ret;
}
int main(int argc, char* argv[]){
//printf("hello world in c++!\n");
init_env();
// --------------------------------------------------------------------------
luabridge::getGlobalNamespace(L)
.beginNamespace("rimeac")
.addFunction("setup_rime", &setup_rime)
.addFunction("init_rime", &init_rime)
.addFunction("finalize_rime", &finalize_rime)
// handle sessions without params
.addFunction("destroy_sessions", &destroy_sessions)
.addFunction("print_sessions", &print_sessions)
.addFunction("add_session", &add_session)
// param with session index start from 1
.addFunction("kill_session", &kill_session)
.addFunction("get_session", &get_session)
.addFunction("switch_session", &switch_session)
// on current session
.addFunction("get_candidates", &get_candidates)
.addFunction("get_comments", &get_comments)
.addFunction("print_session", &print_session)
.addFunction("simulate_keys", &simulate_keys)
.addFunction("select_schema", &select_schema)
.addFunction("select_candidate", &select_candidate)
.addFunction("set_option", &set_option)
.addFunction("get_index_of_current_session", &get_index_of_current_session)
.addFunction("get_index_of_session", &get_index_of_session)
.endNamespace();
// --------------------------------------------------------------------------
int st = luaL_dofile(L, "script.lua");
if (st) {
const char* error_msg = lua_tostring(L, -1);
printf("Error: %s\n", error_msg);
lua_pop(L, -1);
}
// --------------------------------------------------------------------------
finalize_env();
return 0;
}