File: im_ibus.c

package info (click to toggle)
mlterm 3.9.4-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 25,340 kB
  • sloc: ansic: 154,713; sh: 5,302; cpp: 2,953; objc: 2,776; java: 2,472; makefile: 2,445; perl: 1,674; xml: 44
file content (1040 lines) | stat: -rw-r--r-- 24,857 bytes parent folder | download
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
/* -*- c-basic-offset:2; tab-width:2; indent-tabs-mode:nil -*- */

#include <stdio.h>
#include <ibus.h>

#include <pobl/bl_slist.h>
#include <pobl/bl_debug.h>
#include <pobl/bl_mem.h>

#include <ui_im.h>

#include "../im_common.h"
#include "../im_info.h"

#if 0
#define IM_IBUS_DEBUG 1
#endif

#define IBUS_ID -2

#if IBUS_CHECK_VERSION(1, 5, 0)
#define ibus_input_context_is_enabled(context) (TRUE)
#define ibus_input_context_disable(context) (0)
#define ibus_input_context_enable(context) (0)
#endif

#if defined(USE_FRAMEBUFFER) || defined(USE_CONSOLE)
#define USE_IM_CANDIDATE_SCREEN
#define NO_XKB
#elif defined(USE_SDL2)
#define NO_XKB
#endif

#ifdef USE_WAYLAND
#define KeyRelease 3
#define ibus_input_context_set_cursor_location(context, x, y, w, h) \
        ibus_input_context_set_cursor_location_relative(context, x, y, w, h)
#endif

typedef struct im_ibus {
  /* input method common object */
  ui_im_t im;

  IBusInputContext *context;

  vt_char_encoding_t term_encoding;

#ifdef USE_IM_CANDIDATE_SCREEN
  ef_parser_t *parser_term; /* for term encoding */
#endif
  ef_conv_t *conv; /* for term encoding */

  /*
   * Cache a result of ibus_input_context_is_enabled() which uses
   * DBus connection internally.
   */
  gboolean is_enabled;

  XKeyEvent prev_key;

#ifdef USE_IM_CANDIDATE_SCREEN
  gchar *prev_first_cand;
  u_int prev_num_cands;
#endif

  struct im_ibus *next;

} im_ibus_t;

/* --- static variables --- */

static IBusBus *ibus_bus;
static int ibus_bus_fd = -1;
static im_ibus_t *ibus_list = NULL;
static int ref_count = 0;
static ef_parser_t *parser_utf8 = NULL;
static ui_im_export_syms_t *syms = NULL; /* mlterm internal symbols */
#ifdef DEBUG_MODKEY
static int mod_key_debug = 0;
#endif

/* --- static functions --- */

#if 0
static vt_color_t get_near_color(u_int rgb) {
  u_int rgb_bit = 0;

  if ((rgb & 0xff0000) > 0x7f0000) {
    rgb_bit |= 0x4;
  }
  if ((rgb & 0xff00) > 0x7f00) {
    rgb_bit |= 0x2;
  }
  if ((rgb & 0xff) > 0x7f) {
    rgb_bit |= 0x1;
  }

  switch (rgb_bit) {
    case 0:
      return VT_BLACK;
    case 1:
      return VT_BLUE;
    case 2:
      return VT_GREEN;
    case 3:
      return VT_CYAN;
    case 4:
      return VT_RED;
    case 5:
      return VT_MAGENTA;
    case 6:
      return VT_YELLOW;
    case 7:
      return VT_WHITE;
    default:
      return VT_BLACK;
  }
}
#endif

static void update_preedit_text(IBusInputContext *context, IBusText *text, gint cursor_pos,
                                gboolean visible, gpointer data) {
  im_ibus_t *ibus;
  vt_char_t *p;
  u_int len;
  ef_char_t ch;

  ibus = (im_ibus_t*)data;

  if ((len = ibus_text_get_length(text)) > 0) {
    u_int index;

    if (ibus->im.preedit.filled_len == 0) {
      /* Start preediting. */
      int x;
      int y;

      if ((*ibus->im.listener->get_spot)(ibus->im.listener->self, NULL, 0, &x, &y)) {
        u_int line_height;

        line_height = (*ibus->im.listener->get_line_height)(ibus->im.listener->self);
        ibus_input_context_set_cursor_location(ibus->context, x, y - line_height, 0, line_height);
      }
    }

    if ((p = realloc(ibus->im.preedit.chars, sizeof(vt_char_t) * len)) == NULL) {
      return;
    }

    (*syms->vt_str_init)(ibus->im.preedit.chars = p, ibus->im.preedit.num_chars = len);
    ibus->im.preedit.filled_len = 0;

    (*parser_utf8->init)(parser_utf8);
    (*parser_utf8->set_str)(parser_utf8, (u_char*)text->text, strlen(text->text));

    index = 0;
    while ((*parser_utf8->next_char)(parser_utf8, &ch)) {
      u_int count;
      IBusAttribute *attr;
      int is_fullwidth = 0;
      int is_comb = 0;
      /* LS_UNDERLINE_SINGLE is always used. */
#if 0
      int is_underlined = 0;
#endif
      vt_color_t fg_color = VT_FG_COLOR;
      vt_color_t bg_color = VT_BG_COLOR;

      for (count = 0; (attr = ibus_attr_list_get(text->attrs, count)); count++) {
        if (attr->start_index <= index && index < attr->end_index) {
#if 0
          if (attr->type == IBUS_ATTR_TYPE_UNDERLINE) {
            is_underlined = (attr->value != IBUS_ATTR_UNDERLINE_NONE);
          } else
#endif
#if 0
          if (attr->type == IBUS_ATTR_TYPE_FOREGROUND) {
            fg_color = get_near_color(attr->value);
          } else if (attr->type == IBUS_ATTR_TYPE_BACKGROUND) {
            bg_color = get_near_color(attr->value);
          }
#else
          if (attr->type == IBUS_ATTR_TYPE_BACKGROUND) {
            fg_color = VT_BG_COLOR;
            bg_color = VT_FG_COLOR;
          }
#endif
        }
      }

      if ((*syms->vt_convert_to_internal_ch)(ibus->im.vtparser, &ch) <= 0) {
        continue;
      }

      if (ch.property & EF_FULLWIDTH) {
        is_fullwidth = 1;
      } else if (ch.property & EF_AWIDTH) {
        /* TODO: check col_size_of_width_a */
        is_fullwidth = 1;
      }

      if (ch.property & EF_COMBINING) {
        is_comb = 1;

        if ((*syms->vt_char_combine)(p - 1, ef_char_to_int(&ch), ch.cs, is_fullwidth,
                                     (ch.property & EF_AWIDTH) ? 1 : 0, is_comb,
                                     fg_color, bg_color, 0, 0, LS_UNDERLINE_SINGLE, 0, 0)) {
          continue;
        }

        /*
         * if combining failed , char is normally appended.
         */
      }

      (*syms->vt_char_set)(p, ef_char_to_int(&ch), ch.cs, is_fullwidth,
                           (ch.property & EF_AWIDTH) ? 1 : 0, is_comb, fg_color,
                           bg_color, 0, 0, LS_UNDERLINE_SINGLE, 0, 0);

      p++;
      ibus->im.preedit.filled_len++;

      index++;
    }
  } else {
#ifdef USE_IM_CANDIDATE_SCREEN
    if (ibus->im.cand_screen) {
      (*ibus->im.cand_screen->destroy)(ibus->im.cand_screen);
      ibus->im.cand_screen = NULL;
    }
#endif

    if (ibus->im.preedit.filled_len == 0) {
      return;
    }

    /* Stop preediting. */
    ibus->im.preedit.filled_len = 0;
  }

  ibus->im.preedit.cursor_offset = cursor_pos;

  (*ibus->im.listener->draw_preedit_str)(ibus->im.listener->self, ibus->im.preedit.chars,
                                         ibus->im.preedit.filled_len,
                                         ibus->im.preedit.cursor_offset);
}

static void hide_preedit_text(IBusInputContext *context, gpointer data) {
  im_ibus_t *ibus;

  ibus = (im_ibus_t*)data;

  if (ibus->im.preedit.filled_len == 0) {
    return;
  }

#ifdef USE_IM_CANDIDATE_SCREEN
  if (ibus->im.cand_screen) {
    (*ibus->im.cand_screen->hide)(ibus->im.cand_screen);
  }
#endif

  /* Stop preediting. */
  ibus->im.preedit.filled_len = 0;
  ibus->im.preedit.cursor_offset = 0;

  (*ibus->im.listener->draw_preedit_str)(ibus->im.listener->self, ibus->im.preedit.chars,
                                         ibus->im.preedit.filled_len,
                                         ibus->im.preedit.cursor_offset);
}

static void commit_text(IBusInputContext *context, IBusText *text, gpointer data) {
  im_ibus_t *ibus;

  ibus = (im_ibus_t*)data;

  if (ibus->im.preedit.filled_len > 0) {
    /* Reset preedit */
    ibus->im.preedit.filled_len = 0;
    ibus->im.preedit.cursor_offset = 0;
    (*ibus->im.listener->draw_preedit_str)(ibus->im.listener->self, ibus->im.preedit.chars,
                                           ibus->im.preedit.filled_len,
                                           ibus->im.preedit.cursor_offset);
  }

  if (ibus_text_get_length(text) == 0) {
    /* do nothing */
  } else {
    (*ibus->im.listener->write_to_term)(ibus->im.listener->self, (u_char*)text->text,
                                        strlen(text->text),
                                        ibus->term_encoding == VT_UTF8 ? NULL : parser_utf8);
  }

#ifdef USE_IM_CANDIDATE_SCREEN
  if (ibus->im.cand_screen) {
    (*ibus->im.cand_screen->destroy)(ibus->im.cand_screen);
    ibus->im.cand_screen = NULL;
  }
#endif
}

static void forward_key_event(IBusInputContext *context, guint keyval, guint keycode, guint state,
                              gpointer data) {
  im_ibus_t *ibus;

  ibus = (im_ibus_t*)data;

  if (ibus->prev_key.keycode ==
#ifdef NO_XKB
      keycode
#else
      keycode + 8
#endif
      ) {
    ibus->prev_key.state |= IBUS_IGNORED_MASK;
#ifdef USE_XLIB
    XPutBackEvent(ibus->prev_key.display, (XEvent*)&ibus->prev_key);
#endif
    memset(&ibus->prev_key, 0, sizeof(XKeyEvent));
  }
}

#ifdef USE_IM_CANDIDATE_SCREEN

static void show_lookup_table(IBusInputContext *context, gpointer data) {
  im_ibus_t *ibus;

  ibus = (im_ibus_t*)data;

  if (ibus->im.cand_screen) {
    (*ibus->im.cand_screen->show)(ibus->im.cand_screen);
  }
}

static void hide_lookup_table(IBusInputContext *context, gpointer data) {
  im_ibus_t *ibus;

  ibus = (im_ibus_t*)data;

  if (ibus->im.cand_screen) {
    (*ibus->im.cand_screen->hide)(ibus->im.cand_screen);
  }
}

static void update_lookup_table(IBusInputContext *context, IBusLookupTable *table, gboolean visible,
                                gpointer data) {
  im_ibus_t *ibus;
  u_int num_cands;
  int cur_pos;
  u_int i;
  int x;
  int y;

  ibus = (im_ibus_t*)data;

  if ((num_cands = ibus_lookup_table_get_number_of_candidates(table)) == 0) {
    return;
  }

  cur_pos = ibus_lookup_table_get_cursor_pos(table);

  (*ibus->im.listener->get_spot)(ibus->im.listener->self, ibus->im.preedit.chars,
                                 ibus->im.preedit.segment_offset, &x, &y);

  if (ibus->im.cand_screen == NULL) {
    if (cur_pos == 0) {
      return;
    }

    if (!(ibus->im.cand_screen = (*syms->ui_im_candidate_screen_new)(
              ibus->im.disp, ibus->im.font_man, ibus->im.color_man, ibus->im.vtparser,
              (*ibus->im.listener->is_vertical)(ibus->im.listener->self), 1,
              (*ibus->im.listener->get_line_height)(ibus->im.listener->self), x, y))) {
#ifdef DEBUG
      bl_warn_printf(BL_DEBUG_TAG " ui_im_candidate_screen_new() failed.\n");
#endif

      return;
    }
  } else {
    (*ibus->im.cand_screen->show)(ibus->im.cand_screen);
  }

  if (!(*ibus->im.cand_screen->init)(ibus->im.cand_screen, num_cands, 10)) {
    (*ibus->im.cand_screen->destroy)(ibus->im.cand_screen);
    ibus->im.cand_screen = NULL;

    return;
  }

  (*ibus->im.cand_screen->set_spot)(ibus->im.cand_screen, x, y);

  for (i = 0; i < num_cands; i++) {
    const u_char *str;

    /* ibus 1.4.1 on Ubuntu 12.10 can return NULL if num_cands > 0. */
    if (!(str = ibus_text_get_text(ibus_lookup_table_get_candidate(table, i)))) {
      continue;
    }

    if (ibus->term_encoding != VT_UTF8) {
      u_char *p;

      if (im_convert_encoding(parser_utf8, ibus->conv, str, &p, strlen(str) + 1)) {
        (*ibus->im.cand_screen->set)(ibus->im.cand_screen, ibus->parser_term, p, i);
        free(p);
      }
    } else {
      (*ibus->im.cand_screen->set)(ibus->im.cand_screen, ibus->parser_term, str, i);
    }
  }

  (*ibus->im.cand_screen->select)(ibus->im.cand_screen, cur_pos);
}

#endif

static void connection_handler(void) {
#ifdef DBUS_H
  DBusConnection *connection;

  connection = ibus_connection_get_connection(ibus_bus_get_connection(ibus_bus));

  dbus_connection_read_write(connection, 0);

  while (dbus_connection_dispatch(connection) == DBUS_DISPATCH_DATA_REMAINS)
    ;
#else
#if 0
  g_dbus_connection_flush_sync(ibus_bus_get_connection(ibus_bus), NULL, NULL);
#endif
  g_main_context_iteration(g_main_context_default(), FALSE);
#endif
}

static int add_event_source(void) {
#ifdef DBUS_H
  if (!dbus_connection_get_unix_fd(
          ibus_connection_get_connection(ibus_bus_get_connection(ibus_bus)), &ibus_bus_fd)) {
    return 0;
  }
#else
  /*
   * GIOStream returned by g_dbus_connection_get_stream() is forcibly
   * regarded as GSocketConnection.
   */
  GIOStream *stream = g_dbus_connection_get_stream(ibus_bus_get_connection(ibus_bus));
  if ((ibus_bus_fd = g_socket_get_fd(g_socket_connection_get_socket(
                                       G_SOCKET_CONNECTION(stream)))) == -1) {
    return 0;
  }
#endif
  (*syms->ui_event_source_add_fd)(ibus_bus_fd, connection_handler);
  (*syms->ui_event_source_add_fd)(IBUS_ID, connection_handler);

  return 1;
}

static void remove_event_source(int complete) {
  if (ibus_bus_fd >= 0) {
    (*syms->ui_event_source_remove_fd)(ibus_bus_fd);
    ibus_bus_fd = -1;
  }

  if (complete) {
    (*syms->ui_event_source_remove_fd)(IBUS_ID);
  }
}

/*
 * methods of ui_im_t
 */

static void destroy(ui_im_t *im) {
  im_ibus_t *ibus;

  ibus = (im_ibus_t*)im;

  if (ibus->context) {
#ifdef DBUS_H
    ibus_object_destroy((IBusObject*)ibus->context);
#else
    ibus_proxy_destroy((IBusProxy*)ibus->context);
#endif
  }

  bl_slist_remove(ibus_list, ibus);

  if (ibus->conv) {
    (*ibus->conv->destroy)(ibus->conv);
  }

#ifdef USE_IM_CANDIDATE_SCREEN
  if (ibus->parser_term) {
    (*ibus->parser_term->destroy)(ibus->parser_term);
  }

  free(ibus->prev_first_cand);
#endif

  free(ibus);

  if (--ref_count == 0) {
    remove_event_source(1);

    ibus_object_destroy((IBusObject*)ibus_bus);
    ibus_bus = NULL;

    if (parser_utf8) {
      (*parser_utf8->destroy)(parser_utf8);
      parser_utf8 = NULL;
    }
  }

#ifdef IM_IBUS_DEBUG
  bl_debug_printf(BL_DEBUG_TAG " An object was destroyed. ref_count: %d\n", ref_count);
#endif
}

#ifdef NO_XKB
static KeySym native_to_ibus_ksym(KeySym ksym) {
  switch (ksym) {
    case XK_BackSpace:
      return IBUS_BackSpace;

    case XK_Tab:
      return IBUS_Tab;

    case XK_Return:
      return IBUS_Return;

    case XK_Escape:
      return IBUS_Escape;

    case XK_Zenkaku_Hankaku:
      return IBUS_Zenkaku_Hankaku;

    case XK_Hiragana_Katakana:
      return IBUS_Hiragana_Katakana;

    case XK_Muhenkan:
      return IBUS_Muhenkan;

    case XK_Henkan_Mode:
      return IBUS_Henkan_Mode;

    case XK_Home:
      return IBUS_Home;

    case XK_Left:
      return IBUS_Left;

    case XK_Up:
      return IBUS_Up;

    case XK_Right:
      return IBUS_Right;

    case XK_Down:
      return IBUS_Down;

    case XK_Prior:
      return IBUS_Prior;

    case XK_Next:
      return IBUS_Next;

    case XK_Insert:
      return IBUS_Insert;

    case XK_End:
      return IBUS_End;

    case XK_Num_Lock:
      return IBUS_Num_Lock;

    case XK_Shift_L:
      return IBUS_Shift_L;

    case XK_Shift_R:
      return IBUS_Shift_R;

    case XK_Control_L:
      return IBUS_Control_L;

    case XK_Control_R:
      return IBUS_Control_R;

    case XK_Caps_Lock:
      return IBUS_Caps_Lock;

#if 0
    case XK_Meta_L:
      return IBUS_Super_L; /* Windows key on linux */

    case XK_Meta_R:
      return IBUS_Super_R; /* Menu key on linux */
#else
    case XK_Meta_L:
      return IBUS_Meta_L;

    case XK_Meta_R:
      return IBUS_Meta_R;
#endif

    case XK_Alt_L:
      return IBUS_Alt_L;

    case XK_Alt_R:
      return IBUS_Alt_R;

    case XK_Delete:
      return IBUS_Delete;

    case XK_Super_L:
      return IBUS_Super_L;

    case XK_Super_R:
      return IBUS_Super_R;

    default:
      break;
  }

  return ksym;
}
#else
#define native_to_ibus_ksym(ksym) (ksym)
#endif

static int key_event(ui_im_t *im, u_char key_char, KeySym ksym, XKeyEvent *event) {
  im_ibus_t *ibus;

  ibus = (im_ibus_t*)im;

  if (!ibus->context) {
    return 1;
  }

  if (event->state & IBUS_IGNORED_MASK) {
    /* Is put back in forward_key_event */
    event->state &= ~IBUS_IGNORED_MASK;
  } else if (ibus_input_context_process_key_event(ibus->context, native_to_ibus_ksym(ksym),
#ifdef NO_XKB
                                                  event->keycode, event->state
#else
                                                  event->keycode - 8,
                                                  event->state |
                                                      (event->type == KeyRelease ? IBUS_RELEASE_MASK
                                                                                 : 0)
#endif
                                                  )) {
    gboolean is_enabled_old;

    is_enabled_old = ibus->is_enabled;
    ibus->is_enabled = ibus_input_context_is_enabled(ibus->context);

#if !IBUS_CHECK_VERSION(1, 5, 0)
    if (ibus->is_enabled != is_enabled_old) {
      return 0;
    } else
#endif
        if (ibus->is_enabled) {
#ifndef DBUS_H
#if 0
      g_dbus_connection_flush_sync(ibus_bus_get_connection(ibus_bus), NULL, NULL);
#endif
      g_main_context_iteration(g_main_context_default(), FALSE);
#endif

      memcpy(&ibus->prev_key, event, sizeof(XKeyEvent));

      return 0;
    }
  } else if (ibus->im.preedit.filled_len > 0) {
/* Pressing "q" in preediting. */
#ifndef DBUS_H
#if 0
    g_dbus_connection_flush_sync(ibus_bus_get_connection(ibus_bus), NULL, NULL);
#endif
    g_main_context_iteration(g_main_context_default(), FALSE);
#endif
  }

  return 1;
}

static void set_engine(IBusInputContext *context, gchar *name) {
#ifdef DEBUG
  bl_msg_printf("iBus engine %s -> %s\n",
                ibus_engine_desc_get_name(ibus_input_context_get_engine(context)), name);
#endif
  ibus_input_context_set_engine(context, name);
}

#if IBUS_CHECK_VERSION(1, 5, 0)
static void next_engine(IBusInputContext *context) {
  IBusConfig *config;
  GVariant *var;

  if ((config = ibus_bus_get_config(ibus_bus)) &&
      (var = ibus_config_get_value(config, "general", "preload-engines"))) {
    static int show_engines = 1;
    const gchar *cur_name;
    GVariantIter *iter;
    gchar *name;

    cur_name = ibus_engine_desc_get_name(ibus_input_context_get_engine(context));

    g_variant_get(var, "as", &iter);

    if (show_engines) {
      bl_msg_printf("iBus engines: ");
      while (g_variant_iter_loop(iter, "s", &name)) {
        bl_msg_printf(name);
        bl_msg_printf(",");
      }
      bl_msg_printf("\n");

      g_variant_iter_init(iter, var);
      show_engines = 0;
    }

    if (g_variant_iter_loop(iter, "s", &name)) {
      gchar *first_name;
      int loop;

      first_name = g_strdup(name);
      loop = 1;

      do {
        if (strcmp(cur_name, name) == 0) {
          loop = 0;
        }

        if (!g_variant_iter_loop(iter, "s", &name)) {
          loop = 0;
          name = first_name;
        }
      } while (loop);

      set_engine(context, name);

      free(first_name);
    }

    g_variant_iter_free(iter);
    g_variant_unref(var);
  }
}
#endif

static int switch_mode(ui_im_t *im) {
  im_ibus_t *ibus;

  ibus = (im_ibus_t*)im;

  if (!ibus->context) {
    return 0;
  }

#if IBUS_CHECK_VERSION(1, 5, 0)
  next_engine(ibus->context);
  ibus->is_enabled = !ibus->is_enabled;
#else
  if (ibus->is_enabled) {
    ibus_input_context_disable(ibus->context);
    ibus->is_enabled = FALSE;
  } else {
    ibus_input_context_enable(ibus->context);
    ibus->is_enabled = TRUE;
  }
#endif

  return 1;
}

static int is_active(ui_im_t *im) { return ((im_ibus_t*)im)->is_enabled; }

static void focused(ui_im_t *im) {
  im_ibus_t *ibus;

  ibus = (im_ibus_t*)im;

  if (!ibus->context) {
    return;
  }

  ibus_input_context_focus_in(ibus->context);

  if (ibus->im.cand_screen) {
    (*ibus->im.cand_screen->show)(ibus->im.cand_screen);
  }
}

static void unfocused(ui_im_t *im) {
  im_ibus_t *ibus;

  ibus = (im_ibus_t*)im;

  if (!ibus->context) {
    return;
  }

  ibus_input_context_focus_out(ibus->context);

  if (ibus->im.cand_screen) {
    (*ibus->im.cand_screen->hide)(ibus->im.cand_screen);
  }
}

static IBusInputContext *context_new(im_ibus_t *ibus, char *engine) {
  IBusInputContext *context;

  if (!(context = ibus_bus_create_input_context(ibus_bus, "mlterm"))) {
    return NULL;
  }

  ibus_input_context_set_capabilities(context,
                                      IBUS_CAP_PREEDIT_TEXT | IBUS_CAP_FOCUS
#ifdef USE_IM_CANDIDATE_SCREEN
                                      | IBUS_CAP_LOOKUP_TABLE
#endif
#if 0
                                      | IBUS_CAP_SURROUNDING_TEXT
#endif
                                      );

  g_signal_connect(context, "update-preedit-text", G_CALLBACK(update_preedit_text), ibus);
  g_signal_connect(context, "hide-preedit-text", G_CALLBACK(hide_preedit_text), ibus);
  g_signal_connect(context, "commit-text", G_CALLBACK(commit_text), ibus);
  g_signal_connect(context, "forward-key-event", G_CALLBACK(forward_key_event), ibus);
#ifdef USE_IM_CANDIDATE_SCREEN
  g_signal_connect(context, "update-lookup-table", G_CALLBACK(update_lookup_table), ibus);
  g_signal_connect(context, "show-lookup-table", G_CALLBACK(show_lookup_table), ibus);
  g_signal_connect(context, "hide-lookup-table", G_CALLBACK(hide_lookup_table), ibus);
#endif

  if (engine) {
    set_engine(context, engine);
  }
#if (defined(USE_FRAMEBUFFER) || defined(USE_CONSOLE)) && IBUS_CHECK_VERSION(1, 5, 0)
  else /* if (strcmp(ibus_engine_desc_get_name(ibus_input_context_get_engine(context)),
                     "dummy") == 0) */ {
    /* Input method engine might not be changed from "dummy" without X server. */
    next_engine(context);
  }
#endif

#ifdef DEBUG
  bl_debug_printf("Engine is %s in context_new()\n",
                  ibus_engine_desc_get_name(ibus_input_context_get_engine(context)));
#endif

  return context;
}

static void connected(IBusBus *bus, gpointer data) {
  im_ibus_t *ibus;

  if (bus != ibus_bus || !ibus_bus_is_connected(ibus_bus) || !add_event_source()) {
    return;
  }

  for (ibus = ibus_list; ibus; ibus = bl_slist_next(ibus_list)) {
    ibus->context = context_new(ibus, NULL);
  }
}

static void disconnected(IBusBus *bus, gpointer data) {
  im_ibus_t *ibus;

  if (bus != ibus_bus) {
    return;
  }

  remove_event_source(0);

  for (ibus = ibus_list; ibus; ibus = bl_slist_next(ibus_list)) {
#ifdef DBUS_H
    ibus_object_destroy((IBusObject*)ibus->context);
#else
    ibus_proxy_destroy((IBusProxy*)ibus->context);
#endif
    ibus->context = NULL;
    ibus->is_enabled = FALSE;
  }
}

/* --- global functions --- */

ui_im_t *im_ibus_new(u_int64_t magic, vt_char_encoding_t term_encoding,
                     ui_im_export_syms_t *export_syms, char *engine,
                     u_int mod_ignore_mask /* Not used for now. */
                     ) {
  static int is_init;
  im_ibus_t *ibus = NULL;

  if (magic != (u_int64_t)IM_API_COMPAT_CHECK_MAGIC) {
    bl_error_printf("Incompatible input method API.\n");

    return NULL;
  }

#ifdef DEBUG_MODKEY
  if (getenv("MOD_KEY_DEBUG")) {
    mod_key_debug = 1;
  }
#endif

  if (!is_init) {
    ibus_init();

    /* Don't call ibus_init() again if ibus daemon is not found below. */
    is_init = 1;
  }

  if (!ibus_bus) {
    syms = export_syms;

/* g_getenv( "DISPLAY") will be called in ibus_get_socket_path(). */
#if 0
    ibus_set_display(g_getenv("DISPLAY"));
#endif

    ibus_bus = ibus_bus_new();

    if (!ibus_bus_is_connected(ibus_bus)) {
      bl_error_printf("IBus daemon is not found.\n");

      goto error;
    }

    if (!add_event_source()) {
      goto error;
    }

    if (!(parser_utf8 = (*syms->vt_char_encoding_parser_new)(VT_UTF8))) {
      goto error;
    }

    g_signal_connect(ibus_bus, "connected", G_CALLBACK(connected), NULL);
    g_signal_connect(ibus_bus, "disconnected", G_CALLBACK(disconnected), NULL);
  }

  if (!(ibus = calloc(1, sizeof(im_ibus_t)))) {
#ifdef DEBUG
    bl_warn_printf(BL_DEBUG_TAG " malloc failed.\n");
#endif

    goto error;
  }

  if (term_encoding != VT_UTF8) {
    if (!(ibus->conv = (*syms->vt_char_encoding_conv_new)(term_encoding))) {
      goto error;
    }
  }

#ifdef USE_IM_CANDIDATE_SCREEN
  if (!(ibus->parser_term = (*syms->vt_char_encoding_parser_new)(term_encoding))) {
    goto error;
  }
#endif

  ibus->term_encoding = term_encoding;

  if (!(ibus->context = context_new(ibus, engine))) {
    goto error;
  }

  ibus->is_enabled = FALSE;

  /*
   * set methods of ui_im_t
   */
  ibus->im.destroy = destroy;
  ibus->im.key_event = key_event;
  ibus->im.switch_mode = switch_mode;
  ibus->im.is_active = is_active;
  ibus->im.focused = focused;
  ibus->im.unfocused = unfocused;

  bl_slist_insert_head(ibus_list, ibus);

  ref_count++;

#ifdef IM_IBUS_DEBUG
  bl_debug_printf("New object was created. ref_count is %d.\n", ref_count);
#endif

  return (ui_im_t*)ibus;

error:
  if (ref_count == 0) {
    remove_event_source(1);

    ibus_object_destroy((IBusObject*)ibus_bus);
    ibus_bus = NULL;

    if (parser_utf8) {
      (*parser_utf8->destroy)(parser_utf8);
      parser_utf8 = NULL;
    }
  }

  if (ibus) {
    if (ibus->conv) {
      (*ibus->conv->destroy)(ibus->conv);
    }

#ifdef USE_IM_CANDIDATE_SCREEN
    if (ibus->parser_term) {
      (*ibus->parser_term->destroy)(ibus->parser_term);
    }
#endif

    free(ibus);
  }

  return NULL;
}

/* --- module entry point for external tools --- */

im_info_t *im_ibus_get_info(char *locale, char *encoding) {
  im_info_t *result;

  if (!(result = malloc(sizeof(im_info_t)))) {
    return NULL;
  }

  result->id = strdup("ibus");
  result->name = strdup("iBus");
  result->num_args = 0;
  result->args = NULL;
  result->readable_args = NULL;

  return result;
}