1 // Copyright 2010-2018, Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 // * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 #include <cstddef>
31 #include <cstdio>
32
33 #include "base/flags.h"
34 #include "base/init_mozc.h"
35 #include "base/logging.h"
36 #include "base/version.h"
37 #include "unix/ibus/main.h"
38 #include "unix/ibus/mozc_engine.h"
39 #include "unix/ibus/path_util.h"
40
41 DEFINE_bool(ibus, false, "The engine is started by ibus-daemon");
42
43 namespace {
44
45 IBusBus *g_bus = NULL;
46
47 #ifndef NO_LOGGING
EnableVerboseLog()48 void EnableVerboseLog() {
49 const int kDefaultVerboseLevel = 1;
50 if (mozc::Logging::GetVerboseLevel() < kDefaultVerboseLevel) {
51 mozc::Logging::SetVerboseLevel(kDefaultVerboseLevel);
52 }
53 }
54 #endif // NO_LOGGING
55
IgnoreSigChild()56 void IgnoreSigChild() {
57 // Don't wait() child process termination.
58 struct sigaction sa;
59 sa.sa_handler = SIG_IGN;
60 ::sigemptyset(&sa.sa_mask);
61 sa.sa_flags = 0;
62 CHECK_EQ(0, ::sigaction(SIGCHLD, &sa, NULL));
63 // TODO(taku): move this function inside client::Session::LaunchTool
64 }
65
66 // Creates a IBusComponent object and add engine(s) to the object.
GetIBusComponent()67 IBusComponent *GetIBusComponent() {
68 IBusComponent *component = ibus_component_new(
69 kComponentName,
70 kComponentDescription,
71 mozc::Version::GetMozcVersion().c_str(),
72 kComponentLicense,
73 kComponentAuthor,
74 kComponentHomepage,
75 "",
76 kComponentTextdomain);
77 const string icon_path = mozc::ibus::GetIconPath(kEngineIcon);
78 for (size_t i = 0; i < kEngineArrayLen; ++i) {
79 ibus_component_add_engine(component,
80 ibus_engine_desc_new(kEngineNameArray[i],
81 kEngineLongnameArray[i],
82 kEngineDescription,
83 kEngineLanguage,
84 kComponentLicense,
85 kComponentAuthor,
86 icon_path.c_str(),
87 kEngineLayoutArray[i]));
88 }
89 return component;
90 }
91
92 // Initializes ibus components and adds Mozc engine.
InitIBusComponent(bool executed_by_ibus_daemon)93 void InitIBusComponent(bool executed_by_ibus_daemon) {
94 g_bus = ibus_bus_new();
95 g_signal_connect(g_bus,
96 "disconnected",
97 G_CALLBACK(mozc::ibus::MozcEngine::Disconnected),
98 NULL);
99
100 IBusComponent *component = GetIBusComponent();
101 IBusFactory *factory = ibus_factory_new(ibus_bus_get_connection(g_bus));
102 GList *engines = ibus_component_get_engines(component);
103 for (GList *p = engines; p; p = p->next) {
104 IBusEngineDesc *engine = reinterpret_cast<IBusEngineDesc*>(p->data);
105 const gchar * const engine_name = ibus_engine_desc_get_name(engine);
106 ibus_factory_add_engine(
107 factory, engine_name, mozc::ibus::MozcEngine::GetType());
108 }
109
110 if (executed_by_ibus_daemon) {
111 ibus_bus_request_name(g_bus, kComponentName, 0);
112 } else {
113 ibus_bus_register_component(g_bus, component);
114 }
115 g_object_unref(component);
116 }
117
118 } // namespace
119
main(gint argc,gchar ** argv)120 int main(gint argc, gchar **argv) {
121 mozc::InitMozc(argv[0], &argc, &argv, true);
122 ibus_init();
123 InitIBusComponent(FLAGS_ibus);
124 #ifndef NO_LOGGING
125 EnableVerboseLog();
126 #endif // NO_LOGGING
127 IgnoreSigChild();
128 ibus_main();
129 return 0;
130 }
131