Enable to modify the iBus keyboard layout without the root permission.
Update from the upstream. (BUILD=4220)
* Enabled to dynamically generate the entries of iBus engines.
* Enabled to modify the iBus keyboard layout without the root permission.
-----
After this change, the contents of /usr/share/ibus/component/mozc.xml is changed as follows.
Before
```
<component>
<name>com.google.IBus.Mozc</name>
<description>Mozc Component</description>
<exec>/usr/lib/ibus-mozc/ibus-engine-mozc --ibus</exec>
<version>0.0.0.0</version>
<author>Google Inc.</author>
<license>New BSD</license>
<homepage>https://github.com/google/mozc</homepage>
<textdomain>ibus-mozc</textdomain>
<engines>
<engine>
<description>Mozc (Japanese Input Method)</description>
<language>ja</language>
<icon>/usr/share/ibus-mozc/product_icon.png</icon>
<rank>80</rank>
<icon_prop_key>InputMode</icon_prop_key>
<symbol>あ</symbol>
<setup>/usr/lib/mozc/mozc_tool --mode=config_dialog</setup>
<name>mozc-jp</name>
<longname>Mozc</longname>
<layout>default</layout>
</engine>
</engines>
</component>
```
After
```
<component>
<name>com.google.IBus.Mozc</name>
<description>Mozc Component</description>
<exec>/usr/lib/ibus-mozc/ibus-engine-mozc --ibus</exec>
<version>0.0.0.0</version>
<author>Google Inc.</author>
<license>New BSD</license>
<homepage>https://github.com/google/mozc</homepage>
<textdomain>ibus-mozc</textdomain>
<engines exec="/usr/lib/ibus-mozc/ibus-engine-mozc --xml" />
</component>
<!-- Settings of <engines> and <layout> are stored in ibus_config.textproto -->
<!-- under the user configuration directory, which is either of: -->
<!-- * $XDG_CONFIG_HOME/mozc/ibus_config.textproto -->
<!-- * $HOME/.config/mozc/ibus_config.textproto -->
<!-- * $HOME/.mozc/ibus_config.textproto -->
```
Then, `/usr/lib/ibus-mozc/ibus-engine-mozc --xml` returns the values of the <engine> tags.
If `ibus_config.textproto` exists under the user config directory.
(i.e. `~/.mozc/ibus_config.textproto` or `~/.config/mozc/ibus_config.textproto`)
Mozc uses the settings in the file. If `ibus_config.textproto` does not exist,
it is created with the default values.
So, the users do not need to modify `/usr/share/ibus/component/mozc.xml` directly.
This is the same mechanism with ibus-anthy.
This change will fix two issues.
* There was no way to use Kana layout for US keyboard. With this change,
if the layout is us, Kana layout for US keyboard is used instead of JP keyboard.
* If multilingual layouts were selected, the keyboard layout for Mozc was not stable.
For example, when US and FR keyboards were selected in addition to Mozc,
Mozc's layout was not stable. This change enables to specify the static layout for Mozc.
Hiroyuki Komatsu
3 years ago
32 | 32 | MINOR = 26 |
33 | 33 | |
34 | 34 | # Number to be increased. This value may be replaced by other tools. |
35 | BUILD = 4219 | |
35 | BUILD = 4220 | |
36 | 36 | |
37 | 37 | # Represent the platform and release channel. |
38 | 38 | REVISION = 100 |
35 | 35 | "py_binary_mozc", |
36 | 36 | "select_mozc", |
37 | 37 | ) |
38 | load("//tools/build_defs:stubs.bzl", "portable_proto_library") | |
38 | 39 | |
39 | 40 | package(default_visibility = ["//:__subpackages__"]) |
40 | 41 | |
49 | 50 | srcs = ["gen_mozc_xml.py"], |
50 | 51 | ) |
51 | 52 | |
52 | # This genrule uses pkg-config in gen_mozc_xml_main. | |
53 | 53 | genrule( |
54 | 54 | name = "gen_mozc_xml", |
55 | 55 | outs = ["mozc.xml"], |
56 | cmd = select_mozc( | |
57 | default = "touch $@", # This is stub. | |
58 | linux = ("$(location :gen_mozc_xml_main) --branding=Mozc" + | |
59 | " --server_dir=" + MOZC_SERVER_DIRECTORY + | |
60 | " --ibus_mozc_path=" + IBUS_MOZC_PATH + | |
61 | " --ibus_mozc_icon_path=" + IBUS_MOZC_ICON_PATH + | |
62 | " > $@"), | |
63 | ), | |
56 | cmd = ("$(location :gen_mozc_xml_main) --branding=Mozc" + | |
57 | " --server_dir=" + MOZC_SERVER_DIRECTORY + | |
58 | " --ibus_mozc_path=" + IBUS_MOZC_PATH + | |
59 | " --ibus_mozc_icon_path=" + IBUS_MOZC_ICON_PATH + | |
60 | " > $@"), | |
64 | 61 | exec_tools = [":gen_mozc_xml_main"], |
65 | 62 | ) |
66 | 63 | |
67 | # This genrule uses pkg-config in gen_mozc_xml_main. | |
68 | 64 | genrule( |
69 | 65 | name = "gen_main_h", |
70 | 66 | outs = ["main.h"], |
71 | cmd = select_mozc( | |
72 | default = "touch $@", # This is stub. | |
73 | linux = ("$(location :gen_mozc_xml_main) --branding=Mozc" + | |
74 | " --output_cpp" + | |
75 | " --ibus_mozc_path=" + IBUS_MOZC_PATH + | |
76 | " --ibus_mozc_icon_path=" + IBUS_MOZC_ICON_PATH + | |
77 | " > $@"), | |
78 | ), | |
67 | cmd = ("$(location :gen_mozc_xml_main) --branding=Mozc" + | |
68 | " --output_cpp" + | |
69 | " --ibus_mozc_path=" + IBUS_MOZC_PATH + | |
70 | " --ibus_mozc_icon_path=" + IBUS_MOZC_ICON_PATH + | |
71 | " > $@"), | |
79 | 72 | exec_tools = [":gen_mozc_xml_main"], |
80 | 73 | ) |
81 | 74 | |
168 | 161 | "surrounding_text_util.h", |
169 | 162 | ], |
170 | 163 | ), |
164 | defines = [ | |
165 | "ENABLE_GTK_RENDERER", | |
166 | "MOZC_ENABLE_X11_SELECTION_MONITOR", | |
167 | ], | |
171 | 168 | deps = [ |
172 | 169 | ":gtk_candidate_window_handler", |
170 | ":ibus_config", | |
173 | 171 | ":ibus_property_handler", |
174 | 172 | ":ibus_utils", |
175 | 173 | ":x11_selection_monitor", |
179 | 177 | ], |
180 | 178 | ) |
181 | 179 | |
180 | proto_library( | |
181 | name = "ibus_config_proto_full", | |
182 | srcs = [ | |
183 | "ibus_config.proto", | |
184 | ], | |
185 | ) | |
186 | ||
187 | portable_proto_library( | |
188 | name = "ibus_config_proto", | |
189 | config_string = "allow_all: true", | |
190 | header_outs = [ | |
191 | "ibus_config.pb.h", | |
192 | ], | |
193 | proto_deps = ["ibus_config_proto_full"], | |
194 | ) | |
195 | ||
196 | cc_library_mozc( | |
197 | name = "ibus_config", | |
198 | srcs = ["ibus_config.cc"], | |
199 | hdrs = [ | |
200 | "ibus_config.h", | |
201 | ":gen_main_h", | |
202 | ], | |
203 | copts = ["-Wno-unused-variable"], | |
204 | deps = [ | |
205 | ":ibus_config_proto", | |
206 | "//base:file_stream", | |
207 | "//base:file_util", | |
208 | "//base:logging", | |
209 | "//base:system_util", | |
210 | "//base/protobuf:text_format", | |
211 | "@com_google_absl//absl/strings", | |
212 | ], | |
213 | ) | |
214 | ||
182 | 215 | cc_binary_mozc( |
183 | 216 | name = "ibus_mozc", |
184 | 217 | srcs = select_mozc( |
190 | 223 | ), |
191 | 224 | data = [":gen_mozc_xml"], |
192 | 225 | deps = [ |
226 | ":ibus_config", | |
193 | 227 | ":ibus_mozc_lib", |
194 | 228 | ":ibus_mozc_metadata", |
195 | 229 | "//base", |
230 | "//base:flags", | |
196 | 231 | "//base:init_mozc", |
197 | 232 | ], |
198 | 233 | ) |
39 | 39 | |
40 | 40 | import optparse |
41 | 41 | import os |
42 | import subprocess | |
43 | 42 | import sys |
44 | 43 | |
45 | 44 | # Information to generate <component> part of mozc.xml. %s will be replaced with |
67 | 66 | |
68 | 67 | #ifndef %s |
69 | 68 | #define %s |
69 | ||
70 | #include <cstddef> | |
71 | ||
70 | 72 | namespace {""" |
71 | 73 | |
72 | 74 | CPP_FOOTER = """} // namespace |
73 | 75 | #endif // %s""" |
74 | 76 | |
75 | 77 | |
76 | def OutputXmlElement(param_dict, element_name, value): | |
77 | print(' <%s>%s</%s>' % (element_name, (value % param_dict), element_name)) | |
78 | def GetXmlElement(param_dict, element_name, value): | |
79 | return ' <%s>%s</%s>' % (element_name, (value % param_dict), element_name) | |
80 | ||
81 | ||
82 | def GetTextProtoElement(param_dict, element_name, value): | |
83 | return ' %s : "%s"' % (element_name, (value % param_dict)) | |
84 | ||
85 | ||
86 | def GetEnginesXml(param_dict, engine_common, engines, setup_arg): | |
87 | """Outputs a XML data for ibus-daemon. | |
88 | ||
89 | Args: | |
90 | param_dict: A dictionary to embed options into output string. | |
91 | For example, {'product_name': 'Mozc'}. | |
92 | engine_common: A dictionary from a property name to a property value that | |
93 | are commonly used in all engines. For example, {'language': 'ja'}. | |
94 | engines: A dictionary from a property name to a list of property values of | |
95 | engines. For example, {'name': ['mozc-jp', 'mozc', 'mozc-dv']}. | |
96 | setup_arg: A command line to execute Mozc's configuration tool. | |
97 | Returns: | |
98 | output string in XML. | |
99 | """ | |
100 | output = ['<engines>'] | |
101 | for i in range(len(engines['name'])): | |
102 | output.append('<engine>') | |
103 | for key in engine_common: | |
104 | output.append(GetXmlElement(param_dict, key, engine_common[key])) | |
105 | if setup_arg: | |
106 | output.append(GetXmlElement(param_dict, 'setup', ' '.join(setup_arg))) | |
107 | for key in engines: | |
108 | output.append(GetXmlElement(param_dict, key, engines[key][i])) | |
109 | output.append('</engine>') | |
110 | output.append('</engines>') | |
111 | return '\n'.join(output) | |
112 | ||
113 | ||
114 | def GetIbusConfigTextProto(param_dict, engines): | |
115 | """Outputs a TextProto data for iBus config. | |
116 | ||
117 | Args: | |
118 | param_dict: A dictionary to embed options into output string. | |
119 | For example, {'product_name': 'Mozc'}. | |
120 | engines: A dictionary from a property name to a list of property values of | |
121 | engines. For example, {'name': ['mozc-jp', 'mozc', 'mozc-dv']}. | |
122 | Returns: | |
123 | output string in TextProto. | |
124 | """ | |
125 | output = [] | |
126 | for i in range(len(engines['name'])): | |
127 | output.append('engines {') | |
128 | for key in engines: | |
129 | output.append(GetTextProtoElement(param_dict, key, engines[key][i])) | |
130 | output.append('}') | |
131 | return '\n'.join(output) | |
78 | 132 | |
79 | 133 | |
80 | 134 | def OutputXml(param_dict, component, engine_common, engines, setup_arg): |
89 | 143 | are commonly used in all engines. For example, {'language': 'ja'}. |
90 | 144 | engines: A dictionary from a property name to a list of property values of |
91 | 145 | engines. For example, {'name': ['mozc-jp', 'mozc', 'mozc-dv']}. |
92 | """ | |
146 | setup_arg: A command line to execute Mozc's configuration tool. | |
147 | """ | |
148 | del engine_common, engines, setup_arg | |
93 | 149 | print('<component>') |
94 | 150 | for key in component: |
95 | OutputXmlElement(param_dict, key, component[key]) | |
96 | print('<engines>') | |
97 | for i in range(len(engines['name'])): | |
98 | print('<engine>') | |
99 | for key in engine_common: | |
100 | OutputXmlElement(param_dict, key, engine_common[key]) | |
101 | if setup_arg: | |
102 | OutputXmlElement(param_dict, 'setup', ' '.join(setup_arg)) | |
103 | for key in engines: | |
104 | OutputXmlElement(param_dict, key, engines[key][i]) | |
105 | print('</engine>') | |
106 | print('</engines>') | |
151 | print(GetXmlElement(param_dict, key, component[key])) | |
152 | print(' <engines exec="%s --xml" />' % param_dict['ibus_mozc_path']) | |
107 | 153 | print('</component>') |
154 | ||
155 | print(''' | |
156 | <!-- Settings of <engines> and <layout> are stored in ibus_config.textproto --> | |
157 | <!-- under the user configuration directory, which is either of: --> | |
158 | <!-- * $XDG_CONFIG_HOME/mozc/ibus_config.textproto --> | |
159 | <!-- * $HOME/.config/mozc/ibus_config.textproto --> | |
160 | <!-- * $HOME/.mozc/ibus_config.textproto --> | |
161 | ''') | |
108 | 162 | |
109 | 163 | |
110 | 164 | def OutputCppVariable(param_dict, prefix, variable_name, value): |
112 | 166 | (value % param_dict))) |
113 | 167 | |
114 | 168 | |
115 | def OutputCpp(param_dict, component, engine_common, engines): | |
169 | def OutputCpp(param_dict, component, engine_common, engines, setup_arg): | |
116 | 170 | """Outputs a C++ header file for mozc/unix/ibus/main.cc. |
117 | 171 | |
118 | 172 | Args: |
120 | 174 | component: ditto. |
121 | 175 | engine_common: ditto. |
122 | 176 | engines: ditto. |
177 | setup_arg: ditto. | |
123 | 178 | """ |
124 | 179 | guard_name = 'MOZC_UNIX_IBUS_MAIN_H_' |
125 | 180 | print(CPP_HEADER % (guard_name, guard_name)) |
127 | 182 | OutputCppVariable(param_dict, 'Component', key, component[key]) |
128 | 183 | for key in engine_common: |
129 | 184 | OutputCppVariable(param_dict, 'Engine', key, engine_common[key]) |
185 | OutputCppVariable(param_dict, 'Engine', 'Setup', ' '.join(setup_arg)) | |
130 | 186 | for key in engines: |
131 | 187 | print('const char* kEngine%sArray[] = {' % key.capitalize()) |
132 | 188 | for i in range(len(engines[key])): |
133 | 189 | print('"%s",' % (engines[key][i] % param_dict)) |
134 | 190 | print('};') |
135 | 191 | print('const size_t kEngineArrayLen = %s;' % len(engines['name'])) |
192 | print('const char kEnginesXml[] = R"#(', end='') | |
193 | print(GetEnginesXml(param_dict, engine_common, engines, setup_arg)) | |
194 | print(')#";') | |
195 | print('const char kIbusConfigTextProto[] = R"#(', end='') | |
196 | print(GetIbusConfigTextProto(param_dict, engines)) | |
197 | print(')#";') | |
136 | 198 | print(CPP_FOOTER % guard_name) |
137 | ||
138 | ||
139 | def CheckIBusVersion(options, minimum_version): | |
140 | """Tests if ibus version is equal to or greater than the given value.""" | |
141 | command_line = ['pkg-config', '--exists', 'ibus-1.0 >= %s' % minimum_version] | |
142 | return_code = subprocess.call(command_line) | |
143 | if return_code == 0: | |
144 | return True | |
145 | else: | |
146 | return False | |
147 | 199 | |
148 | 200 | |
149 | 201 | def main(): |
180 | 232 | 'language': 'ja', |
181 | 233 | 'icon': '%(ibus_mozc_icon_path)s', |
182 | 234 | 'rank': '80', |
235 | # Make sure that the property key 'InputMode' matches to the property name | |
236 | # specified to |ibus_property_new| in unix/ibus/property_handler.cc | |
237 | 'icon_prop_key': 'InputMode', | |
238 | 'symbol': 'あ', | |
183 | 239 | } |
184 | 240 | |
185 | 241 | # DO NOT change the engine name 'mozc-jp'. The names is referenced by |
187 | 243 | engines_props = { |
188 | 244 | 'name': ['mozc-jp'], |
189 | 245 | 'longname': ['%(product_name)s'], |
246 | 'layout': ['default'], | |
190 | 247 | } |
191 | ||
192 | # IBus 1.5.11 and greater supports 'icon_prop_key'. | |
193 | # See ibus/ibus@23c45b970b195008a54884a1a9d810e7f8b22c5c | |
194 | if CheckIBusVersion(options, '1.5.11'): | |
195 | # Make sure that the property key 'InputMode' matches to the property name | |
196 | # specified to |ibus_property_new| in unix/ibus/property_handler.cc | |
197 | engine_common_props['icon_prop_key'] = 'InputMode' | |
198 | ||
199 | if CheckIBusVersion(options, '1.5.0'): | |
200 | engine_common_props['symbol'] = 'あ' | |
201 | engines_props['layout'] = ['default'] | |
202 | else: | |
203 | engines_props['layout'] = ['jp'] | |
204 | 248 | |
205 | 249 | if options.output_cpp: |
206 | 250 | OutputCpp(param_dict, IBUS_COMPONENT_PROPS, engine_common_props, |
207 | engines_props) | |
251 | engines_props, setup_arg) | |
208 | 252 | else: |
209 | 253 | OutputXml(param_dict, IBUS_COMPONENT_PROPS, engine_common_props, |
210 | 254 | engines_props, setup_arg) |
134 | 134 | ], |
135 | 135 | }, |
136 | 136 | { |
137 | 'target_name': 'genproto_ibus_config_proto', | |
138 | 'type': 'none', | |
139 | 'toolsets': ['host'], | |
140 | 'sources': [ | |
141 | 'ibus_config.proto', | |
142 | ], | |
143 | 'includes': [ | |
144 | '../../protobuf/genproto.gypi', | |
145 | ], | |
146 | }, | |
147 | { | |
148 | 'target_name': 'ibus_config_proto', | |
149 | 'type': 'static_library', | |
150 | 'hard_dependency': 1, | |
151 | 'sources': [ | |
152 | '<(proto_out_dir)/unix/ibus/ibus_config.pb.cc', | |
153 | ], | |
154 | 'dependencies': [ | |
155 | '../../protobuf/protobuf.gyp:protobuf', | |
156 | 'genproto_ibus_config_proto#host', | |
157 | ], | |
158 | 'export_dependent_settings': [ | |
159 | 'genproto_ibus_config_proto#host', | |
160 | ], | |
161 | }, | |
162 | { | |
137 | 163 | 'target_name': 'ibus_mozc_lib', |
138 | 164 | 'type': 'static_library', |
139 | 165 | 'sources': [ |
140 | 166 | 'engine_registrar.cc', |
141 | 167 | 'ibus_candidate_window_handler.cc', |
168 | 'ibus_config.cc', | |
142 | 169 | 'key_event_handler.cc', |
143 | 170 | 'key_translator.cc', |
144 | 171 | 'mozc_engine.cc', |
146 | 173 | 'surrounding_text_util.cc', |
147 | 174 | ], |
148 | 175 | 'dependencies': [ |
176 | '../../base/absl.gyp:absl_strings', | |
149 | 177 | '../../client/client.gyp:client', |
150 | 178 | '../../protocol/protocol.gyp:commands_proto', |
151 | 179 | '../../session/session_base.gyp:ime_switch_util', |
180 | 'gen_ibus_mozc_files', | |
181 | 'ibus_config_proto', | |
152 | 182 | 'ibus_property_handler', |
153 | 183 | 'message_translator', |
154 | 184 | 'path_util', |
167 | 197 | ], |
168 | 198 | }, |
169 | 199 | { |
170 | 'target_name': 'ibus_mozc', | |
171 | 'type': 'executable', | |
172 | 'sources': [ | |
173 | 'main.cc', | |
174 | '<(gen_out_dir)/main.h', | |
175 | ], | |
200 | 'target_name': 'gen_ibus_mozc_files', | |
201 | 'type': 'none', | |
176 | 202 | 'actions': [ |
177 | 203 | { |
178 | 204 | 'action_name': 'gen_main_h', |
194 | 220 | }, |
195 | 221 | ], |
196 | 222 | 'dependencies': [ |
197 | '../../base/base.gyp:base', | |
223 | 'gen_mozc_xml', | |
224 | ], | |
225 | }, | |
226 | { | |
227 | 'target_name': 'ibus_mozc', | |
228 | 'type': 'executable', | |
229 | 'sources': [ | |
230 | 'main.cc', | |
231 | '<(gen_out_dir)/main.h', | |
232 | ], | |
233 | 'dependencies': [ | |
234 | '../../base/base.gyp:base', | |
235 | 'gen_ibus_mozc_files', | |
198 | 236 | 'gen_mozc_xml', |
199 | 237 | 'ibus_mozc_lib', |
200 | 238 | 'ibus_mozc_metadata', |
0 | // Copyright 2010-2020, Google Inc. | |
1 | // All rights reserved. | |
2 | // | |
3 | // Redistribution and use in source and binary forms, with or without | |
4 | // modification, are permitted provided that the following conditions are | |
5 | // met: | |
6 | // | |
7 | // * Redistributions of source code must retain the above copyright | |
8 | // notice, this list of conditions and the following disclaimer. | |
9 | // * Redistributions in binary form must reproduce the above | |
10 | // copyright notice, this list of conditions and the following disclaimer | |
11 | // in the documentation and/or other materials provided with the | |
12 | // distribution. | |
13 | // * Neither the name of Google Inc. nor the names of its | |
14 | // contributors may be used to endorse or promote products derived from | |
15 | // this software without specific prior written permission. | |
16 | // | |
17 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
18 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
19 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
20 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
21 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
22 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
23 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
24 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
25 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
26 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
27 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
28 | ||
29 | #include "unix/ibus/ibus_config.h" | |
30 | ||
31 | #include "base/file_stream.h" | |
32 | #include "base/file_util.h" | |
33 | #include "base/logging.h" | |
34 | #include "base/protobuf/text_format.h" | |
35 | #include "base/system_util.h" | |
36 | #include "unix/ibus/main.h" | |
37 | #include "absl/strings/ascii.h" | |
38 | #include "absl/strings/match.h" | |
39 | #include "absl/strings/str_cat.h" | |
40 | #include "absl/strings/str_replace.h" | |
41 | #include "absl/strings/str_split.h" | |
42 | ||
43 | namespace mozc { | |
44 | ||
45 | constexpr char kIbusConfigFile[] = "ibus_config.textproto"; | |
46 | ||
47 | namespace { | |
48 | std::string UpdateConfigFile() { | |
49 | const std::string engines_file = FileUtil::JoinPath( | |
50 | SystemUtil::GetUserProfileDirectory(), kIbusConfigFile); | |
51 | if (FileUtil::FileExists(engines_file)) { | |
52 | InputFileStream ifs(engines_file.c_str()); | |
53 | return ifs.Read(); | |
54 | } else { | |
55 | OutputFileStream ofs(engines_file.c_str()); | |
56 | ofs << kIbusConfigTextProto; | |
57 | ofs.close(); | |
58 | if (ofs.fail()) { | |
59 | LOG(ERROR) << "Failed to write " << engines_file; | |
60 | } | |
61 | return kIbusConfigTextProto; | |
62 | } | |
63 | } | |
64 | ||
65 | bool ParseConfig(const std::string &data, ibus::Config &config) { | |
66 | if (mozc::protobuf::TextFormat::ParseFromString(data, &config)) { | |
67 | return true; | |
68 | } | |
69 | // Failed to parse the data, fallback to the default setting. | |
70 | mozc::protobuf::TextFormat::ParseFromString(kIbusConfigTextProto, &config); | |
71 | return false; | |
72 | } | |
73 | ||
74 | std::string EscapeXmlValue(const std::string &value) { | |
75 | return absl::StrReplaceAll(value, {{"&", "&"}, | |
76 | {"<", "<"}, | |
77 | {">", ">"}, | |
78 | {"\"", """}, | |
79 | {"'", "'"}}); | |
80 | } | |
81 | ||
82 | std::string CreateEnginesXml(const ibus::Config &config) { | |
83 | std::string output = "<engines>\n"; | |
84 | for (const ibus::Engine &engine : config.engines()) { | |
85 | absl::StrAppend( | |
86 | &output, | |
87 | "<engine>\n", | |
88 | " <description>", kEngineDescription, "</description>\n", | |
89 | " <language>", kEngineLanguage, "</language>\n", | |
90 | " <icon>", kEngineIcon, "</icon>\n", | |
91 | " <rank>", kEngineRank, "</rank>\n", | |
92 | " <icon_prop_key>", kEngineIcon_prop_key, "</icon_prop_key>\n", | |
93 | " <symbol>", kEngineSymbol, "</symbol>\n", | |
94 | " <setup>", kEngineSetup, "</setup>\n", | |
95 | " <name>", EscapeXmlValue(engine.name()), "</name>\n", | |
96 | " <longname>", EscapeXmlValue(engine.longname()), "</longname>\n", | |
97 | " <layout>", EscapeXmlValue(engine.layout()), "</layout>\n", | |
98 | "</engine>\n"); | |
99 | } | |
100 | absl::StrAppend(&output, "</engines>\n"); | |
101 | return output; | |
102 | } | |
103 | } // namespace | |
104 | ||
105 | const std::string &IbusConfig::InitEnginesXml() { | |
106 | const std::string config_data = UpdateConfigFile(); | |
107 | const bool valid_user_config = ParseConfig(config_data, config_); | |
108 | engine_xml_ = CreateEnginesXml(config_); | |
109 | if (!valid_user_config) { | |
110 | engine_xml_ += ("<!-- Failed to parse the user config. -->\n" | |
111 | "<!-- Used the default setting instead. -->\n"); | |
112 | } | |
113 | return engine_xml_; | |
114 | } | |
115 | ||
116 | const ibus::Config &IbusConfig::GetConfig() const { | |
117 | return config_; | |
118 | } | |
119 | ||
120 | const std::string &IbusConfig::GetLayout(const std::string& name) const { | |
121 | for (const ibus::Engine &engine : config_.engines()) { | |
122 | if (engine.name() == name) { | |
123 | return engine.layout(); | |
124 | } | |
125 | } | |
126 | return default_layout_; | |
127 | } | |
128 | } // namespace mozc |
0 | // Copyright 2010-2020, Google Inc. | |
1 | // All rights reserved. | |
2 | // | |
3 | // Redistribution and use in source and binary forms, with or without | |
4 | // modification, are permitted provided that the following conditions are | |
5 | // met: | |
6 | // | |
7 | // * Redistributions of source code must retain the above copyright | |
8 | // notice, this list of conditions and the following disclaimer. | |
9 | // * Redistributions in binary form must reproduce the above | |
10 | // copyright notice, this list of conditions and the following disclaimer | |
11 | // in the documentation and/or other materials provided with the | |
12 | // distribution. | |
13 | // * Neither the name of Google Inc. nor the names of its | |
14 | // contributors may be used to endorse or promote products derived from | |
15 | // this software without specific prior written permission. | |
16 | // | |
17 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
18 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
19 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
20 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
21 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
22 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
23 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
24 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
25 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
26 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
27 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
28 | ||
29 | #ifndef MOZC_UNIX_IBUS_IBUS_CONFIG_H_ | |
30 | #define MOZC_UNIX_IBUS_IBUS_CONFIG_H_ | |
31 | ||
32 | #include <map> | |
33 | #include <string> | |
34 | ||
35 | #include "unix/ibus/ibus_config.pb.h" | |
36 | ||
37 | namespace mozc { | |
38 | ||
39 | class IbusConfig { | |
40 | public: | |
41 | IbusConfig() : default_layout_("default") {} | |
42 | virtual ~IbusConfig() = default; | |
43 | ||
44 | // Disallow implicit constructors. | |
45 | IbusConfig(const IbusConfig&) = delete; | |
46 | IbusConfig& operator=(const IbusConfig&) = delete; | |
47 | ||
48 | const std::string &InitEnginesXml(); | |
49 | const std::string &GetLayout(const std::string &name) const; | |
50 | const ibus::Config &GetConfig() const; | |
51 | ||
52 | private: | |
53 | std::string default_layout_; | |
54 | std::string engine_xml_; | |
55 | ibus::Config config_; | |
56 | }; | |
57 | ||
58 | } // namespace mozc | |
59 | ||
60 | #endif // MOZC_UNIX_IBUS_IBUS_CONFIG_H_ |
0 | // Copyright 2010-2020, Google Inc. | |
1 | // All rights reserved. | |
2 | // | |
3 | // Redistribution and use in source and binary forms, with or without | |
4 | // modification, are permitted provided that the following conditions are | |
5 | // met: | |
6 | // | |
7 | // * Redistributions of source code must retain the above copyright | |
8 | // notice, this list of conditions and the following disclaimer. | |
9 | // * Redistributions in binary form must reproduce the above | |
10 | // copyright notice, this list of conditions and the following disclaimer | |
11 | // in the documentation and/or other materials provided with the | |
12 | // distribution. | |
13 | // * Neither the name of Google Inc. nor the names of its | |
14 | // contributors may be used to endorse or promote products derived from | |
15 | // this software without specific prior written permission. | |
16 | // | |
17 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
18 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
19 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
20 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
21 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
22 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
23 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
24 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
25 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
26 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
27 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
28 | ||
29 | syntax = "proto2"; | |
30 | ||
31 | package mozc.ibus; | |
32 | ||
33 | message Config { | |
34 | repeated Engine engines = 1; | |
35 | } | |
36 | ||
37 | message Engine { | |
38 | optional string name = 1; | |
39 | optional string longname = 2; | |
40 | optional string layout = 3; | |
41 | } |
33 | 33 | #include "base/init_mozc.h" |
34 | 34 | #include "base/logging.h" |
35 | 35 | #include "base/version.h" |
36 | #include "unix/ibus/ibus_config.h" | |
36 | 37 | #include "unix/ibus/main.h" |
37 | 38 | #include "unix/ibus/mozc_engine.h" |
38 | 39 | #include "unix/ibus/path_util.h" |
39 | 40 | |
40 | 41 | DEFINE_bool(ibus, false, "The engine is started by ibus-daemon"); |
42 | DEFINE_bool(xml, false, "Output xml data for the engine."); | |
41 | 43 | |
42 | 44 | namespace { |
43 | 45 | |
69 | 71 | mozc::Version::GetMozcVersion().c_str(), kComponentLicense, |
70 | 72 | kComponentAuthor, kComponentHomepage, "", kComponentTextdomain); |
71 | 73 | const std::string icon_path = mozc::ibus::GetIconPath(kEngineIcon); |
72 | for (size_t i = 0; i < kEngineArrayLen; ++i) { | |
74 | ||
75 | mozc::IbusConfig ibus_config; | |
76 | ibus_config.InitEnginesXml(); | |
77 | for (const mozc::ibus::Engine &engine : ibus_config.GetConfig().engines()) { | |
73 | 78 | ibus_component_add_engine( |
74 | 79 | component, |
75 | ibus_engine_desc_new(kEngineNameArray[i], kEngineLongnameArray[i], | |
80 | ibus_engine_desc_new(engine.name().c_str(), engine.longname().c_str(), | |
76 | 81 | kEngineDescription, kEngineLanguage, |
77 | 82 | kComponentLicense, kComponentAuthor, |
78 | icon_path.c_str(), kEngineLayoutArray[i])); | |
83 | icon_path.c_str(), engine.layout().c_str())); | |
79 | 84 | } |
80 | 85 | return component; |
81 | 86 | } |
104 | 109 | g_object_unref(component); |
105 | 110 | } |
106 | 111 | |
112 | void OutputXml() { | |
113 | mozc::IbusConfig ibus_config; | |
114 | std::cout << ibus_config.InitEnginesXml() << std::endl; | |
115 | } | |
116 | ||
107 | 117 | } // namespace |
108 | 118 | |
109 | 119 | int main(gint argc, gchar **argv) { |
110 | 120 | mozc::InitMozc(argv[0], &argc, &argv); |
121 | if (mozc::GetFlag(FLAGS_xml)) { | |
122 | OutputXml(); | |
123 | return 0; | |
124 | } | |
125 | ||
111 | 126 | ibus_init(); |
112 | InitIBusComponent(FLAGS_ibus); | |
127 | InitIBusComponent(mozc::GetFlag(FLAGS_ibus)); | |
113 | 128 | #ifndef MOZC_NO_LOGGING |
114 | 129 | EnableVerboseLog(); |
115 | 130 | #endif // MOZC_NO_LOGGING |
26 | 26 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
27 | 27 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | 28 | |
29 | #include <iostream> | |
30 | ||
31 | #include "base/flags.h" | |
32 | #include "base/init_mozc.h" | |
33 | #include "unix/ibus/ibus_config.h" | |
34 | ||
35 | DEFINE_bool(xml, false, "Output xml data for the engine."); | |
36 | ||
37 | namespace { | |
38 | void OutputXml() { | |
39 | mozc::IbusConfig ibus_config; | |
40 | std::cout << ibus_config.InitEnginesXml() << std::endl; | |
41 | } | |
42 | } // namespace | |
43 | ||
29 | 44 | int main(int argc, char **argv) { |
45 | mozc::InitMozc(argv[0], &argc, &argv); | |
46 | if (mozc::GetFlag(FLAGS_xml)) { | |
47 | OutputXml(); | |
48 | return 0; | |
49 | } | |
50 | ||
30 | 51 | // This is a stub used by platforms which do not support iBus |
31 | 52 | // (i.e. non Linux environments). |
32 | 53 | return 1; |
257 | 257 | } |
258 | 258 | #endif // MOZC_ENABLE_X11_SELECTION_MONITOR |
259 | 259 | |
260 | ibus_config_.InitEnginesXml(); | |
261 | ||
260 | 262 | // TODO(yusukes): write a unit test to check if the capability is set |
261 | 263 | // as expected. |
262 | 264 | } |
348 | 350 | return FALSE; |
349 | 351 | } |
350 | 352 | |
351 | // TODO(yusukes): use |layout| in IBusEngineDesc if possible. | |
352 | const bool layout_is_jp = !g_strcmp0(ibus_engine_get_name(engine), "mozc-jp"); | |
353 | // layout_is_jp is only used determine Kana input with US layout. | |
354 | const std::string &layout = | |
355 | ibus_config_.GetLayout(ibus_engine_get_name(engine)); | |
356 | const bool layout_is_jp = (layout != "us"); | |
353 | 357 | |
354 | 358 | commands::KeyEvent key; |
355 | 359 | if (!key_event_handler_->GetKeyEvent(keyval, keycode, modifiers, |
37 | 37 | #include "protocol/commands.pb.h" |
38 | 38 | #include "testing/base/public/gunit_prod.h" |
39 | 39 | #include "unix/ibus/engine_interface.h" |
40 | #include "unix/ibus/ibus_config.h" | |
40 | 41 | |
41 | 42 | namespace mozc { |
42 | 43 | |
147 | 148 | |
148 | 149 | // Unique IDs of candidates that are currently shown. |
149 | 150 | std::vector<int32> unique_candidate_ids_; |
151 | IbusConfig ibus_config_; | |
150 | 152 | |
151 | 153 | friend class LaunchToolTest; |
152 | 154 | FRIEND_TEST(LaunchToolTest, LaunchToolTest); |