80d98894f16820535c32cb19f5e6450f6c8cce1e
[platform/core/uifw/isf.git] / ism / src / isf_control.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) 2012-2015 Samsung Electronics Co., Ltd.
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_CONFIG_PATH
26 #define Uses_SCIM_TRANSACTION
27 #define Uses_ISF_IMCONTROL_CLIENT
28 #define Uses_SCIM_COMPOSE_KEY
29 #define Uses_SCIM_PANEL_AGENT
30
31 #include <string.h>
32 #include "scim.h"
33 #include "isf_control.h"
34
35 #include <dlog.h>
36 #include <vconf.h>
37 #include <vconf-keys.h>
38 #include <Eina.h>
39 #include "isf_debug.h"
40 #ifdef LOG_TAG
41 #undef LOG_TAG
42 #endif
43 #define LOG_TAG "ISF_CONTROL"
44
45 using namespace scim;
46
47
48 EXAPI int isf_control_set_active_ise_by_uuid (const char *uuid)
49 {
50     if (uuid == NULL)
51         return -1;
52
53     IMControlClient imcontrol_client;
54     int ret = 0;
55     if (!imcontrol_client.open_connection ())
56         return -1;
57
58     imcontrol_client.prepare ();
59     if (!imcontrol_client.set_active_ise_by_uuid (uuid))
60         ret = -1;
61
62     imcontrol_client.close_connection ();
63
64     return ret;
65 }
66
67 EXAPI int isf_control_get_active_ise (char **uuid)
68 {
69     if (uuid == NULL)
70         return -1;
71
72     String strUuid;
73     IMControlClient imcontrol_client;
74     int ret = 0;
75
76     if (!imcontrol_client.open_connection ())
77         return -1;
78
79     imcontrol_client.prepare ();
80     if (!imcontrol_client.get_active_ise (strUuid)) {
81         ret = -1;
82     }
83
84     imcontrol_client.close_connection ();
85
86     if (ret == -1)
87         return -1;
88
89     *uuid = strUuid.length () ? strdup (strUuid.c_str ()) : strdup ("");
90
91     return strUuid.length ();
92 }
93
94 EXAPI int isf_control_get_ise_list (char ***uuid_list)
95 {
96     if (uuid_list == NULL)
97         return -1;
98
99     int count;
100     IMControlClient imcontrol_client;
101
102     if (!imcontrol_client.open_connection ())
103         return -1;
104
105     imcontrol_client.prepare ();
106     if (!imcontrol_client.get_ise_list (&count, uuid_list))
107         count = -1;
108
109     imcontrol_client.close_connection ();
110
111     return count;
112 }
113
114 EXAPI int isf_control_get_ise_info (const char *uuid, char **name, char **language, ISE_TYPE_T *type, int *option)
115 {
116     return isf_control_get_ise_info_and_module_name (uuid, name, language, type, option, NULL);
117 }
118
119 EXAPI int isf_control_get_ise_info_and_module_name (const char *uuid, char **name, char **language, ISE_TYPE_T *type, int *option, char **module_name)
120 {
121     if (uuid == NULL)
122         return -1;
123
124     int nType   = 0;
125     int nOption = 0;
126     int ret = 0;
127     String strName, strLanguage, strModuleName;
128
129     IMControlClient imcontrol_client;
130     if (!imcontrol_client.open_connection ())
131         return -1;
132
133     imcontrol_client.prepare ();
134     if (!imcontrol_client.get_ise_info (uuid, strName, strLanguage, nType, nOption, strModuleName)) {
135         ret = -1;
136     }
137
138     imcontrol_client.close_connection ();
139
140     if (ret == -1)
141         return ret;
142
143     if (name != NULL)
144         *name = strName.length () ? strdup (strName.c_str ()) : strdup ("");
145
146     if (language != NULL)
147         *language = strLanguage.length () ? strdup (strLanguage.c_str ()) : strdup ("");
148
149     if (type != NULL)
150         *type = (ISE_TYPE_T)nType;
151
152     if (option != NULL)
153         *option = nOption;
154
155     if (module_name != NULL)
156         *module_name = strModuleName.length () ? strdup (strModuleName.c_str ()) : strdup ("");
157
158     return ret;
159 }
160
161 EXAPI int isf_control_set_active_ise_to_default (void)
162 {
163     IMControlClient imcontrol_client;
164     int ret = 0;
165
166     if (!imcontrol_client.open_connection ())
167         return -1;
168
169     imcontrol_client.prepare ();
170     imcontrol_client.set_active_ise_to_default ();
171     if (!imcontrol_client.send ())
172         ret = -1;
173
174     imcontrol_client.close_connection ();
175
176     return ret;
177 }
178
179 EXAPI int isf_control_reset_ise_option (void)
180 {
181     IMControlClient imcontrol_client;
182     int ret = 0;
183     if (!imcontrol_client.open_connection ())
184         return -1;
185
186     imcontrol_client.prepare ();
187     if (!imcontrol_client.reset_ise_option ())
188         ret = -1;
189
190     imcontrol_client.close_connection ();
191
192     return ret;
193 }
194
195 EXAPI int isf_control_set_initial_ise_by_uuid (const char *uuid)
196 {
197     if (uuid == NULL)
198         return -1;
199
200     int ret = 0;
201     IMControlClient imcontrol_client;
202     if (!imcontrol_client.open_connection ())
203         return -1;
204
205     imcontrol_client.prepare ();
206     if (!imcontrol_client.set_initial_ise_by_uuid (uuid))
207         ret = -1;
208
209     imcontrol_client.close_connection ();
210
211     return ret;
212 }
213
214 EXAPI int isf_control_get_initial_ise (char **uuid)
215 {
216     if (uuid == NULL)
217         return -1;
218
219     String strUuid = scim_global_config_read (String (SCIM_GLOBAL_CONFIG_INITIAL_ISE_UUID), String (SCIM_COMPOSE_KEY_FACTORY_UUID));
220
221     *uuid = strUuid.length () ? strdup (strUuid.c_str ()) : strdup ("");
222
223     return strUuid.length ();
224 }
225
226 EXAPI int isf_control_show_ise_selector (void)
227 {
228     return isf_control_show_ime_selector();
229 }
230
231 EXAPI int isf_control_get_ise_count (ISE_TYPE_T type)
232 {
233     char **iselist = NULL;
234     int all_ise_count, ise_count = 0;
235     ISE_TYPE_T isetype;
236
237     all_ise_count = isf_control_get_ise_list (&iselist);
238     if (all_ise_count < 0) {
239         if (iselist)
240             free (iselist);
241         return -1;
242     }
243
244     for (int i = 0; i < all_ise_count; i++) {
245         if (iselist[i]) {
246             isf_control_get_ise_info (iselist[i], NULL, NULL, &isetype, NULL);
247             if (isetype == type) {
248                 ise_count++;
249             }
250
251             free (iselist[i]);
252         }
253     }
254
255     if (iselist)
256         free (iselist);
257
258     return ise_count;
259 }
260
261 EXAPI int isf_control_show_ise_option_window (void)
262 {
263     IMControlClient imcontrol_client;
264     int ret = 0;
265     if (!imcontrol_client.open_connection ())
266         return -1;
267
268     imcontrol_client.prepare ();
269     imcontrol_client.show_ise_option_window ();
270     if (!imcontrol_client.send ())
271         ret = -1;
272
273     imcontrol_client.close_connection ();
274
275     return ret;
276 }
277
278 EXAPI int isf_control_get_all_ime_info (ime_info_s **info)
279 {
280     int count = -1, i = 0;
281     int ret = 0;
282     HELPER_ISE_INFO helper_info;
283     ime_info_s *ime_info = NULL;
284
285     if (info == NULL)
286         return count;
287
288     IMControlClient imcontrol_client;
289     if (!imcontrol_client.open_connection ())
290         return -1;
291
292     imcontrol_client.prepare ();
293     if (!imcontrol_client.get_all_helper_ise_info (helper_info))
294         ret = -1;
295
296     imcontrol_client.close_connection ();
297
298     if (ret == -1)
299         return -1;
300
301     count = static_cast<int>(helper_info.label.size());
302     if (count > 0) {
303         ime_info = (ime_info_s *)calloc (count, sizeof (ime_info_s));
304         if (ime_info) {
305             for (i = 0; i < count; i++) {
306                 snprintf(ime_info[i].appid, sizeof (ime_info[i].appid), "%s", helper_info.appid[i].c_str());
307                 snprintf(ime_info[i].label, sizeof (ime_info[i].label), "%s", helper_info.label[i].c_str());
308                 ime_info[i].is_enabled = static_cast<bool>(helper_info.is_enabled[i]);
309                 ime_info[i].is_preinstalled = static_cast<bool>(helper_info.is_preinstalled[i]);
310                 ime_info[i].has_option = static_cast<int>(helper_info.has_option[i]);
311             }
312             *info = ime_info;
313         }
314         else {
315             count = -1;
316         }
317     }
318
319     return count;
320 }
321
322 EXAPI int isf_control_open_ime_option_window (void)
323 {
324     return isf_control_show_ise_option_window ();
325 }
326
327 EXAPI int isf_control_get_active_ime (char **appid)
328 {
329     if (appid == NULL)
330         return -1;
331
332     String strUuid;
333     int ret = 0;
334     IMControlClient imcontrol_client;
335     if (!imcontrol_client.open_connection ()) {
336         LOGW ("open_connection failed");
337         return -1;
338     }
339
340     imcontrol_client.prepare ();
341     if (!imcontrol_client.get_active_ise (strUuid)) {
342         LOGW ("get_active_ise failed");
343         ret = -1;
344     }
345
346     imcontrol_client.close_connection ();
347
348     if (ret == -1)
349         return -1;
350
351     *appid = strUuid.length () ? strdup (strUuid.c_str ()) : NULL;
352
353     if (*appid == NULL) {
354         LOGW ("appid is invalid");
355         return -1;
356     }
357     else
358         return strUuid.length ();
359 }
360
361 EXAPI int isf_control_set_active_ime (const char *appid)
362 {
363     return isf_control_set_active_ise_by_uuid(appid);
364 }
365
366 EXAPI int isf_control_set_enable_ime (const char *appid, bool is_enabled)
367 {
368     if (!appid)
369         return -1;
370
371     IMControlClient imcontrol_client;
372     int ret = 0;
373
374     if (!imcontrol_client.open_connection ())
375         return -1;
376
377     imcontrol_client.prepare ();
378
379     if (!imcontrol_client.set_enable_helper_ise_info (appid, is_enabled))
380         ret = -1;
381
382     imcontrol_client.close_connection ();
383
384     return ret;
385 }
386
387 EXAPI int isf_control_show_ime_list (void)
388 {
389     IMControlClient imcontrol_client;
390     int ret = 0;
391
392     if (!imcontrol_client.open_connection ())
393         return -1;
394
395     imcontrol_client.prepare ();
396     if (!imcontrol_client.show_helper_ise_list ()) {
397         LOGW ("show_helper_ise_list failed");
398         ret = -1;
399     }
400
401     imcontrol_client.close_connection ();
402
403     return ret;
404 }
405
406 EXAPI int isf_control_show_ime_selector (void)
407 {
408     IMControlClient imcontrol_client;
409     int ret = 0;
410
411     if (!imcontrol_client.open_connection ())
412         return -1;
413
414     imcontrol_client.prepare ();
415     if (!imcontrol_client.show_helper_ise_selector ()) {
416         LOGW ("show_helper_ise_selector failed");
417         ret = -1;
418     }
419
420     imcontrol_client.close_connection ();
421
422     return ret;
423 }
424
425 EXAPI int isf_control_is_ime_enabled (const char *appid, bool *enabled)
426 {
427     if (!appid || !enabled || strlen(appid) < 1) {
428         LOGW ("Invalid parameter");
429         return -1;
430     }
431
432     int nEnabled = -1;
433     int ret = 0;
434
435     IMControlClient imcontrol_client;
436     if (!imcontrol_client.open_connection ()) {
437         LOGW ("open_connection failed");
438         return -1;
439     }
440
441     imcontrol_client.prepare ();
442     if (!imcontrol_client.is_helper_ise_enabled (appid, nEnabled)) {
443         LOGW ("is_helper_ise_enabled failed");
444         ret = -1;
445     }
446
447     imcontrol_client.close_connection ();
448
449     if (ret == -1)
450         return -1;
451
452     if (nEnabled < 0) {
453         LOGW ("Failed; appid(%s), nEnabled=%d", appid, nEnabled);
454         return -1;
455     }
456     else
457         *enabled = static_cast<bool>(nEnabled);
458
459     return ret;
460 }
461
462 EXAPI int isf_control_get_recent_ime_geometry (int *x, int *y, int *w, int *h)
463 {
464     return isf_control_get_recent_ime_geometry_with_rotation_angle (-1, x, y, w, h);
465 }
466
467 EXAPI int isf_control_get_recent_ime_geometry_with_rotation_angle (int angle, int * x, int * y, int * w, int * h)
468 {
469     int ime_x = -1, ime_y = -1, ime_w = -1, ime_h = -1;
470
471     IMControlClient imcontrol_client;
472     int ret = 0;
473
474     if ((angle != -1) && (angle % 90 != 0))
475         return -1;
476
477 #ifdef WAYLAND
478     char *geometry = NULL;
479     char **arr = NULL;
480     switch(angle) {
481         case 90:
482         case 270:
483             geometry = vconf_get_str(VCONFKEY_ISF_IME_RECENT_LAND_GEOMETRY);
484             break;
485         default:
486             geometry = vconf_get_str(VCONFKEY_ISF_IME_RECENT_PORT_GEOMETRY);
487             break;
488     }
489
490     arr = eina_str_split(geometry, ",", 0);
491     if (arr) {
492         if (arr[0])
493             ime_x = atoi(arr[0]);
494
495         if (arr[1])
496             ime_y = atoi(arr[1]);
497
498         if (arr[2])
499             ime_w = atoi(arr[2]);
500
501         if (arr[3])
502             ime_h = atoi(arr[3]);
503
504         free(arr[0]);
505         free(arr);
506     }
507     else {
508         ret = -1;
509     }
510
511     if (ime_x < 0 && ime_y < 0 && ime_w < 0 && ime_h < 0) {
512         ret = -1;
513     }
514
515     if (geometry)
516         free(geometry);
517 #else
518     if (!imcontrol_client.open_connection ())
519         return -1;
520
521     imcontrol_client.prepare ();
522     if (!imcontrol_client.get_recent_ime_geometry (&ime_x, &ime_y, &ime_w, &ime_h, angle))
523         ret = -1;
524
525     imcontrol_client.close_connection ();
526
527     if (ret == -1)
528         return ret;
529 #endif
530
531     if (ret != -1) {
532         if (x)
533             *x = ime_x;
534
535         if (y)
536             *y = ime_y;
537
538         if (w)
539             *w = ime_w;
540
541         if (h)
542             *h = ime_h;
543     }
544
545     return ret;
546 }
547
548 EXAPI int isf_control_hide_ime (void)
549 {
550     IMControlClient imcontrol_client;
551     int ret = 0;
552
553     if (!imcontrol_client.open_connection ())
554         return -1;
555
556     imcontrol_client.prepare ();
557     if (!imcontrol_client.hide_helper_ise ()) {
558         LOGW ("hide_helper_ise failed");
559         ret = -1;
560     }
561
562     imcontrol_client.close_connection ();
563
564     return ret;
565 }
566
567
568 /*
569 vi:ts=4:nowrap:ai:expandtab
570 */