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 "unix/ibus/engine_registrar.h"
31 
32 #include "base/logging.h"
33 #include "unix/ibus/engine_interface.h"
34 
35 namespace {
36 mozc::ibus::EngineInterface *g_engine = NULL;
37 }
38 
39 namespace mozc {
40 namespace ibus {
41 
Register(EngineInterface * engine,IBusEngineClass * engine_class)42 bool EngineRegistrar::Register(EngineInterface *engine,
43                                IBusEngineClass *engine_class) {
44   DCHECK(engine) << "engine is NULL";
45   DCHECK(!g_engine) << "engine is already registered";
46 
47   g_engine = engine;
48 
49   engine_class->cursor_down = CursorDown;
50   engine_class->candidate_clicked = CandidateClicked;
51   engine_class->cursor_down = CursorDown;
52   engine_class->cursor_up = CursorUp;
53   engine_class->disable = Disable;
54   engine_class->enable = Enable;
55   engine_class->focus_in = FocusIn;
56   engine_class->focus_out = FocusOut;
57   engine_class->page_down = PageDown;
58   engine_class->page_up = PageUp;
59   engine_class->process_key_event = ProcessKeyEvent;
60   engine_class->property_activate = PropertyActivate;
61   engine_class->property_hide = PropertyHide;
62   engine_class->property_show = PropertyShow;
63   engine_class->reset = Reset;
64   engine_class->set_capabilities = SetCapabilities;
65   engine_class->set_cursor_location = SetCursorLocation;
66 #if defined(MOZC_ENABLE_IBUS_INPUT_PURPOSE)
67   engine_class->set_content_type = SetContentType;
68 #endif  // MOZC_ENABLE_IBUS_INPUT_PURPOSE
69   return true;
70 }
71 
Unregister(IBusEngineClass * engine_class)72 EngineInterface *EngineRegistrar::Unregister(IBusEngineClass *engine_class) {
73   DCHECK(g_engine) << "engine is not registered";
74 
75   engine_class->cursor_down = NULL;
76   engine_class->candidate_clicked = NULL;
77   engine_class->cursor_down = NULL;
78   engine_class->cursor_up = NULL;
79   engine_class->disable = NULL;
80   engine_class->enable = NULL;
81   engine_class->focus_in = NULL;
82   engine_class->focus_out = NULL;
83   engine_class->page_down = NULL;
84   engine_class->page_up = NULL;
85   engine_class->process_key_event = NULL;
86   engine_class->property_activate = NULL;
87   engine_class->property_hide = NULL;
88   engine_class->property_show = NULL;
89   engine_class->reset = NULL;
90   engine_class->set_capabilities = NULL;
91   engine_class->set_cursor_location = NULL;
92 #if defined(MOZC_ENABLE_IBUS_INPUT_PURPOSE)
93   engine_class->set_content_type = NULL;
94 #endif  // MOZC_ENABLE_IBUS_INPUT_PURPOSE
95 
96   mozc::ibus::EngineInterface *previous = g_engine;
97   g_engine = NULL;
98   return previous;
99 }
100 
CandidateClicked(IBusEngine * engine,guint index,guint button,guint state)101 void EngineRegistrar::CandidateClicked(
102     IBusEngine *engine,
103     guint index,
104     guint button,
105     guint state) {
106   g_engine->CandidateClicked(engine, index, button, state);
107 }
108 
CursorDown(IBusEngine * engine)109 void EngineRegistrar::CursorDown(IBusEngine *engine) {
110   g_engine->CursorDown(engine);
111 }
112 
CursorUp(IBusEngine * engine)113 void EngineRegistrar::CursorUp(IBusEngine *engine) {
114   g_engine->CursorUp(engine);
115 }
116 
Disable(IBusEngine * engine)117 void EngineRegistrar::Disable(IBusEngine *engine) {
118   g_engine->Disable(engine);
119 }
120 
Enable(IBusEngine * engine)121 void EngineRegistrar::Enable(IBusEngine *engine) {
122   g_engine->Enable(engine);
123 }
124 
FocusIn(IBusEngine * engine)125 void EngineRegistrar::FocusIn(IBusEngine *engine) {
126   g_engine->FocusIn(engine);
127 }
128 
FocusOut(IBusEngine * engine)129 void EngineRegistrar::FocusOut(IBusEngine *engine) {
130   g_engine->FocusOut(engine);
131 }
132 
PageDown(IBusEngine * engine)133 void EngineRegistrar::PageDown(IBusEngine *engine) {
134   g_engine->PageDown(engine);
135 }
136 
PageUp(IBusEngine * engine)137 void EngineRegistrar::PageUp(IBusEngine *engine) {
138   g_engine->PageUp(engine);
139 }
140 
ProcessKeyEvent(IBusEngine * engine,guint keyval,guint keycode,guint state)141 gboolean EngineRegistrar::ProcessKeyEvent(
142     IBusEngine *engine,
143     guint keyval,
144     guint keycode,
145     guint state) {
146   return g_engine->ProcessKeyEvent(engine, keyval, keycode, state);
147 }
148 
PropertyActivate(IBusEngine * engine,const gchar * property_name,guint property_state)149 void EngineRegistrar::PropertyActivate(
150     IBusEngine *engine,
151     const gchar *property_name,
152     guint property_state) {
153   g_engine->PropertyActivate(engine, property_name, property_state);
154 }
155 
PropertyHide(IBusEngine * engine,const gchar * property_name)156 void EngineRegistrar::PropertyHide(
157     IBusEngine *engine,
158     const gchar *property_name) {
159   g_engine->PropertyHide(engine, property_name);
160 }
161 
PropertyShow(IBusEngine * engine,const gchar * property_name)162 void EngineRegistrar::PropertyShow(
163     IBusEngine *engine,
164     const gchar *property_name) {
165   g_engine->PropertyShow(engine, property_name);
166 }
167 
Reset(IBusEngine * engine)168 void EngineRegistrar::Reset(IBusEngine *engine) {
169   g_engine->Reset(engine);
170 }
171 
SetCapabilities(IBusEngine * engine,guint capabilities)172 void EngineRegistrar::SetCapabilities(
173     IBusEngine *engine,
174     guint capabilities) {
175   g_engine->SetCapabilities(engine, capabilities);
176 }
177 
SetCursorLocation(IBusEngine * engine,gint x,gint y,gint w,gint h)178 void EngineRegistrar::SetCursorLocation(
179     IBusEngine *engine,
180     gint x,
181     gint y,
182     gint w,
183     gint h) {
184   g_engine->SetCursorLocation(engine, x, y, w, h);
185 }
186 
SetContentType(IBusEngine * engine,guint purpose,guint hints)187 void EngineRegistrar::SetContentType(
188     IBusEngine *engine,
189     guint purpose,
190     guint hints) {
191   g_engine->SetContentType(engine, purpose, hints);
192 }
193 
194 }  // namespace ibus
195 }  // namespace mozc
196