Tizen 2.1 base
[platform/core/uifw/ise-engine-sunpinyin.git] / wrapper / xim / IMdkit / i18nUtil.c
1 /******************************************************************
2  
3          Copyright 1994, 1995 by Sun Microsystems, Inc.
4          Copyright 1993, 1994 by Hewlett-Packard Company
5  
6 Permission to use, copy, modify, distribute, and sell this software
7 and its documentation for any purpose is hereby granted without fee,
8 provided that the above copyright notice appear in all copies and
9 that both that copyright notice and this permission notice appear
10 in supporting documentation, and that the name of Sun Microsystems, Inc.
11 and Hewlett-Packard not be used in advertising or publicity pertaining to
12 distribution of the software without specific, written prior permission.
13 Sun Microsystems, Inc. and Hewlett-Packard make no representations about
14 the suitability of this software for any purpose.  It is provided "as is"
15 without express or implied warranty.
16  
17 SUN MICROSYSTEMS INC. AND HEWLETT-PACKARD COMPANY DISCLAIMS ALL
18 WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
20 SUN MICROSYSTEMS, INC. AND HEWLETT-PACKARD COMPANY BE LIABLE FOR ANY
21 SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
22 RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
23 CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
24 IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
25  
26   Author: Hidetoshi Tajima(tajima@Eng.Sun.COM) Sun Microsystems, Inc.
27
28     This version tidied and debugged by Steve Underwood May 1999
29  
30 ******************************************************************/
31
32 #include <X11/Xlib.h>
33 #include "IMdkit.h"
34 #include "Xi18n.h"
35 #include "FrameMgr.h"
36 #include "XimFunc.h"
37
38 Xi18nClient *_Xi18nFindClient (Xi18n, CARD16);
39
40 int
41 _Xi18nNeedSwap (Xi18n i18n_core, CARD16 connect_id)
42 {
43     CARD8 im_byteOrder = i18n_core->address.im_byteOrder;
44     Xi18nClient *client = _Xi18nFindClient (i18n_core, connect_id);
45
46     return (client->byte_order != im_byteOrder);
47 }
48
49 Xi18nClient *_Xi18nNewClient(Xi18n i18n_core)
50 {
51     static CARD16 connect_id = 0;
52     int new_connect_id;
53     Xi18nClient *client;
54
55     if (i18n_core->address.free_clients)
56     {
57         client = i18n_core->address.free_clients;
58         i18n_core->address.free_clients = client->next;
59         new_connect_id = client->connect_id;
60     }
61     else
62     {
63         client = (Xi18nClient *) malloc (sizeof (Xi18nClient));
64         new_connect_id = ++connect_id;
65     }
66     /*endif*/
67     memset (client, 0, sizeof (Xi18nClient));
68     client->connect_id = new_connect_id;
69     client->pending = (XIMPending *) NULL;
70     client->sync = False;
71     client->byte_order = '?';   /* initial value */
72     memset (&client->pending, 0, sizeof (XIMPending *));
73     client->property_offset = 0;
74     client->next = i18n_core->address.clients;
75     i18n_core->address.clients = client;
76
77     return (Xi18nClient *) client;
78 }
79
80 Xi18nClient *_Xi18nFindClient (Xi18n i18n_core, CARD16 connect_id)
81 {
82     Xi18nClient *client = i18n_core->address.clients;
83
84     while (client)
85     {
86         if (client->connect_id == connect_id)
87             return client;
88         /*endif*/
89         client = client->next;
90     }
91     /*endwhile*/
92     return NULL;
93 }
94
95 void _Xi18nDeleteClient (Xi18n i18n_core, CARD16 connect_id)
96 {
97     Xi18nClient *target = _Xi18nFindClient (i18n_core, connect_id);
98     Xi18nClient *ccp;
99     Xi18nClient *ccp0;
100
101     for (ccp = i18n_core->address.clients, ccp0 = NULL;
102          ccp != NULL;
103          ccp0 = ccp, ccp = ccp->next)
104     {
105         if (ccp == target)
106         {
107             if (ccp0 == NULL)
108                 i18n_core->address.clients = ccp->next;
109             else
110                 ccp0->next = ccp->next;
111             /*endif*/
112             /* put it back to free list */
113             target->next = i18n_core->address.free_clients;
114             i18n_core->address.free_clients = target;
115             return;
116         }
117         /*endif*/
118     }
119     /*endfor*/
120 }
121
122 void _Xi18nSendMessage (XIMS ims,
123                         CARD16 connect_id,
124                         CARD8 major_opcode,
125                         CARD8 minor_opcode,
126                         unsigned char *data,
127                         long length)
128 {
129     Xi18n i18n_core = ims->protocol;
130     FrameMgr fm;
131     extern XimFrameRec packet_header_fr[];
132     unsigned char *reply_hdr = NULL;
133     int header_size;
134     unsigned char *reply = NULL;
135     unsigned char *replyp;
136     int reply_length;
137     long p_len = length/4;
138
139     fm = FrameMgrInit (packet_header_fr,
140                        NULL,
141                        _Xi18nNeedSwap (i18n_core, connect_id));
142
143     header_size = FrameMgrGetTotalSize (fm);
144     reply_hdr = (unsigned char *) malloc (header_size);
145     if (reply_hdr == NULL)
146     {
147         _Xi18nSendMessage (ims, connect_id, XIM_ERROR, 0, 0, 0);
148         return;
149     }
150     /*endif*/
151     FrameMgrSetBuffer (fm, reply_hdr);
152
153     /* put data */
154     FrameMgrPutToken (fm, major_opcode);
155     FrameMgrPutToken (fm, minor_opcode);
156     FrameMgrPutToken (fm, p_len);
157
158     reply_length = header_size + length;
159     reply = (unsigned char *) malloc (reply_length);
160     replyp = reply;
161     memmove (reply, reply_hdr, header_size);
162     replyp += header_size;
163     memmove (replyp, data, length);
164
165     i18n_core->methods.send (ims, connect_id, reply, reply_length);
166
167     XFree (reply);
168     XFree (reply_hdr);
169     FrameMgrFree (fm);
170 }
171
172 void _Xi18nSendTriggerKey (XIMS ims, CARD16 connect_id)
173 {
174     Xi18n i18n_core = ims->protocol;
175     FrameMgr fm;
176     extern XimFrameRec register_triggerkeys_fr[];
177     XIMTriggerKey *on_keys = i18n_core->address.on_keys.keylist;
178     XIMTriggerKey *off_keys = i18n_core->address.off_keys.keylist;
179     int on_key_num = i18n_core->address.on_keys.count_keys;
180     int off_key_num = i18n_core->address.off_keys.count_keys;
181     unsigned char *reply = NULL;
182     register int i, total_size;
183     CARD16 im_id;
184
185     if (on_key_num == 0  &&  off_key_num == 0)
186         return;
187     /*endif*/
188     
189     fm = FrameMgrInit (register_triggerkeys_fr,
190                        NULL,
191                        _Xi18nNeedSwap (i18n_core, connect_id));
192
193     /* set iteration count for on-keys list */
194     FrameMgrSetIterCount (fm, on_key_num);
195     /* set iteration count for off-keys list */
196     FrameMgrSetIterCount (fm, off_key_num);
197
198     /* get total_size */
199     total_size = FrameMgrGetTotalSize (fm);
200
201     reply = (unsigned char *) malloc (total_size);
202     if (!reply)
203         return;
204     /*endif*/
205     memset (reply, 0, total_size);
206     FrameMgrSetBuffer (fm, reply);
207
208     /* Right now XIM_OPEN_REPLY hasn't been sent to this new client, so
209        the input-method-id is still invalid, and should be set to zero...
210        Reter to $(XC)/lib/X11/imDefLkup.c:_XimRegisterTriggerKeysCallback
211      */
212     im_id = 0;
213     FrameMgrPutToken (fm, im_id);  /* input-method-id */
214     for (i = 0;  i < on_key_num;  i++)
215     {
216         FrameMgrPutToken (fm, on_keys[i].keysym);
217         FrameMgrPutToken (fm, on_keys[i].modifier);
218         FrameMgrPutToken (fm, on_keys[i].modifier_mask);
219     }
220     /*endfor*/
221     for (i = 0;  i < off_key_num;  i++)
222     {
223         FrameMgrPutToken (fm, off_keys[i].keysym);
224         FrameMgrPutToken (fm, off_keys[i].modifier);
225         FrameMgrPutToken (fm, off_keys[i].modifier_mask);
226     }
227     /*endfor*/
228     _Xi18nSendMessage (ims,
229                        connect_id,
230                        XIM_REGISTER_TRIGGERKEYS,
231                        0,
232                        reply,
233                        total_size);
234     FrameMgrFree (fm);
235     XFree(reply);
236 }
237
238 void _Xi18nSetEventMask (XIMS ims,
239                          CARD16 connect_id,
240                          CARD16 im_id,
241                          CARD16 ic_id,
242                          CARD32 forward_mask,
243                          CARD32 sync_mask)
244 {
245     Xi18n i18n_core = ims->protocol;
246     FrameMgr fm;
247     extern XimFrameRec set_event_mask_fr[];
248     unsigned char *reply = NULL;
249     register int total_size;
250
251     fm = FrameMgrInit (set_event_mask_fr,
252                        NULL,
253                        _Xi18nNeedSwap (i18n_core, connect_id));
254
255     total_size = FrameMgrGetTotalSize (fm);
256     reply = (unsigned char *) malloc (total_size);
257     if (!reply)
258         return;
259     /*endif*/
260     memset (reply, 0, total_size);
261     FrameMgrSetBuffer (fm, reply);
262
263     FrameMgrPutToken (fm, im_id);       /* input-method-id */
264     FrameMgrPutToken (fm, ic_id);       /* input-context-id */
265     FrameMgrPutToken (fm, forward_mask);
266     FrameMgrPutToken (fm, sync_mask);
267
268     _Xi18nSendMessage (ims,
269                        connect_id,
270                        XIM_SET_EVENT_MASK,
271                        0,
272                        reply,
273                        total_size);
274
275     FrameMgrFree (fm);
276     XFree(reply);
277 }