1 #include "rime_config.h"
2 #include "rime_settings.h"
3 #include <rime_api.h>
4 #include <string.h>
5 
6 extern RimeApi *rime_api;
7 
8 static struct ColorSchemeDefinition preset_color_schemes[] = {
9   { "aqua", 0xffffff, 0x0a3dfa },
10   { "azure", 0xffffff, 0x0a3dea },
11   { "ink", 0xffffff, 0x000000 },
12   { "luna", 0x000000, 0xffff7f },
13   { NULL, 0, 0 }
14 };
15 
16 static struct IBusRimeSettings ibus_rime_settings_default = {
17   .embed_preedit_text = TRUE,
18   .preedit_style = PREEDIT_STYLE_COMPOSITION,
19   .cursor_type = CURSOR_TYPE_INSERT,
20   .lookup_table_orientation = IBUS_ORIENTATION_SYSTEM,
21   .color_scheme = NULL,
22 };
23 
24 struct IBusRimeSettings g_ibus_rime_settings;
25 
26 static void
select_color_scheme(struct IBusRimeSettings * settings,const char * color_scheme_id)27 select_color_scheme(struct IBusRimeSettings* settings,
28 		    const char* color_scheme_id)
29 {
30   struct ColorSchemeDefinition* c;
31   for (c = preset_color_schemes; c->color_scheme_id; ++c) {
32     if (!strcmp(c->color_scheme_id, color_scheme_id)) {
33       settings->color_scheme = c;
34       g_debug("selected color scheme: %s", color_scheme_id);
35       return;
36     }
37   }
38   // fallback to default
39   settings->color_scheme = NULL;
40 }
41 
42 void
ibus_rime_load_settings()43 ibus_rime_load_settings()
44 {
45   g_ibus_rime_settings = ibus_rime_settings_default;
46 
47   RimeConfig config = {0};
48   if (!rime_api->config_open("ibus_rime", &config)) {
49     g_error("error loading settings for ibus_rime");
50     return;
51   }
52 
53   Bool inline_preedit = False;
54   if (rime_api->config_get_bool(
55           &config, "style/inline_preedit", &inline_preedit)) {
56     g_ibus_rime_settings.embed_preedit_text = !!inline_preedit;
57   }
58 
59   const char* preedit_style_str =
60       rime_api->config_get_cstring(&config, "style/preedit_style");
61   if(preedit_style_str) {
62     if(!strcmp(preedit_style_str, "composition")) {
63       g_ibus_rime_settings.preedit_style = PREEDIT_STYLE_COMPOSITION;
64     } else if(!strcmp(preedit_style_str, "preview")) {
65       g_ibus_rime_settings.preedit_style = PREEDIT_STYLE_PREVIEW;
66     }
67   }
68 
69   const char* cursor_type_str =
70       rime_api->config_get_cstring(&config, "style/cursor_type");
71   if (cursor_type_str) {
72     if (!strcmp(cursor_type_str, "insert")) {
73       g_ibus_rime_settings.cursor_type = CURSOR_TYPE_INSERT;
74     } else if (!strcmp(cursor_type_str, "select")) {
75       g_ibus_rime_settings.cursor_type = CURSOR_TYPE_SELECT;
76     }
77   }
78 
79   Bool horizontal = False;
80   if (rime_api->config_get_bool(&config, "style/horizontal", &horizontal)) {
81     g_ibus_rime_settings.lookup_table_orientation =
82       horizontal ? IBUS_ORIENTATION_HORIZONTAL : IBUS_ORIENTATION_VERTICAL;
83   }
84 
85   const char* color_scheme =
86     rime_api->config_get_cstring(&config, "style/color_scheme");
87   if (color_scheme) {
88     select_color_scheme(&g_ibus_rime_settings, color_scheme);
89   }
90 
91   rime_api->config_close(&config);
92 }
93