From 9ba59b64d53365c1fe93c1c245b4ec3e35bdadf0 Mon Sep 17 00:00:00 2001 From: Hiroyuki Komatsu Date: Sat, 12 Dec 2020 19:12:52 +0900 Subject: [PATCH] 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 ``` com.google.IBus.Mozc Mozc Component /usr/lib/ibus-mozc/ibus-engine-mozc --ibus 0.0.0.0 Google Inc. New BSD https://github.com/google/mozc ibus-mozc Mozc (Japanese Input Method) ja /usr/share/ibus-mozc/product_icon.png 80 InputMode /usr/lib/mozc/mozc_tool --mode=config_dialog mozc-jp Mozc default ``` After ``` com.google.IBus.Mozc Mozc Component /usr/lib/ibus-mozc/ibus-engine-mozc --ibus 0.0.0.0 Google Inc. New BSD https://github.com/google/mozc ibus-mozc ``` Then, `/usr/lib/ibus-mozc/ibus-engine-mozc --xml` returns the values of the 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. --- src/data/version/mozc_version_template.bzl | 2 +- src/unix/ibus/BUILD | 71 +++++++++--- src/unix/ibus/gen_mozc_xml.py | 124 +++++++++++++------- src/unix/ibus/ibus.gyp | 50 +++++++- src/unix/ibus/ibus_config.cc | 129 +++++++++++++++++++++ src/unix/ibus/ibus_config.h | 61 ++++++++++ src/unix/ibus/ibus_config.proto | 42 +++++++ src/unix/ibus/main.cc | 23 +++- src/unix/ibus/main_stub.cc | 21 ++++ src/unix/ibus/mozc_engine.cc | 8 +- src/unix/ibus/mozc_engine.h | 2 + 11 files changed, 462 insertions(+), 71 deletions(-) create mode 100644 src/unix/ibus/ibus_config.cc create mode 100644 src/unix/ibus/ibus_config.h create mode 100644 src/unix/ibus/ibus_config.proto diff --git a/src/data/version/mozc_version_template.bzl b/src/data/version/mozc_version_template.bzl index 3bc5767f9..192dc1e8a 100644 --- a/src/data/version/mozc_version_template.bzl +++ b/src/data/version/mozc_version_template.bzl @@ -33,7 +33,7 @@ MAJOR = 2 MINOR = 26 # Number to be increased. This value may be replaced by other tools. -BUILD = 4219 +BUILD = 4220 # Represent the platform and release channel. REVISION = 100 diff --git a/src/unix/ibus/BUILD b/src/unix/ibus/BUILD index da8511249..8ce5a622a 100644 --- a/src/unix/ibus/BUILD +++ b/src/unix/ibus/BUILD @@ -36,6 +36,7 @@ load( "py_binary_mozc", "select_mozc", ) +load("//tools/build_defs:stubs.bzl", "portable_proto_library") package(default_visibility = ["//:__subpackages__"]) @@ -50,33 +51,25 @@ py_binary_mozc( srcs = ["gen_mozc_xml.py"], ) -# This genrule uses pkg-config in gen_mozc_xml_main. genrule( name = "gen_mozc_xml", outs = ["mozc.xml"], - cmd = select_mozc( - default = "touch $@", # This is stub. - linux = ("$(location :gen_mozc_xml_main) --branding=Mozc" + - " --server_dir=" + MOZC_SERVER_DIRECTORY + - " --ibus_mozc_path=" + IBUS_MOZC_PATH + - " --ibus_mozc_icon_path=" + IBUS_MOZC_ICON_PATH + - " > $@"), - ), + cmd = ("$(location :gen_mozc_xml_main) --branding=Mozc" + + " --server_dir=" + MOZC_SERVER_DIRECTORY + + " --ibus_mozc_path=" + IBUS_MOZC_PATH + + " --ibus_mozc_icon_path=" + IBUS_MOZC_ICON_PATH + + " > $@"), exec_tools = [":gen_mozc_xml_main"], ) -# This genrule uses pkg-config in gen_mozc_xml_main. genrule( name = "gen_main_h", outs = ["main.h"], - cmd = select_mozc( - default = "touch $@", # This is stub. - linux = ("$(location :gen_mozc_xml_main) --branding=Mozc" + - " --output_cpp" + - " --ibus_mozc_path=" + IBUS_MOZC_PATH + - " --ibus_mozc_icon_path=" + IBUS_MOZC_ICON_PATH + - " > $@"), - ), + cmd = ("$(location :gen_mozc_xml_main) --branding=Mozc" + + " --output_cpp" + + " --ibus_mozc_path=" + IBUS_MOZC_PATH + + " --ibus_mozc_icon_path=" + IBUS_MOZC_ICON_PATH + + " > $@"), exec_tools = [":gen_mozc_xml_main"], ) @@ -169,8 +162,13 @@ cc_library_mozc( "surrounding_text_util.h", ], ), + defines = [ + "ENABLE_GTK_RENDERER", + "MOZC_ENABLE_X11_SELECTION_MONITOR", + ], deps = [ ":gtk_candidate_window_handler", + ":ibus_config", ":ibus_property_handler", ":ibus_utils", ":x11_selection_monitor", @@ -180,6 +178,41 @@ cc_library_mozc( ], ) +proto_library( + name = "ibus_config_proto_full", + srcs = [ + "ibus_config.proto", + ], +) + +portable_proto_library( + name = "ibus_config_proto", + config_string = "allow_all: true", + header_outs = [ + "ibus_config.pb.h", + ], + proto_deps = ["ibus_config_proto_full"], +) + +cc_library_mozc( + name = "ibus_config", + srcs = ["ibus_config.cc"], + hdrs = [ + "ibus_config.h", + ":gen_main_h", + ], + copts = ["-Wno-unused-variable"], + deps = [ + ":ibus_config_proto", + "//base:file_stream", + "//base:file_util", + "//base:logging", + "//base:system_util", + "//base/protobuf:text_format", + "@com_google_absl//absl/strings", + ], +) + cc_binary_mozc( name = "ibus_mozc", srcs = select_mozc( @@ -191,9 +224,11 @@ cc_binary_mozc( ), data = [":gen_mozc_xml"], deps = [ + ":ibus_config", ":ibus_mozc_lib", ":ibus_mozc_metadata", "//base", + "//base:flags", "//base:init_mozc", ], ) diff --git a/src/unix/ibus/gen_mozc_xml.py b/src/unix/ibus/gen_mozc_xml.py index 856d4c7bb..7186c00d8 100644 --- a/src/unix/ibus/gen_mozc_xml.py +++ b/src/unix/ibus/gen_mozc_xml.py @@ -40,7 +40,6 @@ __author__ = "yusukes" import optparse import os -import subprocess import sys # Information to generate part of mozc.xml. %s will be replaced with @@ -68,14 +67,69 @@ CPP_HEADER = """// Copyright 2010 Google Inc. All Rights Reserved. #ifndef %s #define %s + +#include + namespace {""" CPP_FOOTER = """} // namespace #endif // %s""" -def OutputXmlElement(param_dict, element_name, value): - print(' <%s>%s' % (element_name, (value % param_dict), element_name)) +def GetXmlElement(param_dict, element_name, value): + return ' <%s>%s' % (element_name, (value % param_dict), element_name) + + +def GetTextProtoElement(param_dict, element_name, value): + return ' %s : "%s"' % (element_name, (value % param_dict)) + + +def GetEnginesXml(param_dict, engine_common, engines, setup_arg): + """Outputs a XML data for ibus-daemon. + + Args: + param_dict: A dictionary to embed options into output string. + For example, {'product_name': 'Mozc'}. + engine_common: A dictionary from a property name to a property value that + are commonly used in all engines. For example, {'language': 'ja'}. + engines: A dictionary from a property name to a list of property values of + engines. For example, {'name': ['mozc-jp', 'mozc', 'mozc-dv']}. + setup_arg: A command line to execute Mozc's configuration tool. + Returns: + output string in XML. + """ + output = [''] + for i in range(len(engines['name'])): + output.append('') + for key in engine_common: + output.append(GetXmlElement(param_dict, key, engine_common[key])) + if setup_arg: + output.append(GetXmlElement(param_dict, 'setup', ' '.join(setup_arg))) + for key in engines: + output.append(GetXmlElement(param_dict, key, engines[key][i])) + output.append('') + output.append('') + return '\n'.join(output) + + +def GetIbusConfigTextProto(param_dict, engines): + """Outputs a TextProto data for iBus config. + + Args: + param_dict: A dictionary to embed options into output string. + For example, {'product_name': 'Mozc'}. + engines: A dictionary from a property name to a list of property values of + engines. For example, {'name': ['mozc-jp', 'mozc', 'mozc-dv']}. + Returns: + output string in TextProto. + """ + output = [] + for i in range(len(engines['name'])): + output.append('engines {') + for key in engines: + output.append(GetTextProtoElement(param_dict, key, engines[key][i])) + output.append('}') + return '\n'.join(output) def OutputXml(param_dict, component, engine_common, engines, setup_arg): @@ -90,30 +144,30 @@ def OutputXml(param_dict, component, engine_common, engines, setup_arg): are commonly used in all engines. For example, {'language': 'ja'}. engines: A dictionary from a property name to a list of property values of engines. For example, {'name': ['mozc-jp', 'mozc', 'mozc-dv']}. + setup_arg: A command line to execute Mozc's configuration tool. """ + del engine_common, engines, setup_arg print('') for key in component: - OutputXmlElement(param_dict, key, component[key]) - print('') - for i in range(len(engines['name'])): - print('') - for key in engine_common: - OutputXmlElement(param_dict, key, engine_common[key]) - if setup_arg: - OutputXmlElement(param_dict, 'setup', ' '.join(setup_arg)) - for key in engines: - OutputXmlElement(param_dict, key, engines[key][i]) - print('') - print('') + print(GetXmlElement(param_dict, key, component[key])) + print(' ' % param_dict['ibus_mozc_path']) print('') + print(''' + + + + + +''') + def OutputCppVariable(param_dict, prefix, variable_name, value): print('const char k%s%s[] = "%s";' % (prefix, variable_name.capitalize(), (value % param_dict))) -def OutputCpp(param_dict, component, engine_common, engines): +def OutputCpp(param_dict, component, engine_common, engines, setup_arg): """Outputs a C++ header file for mozc/unix/ibus/main.cc. Args: @@ -121,6 +175,7 @@ def OutputCpp(param_dict, component, engine_common, engines): component: ditto. engine_common: ditto. engines: ditto. + setup_arg: ditto. """ guard_name = 'MOZC_UNIX_IBUS_MAIN_H_' print(CPP_HEADER % (guard_name, guard_name)) @@ -128,25 +183,22 @@ def OutputCpp(param_dict, component, engine_common, engines): OutputCppVariable(param_dict, 'Component', key, component[key]) for key in engine_common: OutputCppVariable(param_dict, 'Engine', key, engine_common[key]) + OutputCppVariable(param_dict, 'Engine', 'Setup', ' '.join(setup_arg)) for key in engines: print('const char* kEngine%sArray[] = {' % key.capitalize()) for i in range(len(engines[key])): print('"%s",' % (engines[key][i] % param_dict)) print('};') print('const size_t kEngineArrayLen = %s;' % len(engines['name'])) + print('const char kEnginesXml[] = R"#(', end='') + print(GetEnginesXml(param_dict, engine_common, engines, setup_arg)) + print(')#";') + print('const char kIbusConfigTextProto[] = R"#(', end='') + print(GetIbusConfigTextProto(param_dict, engines)) + print(')#";') print(CPP_FOOTER % guard_name) -def CheckIBusVersion(options, minimum_version): - """Tests if ibus version is equal to or greater than the given value.""" - command_line = ['pkg-config', '--exists', 'ibus-1.0 >= %s' % minimum_version] - return_code = subprocess.call(command_line) - if return_code == 0: - return True - else: - return False - - def main(): """The main function.""" parser = optparse.OptionParser(usage='Usage: %prog [options]') @@ -181,6 +233,10 @@ def main(): 'language': 'ja', 'icon': '%(ibus_mozc_icon_path)s', 'rank': '80', + # Make sure that the property key 'InputMode' matches to the property name + # specified to |ibus_property_new| in unix/ibus/property_handler.cc + 'icon_prop_key': 'InputMode', + 'symbol': 'あ', } # DO NOT change the engine name 'mozc-jp'. The names is referenced by @@ -188,24 +244,12 @@ def main(): engines_props = { 'name': ['mozc-jp'], 'longname': ['%(product_name)s'], + 'layout': ['default'], } - # IBus 1.5.11 and greater supports 'icon_prop_key'. - # See ibus/ibus@23c45b970b195008a54884a1a9d810e7f8b22c5c - if CheckIBusVersion(options, '1.5.11'): - # Make sure that the property key 'InputMode' matches to the property name - # specified to |ibus_property_new| in unix/ibus/property_handler.cc - engine_common_props['icon_prop_key'] = 'InputMode' - - if CheckIBusVersion(options, '1.5.0'): - engine_common_props['symbol'] = 'あ' - engines_props['layout'] = ['default'] - else: - engines_props['layout'] = ['jp'] - if options.output_cpp: OutputCpp(param_dict, IBUS_COMPONENT_PROPS, engine_common_props, - engines_props) + engines_props, setup_arg) else: OutputXml(param_dict, IBUS_COMPONENT_PROPS, engine_common_props, engines_props, setup_arg) diff --git a/src/unix/ibus/ibus.gyp b/src/unix/ibus/ibus.gyp index 6fa7b9671..af3c981bf 100644 --- a/src/unix/ibus/ibus.gyp +++ b/src/unix/ibus/ibus.gyp @@ -134,12 +134,39 @@ '../../base/base.gyp:base', ], }, + { + 'target_name': 'genproto_ibus_config_proto', + 'type': 'none', + 'toolsets': ['host'], + 'sources': [ + 'ibus_config.proto', + ], + 'includes': [ + '../../protobuf/genproto.gypi', + ], + }, + { + 'target_name': 'ibus_config_proto', + 'type': 'static_library', + 'hard_dependency': 1, + 'sources': [ + '<(proto_out_dir)/unix/ibus/ibus_config.pb.cc', + ], + 'dependencies': [ + '../../protobuf/protobuf.gyp:protobuf', + 'genproto_ibus_config_proto#host', + ], + 'export_dependent_settings': [ + 'genproto_ibus_config_proto#host', + ], + }, { 'target_name': 'ibus_mozc_lib', 'type': 'static_library', 'sources': [ 'engine_registrar.cc', 'ibus_candidate_window_handler.cc', + 'ibus_config.cc', 'key_event_handler.cc', 'key_translator.cc', 'mozc_engine.cc', @@ -147,9 +174,12 @@ 'surrounding_text_util.cc', ], 'dependencies': [ + '../../base/absl.gyp:absl_strings', '../../client/client.gyp:client', '../../protocol/protocol.gyp:commands_proto', '../../session/session_base.gyp:ime_switch_util', + 'gen_ibus_mozc_files', + 'ibus_config_proto', 'ibus_property_handler', 'message_translator', 'path_util', @@ -168,12 +198,8 @@ ], }, { - 'target_name': 'ibus_mozc', - 'type': 'executable', - 'sources': [ - 'main.cc', - '<(gen_out_dir)/main.h', - ], + 'target_name': 'gen_ibus_mozc_files', + 'type': 'none', 'actions': [ { 'action_name': 'gen_main_h', @@ -194,8 +220,20 @@ ], }, ], + 'dependencies': [ + 'gen_mozc_xml', + ], + }, + { + 'target_name': 'ibus_mozc', + 'type': 'executable', + 'sources': [ + 'main.cc', + '<(gen_out_dir)/main.h', + ], 'dependencies': [ '../../base/base.gyp:base', + 'gen_ibus_mozc_files', 'gen_mozc_xml', 'ibus_mozc_lib', 'ibus_mozc_metadata', diff --git a/src/unix/ibus/ibus_config.cc b/src/unix/ibus/ibus_config.cc new file mode 100644 index 000000000..78ce8145a --- /dev/null +++ b/src/unix/ibus/ibus_config.cc @@ -0,0 +1,129 @@ +// Copyright 2010-2020, Google Inc. +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +#include "unix/ibus/ibus_config.h" + +#include "base/file_stream.h" +#include "base/file_util.h" +#include "base/logging.h" +#include "base/protobuf/text_format.h" +#include "base/system_util.h" +#include "unix/ibus/main.h" +#include "absl/strings/ascii.h" +#include "absl/strings/match.h" +#include "absl/strings/str_cat.h" +#include "absl/strings/str_replace.h" +#include "absl/strings/str_split.h" + +namespace mozc { + +constexpr char kIbusConfigFile[] = "ibus_config.textproto"; + +namespace { +std::string UpdateConfigFile() { + const std::string engines_file = FileUtil::JoinPath( + SystemUtil::GetUserProfileDirectory(), kIbusConfigFile); + if (FileUtil::FileExists(engines_file)) { + InputFileStream ifs(engines_file.c_str()); + return ifs.Read(); + } else { + OutputFileStream ofs(engines_file.c_str()); + ofs << kIbusConfigTextProto; + ofs.close(); + if (ofs.fail()) { + LOG(ERROR) << "Failed to write " << engines_file; + } + return kIbusConfigTextProto; + } +} + +bool ParseConfig(const std::string &data, ibus::Config &config) { + if (mozc::protobuf::TextFormat::ParseFromString(data, &config)) { + return true; + } + // Failed to parse the data, fallback to the default setting. + mozc::protobuf::TextFormat::ParseFromString(kIbusConfigTextProto, &config); + return false; +} + +std::string EscapeXmlValue(const std::string &value) { + return absl::StrReplaceAll(value, {{"&", "&"}, + {"<", "<"}, + {">", ">"}, + {"\"", """}, + {"'", "'"}}); +} + +std::string CreateEnginesXml(const ibus::Config &config) { + std::string output = "\n"; + for (const ibus::Engine &engine : config.engines()) { + absl::StrAppend( + &output, + "\n", + " ", kEngineDescription, "\n", + " ", kEngineLanguage, "\n", + " ", kEngineIcon, "\n", + " ", kEngineRank, "\n", + " ", kEngineIcon_prop_key, "\n", + " ", kEngineSymbol, "\n", + " ", kEngineSetup, "\n", + " ", EscapeXmlValue(engine.name()), "\n", + " ", EscapeXmlValue(engine.longname()), "\n", + " ", EscapeXmlValue(engine.layout()), "\n", + "\n"); + } + absl::StrAppend(&output, "\n"); + return output; +} +} // namespace + +const std::string &IbusConfig::InitEnginesXml() { + const std::string config_data = UpdateConfigFile(); + const bool valid_user_config = ParseConfig(config_data, config_); + engine_xml_ = CreateEnginesXml(config_); + if (!valid_user_config) { + engine_xml_ += ("\n" + "\n"); + } + return engine_xml_; +} + +const ibus::Config &IbusConfig::GetConfig() const { + return config_; +} + +const std::string &IbusConfig::GetLayout(const std::string& name) const { + for (const ibus::Engine &engine : config_.engines()) { + if (engine.name() == name) { + return engine.layout(); + } + } + return default_layout_; +} +} // namespace mozc diff --git a/src/unix/ibus/ibus_config.h b/src/unix/ibus/ibus_config.h new file mode 100644 index 000000000..f0dcfe79e --- /dev/null +++ b/src/unix/ibus/ibus_config.h @@ -0,0 +1,61 @@ +// Copyright 2010-2020, Google Inc. +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +#ifndef MOZC_UNIX_IBUS_IBUS_CONFIG_H_ +#define MOZC_UNIX_IBUS_IBUS_CONFIG_H_ + +#include +#include + +#include "unix/ibus/ibus_config.pb.h" + +namespace mozc { + +class IbusConfig { + public: + IbusConfig() : default_layout_("default") {} + virtual ~IbusConfig() = default; + + // Disallow implicit constructors. + IbusConfig(const IbusConfig&) = delete; + IbusConfig& operator=(const IbusConfig&) = delete; + + const std::string &InitEnginesXml(); + const std::string &GetLayout(const std::string &name) const; + const ibus::Config &GetConfig() const; + + private: + std::string default_layout_; + std::string engine_xml_; + ibus::Config config_; +}; + +} // namespace mozc + +#endif // MOZC_UNIX_IBUS_IBUS_CONFIG_H_ diff --git a/src/unix/ibus/ibus_config.proto b/src/unix/ibus/ibus_config.proto new file mode 100644 index 000000000..d5ea1c0c7 --- /dev/null +++ b/src/unix/ibus/ibus_config.proto @@ -0,0 +1,42 @@ +// Copyright 2010-2020, Google Inc. +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +syntax = "proto2"; + +package mozc.ibus; + +message Config { + repeated Engine engines = 1; +} + +message Engine { + optional string name = 1; + optional string longname = 2; + optional string layout = 3; +} diff --git a/src/unix/ibus/main.cc b/src/unix/ibus/main.cc index cb6029763..0dd07c5d8 100644 --- a/src/unix/ibus/main.cc +++ b/src/unix/ibus/main.cc @@ -34,11 +34,13 @@ #include "base/init_mozc.h" #include "base/logging.h" #include "base/version.h" +#include "unix/ibus/ibus_config.h" #include "unix/ibus/main.h" #include "unix/ibus/mozc_engine.h" #include "unix/ibus/path_util.h" DEFINE_bool(ibus, false, "The engine is started by ibus-daemon"); +DEFINE_bool(xml, false, "Output xml data for the engine."); namespace { @@ -70,13 +72,16 @@ IBusComponent *GetIBusComponent() { mozc::Version::GetMozcVersion().c_str(), kComponentLicense, kComponentAuthor, kComponentHomepage, "", kComponentTextdomain); const std::string icon_path = mozc::ibus::GetIconPath(kEngineIcon); - for (size_t i = 0; i < kEngineArrayLen; ++i) { + + mozc::IbusConfig ibus_config; + ibus_config.InitEnginesXml(); + for (const mozc::ibus::Engine &engine : ibus_config.GetConfig().engines()) { ibus_component_add_engine( component, - ibus_engine_desc_new(kEngineNameArray[i], kEngineLongnameArray[i], + ibus_engine_desc_new(engine.name().c_str(), engine.longname().c_str(), kEngineDescription, kEngineLanguage, kComponentLicense, kComponentAuthor, - icon_path.c_str(), kEngineLayoutArray[i])); + icon_path.c_str(), engine.layout().c_str())); } return component; } @@ -105,12 +110,22 @@ void InitIBusComponent(bool executed_by_ibus_daemon) { g_object_unref(component); } +void OutputXml() { + mozc::IbusConfig ibus_config; + std::cout << ibus_config.InitEnginesXml() << std::endl; +} + } // namespace int main(gint argc, gchar **argv) { mozc::InitMozc(argv[0], &argc, &argv); + if (mozc::GetFlag(FLAGS_xml)) { + OutputXml(); + return 0; + } + ibus_init(); - InitIBusComponent(FLAGS_ibus); + InitIBusComponent(mozc::GetFlag(FLAGS_ibus)); #ifndef MOZC_NO_LOGGING EnableVerboseLog(); #endif // MOZC_NO_LOGGING diff --git a/src/unix/ibus/main_stub.cc b/src/unix/ibus/main_stub.cc index 2b753dcee..9008cb932 100644 --- a/src/unix/ibus/main_stub.cc +++ b/src/unix/ibus/main_stub.cc @@ -27,7 +27,28 @@ // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +#include + +#include "base/flags.h" +#include "base/init_mozc.h" +#include "unix/ibus/ibus_config.h" + +DEFINE_bool(xml, false, "Output xml data for the engine."); + +namespace { +void OutputXml() { + mozc::IbusConfig ibus_config; + std::cout << ibus_config.InitEnginesXml() << std::endl; +} +} // namespace + int main(int argc, char **argv) { + mozc::InitMozc(argv[0], &argc, &argv); + if (mozc::GetFlag(FLAGS_xml)) { + OutputXml(); + return 0; + } + // This is a stub used by platforms which do not support iBus // (i.e. non Linux environments). return 1; diff --git a/src/unix/ibus/mozc_engine.cc b/src/unix/ibus/mozc_engine.cc index 37ac61de1..c48cc03c6 100644 --- a/src/unix/ibus/mozc_engine.cc +++ b/src/unix/ibus/mozc_engine.cc @@ -258,6 +258,8 @@ MozcEngine::MozcEngine() } #endif // MOZC_ENABLE_X11_SELECTION_MONITOR + ibus_config_.InitEnginesXml(); + // TODO(yusukes): write a unit test to check if the capability is set // as expected. } @@ -349,8 +351,10 @@ gboolean MozcEngine::ProcessKeyEvent(IBusEngine *engine, guint keyval, return FALSE; } - // TODO(yusukes): use |layout| in IBusEngineDesc if possible. - const bool layout_is_jp = !g_strcmp0(ibus_engine_get_name(engine), "mozc-jp"); + // layout_is_jp is only used determine Kana input with US layout. + const std::string &layout = + ibus_config_.GetLayout(ibus_engine_get_name(engine)); + const bool layout_is_jp = (layout != "us"); commands::KeyEvent key; if (!key_event_handler_->GetKeyEvent(keyval, keycode, modifiers, diff --git a/src/unix/ibus/mozc_engine.h b/src/unix/ibus/mozc_engine.h index 82453d05c..99a73cafa 100644 --- a/src/unix/ibus/mozc_engine.h +++ b/src/unix/ibus/mozc_engine.h @@ -38,6 +38,7 @@ #include "protocol/commands.pb.h" #include "testing/base/public/gunit_prod.h" #include "unix/ibus/engine_interface.h" +#include "unix/ibus/ibus_config.h" namespace mozc { @@ -148,6 +149,7 @@ class MozcEngine : public EngineInterface { // Unique IDs of candidates that are currently shown. std::vector unique_candidate_ids_; + IbusConfig ibus_config_; friend class LaunchToolTest; FRIEND_TEST(LaunchToolTest, LaunchToolTest);