2 * ISF(Input Service Framework)
4 * ISF is based on SCIM 1.4.7 and extended for supporting more mobile fitable.
5 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
7 * Contact: Haifeng Deng <haifeng.deng@samsung.com>, Hengliang Luo <hl.luo@samsung.com>
9 * This library is free software; you can redistribute it and/or modify it under
10 * the terms of the GNU Lesser General Public License as published by the
11 * Free Software Foundation; either version 2.1 of the License, or (at your option)
14 * This library is distributed in the hope that it will be useful, but WITHOUT ANY
15 * WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17 * License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this library; if not, write to the Free Software Foundation, Inc., 51
21 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 #define Uses_SCIM_TRANSACTION
26 #define Uses_ISF_IMCONTROL_CLIENT
32 #define IMCONTROLDBG(str...)
33 #define IMCONTROLERR(str...) printf(str)
38 typedef Signal1<void, int> IMControlClientSignalVoid;
40 class IMControlClient::IMControlClientImpl
42 SocketClient m_socket_imclient2panel;
43 SocketClient m_socket_panel2imclient;
45 uint32 m_socket_i2p_magic_key;
46 uint32 m_socket_p2i_magic_key;
49 IMControlClientSignalVoid m_signal_show_ise;
50 IMControlClientSignalVoid m_signal_hide_ise;
53 IMControlClientImpl ()
54 : m_socket_timeout (scim_get_default_socket_timeout ()),
55 m_socket_i2p_magic_key (0),
56 m_socket_p2i_magic_key (0) {
59 int open_connection (void) {
61 String display = String(getenv ("DISPLAY"));
63 SocketAddress addr (scim_get_default_panel_socket_address (display));
65 if (m_socket_imclient2panel.is_connected ()) close_connection ();
67 bool ret=false, ret2=false;
70 /* Try three times. */
72 ret = m_socket_imclient2panel.connect (addr);
73 ret2 = m_socket_panel2imclient.connect (addr);
76 scim_launch_panel (true, config, display, NULL);
77 for (int i = 0; i < 200; ++i) {
78 if (m_socket_imclient2panel.connect (addr)) {
86 if (ret && scim_socket_open_connection (m_socket_i2p_magic_key, String ("IMControl_Active"), String ("Panel"), m_socket_imclient2panel, m_socket_timeout)) {
87 if (ret2 && scim_socket_open_connection (m_socket_p2i_magic_key, String ("IMControl_Passive"), String ("Panel"), m_socket_panel2imclient, m_socket_timeout))
90 m_socket_imclient2panel.close ();
91 m_socket_panel2imclient.close ();
93 if (count++ >= 3) break;
98 return m_socket_imclient2panel.get_id ();
101 void close_connection (void) {
102 m_socket_imclient2panel.close ();
103 m_socket_panel2imclient.close ();
104 m_socket_i2p_magic_key = 0;
105 m_socket_p2i_magic_key = 0;
108 bool is_connected (void) const {
109 return (m_socket_imclient2panel.is_connected () && m_socket_panel2imclient.is_connected ());
112 int get_panel2imclient_connection_number (void) const {
113 return m_socket_panel2imclient.get_id ();
116 bool prepare (void) {
117 if (!m_socket_imclient2panel.is_connected ()) return false;
120 m_trans.put_command (SCIM_TRANS_CMD_REQUEST);
121 m_trans.put_data (m_socket_i2p_magic_key);
127 if (!m_socket_imclient2panel.is_connected ()) return false;
128 if (m_trans.get_data_type () != SCIM_TRANS_DATA_UNKNOWN)
129 return m_trans.write_to_socket (m_socket_imclient2panel, 0x4d494353);
134 void show_ise (void *data, int length) {
135 m_trans.put_command (ISM_TRANS_CMD_SHOW_ISE_PANEL);
136 m_trans.put_data ((const char *)data, (size_t)length);
139 void hide_ise (void) {
140 m_trans.put_command (ISM_TRANS_CMD_HIDE_ISE_PANEL);
143 void show_control_panel (void) {
144 m_trans.put_command (ISM_TRANS_CMD_SHOW_ISF_CONTROL);
147 void hide_control_panel (void) {
148 m_trans.put_command (ISM_TRANS_CMD_HIDE_ISF_CONTROL);
151 void set_mode (int mode) {
152 m_trans.put_command (ISM_TRANS_CMD_SET_ISE_MODE);
153 m_trans.put_data (mode);
156 void set_imdata (const char* data, int len) {
157 m_trans.put_command (ISM_TRANS_CMD_SET_ISE_IMDATA);
158 m_trans.put_data (data, len);
161 void get_imdata (char* data, int* len) {
164 char* data_temp = NULL;
166 m_trans.put_command (ISM_TRANS_CMD_GET_ISE_IMDATA);
167 m_trans.write_to_socket (m_socket_imclient2panel);
168 if (!m_trans.read_from_socket (m_socket_imclient2panel, m_socket_timeout))
169 IMCONTROLERR ("%s:: read_from_socket() may be timeout \n", __FUNCTION__);
171 if (m_trans.get_command (cmd) && cmd == SCIM_TRANS_CMD_REPLY &&
172 m_trans.get_command (cmd) && cmd == SCIM_TRANS_CMD_OK &&
173 m_trans.get_data (&data_temp, datalen)) {
174 memcpy (data, data_temp, datalen);
177 IMCONTROLERR ("%s:: get_command() or get_data() may fail!!!\n", __FUNCTION__);
182 void get_ise_window_geometry (int* x, int* y, int* width, int* height) {
189 m_trans.put_command (ISM_TRANS_CMD_GET_ACTIVE_ISE_GEOMETRY);
190 m_trans.write_to_socket (m_socket_imclient2panel);
192 if (!m_trans.read_from_socket (m_socket_imclient2panel, m_socket_timeout))
193 IMCONTROLERR ("%s:: read_from_socket() may be timeout \n", __FUNCTION__);
195 if (m_trans.get_command (cmd) && cmd == SCIM_TRANS_CMD_REPLY &&
196 m_trans.get_command (cmd) && cmd == SCIM_TRANS_CMD_OK &&
197 m_trans.get_data (x_temp) &&
198 m_trans.get_data (y_temp) &&
199 m_trans.get_data (w_temp) &&
200 m_trans.get_data (h_temp)) {
206 IMCONTROLERR ("%s:: get_command() or get_data() may fail!!!\n", __FUNCTION__);
210 void get_candidate_window_geometry (int* x, int* y, int* width, int* height) {
217 m_trans.put_command (ISM_TRANS_CMD_GET_CANDIDATE_GEOMETRY);
218 m_trans.write_to_socket (m_socket_imclient2panel);
220 if (!m_trans.read_from_socket (m_socket_imclient2panel, m_socket_timeout))
221 IMCONTROLERR ("%s::read_from_socket () may be timeout \n", __FUNCTION__);
223 if (m_trans.get_command (cmd) && cmd == SCIM_TRANS_CMD_REPLY &&
224 m_trans.get_command (cmd) && cmd == SCIM_TRANS_CMD_OK &&
225 m_trans.get_data (x_temp) &&
226 m_trans.get_data (y_temp) &&
227 m_trans.get_data (w_temp) &&
228 m_trans.get_data (h_temp)) {
234 IMCONTROLERR ("%s::get_command () or get_data () is failed!!!\n", __FUNCTION__);
238 void get_ise_language_locale (char **locale) {
243 m_trans.put_command (ISM_TRANS_CMD_GET_ISE_LANGUAGE_LOCALE);
244 m_trans.write_to_socket (m_socket_imclient2panel);
245 if (!m_trans.read_from_socket (m_socket_imclient2panel, m_socket_timeout))
246 IMCONTROLERR ("%s::read_from_socket () may be timeout \n", __FUNCTION__);
248 if (m_trans.get_command (cmd) && cmd == SCIM_TRANS_CMD_REPLY &&
249 m_trans.get_command (cmd) && cmd == SCIM_TRANS_CMD_OK &&
250 m_trans.get_data (&data, datalen)) {
252 *locale = strndup (data, datalen);
254 IMCONTROLERR ("%s::get_command () or get_data () is failed!!!\n", __FUNCTION__);
256 *locale = strdup ("");
262 void set_return_key_type (int type) {
263 m_trans.put_command (ISM_TRANS_CMD_SET_RETURN_KEY_TYPE);
264 m_trans.put_data (type);
267 void get_return_key_type (int &type) {
271 m_trans.put_command (ISM_TRANS_CMD_GET_RETURN_KEY_TYPE);
272 m_trans.write_to_socket (m_socket_imclient2panel);
273 if (!m_trans.read_from_socket (m_socket_imclient2panel, m_socket_timeout))
274 IMCONTROLERR ("%s:: read_from_socket() may be timeout \n", __FUNCTION__);
276 if (m_trans.get_command (cmd) && cmd == SCIM_TRANS_CMD_REPLY &&
277 m_trans.get_command (cmd) && cmd == SCIM_TRANS_CMD_OK &&
278 m_trans.get_data (temp)) {
281 IMCONTROLERR ("%s:: get_command() or get_data() may fail!!!\n", __FUNCTION__);
285 void set_return_key_disable (int disabled) {
286 m_trans.put_command (ISM_TRANS_CMD_SET_RETURN_KEY_DISABLE);
287 m_trans.put_data (disabled);
290 void get_return_key_disable (int &disabled) {
294 m_trans.put_command (ISM_TRANS_CMD_GET_RETURN_KEY_DISABLE);
295 m_trans.write_to_socket (m_socket_imclient2panel);
296 if (!m_trans.read_from_socket (m_socket_imclient2panel, m_socket_timeout))
297 IMCONTROLERR ("%s:: read_from_socket() may be timeout \n", __FUNCTION__);
299 if (m_trans.get_command (cmd) && cmd == SCIM_TRANS_CMD_REPLY &&
300 m_trans.get_command (cmd) && cmd == SCIM_TRANS_CMD_OK &&
301 m_trans.get_data (temp)) {
304 IMCONTROLERR ("%s:: get_command() or get_data() may fail!!!\n", __FUNCTION__);
308 void set_layout (int layout) {
309 m_trans.put_command (ISM_TRANS_CMD_SET_LAYOUT);
310 m_trans.put_data (layout);
313 void get_layout (int* layout) {
317 m_trans.put_command (ISM_TRANS_CMD_GET_LAYOUT);
318 m_trans.write_to_socket (m_socket_imclient2panel);
319 if (!m_trans.read_from_socket (m_socket_imclient2panel, m_socket_timeout))
320 IMCONTROLERR ("%s:: read_from_socket() may be timeout \n", __FUNCTION__);
322 if (m_trans.get_command (cmd) && cmd == SCIM_TRANS_CMD_REPLY &&
323 m_trans.get_command (cmd) && cmd == SCIM_TRANS_CMD_OK &&
324 m_trans.get_data (layout_temp)) {
325 *layout = layout_temp;
327 IMCONTROLERR ("%s:: get_command() or get_data() may fail!!!\n", __FUNCTION__);
331 void set_ise_language (int language) {
332 m_trans.put_command (ISM_TRANS_CMD_SET_ISE_LANGUAGE);
333 m_trans.put_data (language);
336 void set_active_ise_by_uuid (const char* uuid) {
338 m_trans.put_command (ISM_TRANS_CMD_SET_ACTIVE_ISE_BY_UUID);
339 m_trans.put_data (uuid, strlen(uuid)+1);
340 m_trans.write_to_socket (m_socket_imclient2panel);
341 if (!m_trans.read_from_socket (m_socket_imclient2panel, m_socket_timeout))
342 IMCONTROLERR ("%s:: read_from_socket() may be timeout \n", __FUNCTION__);
344 if (m_trans.get_command (cmd) && cmd == SCIM_TRANS_CMD_REPLY &&
345 m_trans.get_command (cmd) && cmd == SCIM_TRANS_CMD_OK) {
347 IMCONTROLERR ("%s:: get_command() or get_data() may fail!!!\n", __FUNCTION__);
351 void get_ise_list (int* count, char*** iselist) {
353 uint32 count_temp = 0;
356 char * buf_temp = NULL;
358 m_trans.put_command (ISM_TRANS_CMD_GET_ISE_LIST);
359 m_trans.write_to_socket (m_socket_imclient2panel);
360 if (!m_trans.read_from_socket (m_socket_imclient2panel, m_socket_timeout))
361 IMCONTROLERR ("%s:: read_from_socket() may be timeout \n", __FUNCTION__);
363 if (m_trans.get_command (cmd) && cmd == SCIM_TRANS_CMD_REPLY &&
364 m_trans.get_command (cmd) && cmd == SCIM_TRANS_CMD_OK &&
365 m_trans.get_data (count_temp) ) {
369 IMCONTROLERR ("%s:: get_command() or get_data() may fail!!!\n", __FUNCTION__);
372 if (count_temp > 0) {
373 buf = (char**)malloc (count_temp * sizeof (char*));
375 memset (buf, 0, count_temp * sizeof (char*));
376 for (uint32 i = 0; i < count_temp; i++) {
377 if (m_trans.get_data (&buf_temp, len))
385 void reset_ise_option (void) {
388 m_trans.put_command (ISM_TRANS_CMD_RESET_ISE_OPTION);
389 m_trans.write_to_socket (m_socket_imclient2panel);
390 if (!m_trans.read_from_socket (m_socket_imclient2panel, m_socket_timeout))
391 IMCONTROLERR ("%s:: read_from_socket() may be timeout \n", __FUNCTION__);
393 if (m_trans.get_command (cmd) && cmd == SCIM_TRANS_CMD_REPLY &&
394 m_trans.get_command (cmd) && cmd == SCIM_TRANS_CMD_OK) {
397 IMCONTROLERR ("%s:: get_command() is failed!!!\n", __FUNCTION__);
401 void set_caps_mode (int mode) {
402 m_trans.put_command (ISM_TRANS_CMD_SET_CAPS_MODE);
403 m_trans.put_data (mode);
407 IMControlClient::IMControlClient ()
408 : m_impl (new IMControlClientImpl ())
412 IMControlClient::~IMControlClient ()
418 IMControlClient::open_connection (void)
420 return m_impl->open_connection ();
424 IMControlClient::close_connection (void)
426 m_impl->close_connection ();
429 bool IMControlClient::is_connected (void) const
431 return m_impl->is_connected ();
434 int IMControlClient::get_panel2imclient_connection_number (void) const
436 return m_impl->get_panel2imclient_connection_number ();
440 IMControlClient::prepare (void)
442 return m_impl->prepare ();
446 IMControlClient::send (void)
448 return m_impl->send ();
451 void IMControlClient::show_ise (void *data, int length)
453 m_impl->show_ise (data,length);
456 void IMControlClient::hide_ise (void)
461 void IMControlClient::show_control_panel (void)
463 m_impl->show_control_panel ();
466 void IMControlClient::hide_control_panel (void)
468 m_impl->hide_control_panel ();
471 void IMControlClient::set_mode (int mode)
473 m_impl->set_mode (mode);
476 void IMControlClient::set_imdata (const char* data, int len)
478 m_impl->set_imdata (data, len);
481 void IMControlClient::get_imdata (char* data, int* len)
483 m_impl->get_imdata (data, len);
486 void IMControlClient::get_ise_window_geometry (int* x, int* y, int* width, int* height)
488 m_impl->get_ise_window_geometry (x, y, width, height);
491 void IMControlClient::get_candidate_window_geometry (int* x, int* y, int* width, int* height)
493 m_impl->get_candidate_window_geometry (x, y, width, height);
496 void IMControlClient::get_ise_language_locale (char **locale)
498 m_impl->get_ise_language_locale (locale);
501 void IMControlClient::set_return_key_type (int type)
503 m_impl->set_return_key_type (type);
506 void IMControlClient::get_return_key_type (int &type)
508 m_impl->get_return_key_type (type);
511 void IMControlClient::set_return_key_disable (int disabled)
513 m_impl->set_return_key_disable (disabled);
516 void IMControlClient::get_return_key_disable (int &disabled)
518 m_impl->get_return_key_disable (disabled);
521 void IMControlClient::set_layout (int layout)
523 m_impl->set_layout (layout);
526 void IMControlClient::get_layout (int* layout)
528 m_impl->get_layout (layout);
531 void IMControlClient::set_ise_language (int language)
533 m_impl->set_ise_language (language);
536 void IMControlClient::set_active_ise_by_uuid (const char* uuid)
538 m_impl->set_active_ise_by_uuid (uuid);
541 void IMControlClient::get_ise_list (int* count, char*** iselist)
543 m_impl->get_ise_list (count, iselist);
546 void IMControlClient::reset_ise_option (void)
548 m_impl->reset_ise_option ();
551 void IMControlClient::set_caps_mode (int mode)
553 m_impl->set_caps_mode (mode);
559 vi:ts=4:nowrap:ai:expandtab