summaryrefslogtreecommitdiff
path: root/IMdkit/i18nUtil.c
blob: c07de481a04c2642427c5cc7c54729ea41f1e2b0 (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
/******************************************************************
 
         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 <X11/Xlib.h>
#include "IMdkit.h"
#include "Xi18n.h"
#include "FrameMgr.h"
#include "XimFunc.h"

Xi18nClient *_Xi18nFindClient (Xi18n, CARD16);

int
_Xi18nNeedSwap (Xi18n i18n_core, CARD16 connect_id)
{
    CARD8 im_byteOrder = i18n_core->address.im_byteOrder;
    Xi18nClient *client = _Xi18nFindClient (i18n_core, connect_id);

    return (client->byte_order != im_byteOrder);
}

Xi18nClient *_Xi18nNewClient(Xi18n i18n_core)
{
    static CARD16 connect_id = 0;
    int new_connect_id;
    Xi18nClient *client;

    if (i18n_core->address.free_clients)
    {
        client = i18n_core->address.free_clients;
        i18n_core->address.free_clients = client->next;
	new_connect_id = client->connect_id;
    }
    else
    {
        client = (Xi18nClient *) malloc (sizeof (Xi18nClient));
	new_connect_id = ++connect_id;
    }
    /*endif*/
    memset (client, 0, sizeof (Xi18nClient));
    client->connect_id = new_connect_id;
    client->pending = (XIMPending *) NULL;
    client->sync = False;
    client->byte_order = '?'; 	/* initial value */
    memset (&client->pending, 0, sizeof (XIMPending *));
    client->property_offset = 0;
    client->next = i18n_core->address.clients;
    i18n_core->address.clients = client;

    return (Xi18nClient *) client;
}

Xi18nClient *_Xi18nFindClient (Xi18n i18n_core, CARD16 connect_id)
{
    Xi18nClient *client = i18n_core->address.clients;

    while (client)
    {
        if (client->connect_id == connect_id)
            return client;
        /*endif*/
        client = client->next;
    }
    /*endwhile*/
    return NULL;
}

void _Xi18nDeleteClient (Xi18n i18n_core, CARD16 connect_id)
{
    Xi18nClient *target = _Xi18nFindClient (i18n_core, connect_id);
    Xi18nClient *ccp;
    Xi18nClient *ccp0;

    for (ccp = i18n_core->address.clients, ccp0 = NULL;
         ccp != NULL;
         ccp0 = ccp, ccp = ccp->next)
    {
        if (ccp == target)
        {
            if (ccp0 == NULL)
                i18n_core->address.clients = ccp->next;
            else
                ccp0->next = ccp->next;
            /*endif*/
            /* put it back to free list */
            target->next = i18n_core->address.free_clients;
            i18n_core->address.free_clients = target;
            return;
        }
        /*endif*/
    }
    /*endfor*/
}

void _Xi18nSendMessage (XIMS ims,
                        CARD16 connect_id,
                        CARD8 major_opcode,
                        CARD8 minor_opcode,
                        unsigned char *data,
                        long length)
{
    Xi18n i18n_core = ims->protocol;
    FrameMgr fm;
    extern XimFrameRec packet_header_fr[];
    unsigned char *reply_hdr = NULL;
    int header_size;
    unsigned char *reply = NULL;
    unsigned char *replyp;
    int reply_length;
    long p_len = length/4;

    fm = FrameMgrInit (packet_header_fr,
                       NULL,
                       _Xi18nNeedSwap (i18n_core, connect_id));

    header_size = FrameMgrGetTotalSize (fm);
    reply_hdr = (unsigned char *) malloc (header_size);
    if (reply_hdr == NULL)
    {
        _Xi18nSendMessage (ims, connect_id, XIM_ERROR, 0, 0, 0);
        return;
    }
    /*endif*/
    FrameMgrSetBuffer (fm, reply_hdr);

    /* put data */
    FrameMgrPutToken (fm, major_opcode);
    FrameMgrPutToken (fm, minor_opcode);
    FrameMgrPutToken (fm, p_len);

    reply_length = header_size + length;
    reply = (unsigned char *) malloc (reply_length);
    replyp = reply;
    memmove (reply, reply_hdr, header_size);
    replyp += header_size;
    memmove (replyp, data, length);

    i18n_core->methods.send (ims, connect_id, reply, reply_length);

    XFree (reply);
    XFree (reply_hdr);
    FrameMgrFree (fm);
}

void _Xi18nSendTriggerKey (XIMS ims, CARD16 connect_id)
{
    Xi18n i18n_core = ims->protocol;
    FrameMgr fm;
    extern XimFrameRec register_triggerkeys_fr[];
    XIMTriggerKey *on_keys = i18n_core->address.on_keys.keylist;
    XIMTriggerKey *off_keys = i18n_core->address.off_keys.keylist;
    int on_key_num = i18n_core->address.on_keys.count_keys;
    int off_key_num = i18n_core->address.off_keys.count_keys;
    unsigned char *reply = NULL;
    register int i, total_size;
    CARD16 im_id;

    if (on_key_num == 0  &&  off_key_num == 0)
        return;
    /*endif*/
    
    fm = FrameMgrInit (register_triggerkeys_fr,
                       NULL,
                       _Xi18nNeedSwap (i18n_core, connect_id));

    /* set iteration count for on-keys list */
    FrameMgrSetIterCount (fm, on_key_num);
    /* set iteration count for off-keys list */
    FrameMgrSetIterCount (fm, off_key_num);

    /* get total_size */
    total_size = FrameMgrGetTotalSize (fm);

    reply = (unsigned char *) malloc (total_size);
    if (!reply)
        return;
    /*endif*/
    memset (reply, 0, total_size);
    FrameMgrSetBuffer (fm, reply);

    /* Right now XIM_OPEN_REPLY hasn't been sent to this new client, so
       the input-method-id is still invalid, and should be set to zero...
       Reter to $(XC)/lib/X11/imDefLkup.c:_XimRegisterTriggerKeysCallback
     */
    im_id = 0;
    FrameMgrPutToken (fm, im_id);  /* input-method-id */
    for (i = 0;  i < on_key_num;  i++)
    {
        FrameMgrPutToken (fm, on_keys[i].keysym);
        FrameMgrPutToken (fm, on_keys[i].modifier);
        FrameMgrPutToken (fm, on_keys[i].modifier_mask);
    }
    /*endfor*/
    for (i = 0;  i < off_key_num;  i++)
    {
        FrameMgrPutToken (fm, off_keys[i].keysym);
        FrameMgrPutToken (fm, off_keys[i].modifier);
        FrameMgrPutToken (fm, off_keys[i].modifier_mask);
    }
    /*endfor*/
    _Xi18nSendMessage (ims,
                       connect_id,
                       XIM_REGISTER_TRIGGERKEYS,
                       0,
                       reply,
                       total_size);
    FrameMgrFree (fm);
    XFree(reply);
}

void _Xi18nSetEventMask (XIMS ims,
                         CARD16 connect_id,
                         CARD16 im_id,
                         CARD16 ic_id,
                         CARD32 forward_mask,
                         CARD32 sync_mask)
{
    Xi18n i18n_core = ims->protocol;
    FrameMgr fm;
    extern XimFrameRec set_event_mask_fr[];
    unsigned char *reply = NULL;
    register int total_size;

    fm = FrameMgrInit (set_event_mask_fr,
                       NULL,
                       _Xi18nNeedSwap (i18n_core, connect_id));

    total_size = FrameMgrGetTotalSize (fm);
    reply = (unsigned char *) malloc (total_size);
    if (!reply)
        return;
    /*endif*/
    memset (reply, 0, total_size);
    FrameMgrSetBuffer (fm, reply);

    FrameMgrPutToken (fm, im_id); 	/* input-method-id */
    FrameMgrPutToken (fm, ic_id); 	/* input-context-id */
    FrameMgrPutToken (fm, forward_mask);
    FrameMgrPutToken (fm, sync_mask);

    _Xi18nSendMessage (ims,
                       connect_id,
                       XIM_SET_EVENT_MASK,
                       0,
                       reply,
                       total_size);

    FrameMgrFree (fm);
    XFree(reply);
}