upload tizen1.0 source
[profile/ivi/isf.git] / ism / src / isf_imcontrol_client.cpp
1 /*
2  * ISF(Input Service Framework)
3  *
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.
6  *
7  * Contact: Haifeng Deng <haifeng.deng@samsung.com>, Hengliang Luo <hl.luo@samsung.com>
8  *
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)
12  * any later version.
13  *
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.
18  *
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
22  *
23  */
24
25 #define Uses_SCIM_TRANSACTION
26 #define Uses_ISF_IMCONTROL_CLIENT
27
28
29 #include <string.h>
30 #include "scim.h"
31
32 #define IMCONTROLDBG(str...)
33 #define IMCONTROLERR(str...) printf(str)
34
35 namespace scim
36 {
37
38 typedef Signal1<void, int> IMControlClientSignalVoid;
39
40 class IMControlClient::IMControlClientImpl
41 {
42     SocketClient                                m_socket_imclient2panel;
43     SocketClient                                m_socket_panel2imclient;
44     int                                         m_socket_timeout;
45     uint32                                      m_socket_i2p_magic_key;
46     uint32                                      m_socket_p2i_magic_key;
47     Transaction                                 m_trans;
48
49     IMControlClientSignalVoid                   m_signal_show_ise;
50     IMControlClientSignalVoid                   m_signal_hide_ise;
51
52 public:
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) {
57     }
58
59     int open_connection (void) {
60         String config = "";
61         String display = String(getenv ("DISPLAY"));
62
63         SocketAddress addr (scim_get_default_panel_socket_address (display));
64
65         if (m_socket_imclient2panel.is_connected ()) close_connection ();
66
67         bool ret=false, ret2=false;
68         int count = 0;
69
70         /* Try three times. */
71         while (1) {
72             ret = m_socket_imclient2panel.connect (addr);
73             ret2 = m_socket_panel2imclient.connect (addr);
74             if (!ret) {
75                 scim_usleep (100000);
76                 scim_launch_panel (true, config, display, NULL);
77                 for (int i = 0; i < 200; ++i) {
78                     if (m_socket_imclient2panel.connect (addr)) {
79                         ret = true;
80                         break;
81                     }
82                     scim_usleep (100000);
83                 }
84             }
85
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))
88                     break;
89             }
90             m_socket_imclient2panel.close ();
91             m_socket_panel2imclient.close ();
92
93             if (count++ >= 3) break;
94
95             scim_usleep (100000);
96         }
97
98         return m_socket_imclient2panel.get_id ();
99     }
100
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;
106     }
107
108     bool is_connected (void) const {
109         return (m_socket_imclient2panel.is_connected () && m_socket_panel2imclient.is_connected ());
110     }
111
112     int  get_panel2imclient_connection_number  (void) const {
113         return m_socket_panel2imclient.get_id ();
114     }
115
116     bool prepare (void) {
117         if (!m_socket_imclient2panel.is_connected ()) return false;
118
119         m_trans.clear ();
120         m_trans.put_command (SCIM_TRANS_CMD_REQUEST);
121         m_trans.put_data (m_socket_i2p_magic_key);
122
123         return true;
124     }
125
126     bool send (void) {
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);
130         return false;
131     }
132
133
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);
137     }
138
139     void hide_ise (void) {
140         m_trans.put_command (ISM_TRANS_CMD_HIDE_ISE_PANEL);
141     }
142
143     void show_control_panel (void) {
144         m_trans.put_command (ISM_TRANS_CMD_SHOW_ISF_CONTROL);
145     }
146
147     void hide_control_panel (void) {
148         m_trans.put_command (ISM_TRANS_CMD_HIDE_ISF_CONTROL);
149     }
150
151     void set_mode (int mode) {
152         m_trans.put_command (ISM_TRANS_CMD_SET_ISE_MODE);
153         m_trans.put_data (mode);
154     }
155
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);
159     }
160
161     void get_imdata (char* data, int* len) {
162         int cmd;
163         size_t datalen = 0;
164         char* data_temp = NULL;
165
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__);
170
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);
175             *len = datalen;
176         } else {
177             IMCONTROLERR ("%s:: get_command() or get_data() may fail!!!\n", __FUNCTION__);
178         }
179         delete [] data_temp;
180     }
181
182     void get_ise_window_geometry (int* x, int* y, int* width, int* height) {
183         int cmd;
184         uint32 x_temp = 0;
185         uint32 y_temp = 0;
186         uint32 w_temp = 0;
187         uint32 h_temp = 0;
188
189         m_trans.put_command (ISM_TRANS_CMD_GET_ACTIVE_ISE_GEOMETRY);
190         m_trans.write_to_socket (m_socket_imclient2panel);
191
192         if (!m_trans.read_from_socket (m_socket_imclient2panel, m_socket_timeout))
193             IMCONTROLERR ("%s:: read_from_socket() may be timeout \n", __FUNCTION__);
194
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)) {
201             *x = x_temp;
202             *y = y_temp;
203             *width = w_temp;
204             *height = h_temp;
205         } else {
206             IMCONTROLERR ("%s:: get_command() or get_data() may fail!!!\n", __FUNCTION__);
207         }
208     }
209
210     void get_candidate_window_geometry (int* x, int* y, int* width, int* height) {
211         int cmd;
212         uint32 x_temp = 0;
213         uint32 y_temp = 0;
214         uint32 w_temp = 0;
215         uint32 h_temp = 0;
216
217         m_trans.put_command (ISM_TRANS_CMD_GET_CANDIDATE_GEOMETRY);
218         m_trans.write_to_socket (m_socket_imclient2panel);
219
220         if (!m_trans.read_from_socket (m_socket_imclient2panel, m_socket_timeout))
221             IMCONTROLERR ("%s::read_from_socket () may be timeout \n", __FUNCTION__);
222
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)) {
229             *x = x_temp;
230             *y = y_temp;
231             *width = w_temp;
232             *height = h_temp;
233         } else {
234             IMCONTROLERR ("%s::get_command () or get_data () is failed!!!\n", __FUNCTION__);
235         }
236     }
237
238     void get_ise_language_locale (char **locale) {
239         int cmd;
240         size_t datalen = 0;
241         char  *data = NULL;
242
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__);
247
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)) {
251             if (locale)
252                 *locale = strndup (data, datalen);
253         } else {
254             IMCONTROLERR ("%s::get_command () or get_data () is failed!!!\n", __FUNCTION__);
255             if (locale)
256                 *locale = strdup ("");
257         }
258         if (data)
259             delete [] data;
260     }
261
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);
265     }
266
267     void get_return_key_type (int &type) {
268         int cmd;
269         uint32 temp;
270
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__);
275
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)) {
279             type = temp;
280         } else {
281             IMCONTROLERR ("%s:: get_command() or get_data() may fail!!!\n", __FUNCTION__);
282         }
283     }
284
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);
288     }
289
290     void get_return_key_disable (int &disabled) {
291         int cmd;
292         uint32 temp;
293
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__);
298
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)) {
302             disabled = temp;
303         } else {
304             IMCONTROLERR ("%s:: get_command() or get_data() may fail!!!\n", __FUNCTION__);
305         }
306     }
307
308     void set_layout (int layout) {
309         m_trans.put_command (ISM_TRANS_CMD_SET_LAYOUT);
310         m_trans.put_data (layout);
311     }
312
313     void get_layout (int* layout) {
314         int cmd;
315         uint32 layout_temp;
316
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__);
321
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;
326         } else {
327             IMCONTROLERR ("%s:: get_command() or get_data() may fail!!!\n", __FUNCTION__);
328         }
329     }
330
331     void set_ise_language (int language) {
332         m_trans.put_command (ISM_TRANS_CMD_SET_ISE_LANGUAGE);
333         m_trans.put_data (language);
334     }
335
336     void set_active_ise_by_uuid (const char* uuid) {
337         int cmd;
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__);
343
344         if (m_trans.get_command (cmd) && cmd == SCIM_TRANS_CMD_REPLY &&
345                 m_trans.get_command (cmd) && cmd == SCIM_TRANS_CMD_OK) {
346         } else {
347             IMCONTROLERR ("%s:: get_command() or get_data() may fail!!!\n", __FUNCTION__);
348         }
349     }
350
351     void get_ise_list (int* count, char*** iselist) {
352         int cmd;
353         uint32 count_temp = 0;
354         char **buf = NULL;
355         size_t len;
356         char * buf_temp = NULL;
357
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__);
362
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) ) {
366             *count = count_temp;
367         } else {
368             *count = 0;
369             IMCONTROLERR ("%s:: get_command() or get_data() may fail!!!\n", __FUNCTION__);
370         }
371
372         if (count_temp > 0) {
373             buf = (char**)malloc (count_temp * sizeof (char*));
374             if (buf) {
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))
378                         buf[i] = buf_temp;
379                 }
380             }
381         }
382         *iselist = buf;
383     }
384
385     void reset_ise_option (void) {
386         int cmd;
387
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__);
392
393         if (m_trans.get_command (cmd) && cmd == SCIM_TRANS_CMD_REPLY &&
394                 m_trans.get_command (cmd) && cmd == SCIM_TRANS_CMD_OK) {
395             ;
396         } else {
397             IMCONTROLERR ("%s:: get_command() is failed!!!\n", __FUNCTION__);
398         }
399     }
400
401     void set_caps_mode (int mode) {
402         m_trans.put_command (ISM_TRANS_CMD_SET_CAPS_MODE);
403         m_trans.put_data (mode);
404     }
405 };
406
407 IMControlClient::IMControlClient ()
408         : m_impl (new IMControlClientImpl ())
409 {
410 }
411
412 IMControlClient::~IMControlClient ()
413 {
414     delete m_impl;
415 }
416
417 int
418 IMControlClient::open_connection (void)
419 {
420     return m_impl->open_connection ();
421 }
422
423 void
424 IMControlClient::close_connection (void)
425 {
426     m_impl->close_connection ();
427 }
428
429 bool IMControlClient::is_connected (void) const
430 {
431     return m_impl->is_connected ();
432 }
433
434 int IMControlClient::get_panel2imclient_connection_number (void) const
435 {
436     return m_impl->get_panel2imclient_connection_number ();
437 }
438
439 bool
440 IMControlClient::prepare (void)
441 {
442     return m_impl->prepare ();
443 }
444
445 bool
446 IMControlClient::send (void)
447 {
448     return m_impl->send ();
449 }
450
451 void IMControlClient::show_ise (void *data, int length)
452 {
453     m_impl->show_ise (data,length);
454 }
455
456 void IMControlClient::hide_ise (void)
457 {
458     m_impl->hide_ise ();
459 }
460
461 void IMControlClient::show_control_panel (void)
462 {
463     m_impl->show_control_panel ();
464 }
465
466 void IMControlClient::hide_control_panel (void)
467 {
468     m_impl->hide_control_panel ();
469 }
470
471 void IMControlClient::set_mode (int mode)
472 {
473     m_impl->set_mode (mode);
474 }
475
476 void IMControlClient::set_imdata (const char* data, int len)
477 {
478     m_impl->set_imdata (data, len);
479 }
480
481 void IMControlClient::get_imdata (char* data, int* len)
482 {
483     m_impl->get_imdata (data, len);
484 }
485
486 void IMControlClient::get_ise_window_geometry (int* x, int* y, int* width, int* height)
487 {
488     m_impl->get_ise_window_geometry (x, y, width, height);
489 }
490
491 void IMControlClient::get_candidate_window_geometry (int* x, int* y, int* width, int* height)
492 {
493     m_impl->get_candidate_window_geometry (x, y, width, height);
494 }
495
496 void IMControlClient::get_ise_language_locale (char **locale)
497 {
498     m_impl->get_ise_language_locale (locale);
499 }
500
501 void IMControlClient::set_return_key_type (int type)
502 {
503     m_impl->set_return_key_type (type);
504 }
505
506 void IMControlClient::get_return_key_type (int &type)
507 {
508     m_impl->get_return_key_type (type);
509 }
510
511 void IMControlClient::set_return_key_disable (int disabled)
512 {
513     m_impl->set_return_key_disable (disabled);
514 }
515
516 void IMControlClient::get_return_key_disable (int &disabled)
517 {
518     m_impl->get_return_key_disable (disabled);
519 }
520
521 void IMControlClient::set_layout (int layout)
522 {
523     m_impl->set_layout (layout);
524 }
525
526 void IMControlClient::get_layout (int* layout)
527 {
528     m_impl->get_layout (layout);
529 }
530
531 void IMControlClient::set_ise_language (int language)
532 {
533     m_impl->set_ise_language (language);
534 }
535
536 void IMControlClient::set_active_ise_by_uuid (const char* uuid)
537 {
538     m_impl->set_active_ise_by_uuid (uuid);
539 }
540
541 void IMControlClient::get_ise_list (int* count, char*** iselist)
542 {
543     m_impl->get_ise_list (count, iselist);
544 }
545
546 void IMControlClient::reset_ise_option (void)
547 {
548     m_impl->reset_ise_option ();
549 }
550
551 void IMControlClient::set_caps_mode (int mode)
552 {
553     m_impl->set_caps_mode (mode);
554 }
555
556 };
557
558 /*
559 vi:ts=4:nowrap:ai:expandtab
560 */