Added interface for restoring ISE option window
[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_resume_ime_option_window (void)
328 {
329     IMControlClient imcontrol_client;
330     int ret = 0;
331     if (!imcontrol_client.open_connection ())
332         return -1;
333
334     imcontrol_client.prepare ();
335     imcontrol_client.resume_ise_option_window ();
336     if (!imcontrol_client.send ())
337         ret = -1;
338
339     imcontrol_client.close_connection ();
340
341     return ret;
342 }
343
344 EXAPI int isf_control_get_active_ime (char **appid)
345 {
346     if (appid == NULL)
347         return -1;
348
349     String strUuid;
350     int ret = 0;
351     IMControlClient imcontrol_client;
352     if (!imcontrol_client.open_connection ()) {
353         LOGW ("open_connection failed");
354         return -1;
355     }
356
357     imcontrol_client.prepare ();
358     if (!imcontrol_client.get_active_ise (strUuid)) {
359         LOGW ("get_active_ise failed");
360         ret = -1;
361     }
362
363     imcontrol_client.close_connection ();
364
365     if (ret == -1)
366         return -1;
367
368     *appid = strUuid.length () ? strdup (strUuid.c_str ()) : NULL;
369
370     if (*appid == NULL) {
371         LOGW ("appid is invalid");
372         return -1;
373     }
374     else
375         return strUuid.length ();
376 }
377
378 EXAPI int isf_control_set_active_ime (const char *appid)
379 {
380     return isf_control_set_active_ise_by_uuid(appid);
381 }
382
383 EXAPI int isf_control_set_enable_ime (const char *appid, bool is_enabled)
384 {
385     if (!appid)
386         return -1;
387
388     IMControlClient imcontrol_client;
389     int ret = 0;
390
391     if (!imcontrol_client.open_connection ())
392         return -1;
393
394     imcontrol_client.prepare ();
395
396     if (!imcontrol_client.set_enable_helper_ise_info (appid, is_enabled))
397         ret = -1;
398
399     imcontrol_client.close_connection ();
400
401     return ret;
402 }
403
404 EXAPI int isf_control_show_ime_list (void)
405 {
406     IMControlClient imcontrol_client;
407     int ret = 0;
408
409     if (!imcontrol_client.open_connection ())
410         return -1;
411
412     imcontrol_client.prepare ();
413     if (!imcontrol_client.show_helper_ise_list ()) {
414         LOGW ("show_helper_ise_list failed");
415         ret = -1;
416     }
417
418     imcontrol_client.close_connection ();
419
420     return ret;
421 }
422
423 EXAPI int isf_control_show_ime_selector (void)
424 {
425     IMControlClient imcontrol_client;
426     int ret = 0;
427
428     if (!imcontrol_client.open_connection ())
429         return -1;
430
431     imcontrol_client.prepare ();
432     if (!imcontrol_client.show_helper_ise_selector ()) {
433         LOGW ("show_helper_ise_selector failed");
434         ret = -1;
435     }
436
437     imcontrol_client.close_connection ();
438
439     return ret;
440 }
441
442 EXAPI int isf_control_is_ime_enabled (const char *appid, bool *enabled)
443 {
444     if (!appid || !enabled || strlen(appid) < 1) {
445         LOGW ("Invalid parameter");
446         return -1;
447     }
448
449     int nEnabled = -1;
450     int ret = 0;
451
452     IMControlClient imcontrol_client;
453     if (!imcontrol_client.open_connection ()) {
454         LOGW ("open_connection failed");
455         return -1;
456     }
457
458     imcontrol_client.prepare ();
459     if (!imcontrol_client.is_helper_ise_enabled (appid, nEnabled)) {
460         LOGW ("is_helper_ise_enabled failed");
461         ret = -1;
462     }
463
464     imcontrol_client.close_connection ();
465
466     if (ret == -1)
467         return -1;
468
469     if (nEnabled < 0) {
470         LOGW ("Failed; appid(%s), nEnabled=%d", appid, nEnabled);
471         return -1;
472     }
473     else
474         *enabled = static_cast<bool>(nEnabled);
475
476     return ret;
477 }
478
479 EXAPI int isf_control_get_recent_ime_geometry (int *x, int *y, int *w, int *h)
480 {
481     return isf_control_get_recent_ime_geometry_with_rotation_angle (-1, x, y, w, h);
482 }
483
484 EXAPI int isf_control_get_recent_ime_geometry_with_rotation_angle (int angle, int * x, int * y, int * w, int * h)
485 {
486     int ime_x = -1, ime_y = -1, ime_w = -1, ime_h = -1;
487
488     IMControlClient imcontrol_client;
489     int ret = 0;
490
491     if ((angle != -1) && (angle % 90 != 0))
492         return -1;
493
494 #ifdef WAYLAND
495     char *geometry = NULL;
496     char **arr = NULL;
497     switch(angle) {
498         case 90:
499         case 270:
500             geometry = vconf_get_str(VCONFKEY_ISF_IME_RECENT_LAND_GEOMETRY);
501             break;
502         default:
503             geometry = vconf_get_str(VCONFKEY_ISF_IME_RECENT_PORT_GEOMETRY);
504             break;
505     }
506
507     arr = eina_str_split(geometry, ",", 0);
508     if (arr) {
509         if (arr[0])
510             ime_x = atoi(arr[0]);
511
512         if (arr[1])
513             ime_y = atoi(arr[1]);
514
515         if (arr[2])
516             ime_w = atoi(arr[2]);
517
518         if (arr[3])
519             ime_h = atoi(arr[3]);
520
521         free(arr[0]);
522         free(arr);
523     }
524     else {
525         ret = -1;
526     }
527
528     if (ime_x < 0 && ime_y < 0 && ime_w < 0 && ime_h < 0) {
529         ret = -1;
530     }
531
532     if (geometry)
533         free(geometry);
534 #else
535     if (!imcontrol_client.open_connection ())
536         return -1;
537
538     imcontrol_client.prepare ();
539     if (!imcontrol_client.get_recent_ime_geometry (&ime_x, &ime_y, &ime_w, &ime_h, angle))
540         ret = -1;
541
542     imcontrol_client.close_connection ();
543
544     if (ret == -1)
545         return ret;
546 #endif
547
548     if (ret != -1) {
549         if (x)
550             *x = ime_x;
551
552         if (y)
553             *y = ime_y;
554
555         if (w)
556             *w = ime_w;
557
558         if (h)
559             *h = ime_h;
560     }
561
562     return ret;
563 }
564
565 EXAPI int isf_control_hide_ime (void)
566 {
567     IMControlClient imcontrol_client;
568     int ret = 0;
569
570     if (!imcontrol_client.open_connection ())
571         return -1;
572
573     imcontrol_client.prepare ();
574     if (!imcontrol_client.hide_helper_ise ()) {
575         LOGW ("hide_helper_ise failed");
576         ret = -1;
577     }
578
579     imcontrol_client.close_connection ();
580
581     return ret;
582 }
583
584
585 /*
586 vi:ts=4:nowrap:ai:expandtab
587 */