Back to index

im-sdk  12.3.91
phonetic_im.c
Go to the documentation of this file.
00001 /* 
00002  * Copyright 1998 by Sun Microsystems, Inc., 901 San Antonio Road,
00003  * Palo Alto, California, 94303, U.S.A. All rights reserved.
00004  *
00005  * This software is the confidential and proprietary information of Sun
00006  * Microsystems, Inc. ("Confidential Information").  You shall not
00007  * disclose such Confidential Information and shall use it only in
00008  * accordance with the terms of the license agreement you entered
00009  * into with Sun.
00010  */
00011 #include <stdio.h>
00012 #include <stdlib.h>
00013 #include <string.h>
00014 #include <sys/types.h>
00015 #include <dlfcn.h>
00016 
00017 #include "unit_input.h"
00018 #include "phonetic_im.h"
00019 #include "phonetic_table.h"
00020 #include "phonetic_filter.h"
00021 #include "logf.h"
00022 
00023 int  phonetic_SetOptions(IMECore, char *);
00024 int  phonetic_SetValues(IMECore, IMEArgList, int);
00025 int  phonetic_Init(IMECore);
00026 int  phonetic_Open(IMECore, IMEBuffer, void *);
00027 int  phonetic_Filter(IMECore, IMEKey, IMEArgList, IMEBuffer);
00028 void phonetic_Close(IMECore);
00029 
00030 #ifdef WIN32
00031 #define EXPORT extern __declspec(dllexport)
00032 EXPORT
00033 #endif
00034 
00035 /* IF Method */
00036 IMEMethodsRec ime_methods = {
00037        phonetic_SetOptions,
00038        phonetic_SetValues,
00039        phonetic_Init,
00040        phonetic_Open,
00041        phonetic_Filter,
00042        phonetic_Close
00043 };
00044 
00045 /* Set options for codetable input method. */
00046 int phonetic_SetOptions(IMECore core, char *options)
00047 {
00048        char   data_path[256];
00049   int i;
00050 
00051   /* Change path : /usr/lib/im/locale/in_IN/<locale_name>/phonetic.cfg */
00052        sprintf(data_path, "%s/%s/%s/%s", DEFAULT_ENGINE_PATH, core->envinfo.lang_name, options, PHONETIC_CONFIG_NAME);
00053 
00054        core->envinfo.data_path = (char *)strdup(data_path);
00055        log_f("phonetic_im: data_path :%s\n", core->envinfo.data_path);
00056 
00057   fNum = 0;
00058   table_Num = 0;
00059   for (i=0; i<MAX_ENGINE_NUM; i++)
00060     phtable_Info[i] = NULL;
00061 
00062        return (0);
00063 }
00064 
00065 int phonetic_SetValues(IMECore core, IMEArgList arglist, int option_value)
00066 {
00067        log_f("phonetic_im: phonetic_SetValues === \n");
00068 
00069        if (arglist == NULL) return(-1);
00070 
00071   arglist->args_num = 0;
00072        return(0);
00073 }
00074 
00075 /* return the name and encode status and status of codetable IME */
00076 
00077 int phonetic_Init(IMECore core)
00078 {
00079        int           ret;
00080        char          *file_name;
00081        TableStruct tHeader;
00082 
00083        log_f("phonetic_im: phonetic_Init ====\n");
00084 
00085        /* read table from file to memory buffer  */
00086        file_name = core->envinfo.data_path;
00087        log_f("phonetic_im: file name :%s\n",file_name);
00088 
00089        ret = LoadTableHeader(file_name, &tHeader); 
00090        if (ret == -1) 
00091               return (-1);
00092 
00093        /* Return Information that needed by Input Method Manager */
00094        /* Such as Encode, Cname, Status */
00095 
00096        core->baseinfo.status = ENGINE_NOT_INITIATED;
00097   log_f("Lname:%s\n",tHeader.Lname);
00098   core->baseinfo.lname = (char *)strdup(tHeader.Lname);
00099   log_f("Cname:%s\n",tHeader.Cname);
00100        core->baseinfo.cname = (char *)strdup(tHeader.Cname);
00101   log_f("encode_id:%d\n",tHeader.Encode);
00102        core->baseinfo.encode_id = tHeader.Encode;
00103   log_f("Engine_id:%d\n",core->baseinfo.engine_id);
00104 
00105   log_f("UsedCodes:%s\n",tHeader.UsedCodes);
00106 
00107        return(0);
00108 }
00109 
00110 int phonetic_Open(IMECore core, IMEBuffer ime_buffer, void *handler)
00111 {
00112        char          *file_name;
00113        int           i, ret;
00114 
00115        log_f("phonetic_im: phonetic_Open ==== \n");
00116 
00117        ime_buffer->encode = 0;
00118        ime_buffer->return_status = 0;
00119        ime_buffer->preedit_len = 0;
00120        ime_buffer->commit_len  = 0;
00121        ime_buffer->lookup_num  = 0;
00122        ime_buffer->cur_lookup_pos = 0;
00123 
00124 
00125        if (core->baseinfo.status == ENGINE_NOT_INSTALLED){
00126     log_f("phonetic_Open: ENGINE_NOT_INSTALLED \n");
00127        return(-1);
00128   }
00129 
00130        if (core->baseinfo.status == ENGINE_INITIATED) {
00131     if(table_Num > 0){
00132       log_f("table_Num:%d\n", table_Num);
00133       for (i=0; i<table_Num; i++){
00134         log_f("phtable_Info[%d].EngineId:%d, phtable_Info[%d]->ptr:%x\n",i,phtable_Info[i]->EngineId, i, phtable_Info[i]->ptr);
00135         log_f("core->baseinfo.engine_id:%d\n",core->baseinfo.engine_id);
00136         if(phtable_Info[i]->EngineId == core->baseinfo.engine_id){
00137           core->envinfo.data_ptr = phtable_Info[i]->ptr; 
00138           fNum = i;
00139           log_f("fNum:%d\n",fNum);
00140         }
00141       }
00142     }
00143     log_f("phonetic_Open: ENGINE_INITIATED \n");
00144               return 0;
00145   }
00146 
00147        /* if IME engine is not initiated, would malloc dictionary buffer */
00148 
00149        core->baseinfo.status = ENGINE_NOT_INSTALLED;
00150 
00151        /* read table from file to memory buffer  */
00152        file_name = core->envinfo.data_path;
00153        log_f("phonetic_Open: file name :%s\n",file_name);
00154 
00155        phtable_Info[table_Num] = (TableStruct *)calloc(1, sizeof(TableStruct));
00156        if (phtable_Info[table_Num] == NULL) {
00157               fprintf(stderr, "Error in Calloc: %s\n",core->baseinfo.ename);
00158               return(-1);
00159        }
00160 
00161   ret = LoadTableHeader(file_name, phtable_Info[table_Num]);
00162   if(ret == -1){
00163     free((char *)phtable_Info[table_Num]);
00164     return(-1);
00165   }
00166   phtable_Info[table_Num]->EngineId = core->baseinfo.engine_id;
00167   phtable_Info[table_Num]->ptr = (char *)phtable_Info[table_Num];
00168 
00169   log_f("phonetic_Open: Calling dlsym for Fname:%s\n",phtable_Info[table_Num]->Fname);
00170   log_f("phonetic_Open: Calling dlsym handler:%x\n",handler);
00171   fArray[table_Num] = (int(*)(char *, unsigned char **, int *, int, int)) dlsym(handler, phtable_Info[table_Num]->Fname);
00172   if(!fArray[table_Num]){
00173     log_f("dlsym error\n");
00174     return(-1);
00175   }
00176 
00177        core->baseinfo.status = ENGINE_INITIATED;
00178        core->envinfo.data_ptr = (char *)phtable_Info[table_Num];
00179 
00180   fNum = table_Num;
00181 
00182   table_Num ++; 
00183 
00184   word_start_flag = 1;
00185   word_separator_flag = 0;
00186   word_end_flag = 0;
00187 
00188        return(0);
00189 }
00190 
00191 /* close IME, and free codetable pointers */
00192 void phonetic_Close(IMECore core)
00193 {
00194        int           i;
00195 
00196        log_f("phonetic_im: phonetic_Close ==== \n");
00197 
00198   for (i=0; i<table_Num; i++){
00199     if(phtable_Info[i])
00200       free(phtable_Info[i]);
00201   }
00202 
00203   if (core->baseinfo.lname)
00204     free((char *)core->baseinfo.lname);
00205        if (core->baseinfo.cname)
00206               free((char *)core->baseinfo.cname);
00207        if (core->envinfo.data_path)
00208               free((char *)core->envinfo.data_path);
00209 }
00210 
00211 /* process key input event                                             */
00212 /* return value:  IME_NOT_USED_KEY:  if IME not use this key, return   */
00213 /*                                   this key to systerm directly      */
00214 /*                IME_USED_KEY:      if IME has used this key          */
00215 
00216 int  phonetic_Filter(core, key_event, ime_args, ime_buffer)
00217 IMECore core;
00218 IMEKey  key_event;
00219 IMEArgList ime_args;
00220 IMEBuffer ime_buffer;
00221 {
00222        int           ret;
00223        TableStruct *tHeader;
00224 
00225        log_f("phonetic_im: phonetic_Filter ==== \n");
00226 
00227        if (core->baseinfo.status != ENGINE_INITIATED)
00228               return(IME_NOT_USED_KEY);
00229               
00230        /* set tHeader's argument setting */
00231        
00232        /* tHeader's argument setting are always coveried by user defined data */
00233        tHeader = (TableStruct *)(core->envinfo.data_ptr);
00234        tHeader->Output_Encode = core->envinfo.output_encode_id;
00235 
00236        ret = phonetic_filter(tHeader, key_event, ime_buffer);
00237        log_f("phonetic_im: phonetic_filter : return: %d\n", ret);
00238        return(ret);
00239 }
00240