Back to index

im-sdk  12.3.91
i18nUtil.c
Go to the documentation of this file.
00001 /******************************************************************
00002  
00003          Copyright 1994, 1995 by Sun Microsystems, Inc.
00004          Copyright 1993, 1994 by Hewlett-Packard Company
00005  
00006 Permission to use, copy, modify, distribute, and sell this software
00007 and its documentation for any purpose is hereby granted without fee,
00008 provided that the above copyright notice appear in all copies and
00009 that both that copyright notice and this permission notice appear
00010 in supporting documentation, and that the name of Sun Microsystems, Inc.
00011 and Hewlett-Packard not be used in advertising or publicity pertaining to
00012 distribution of the software without specific, written prior permission.
00013 Sun Microsystems, Inc. and Hewlett-Packard make no representations about
00014 the suitability of this software for any purpose.  It is provided "as is"
00015 without express or implied warranty.
00016  
00017 SUN MICROSYSTEMS INC. AND HEWLETT-PACKARD COMPANY DISCLAIMS ALL
00018 WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED
00019 WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
00020 SUN MICROSYSTEMS, INC. AND HEWLETT-PACKARD COMPANY BE LIABLE FOR ANY
00021 SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
00022 RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
00023 CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
00024 IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
00025  
00026   Author: Hidetoshi Tajima(tajima@Eng.Sun.COM) Sun Microsystems, Inc.
00027  
00028 ******************************************************************/
00029 #include <X11/Xlib.h>
00030 #include "X11R6IMProtoData.h"
00031 #include "Xi18n.h"
00032 #include "FrameMgr.h"
00033 
00034 Xi18nClient *_Xi18nFindClient(Xi18n, CARD16);
00035 
00036 int
00037 _Xi18nNeedSwap(Xi18n i18n_core, CARD16 connect_id)
00038 {
00039     CARD8 im_byteOrder = i18n_core->address.im_byteOrder;
00040     Xi18nClient *client = _Xi18nFindClient(i18n_core, connect_id);
00041     
00042     return (client->byte_order != im_byteOrder);
00043 }
00044 
00045 Xi18nClient *
00046 _Xi18nNewClient(Xi18n i18n_core)
00047 {
00048     static CARD16 connect_id = 0;
00049     Xi18nClient *client;
00050 
00051     if (i18n_core->address.free_clients != NULL) {
00052        client = i18n_core->address.free_clients;
00053        i18n_core->address.free_clients = client->next;
00054     } else {
00055        client = (Xi18nClient *)malloc(sizeof(Xi18nClient));
00056     }
00057     memset(client, 0, sizeof(Xi18nClient));
00058     client->connect_id = ++connect_id;
00059     client->pending = (XIMPending *)NULL;
00060     client->sync = False;
00061     client->byte_order = '?';      /* initial value */
00062     memset(&client->pending, 0, sizeof(XIMPending *));
00063     client->next = i18n_core->address.clients;
00064     i18n_core->address.clients = client;
00065 
00066     return (Xi18nClient *)client;
00067 }
00068 
00069 Xi18nClient *
00070 _Xi18nFindClient(Xi18n i18n_core, CARD16 connect_id)
00071 {
00072     Xi18nClient *client = i18n_core->address.clients;
00073 
00074     while (client != NULL) {
00075        if (client->connect_id == connect_id)
00076          return client;
00077        client = client->next;
00078     }
00079     return NULL;
00080 }
00081 
00082 void
00083 _Xi18nDeleteClient(Xi18n i18n_core, CARD16 connect_id)
00084 {
00085     Xi18nClient *target = _Xi18nFindClient(i18n_core, connect_id);
00086     Xi18nClient *ccp, *ccp0;
00087 
00088     for (ccp = i18n_core->address.clients, ccp0 = NULL;
00089         ccp != NULL;
00090         ccp0 = ccp, ccp = ccp->next) {
00091        if (ccp == target) {
00092            if (ccp0 == NULL) {
00093               i18n_core->address.clients = ccp->next;
00094            } else {
00095               ccp0->next = ccp->next;
00096            }
00097            /* put it back to free list */
00098            target->next = i18n_core->address.free_clients;
00099            i18n_core->address.free_clients = target;
00100            return;
00101        }
00102     }
00103     return;
00104 }
00105 
00106 void
00107 _Xi18nSendMessage(XIMS ims, CARD16 connect_id,
00108                 CARD8 major_opcode, CARD8 minor_opcode,
00109                 unsigned char *data, long length)
00110 {
00111     Xi18n i18n_core = ims->protocol;
00112     FrameMgr fm;
00113     extern XimFrameRec packet_header_fr[];
00114     unsigned char *reply_hdr = NULL;
00115     int header_size;
00116     unsigned char *reply = NULL;
00117     unsigned char *replyp;
00118     int reply_length;
00119     long p_len = length/4;
00120 
00121     /* create FrameMgr */
00122     fm = FrameMgrInit(packet_header_fr, NULL,
00123                     _Xi18nNeedSwap(i18n_core, connect_id));
00124 
00125     header_size = FrameMgrGetTotalSize(fm);
00126     reply_hdr = (unsigned char *)malloc(header_size);
00127     if (!reply_hdr) {
00128        _Xi18nSendMessage(ims, connect_id, XIM_ERROR, 0, 0, 0);
00129        return;
00130     }
00131     FrameMgrSetBuffer(fm, reply_hdr);
00132 
00133     /* put data */
00134     FrameMgrPutToken(fm, major_opcode);
00135     FrameMgrPutToken(fm, minor_opcode);
00136     FrameMgrPutToken(fm, p_len);
00137 
00138     reply_length = header_size + length;
00139     reply = (unsigned char *)malloc(reply_length);
00140     replyp = reply;
00141     memmove(reply, reply_hdr, header_size);
00142     replyp += header_size;
00143     memmove(replyp, data, length);
00144 
00145     i18n_core->methods.send(ims, connect_id, reply, reply_length);
00146 
00147     XFree(reply_hdr);
00148     XFree(reply);
00149     /* free FrameMgr */
00150     FrameMgrFree(fm);
00151 
00152     return;
00153 }
00154 
00155 void
00156 _Xi18nSendTriggerKey(XIMS ims, CARD16 connect_id)
00157 {
00158     Xi18n i18n_core = ims->protocol;
00159     FrameMgr fm;
00160     extern XimFrameRec register_triggerkeys_fr[];
00161     XIMTriggerKey *on_keys = i18n_core->address.on_keys.keylist;
00162     XIMTriggerKey *off_keys = i18n_core->address.off_keys.keylist;
00163     int on_key_num = i18n_core->address.on_keys.count_keys;
00164     int off_key_num = i18n_core->address.off_keys.count_keys;
00165     unsigned char *reply = NULL;
00166     register int i, total_size;
00167     CARD16 im_id;
00168 
00169     if (on_key_num == 0 && off_key_num == 0)
00170       return;
00171 
00172     /* create FrameMgr */
00173     fm = FrameMgrInit(register_triggerkeys_fr, NULL,
00174                     _Xi18nNeedSwap(i18n_core, connect_id));
00175 
00176     /* set iteration count for on-keys list */
00177     FrameMgrSetIterCount(fm, on_key_num);
00178     /* set iteration count for off-keys list */
00179     FrameMgrSetIterCount(fm, off_key_num);
00180 
00181     /* get total_size */
00182     total_size = FrameMgrGetTotalSize(fm);
00183 
00184     reply = (unsigned char *)malloc(total_size);
00185     if (!reply) {
00186        return;
00187     }
00188     memset(reply, 0, total_size);
00189     FrameMgrSetBuffer(fm, reply);
00190 
00191     /* Right now XIM_OPEN_REPLY hasn't been sent to this new client, so
00192        the input-method-id is still invalid, and should be set to zero...
00193        Reter to $(XC)/lib/X11/imDefLkup.c:_XimRegisterTriggerKeysCallback
00194      */
00195     im_id = 0;
00196     FrameMgrPutToken(fm, im_id); /* input-method-id */
00197     for (i = 0; i < on_key_num; i++) {
00198        FrameMgrPutToken(fm, on_keys[i].keysym);
00199        FrameMgrPutToken(fm, on_keys[i].modifier);
00200        FrameMgrPutToken(fm, on_keys[i].modifier_mask);
00201     }
00202     for (i = 0; i < off_key_num; i++) {
00203        FrameMgrPutToken(fm, off_keys[i].keysym);
00204        FrameMgrPutToken(fm, off_keys[i].modifier);
00205        FrameMgrPutToken(fm, off_keys[i].modifier_mask);
00206     }
00207 
00208     _Xi18nSendMessage(ims, connect_id, XIM_REGISTER_TRIGGERKEYS, 0,
00209                     reply, total_size);
00210 
00211     XFree(reply);
00212     /* free FrameMgr */
00213     FrameMgrFree(fm);
00214 
00215     return;
00216 }
00217 
00218 void
00219 _Xi18nSetEventMask(XIMS ims, CARD16 connect_id,
00220                  CARD16 im_id, CARD16 ic_id,
00221                  CARD32 forward_mask, CARD32 sync_mask)
00222 {
00223     Xi18n i18n_core = ims->protocol;
00224     FrameMgr fm;
00225     extern XimFrameRec set_event_mask_fr[];
00226     unsigned char *reply = NULL;
00227     register int total_size;
00228 
00229     /* create FrameMgr */
00230     fm = FrameMgrInit(set_event_mask_fr, NULL,
00231                     _Xi18nNeedSwap(i18n_core, connect_id));
00232 
00233     total_size = FrameMgrGetTotalSize(fm);
00234     reply = (unsigned char *)malloc(total_size);
00235     if (!reply) {
00236        return;
00237     }
00238     memset(reply, 0, total_size);
00239     FrameMgrSetBuffer(fm, reply);
00240 
00241     FrameMgrPutToken(fm, im_id);   /* input-method-id */
00242     FrameMgrPutToken(fm, ic_id);   /* input-context-id */
00243     FrameMgrPutToken(fm, forward_mask);
00244     FrameMgrPutToken(fm, sync_mask);
00245 
00246     _Xi18nSendMessage(ims, connect_id,
00247                     XIM_SET_EVENT_MASK, 0, reply, total_size);
00248 
00249     XFree(reply);
00250     /* free FrameMgr */
00251     FrameMgrFree(fm);
00252 
00253     return;
00254 }