1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
|
/*
* Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the License);
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an AS IS BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <dlog.h>
#include <inputmethod.h>
#include <inputmethod_internal.h>
#include <pkgmgr-info.h>
#include "ise-dbus.h"
#ifdef LOG_TAG
#undef LOG_TAG
#endif
#define LOG_TAG "ISE_DEFAULT"
#define WAIT_FOR_SYNC_RESPONSE_TIMEOUT 0.5
static bool is_server_started = false;
static engine_loader_dbus_info_s *dbus_info = NULL;
static ISELanguageManager _language_manager;
static bool _engine_loader_event_done = false;
static void _server_appeared_cb(GDBusConnection *connection, const gchar *name, const gchar *name_owner, gpointer user_data)
{
LOGD("name : %s, name_owner : %s", name, name_owner);
}
static void _server_vanished_cb(GDBusConnection *connection, const gchar *name, gpointer user_data)
{
LOGD("name : %s", name);
}
static bool _dbus_init()
{
GError *error = NULL;
int watch_id = 0;
if (!dbus_info) {
LOGW("dbus info is not allocated.");
return false;
}
if (dbus_info->gdbus_connection == NULL) {
GDBusConnection *conn = NULL;
conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
if (conn == NULL) {
if (error != NULL) {
LOGE("g_bus_get_sync error message = %s", error->message);
g_error_free(error);
}
return false;
}
dbus_info->gdbus_connection = conn;
}
LOGD("Connected bus name : %s", g_dbus_connection_get_unique_name(dbus_info->gdbus_connection));
watch_id = g_bus_watch_name(G_BUS_TYPE_SYSTEM,
ENGINE_LOADER_DBUS_NAME,
G_BUS_NAME_WATCHER_FLAGS_NONE,
_server_appeared_cb,
_server_vanished_cb,
NULL, NULL);
if (watch_id == 0) {
LOGE("Failed to get identifier");
return false;
}
return true;
}
static void _handle_show_preedit_string(GVariant *parameters)
{
int ret = ime_show_preedit_string();
if (ret != IME_ERROR_NONE)
LOGE("ime_show_preedit_string() failed");
}
static void _handle_update_preedit_string(GVariant *parameters)
{
GVariantIter *attr_iter = NULL;
GVariant *iter_body;
char *preedit_string = NULL;
unsigned int start;
unsigned int length;
unsigned int type;
unsigned int value;
Eina_List *list = NULL;
ime_preedit_attribute *attr;
g_variant_get(parameters, "(a(v)&s)", &attr_iter, &preedit_string);
if (!attr_iter || !preedit_string)
LOGE("Failed to get variant");
while (g_variant_iter_loop (attr_iter, "(v)", &iter_body)) {
g_variant_get(iter_body, "(uuuu)", &start, &length, &type, &value);
attr = (ime_preedit_attribute *)calloc(1, sizeof(ime_preedit_attribute));
if (attr) {
attr->start = start;
attr->length = length;
attr->type = (ime_attribute_type)type;
attr->value = value;
list = eina_list_append(list, attr);
}
}
int ret = ime_update_preedit_string(preedit_string, list);
if (ret != IME_ERROR_NONE) {
EINA_LIST_FREE(list, attr)
free(attr);
}
}
static void _handle_hide_preedit_string(GVariant *parameters)
{
int ret = ime_hide_preedit_string();
if (ret != IME_ERROR_NONE)
LOGE("ime_hide_preedit_string() failed");
}
static void _handle_commit_string(GVariant *parameters)
{
char *commit_string = NULL;
g_variant_get(parameters, "(&s)", &commit_string);
if (!commit_string)
LOGE("Failed to get variant");
int ret = ime_commit_string(commit_string);
if (ret != IME_ERROR_NONE)
LOGE("ime_commit_string() failed");
}
static void _handle_forward_key_event(GVariant *parameters)
{
GVariantIter *key_event_iter = NULL;
GVariant *value = NULL;
gchar *key = NULL;
scim::KeyEvent key_event;
char *dev_name = NULL;
g_variant_get(parameters, "(a{sv}&s)", &key_event_iter, &dev_name);
if (!key_event_iter)
LOGD("Failed to get iter");
key_event.dev_name = std::string(dev_name);
while (g_variant_iter_loop (key_event_iter, "{sv}", &key, &value)) {
if (g_strcmp0(key, "key_code") == 0)
key_event.code = g_variant_get_uint32(value);
else if (g_strcmp0(key, "key_mask") == 0)
key_event.mask = g_variant_get_uint16(value);
else if (g_strcmp0(key, "key_layout") == 0)
key_event.layout = g_variant_get_uint16(value);
else if (g_strcmp0(key, "key_dev_class") == 0)
key_event.dev_class = g_variant_get_uint16(value);
else if (g_strcmp0(key, "key_dev_subclass") == 0)
key_event.dev_subclass = g_variant_get_uint16(value);
}
if (key)
g_free(key);
if (value)
g_variant_unref(value);
int ret = ime_send_key_event((ime_key_code_e)key_event.code, (ime_key_mask_e)key_event.mask, true);
if (ret != IME_ERROR_NONE)
LOGE("ime_send_key_event() failed");
}
static void _handle_delete_surrounding_text(GVariant *parameters)
{
int offset, len;
g_variant_get(parameters, "(ii)", &offset, &len);
int ret = ime_delete_surrounding_text(offset, len);
if (ret != IME_ERROR_NONE)
LOGE("ime_delete_surrounding_text() failed");
}
static void _handle_set_selection(GVariant *parameters)
{
int start, end;
g_variant_get(parameters, "(ii)", &start, &end);
int ret = ime_set_selection(start, end);
if (ret != IME_ERROR_NONE)
LOGE("ime_set_selection() failed");
}
static void _handle_send_private_command(GVariant *parameters)
{
char *private_command = NULL;
g_variant_get(parameters, "(&s)", &private_command);
if (!private_command)
LOGE("Failed to get variant");
int ret = ime_send_private_command(private_command);
if (ret != IME_ERROR_NONE)
LOGE("ime_send_private_command() failed");
}
static void _handle_show_aux_string(GVariant *parameters)
{
int ret = ime_show_aux_string();
if (ret != IME_ERROR_NONE)
LOGE("ime_show_aux_string() failed");
}
static void _handle_show_lookup_table(GVariant *parameters)
{
int ret = ime_show_lookup_table();
if (ret != IME_ERROR_NONE)
LOGE("ime_show_lookup_table() failed");
}
static void _handle_hide_aux_string(GVariant *parameters)
{
int ret = ime_hide_aux_string();
if (ret != IME_ERROR_NONE)
LOGE("ime_hide_aux_string() failed");
}
static void _handle_hide_lookup_table(GVariant *parameters)
{
int ret = ime_hide_lookup_table();
if (ret != IME_ERROR_NONE)
LOGE("ime_hide_lookup_table() failed");
}
static void _handle_update_preedit_caret(GVariant *parameters)
{
int caret;
g_variant_get(parameters, "(i)", &caret);
int ret = ime_update_preedit_caret(caret);
if (ret != IME_ERROR_NONE)
LOGE("ime_update_preedit_caret() failed");
}
static void _handle_update_preedit_string_with_commit(GVariant *parameters)
{
GVariantIter *attr_iter = NULL;
GVariant *iter_body;
char *preedit_string = NULL;
char *commit_string = NULL;
int caret;
unsigned int start;
unsigned int length;
unsigned int type;
unsigned int value;
scim::AttributeList attr;
g_variant_get(parameters, "(a(v)&s&si)", &attr_iter, &preedit_string, &commit_string, &caret);
if (!attr_iter || !preedit_string || !commit_string)
LOGE("Failed to get variant");
while (g_variant_iter_loop (attr_iter, "(v)", &iter_body)) {
g_variant_get(iter_body, "(uuuu)", &start, &length, &type, &value);
attr.push_back(scim::Attribute(start, length, (scim::AttributeType)type, value));
}
int ret = ime_update_preedit_string_with_commit(preedit_string, commit_string, attr, caret);
if (ret != IME_ERROR_NONE)
LOGE("ime_update_preedit_string_with_commit() failed");
}
static void _handle_update_aux_string(GVariant *parameters)
{
GVariantIter *attr_iter = NULL;
GVariant *iter_body;
char *aux_string = NULL;
unsigned int start;
unsigned int length;
unsigned int type;
unsigned int value;
scim::AttributeList attr;
g_variant_get(parameters, "(a(v)&s)", &attr_iter, &aux_string);
if (!attr_iter || !aux_string)
LOGE("Failed to get variant");
while (g_variant_iter_loop (attr_iter, "(v)", &iter_body)) {
g_variant_get(iter_body, "(uuuu)", &start, &length, &type, &value);
attr.push_back(scim::Attribute(start, length, (scim::AttributeType)type, value));
}
int ret = ime_update_aux_string(aux_string);
if (ret != IME_ERROR_NONE)
LOGE("ime_update_aux_string() failed");
}
static void _handle_recapture_string(GVariant *parameters)
{
GVariantIter *attr_iter = NULL;
GVariant *iter_body;
char *preedit_string = NULL;
char *commit_string = NULL;
int offset, len;
unsigned int start;
unsigned int length;
unsigned int type;
unsigned int value;
scim::AttributeList attr;
g_variant_get(parameters, "(a(v)&s&sii)", &attr_iter, &preedit_string, &commit_string, &offset, &len);
if (!attr_iter || !preedit_string || !commit_string)
LOGE("Failed to get variant");
while (g_variant_iter_loop (attr_iter, "(v)", &iter_body)) {
g_variant_get(iter_body, "(uuuu)", &start, &length, &type, &value);
attr.push_back(scim::Attribute(start, length, (scim::AttributeType)type, value));
}
int ret = ime_recapture_string(offset, len, preedit_string, commit_string, attr);
if (ret != IME_ERROR_NONE)
LOGE("ime_recapture_string() failed");
}
static void _handle_update_lookup_table(GVariant *parameters)
{
GVariantIter *attr_iter = NULL;
GVariantIter *candidate_iter = NULL;
GVariant *attrs_iter_body;
GVariant *candidate_iter_body;
scim::CommonLookupTable lookup_table;
scim::AttributeList attr_list;
char *candidate_text = NULL;
int idx = 0;
int page_size;
int cursor_pos;
g_variant_get(parameters, "(a(a(v))a(v)ii)", &attr_iter, &candidate_iter, &page_size, &cursor_pos);
if (!attr_iter || !candidate_iter)
LOGE("Failed to get variant");
lookup_table.set_page_size(page_size);
lookup_table.set_cursor_pos(cursor_pos);
while (g_variant_iter_loop (candidate_iter, "(v)", &candidate_iter_body)) {
g_variant_get(candidate_iter_body, "(&s)", &candidate_text);
lookup_table.append_candidate(scim::utf8_mbstowcs ((const char*)candidate_text));
}
while (g_variant_iter_loop (attr_iter, "(a(v))", &attrs_iter_body)) {
GVariantIter *innter_iter = NULL;
GVariant *iner_body;
g_variant_get(attrs_iter_body, "(a(v))", &innter_iter);
while (g_variant_iter_loop (innter_iter, "(v)", &iner_body)) {
int start, length, type, value;
g_variant_get(iner_body, "(uuuu)", &start, &length, &type, &value);
if (start == 0 && length == 0 && type == 0 && value == 0)
break;
else
attr_list.push_back(scim::Attribute(start, length, (scim::AttributeType)type, value));
}
if (!attr_list.empty())
lookup_table.add_attributes(idx++, attr_list);
attr_list.clear();
}
int ret = ime_update_lookup_table(lookup_table);
if (ret != IME_ERROR_NONE)
LOGE("ime_update_lookup_table() failed");
}
static void _handle_register_properties(GVariant *parameters)
{
GVariantIter *list_iter = NULL;
GVariant *iter_body;
scim::PropertyList property_list;
scim::Property property;
char *key = NULL;
char *label = NULL;
char *icon = NULL;
char *tip = NULL;
g_variant_get(parameters, "(a(v))", &list_iter);
if (!list_iter)
LOGE("Failed to get variant");
while (g_variant_iter_loop (list_iter, "(v)", &iter_body)) {
g_variant_get(iter_body, "(&s&s&s&s)", &key, &label, &icon, &tip);
property.set_key(scim::String(key));
property.set_label(scim::String(label));
property.set_icon(scim::String(icon));
property.set_tip(scim::String(tip));
property_list.push_back(property);
}
int ret = ime_register_properties(property_list);
if (ret != IME_ERROR_NONE)
LOGE("ime_register_properties() failed");
}
static void _handle_update_property(GVariant *parameters)
{
char *key = NULL;
char *label = NULL;
char *icon = NULL;
char *tip = NULL;
scim::Property property;
g_variant_get(parameters, "(&s&s&s&s)", &key, &label, &icon, &tip);
property.set_key(scim::String(key));
property.set_label(scim::String(label));
property.set_icon(scim::String(icon));
property.set_tip(scim::String(tip));
int ret = ime_update_property(property);
if (ret != IME_ERROR_NONE)
LOGE("ime_update_property() failed");
}
static void _handle_expand_candidate(GVariant *parameters)
{
int ret = ime_expand_candidate();
if (ret != IME_ERROR_NONE)
LOGE("ime_expand_candidate() failed");
}
static void _handle_contract_candidate(GVariant *parameters)
{
int ret = ime_contract_candidate();
if (ret != IME_ERROR_NONE)
LOGE("ime_contract_candidate() failed");
}
static void _handle_set_candidate_style(GVariant *parameters)
{
int port_line, mode;
g_variant_get(parameters, "(ii)", &port_line, &mode);
int ret = ime_set_candidate_style((scim::ISF_CANDIDATE_PORTRAIT_LINE_T)port_line, (scim::ISF_CANDIDATE_MODE_T)mode);
if (ret != IME_ERROR_NONE)
LOGE("ime_set_candidate_style() failed");
}
static void _handle_send_processing_result(GVariant *parameters)
{
scim::KeyEvent key_event;
uint32_t code = 0;
uint16_t mask = 0;
uint16_t layout = 0;
char *dev_name = NULL;
uint16_t dev_class = 0;
uint16_t dev_subclass = 0;
GVariantIter *key_event_iter = NULL;
GVariant *value = NULL;
gchar *key = NULL;
uint32_t serial = 0;
gboolean is_success;
g_variant_get(parameters, "(a{sv}&sub)", &key_event_iter, &dev_name, &serial, &is_success);
if (key_event_iter && dev_name) {
while (g_variant_iter_loop (key_event_iter, "{sv}", &key, &value)) {
if (g_strcmp0(key, "key_code") == 0)
code = g_variant_get_uint32(value);
else if (g_strcmp0(key, "key_mask") == 0)
mask = g_variant_get_uint16(value);
else if (g_strcmp0(key, "key_layout") == 0)
layout = g_variant_get_uint16(value);
else if (g_strcmp0(key, "key_dev_class") == 0)
dev_class = g_variant_get_uint16(value);
else if (g_strcmp0(key, "key_dev_subclass") == 0)
dev_subclass = g_variant_get_uint16(value);
}
if (key)
g_free(key);
if (value)
g_variant_unref(value);
key_event.code = code;
key_event.mask = mask;
key_event.layout = layout;
key_event.dev_name = std::string(dev_name);
key_event.dev_class = dev_class;
key_event.dev_subclass = dev_subclass;
}
int ret = ime_send_key_event_processing_result(key_event, serial, is_success);
if (ret != IME_ERROR_NONE)
LOGE("ime_send_key_event_processing_result() failed");
}
static void _handle_engine_loader_cb(GDBusConnection *connection,
const gchar *sender_name,
const gchar *object_path,
const gchar *interface_name,
const gchar *signal_name,
GVariant *parameters,
gpointer user_data)
{
LOGD("own_name : %s, signal_name : %s", g_dbus_connection_get_unique_name(connection), signal_name);
const sclchar *cur_lang = _language_manager.get_current_language();
if (cur_lang) {
LANGUAGE_INFO *info = _language_manager.get_language_info(cur_lang);
if (info && info->load_in_ime)
return;
}
if (g_strcmp0(signal_name, "show_preedit_string") == 0) {
_handle_show_preedit_string(parameters);
} else if (g_strcmp0(signal_name, "update_preedit_string") == 0) {
_handle_update_preedit_string(parameters);
} else if (g_strcmp0(signal_name, "hide_preedit_string") == 0) {
_handle_hide_preedit_string(parameters);
} else if (g_strcmp0(signal_name, "commit_string") == 0) {
_handle_commit_string(parameters);
} else if (g_strcmp0(signal_name, "forward_key_event") == 0) {
_handle_forward_key_event(parameters);
} else if (g_strcmp0(signal_name, "delete_surrounding_text") == 0) {
_handle_delete_surrounding_text(parameters);
} else if (g_strcmp0(signal_name, "set_selection") == 0) {
_handle_set_selection(parameters);
} else if (g_strcmp0(signal_name, "send_private_command") == 0) {
_handle_send_private_command(parameters);
} else if (g_strcmp0(signal_name, "show_aux_string") == 0) {
_handle_show_aux_string(parameters);
} else if (g_strcmp0(signal_name, "show_lookup_table") == 0) {
_handle_show_lookup_table(parameters);
} else if (g_strcmp0(signal_name, "hide_aux_string") == 0) {
_handle_hide_aux_string(parameters);
} else if (g_strcmp0(signal_name, "hide_lookup_table") == 0) {
_handle_hide_lookup_table(parameters);
} else if (g_strcmp0(signal_name, "update_preedit_caret") == 0) {
_handle_update_preedit_caret(parameters);
} else if (g_strcmp0(signal_name, "update_preedit_string_with_commit") == 0) {
_handle_update_preedit_string_with_commit(parameters);
} else if (g_strcmp0(signal_name, "update_aux_string") == 0) {
_handle_update_aux_string(parameters);
} else if (g_strcmp0(signal_name, "recapture_string") == 0) {
_handle_recapture_string(parameters);
} else if (g_strcmp0(signal_name, "update_lookup_table") == 0) {
_handle_update_lookup_table(parameters);
} else if (g_strcmp0(signal_name, "register_properties") == 0) {
_handle_register_properties(parameters);
} else if (g_strcmp0(signal_name, "update_property") == 0) {
_handle_update_property(parameters);
} else if (g_strcmp0(signal_name, "expand_candidate") == 0) {
_handle_expand_candidate(parameters);
} else if (g_strcmp0(signal_name, "contract_candidate") == 0) {
_handle_contract_candidate(parameters);
} else if (g_strcmp0(signal_name, "set_candidate_style") == 0) {
_handle_set_candidate_style(parameters);
} else if (g_strcmp0(signal_name, "send_processing_result") == 0) {
_handle_send_processing_result(parameters);
} else if (g_strcmp0(signal_name, "engine_loader_event_done") == 0) {
_engine_loader_event_done = true;
}
}
static bool _dbus_signal_init()
{
if (dbus_info && dbus_info->monitor_id == 0) {
int id = g_dbus_connection_signal_subscribe(dbus_info->gdbus_connection,
ENGINE_LOADER_DBUS_NAME,
ENGINE_LOADER_ISE_INTERFACE_NAME,
NULL,
ENGINE_LOADER_OBJECT_PATH,
NULL,
G_DBUS_SIGNAL_FLAGS_NONE,
_handle_engine_loader_cb,
dbus_info->user_data,
NULL);
if (id == 0) {
LOGE("g_dbus_connection_signal_subscribe() failed");
return false;
} else
dbus_info->monitor_id = id;
}
return true;
}
static GDBusMessage *_get_gdbus_message(GVariant *body, const char *cmd)
{
GDBusMessage *message = NULL;
message = g_dbus_message_new_method_call(
ENGINE_LOADER_DBUS_NAME,
ENGINE_LOADER_OBJECT_PATH,
ENGINE_LOADER_INTERFACE_NAME,
cmd);
if (!message) {
LOGE("Failed to create a new gdbus message");
if (body)
g_variant_unref(body);
return NULL;
}
if (body != NULL)
g_dbus_message_set_body(message, body);
return message;
}
static bool _send_gdbus_sync_message(GDBusConnection *gdbus_connection, GDBusMessage *msg, GDBusMessage **reply, const char *cmd)
{
GError *err = NULL;
*reply = g_dbus_connection_send_message_with_reply_sync(
gdbus_connection,
msg,
G_DBUS_SEND_MESSAGE_FLAGS_NONE,
-1,
NULL,
NULL,
&err);
if (!*reply) {
if (err != NULL) {
LOGE("Error occurred when sending message(%s) : %s", cmd, err->message);
g_error_free(err);
}
return false;
}
if (g_dbus_message_to_gerror(*reply, &err)) {
LOGE("error message = %s, code = %d", err->message, err->code);
g_error_free(err);
return false;
}
LOGD("Send a message to server(cmd : %s)", cmd);
return true;
}
static bool _send_sync_message(GDBusConnection *gdbus_connection, GVariant *body, GDBusMessage **reply, const char *cmd)
{
bool ret = false;
GDBusMessage *msg = NULL;
msg = _get_gdbus_message(body, cmd);
if (msg == NULL)
return false;
ret = _send_gdbus_sync_message(gdbus_connection, msg, reply, cmd);
if (msg)
g_object_unref(msg);
return ret;
}
static bool _send_async_message(GDBusConnection *gdbus_connection, GVariant *body, const char *cmd)
{
GDBusMessage *msg = NULL;
GError *err = NULL;
msg = _get_gdbus_message(body, cmd);
if (msg == NULL)
return false;
g_dbus_connection_send_message(gdbus_connection, msg, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, &err);
if (msg)
g_object_unref(msg);
if (err != NULL) {
LOGE("Error occurred when sending message(%s) : %s", cmd, err->message);
g_error_free(err);
return false;
}
return true;
}
static bool _monitor_register(GDBusConnection *gdbus_connection)
{
bool ret = false;
GDBusMessage *reply = NULL;
GVariant *reply_body = NULL;
if (!dbus_info) {
LOGW("dbus info is not allocated.");
return false;
}
ret = _send_sync_message(gdbus_connection, g_variant_new("()"), &reply, "loader_service_register");
if (!ret) {
LOGE("_send_sync_message() failed");
return false;
}
reply_body = g_dbus_message_get_body(reply);
g_variant_get(reply_body, "(i)", dbus_info->server_watcher_id);
if (reply)
g_object_unref(reply);
is_server_started = true;
return true;
}
static void _on_name_appeared(GDBusConnection *connection,
const gchar *name,
const gchar *name_owner,
gpointer user_data)
{
if (!dbus_info) {
LOGW("dbus info is not allocated.");
return;
}
if (!is_server_started) {
dbus_info->server_watcher_id = (intptr_t)user_data;
_monitor_register(connection);
}
}
static void _on_name_vanished(GDBusConnection *connection,
const gchar *name,
gpointer user_data)
{
is_server_started = false;
}
bool engine_loader_dbus_init(void *data)
{
bool ret;
dbus_info = (engine_loader_dbus_info_s*)calloc(1, sizeof(engine_loader_dbus_info_s));
if (dbus_info == NULL) {
LOGE("Failed to allocate memory");
return false;
}
dbus_info->user_data = data;
ret = _dbus_init();
if (!ret) {
LOGE("_dbus_init() failed : %d", ret);
goto cleanup;
}
ret = _dbus_signal_init();
if (!ret) {
LOGE("_dbus_signal_init() failed : %d", ret);
goto cleanup;
}
ret = _monitor_register(dbus_info->gdbus_connection);
if (!ret) {
LOGE("_monitor_register() failed : %d", ret);
goto cleanup;
}
if (dbus_info->server_monitor_id == 0) {
dbus_info->server_monitor_id = g_bus_watch_name_on_connection(
dbus_info->gdbus_connection,
ENGINE_LOADER_DBUS_NAME,
G_BUS_NAME_WATCHER_FLAGS_NONE,
_on_name_appeared,
_on_name_vanished,
(void *)((intptr_t)dbus_info->server_watcher_id),
NULL);
if (dbus_info->server_monitor_id == 0) {
g_dbus_connection_signal_unsubscribe(dbus_info->gdbus_connection, dbus_info->monitor_id);
dbus_info->monitor_id = 0;
LOGE("Failed to get identifier");
ret = false;
goto cleanup;
}
}
return true;
cleanup:
if (dbus_info->gdbus_connection)
g_object_unref(dbus_info->gdbus_connection);
free(dbus_info);
dbus_info = NULL;
return ret;
}
bool engine_loader_dbus_shutdown()
{
bool ret = false;
if (dbus_info) {
if (dbus_info->server_watcher_id) {
ret = _send_async_message(dbus_info->gdbus_connection, g_variant_new("(i)", dbus_info->server_watcher_id), "loader_service_unregister");
if (!ret) {
LOGE("Failed to unregister client");
return ret;
}
}
if (dbus_info->server_monitor_id)
g_bus_unwatch_name(dbus_info->server_monitor_id);
if (dbus_info->monitor_id)
g_dbus_connection_signal_unsubscribe(dbus_info->gdbus_connection, dbus_info->monitor_id);
free(dbus_info);
}
dbus_info = NULL;
return true;
}
void engine_loader_set_imengine(const char *engine_id, const char *module_name)
{
GDBusMessage *reply = NULL;
GVariant *reply_body = NULL;
gboolean result = false;
LOGD("engine id : %s, module name : %s", engine_id, module_name);
if (!dbus_info) {
LOGW("dbus info is not allocated.");
return;
}
if (_send_sync_message(dbus_info->gdbus_connection, g_variant_new("(ss)", engine_id, module_name), &reply, "set_imengine")) {
reply_body = g_dbus_message_get_body(reply);
g_variant_get(reply_body, "(b)", &result);
}
if (!result)
LOGE("Failed to set imengine");
if (reply_body)
g_variant_unref(reply_body);
}
void engine_loader_flush_imengine()
{
GDBusMessage *reply = NULL;
_engine_loader_event_done = false;
if (!dbus_info || !_send_sync_message(dbus_info->gdbus_connection, g_variant_new("()"), &reply, "flush_imengine"))
LOGE("Failed to flush imengine");
double start_time = ecore_time_get();
while (!_engine_loader_event_done && (ecore_time_get() - start_time < WAIT_FOR_SYNC_RESPONSE_TIMEOUT))
ecore_main_loop_iterate();
}
void engine_loader_reset_imengine()
{
GDBusMessage *reply = NULL;
_engine_loader_event_done = false;
if (!dbus_info || !_send_sync_message(dbus_info->gdbus_connection, g_variant_new("()"), &reply, "reset_imengine"))
LOGE("Failed to reset imengine");
double start_time = ecore_time_get();
while (!_engine_loader_event_done && (ecore_time_get() - start_time < WAIT_FOR_SYNC_RESPONSE_TIMEOUT))
ecore_main_loop_iterate();
}
void engine_loader_send_imengine_event(int command, uint32_t value)
{
if (!dbus_info || !_send_async_message(dbus_info->gdbus_connection, g_variant_new("(iu)", command, value), "send_imengine_event"))
LOGE("Failed to send imengine event");
}
void engine_loader_process_key_event(scim::KeyEvent& key, uint32_t serial, bool need_surrounding_text)
{
gboolean result = false;
GDBusMessage *reply = NULL;
GVariant *reply_body = NULL;
GVariantBuilder *key_event_builder = NULL;
if (!dbus_info) {
LOGE("dbus info is not allocated.");
return;
}
if (need_surrounding_text) {
char *text = NULL;
int cursor;
ime_get_surrounding_text(-1, -1, &text, &cursor);
SECURE_LOGD("surrounding text : %s, cursor : %d\n", text, cursor);
if (_send_sync_message(dbus_info->gdbus_connection, g_variant_new("(si)", text, cursor), &reply, "set_surrounding_text")) {
reply_body = g_dbus_message_get_body(reply);
g_variant_get(reply_body, "(b)", &result);
}
if (!result)
LOGE("Failed to set surrounding_text");
if (reply_body) {
g_variant_unref(reply_body);
reply_body = NULL;
}
if (text)
free(text);
}
key_event_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
g_variant_builder_add(key_event_builder, "{sv}", "key_code", g_variant_new_uint32(key.code));
g_variant_builder_add(key_event_builder, "{sv}", "key_mask", g_variant_new_uint16(key.mask));
g_variant_builder_add(key_event_builder, "{sv}", "key_layout", g_variant_new_uint16(key.layout));
g_variant_builder_add(key_event_builder, "{sv}", "key_dev_class", g_variant_new_uint16(key.dev_class));
g_variant_builder_add(key_event_builder, "{sv}", "key_dev_subclass", g_variant_new_uint16(key.dev_subclass));
if (!_send_async_message(dbus_info->gdbus_connection, g_variant_new("(a{sv}su)", key_event_builder, key.dev_name.c_str(), serial), "process_key_event"))
LOGE("Failed to send process key event");
g_variant_builder_unref(key_event_builder);
}
void engine_loader_focus_in()
{
if (!dbus_info || !_send_async_message(dbus_info->gdbus_connection, g_variant_new("()"), "focus_in"))
LOGE("Failed to send focus_in event");
}
void engine_loader_focus_out()
{
if (!dbus_info || !_send_async_message(dbus_info->gdbus_connection, g_variant_new("()"), "focus_out"))
LOGE("Failed to send focus_out event");
}
void engine_loader_update_cursor_position(int cursor_pos)
{
if (!dbus_info || !_send_async_message(dbus_info->gdbus_connection, g_variant_new("(i)", cursor_pos), "update_cursor_position"))
LOGE("Failed to update cursor position");
}
void engine_loader_set_autocapital_type(uint32_t type)
{
if (!dbus_info || !_send_async_message(dbus_info->gdbus_connection, g_variant_new("(u)", type), "set_autocapital_type"))
LOGE("Failed to send autocapital type");
}
void engine_loader_set_prediction_allow(uint32_t prediction_allow)
{
if (!dbus_info || !_send_async_message(dbus_info->gdbus_connection, g_variant_new("(u)", prediction_allow), "set_prediction_allow"))
LOGE("Failed to send prediction allow");
}
void engine_loader_reset_input_context()
{
if (!dbus_info || !_send_async_message(dbus_info->gdbus_connection, g_variant_new("()"), "reset_input_context"))
LOGE("Failed to reset input context");
}
void engine_loader_set_layout(uint32_t layout)
{
if (!dbus_info || !_send_async_message(dbus_info->gdbus_connection, g_variant_new("(u)", layout), "set_layout"))
LOGE("Failed to set layout");
}
void engine_loader_set_imdata(const char *imdata, uint32_t len)
{
if (!dbus_info || !_send_async_message(dbus_info->gdbus_connection, g_variant_new("(su)", imdata, len), "set_imdata"))
LOGE("Failed to set imdata");
}
void engine_loader_set_input_hint(uint32_t input_hint)
{
if (!dbus_info || !_send_async_message(dbus_info->gdbus_connection, g_variant_new("(u)", input_hint), "set_input_hint"))
LOGE("Failed to set input hint");
}
void engine_loader_update_bidi_direction(uint32_t direction)
{
if (!dbus_info || !_send_async_message(dbus_info->gdbus_connection, g_variant_new("(u)", direction), "update_bidi_direction"))
LOGE("Failed to update bidi direction");
}
void engine_loader_trigger_property(const char *property)
{
if (!dbus_info || !_send_async_message(dbus_info->gdbus_connection, g_variant_new("(s)", property), "trigger_property"))
LOGE("Failed to send trigger property");
}
void engine_loader_show_candidate_more_window()
{
if (!dbus_info || !_send_async_message(dbus_info->gdbus_connection, g_variant_new("()"), "show_candidate_more_window"))
LOGE("Failed to show candidate more window");
}
void engine_loader_hide_candidate_more_window()
{
if (!dbus_info || !_send_async_message(dbus_info->gdbus_connection, g_variant_new("()"), "hide_candidate_more_window"))
LOGE("Failed to hide candidate more window");
}
void engine_loader_select_aux(uint32_t item)
{
if (!dbus_info || !_send_async_message(dbus_info->gdbus_connection, g_variant_new("(u)", item), "select_aux"))
LOGE("Failed to select aux");
}
void engine_loader_select_candidate(uint32_t item)
{
if (!dbus_info || !_send_async_message(dbus_info->gdbus_connection, g_variant_new("(u)", item), "select_candidate"))
LOGE("Failed to select candidate");
}
void engine_loader_candidate_table_page_up()
{
if (!dbus_info || !_send_async_message(dbus_info->gdbus_connection, g_variant_new("()"), "candidate_table_page_up"))
LOGE("Failed to page up the candidate table");
}
void engine_loader_candidate_table_page_down()
{
if (!dbus_info || !_send_async_message(dbus_info->gdbus_connection, g_variant_new("()"), "candidate_table_page_down"))
LOGE("Failed to page down the candidate table");
}
void engine_loader_change_candidate_page_size(uint32_t size)
{
if (!dbus_info || !_send_async_message(dbus_info->gdbus_connection, g_variant_new("(u)", size), "change_candidate_page_size"))
LOGE("Failed to change candidate page size");
}
void engine_loader_set_candidate_item_layout(std::vector<uint32_t> item)
{
gboolean result = false;
GDBusMessage *reply = NULL;
GVariant *reply_body = NULL;
GVariantBuilder *item_builder = NULL;
GVariant *body = NULL;
std::vector<uint32_t>::iterator it;
if (!dbus_info) {
LOGW("dbus info is not allocated.");
return;
}
item_builder = g_variant_builder_new(G_VARIANT_TYPE("a(v)"));
for (it = item.begin(); it != item.end(); ++it) {
body = g_variant_new("(u)", *it);
g_variant_builder_add(item_builder, "(v)", body);
}
if (_send_sync_message(dbus_info->gdbus_connection, g_variant_new("(a(v))", item_builder), &reply, "set_candidate_item_layout")) {
reply_body = g_dbus_message_get_body(reply);
g_variant_get(reply_body, "(b)", &result);
}
if (!result)
LOGE("Failed to set candidate item layout");
g_variant_builder_unref(item_builder);
if (reply_body)
g_variant_unref(reply_body);
}
void engine_loader_change_candidate_number(uint32_t page_num)
{
if (!dbus_info || !_send_async_message(dbus_info->gdbus_connection, g_variant_new("(u)", page_num), "change_candidate_number"))
LOGE("Failed to change candidate number");
}
void engine_loader_long_press_candidate_item(uint32_t index)
{
if (!dbus_info || !_send_async_message(dbus_info->gdbus_connection, g_variant_new("(u)", index), "long_press_candidate_item"))
LOGE("Failed to send long pressed item");
}
|