Back to index

im-sdk  12.3.91
IIIMProtocol.cpp
Go to the documentation of this file.
00001 /*
00002 Copyright 1990-2001 Sun Microsystems, Inc. All Rights Reserved.
00003 
00004 Permission is hereby granted, free of charge, to any person obtaining a
00005 copy of this software and associated documentation files (the
00006 "Software"), to deal in the Software without restriction, including
00007 without limitation the rights to use, copy, modify, merge, publish,
00008 distribute, sublicense, and/or sell copies of the Software, and to
00009 permit persons to whom the Software is furnished to do so, subject to
00010 the following conditions: The above copyright notice and this
00011 permission notice shall be included in all copies or substantial
00012 portions of the Software.
00013 
00014 
00015 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00016 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00017 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00018 IN NO EVENT SHALL THE OPEN GROUP OR SUN MICROSYSTEMS, INC. BE LIABLE
00019 FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
00020 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
00021 THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE EVEN IF
00022 ADVISED IN ADVANCE OF THE POSSIBILITY OF SUCH DAMAGES.
00023 
00024 
00025 Except as contained in this notice, the names of The Open Group and/or
00026 Sun Microsystems, Inc. shall not be used in advertising or otherwise to
00027 promote the sale, use or other dealings in this Software without prior
00028 written authorization from The Open Group and/or Sun Microsystems,
00029 Inc., as applicable.
00030 
00031 
00032 X Window System is a trademark of The Open Group
00033 
00034 OSF/1, OSF/Motif and Motif are registered trademarks, and OSF, the OSF
00035 logo, LBX, X Window System, and Xinerama are trademarks of the Open
00036 Group. All other trademarks and registered trademarks mentioned herein
00037 are the property of their respective owners. No right, title or
00038 interest in or to any trademark, service mark, logo or trade name of
00039 Sun Microsystems, Inc. or its licensors is granted.
00040 
00041 */
00042 #include <stdio.h>
00043 #include "IIIMProtocol.hh"
00044 #include "IIIMPClient.hh"
00045 #include "IMTrans.hh"
00046 #include "IMThread.hh"
00047 #include "CompoundString.hh"
00048 
00049 static void
00050 CountAttrList(const IMListOfAttr *attr, int *total_count) {
00051   *total_count = 0;
00052 
00053   while (attr->name != 0) {
00054     attr++;
00055     ++(*total_count);
00056   }
00057 }
00058 
00059 static IMAttr*
00060 CreateAttrList(const IMListOfAttr *attr, int *total_count) {
00061   IMAttr *args, *p;
00062   unsigned int buf_size;
00063 
00064   CountAttrList(attr, total_count);
00065 
00066   buf_size = (unsigned)(*total_count + 1) * sizeof(IMAttr);
00067   args = new IMAttr[*total_count + 1];
00068 
00069   if (!args) return (IMAttr*)0;
00070 
00071   for (p = args; attr->name != 0; attr++, p++) {
00072     p->name = attr->name;
00073     p->length = CompoundString(attr->name).length();
00074     p->type = attr->type;
00075     p->attribute_id = attr->id;
00076   }
00077   p->name = (char*)0;
00078 
00079   return args;
00080 }
00081 
00082 int
00083 IIIMProtocol::getICAttrIDFromName(const char *name) {
00084   IMAttr *args;
00085   int i;
00086 
00087   args = ic_attr;
00088   CompoundString cname(name);
00089   for (i = 0; i < ic_attr_num; i++) {
00090     if (cname == CompoundString(args[i].name))
00091       return args[i].attribute_id;
00092   }
00093   return (-1); /* not found */
00094 }
00095 
00096 void
00097 IIIMProtocol::init_attrList() {
00098   IMAttr *args;
00099   int total_count;
00100 
00101   /* init IMAttr list */
00102   args = CreateAttrList(Default_IM, &total_count);
00103 
00104   im_attr_num = total_count;
00105   im_attr = (IMAttr *)args;
00106 
00107   /* init IMAttr list */
00108   args = CreateAttrList(Default_IC, &total_count);
00109 
00110   ic_attr_num = total_count;
00111   ic_attr = (IMAttr*)args;
00112 
00113   return;
00114 }
00115 
00116 IIIMProtocol::
00117 IIIMProtocol(IMArg *args, int args_count) : IMProtocol(args, args_count) {
00118   protocol_name = "IIIMP";
00119 
00120   // ICATTRIBUTE names
00121   ICLocaleName = "localeName";
00122   ICCharSubsetsName = "characterSubsets";
00123   ICEngineName = "engineName";
00124 
00125   // IMATTRIBUTE names
00126   IMListName = "inputMethodList";
00127   IMClientName = "clientDescriptor";
00128   IMObjectListName = "objectDescriptorList";
00129   IMCCDEFName = "ccdef";
00130   IMGUIObjectName = "guiObject";
00131   IMLWEObjectName = "lweObject";
00132 
00133   int n = 0;
00134   Default_IM[n].name = (char*)IMListName;
00135   Default_IM[n].type = XimType_UTF16_STRING;
00136   Default_IM[n].id = INPUTMETHOD_LIST;
00137   n++;
00138   Default_IM[n].name = (char*)IMClientName;
00139   Default_IM[n].type = XimType_LISTOF_UTF16_STRING;
00140   Default_IM[n].id = CLIENT_DESCRIPTER;
00141   n++;
00142   Default_IM[n].name = (char*)IMObjectListName;
00143   Default_IM[n].type = XimType_UTF16_STRING;
00144   Default_IM[n].id = OBJECT_DESCRIPTER_LIST;
00145   n++;
00146   Default_IM[n].name = (char*)IMCCDEFName;
00147   Default_IM[n].type = XimType_UTF16_STRING;
00148   Default_IM[n].id = CCDEF;
00149   n++;
00150   Default_IM[n].name = (char*)IMGUIObjectName;
00151   Default_IM[n].type = XimType_UTF16_STRING;
00152   Default_IM[n].id = GUI_OBJECT;
00153   n++;
00154   Default_IM[n].name = (char*)IMLWEObjectName;
00155   Default_IM[n].type = XimType_UTF16_STRING;
00156   Default_IM[n].id = LWE_OBJECT;
00157   n++;
00158   Default_IM[n].name = 0;
00159   Default_IM[n].type = 0;
00160   Default_IM[n].id = 0;
00161 
00162   n = 0;
00163   Default_IC[n].name = (char*)ICLocaleName;
00164   Default_IC[n].type = XimType_UTF16_STRING;
00165   Default_IC[n].id = IC_INPUT_LANGUAGE;
00166   n++;
00167   Default_IC[n].name = (char*)ICEngineName;
00168   Default_IC[n].type = XimType_UTF16_STRING;
00169   Default_IC[n].id = IC_INPUT_METHOD;
00170   n++;
00171   Default_IC[n].name = (char*)ICCharSubsetsName;
00172   Default_IC[n].type = XimType_UTF16_STRING;
00173   Default_IC[n].id = IC_CHARACTER_SUBSETS;
00174   n++;
00175   Default_IC[n].name = 0;
00176   Default_IC[n].type = 0;
00177   Default_IC[n].id = 0;
00178 
00179   unsigned short endian = 1;
00180   if (*(char *)&endian) {
00181     im_byteOrder = 'l';
00182   } else {
00183     im_byteOrder = 'B';
00184   }
00185   /* install IM Attr and IC Attr list  */
00186   init_attrList();
00187 }
00188 
00189 IIIMProtocol::
00190 ~IIIMProtocol() {
00191   delete [] im_attr;
00192   delete [] ic_attr;
00193 }
00194 
00195 int
00196 IIIMProtocol::run() {
00197   try {
00198      trans_listen = new IMTransListen(im_address, protocol_name, 1);
00199   } catch (char *err_msg) {
00200     fprintf(stderr, "Error : %s\n", err_msg);
00201     return -1;
00202   }    
00203 #ifdef linux
00204   // 4354367: Linix Htt: htt_xbe can not start on standalone system
00205   try {
00206      trans_listen_local = new IMTransListen(im_address, protocol_name, 0);
00207   } catch (char *err_msg) {
00208     fprintf(stderr, "Error : %s\n", err_msg);
00209   }
00210 #endif
00211   // open a new thread to accept connection request from IIIMP clients
00212   new IMThread(accept_loop, this);
00213 
00214 #ifdef linux
00215   // 4354367: Linix Htt: htt_xbe can not start on standalone system
00216   new IMThread(accept_loop_local, this);
00217 #endif
00218 
00219   return 0;
00220 }
00221 
00222 int
00223 IIIMProtocol::set_values(IMArg *args, int n) {
00224   return IMProtocol::set_values(args, n);
00225 }
00226 
00227 void *
00228 IIIMProtocol::accept_loop(void *client_data) {
00229   IIIMProtocol *iiimp_protocol = (IIIMProtocol*)client_data;
00230   IMTransListen *trans_listen = iiimp_protocol->trans_listen;
00231   while (1) {
00232     try {
00233       IMTransAccept *ss = iiimp_protocol->getIMTransAccept(trans_listen);
00234       if (ss) {
00235        IIIMPClient *client = new IIIMPClient(iiimp_protocol, ss);
00236        iiimp_protocol->iiimp_client_list.addItem(client);
00237       }
00238     } catch(const char* p) {
00239       // cerr << p << endl;
00240       return 0;
00241     }
00242   }
00243   // unreachable
00244   return 0;
00245 }
00246 
00247 #ifdef linux
00248   // 4354367: Linix Htt: htt_xbe can not start on standalone system
00249 void *
00250 IIIMProtocol::accept_loop_local(void *client_data) {
00251   IIIMProtocol *iiimp_protocol = (IIIMProtocol*)client_data;
00252   IMTransListen *trans_listen = iiimp_protocol->trans_listen_local;
00253   while (1) {
00254     try {
00255       IMTransAccept *ss = iiimp_protocol->getIMTransAccept(trans_listen);
00256       if (ss) {
00257        IIIMPClient *client = new IIIMPClient(iiimp_protocol, ss);
00258        iiimp_protocol->iiimp_client_list.addItem(client);
00259       }
00260     } catch(const char* p) {
00261       // cerr << p << endl;
00262       return 0;
00263     }
00264   }
00265   // unreachable
00266   return 0;
00267 }
00268 #endif