Add init interface with main_window as a parameter
[platform/core/uifw/libscl-core.git] / src / sclcoreimpl.cpp
1 /*
2  * Copyright (c) 2014 - 2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 #include "sclcoreimpl.h"
19 #include <app_common.h>
20 #include <dlog.h>
21
22 using namespace scl;
23
24 CSCLCoreImpl::CSCLCoreImpl()
25 {
26     m_event_callback = NULL;
27     m_display = NULL;
28     m_uuid = NULL;
29
30     m_initialized = FALSE;
31 }
32
33 CSCLCoreImpl::~CSCLCoreImpl()
34 {
35     if (m_initialized) {
36         fini();
37     }
38     if (m_display) {
39         free(m_display);
40         m_display = NULL;
41     }
42     if (m_uuid) {
43         free(m_uuid);
44         m_uuid = NULL;
45     }
46 }
47
48 CSCLCoreImpl*
49 CSCLCoreImpl::get_instance()
50 {
51     static CSCLCoreImpl instance;
52     return &instance;
53 }
54
55 void CSCLCoreImpl::init(sclwindow main_window)
56 {
57     m_core_ui.init(main_window);
58
59     m_connection.init();
60     m_connection.open_connection(m_display);
61
62     if (m_event_callback) {
63         m_event_callback->on_init();
64     }
65
66     m_initialized = TRUE;
67 }
68
69 void CSCLCoreImpl::fini()
70 {
71     if (m_event_callback) {
72         m_event_callback->on_exit();
73     }
74
75     m_connection.close_connection();
76
77     m_core_ui.fini();
78     m_connection.fini();
79
80     m_initialized = FALSE;
81 }
82
83 void CSCLCoreImpl::set_core_event_callback(ISCLCoreEventCallback *callback)
84 {
85     m_event_callback = callback;
86 }
87
88 ISCLCoreEventCallback* CSCLCoreImpl::get_core_event_callback()
89 {
90     ISCLCoreEventCallback* ret = m_event_callback;
91     return ret;
92 }
93
94 CSCLCoreUI* CSCLCoreImpl::get_core_ui()
95 {
96     return &m_core_ui;
97 }
98
99 CSCLConnection* CSCLCoreImpl::get_connection()
100 {
101     return &m_connection;
102 }
103
104 sclchar* CSCLCoreImpl::get_uuid()
105 {
106     return m_uuid;
107 }
108
109 sclchar* CSCLCoreImpl::get_display()
110 {
111     return m_display;
112 }
113
114 void CSCLCoreImpl::config_reload()
115 {
116     m_connection.config_reload();
117 }
118
119 sclboolean CSCLCoreImpl::config_read_int(const sclchar *name, sclint &value)
120 {
121     return m_connection.config_read_int(name, value);
122 }
123
124 sclboolean CSCLCoreImpl::config_read_string(const sclchar *name, std::string &value)
125 {
126     return m_connection.config_read_string(name, value);
127 }
128
129 sclboolean CSCLCoreImpl::config_write_int(const sclchar *name, sclint value)
130 {
131     return m_connection.config_write_int(name, value);
132 }
133
134 sclboolean CSCLCoreImpl::config_write_string(const sclchar *name, const std::string value)
135 {
136     return m_connection.config_write_string(name, value);
137 }
138
139 sclboolean CSCLCoreImpl::config_erase(const sclchar *name)
140 {
141     return m_connection.config_erase(name);
142 }
143
144 sclboolean CSCLCoreImpl::config_flush(void)
145 {
146     return m_connection.config_flush();
147 }
148
149 void CSCLCoreImpl::send_imengine_event(sclint ic, const sclchar *ic_uuid, const sclint command, const sclu32 value)
150 {
151     m_connection.send_imengine_event(ic, ic_uuid, command, value);
152 }
153
154 void CSCLCoreImpl::reset_keyboard_ise()
155 {
156     m_connection.reset_keyboard_ise();
157 }
158
159 void CSCLCoreImpl::flush_keyboard_ise()
160 {
161     m_connection.flush_keyboard_ise();
162 }
163
164 void CSCLCoreImpl::send_key_event(sclint ic, const sclchar *ic_uuid, sclu32 keycode, sclu16 keymask)
165 {
166     m_connection.send_key_event(ic, ic_uuid, keycode, keymask);
167 }
168
169 void CSCLCoreImpl::forward_key_event(sclint ic, const sclchar *ic_uuid, sclu32 keycode, sclu16 keymask)
170 {
171     m_connection.forward_key_event(ic, ic_uuid, keycode, keymask);
172 }
173
174 void CSCLCoreImpl::commit_string(sclint ic, const sclchar *ic_uuid, const sclchar *str)
175 {
176     m_connection.commit_string(ic, ic_uuid, str);
177 }
178
179 void CSCLCoreImpl::select_candidate(int index)
180 {
181     m_connection.select_candidate(index);
182 }
183
184 void CSCLCoreImpl::show_preedit_string(sclint ic, const sclchar *ic_uuid)
185 {
186     m_connection.show_preedit_string(ic, ic_uuid);
187 }
188
189 void CSCLCoreImpl::show_aux_string(void)
190 {
191     m_connection.show_aux_string();
192 }
193
194 void CSCLCoreImpl::show_candidate_string(void)
195 {
196     m_connection.show_candidate_string();
197 }
198
199 void CSCLCoreImpl::show_associate_string(void)
200 {
201     m_connection.show_associate_string();
202 }
203
204 void CSCLCoreImpl::hide_preedit_string(sclint ic, const sclchar *ic_uuid)
205 {
206     m_connection.hide_preedit_string(ic, ic_uuid);
207 }
208
209 void CSCLCoreImpl::hide_aux_string(void)
210 {
211     m_connection.hide_aux_string();
212 }
213
214 void CSCLCoreImpl::hide_candidate_string(void)
215 {
216     m_connection.hide_candidate_string();
217 }
218
219 void CSCLCoreImpl::hide_associate_string(void)
220 {
221     m_connection.hide_associate_string();
222 }
223
224 void CSCLCoreImpl::update_preedit_string(sclint ic, const sclchar *ic_uuid, const sclchar *str)
225 {
226     m_connection.update_preedit_string(ic, ic_uuid, str);
227 }
228
229 void CSCLCoreImpl::update_preedit_string(sclint ic, const sclchar *ic_uuid, const sclchar *str, const scim::AttributeList &attrs)
230 {
231     m_connection.update_preedit_string(ic, ic_uuid, str, attrs);
232 }
233
234 void CSCLCoreImpl::update_aux_string(const sclchar *str)
235 {
236     m_connection.update_aux_string(str);
237 }
238
239 void CSCLCoreImpl::update_input_context(sclu32 type, sclu32 value)
240 {
241     m_connection.update_input_context(type, value);
242 }
243
244 void CSCLCoreImpl::update_geometry(sclint x, sclint y, sclint width, sclint height)
245 {
246     m_connection.update_geometry(x, y, width, height);
247 }
248
249 void CSCLCoreImpl::get_surrounding_text(const sclchar* ic_uuid, sclint maxlen_before, sclint maxlen_after) const
250 {
251     m_connection.get_surrounding_text(ic_uuid, maxlen_before, maxlen_after);
252 }
253
254 sclint CSCLCoreImpl::get_surrounding_text(sclint maxlen_before, sclint maxlen_after, sclchar **text, int &cursor)
255 {
256     return m_connection.get_surrounding_text(maxlen_before, maxlen_after, text, cursor);
257 }
258
259 void CSCLCoreImpl::delete_surrounding_text(sclint offset, sclint len) const
260 {
261     m_connection.delete_surrounding_text(offset, len);
262 }
263
264 void CSCLCoreImpl::set_candidate_position(sclint left, sclint top)
265 {
266     m_connection.set_candidate_position(left, top);
267 }
268
269 void CSCLCoreImpl::enable_soft_candidate(sclboolean enable)
270 {
271     m_connection.enable_soft_candidate(enable);
272 }
273
274 void CSCLCoreImpl::candidate_hide(void)
275 {
276     m_connection.candidate_hide();
277 }
278
279 void CSCLCoreImpl::set_keyboard_ise_by_uuid(const sclchar *uuid)
280 {
281     m_connection.set_keyboard_ise_by_uuid(uuid);
282 }
283
284 void CSCLCoreImpl::get_keyboard_ise(const sclchar *uuid)
285 {
286     m_connection.get_keyboard_ise(uuid);
287 }
288
289 void CSCLCoreImpl::on_run(const sclchar *uuid, const sclchar *display)
290 {
291     LOGD("uuid : '%s', display : '%s'\n", uuid, display);
292
293     if (uuid && strlen(uuid) > 0) {
294         if (m_uuid) {
295             free(m_uuid);
296         }
297
298         m_uuid = strdup(uuid);
299     }
300
301     if (display) {
302         if (m_display) {
303             free(m_display);
304         }
305
306         m_display = strdup(display);
307     }
308
309     if (m_event_callback) {
310         m_event_callback->on_run(0, NULL);
311     }
312 }
313
314 void CSCLCoreImpl::run()
315 {
316     if (!m_initialized) {
317         init(NULL);
318     }
319
320     if (!m_uuid) {
321         char *appid = NULL;
322         app_get_id(&appid);
323
324         LOGD("appid : '%s'\n", appid);
325
326         if (appid) {
327             m_uuid = strdup(appid);
328             free(appid);
329         }
330     }
331
332     if (!m_display) {
333         const char *display = getenv("DISPLAY");
334         LOGD("display env : '%s'\n", display);
335         m_display = display ? strdup(display) : strdup(":0");
336     }
337
338     m_core_ui.run(m_display);
339 }
340
341 sclwindow CSCLCoreImpl::get_main_window()
342 {
343     return m_core_ui.get_main_window();
344 }
345
346 int CSCLCoreImpl::get_screen_rotation_degree()
347 {
348     return m_core_ui.get_screen_rotation_degree();
349 }
350
351 void CSCLCoreImpl::set_keyboard_size_hints(SclSize portrait, SclSize landscape)
352 {
353     m_core_ui.set_keyboard_size_hints(portrait, landscape);
354 }
355
356 sclwindow CSCLCoreImpl::create_option_window()
357 {
358     return m_core_ui.create_option_window(OPTION_WINDOW_TYPE_NORMAL);
359 }
360
361 void CSCLCoreImpl::destroy_option_window(sclwindow window)
362 {
363     m_core_ui.destroy_option_window(window);
364 }
365
366 void CSCLCoreImpl::set_selection(sclint start, sclint end)
367 {
368     m_connection.set_selection(start, end);
369 }
370
371 void CSCLCoreImpl::send_private_command(const sclchar *command)
372 {
373     m_connection.send_private_command(command);
374 }
375
376 void CSCLCoreImpl::get_selection_text(sclchar **text)
377 {
378     m_connection.get_selection_text(text);
379 }
380
381 void CSCLCoreImpl::request_ise_hide()
382 {
383     m_connection.request_ise_hide();
384 }