File: inmethod.c

package info (click to toggle)
tgif 1%3A4.2.5-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 29,784 kB
  • sloc: ansic: 186,444; sh: 10,884; perl: 2,956; awk: 487; makefile: 101
file content (842 lines) | stat: -rw-r--r-- 25,237 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
/*
 * Author:      William Chia-Wei Cheng (bill.cheng@acm.org)
 *
 * Copyright (C) 2001-2009, William Chia-Wei Cheng.
 *
 * This file may be distributed under the terms of the Q Public License
 * as defined by Trolltech AS of Norway and appearing in the file
 * LICENSE.QPL included in the packaging of this file.
 *
 * THIS FILE IS PROVIDED AS IS WITH NO WARRANTY OF ANY KIND, INCLUDING
 * THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL,
 * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
 * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 * @(#)$Header: /mm2/home/cvs/bc-src/tgif/inmethod.c,v 1.14 2011/06/14 02:32:18 william Exp $
 */

#define _INCLUDE_FROM_INMETHOD_C_

#include "tgifdefs.h"

#ifndef _NO_XCIN
#include "cli_xcin.e"
#endif /* ~_NO_XCIN */
#ifndef _NO_CHINPUT
#include "chinput.e"
#endif /* ~_NO_CHINPUT */
#ifndef _NO_KINPUT
#include "convkinput.e"
#endif /* ~_NO_KINPUT */
#ifndef _NO_XIM
#include "convxim.e"
#endif /* ~_NO_XIM */
#ifndef _NO_TGTWB5
#include "tgtwb5.e"
#endif /* ~_NO_TGTWB5 */

#include "dialog.e"
#include "inmethod.e"
#include "msg.e"
#include "util.e"
#include "setup.e"
#include "strtbl.e"
#include "tidget.e"

#ifndef _NO_DL_SUPPORT
#define CLOSE_DL(handle) dlclose(handle)
#define OPEN_DL(path,flag) dlopen((path),(flag))
#define GET_DL_SYM(handle,symbol) dlsym((handle),(symbol))
#ifndef RTLD_NOW
#define OPEN_DL_MODE 1
#else /* RTLD_NOW */
#define OPEN_DL_MODE (RTLD_NOW|RTLD_GLOBAL)
#endif /* ~RTLD_NOW */
#else /* _NO_DL_SUPPORT */
#define CLOSE_DL(handle)
#define OPEN_DL(path,flag) NULL
#define GET_DL_SYM(handle,symbol) NULL
#define OPEN_DL_MODE 0
#endif /* ~_NO_DL_SUPPORT */

int gnInputMethod=TGIM_NONE; /* one of TGIM_* */
int gnOverTheSpot=FALSE;

int cmdLineHasInputMethod=FALSE;
int cmdLineDoubleByteInputMethod=FALSE;
char cmdLineInputMethod[MAXSTRING];

static int gnInputMethodIndex=INVALID;
static int gnSingleOrDoubleByteInputMethod=0;

typedef void (CleanUpFunc)ARGS_DECL((Display *dpy, Window win));
typedef int (InitFunc)ARGS_DECL((Display *dpy, Window win, char *arg));
typedef int (FocusInFunc)ARGS_DECL((Display *dpy, Window win));
typedef int (FocusOutFunc)ARGS_DECL((Display *dpy, Window win));
typedef int (SendKeyFunc)ARGS_DECL((Display *dpy, Window win,
                                    XKeyEvent *key_ev, char *buf));
typedef int (ConvPropertyFunc)ARGS_DECL((Display *dpy, Window win,
                                         XPropertyEvent *prop_ev,
                                         char **ppsz_buf));
typedef int (ExpectCMFunc)ARGS_DECL((Display *dpy, Window win));
typedef int (HandleCMFunc)ARGS_DECL((Display *dpy, Window win,
                                     XClientMessageEvent *cm_ev,
                                     XKeyEvent *key_ev, char *buf));
typedef int (ActivateOnCntrlSpaceFunc)ARGS_DECL((Display *dpy, Window win));
typedef int (HandleCntrlSpaceFunc)ARGS_DECL((Display *dpy, Window win));
typedef int (DeactivateOnCreateTextFunc)ARGS_DECL((Display *dpy, Window win));
typedef int (HandleCreateTextFunc)ARGS_DECL((Display *dpy, Window win));
typedef int (TellCursorPosFunc)ARGS_DECL((Display *dpy, Window win,
                                          int cur_x, int cur_y));
typedef int (ExpectNextEventFunc)ARGS_DECL((Display *dpy, Window win));
typedef int (HandleNextEventFunc)ARGS_DECL((Display *dpy, Window win,
                                            XEvent *ev));
typedef int (HandleNewCurTextFunc)ARGS_DECL((Display *dpy, Window win));
typedef int (ExpectLookupStringFunc)ARGS_DECL((Display *dpy, Window win));
typedef int (HandleLookupStringFunc)ARGS_DECL((Display *dpy, Window win,
                                               XKeyEvent *key_ev, char *psz_buf,
                                               int buf_size, KeySym *p_key_sym,
                                               int *pn_has_ch));

typedef struct tagIMInfo {
   char *pszName;
   int nInputMethod;
   int nSingleDouble;
   CleanUpFunc *pCleanUpFunc;
   InitFunc *pInitFunc;
   FocusInFunc *pFocusInFunc;
   FocusOutFunc *pFocusOutFunc;
   SendKeyFunc *pSendKeyFunc;
   ConvPropertyFunc *pConvPropertyFunc;
   ExpectCMFunc *pExpectCMFunc;
   HandleCMFunc *pHandleCMFunc;
   ActivateOnCntrlSpaceFunc *pActivateOnCntrlSpaceFunc;
   HandleCntrlSpaceFunc *pHandleCntrlSpaceFunc;
   DeactivateOnCreateTextFunc *pDeactivateOnCreateTextFunc;
   HandleCreateTextFunc *pHandleCreateTextFunc;
   TellCursorPosFunc *pTellCursorPosFunc;
   ExpectNextEventFunc *pExpectNextEventFunc;
   HandleNextEventFunc *pHandleNextEventFunc;
   HandleNewCurTextFunc *pHandleNewCurTextFunc;
   ExpectLookupStringFunc *pExpectLookupStringFunc;
   HandleLookupStringFunc *pHandleLookupStringFunc;
} IMInfo;

static IMInfo *gpIMInfo=NULL;

/* --------------------- xcin Routines --------------------- */

#ifndef _NO_XCIN
static int XcinInit(Display *dpy, Window win, char *arg)
{
   return TRUE;
}

static int XcinFocusIn(Display *dpy, Window win)
{
   Tg_send_FocusIn(dpy, win);
   return TRUE;
}

static int XcinFocusOut(Display *dpy, Window win)
{
   Tg_send_FocusOut(dpy, win);
   return TRUE;
}

static int XcinSendKey(Display *dpy, Window win, XKeyEvent *key_ev, char *buf)
{
   return Tg_send_key(dpy, win, key_ev, buf);
}

static int XcinExpectCM(Display *dpy, Window win)
{
   return FALSE;
}
#endif /* ~_NO_XCIN */

/* --------------------- chinput Routines --------------------- */

#ifndef _NO_CHINPUT
static int ChinputInit(Display *dpy, Window win, char *arg)
{
   return (Tg_HZclientInit(dpy)==0);
}

static int ChinputFocusIn(Display *dpy, Window win)
{
   return TRUE;
}

static int ChinputFocusOut(Display *dpy, Window win)
{
   return TRUE;
}

static int ChinputSendKey(Display *dpy, Window win, XKeyEvent *key_ev, char *buf)
{
   Tg_HZsendKey(dpy, win, key_ev);
   *buf = '\0';
   return TRUE;
}

static int ChinputExpectCM(Display *dpy, Window win)
{
   return TRUE;
}

static int ChinputHandleCM(Display *dpy, Window win, XClientMessageEvent *cm_ev, XKeyEvent *key_ev, char *buf)
{
   return Tg_HZhandleCM(cm_ev, key_ev, buf);
}
#endif /* ~_NO_CHINPUT */

/* --------------------- kinput Routines --------------------- */

#ifndef _NO_KINPUT
static void KinputCleanUp(Display *dpy, Window win)
{
   imProtocol = IM_NONE;
}

static int KinputInit(Display *dpy, Window win, char *arg)
{
   char *c_ptr=NULL;

   copyAndPasteJIS = FALSE;
   if ((c_ptr=XGetDefault(mainDisplay, TOOL_NAME, "CopyAndPasteJIS")) != NULL &&
         UtilStrICmp(c_ptr, "true") == 0) {
      copyAndPasteJIS = TRUE;
   }
   gnOverTheSpot = FALSE;
   if ((c_ptr=XGetDefault(mainDisplay, TOOL_NAME, "PreeditType")) != NULL &&
         UtilStrICmp(c_ptr, "overthespot") == 0) {
      gnOverTheSpot = TRUE;
   }
   KinputSetConvOverSpot(gnOverTheSpot);

   if ((c_ptr=XGetDefault(mainDisplay, TOOL_NAME, "ConvSelection")) != NULL) {
      UtilTrimBlanks(c_ptr);
      strncpy(kinputConvSelName, c_ptr, MAXSTRING-1);
      kinputConvSelName[MAXSTRING-1] = '\0';
   }

   imProtocol = IM_KINPUT;
   return TRUE;
}

static int KinputFocusIn(Display *dpy, Window win)
{
   return TRUE;
}

static int KinputFocusOut(Display *dpy, Window win)
{
   return TRUE;
}

static int KinputSendKey(Display *dpy, Window win, XKeyEvent *key_ev, char *buf)
{
   return FALSE;
}

static int KinputConvProperty(Display *dpy, Window win, XPropertyEvent *prop_ev, char **ppsz_buf)
{
   char *c_ptr=KinputCheckConvProperty(dpy, win, prop_ev);

   if (c_ptr != NULL && ppsz_buf != NULL) {
      *ppsz_buf = c_ptr;
      return TRUE;
   }
   return FALSE;
}

static int KinputExpectCM(Display *dpy, Window win)
{
   return (win == drawWindow ||
         (TidgetHasFocus() && win == TidgetGetFocusWindow()));
}

static int KinputHandleCM(Display *dpy, Window win, XClientMessageEvent *cm_ev, XKeyEvent *key_ev, char *buf)
{
   KinputCheckClientMessage(dpy, win, cm_ev);
   return FALSE;
}

static int KinputActiveOnCntrlSpace(Display *dpy, Window win)
{
   return (win == drawWindow ||
         (TidgetHasFocus() && win == TidgetGetFocusWindow()));
}

static int KinputHandleCntrlSpace(Display *dpy, Window win)
{
   KinputBeginConversion(dpy, win);
   return TRUE;
}

static int KinputDeactiveOnCreateText(Display *dpy, Window win)
{
   return TRUE;
}

static int KinputHandleCreateText(Display *dpy, Window win)
{
   KinputEndConversion(dpy, win);
   return TRUE;
}

static int KinputTellCursorPos(Display *dpy, Window win, int cur_x, int cur_y)
{
   KinputTellCursorPosition(dpy, win, cur_x, cur_y);
   return TRUE;
}
#endif /* ~_NO_KINPUT */

/* --------------------- xim Routines --------------------- */

#ifndef _NO_XIM
static void XimCleanUp(Display *dpy, Window win)
{
   XIMCleanUp();
}

static int XimInit(Display *dpy, Window win, char *arg)
{
   char *c_ptr=NULL;
   XIMInit();
   gnOverTheSpot = FALSE;
   if ((c_ptr=XGetDefault(mainDisplay, TOOL_NAME, "PreeditType")) != NULL &&
         UtilStrICmp(c_ptr, "overthespot") == 0) {
      gnOverTheSpot = TRUE;
   }
   XIMSetConvOverSpot(gnOverTheSpot);
   return TRUE;
}

static int XimFocusIn(Display *dpy, Window win)
{
   XIMSetICFocus(dpy, win);
   return TRUE;
}

static int XimFocusOut(Display *dpy, Window win)
{
   XIMUnsetICFocus(win);
   return TRUE;
}

static int XimDeactiveOnCreateText(Display *dpy, Window win)
{
   return TRUE;
}

static int XimHandleCreateText(Display *dpy, Window win)
{
   XIMUnsetICFocus(win);
   return TRUE;
}

static int XimExpectNextEvent(Display *dpy, Window win)
{
   return (win == drawWindow ||
         (TidgetHasFocus() && win == TidgetGetFocusWindow()));
}

static int XimHandleNextEvent(Display *dpy, Window win, XEvent *ev)
{
   XIMNextEvent(dpy, win, ev);
   return TRUE;
}

static int XimHandleNewCurText(Display *dpy, Window win)
{
   XIMSetICFocus(dpy, win);
   return TRUE;
}

static int XimExpectLookupString(Display *dpy, Window win)
{
   return (win == drawWindow ||
         (TidgetHasFocus() && win == TidgetGetFocusWindow()));
}

static XIMStatus c_stat;

static int XimHandleLookupString(Display *dpy, Window win, XKeyEvent *key_ev, char *psz_buf, int buf_size, KeySym *p_key_sym, int *pn_has_ch)
{
   *pn_has_ch = XIMLookupString(key_ev, psz_buf, buf_size, p_key_sym, &c_stat);
   return TRUE;
}

static int XimTellCursorPos(Display *dpy, Window win, int cur_x, int cur_y)
{
   XIMTellCursorPosition(dpy, win, cur_x, cur_y);
   return TRUE;
}
#endif /* ~_NO_XIM */

/* --------------------- tgtwb5 Routines --------------------- */

#ifndef _NO_TGTWB5
static void Tgtwb5CleanUp(Display *dpy, Window win)
{
   Tgtwb5_CleanUp(dpy, win);
}

static int Tgtwb5Init(Display *dpy, Window win, char *arg)
{
   return Tgtwb5_Init(dpy, win, arg);
}

static int Tgtwb5SendKey(Display *dpy, Window win, XKeyEvent *key_ev, char *buf)
{
   return Tgtwb5_SendKey(dpy, win, key_ev, buf);
}

static int Tgtwb5ExpectCM(Display *dpy, Window win)
{
   return FALSE;
}

static int Tgtwb5ActiveOnCntrlSpace(Display *dpy, Window win)
{
   return (win == drawWindow ||
         (TidgetHasFocus() && win == TidgetGetFocusWindow()));
}

static int Tgtwb5HandleCntrlSpace(Display *dpy, Window win)
{
   return Tgtwb5_HandleCntrlSpace(dpy, win);
}

static int Tgtwb5DeactiveOnCreateText(Display *dpy, Window win)
{
   return TRUE;
}

static int Tgtwb5HandleCreateText(Display *dpy, Window win)
{
   Tgtwb5_HandleCreateText(dpy, win);
   return TRUE;
}
#endif /* ~_NO_XCIN */

/* --------------------- Global Routines --------------------- */

static struct tagIMInfo gstIMInfo[] = {
#ifndef _NO_XCIN
   { "xcin", TGIM_XCIN, TGIM_DBIM,
     NULL, XcinInit, XcinFocusIn, XcinFocusOut,
     XcinSendKey, NULL, XcinExpectCM, NULL,
     NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
   },
#endif /* ~_NO_XCIN */
#ifndef _NO_CHINPUT
   { "chinput", TGIM_CHINPUT, TGIM_DBIM,
     NULL, ChinputInit, ChinputFocusIn, ChinputFocusOut,
     ChinputSendKey, NULL, ChinputExpectCM, ChinputHandleCM,
     NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
   },
#endif /* ~_NO_CHINPUT */
#ifndef _NO_KINPUT
   { "kinput2", TGIM_KINPUT, TGIM_DBIM,
     KinputCleanUp, KinputInit, KinputFocusIn, KinputFocusOut,
     KinputSendKey, KinputConvProperty, KinputExpectCM, KinputHandleCM,
     KinputActiveOnCntrlSpace, KinputHandleCntrlSpace,
     KinputDeactiveOnCreateText, KinputHandleCreateText,
     KinputTellCursorPos, NULL, NULL, NULL, NULL, NULL
   },
#endif /* ~_NO_KINPUT */
#ifndef _NO_XIM
   { "xim", TGIM_XIM, (TGIM_DBIM|TGIM_SBIM),
     XimCleanUp, XimInit, XimFocusIn, XimFocusOut,
     NULL, NULL, NULL, NULL, NULL, NULL,
     XimDeactiveOnCreateText, XimHandleCreateText,
     XimTellCursorPos, XimExpectNextEvent, XimHandleNextEvent,
     XimHandleNewCurText, XimExpectLookupString, XimHandleLookupString
   },
#endif /* ~_NO_XIM */
#ifndef _NO_TGTWB5
   { "tgtwb5/tgchgb", TGIM_TGTWB5, TGIM_DBIM,
     Tgtwb5CleanUp, Tgtwb5Init, NULL, NULL,
     Tgtwb5SendKey, NULL, Tgtwb5ExpectCM, NULL,
     Tgtwb5ActiveOnCntrlSpace, Tgtwb5HandleCntrlSpace,
     Tgtwb5DeactiveOnCreateText, Tgtwb5HandleCreateText,
     NULL, NULL, NULL, NULL, NULL, NULL
   },
#endif /* ~_NO_TGTWB5 */
   { NULL, TGIM_NONE, 0, NULL, NULL, NULL, NULL,
     NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
     NULL, NULL, NULL, NULL, NULL
   }
};

static void *gpLibIdnHandle=NULL;
static Tgim_stringprep_convert_Func *gpfn_stringprep_convert=NULL;

static void CleanUpTgim_stringprep_convert(void)
{
   if (gpLibIdnHandle != NULL) {
      gpfn_stringprep_convert = NULL;
      CLOSE_DL(gpLibIdnHandle);
      gpLibIdnHandle = NULL;
   }
}

static void InitTgim_stringprep_convert(void)
{
#if (!defined(_NO_IDNLIB) || defined(HAVE_LIBZ))
   gpLibIdnHandle = OPEN_DL("libidn.so", OPEN_DL_MODE);
   if (gpLibIdnHandle != NULL) {
      gpfn_stringprep_convert =
            (Tgim_stringprep_convert_Func*)GET_DL_SYM(gpLibIdnHandle,
            "stringprep_convert");
      if (gpfn_stringprep_convert == NULL) {
         CLOSE_DL(gpLibIdnHandle);
         gpLibIdnHandle = NULL;
      }
   }
#endif /* (!defined(_NO_IDNLIB) || defined(HAVE_LIBZ)) */
}

int Tgim_has_stringprep_convert(void)
{
#if (!defined(_NO_IDNLIB) || defined(HAVE_LIBZ))
   if (gpLibIdnHandle != NULL && gpfn_stringprep_convert != NULL) {
      return TRUE;
   }
#endif /* (!defined(_NO_IDNLIB) || defined(HAVE_LIBZ)) */
   return FALSE;
}

char *Tgim_stringprep_convert(const char *str, const char *to_codeset,
      const char *from_codeset)
{
#if (!defined(_NO_IDNLIB) || defined(HAVE_LIBZ))
   if (gpLibIdnHandle != NULL && gpfn_stringprep_convert != NULL) {
       return (gpfn_stringprep_convert)(str, to_codeset, from_codeset);
   }
#endif /* (!defined(_NO_IDNLIB) || defined(HAVE_LIBZ)) */
   return NULL;
}

void CleanUpInputMethods(void)
{
   CleanUpTgim_stringprep_convert();
   if (gnInputMethodIndex != INVALID &&
         gstIMInfo[gnInputMethodIndex].pCleanUpFunc != NULL) {
      (gstIMInfo[gnInputMethodIndex].pCleanUpFunc)(mainDisplay, mainWindow);
   }
   gnInputMethodIndex = INVALID;
   cmdLineHasInputMethod = FALSE;
   cmdLineDoubleByteInputMethod = FALSE;
   *cmdLineInputMethod = '\0';
   gpIMInfo = NULL;
   gnSingleOrDoubleByteInputMethod = 0;
}

int InitInputMethods(void)
{
   char *psz=NULL, single_double=0;

   gnInputMethod = gnInputMethodIndex = INVALID;
   if (cmdLineHasInputMethod) {
      UtilTrimBlanks(cmdLineInputMethod);
      psz = cmdLineInputMethod;
      single_double = (cmdLineDoubleByteInputMethod ? TGIM_DBIM : TGIM_SBIM);
   } else {
      psz = XGetDefault(mainDisplay, TOOL_NAME, "DoubleByteInputMethod");
      if (psz == NULL) {
         psz = XGetDefault(mainDisplay, TOOL_NAME, "SingleByteInputMethod");
         single_double = TGIM_SBIM;
      } else {
         if (XGetDefault(mainDisplay, TOOL_NAME, "SingleByteInputMethod") !=
               NULL) {
            sprintf(gszMsgBox, TgLoadString(STID_TWO_XDEF_ONE_IGNORED),
                  TOOL_NAME, "DoubleByteInputMethod", TOOL_NAME,
                  "SingleByteInputMethod", TOOL_NAME, "SingleByteInputMethod");
            Msg(gszMsgBox);
            fprintf(stderr, "%s\n", gszMsgBox);
         }
         single_double = TGIM_DBIM;
      }
   }
   gnSingleOrDoubleByteInputMethod = single_double;

   if (psz != NULL) {
      IMInfo *pIMInfo=gstIMInfo;
      char *psz_comma=strchr(psz, ',');
      int i=0;

      if (psz_comma != NULL) *psz_comma = '\0';
      for ( ; pIMInfo->pszName != NULL; pIMInfo++, i++) {
         char *psz_slash=strchr(pIMInfo->pszName,'/');

         if (psz_slash == NULL) {
            if (UtilStrICmp(psz, pIMInfo->pszName) == 0 &&
                  (single_double & pIMInfo->nSingleDouble) == single_double) {
               gnInputMethod = pIMInfo->nInputMethod;
               gnInputMethodIndex = i;
               gpIMInfo = pIMInfo;
               if (gstIMInfo[gnInputMethodIndex].pInitFunc != NULL) {
                  if (psz_comma == NULL) {
                     if (!((gstIMInfo[gnInputMethodIndex].pInitFunc)(mainDisplay,
                           mainWindow, NULL))) {
                        /* may not be a problem */
                     }
                  } else {
                     if (!((gstIMInfo[gnInputMethodIndex].pInitFunc)(mainDisplay,
                           mainWindow, &psz_comma[1]))) {
                        /* may not be a problem */
                     }
                  }
               }
               break;
            }
         } else {
            char *psz_dup=UtilStrDup(pIMInfo->pszName), *psz_cur=NULL;
            int done=FALSE;

            if (psz_dup == NULL) FailAllocMessage();
            psz_cur = psz_dup;
            psz_slash = strchr(psz_cur, '/');
            while (!done) {
               if (psz_slash != NULL) *psz_slash = '\0';
               if (UtilStrICmp(psz, psz_cur) == 0 &&
                     (single_double & pIMInfo->nSingleDouble) == single_double) {
                  gnInputMethod = pIMInfo->nInputMethod;
                  gnInputMethodIndex = i;
                  gpIMInfo = pIMInfo;
                  if (gstIMInfo[gnInputMethodIndex].pInitFunc != NULL) {
                     if (psz_comma == NULL) {
                        if (!((gstIMInfo[gnInputMethodIndex].pInitFunc)(mainDisplay,
                              mainWindow, NULL))) {
                           /* may not be a problem */
                        }
                     } else {
                        if (!((gstIMInfo[gnInputMethodIndex].pInitFunc)(mainDisplay,
                              mainWindow, &psz_comma[1]))) {
                           /* may not be a problem */
                        }
                     }
                  }
                  done = TRUE;
               }
               if (psz_slash != NULL) {
                  *psz_slash++ = '/';
                  psz_cur = psz_slash;
                  psz_slash = strchr(psz_cur, '/');
               } else {
                  break;
               }
            }
            UtilFree(psz_dup);
            if (done) {
               break;
            }
         }
      }
      if (psz_comma != NULL) *psz_comma++ = ',';

      if (gnInputMethod == INVALID) {
         if (single_double == TGIM_DBIM) {
            fprintf(stderr, TgLoadString(STID_DBIM_UNSUPPORTED), psz);
         } else {
            fprintf(stderr, TgLoadString(STID_SBIM_UNSUPPORTED), psz);
         }
         fprintf(stderr, "\n");
      }
   }
   if (gnSingleOrDoubleByteInputMethod == TGIM_SBIM &&
         gnInputMethod != INVALID) {
      InitTgim_stringprep_convert();
   }
   return TRUE;
}

int ResetInputMethod(void)
{
   int rc=FALSE;

   if (gpIMInfo == NULL) return FALSE;

   sprintf(gszMsgBox, TgLoadString(STID_OK_TO_RESET_GIVEN_IM),
         gpIMInfo->pszName);
   if (MsgBox(gszMsgBox, TOOL_NAME, YNC_MB) != MB_ID_YES) {
      return FALSE;
   }
   CleanUpInputMethods();
   rc = InitInputMethods();

   sprintf(gszMsgBox, TgLoadString(STID_GIVEN_IM_RESETED), gpIMInfo->pszName);
   Msg(gszMsgBox);

   return rc;
}

int InputMethodTypeMatched(int double_byte)
{
   if (gnInputMethodIndex == INVALID || gpIMInfo == NULL) return FALSE;

   if (double_byte) {
      return (gnSingleOrDoubleByteInputMethod == TGIM_DBIM &&
            (gpIMInfo->nSingleDouble & TGIM_DBIM) == TGIM_DBIM);
   } else {
      return (gnSingleOrDoubleByteInputMethod == TGIM_SBIM &&
            (gpIMInfo->nSingleDouble & TGIM_SBIM) == TGIM_SBIM);
   }
}

int tgIMFocusIn(Display *dpy, Window win)
{
   if (gnInputMethodIndex == INVALID ||
         gstIMInfo[gnInputMethodIndex].pFocusInFunc == NULL) {
      return FALSE;
   }
   return ((gstIMInfo[gnInputMethodIndex].pFocusInFunc)(dpy, win));
}

int tgIMFocusOut(Display *dpy, Window win)
{
   if (gnInputMethodIndex == INVALID ||
         gstIMInfo[gnInputMethodIndex].pFocusOutFunc == NULL) {
      return FALSE;
   }
   return ((gstIMInfo[gnInputMethodIndex].pFocusOutFunc)(dpy, win));
}

int tgIMTranslateKeyEvent(Display *dpy, Window win, XKeyEvent *key_ev, char *buf)
{
   if (gnInputMethodIndex == INVALID ||
         gstIMInfo[gnInputMethodIndex].pSendKeyFunc == NULL) {
      return FALSE;
   }
   return ((gstIMInfo[gnInputMethodIndex].pSendKeyFunc)(dpy, win, key_ev, buf));
}

int tgIMConvertProperty(Display *dpy, Window win, XPropertyEvent *prop_ev, char **ppsz_buf)
{
   if (gnInputMethodIndex == INVALID ||
         gstIMInfo[gnInputMethodIndex].pConvPropertyFunc == NULL) {
      return FALSE;
   }
   return ((gstIMInfo[gnInputMethodIndex].pConvPropertyFunc)(dpy, win, prop_ev,
         ppsz_buf));
}

int tgIMExpectClientMessage(Display *dpy, Window win)
{
   if (gnInputMethodIndex == INVALID ||
         gstIMInfo[gnInputMethodIndex].pExpectCMFunc == NULL) {
      return FALSE;
   }
   return ((gstIMInfo[gnInputMethodIndex].pExpectCMFunc)(dpy, win));
}

int tgIMHandleClientMessage(Display *dpy, Window win, XClientMessageEvent *cm_ev, XKeyEvent *key_ev, char *buf)
{
   if (gnInputMethodIndex == INVALID ||
         gstIMInfo[gnInputMethodIndex].pHandleCMFunc == NULL) {
      return FALSE;
   }
   return ((gstIMInfo[gnInputMethodIndex].pHandleCMFunc)(dpy, win, cm_ev,
         key_ev, buf));
}

int tgIMActiveOnCntrlSpace(Display *dpy, Window win)
{
   if (gnInputMethodIndex == INVALID ||
         gstIMInfo[gnInputMethodIndex].pActivateOnCntrlSpaceFunc == NULL) {
      return FALSE;
   }
   return ((gstIMInfo[gnInputMethodIndex].pActivateOnCntrlSpaceFunc)(dpy, win));
}

int tgIMHandleCntrlSpace(Display *dpy, Window win)
{
   if (gnInputMethodIndex == INVALID ||
         gstIMInfo[gnInputMethodIndex].pHandleCntrlSpaceFunc == NULL) {
      return FALSE;
   }
   return ((gstIMInfo[gnInputMethodIndex].pHandleCntrlSpaceFunc)(dpy, win));
}

int tgIMDeactiveOnCreateText(Display *dpy, Window win)
{
   if (gnInputMethodIndex == INVALID ||
         gstIMInfo[gnInputMethodIndex].pDeactivateOnCreateTextFunc == NULL) {
      return FALSE;
   }
   return ((gstIMInfo[gnInputMethodIndex].pDeactivateOnCreateTextFunc)(dpy,
         win));
}

int tgIMHandleCreateText(Display *dpy, Window win)
{
   if (gnInputMethodIndex == INVALID ||
         gstIMInfo[gnInputMethodIndex].pHandleCreateTextFunc == NULL) {
      return FALSE;
   }
   return ((gstIMInfo[gnInputMethodIndex].pHandleCreateTextFunc)(dpy, win));
}

int tgIMTellCursorPosition(Display *dpy, Window win, int cur_x, int cur_y)
{
   if (gnInputMethodIndex == INVALID ||
         gstIMInfo[gnInputMethodIndex].pTellCursorPosFunc == NULL) {
      return FALSE;
   }
   return ((gstIMInfo[gnInputMethodIndex].pTellCursorPosFunc)(dpy, win,
         cur_x, cur_y));
}

int tgIMExpectNextEvent(Display *dpy, Window win)
{
   if (gnInputMethodIndex == INVALID ||
         gstIMInfo[gnInputMethodIndex].pExpectNextEventFunc == NULL) {
      return FALSE;
   }
   return ((gstIMInfo[gnInputMethodIndex].pExpectNextEventFunc)(dpy, win));
}

int tgIMHandleNextEvent(Display *dpy, Window win, XEvent *ev)
{
   if (gnInputMethodIndex == INVALID ||
         gstIMInfo[gnInputMethodIndex].pHandleNextEventFunc == NULL) {
      return FALSE;
   }
   return ((gstIMInfo[gnInputMethodIndex].pHandleNextEventFunc)(dpy, win, ev));
}

int tgIMHandleNewCurText(Display *dpy, Window win)
{
   if (gnInputMethodIndex == INVALID ||
         gstIMInfo[gnInputMethodIndex].pHandleNewCurTextFunc == NULL) {
      return FALSE;
   }
   return ((gstIMInfo[gnInputMethodIndex].pHandleNewCurTextFunc)(dpy, win));
}

int tgIMExpectLookupString(Display *dpy, Window win)
{
   if (gnInputMethodIndex == INVALID ||
         gstIMInfo[gnInputMethodIndex].pExpectLookupStringFunc == NULL) {
      return FALSE;
   }
   return ((gstIMInfo[gnInputMethodIndex].pExpectLookupStringFunc)(dpy, win));
}

int tgIMHandleLookupString(Display *dpy, Window win, XKeyEvent *key_ev, char *psz_buf, int buf_size, KeySym *p_key_sym, int *pn_has_ch)
{
   if (gnInputMethodIndex == INVALID ||
         gstIMInfo[gnInputMethodIndex].pHandleLookupStringFunc == NULL) {
      return FALSE;
   }
   return ((gstIMInfo[gnInputMethodIndex].pHandleLookupStringFunc)(dpy, win,
         key_ev, psz_buf, buf_size, p_key_sym, pn_has_ch));
}