Back to index

im-sdk  12.3.91
le_interface.c
Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <iconv.h>
00003 
00004 #include "le_object.h"
00005 #include "le_info.h"
00006 
00007 Bool if_le_OpenIF(iml_if_t *);
00008 Bool if_le_CloseIF(iml_if_t *);
00009 Bool if_le_GetIFValue(iml_if_t *, IMArgList, int);
00010 Bool if_le_SetIFValue(iml_if_t *, IMArgList, int);
00011 Bool if_le_OpenDesktop(iml_desktop_t *, IMArgList, int);
00012 Bool if_le_CloseDesktop(iml_desktop_t *);
00013 Bool if_le_CreateSC(iml_session_t *, IMArgList, int);
00014 Bool if_le_DestroySC(iml_session_t *);
00015 Bool if_le_GetSCValue(iml_session_t *, IMArgList, int);
00016 Bool if_le_SetSCValue(iml_session_t *, IMArgList, int);
00017 IMText *if_le_ResetSC(iml_session_t *);
00018 void if_le_SetSCFocus(iml_session_t *);
00019 void if_le_UnsetSCFocus(iml_session_t *);
00020 void if_le_SendEvent(iml_session_t *, IMInputEvent * ev);
00021 
00022 /* IF Method */
00023 if_methods_t le_methods = {
00024     if_le_OpenIF,
00025     if_le_CloseIF,
00026     if_le_GetIFValue,
00027     if_le_SetIFValue,
00028     if_le_OpenDesktop,
00029     if_le_CloseDesktop,
00030     if_le_CreateSC,
00031     if_le_DestroySC,
00032     if_le_GetSCValue,
00033     if_le_SetSCValue,
00034     if_le_ResetSC,
00035     if_le_SetSCFocus,
00036     if_le_UnsetSCFocus,
00037     if_le_SendEvent
00038 };
00039 
00040 LeInfoRec   *le_info = NULL;
00041 LeObjectRec *le_object = NULL;
00042 
00043 /* htt_server get the IF information when start htt_server */
00044 void if_GetIfInfo(IMArgList args, int num_args)
00045 {
00046     int i;
00047 
00048     DEBUG_printf("if_GetIfInfo(), num_args: %d\n", num_args);
00049 
00050     if (le_object == NULL) {
00051         le_object = (LeObjectRec *)le_object_new();
00052     }
00053     if (le_object == NULL)
00054         return;
00055 
00056     /*
00057      * get the input method information
00058      */
00059     if (le_info == NULL) {
00060       le_info = (LeInfoRec *) le_info_new();
00061       if (le_info == NULL)
00062        return;
00063     }
00064 
00065     for (i = 0; i < num_args; i++, args++) {
00066        switch (args->id) {
00067        case IF_VERSION:
00068            args->value = (IMArgVal) le_object->version;
00069            break;
00070        case IF_METHOD_TABLE:
00071            args->value = (IMArgVal) & le_methods;
00072            break;
00073        case IF_LE_NAME:
00074            args->value = (IMArgVal) le_object->lename;
00075            break;
00076        case IF_SUPPORTED_LOCALES:
00077            args->value = (IMArgVal) le_object->locales;
00078            break;
00079        case IF_SUPPORTED_OBJECTS:
00080            args->value = (IMArgVal) le_object->objects;
00081            break;
00082        case IF_NEED_THREAD_LOCK:
00083            args->value = (IMArgVal) LE_OK;
00084            break;
00085        case IF_SUPPORTED_IMEINFO:
00086            if (le_info && le_info->ime_modules)
00087             {
00088               ImeModuleContextRec **imm = le_info->ime_modules;
00089               iconv_t *cd = iconv_open ("UTF-8", "BIG5");
00090               iconv_t *ucd = iconv_open ("UTF-16", "UTF-8");
00091               IMEInfo *info = (IMEInfo *)malloc (sizeof (IMEInfo) * (le_info->num_ime_modules + 1));
00092               int k;
00093 
00094               /*
00095                * the localized inforatmion is in Big5 encoding, need to convert to
00096                * UTF-16, the internal encoding within IIIM
00097                */
00098               memset (info, 0, sizeof (IMEInfo) * (le_info->num_ime_modules + 1));
00099               for (k=0; k < le_info->num_ime_modules; ++k)
00100                 {
00101                   ImeInfoRec *imeinfo = imm[k]->ime_module->info;
00102                   char *utfname;
00103                   char *inbuf;
00104                   int inbytesleft;
00105                   char *outbuf;
00106                   int outbytesleft;
00107                   UTFCHAR u16_buf[256], *pu;
00108                   int num_chars = 0;
00109 
00110                   info[k].enable = imm[k]->enabled;
00111                   info[k].ime_id = strdup (imeinfo->uuid);
00112                   if (imeinfo->encoding == ENCODE_UTF8)
00113                     utfname = strdup (imeinfo->name);
00114                   else
00115                      {
00116                        /*
00117                         * the localized info is in Big5 encoding, need to convert
00118                         * to UTF-16, the internal encoding used in IIIM
00119                         */
00120                        char utf_buf[1024];
00121 
00122                        inbuf = imeinfo->name;
00123                        inbytesleft = strlen (imeinfo->name);
00124                        outbuf = utf_buf;
00125                        outbytesleft = 1024;
00126 
00127                        memset (utf_buf, 0, 1024);
00128 
00129                        /* convert to UTF-8 at first */
00130                        iconv (cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
00131                        utfname = strdup (utf_buf);
00132                      }
00133 
00134                   memset (u16_buf, 0, 256 * sizeof (UTFCHAR));
00135                   inbuf = utfname;
00136                   inbytesleft = strlen (utfname);
00137                   outbuf = u16_buf;
00138                   outbytesleft = 256 * sizeof (UTFCHAR);
00139 
00140                   iconv (ucd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
00141                   pu = u16_buf;
00142                   while (*pu) {++pu; ++num_chars;}
00143 
00144                   pu = (UTFCHAR *)malloc (sizeof (UTFCHAR) * (num_chars + 1));
00145                   memcpy (pu, u16_buf, sizeof (UTFCHAR) * (num_chars + 1));
00146                   info[k].imename = pu;
00147 
00148                   free (utfname);
00149                 }
00150 
00151               iconv_close (cd);
00152               iconv_close (ucd);
00153 
00154               args->value = (IMArgVal)info;
00155             }
00156            break;
00157        default:
00158            break;
00159        }
00160     }
00161 }
00162 
00163 /* Open the IF interface */
00164 Bool if_le_OpenIF(iml_if_t * If)
00165 {
00166     DEBUG_printf("if_le_OpenIF()\n");
00167 
00168     if (le_info == NULL) {
00169        le_info = (LeInfoRec *) le_info_new();
00170        if (le_info == NULL)
00171            return (LE_FAIL);
00172     }
00173 
00174     le_info_print(le_info);
00175     return (LE_OK);
00176 }
00177 
00178 /* Close the IF interface */
00179 Bool if_le_CloseIF(iml_if_t * If)
00180 {
00181     DEBUG_printf("if_le_CloseIF()\n");
00182 
00183     le_object_destroy(le_object);
00184     le_object = NULL;
00185 
00186     le_info_destroy(le_info);
00187     le_info = NULL;
00188 
00189     return (LE_OK);
00190 }
00191 
00192 Bool if_le_GetIFValue(iml_if_t * If, IMArgList args, int num_args)
00193 {
00194     DEBUG_printf("if_le_GetIFValue()\n");
00195     return (LE_OK);
00196 }
00197 
00198 Bool if_le_SetIFValue(iml_if_t * If, IMArgList args, int num_args)
00199 {
00200     DEBUG_printf("if_le_SetIFValue()\n");
00201     return (LE_OK);
00202 }
00203 
00204 Bool if_le_OpenDesktop(iml_desktop_t * desktop, IMArgList args,
00205                      int num_args)
00206 {
00207     DEBUG_printf("if_le_OpenDesktop(), desktop: %p, user: %s, host: %s\n",
00208                desktop, desktop->user_name, desktop->host_name);
00209 
00210     le_desktop_create(desktop);
00211     return (LE_OK);
00212 }
00213 
00214 Bool if_le_CloseDesktop(iml_desktop_t * desktop)
00215 {
00216     DEBUG_printf("if_le_CloseDesktop(), desktop: %p\n", desktop);
00217 
00218     le_desktop_destroy(desktop);
00219     return (LE_OK);
00220 }
00221 
00222 Bool if_le_CreateSC(iml_session_t * s, IMArgList args, int num_args)
00223 {
00224     DEBUG_printf("if_le_CreateSC(), s: %p, num_args: %d\n", s, num_args);
00225 
00226     s->specific_data = NULL;
00227     le_session_create(s);
00228     return (LE_OK);
00229 }
00230 
00231 Bool if_le_DestroySC(iml_session_t * s)
00232 {
00233     DEBUG_printf("if_le_DestroySC(), s: %p\n", s);
00234 
00235     le_session_destroy(s);
00236     return (LE_OK);
00237 }
00238 
00239 Bool if_le_GetSCValue(iml_session_t * s, IMArgList args, int num_args)
00240 {
00241     DEBUG_printf("if_le_GetSCValue(), s: %p\n", s);
00242     return (LE_OK);
00243 }
00244 
00245 Bool if_le_SetSCValue(iml_session_t * s, IMArgList args, int num_args)
00246 {
00247     int i;
00248     IMArg *p = args;
00249 
00250     DEBUG_printf("if_le_SetSCValue(), s: %p\n", s);
00251 
00252     le_session_set_as_desktop_current_session(s);
00253 
00254     for (i = 0; i < num_args; i++, p++) {
00255        switch (p->id) {
00256        case SC_TRIGGER_ON_NOTIFY:
00257            DEBUG_printf("  SC_TRIGGER_ON_NOTIFY\n");
00258            le_session_set_conversion_status(s, CONVERSION_ON);
00259            break;
00260        case SC_TRIGGER_OFF_NOTIFY:
00261            DEBUG_printf("  SC_TRIGGER_OFF_NOTIFY\n");
00262            le_session_set_conversion_status(s, CONVERSION_OFF);
00263            break;
00264        case SC_REALIZE:
00265            DEBUG_printf("  SC_REALIZE\n");
00266            le_start_aux_objects(s);
00267            break;
00268        case SC_CLIENT_LOCALE:
00269            DEBUG_printf("  SC_CLIENT_LOCALE: [%s]\n", p->value);
00270            if (!strcasecmp(p->value, "zh_CN")) {
00271               le_session_set_client_encoding(s, ENCODE_GB18030);
00272            } else if (!strcasecmp(p->value, "zh_TW")) {
00273               le_session_set_client_encoding(s, ENCODE_BIG5);
00274            } else if (!strcasecmp(p->value, "zh_HK")) {
00275               le_session_set_client_encoding(s, ENCODE_BIG5HKSCS);
00276            }
00277            break;
00278        case SC_CLIENT_INPUT_METHOD_ENGINE:
00279            if (le_info && le_info->ime_modules)
00280              {
00281               iconv_t *ucd = iconv_open ("UTF-8", "UTF-16");
00282               iconv_t *cd = iconv_open ("UTF-8", "BIG5");
00283               ImeModuleContextRec **imm = le_info->ime_modules;
00284               char utfname[256];
00285               ImeModuleRec *ime = NULL;
00286 
00287               char *inbuf = p->value;
00288               int inbytesleft = 0;
00289               char *outbuf = utfname;
00290               int outbytesleft = 256;
00291               UTFCHAR *pu = p->value;
00292 
00293               while (*pu)
00294                 {
00295                   inbytesleft += sizeof (UTFCHAR);
00296                   ++pu;
00297                 }
00298 
00299               /* convert from UTF-16 to UTF-8 */
00300               memset (utfname, 0, 256);
00301               iconv (ucd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
00302 
00303               for (i=0; i < le_info->num_ime_modules; ++i)
00304                 {
00305                   char buf[1024];
00306                   ImeInfoRec *info = imm[i]->ime_module->info;
00307 
00308                   /* convert localized name from Big5 to UTF8 */
00309                   memset (buf, 0, 1024);
00310                   if (info->encoding != ENCODE_UTF8)
00311                     {
00312                      inbuf = info->name;
00313                      inbytesleft = strlen (info->name);
00314                      outbuf = buf;
00315                      outbytesleft = 1024;
00316 
00317                      iconv (cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
00318                     }
00319                   else
00320                     strcpy (buf, info->name);
00321 
00322                   if (!strncmp (buf, utfname, strlen (buf)))
00323                     {
00324                      ime = imm[i]->ime_module;
00325                      DEBUG_printf ("TWLE: found ime module %s\n", buf);
00326                      break;
00327                     }
00328                 }
00329 
00330                 if (ime)
00331                   le_session_switch_to_new_ime_module (s, ime);
00332 
00333                iconv_close (cd);
00334               iconv_close (ucd);
00335              }
00336 
00337            break;
00338        default:
00339            break;
00340        }
00341     }
00342     return (LE_OK);
00343 }
00344 
00345 IMText *if_le_ResetSC(iml_session_t * s)
00346 {
00347     DEBUG_printf("if_le_ResetSC(), s: %p\n", s);
00348 
00349     IMText *im_text = (IMText *) le_session_reset(s);
00350     return (IMText *) im_text;
00351 }
00352 
00353 void if_le_SetSCFocus(iml_session_t * s)
00354 {
00355     DEBUG_printf("if_le_SetSCFocus(), s: %p\n", s);
00356 
00357     le_session_set_focus_in(s);
00358     return;
00359 }
00360 
00361 void if_le_UnsetSCFocus(iml_session_t * s)
00362 {
00363     DEBUG_printf("if_le_UnsetSCFocus(), s: %p\n", s);
00364 
00365     le_session_set_focus_out(s);
00366     return;
00367 }
00368 
00369 void if_le_SendEvent(iml_session_t * s, IMInputEvent * ev)
00370 {
00371     DEBUG_printf("if_le_SendEvent(), desktop: %p, session: %p\n", s->desktop, s);
00372 
00373     if (ev) {
00374        if (ev->type == IM_EventKeyList) {
00375            DEBUG_printf("Keyboard Event\n");
00376            le_session_process_key_event(s, (IMKeyListEvent *) ev);
00377        } else if (ev->type == IM_EventAux) {
00378            DEBUG_printf("Aux Event\n");
00379            le_session_proc_aux_event(s, (IMAuxEvent *) ev);
00380        }
00381     }
00382 }