summaryrefslogtreecommitdiff
path: root/IMdkit/i18nMethod.c
blob: 910647973c42aa35c7921637bb24a3225d1cb1e2 (plain)
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
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
/******************************************************************
 
         Copyright 1994, 1995 by Sun Microsystems, Inc.
         Copyright 1993, 1994 by Hewlett-Packard Company
 
Permission to use, copy, modify, distribute, and sell this software
and its documentation for any purpose is hereby granted without fee,
provided that the above copyright notice appear in all copies and
that both that copyright notice and this permission notice appear
in supporting documentation, and that the name of Sun Microsystems, Inc.
and Hewlett-Packard not be used in advertising or publicity pertaining to
distribution of the software without specific, written prior permission.
Sun Microsystems, Inc. and Hewlett-Packard make no representations about
the suitability of this software for any purpose.  It is provided "as is"
without express or implied warranty.
 
SUN MICROSYSTEMS INC. AND HEWLETT-PACKARD COMPANY DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
SUN MICROSYSTEMS, INC. AND HEWLETT-PACKARD COMPANY 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.
 
  Author: Hidetoshi Tajima(tajima@Eng.Sun.COM) Sun Microsystems, Inc.

    This version tidied and debugged by Steve Underwood May 1999
 
******************************************************************/

#include <stdio.h>
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#ifndef NEED_EVENTS
#define NEED_EVENTS
#endif
#include <X11/Xproto.h>
#undef NEED_EVENTS
#include "FrameMgr.h"
#include "IMdkit.h"
#include "Xi18n.h"
#include "XimFunc.h"

extern Xi18nClient *_Xi18nFindClient (Xi18n, CARD16);

static void *xi18n_setup (Display *, XIMArg *);
static Status xi18n_openIM (XIMS);
static Status xi18n_closeIM (XIMS);
static char *xi18n_setIMValues (XIMS, XIMArg *);
static char *xi18n_getIMValues (XIMS, XIMArg *);
static Status xi18n_forwardEvent (XIMS, XPointer);
static Status xi18n_commit (XIMS, XPointer);
static int xi18n_callCallback (XIMS, XPointer);
static int xi18n_preeditStart (XIMS, XPointer);
static int xi18n_preeditEnd (XIMS, XPointer);
static int xi18n_syncXlib (XIMS, XPointer);

#ifndef XIM_SERVERS
#define XIM_SERVERS "XIM_SERVERS"
#endif
static Atom XIM_Servers = None;


IMMethodsRec Xi18n_im_methods =
{
    xi18n_setup,
    xi18n_openIM,
    xi18n_closeIM,
    xi18n_setIMValues,
    xi18n_getIMValues,
    xi18n_forwardEvent,
    xi18n_commit,
    xi18n_callCallback,
    xi18n_preeditStart,
    xi18n_preeditEnd,
    xi18n_syncXlib,
};

extern Bool _Xi18nCheckXAddress (Xi18n, TransportSW *, char *);
extern Bool _Xi18nCheckTransAddress (Xi18n, TransportSW *, char *);

TransportSW _TransR[] =
{
    {"X",               1, _Xi18nCheckXAddress},
#ifdef TCPCONN
    {"tcp",             3, _Xi18nCheckTransAddress},
    {"local",           5, _Xi18nCheckTransAddress},
#endif
#ifdef DNETCONN
    {"decnet",          6, _Xi18nCheckTransAddress},
#endif
    {(char *) NULL,     0, (Bool (*) ()) NULL}
};

static Bool GetInputStyles (Xi18n i18n_core, XIMStyles **p_style)
{
    Xi18nAddressRec *address = (Xi18nAddressRec *) &i18n_core->address;
    XIMStyles *p;
    int	i;

    p = &address->input_styles;
    if ((*p_style = (XIMStyles *) malloc (sizeof (XIMStyles)
                                          + p->count_styles*sizeof (XIMStyle)))
        == NULL)
    {
        return False;
    }
    /*endif*/
    (*p_style)->count_styles = p->count_styles;
    (*p_style)->supported_styles = (XIMStyle *) ((XPointer) *p_style + sizeof (XIMStyles));
    for (i = 0;  i < (int) p->count_styles;  i++)
        (*p_style)->supported_styles[i] = p->supported_styles[i];
    /*endfor*/
    return True;
}

static Bool GetOnOffKeys (Xi18n i18n_core, long mask, XIMTriggerKeys **p_key)
{
    Xi18nAddressRec *address = (Xi18nAddressRec *) &i18n_core->address;
    XIMTriggerKeys *p;
    int	i;

    if (mask & I18N_ON_KEYS)
        p = &address->on_keys;
    else
        p = &address->off_keys;
    /*endif*/
    if ((*p_key = (XIMTriggerKeys *) malloc (sizeof(XIMTriggerKeys)
                                             + p->count_keys*sizeof(XIMTriggerKey)))
        == NULL)
    {
        return False;
    }
    /*endif*/
    (*p_key)->count_keys = p->count_keys;
    (*p_key)->keylist =
        (XIMTriggerKey *) ((XPointer) *p_key + sizeof(XIMTriggerKeys));
    for (i = 0;  i < (int) p->count_keys;  i++)
    {
        (*p_key)->keylist[i].keysym = p->keylist[i].keysym;
        (*p_key)->keylist[i].modifier = p->keylist[i].modifier;
        (*p_key)->keylist[i].modifier_mask = p->keylist[i].modifier_mask;
    }
    /*endfor*/
    return True;
}

static Bool GetEncodings(Xi18n i18n_core, XIMEncodings **p_encoding)
{
    Xi18nAddressRec *address = (Xi18nAddressRec *) &i18n_core->address;
    XIMEncodings *p;
    int	i;

    p = &address->encoding_list;

    if ((*p_encoding = (XIMEncodings *) malloc (sizeof (XIMEncodings)
                                              + p->count_encodings*sizeof(XIMEncoding))) == NULL)
    {
        return False;
    }
    /*endif*/
    (*p_encoding)->count_encodings = p->count_encodings;
    (*p_encoding)->supported_encodings =
        (XIMEncoding *) ((XPointer)*p_encoding + sizeof (XIMEncodings));
    for (i = 0;  i < (int) p->count_encodings;  i++)
    {
        (*p_encoding)->supported_encodings[i]
            = (char *) malloc (strlen (p->supported_encodings[i]) + 1);
        strcpy ((*p_encoding)->supported_encodings[i],
                p->supported_encodings[i]);
    }
    /*endif*/
    return True;
}

static char *ParseArgs (Xi18n i18n_core, int mode, XIMArg *args)
{
    Xi18nAddressRec *address = (Xi18nAddressRec *) &i18n_core->address;
    XIMArg *p;

    if (mode == I18N_OPEN  ||  mode == I18N_SET)
    {
        for (p = args;  p->name != NULL;  p++)
        {
            if (strcmp (p->name, IMLocale) == 0)
            {
                if (address->imvalue_mask & I18N_IM_LOCALE)
                    return IMLocale;
                /*endif*/
                address->im_locale = (char *) malloc (strlen (p->value) + 1);
                if (!address->im_locale)
                    return IMLocale;
                /*endif*/
                strcpy (address->im_locale, p->value);
                address->imvalue_mask |= I18N_IM_LOCALE;
            }
            else if (strcmp (p->name, IMServerTransport) == 0)
            {
                if (address->imvalue_mask & I18N_IM_ADDRESS)
                    return IMServerTransport;
                /*endif*/
                address->im_addr = (char *) malloc (strlen (p->value) + 1);
                if (!address->im_addr)
                    return IMServerTransport;
                /*endif*/
                strcpy(address->im_addr, p->value);
                address->imvalue_mask |= I18N_IM_ADDRESS;
            }
            else if (strcmp (p->name, IMServerName) == 0)
            {
                if (address->imvalue_mask & I18N_IM_NAME)
                    return IMServerName;
                /*endif*/
                address->im_name = (char *) malloc (strlen (p->value) + 1);
                if (!address->im_name)
                    return IMServerName;
                /*endif*/
                strcpy (address->im_name, p->value);
                address->imvalue_mask |= I18N_IM_NAME;
            }
            else if (strcmp (p->name, IMServerWindow) == 0)
            {
                if (address->imvalue_mask & I18N_IMSERVER_WIN)
                    return IMServerWindow;
                /*endif*/
                address->im_window = (Window) p->value;
                address->imvalue_mask |= I18N_IMSERVER_WIN;
            }
            else if (strcmp (p->name, IMInputStyles) == 0)
            {
                if (address->imvalue_mask & I18N_INPUT_STYLES)
                    return IMInputStyles;
                /*endif*/
                address->input_styles.count_styles =
                    ((XIMStyles*)p->value)->count_styles;
                address->input_styles.supported_styles =
                    (XIMStyle *) malloc (sizeof (XIMStyle)*address->input_styles.count_styles);
                if (address->input_styles.supported_styles == (XIMStyle *) NULL)
                    return IMInputStyles;
                /*endif*/
                memmove (address->input_styles.supported_styles,
                         ((XIMStyles *) p->value)->supported_styles,
                         sizeof (XIMStyle)*address->input_styles.count_styles);
                address->imvalue_mask |= I18N_INPUT_STYLES;
            }
            else if (strcmp (p->name, IMProtocolHandler) == 0)
            {
                address->improto = (IMProtoHandler) p->value;
                address->imvalue_mask |= I18N_IM_HANDLER;
            }
            else if (strcmp (p->name, IMOnKeysList) == 0)
            {
                if (address->imvalue_mask & I18N_ON_KEYS)
                    return IMOnKeysList;
                /*endif*/
                address->on_keys.count_keys =
                    ((XIMTriggerKeys *) p->value)->count_keys;
                address->on_keys.keylist =
                    (XIMTriggerKey *) malloc (sizeof (XIMTriggerKey)*address->on_keys.count_keys);
                if (address->on_keys.keylist == (XIMTriggerKey *) NULL)
                    return IMOnKeysList;
                /*endif*/
                memmove (address->on_keys.keylist,
                         ((XIMTriggerKeys *) p->value)->keylist,
                         sizeof (XIMTriggerKey)*address->on_keys.count_keys);
                address->imvalue_mask |= I18N_ON_KEYS;
            }
            else if (strcmp (p->name, IMOffKeysList) == 0)
            {
                if (address->imvalue_mask & I18N_OFF_KEYS)
                    return IMOffKeysList;
                /*endif*/
                address->off_keys.count_keys =
                    ((XIMTriggerKeys *) p->value)->count_keys;
                address->off_keys.keylist =
                    (XIMTriggerKey *) malloc (sizeof (XIMTriggerKey)*address->off_keys.count_keys);
                if (address->off_keys.keylist == (XIMTriggerKey *) NULL)
                    return IMOffKeysList;
                /*endif*/
                memmove (address->off_keys.keylist,
                         ((XIMTriggerKeys *) p->value)->keylist,
                         sizeof (XIMTriggerKey)*address->off_keys.count_keys);
                address->imvalue_mask |= I18N_OFF_KEYS;
            }
            else if (strcmp (p->name, IMEncodingList) == 0)
            {
                if (address->imvalue_mask & I18N_ENCODINGS)
                    return IMEncodingList;
                /*endif*/
                address->encoding_list.count_encodings =
                    ((XIMEncodings *) p->value)->count_encodings;
                address->encoding_list.supported_encodings =
                    (XIMEncoding *) malloc (sizeof (XIMEncoding)*address->encoding_list.count_encodings);
                if (address->encoding_list.supported_encodings
                    == (XIMEncoding *) NULL)
                {
                    return IMEncodingList;
                }
                /*endif*/
                memmove (address->encoding_list.supported_encodings,
                         ((XIMEncodings *) p->value)->supported_encodings,
                         sizeof (XIMEncoding)*address->encoding_list.count_encodings);
                address->imvalue_mask |= I18N_ENCODINGS;
            }
            else if (strcmp (p->name, IMFilterEventMask) == 0)
            {
                if (address->imvalue_mask & I18N_FILTERMASK)
                    return IMFilterEventMask;
                /*endif*/
                address->filterevent_mask = (long) p->value;
                address->imvalue_mask |= I18N_FILTERMASK;
            }
            /*endif*/
        }
        /*endfor*/
        if (mode == I18N_OPEN)
        {
            /* check mandatory IM values */
            if (!(address->imvalue_mask & I18N_IM_LOCALE))
            {
                /* locales must be set in IMOpenIM */
                return IMLocale;
            }
            /*endif*/
            if (!(address->imvalue_mask & I18N_IM_ADDRESS))
            {
                /* address must be set in IMOpenIM */
                return IMServerTransport;
            }
            /*endif*/
        }
        /*endif*/
    }
    else if (mode == I18N_GET)
    {
        for (p = args;  p->name != NULL;  p++)
        {
            if (strcmp (p->name, IMLocale) == 0)
            {
                p->value = (char *) malloc (strlen (address->im_locale) + 1);
                if (!p->value)
                    return IMLocale;
                /*endif*/
                strcpy (p->value, address->im_locale);
            }
            else if (strcmp (p->name, IMServerTransport) == 0)
            {
                p->value = (char *) malloc (strlen (address->im_addr) + 1);
                if (!p->value)
                    return IMServerTransport;
                /*endif*/
                strcpy (p->value, address->im_addr);
            }
            else if (strcmp (p->name, IMServerName) == 0)
            {
                if (address->imvalue_mask & I18N_IM_NAME)
                {
                    p->value = (char *) malloc (strlen (address->im_name) + 1);
                    if (!p->value)
                        return IMServerName;
                    /*endif*/
                    strcpy (p->value, address->im_name);
                }
                else
                {
                    return IMServerName;
                }
                /*endif*/
            }
            else if (strcmp (p->name, IMServerWindow) == 0)
            {
                if (address->imvalue_mask & I18N_IMSERVER_WIN)
                    *((Window *) (p->value)) = address->im_window;
                else
                    return IMServerWindow;
                /*endif*/
            }
            else if (strcmp (p->name, IMInputStyles) == 0)
            {
                if (GetInputStyles (i18n_core,
                                    (XIMStyles **) p->value) == False)
                {
                    return IMInputStyles;
                }
                /*endif*/
            }
            else if (strcmp (p->name, IMProtocolHandler) == 0)
            {
                if (address->imvalue_mask & I18N_IM_HANDLER)
                    *((IMProtoHandler *) (p->value)) = address->improto;
                else
                    return IMProtocolHandler;
                /*endif*/
            }
            else if (strcmp (p->name, IMOnKeysList) == 0)
            {
                if (address->imvalue_mask & I18N_ON_KEYS)
                {
                    if (GetOnOffKeys (i18n_core,
                                      I18N_ON_KEYS,
                                      (XIMTriggerKeys **) p->value) == False)
                    {
                        return IMOnKeysList;
                    }
                    /*endif*/
                }
                else
                {
                    return IMOnKeysList;
                }
                /*endif*/
            }
            else if (strcmp (p->name, IMOffKeysList) == 0)
            {
                if (address->imvalue_mask & I18N_OFF_KEYS)
                {
                    if (GetOnOffKeys (i18n_core,
                                      I18N_OFF_KEYS,
                                      (XIMTriggerKeys **) p->value) == False)
                    {
                        return IMOffKeysList;
                    }
                    /*endif*/
                }
                else
                {
                    return IMOffKeysList;
                }
                /*endif*/
            }
            else if (strcmp (p->name, IMEncodingList) == 0)
            {
                if (address->imvalue_mask & I18N_ENCODINGS)
                {
                    if (GetEncodings (i18n_core,
                                      (XIMEncodings **) p->value) == False)
                    {
                        return IMEncodingList;
                    }
                    /*endif*/
                }
                else
                {
                    return IMEncodingList;
                }
                /*endif*/
            }
            else if (strcmp (p->name, IMFilterEventMask) == 0)
            {
                if (address->imvalue_mask & I18N_FILTERMASK)
                    *((long *) (p->value)) = address->filterevent_mask;
                else
                    return IMFilterEventMask;
                /*endif*/
            }
            /*endif*/
        }
        /*endfor*/
    }
    /*endif*/
    return NULL;
}

static int CheckIMName (Xi18n i18n_core)
{
    char *address = i18n_core->address.im_addr;
    int i;

    for (i = 0;  _TransR[i].transportname;  i++)
    {
        while (*address == ' '  ||  *address == '\t')
            address++;
        /*endwhile*/
        if (strncmp (address,
                     _TransR[i].transportname,
                     _TransR[i].namelen) == 0
            &&
            address[_TransR[i].namelen] == '/')
        {
            if (_TransR[i].checkAddr (i18n_core,
                                      &_TransR[i],
                                      address + _TransR[i].namelen + 1) == True)
            {
                return True;
            }
            /*endif*/
            return False;
        }
        /*endif*/
    }
    /*endfor*/
    return False;
}

static int SetXi18nSelectionOwner(Xi18n i18n_core)
{
    Display *dpy = i18n_core->address.dpy;
    Window ims_win = i18n_core->address.im_window;
    Window root = RootWindow (dpy, DefaultScreen (dpy));
    Atom realtype;
    int realformat;
    unsigned long bytesafter;
    long *data=NULL;
    unsigned long length;
    Atom atom;
    int i;
    int found;
    int forse = False;
    char buf[256];

    (void)snprintf(buf, 256, "@server=%s", i18n_core->address.im_name);
    if ((atom = XInternAtom(dpy, buf, False)) == 0)
        return False;
    i18n_core->address.selection = atom;

    if (XIM_Servers == None)
        XIM_Servers = XInternAtom (dpy, XIM_SERVERS, False);
    /*endif*/
    XGetWindowProperty (dpy,
                        root,
                        XIM_Servers,
                        0L,
                        1000000L,
                        False,
                        XA_ATOM,
                        &realtype,
                        &realformat,
                        &length,
                        &bytesafter,
                        (unsigned char **) (&data));
    if (realtype != None && (realtype != XA_ATOM || realformat != 32)) {
        if (data != NULL)
            XFree ((char *) data);
        return False;
    }

    found = False;
    for (i = 0; i < length; i++) {
        if (data[i] == atom) {
            Window owner;
            found = True;
            if ((owner = XGetSelectionOwner (dpy, atom)) != ims_win) {
                if (owner == None  ||  forse == True)
                    XSetSelectionOwner (dpy, atom, ims_win, CurrentTime);
                else
                    return False;
            }
            break;
        }
    }

    if (found == False) {
        XSetSelectionOwner (dpy, atom, ims_win, CurrentTime);
        XChangeProperty (dpy,
                         root,
                         XIM_Servers,
                         XA_ATOM,
                         32,
                         PropModePrepend,
                         (unsigned char *) &atom,
                         1);
    }
    else {
	/* 
	 * We always need to generate the PropertyNotify to the Root Window 
	 */
        XChangeProperty (dpy,
                         root,
                         XIM_Servers,
                         XA_ATOM,
                         32,
                         PropModePrepend,
                         (unsigned char *) data,
                         0);
    }
    if (data != NULL)
        XFree ((char *) data);
    
    /* Intern "LOCALES" and "TRANSOPORT" Target Atoms */
    i18n_core->address.Localename = XInternAtom (dpy, LOCALES, False);
    i18n_core->address.Transportname = XInternAtom (dpy, TRANSPORT, False);
    return (XGetSelectionOwner (dpy, atom) == ims_win);
}

static int DeleteXi18nAtom(Xi18n i18n_core)
{
    Display *dpy = i18n_core->address.dpy;
    Window root = RootWindow (dpy, DefaultScreen (dpy));
    Atom realtype;
    int realformat;
    unsigned long bytesafter;
    long *data=NULL;
    unsigned long length;
    Atom atom;
    int i, ret;
    int found;
    char buf[256];

    (void)snprintf(buf, 256, "@server=%s", i18n_core->address.im_name);
    if ((atom = XInternAtom(dpy, buf, False)) == 0)
        return False;
    i18n_core->address.selection = atom;

    if (XIM_Servers == None)
        XIM_Servers = XInternAtom (dpy, XIM_SERVERS, False);
    XGetWindowProperty (dpy,
                        root,
                        XIM_Servers,
                        0L,
                        1000000L,
                        False,
                        XA_ATOM,
                        &realtype,
                        &realformat,
                        &length,
                        &bytesafter,
                        (unsigned char **) (&data));
    if (realtype != XA_ATOM || realformat != 32) {
        if (data != NULL)
            XFree ((char *) data);
        return False;
    }

    found = False;
    for (i = 0; i < length; i++) {
        if (data[i] == atom) {
            found = True;
            break;
        }
    }

    if (found == True) {
        for (i=i+1; i<length; i++)
            data[i-1] = data[i];
        XChangeProperty (dpy,
                         root,
                         XIM_Servers,
                         XA_ATOM,
                         32,
                         PropModeReplace,
                         (unsigned char *)data,
                         length-1);
        ret = True;
    }
    else {
        XChangeProperty (dpy,
                         root,
                         XIM_Servers,
                         XA_ATOM,
                         32,
                         PropModePrepend,
                         (unsigned char *)data,
                         0);
        ret = False;
    }
    if (data != NULL)
        XFree ((char *) data);
    return ret;
}


/* XIM protocol methods */
static void *xi18n_setup (Display *dpy, XIMArg *args)
{
    Xi18n i18n_core;
    CARD16 endian = 1;

    if ((i18n_core = (Xi18n) malloc (sizeof (Xi18nCore))) == (Xi18n) NULL)
        return NULL;
    /*endif*/
    
    memset (i18n_core, 0, sizeof (Xi18nCore));

    i18n_core->address.dpy = dpy;

    if (ParseArgs (i18n_core, I18N_OPEN, args) != NULL)
    {
        XFree (i18n_core);
        return NULL;
    }
    /*endif*/
    if (*(char *) &endian)
        i18n_core->address.im_byteOrder = 'l';
    else
        i18n_core->address.im_byteOrder = 'B';
    /*endif*/
    
    /* install IMAttr and ICAttr list in i18n_core */
    _Xi18nInitAttrList (i18n_core);

    /* install IMExtension list in i18n_core */
    _Xi18nInitExtension (i18n_core);

    return i18n_core;
}

static void ReturnSelectionNotify (Xi18n i18n_core, XSelectionRequestEvent *ev)
{
    XEvent event;
    Display *dpy = i18n_core->address.dpy;
    char buf[4096];

    event.type = SelectionNotify;
    event.xselection.requestor = ev->requestor;
    event.xselection.selection = ev->selection;
    event.xselection.target = ev->target;
    event.xselection.time = ev->time;
    event.xselection.property = ev->property;
    if (ev->target == i18n_core->address.Localename)
    {
        snprintf (buf, 4096, "@locale=%s", i18n_core->address.im_locale);
    }
    else if (ev->target == i18n_core->address.Transportname)
    {
        snprintf (buf, 4096, "@transport=%s", i18n_core->address.im_addr);
    }
    /*endif*/
    XChangeProperty (dpy,
                     event.xselection.requestor,
                     ev->target,
                     ev->target,
                     8,
                     PropModeReplace,
                     (unsigned char *) buf,
                     strlen (buf));
    XSendEvent (dpy, event.xselection.requestor, False, NoEventMask, &event);
    XFlush (i18n_core->address.dpy);
}

static Bool WaitXSelectionRequest (Display *dpy,
                                   Window win,
                                   XEvent *ev,
                                   XPointer client_data)
{
    XIMS ims = (XIMS) client_data;
    Xi18n i18n_core = ims->protocol;

    if (((XSelectionRequestEvent *) ev)->selection
        == i18n_core->address.selection)
    {
        ReturnSelectionNotify (i18n_core, (XSelectionRequestEvent *) ev);
        return True;
    }
    /*endif*/
    return False;
}

static Status xi18n_openIM(XIMS ims)
{
    Xi18n i18n_core = ims->protocol;
    Display *dpy = i18n_core->address.dpy;

    if (!CheckIMName (i18n_core)
        ||
        !SetXi18nSelectionOwner (i18n_core)
        ||
        !i18n_core->methods.begin (ims))
    {
        XFree (i18n_core->address.im_name);
        XFree (i18n_core->address.im_locale);
        XFree (i18n_core->address.im_addr);
        XFree (i18n_core);
        return False;
    }
    /*endif*/

    _XRegisterFilterByType (dpy,
                            i18n_core->address.im_window,
                            SelectionRequest,
                            SelectionRequest,
                            WaitXSelectionRequest,
                            (XPointer)ims);
    XFlush(dpy);
    return True;
}

static Status xi18n_closeIM(XIMS ims)
{
    Xi18n i18n_core = ims->protocol;
    Display *dpy = i18n_core->address.dpy;

    DeleteXi18nAtom(i18n_core);
    if (!i18n_core->methods.end (ims))
        return False;
    
    _XUnregisterFilter (dpy,
                        i18n_core->address.im_window,
                        WaitXSelectionRequest,
                        (XPointer)ims);
    XFree (i18n_core->address.im_name);
    XFree (i18n_core->address.im_locale);
    XFree (i18n_core->address.im_addr);
    XFree (i18n_core);
    return True;
}

static char *xi18n_setIMValues (XIMS ims, XIMArg *args)
{
    Xi18n i18n_core = ims->protocol;
    char *ret;

    if ((ret = ParseArgs (i18n_core, I18N_SET, args)) != NULL)
        return ret;
    /*endif*/
    return NULL;
}

static char *xi18n_getIMValues (XIMS ims, XIMArg *args)
{
    Xi18n i18n_core = ims->protocol;
    char *ret;

    if ((ret = ParseArgs (i18n_core, I18N_GET, args)) != NULL)
        return ret;
    /*endif*/
    return NULL;
}

static void EventToWireEvent (XEvent *ev, xEvent *event,
			      CARD16 *serial, Bool byte_swap)
{
    FrameMgr fm;
    extern XimFrameRec wire_keyevent_fr[];
    extern XimFrameRec short_fr[];
    BYTE b;
    CARD16 c16;
    CARD32 c32;

    *serial = (CARD16)(ev->xany.serial >> 16);
    switch (ev->type) {
      case KeyPress:
      case KeyRelease:
	{
	    XKeyEvent *kev = (XKeyEvent*)ev;
	    /* create FrameMgr */
	    fm = FrameMgrInit(wire_keyevent_fr, (char *)(&(event->u)), byte_swap);

	    /* set values */
	    b = (BYTE)kev->type;          FrameMgrPutToken(fm, b);
	    b = (BYTE)kev->keycode;       FrameMgrPutToken(fm, b);
	    c16 = (CARD16)(kev->serial & (unsigned long)0xffff);
					  FrameMgrPutToken(fm, c16);
	    c32 = (CARD32)kev->time;      FrameMgrPutToken(fm, c32);
	    c32 = (CARD32)kev->root;      FrameMgrPutToken(fm, c32);
	    c32 = (CARD32)kev->window;    FrameMgrPutToken(fm, c32);
	    c32 = (CARD32)kev->subwindow; FrameMgrPutToken(fm, c32);
	    c16 = (CARD16)kev->x_root;    FrameMgrPutToken(fm, c16);
	    c16 = (CARD16)kev->y_root;    FrameMgrPutToken(fm, c16);
	    c16 = (CARD16)kev->x;         FrameMgrPutToken(fm, c16);
	    c16 = (CARD16)kev->y;         FrameMgrPutToken(fm, c16);
	    c16 = (CARD16)kev->state;     FrameMgrPutToken(fm, c16);
	    b = (BYTE)kev->same_screen;   FrameMgrPutToken(fm, b);
	}
	break;
      default:
	  /* create FrameMgr */
	  fm = FrameMgrInit(short_fr, (char *)(&(event->u.u.sequenceNumber)),
			    byte_swap);
	  c16 = (CARD16)(ev->xany.serial & (unsigned long)0xffff);
	  FrameMgrPutToken(fm, c16);
	  break;
    }
    /* free FrameMgr */
    FrameMgrFree(fm);
}

static Status xi18n_forwardEvent (XIMS ims, XPointer xp)
{
    Xi18n i18n_core = ims->protocol;
    IMForwardEventStruct *call_data = (IMForwardEventStruct *)xp;
    FrameMgr fm;
    extern XimFrameRec forward_event_fr[];
    register int total_size;
    unsigned char *reply = NULL;
    unsigned char *replyp;
    CARD16 serial;
    int event_size;
    Xi18nClient *client;

    client = (Xi18nClient *) _Xi18nFindClient (i18n_core, call_data->connect_id);

    /* create FrameMgr */
    fm = FrameMgrInit (forward_event_fr,
                       NULL,
                       _Xi18nNeedSwap (i18n_core, call_data->connect_id));

    total_size = FrameMgrGetTotalSize (fm);
    event_size = sizeof (xEvent);
    reply = (unsigned char *) malloc (total_size + event_size);
    if (!reply)
    {
        _Xi18nSendMessage (ims,
                           call_data->connect_id,
                           XIM_ERROR,
                           0,
                           0,
                           0);
        return False;
    }
    /*endif*/
    memset (reply, 0, total_size + event_size);
    FrameMgrSetBuffer (fm, reply);
    replyp = reply;

    call_data->sync_bit = 1; 	/* always sync */
    client->sync = True;

    FrameMgrPutToken (fm, call_data->connect_id);
    FrameMgrPutToken (fm, call_data->icid);
    FrameMgrPutToken (fm, call_data->sync_bit);

    replyp += total_size;
    EventToWireEvent (&(call_data->event),
                      (xEvent *) replyp,
                      &serial,
                      _Xi18nNeedSwap (i18n_core, call_data->connect_id));

    FrameMgrPutToken (fm, serial);

    _Xi18nSendMessage (ims,
                       call_data->connect_id,
                       XIM_FORWARD_EVENT,
                       0,
                       reply,
                       total_size + event_size);

    XFree (reply);
    FrameMgrFree (fm);

    return True;
}

static Status xi18n_commit (XIMS ims, XPointer xp)
{
    Xi18n i18n_core = ims->protocol;
    IMCommitStruct *call_data = (IMCommitStruct *)xp;
    FrameMgr fm;
    extern XimFrameRec commit_chars_fr[];
    extern XimFrameRec commit_both_fr[];
    register int total_size;
    unsigned char *reply = NULL;
    CARD16 str_length;

    call_data->flag |= XimSYNCHRONUS;  /* always sync */

    if (!(call_data->flag & XimLookupKeySym)
        &&
        (call_data->flag & XimLookupChars))
    {
        fm = FrameMgrInit (commit_chars_fr,
                           NULL,
                           _Xi18nNeedSwap (i18n_core, call_data->connect_id));

        /* set length of STRING8 */
        str_length = strlen (call_data->commit_string);
        FrameMgrSetSize (fm, str_length);
        total_size = FrameMgrGetTotalSize (fm);
        reply = (unsigned char *) malloc (total_size);
        if (!reply)
        {
            _Xi18nSendMessage (ims,
                               call_data->connect_id,
                               XIM_ERROR,
                               0,
                               0,
                               0);
            return False;
        }
        /*endif*/
        memset (reply, 0, total_size);
        FrameMgrSetBuffer (fm, reply);

        str_length = FrameMgrGetSize (fm);
        FrameMgrPutToken (fm, call_data->connect_id);
        FrameMgrPutToken (fm, call_data->icid);
        FrameMgrPutToken (fm, call_data->flag);
        FrameMgrPutToken (fm, str_length);
        FrameMgrPutToken (fm, call_data->commit_string);
    }
    else
    {
        fm = FrameMgrInit (commit_both_fr,
                           NULL,
                           _Xi18nNeedSwap (i18n_core, call_data->connect_id));
        /* set length of STRING8 */
        str_length = strlen (call_data->commit_string);
        if (str_length > 0)
            FrameMgrSetSize (fm, str_length);
        /*endif*/
        total_size = FrameMgrGetTotalSize (fm);
        reply = (unsigned char *) malloc (total_size);
        if (!reply)
        {
            _Xi18nSendMessage (ims,
                               call_data->connect_id,
                               XIM_ERROR,
                               0,
                               0,
                               0);
            return False;
        }
        /*endif*/
        FrameMgrSetBuffer (fm, reply);
        FrameMgrPutToken (fm, call_data->connect_id);
        FrameMgrPutToken (fm, call_data->icid);
        FrameMgrPutToken (fm, call_data->flag);
        FrameMgrPutToken (fm, call_data->keysym);
        if (str_length > 0)
        {
            str_length = FrameMgrGetSize (fm);
            FrameMgrPutToken (fm, str_length);
            FrameMgrPutToken (fm, call_data->commit_string);
        }
        /*endif*/
    }
    /*endif*/
    _Xi18nSendMessage (ims,
                       call_data->connect_id,
                       XIM_COMMIT,
                       0,
                       reply,
                       total_size);
    FrameMgrFree (fm);
    XFree (reply);

    return True;
}

static int xi18n_callCallback (XIMS ims, XPointer xp)
{
    IMProtocol *call_data = (IMProtocol *)xp;
    switch (call_data->major_code)
    {
    case XIM_GEOMETRY:
        return _Xi18nGeometryCallback (ims, call_data);

    case XIM_PREEDIT_START:
        return _Xi18nPreeditStartCallback (ims, call_data);

    case XIM_PREEDIT_DRAW:
        return _Xi18nPreeditDrawCallback (ims, call_data);

    case XIM_PREEDIT_CARET:
        return _Xi18nPreeditCaretCallback (ims, call_data);

    case XIM_PREEDIT_DONE:
        return _Xi18nPreeditDoneCallback (ims, call_data);

    case XIM_STATUS_START:
        return _Xi18nStatusStartCallback (ims, call_data);

    case XIM_STATUS_DRAW:
        return _Xi18nStatusDrawCallback (ims, call_data);

    case XIM_STATUS_DONE:
        return _Xi18nStatusDoneCallback (ims, call_data);

    case XIM_STR_CONVERSION:
        return _Xi18nStringConversionCallback (ims, call_data);
    }
    /*endswitch*/
    return False;
}

/* preeditStart and preeditEnd are used only for Dynamic Event Flow. */
static int xi18n_preeditStart (XIMS ims, XPointer xp)
{
    IMProtocol *call_data = (IMProtocol *)xp;
    Xi18n i18n_core = ims->protocol;
    IMPreeditStateStruct *preedit_state =
        (IMPreeditStateStruct *) &call_data->preedit_state;
    long mask;
    int on_key_num = i18n_core->address.on_keys.count_keys;
    int off_key_num = i18n_core->address.off_keys.count_keys;

    if (on_key_num == 0  &&  off_key_num == 0)
        return False;
    /*endif*/
    if (i18n_core->address.imvalue_mask & I18N_FILTERMASK)
        mask = i18n_core->address.filterevent_mask;
    else
        mask = DEFAULT_FILTER_MASK;
    /*endif*/
    _Xi18nSetEventMask (ims,
                        preedit_state->connect_id,
                        preedit_state->connect_id,
                        preedit_state->icid,
                        mask,
                        ~mask);
    return True;
}

static int xi18n_preeditEnd (XIMS ims, XPointer xp)
{
    IMProtocol *call_data = (IMProtocol *)xp;
    Xi18n i18n_core = ims->protocol;
    int on_key_num = i18n_core->address.on_keys.count_keys;
    int off_key_num = i18n_core->address.off_keys.count_keys;
    IMPreeditStateStruct *preedit_state;

    preedit_state = (IMPreeditStateStruct *) &call_data->preedit_state;

    if (on_key_num == 0  &&  off_key_num == 0)
        return False;
    /*endif*/
    
    _Xi18nSetEventMask (ims,
                        preedit_state->connect_id,
                        preedit_state->connect_id,
                        preedit_state->icid,
                        0,
                        0);
    return True;
}

static int xi18n_syncXlib (XIMS ims, XPointer xp)
{
    IMProtocol *call_data = (IMProtocol *)xp;
    Xi18n i18n_core = ims->protocol;
    IMSyncXlibStruct *sync_xlib;

    extern XimFrameRec sync_fr[];
    FrameMgr fm;
    CARD16 connect_id = call_data->any.connect_id;
    int total_size;
    unsigned char *reply;

    sync_xlib = (IMSyncXlibStruct *) &call_data->sync_xlib;
    fm = FrameMgrInit (sync_fr, NULL,
                       _Xi18nNeedSwap (i18n_core, connect_id));
    total_size = FrameMgrGetTotalSize(fm);
    reply = (unsigned char *) malloc (total_size);
    if (!reply) {
        _Xi18nSendMessage (ims, connect_id, XIM_ERROR, 0, 0, 0);
        return False;
    }
    memset (reply, 0, total_size);
    FrameMgrSetBuffer (fm, reply);

    /* input input-method ID */
    FrameMgrPutToken (fm, connect_id);
    /* input input-context ID */
    FrameMgrPutToken (fm, sync_xlib->icid);
    _Xi18nSendMessage (ims, connect_id, XIM_SYNC, 0, reply, total_size);

    FrameMgrFree (fm);
    XFree(reply);
    return True;
}