Back to index

im-sdk  12.3.91
IIIMPClientDispatcher.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 #if !defined(USE_FRAMEMGR_ALWAYS)
00043 #else /* USE_FRAMEMGR_ALWAYS */
00044 #include "FrameMgr.h"
00045 #endif /* USE_FRAMEMGR_ALWAYS */
00046 #include "IIIMPClient.hh"
00047 #include "IMProtoHandler.hh"
00048 #include "IIIMPInputContext.hh"
00049 #include "InputContext.hh"
00050 #include "IMProtocolStruct.h"
00051 #include "IIIMProtocol.hh"
00052 #include "IMTrans.hh"
00053 
00054 #if !defined(USE_FRAMEMGR_ALWAYS)
00055 int
00056 IIIMPClient::string_list_count2(char *p, int str_list_size) {
00057   char *ptr;
00058   int num;
00059   CARD16 str_size;
00060 
00061   num = 0;
00062   ptr = p;
00063 
00064   if (str_list_size & 0x03) {
00065     str_list_size &= (~0x03);
00066   }
00067 
00068   if (False == need_swap) {
00069     while (4 <= str_list_size) {
00070       req_get16(ptr, str_size);
00071       str_list_size -= 2;
00072       if ((str_size + 2) & 0x03) { /* padding */
00073        str_size = (((str_size + 2) & (~0x03)) + 2);
00074       }
00075       if (str_list_size < str_size) {
00076        str_size = str_list_size;
00077       }
00078 
00079       ptr += str_size;
00080       str_list_size -= str_size;
00081       num += 1;
00082     }
00083   } else {
00084     while (4 <= str_list_size) {
00085       req_get16s(ptr, str_size);
00086       str_list_size -= 2;
00087       if ((str_size + 2) & 0x03) { /* padding */
00088        str_size = (((str_size + 2) & (~0x03)) + 2);
00089       }
00090       if (str_list_size < str_size) {
00091        str_size = str_list_size;
00092       }
00093 
00094       ptr += str_size;
00095       str_list_size -= str_size;
00096       num += 1;
00097     }
00098   }
00099   return num;
00100 }
00101 
00102 char *
00103 IIIMPClient::req_get_ascii_string(
00104        char * ptr, char ** string, int * size_ret, int * buf_size_ret) {
00105   char *p;
00106   int size;
00107   int len;
00108   int i;
00109 
00110   req_get16(ptr, size);
00111   if (NULL != size_ret) {
00112     *size_ret = size;
00113   }
00114 
00115   len = (size / 2);
00116   p = new char[len + 1];
00117   if (NULL != buf_size_ret) {
00118     *buf_size_ret = (len + 1);
00119   }
00120 
00121   for (i = 0; i < len; i++) {
00122     req_get16(ptr, *(p + i));
00123   }
00124   if ((size + 2) & 0x03) {  /* skip padding */
00125     ptr += (4 - ((size + 2) & 0x03));
00126   }
00127   *(p + len) = '\0';
00128 
00129   *string = p;
00130 
00131   return ptr;
00132 }
00133 
00134 char *
00135 IIIMPClient::req_get_ascii_string_s(
00136        char * ptr, char ** string, int * size_ret, int * buf_size_ret) {
00137   char *p;
00138   int size;
00139   int len;
00140   int i;
00141 
00142   req_get16s(ptr, size);
00143   if (NULL != size_ret) {
00144     *size_ret = size;
00145   }
00146 
00147   len = (size / 2);
00148   p = new char[len + 1];
00149   if (NULL != buf_size_ret) {
00150     *buf_size_ret = (len + 1);
00151   }
00152 
00153   for (i = 0; i < len; i++) {
00154     req_get16s(ptr, *(p + i));
00155   }
00156   if ((size + 2) & 0x03) {  /* skip padding */
00157     ptr += (4 - ((size + 2) & 0x03));
00158   }
00159   *(p + len) = '\0';
00160 
00161   *string = p;
00162 
00163   return ptr;
00164 }
00165 
00166 #else /* USE_FRAMEMGR_ALWAYS */
00167 #endif /* USE_FRAMEMGR_ALWAYS */
00168 
00169 void
00170 IIIMPClient::connect(IMProtocolStruct *call_data, unsigned char *p) {
00171 #if !defined(USE_FRAMEMGR_ALWAYS)
00172   char *data;
00173   char *ptr;
00174   int language_num;
00175   int total_size;
00176   int string_list_size;
00177 #else /* USE_FRAMEMGR_ALWAYS */
00178   FrameMgr fm;
00179   extern XimFrameRec im_connect_fr[], im_connect_reply_fr[];
00180 #endif /* USE_FRAMEMGR_ALWAYS */
00181   CARD8 byte_order;
00182   CARD8 protocol_version;
00183   CARD16 byte_length;
00184 
00185   CARD16 *attributes = (CARD16*)0;
00186 #if !defined(USE_FRAMEMGR_ALWAYS)
00187   int str_length = 0;
00188 #else /* USE_FRAMEMGR_ALWAYS */
00189   CARD16 str_length = 0;
00190 #endif /* USE_FRAMEMGR_ALWAYS */
00191 
00192 #if !defined(USE_FRAMEMGR_ALWAYS)
00193   ptr = (char *)p;
00194 
00195   if (False == need_swap) {
00196     req_get8(ptr, byte_order);
00197     req_get8(ptr, protocol_version);
00198     ptr = req_get_ascii_string(ptr, &data, &str_length, NULL);
00199     if ((0 == str_length) || (str_length & 0x01)) {
00200       delete [] data;
00201       return; /* something wrong */
00202     }
00203     username = data;
00204     // 'data' was copied to username by CompoundString's copy constructor.
00205     // so can be deleted.
00206     delete [] data;
00207 #ifdef DO_AUTHORIZATION
00208     /* authorization */
00209     req_get16(ptr, byte_length);
00210     alloc_auth = string_list_count2(ptr, byte_length);
00211     if (0 < alloc_auth) {
00212       authorization_list = new CompoundString[alloc_auth];
00213     }
00214     for (i = 0; i < alloc_auth; i++) {
00215       ptr = req_get_ascii_string(ptr, &data, NULL, NULL);
00216       *(authorization_list + i) = data;
00217       delete [] data;
00218     }
00219 #endif /* DO_AUTHORIZATION */
00220   } else {
00221     req_get8s(ptr, byte_order);
00222     req_get8s(ptr, protocol_version);
00223     ptr = req_get_ascii_string_s(ptr, &data, &str_length, NULL);
00224     if ((0 == str_length) || (str_length & 0x01)) {
00225       delete [] data;
00226       return; /* something wrong */
00227     }
00228     username = data;
00229     // 'data' was copied to username by CompoundString's copy constructor.
00230     // so can be deleted.
00231     delete [] data;
00232 #ifdef DO_AUTHORIZATION
00233     /* authorization */
00234     req_get16s(ptr, byte_length);
00235     alloc_auth = string_list_count2(ptr, byte_length);
00236     if (0 < alloc_auth) {
00237       authorization_list = new CompoundString[alloc_auth];
00238     }
00239     for (i = 0; i < alloc_auth; i++) {
00240       ptr = req_get_ascii_string_s(ptr, &data, NULL, NULL);
00241       *(authorization_list + i) = data;
00242       delete [] data;
00243     }
00244 #endif /* DO_AUTHORIZATION */
00245   }
00246 #else /* USE_FRAMEMGR_ALWAYS */
00247   /* create FrameMgr */
00248   fm = FrameMgrInit(im_connect_fr, (char *)p, need_swap);
00249 
00250   /* get data */
00251   FrameMgrGetToken(fm, byte_order);
00252   FrameMgrGetToken(fm, protocol_version);
00253 
00254   /* username */
00255   FrameMgrGetToken(fm, str_length);
00256   if (str_length == 0) {
00257     /* something wrong */
00258     return;
00259   } else {
00260     CARD16 str;
00261     CARD8 byte_data;
00262     char *buffer = new char[str_length/2 + 1];
00263     char *usernamep;
00264     memset(buffer, 0, str_length/2 + 1);
00265     for (usernamep = buffer; usernamep < &buffer[str_length/2];
00266         usernamep++) {
00267       FrameMgrGetToken(fm, byte_data);
00268       str = (byte_data << 8) & 0xff00;
00269       FrameMgrGetToken(fm, byte_data);
00270       str += byte_data & 0x00ff;
00271       *usernamep = (char)str;
00272     }
00273     *usernamep = (CARD8)0;
00274     username = buffer;
00275     // 'buffer' was copied to username by CompoundString's copy constructor.
00276     // so can be deleted.
00277     delete [] buffer;
00278   }
00279 
00280 #ifdef DO_AUTHORIZATION
00281   /* authorization */
00282   FrameMgrGetToken(fm, byte_length);
00283   if (byte_length == 0) {
00284     FrameMgrSetIterCount(fm, 0);
00285   } else if (byte_length != 0) {
00286     int length = 0;
00287     CARD16 str;
00288     CARD8 byte_data;
00289 
00290     authorization = new char[byte_length/2];
00291     memset(authorization, 0, byte_length/2);
00292 
00293     number = 0;
00294     while (length < byte_length && number < byte_length) {
00295       FrameMgrGetToken(fm, str_length); length += 4;
00296            
00297       for (strp = authorization;
00298           strp < &authorization[byte_length/2];strp++) {
00299        FrameMgrGetToken(fm, byte_data);
00300        str = (byte_data << 8) & 0xff00;
00301        FrameMgrGetToken(fm, byte_data);
00302        str += byte_data & 0x00ff;
00303        *strp = str;
00304       }
00305       if (number >= byte_length) break;
00306       authorization[number] = (char)0; number++;
00307       length += str_length + ((4 - (str_length%4)) % 4);
00308     }
00309   }
00310 #endif
00311 
00312   /* free FrameMgr */
00313   FrameMgrFree(fm);
00314 #endif /* USE_FRAMEMGR_ALWAYS */
00315 
00316   /* Send IM_REGISTER_TRIGGER_KEYS protocol  */
00317 
00318   if (iiim_protocol->countOnKeys() || iiim_protocol->countOffKeys()) {
00319     register_triggerkeys();
00320   }
00321 
00322 #if !defined(USE_FRAMEMGR_ALWAYS)
00323 
00324   language_num = iiim_protocol->countLanguages();
00325 
00326   total_size = 4;
00327   total_size += 2;   /* imid */
00328   total_size += 2;   /* byte length of list of language names */
00329 
00330   string_list_size = 0;
00331   for (int i = 0; i < language_num; i++) {
00332     unsigned char *uc;
00333     int len;
00334     uc = iiim_protocol->getLang(i + 1).toUchar();
00335     len = strlen((char *)uc);
00336     if (len & 0x01) {
00337       string_list_size += (2 + (2 * len));
00338     } else {
00339       string_list_size += (2 + (2 * len) + 2);
00340     }
00341   }
00342   total_size += string_list_size;
00343   data = new char[total_size];
00344   ptr = (data + 4);
00345   if (False == need_swap) {
00346     req_put16(ptr, im_id);
00347     req_put16(ptr, string_list_size);
00348     for (int i = 0; i < language_num; i++) {
00349       unsigned char *uc;
00350       uc = iiim_protocol->getLang(i + 1).toUchar();
00351       ptr = req_put_ascii_string(ptr, (char *)uc);
00352     }
00353   } else {
00354     req_put16s(ptr, im_id);
00355     req_put16s(ptr, string_list_size);
00356     for (int i = 0; i < language_num; i++) {
00357       unsigned char *uc;
00358       uc = iiim_protocol->getLang(i + 1).toUchar();
00359       ptr = req_put_ascii_string_s(ptr, (char *)uc);
00360     }
00361   }
00362   send_message(IM_CONNECT_REPLY, data, total_size);
00363   delete [] data;
00364 
00365 #else /* USE_FRAMEMGR_ALWAYS */
00366   /* create FrameMgr */
00367   fm = FrameMgrInit(im_connect_reply_fr, NULL, need_swap);
00368 
00369   int iter_count = iiim_protocol->countLanguages();
00370   /* set iteration count of lang names */
00371   FrameMgrSetIterCount(fm, iter_count);
00372 
00373   /* set byte length of BIT16 item in STRING list */
00374   int i;
00375   for (i = 0; i < iter_count; i++) {
00376     CompoundString langp = iiim_protocol->getLang(i+1);
00377     int len = strlen((char*)langp);
00378     FrameMgrSetIterCount(fm, len);
00379   }
00380 
00381   const int total_size = FrameMgrGetTotalSize(fm);
00382   CompoundString reply = CompoundString(total_size, 0);
00383   memset(reply, 0, total_size);
00384   FrameMgrSetBuffer(fm, reply);
00385 
00386   /* set input-method ID */
00387   FrameMgrPutToken(fm, im_id); /* input-method-id */
00388 
00389   for (i = 0; i < iter_count; i++) {
00390     CompoundString langp = iiim_protocol->getLang(i+1);
00391     SetStringToFrame(fm, langp, strlen(langp));
00392   }
00393   send_message(IM_CONNECT_REPLY, reply, total_size);
00394 
00395   /* free FrameMgr */
00396   FrameMgrFree(fm);
00397 #endif /* USE_FRAMEMGR_ALWAYS */
00398 
00399   if (!iiim_protocol->proto_handler->Authenticate(accept_fd, username)) {
00400     /* being_deleted = 1; */
00401     delete this;
00402   }
00403 
00404 }
00405 
00406 void
00407 IIIMPClient::dis_connect(IMProtocolStruct *call_data, unsigned char *p) {
00408 #if !defined(USE_FRAMEMGR_ALWAYS)
00409   char data[4 + 2 + 2];
00410   char *ptr;
00411 #else /* USE_FRAMEMGR_ALWAYS */
00412   FrameMgr fm;
00413   extern XimFrameRec im_disconnect_fr[], im_disconnect_reply_fr[];
00414   XimFrameRec *input_fr = im_disconnect_fr;
00415   XimFrameRec *output_fr = im_disconnect_reply_fr;
00416 #endif /* USE_FRAMEMGR_ALWAYS */
00417   CARD16 input_method_id;
00418   const int IM_OUTPUT_REPLY = IM_DISCONNECT_REPLY;
00419 
00420 #if !defined(USE_FRAMEMGR_ALWAYS)
00421   ptr = (char *)p;
00422   if (False == need_swap) {
00423     req_get16(ptr, input_method_id);
00424     ptr = (data + 4);
00425     req_put16(ptr, input_method_id);
00426     req_put16(ptr, 0);
00427   } else {
00428     req_get16s(ptr, input_method_id);
00429     ptr = (data + 4);
00430     req_put16s(ptr, input_method_id);
00431     req_put16s(ptr, 0);
00432   }
00433   send_message(IM_OUTPUT_REPLY, data, (sizeof (data)));
00434 
00435 #else /* USE_FRAMEMGR_ALWAYS */
00436   /* create FrameMgr */
00437   fm = FrameMgrInit(input_fr, (char *)p, need_swap);
00438   /* get data */
00439   FrameMgrGetToken(fm, input_method_id);
00440 
00441   /* free FrameMgr */
00442   FrameMgrFree(fm);
00443 
00444   /* create FrameMgr */
00445   fm = FrameMgrInit(output_fr, NULL, need_swap);
00446                   
00447   const int total_size = FrameMgrGetTotalSize(fm);
00448   CompoundString reply = CompoundString(total_size, 0);
00449   memset(reply, 0, total_size);
00450   FrameMgrSetBuffer(fm, reply);
00451 
00452   FrameMgrPutToken(fm, input_method_id);
00453   send_message(IM_OUTPUT_REPLY, reply, total_size);
00454 
00455   /* free FrameMgr */
00456   FrameMgrFree(fm);
00457 #endif /* USE_FRAMEMGR_ALWAYS */
00458 }
00459 
00460 void
00461 IIIMPClient::set_icfocus(IMProtocolStruct *call_data, unsigned char *p) {
00462 #if !defined(USE_FRAMEMGR_ALWAYS)
00463   char *ptr;
00464 #else /* USE_FRAMEMGR_ALWAYS */
00465   FrameMgr fm;
00466   extern XimFrameRec im_seticfocus_fr[], im_seticfocus_reply_fr[];
00467   XimFrameRec *input_fr = im_seticfocus_fr;
00468   XimFrameRec *output_fr = im_seticfocus_reply_fr;
00469 #endif /* USE_FRAMEMGR_ALWAYS */
00470   CARD16 input_method_id, input_context_id;
00471   const int IM_OUTPUT_REPLY = IM_SETICFOCUS_REPLY;
00472 
00473 #if !defined(USE_FRAMEMGR_ALWAYS)
00474   ptr = (char *)p;
00475   if (False == need_swap) {
00476     req_get16(ptr, input_method_id);
00477     req_get16(ptr, input_context_id);
00478   } else {
00479     req_get16s(ptr, input_method_id);
00480     req_get16s(ptr, input_context_id);
00481   }
00482 #else /* USE_FRAMEMGR_ALWAYS */
00483   /* create FrameMgr */
00484   fm = FrameMgrInit(input_fr, (char *)p, need_swap);
00485 
00486   /* get data */
00487   FrameMgrGetToken(fm, input_method_id);
00488   FrameMgrGetToken(fm, input_context_id);
00489 
00490   /* free FrameMgr */
00491   FrameMgrFree(fm);
00492 #endif /* USE_FRAMEMGR_ALWAYS */
00493 
00494   InputContext *ic = &(input_context_list.getItem(input_context_id));
00495   if (ic) { 
00496     iiim_protocol->proto_handler->SetICFocus((InputContext*)ic);
00497  } else {
00498     // need to treate as an error
00499   }
00500 
00501 #if !defined(USE_FRAMEMGR_ALWAYS)
00502   send_message(input_method_id, input_context_id, IM_OUTPUT_REPLY);
00503 #else /* USE_FRAMEMGR_ALWAYS */
00504   send_message(input_method_id, input_context_id,
00505               output_fr, IM_OUTPUT_REPLY);
00506 #endif /* USE_FRAMEMGR_ALWAYS */
00507   return;
00508 }
00509 void
00510 IIIMPClient::unset_icfocus(IMProtocolStruct *call_data, unsigned char *p) {
00511 #if !defined(USE_FRAMEMGR_ALWAYS)
00512   char *ptr;
00513 #else /* USE_FRAMEMGR_ALWAYS */
00514   FrameMgr fm;
00515   extern XimFrameRec im_unseticfocus_fr[], im_unseticfocus_reply_fr[];
00516   XimFrameRec *input_fr = im_unseticfocus_fr;
00517   XimFrameRec *output_fr = im_unseticfocus_reply_fr;
00518 #endif /* USE_FRAMEMGR_ALWAYS */
00519   CARD16 input_method_id, input_context_id;
00520   const int IM_OUTPUT_REPLY = IM_UNSETICFOCUS_REPLY;
00521 
00522 #if !defined(USE_FRAMEMGR_ALWAYS)
00523   ptr = (char *)p;
00524   if (False == need_swap) {
00525     req_get16(ptr, input_method_id);
00526     req_get16(ptr, input_context_id);
00527   } else {
00528     req_get16s(ptr, input_method_id);
00529     req_get16s(ptr, input_context_id);
00530   }
00531 #else /* USE_FRAMEMGR_ALWAYS */
00532   /* create FrameMgr */
00533   fm = FrameMgrInit(input_fr, (char *)p, need_swap);
00534 
00535   /* get data */
00536   FrameMgrGetToken(fm, input_method_id);
00537   FrameMgrGetToken(fm, input_context_id);
00538 
00539   /* free FrameMgr */
00540   FrameMgrFree(fm);
00541 #endif /* USE_FRAMEMGR_ALWAYS */
00542 
00543   InputContext *ic = &(input_context_list.getItem(input_context_id));
00544   if (ic) {
00545     iiim_protocol->proto_handler->UnsetICFocus((InputContext*)ic);
00546   } else { 
00547     // need to treate as an error
00548   }
00549 
00550 #if !defined(USE_FRAMEMGR_ALWAYS)
00551   send_message(input_method_id, input_context_id, IM_OUTPUT_REPLY);
00552 #else /* USE_FRAMEMGR_ALWAYS */
00553   send_message(input_method_id, input_context_id,
00554               output_fr, IM_OUTPUT_REPLY);
00555 #endif /* USE_FRAMEMGR_ALWAYS */
00556   return;
00557 }
00558 void
00559 IIIMPClient::destroy_ic(IMProtocolStruct *call_data, unsigned char *p) {
00560 #if !defined(USE_FRAMEMGR_ALWAYS)
00561   char *ptr;
00562 #else /* USE_FRAMEMGR_ALWAYS */
00563   FrameMgr fm;
00564   extern XimFrameRec im_destroyic_fr[], im_destroyic_reply_fr[];
00565   XimFrameRec *input_fr = im_destroyic_fr;
00566   XimFrameRec *output_fr = im_destroyic_reply_fr;
00567 #endif /* USE_FRAMEMGR_ALWAYS */
00568   CARD16 input_method_id, input_context_id;
00569   const int IM_OUTPUT_REPLY = IM_DESTROYIC_REPLY;
00570 
00571 #if !defined(USE_FRAMEMGR_ALWAYS)
00572   ptr = (char *)p;
00573   if (False == need_swap) {
00574     req_get16(ptr, input_method_id);
00575     req_get16(ptr, input_context_id);
00576   } else {
00577     req_get16s(ptr, input_method_id);
00578     req_get16s(ptr, input_context_id);
00579   }
00580 #else /* USE_FRAMEMGR_ALWAYS */
00581   /* create FrameMgr */
00582   fm = FrameMgrInit(input_fr, (char *)p, need_swap);
00583 
00584   /* get data */
00585   FrameMgrGetToken(fm, input_method_id);
00586   FrameMgrGetToken(fm, input_context_id);
00587 
00588   /* free FrameMgr */
00589   FrameMgrFree(fm);
00590 #endif /* USE_FRAMEMGR_ALWAYS */
00591 
00592   InputContext *ic = &(input_context_list.getItem(input_context_id));
00593   input_context_list.remove(ic);
00594   if (ic) {
00595     iiim_protocol->proto_handler->DestroyIC((InputContext*)ic);
00596   } else { 
00597     // need to treate as an error
00598   }
00599 
00600 #if !defined(USE_FRAMEMGR_ALWAYS)
00601   send_message(input_method_id, input_context_id, IM_OUTPUT_REPLY);
00602 #else /* USE_FRAMEMGR_ALWAYS */
00603   send_message(input_method_id, input_context_id,
00604               output_fr, IM_OUTPUT_REPLY);
00605 #endif /* USE_FRAMEMGR_ALWAYS */
00606   if (ic) {
00607     ((IIIMPInputContext*)ic)->unrealize();
00608     // ic should be deleted by DESTROY_IC request from IM client
00609     delete ic;
00610   }
00611   return;
00612 }
00613 void
00614 IIIMPClient::reset_ic(IMProtocolStruct *call_data, unsigned char *p) {
00615 #if !defined(USE_FRAMEMGR_ALWAYS)
00616   char *ptr;
00617 #else /* USE_FRAMEMGR_ALWAYS */
00618   FrameMgr fm;
00619   extern XimFrameRec im_resetic_fr[], im_resetic_reply_fr[];
00620   XimFrameRec *input_fr = im_resetic_fr;
00621   XimFrameRec *output_fr = im_resetic_reply_fr;
00622 #endif /* USE_FRAMEMGR_ALWAYS */
00623   CARD16 input_method_id, input_context_id;
00624   const int IM_OUTPUT_REPLY = IM_RESETIC_REPLY;
00625 
00626 #if !defined(USE_FRAMEMGR_ALWAYS)
00627   ptr = (char *)p;
00628   if (False == need_swap) {
00629     req_get16(ptr, input_method_id);
00630     req_get16(ptr, input_context_id);
00631   } else {
00632     req_get16s(ptr, input_method_id);
00633     req_get16s(ptr, input_context_id);
00634   }
00635 #else /* USE_FRAMEMGR_ALWAYS */
00636   /* create FrameMgr */
00637   fm = FrameMgrInit(input_fr, (char *)p, need_swap);
00638                   
00639   /* get data */
00640   FrameMgrGetToken(fm, input_method_id);
00641   FrameMgrGetToken(fm, input_context_id);
00642 
00643   /* free FrameMgr */
00644   FrameMgrFree(fm);
00645 #endif /* USE_FRAMEMGR_ALWAYS */
00646 
00647   InputContext *ic = &(input_context_list.getItem(input_context_id));
00648   if (ic) {
00649     IMText *ret_string =
00650       iiim_protocol->proto_handler->ResetIC((InputContext*)ic);
00651     ic->commit_string(ret_string);
00652   } else { 
00653     // need to treate as an error
00654   }
00655 #if !defined(USE_FRAMEMGR_ALWAYS)
00656   send_message(input_method_id, input_context_id, IM_OUTPUT_REPLY);
00657 #else /* USE_FRAMEMGR_ALWAYS */
00658   send_message(input_method_id, input_context_id,
00659               output_fr, IM_OUTPUT_REPLY);
00660 #endif /* USE_FRAMEMGR_ALWAYS */
00661   return;
00662 }
00663 
00664 void
00665 IIIMPClient::forward_keyevent(IMProtocolStruct *call_data, unsigned char *p) {
00666 #if !defined(USE_FRAMEMGR_ALWAYS)
00667   IMKeyEventStruct *imkev;
00668   int n_key;
00669   int i;
00670 #else /* USE_FRAMEMGR_ALWAYS */
00671   FrameMgr fm;
00672   FmStatus status;
00673 #endif /* USE_FRAMEMGR_ALWAYS */
00674   CARD16 input_method_id, input_context_id;
00675   CARD32 type, byte_length;
00676   CARD32 *keyevent_list = (CARD32*)0;
00677   int number;
00678 
00679 #if !defined(USE_FRAMEMGR_ALWAYS)
00680   if (False == need_swap) {
00681     req_get16(p, input_method_id);
00682     req_get16(p, input_context_id);
00683     req_get32(p, type);
00684     req_get32(p, byte_length);
00685     if (0 == byte_length) return;
00686     n_key = (byte_length / (4 * (sizeof (CARD32))));
00687     imkev = new IMKeyEventStruct[n_key];
00688     if (NULL == imkev) return;
00689     for (i = 0; i < n_key; i++) {
00690       req_get32(p, (imkev + i)->keyCode);
00691       req_get32(p, (imkev + i)->keyChar);
00692       req_get32(p, (imkev + i)->modifier);
00693       req_get32(p, (imkev + i)->time_stamp);
00694     }
00695   } else {
00696     req_get16s(p, input_method_id);
00697     req_get16s(p, input_context_id);
00698     req_get32s(p, type);
00699     req_get32s(p, byte_length);
00700     if (0 == byte_length) return;
00701     n_key = (byte_length / (4 * (sizeof (CARD32))));
00702     imkev = new IMKeyEventStruct[n_key];
00703     if (NULL == imkev) return;
00704     for (i = 0; i < n_key; i++) {
00705       req_get32s(p, (imkev + i)->keyCode);
00706       req_get32s(p, (imkev + i)->keyChar);
00707       req_get32s(p, (imkev + i)->modifier);
00708       req_get32s(p, (imkev + i)->time_stamp);
00709     }
00710   }
00711 
00712   InputContext *ic = &(input_context_list.getItem(input_context_id));
00713 
00714   if (NULL == ic) {
00715     delete [] imkev;
00716     return;
00717   }
00718 
00719   IMKeyListEvent keylist_event;
00720   keylist_event.type = IM_EventKeyList;
00721   keylist_event.n_operation = 0;
00722   keylist_event.operation_list = 0;
00723   keylist_event.n_key = n_key;
00724   keylist_event.keylist = imkev;
00725 
00726   iiim_protocol->proto_handler->ForwardEvent((InputContext*)ic,
00727                                         (IMInputEvent*)&keylist_event);
00728   delete [] imkev;
00729 
00730   return;
00731 
00732 #else /* USE_FRAMEMGR_ALWAYS */
00733   extern XimFrameRec im_forward_event_keyevent_fr[];
00734   XimFrameRec *input_fr = im_forward_event_keyevent_fr;
00735 
00736   /* create FrameMgr */
00737   fm = FrameMgrInit(input_fr, (char *)p, need_swap);
00738   /* get data */
00739   FrameMgrGetToken(fm, input_method_id);
00740   FrameMgrGetToken(fm, input_context_id);
00741   FrameMgrGetToken(fm, type);
00742 
00743   FrameMgrGetToken(fm, byte_length);
00744 
00745   keyevent_list = (CARD32*)new char[byte_length];
00746   if (keyevent_list == NULL) {
00747     return;
00748   }
00749   if (byte_length != 0) {
00750     number = 0;
00751     while (FrameMgrIsIterLoopEnd(fm, &status) == False) {
00752       /* get a key event */
00753       FrameMgrGetToken(fm, keyevent_list[number]);
00754       number++;
00755     }
00756   }
00757 
00758   /* free FrameMgr */
00759   FrameMgrFree(fm);
00760 
00761   InputContext *ic = &(input_context_list.getItem(input_context_id));
00762 
00763   if (ic) {
00764     IMKeyListEvent keylist_event;
00765     keylist_event.type = IM_EventKeyList;
00766     keylist_event.n_operation = 0;
00767     keylist_event.operation_list = 0;
00768 
00769     int n_key = byte_length/(4 * sizeof(CARD32));
00770     IMKeyEventStruct *imkev = new IMKeyEventStruct[n_key];
00771 
00772     keylist_event.n_key = n_key;
00773     int n = 0;
00774     number = 0;
00775     while (number < byte_length/4) { 
00776       imkev[n].keyCode = keyevent_list[number]; /* keyCode */
00777       imkev[n].keyChar = keyevent_list[number + 1]; /* keyChar */
00778       imkev[n].modifier = keyevent_list[number + 2]; /* modifier */
00779       imkev[n].time_stamp = keyevent_list[number + 3]; /* time_stamp */
00780       number += 4; n++;
00781     }
00782     keylist_event.keylist = imkev;
00783     iiim_protocol->proto_handler->ForwardEvent((InputContext*)ic,
00784                                    (IMInputEvent*)&keylist_event);
00785     delete [] imkev;
00786   } else {
00787     // need to treate as an error
00788   }
00789   delete [] keyevent_list;
00790   return;
00791 #endif /* USE_FRAMEMGR_ALWAYS */
00792 }
00793 void
00794 IIIMPClient::forward_event(IMProtocolStruct *call_data, unsigned char *p) {
00795 #if !defined(USE_FRAMEMGR_ALWAYS)
00796   unsigned char * pp;
00797 #else /* USE_FRAMEMGR_ALWAYS */
00798   FrameMgr fm;
00799   extern XimFrameRec im_forward_event_fr[];
00800   extern XimFrameRec im_forward_event_reply_fr[];
00801   XimFrameRec *output_fr = im_forward_event_reply_fr;
00802 #endif /* USE_FRAMEMGR_ALWAYS */
00803   CARD16 input_method_id, input_context_id;
00804   CARD32 type;
00805   const int IM_OUTPUT_REPLY = IM_FORWARD_EVENT_REPLY;
00806 
00807 #if !defined(USE_FRAMEMGR_ALWAYS)
00808   pp = p;
00809   if (False == need_swap) {
00810     req_get16(pp, input_method_id);
00811     req_get16(pp, input_context_id);
00812     req_get32(pp, type);
00813   } else {
00814     req_get16s(pp, input_method_id);
00815     req_get16s(pp, input_context_id);
00816     req_get32s(pp, type);
00817   }
00818 #else /* USE_FRAMEMGR_ALWAYS */
00819   /* create FrameMgr */
00820   fm = FrameMgrInit(im_forward_event_fr, (char *)p, need_swap);
00821 
00822   /* get data */
00823   FrameMgrGetToken(fm, input_method_id);
00824   FrameMgrGetToken(fm, input_context_id);
00825   FrameMgrGetToken(fm, type);
00826 
00827   /* free FrameMgr */
00828   FrameMgrFree(fm);
00829 #endif /* USE_FRAMEMGR_ALWAYS */
00830 
00831   switch (type) {
00832   case FORWARD_STRING:
00833     forward_string(call_data, p);
00834     break;
00835   case FORWARD_TEXT:
00836     forward_text(call_data, p);
00837     break;
00838   case FORWARD_KEYEVENT:
00839     forward_keyevent(call_data, p);
00840     break;
00841   }
00842 
00843   /* always send a syncroneous reply */
00844 #if !defined(USE_FRAMEMGR_ALWAYS)
00845   send_message(input_method_id, input_context_id, IM_OUTPUT_REPLY);
00846 #else /* USE_FRAMEMGR_ALWAYS */
00847   send_message(input_method_id, input_context_id,
00848               output_fr, IM_OUTPUT_REPLY);
00849 #endif /* USE_FRAMEMGR_ALWAYS */
00850   return;
00851 }
00852 void
00853 IIIMPClient::trigger_notify(IMProtocolStruct *call_data, unsigned char *p) {
00854 #if !defined(USE_FRAMEMGR_ALWAYS)
00855   char data[4 + 2 + 2];
00856   char *ptr;
00857 #else /* USE_FRAMEMGR_ALWAYS */
00858   FrameMgr fm;
00859   extern XimFrameRec im_trigger_notify_fr[], im_trigger_notify_reply_fr[];
00860   XimFrameRec *input_fr = im_trigger_notify_fr;
00861   XimFrameRec *output_fr = im_trigger_notify_reply_fr;
00862 #endif /* USE_FRAMEMGR_ALWAYS */
00863   CARD16 input_method_id, input_context_id;
00864   CARD16 flag;
00865   const int IM_OUTPUT_REPLY = IM_TRIGGER_NOTIFY_REPLY;
00866 
00867 #if !defined(USE_FRAMEMGR_ALWAYS)
00868   ptr = (char *)p;
00869   if (False == need_swap) {
00870     req_get16(ptr, input_method_id);
00871     req_get16(ptr, input_context_id);
00872     req_get16(ptr, flag);
00873   } else {
00874     req_get16s(ptr, input_method_id);
00875     req_get16s(ptr, input_context_id);
00876     req_get16s(ptr, flag);
00877   }
00878 #else /* USE_FRAMEMGR_ALWAYS */
00879   /* create FrameMgr */
00880   fm = FrameMgrInit(input_fr, (char *)p, need_swap);
00881   /* get data */
00882   FrameMgrGetToken(fm, input_method_id);
00883   FrameMgrGetToken(fm, input_context_id);
00884   FrameMgrGetToken(fm, flag);
00885 
00886   /* free FrameMgr */
00887   FrameMgrFree(fm);
00888 #endif /* USE_FRAMEMGR_ALWAYS */
00889 
00890   InputContext *ic = &(input_context_list.getItem(input_context_id));
00891   if (ic) {
00892     if (flag == 0)
00893       iiim_protocol->proto_handler->StartConversion((InputContext*)ic);
00894     else
00895       iiim_protocol->proto_handler->EndConversion((InputContext*)ic);
00896   } else {
00897     // need to treat as an error
00898   }
00899 
00900 #if !defined(USE_FRAMEMGR_ALWAYS)
00901   send_message(input_method_id, input_context_id, IM_OUTPUT_REPLY);
00902 #else /* USE_FRAMEMGR_ALWAYS */
00903   send_message(input_method_id, input_context_id,
00904               output_fr, IM_OUTPUT_REPLY);
00905 #endif /* USE_FRAMEMGR_ALWAYS */
00906   return;
00907 }
00908 void
00909 IIIMPClient::preedit_start_reply(IMProtocolStruct *call_data,
00910                              unsigned char *p) {
00911 #if !defined(USE_FRAMEMGR_ALWAYS)
00912   char *ptr;
00913 #else /* USE_FRAMEMGR_ALWAYS */
00914   FrameMgr fm;
00915   extern XimFrameRec im_preedit_start_reply_fr[];
00916 #endif /* USE_FRAMEMGR_ALWAYS */
00917   IMPreeditStruct preedit;
00918   CARD16 input_method_id, input_context_id;
00919   int return_value;
00920 
00921 #if !defined(USE_FRAMEMGR_ALWAYS)
00922   ptr = (char *)p;
00923   if (False == need_swap) {
00924     req_get16(ptr, input_method_id);
00925     req_get16(ptr, input_context_id);
00926     req_get16(ptr, return_value);
00927   } else {
00928     req_get16s(ptr, input_method_id);
00929     req_get16s(ptr, input_context_id);
00930     req_get16s(ptr, return_value);
00931   }
00932 #ifdef DEBUG
00933   // cerr << "Preedit Start CB reply" << endl;
00934   // cerr << "need to set return_value to preedit rendering object" << endl;
00935 #endif
00936 #else /* USE_FRAMEMGR_ALWAYS */
00937   /* create FrameMgr */
00938   fm = FrameMgrInit(im_preedit_start_reply_fr, (char *)p, need_swap);
00939 
00940   /* get data */
00941   FrameMgrGetToken(fm, input_method_id);
00942   FrameMgrGetToken(fm, input_context_id);
00943   FrameMgrGetToken(fm, return_value);
00944 
00945 #ifdef DEBUG
00946   // cerr << "Preedit Start CB reply" << endl;
00947   // cerr << "need to set return_value to preedit rendering object" << endl;
00948 #endif
00949 
00950   /* free FrameMgr */
00951   FrameMgrFree(fm);
00952 #endif /* USE_FRAMEMGR_ALWAYS */
00953   return;
00954 }
00955 void
00956 IIIMPClient::preedit_caret_reply(IMProtocolStruct *call_data,
00957                              unsigned char *p) {
00958 #if !defined(USE_FRAMEMGR_ALWAYS)
00959   char *ptr;
00960 #else /* USE_FRAMEMGR_ALWAYS */
00961   FrameMgr fm;
00962   extern XimFrameRec im_preedit_caret_reply_fr[];
00963 #endif /* USE_FRAMEMGR_ALWAYS */
00964   IMPreeditStruct preedit;
00965   IMPreeditCaretCallbackStruct *caret =
00966     (IMPreeditCaretCallbackStruct *)&preedit.todo.caret;
00967   CARD16 input_method_id, input_context_id;
00968   int caret_position;
00969 
00970 #if !defined(USE_FRAMEMGR_ALWAYS)
00971   ptr = (char *)p;
00972   if (False == need_swap) {
00973     req_get16(ptr, input_method_id);
00974     req_get16(ptr, input_context_id);
00975     req_get16(ptr, caret_position);
00976   } else {
00977     req_get16s(ptr, input_method_id);
00978     req_get16s(ptr, input_context_id);
00979     req_get16s(ptr, caret_position);
00980   }
00981 #ifdef DEBUG
00982   // cerr << "Preedit Caret CB reply" << endl;
00983   // cerr << "need to set caret_position to preedit rendering object" << endl;
00984 #endif
00985 #else /* USE_FRAMEMGR_ALWAYS */
00986   /* create FrameMgr */
00987   fm = FrameMgrInit(im_preedit_caret_reply_fr, (char *)p, need_swap);
00988 
00989   /* get data */
00990   FrameMgrGetToken(fm, input_method_id);
00991   FrameMgrGetToken(fm, input_context_id);
00992   FrameMgrGetToken(fm, caret_position);
00993 
00994 #ifdef DEBUG
00995   // cerr << "Preedit Caret CB reply" << endl;
00996   // cerr << "need to set caret_position to preedit rendering object" << endl;
00997 #endif
00998 
00999   /* free FrameMgr */
01000   FrameMgrFree(fm);
01001 #endif /* USE_FRAMEMGR_ALWAYS */
01002 
01003   return;
01004 }
01005 
01006 void
01007 IIIMPClient::register_triggerkeys() {
01008 #if !defined(USE_FRAMEMGR_ALWAYS)
01009   char *data;
01010   char *ptr;
01011   int total_size;
01012   CARD32 time_stamp = 0;
01013   int keycode;
01014   int modifier;
01015   IMTriggerKey keyp;
01016   int i;
01017 #else /* USE_FRAMEMGR_ALWAYS */
01018   FrameMgr fm;
01019   extern XimFrameRec im_register_triggerkeys_fr[];
01020 #endif /* USE_FRAMEMGR_ALWAYS */
01021 
01022   CARD16 on_key_num = iiim_protocol->countOnKeys();
01023   CARD16 off_key_num = iiim_protocol->countOffKeys();
01024 
01025   if (on_key_num == 0 && off_key_num == 0) return;
01026 
01027 #if !defined(USE_FRAMEMGR_ALWAYS)
01028   total_size = 4;
01029   total_size += (2 + 2);
01030   total_size += 4;                        /* byte length of on-keys */
01031   total_size += ((4 * 4) * on_key_num);          /* trigger keys to turn on */
01032   total_size += 4;                        /* byte length of off-keys */
01033   total_size += ((4 * 4) * off_key_num);  /* trigger keys to turn off */
01034   data = new char[total_size];
01035   ptr = (data + 4);
01036   if (False == need_swap) {
01037     req_put16(ptr, im_id);
01038     req_put16(ptr, 0);             /* padding */
01039     req_put32(ptr, ((4 * 4) * on_key_num));
01040     for (i = 0; i < on_key_num; i++) {
01041       keyp = iiim_protocol->getOnKey(i + 1);
01042       keycode = keyp.get_keyCode();
01043       req_put32(ptr, keycode);
01044       req_put32(ptr, keycode); /* for keychar */
01045       if ((modifier = keyp.get_modifier()) & (1<<2)) {
01046         /* Java's control_mask(1<<1) and X's control_mask(1<<2)is
01047           different, so it should be changed */
01048        modifier -= (1<<2);
01049        modifier |= (1<<1);
01050       }
01051       req_put32(ptr, modifier);
01052       req_put32(ptr, time_stamp);
01053     }
01054     req_put32(ptr, ((4 * 4) * off_key_num));
01055     for (i = 0; i < off_key_num; i++) {
01056       keyp = iiim_protocol->getOffKey(i + 1);
01057       keycode = keyp.get_keyCode();
01058       req_put32(ptr, keycode);
01059       req_put32(ptr, keycode); /* for keychar */
01060       if ((modifier = keyp.get_modifier()) & (1<<2)) {
01061         /* Java's control_mask(1<<1) and X's control_mask(1<<2)is
01062           different, so it should be changed */
01063        modifier -= (1<<2);
01064        modifier |= (1<<1);
01065       }
01066       req_put32(ptr, modifier);
01067       req_put32(ptr, time_stamp);
01068     }
01069   } else {
01070     req_put16s(ptr, im_id);
01071     req_put16s(ptr, 0);            /* padding */
01072     req_put32s(ptr, ((4 * 4) * on_key_num));
01073     for (i = 0; i < on_key_num; i++) {
01074       keyp = iiim_protocol->getOnKey(i + 1);
01075       keycode = keyp.get_keyCode();
01076       req_put32s(ptr, keycode);
01077       req_put32s(ptr, keycode); /* for keychar */
01078       if ((modifier = keyp.get_modifier()) & (1<<2)) {
01079         /* Java's control_mask(1<<1) and X's control_mask(1<<2)is
01080           different, so it should be changed */
01081        modifier -= (1<<2);
01082        modifier |= (1<<1);
01083       }
01084       req_put32s(ptr, modifier);
01085       req_put32s(ptr, time_stamp);
01086     }
01087     req_put32s(ptr, ((4 * 4) * off_key_num));
01088     for (i = 0; i < off_key_num; i++) {
01089       keyp = iiim_protocol->getOffKey(i + 1);
01090       keycode = keyp.get_keyCode();
01091       req_put32s(ptr, keycode);
01092       req_put32s(ptr, keycode); /* for keychar */
01093       if ((modifier = keyp.get_modifier()) & (1<<2)) {
01094         /* Java's control_mask(1<<1) and X's control_mask(1<<2)is
01095           different, so it should be changed */
01096        modifier -= (1<<2);
01097        modifier |= (1<<1);
01098       }
01099       req_put32s(ptr, modifier);
01100       req_put32s(ptr, time_stamp);
01101     }
01102   }
01103   send_message(IM_REGISTER_TRIGGER_KEYS, data, total_size);
01104   delete [] data;
01105 #else /* USE_FRAMEMGR_ALWAYS */
01106   /* create FrameMgr */
01107   fm = FrameMgrInit(im_register_triggerkeys_fr, NULL, need_swap);
01108 
01109   /* set iteration count for on-keys list */
01110   FrameMgrSetIterCount(fm, on_key_num);
01111   /* set iteration count for off-keys list */
01112   FrameMgrSetIterCount(fm, off_key_num);
01113 
01114   /* get total_size */
01115   const int total_size = FrameMgrGetTotalSize(fm);
01116   CompoundString reply = CompoundString(total_size, 0);
01117   memset(reply, 0, total_size);
01118   FrameMgrSetBuffer(fm, reply);
01119 
01120   /* Right now IM_CONNECT_REPLY hasn't been sent to this new client, so
01121      the input-method-id is still invalid, but it is set to accept_fd
01122   */
01123   FrameMgrPutToken(fm, im_id); /* input-method-id */
01124 
01125   CARD32 time_stamp = 0;
01126   int keycode, modifier;
01127   IMTriggerKey keyp;
01128   int i;
01129 
01130   for (i = 0; i < on_key_num; i++) {
01131     keyp = iiim_protocol->getOnKey(i+1);
01132     keycode = keyp.get_keyCode();
01133     FrameMgrPutToken(fm, keycode);
01134     FrameMgrPutToken(fm, keycode); /* for keychar */
01135     if ((modifier = keyp.get_modifier()) & (1<<2)) {
01136       /* Java's control_mask(1<<1) and X's control_mask(1<<2)is
01137         different, so it should be changed */
01138       modifier -= (1<<2);
01139       modifier |= (1<<1);
01140     }
01141     FrameMgrPutToken(fm, modifier);
01142     FrameMgrPutToken(fm, time_stamp);
01143   }
01144 
01145   for (i = 0; i < off_key_num; i++) {
01146     keyp = iiim_protocol->getOffKey(i+1);
01147     keycode = keyp.get_keyCode();
01148     FrameMgrPutToken(fm, keycode);
01149     FrameMgrPutToken(fm, keycode); /* for keychar */
01150     if ((modifier = keyp.get_modifier()) & (1<<2)) {
01151       /* Java's control_mask(1<<1) and X's control_mask(1<<2)is
01152         different, so it should be changed */
01153       modifier -= (1<<2);
01154       modifier |= (1<<1);
01155     }
01156     FrameMgrPutToken(fm, modifier);
01157     FrameMgrPutToken(fm, time_stamp);
01158   }
01159 
01160   send_message(IM_REGISTER_TRIGGER_KEYS, reply, total_size);
01161 
01162   /* free FrameMgr */
01163   FrameMgrFree(fm);
01164 #endif /* USE_FRAMEMGR_ALWAYS */
01165 
01166   return;
01167 }
01168 
01169 void
01170 IIIMPClient::send_message(int input_method_id, int input_context_id,
01171                        int opcode) {
01172   int  data_len;
01173   char buf[4 + 2 + 2];
01174   char *p;
01175 
01176   /* calculate length */
01177   buf[0] = opcode;
01178   buf[1] = 0;
01179   buf[2] = 0;
01180   buf[3] = 1;
01181 
01182   p = (buf + 4);
01183 
01184   if (False == need_swap) {
01185     req_put16(p, input_method_id);
01186     req_put16(p, input_context_id);
01187   } else {
01188     req_put16s(p, input_method_id);
01189     req_put16s(p, input_context_id);
01190   }
01191 
01192   write_data(buf, sizeof (buf));
01193 
01194   return;
01195 }
01196 void
01197 IIIMPClient::send_message(int opcode, char * request, int request_len) {
01198   int data_len;
01199 
01200   data_len = ((request_len - 4) / 4);
01201   *(request + 0) = opcode;
01202   *(request + 1) = ((data_len >> 16) & 0xff);
01203   *(request + 2) = ((data_len >>  8) & 0xff);
01204   *(request + 3) = ((data_len >>  0) & 0xff);
01205 
01206   write_data(request, request_len);
01207 
01208   return;
01209 }
01210 
01211 #if !defined(USE_FRAMEMGR_ALWAYS)
01212 #else /* USE_FRAMEMGR_ALWAYS */
01213 void
01214 IIIMPClient::send_message(int input_method_id, int input_context_id,
01215                        XimFrameRec *output_fr, int opcode) {
01216   FrameMgr fm;
01217 
01218   fm = FrameMgrInit(output_fr, NULL, need_swap);
01219                   
01220   const int total_size = FrameMgrGetTotalSize(fm);
01221   CompoundString reply = CompoundString(total_size, 0);
01222   memset(reply, 0, total_size);
01223   FrameMgrSetBuffer(fm, reply);
01224 
01225   FrameMgrPutToken(fm, input_method_id);
01226   FrameMgrPutToken(fm, input_context_id);
01227   send_message(opcode, reply, total_size);
01228 
01229   /* free FrameMgr */
01230   FrameMgrFree(fm);
01231 }
01232 #endif /* USE_FRAMEMGR_ALWAYS */
01233 
01234 void
01235 IIIMPClient::send_message(int opcode, CompoundString& csdata, int data_length) {
01236   unsigned char *data = csdata.toUchar();
01237 #if !defined(USE_FRAMEMGR_ALWAYS)
01238   char *ptr;
01239 #else /* USE_FRAMEMGR_ALWAYS */
01240   FrameMgr fm;
01241   extern XimFrameRec im_packet_header_fr[];
01242   int header_size;
01243   unsigned char *replyp;
01244   int reply_length;
01245   long p_len = data_length/4;
01246   CARD8 length[3];
01247 #endif /* USE_FRAMEMGR_ALWAYS */
01248 
01249 #if !defined(USE_FRAMEMGR_ALWAYS)
01250   ptr = new char[4 + data_length];
01251   memcpy(ptr + 4, data, data_length);
01252   send_message(opcode, ptr, 4 + data_length);
01253   delete [] ptr;
01254 #else /* USE_FRAMEMGR_ALWAYS */
01255   /* calculate length */
01256   length[2] = ((p_len >> 16) & 0xff);
01257   length[1] = ((p_len >> 8) & 0xff);
01258   length[0] = (p_len & 0xff);
01259 
01260   /* create FrameMgr */
01261   fm = FrameMgrInit(im_packet_header_fr, NULL, need_swap);
01262 
01263   header_size = FrameMgrGetTotalSize(fm);
01264 
01265   CompoundString reply_hdr = CompoundString(header_size, 0);
01266   FrameMgrSetBuffer(fm, reply_hdr);
01267 
01268   /* put data */
01269   FrameMgrPutToken(fm, opcode);
01270   FrameMgrPutToken(fm, length[2]);
01271   FrameMgrPutToken(fm, length[1]);
01272   FrameMgrPutToken(fm, length[0]);
01273 
01274   reply_length = header_size + data_length;
01275 
01276   CompoundString reply = CompoundString(reply_length, 0);
01277   memmove(reply, reply_hdr, header_size);
01278 
01279   replyp = reply.toUchar();
01280   replyp += header_size;
01281   memmove(replyp, data, data_length);
01282 
01283   write_data((char*)reply, reply_length);
01284 
01285   /* free FrameMgr */
01286   FrameMgrFree(fm);
01287 #endif /* USE_FRAMEMGR_ALWAYS */
01288   return;
01289 }
01290