Back to index

im-sdk  12.3.91
phonetic_filter.c
Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <stdlib.h>
00003 #include <string.h>
00004 #include "SunIM.h"
00005 #include "phonetic_im.h"
00006 #include "phonetic_filter.h"
00007 #include "unit_input.h"
00008 #include "logf.h"
00009 
00010 int Is_UsedCodes_Key(indic_table, key)
00011 TableStruct *indic_table;
00012 int key;
00013 {
00014  if ( index(indic_table->UsedCodes, key) )
00015   return(1);
00016  else
00017   return(0);
00018 }
00019 
00020 int Is_Commit_Key(indic_table, key)
00021 TableStruct *indic_table;
00022 int key;
00023 {
00024   if (key == RETURN_KEY || key == SPACE_KEY || key == TAB_KEY)
00025     return(1);
00026   else
00027    return(0);
00028 }
00029 
00030 int Is_BackSpace_Key(indic_table, key)
00031 TableStruct *indic_table;
00032 int key;
00033 {
00034  if (key == BACKSPACE_KEY || key == DELETE_KEY)
00035   return(1);
00036  else
00037   return(0);
00038 }
00039 
00040 int Is_ClearAll_Key(indic_table, key)
00041 TableStruct *indic_table;
00042 int key;
00043 {
00044  if (key == ESC_KEY)
00045    return(1);
00046  else
00047    return(0);
00048 }
00049 
00050 int commit_candidate(ime_buffer)
00051 IMEBufferRec *ime_buffer;
00052 {
00053   (void)strcpy((char *)Commit_Buf, (char *)Preedit_Buf);
00054   Commit_Len = strlen((char *)Commit_Buf);
00055   log_f("Inside commit_candidate, Commit_Buf <%s>\n",Commit_Buf);
00056   IME_Status = IME_COMMIT;
00057   return(0);
00058 }
00059 
00060 void warning_bell()
00061 {
00062 }
00063 
00064 int process_output_buffer(indic_table, ime_buffer, key, Output_Buf, commit_flag)
00065 TableStruct *indic_table;
00066 IMEBufferRec *ime_buffer;
00067 int key;
00068 unsigned char *Output_Buf;
00069 int commit_flag;
00070 {
00071     log_f("process_output_buffer:Output_Buf:%s, commit_flag:%d\n",Output_Buf, commit_flag);
00072     log_f("process_output_buffer:key:%d\n",key);
00073     if (!commit_flag){
00074       (void)strcpy(prev_Preedit_Buf, Output_Buf);
00075       log_f("commit_flag:%d, prev_Preedit_Buf:%s\n",commit_flag,prev_Preedit_Buf);
00076       (void)strcpy(Preedit_Buf, prev_Preedit_Buf);
00077       Preedit_Len = strlen(Preedit_Buf);
00078       log_f("Preedit_Len:%d\n",Preedit_Len);
00079       Preedit_CaretPos = Preedit_Len;
00080       log_f("*** Preedit_CaretPos:%d\n",Preedit_CaretPos);
00081       IME_Status = IME_PREEDIT_AREA;
00082       free(Output_Buf);
00083     }else{
00084       word_start_flag = 0;
00085       log_f("commit_flag:%d, prev_Preedit_Buf:%s\n",commit_flag,prev_Preedit_Buf);
00086       (void)strcpy(Preedit_Buf, prev_Preedit_Buf);
00087       Preedit_Len = strlen(Preedit_Buf);
00088       Preedit_CaretPos = Preedit_Len;
00089       log_f("Preedit_Buf:%s, Preedit_Len:%d, Preedit_CaretPos:%d\n",Preedit_Buf, Preedit_Len, Preedit_CaretPos);
00090       commit_candidate(ime_buffer);
00091 
00092       /* After committing, copy the Output_Buf into Preedit_Buf */
00093       (void)strcpy(Preedit_Buf, Output_Buf);
00094       (void)strcpy(prev_Preedit_Buf, Output_Buf);
00095       Preedit_Len = strlen(Preedit_Buf);
00096       Preedit_CaretPos = Preedit_Len;
00097       log_f("After commit: Preedit_Buf:%s, Preedit_Len:%d\n",Preedit_Buf, Preedit_Len);
00098       /* After committing, copy the latest key into Input_Buf */
00099       Input_Len = 0;
00100       Input_Buf[Input_Len] = key;
00101       Input_Len ++;
00102       Input_Buf[Input_Len] = '\0';
00103       log_f("Input_Buf:%s, Input_Len:%d\n",Input_Buf, Input_Len);
00104       IME_Status |= IME_PREEDIT_AREA;
00105       free(Output_Buf);
00106     }
00107     return (1);
00108 }
00109 
00110 /* return value:  IME_NOT_USED_KEY:  if IME not use this key, return */ 
00111 /*                                   this key to systerm directly    */
00112 /*                IME_USED_KEY:      if IME has used this key        */
00113 
00114 int phonetic_filter(indic_table, key_event, ime_buffer)
00115 TableStruct *indic_table;
00116 IMEKey key_event;
00117 IMEBufferRec *ime_buffer;
00118 {
00119   int     key;
00120   int commit_flag;
00121   char    ch;
00122   unsigned char *Output_Buf;
00123  
00124   ime_buffer->encode = indic_table->Encode;
00125 
00126   IME_Status = 0;
00127   commit_flag = 0;
00128 
00129   key = map_keyevent_to_imekey(indic_table, key_event);
00130   log_f("phonetic_filter: map_keyevent_to_imekey: return key:0x%x\n", key);
00131  
00132   if (key == IME_NOT_USED_KEY){
00133     log_f("IME_NOT_USED_KEY \n");
00134     return(IME_NOT_USED_KEY);
00135   }
00136 
00137   if ((key == SPACE_KEY) && (Preedit_Len == 0)){
00138     log_f("Starting of the Word \n");
00139     word_start_flag = 1;
00140     word_end_flag = 1;
00141     key = key_event->keyChar;
00142 
00143     log_f("prev_Preedit_Buf:%s\n",prev_Preedit_Buf);
00144     (void)strcpy(Preedit_Buf, prev_Preedit_Buf);
00145     Preedit_Len = strlen(Preedit_Buf);
00146     Preedit_Buf[Preedit_Len] = key;
00147     Preedit_Len++;
00148     Preedit_Buf[Preedit_Len] = '\0';
00149     log_f("Preedit_Buf:%s\n",Preedit_Buf);
00150     Preedit_Len = strlen(Preedit_Buf);
00151     Preedit_CaretPos = Preedit_Len;
00152     commit_candidate(ime_buffer);
00153    
00154     Preedit_Len = 0;
00155     Preedit_Buf[Preedit_Len] = '\0';
00156     (void)memset((char *)prev_Preedit_Buf, '\0', sizeof(prev_Preedit_Buf));
00157     IME_Status |= IME_PREEDIT_AREA;
00158 
00159     /* After committing, copy the latest key into Input_Buf */
00160     Input_Len = 0;
00161     Input_Buf[Input_Len] = '\0';
00162     log_f("Input_Buf:%s, Input_Len:%d\n",Input_Buf, Input_Len);
00163     return(IME_USED_KEY);
00164   }
00165  
00166   if (Is_UsedCodes_Key(indic_table, key)){
00167     /* if not key by key, and is selectkey, and Lookup Mode */
00168     /* directly select */
00169   
00170     Input_Buf[Input_Len] = key;
00171     Input_Len ++;
00172     Input_Buf[Input_Len] = '\0';
00173   
00174     log_f("Input_Buf:%s, Input_Len:%d, word_start_flag:%d\n",Input_Buf, Input_Len, word_start_flag);
00175 
00176     log_f("phonetic_Filter: fNum:%d\n",fNum);
00177     log_f("phonetic_Filter: word_end_flag:%d\n",word_end_flag);
00178     /* Array of Function Pointers */
00179     fArray[fNum](Input_Buf, &Output_Buf, &commit_flag, word_start_flag, word_end_flag);
00180 
00181     log_f("Output_Buf:%s, commit_flag:%d\n",Output_Buf, commit_flag);
00182 
00183     process_output_buffer(indic_table, ime_buffer, key, Output_Buf, commit_flag);
00184   }
00185 
00186   if (Is_ClearAll_Key(indic_table, key)) { 
00187     /* Esc , clear preedit and so on */
00188     log_f("ESC_KEY\n");
00189     /*  if no inputted key, directly return this key */
00190     if (Input_Len==0) return(IME_NOT_USED_KEY);
00191   
00192     Input_Len = 0;
00193     Preedit_Len = 0;
00194     Preedit_CaretPos = 0;
00195     IME_Status = IME_PREEDIT_AREA ;
00196     free(Output_Buf);
00197     return (IME_USED_KEY);
00198   }
00199 
00200   if (Is_BackSpace_Key(indic_table, key)) { /* Back Space & Delete */
00201     log_f("BACKSPACE_KEY, Input_Len:%d, Preedit_Len:%d\n", Input_Len, Preedit_Len);
00202     /*  if no inputted key, directly return this key */
00203     if (Input_Len==0) return(IME_NOT_USED_KEY);
00204 
00205     Input_Len --;
00206     ch = Input_Buf[Input_Len];
00207     log_f("ch:%c, Preedit_Buf:%s\n", ch, Preedit_Buf);
00208     Input_Buf[Input_Len] = '\0';
00209 
00210     log_f("phonetic_Filter: word_end_flag:%d\n",word_end_flag);
00211     fArray[fNum](Input_Buf, &Output_Buf, &commit_flag, word_start_flag, word_end_flag);
00212     process_output_buffer(indic_table, ime_buffer, key, Output_Buf, commit_flag);
00213     return(IME_USED_KEY);
00214   }
00215 
00216   if (Is_Commit_Key(indic_table, key)) { /* space or return key */
00217     log_f("SPACE/RETURN/TAB KEY: Input_Buf:[%s]\n",Input_Buf);
00218     if (Input_Len==0){
00219       word_start_flag = 1;
00220       return(IME_NOT_USED_KEY);
00221     }
00222 
00223     /* if any keys and key is Space key and mode is not keybykey */
00224     /* then search and display the candidates */
00225 
00226     if((key == RETURN_KEY || key == SPACE_KEY || key == TAB_KEY) && (Preedit_Len > 0)){
00227       log_f("RETURN/SPACE key: Input_Buf:%s\n",Input_Buf);
00228       log_f("RETURN/SPACE key: Preedit_Buf:%s\n",Preedit_Buf);
00229       word_end_flag = 1;
00230       log_f("SPACE_KEY: word_start_flag:%d\n",word_start_flag);
00231       log_f("SPACE_KEY: word_end_flag:%d\n",word_end_flag);
00232       fArray[fNum](Input_Buf, &Output_Buf, &commit_flag, word_start_flag, word_end_flag);
00233       log_f("SPACE_KEY: commit_flag:%d\n",commit_flag);
00234       process_output_buffer(indic_table, ime_buffer, key, Output_Buf, commit_flag);
00235       word_start_flag = 1;
00236     }
00237     if((key == RETURN_KEY) && (Preedit_Len > 0)){
00238       strcat((char *)Preedit_Buf,(char *)"\n");
00239       log_f("Preedit_Buf:%s\n",Preedit_Buf);
00240       Preedit_Len = strlen(Preedit_Buf);
00241     }else if((key == SPACE_KEY) && (Preedit_Len > 0)){
00242       strcat((char *)Preedit_Buf,(char *)" ");
00243       log_f("Preedit_Buf:%s\n",Preedit_Buf);
00244       Preedit_Len = strlen(Preedit_Buf);
00245     }else if((key == TAB_KEY) && (Preedit_Len > 0)){
00246       strcat((char *)Preedit_Buf,(char *)"\t");
00247       log_f("Preedit_Buf:%s\n",Preedit_Buf);
00248       Preedit_Len = strlen(Preedit_Buf);
00249     }
00250     word_end_flag = 0;
00251     commit_candidate(ime_buffer);
00252 
00253     /* clear Preedit_Buf , prev_Preedit_Buf and Input_Buf */ 
00254     Preedit_Len = 0;
00255     Preedit_CaretPos = 0;
00256     (void)memset((char *)prev_Preedit_Buf, '\0', sizeof(prev_Preedit_Buf));
00257     Input_Len = 0;
00258     Input_Buf[Input_Len]='\0';
00259     IME_Status |= IME_PREEDIT_AREA; 
00260     return(IME_USED_KEY);
00261   }
00262 
00263   /* for unnormal keys */
00264   if (Input_Len==0) return(IME_NOT_USED_KEY);
00265   else return(IME_USED_KEY);
00266 }
00267 
00268 /*********************** Key Mapping ***********************/
00269 
00270 /*  get the keys that IME will use */
00271 int map_keyevent_to_imekey(TableStruct *indic_table, IMEKey key_event)
00272 {
00273        int keycode, keystatus, keychar;
00274 
00275        keycode = key_event->keyCode;
00276        keychar = key_event->keyChar;
00277        keystatus = key_event->modifier;
00278 
00279        log_f("phonetic_filter: keycode: 0x%x, keychar:0x%x, keystatus: 0x%x\n", keycode, keychar, keystatus);
00280        /* normal status */
00281        if (keystatus  == 0) {
00282               /* Function Keys */
00283               if (keychar == 0) {
00284                      log_f(" Function Key :%d\n", keycode);
00285                      switch (keycode) {
00286                             case IM_VK_ESCAPE:
00287                                    return(ESC_KEY);
00288 
00289                             case IM_VK_BACK_SPACE:
00290                                    return(BACKSPACE_KEY);
00291 
00292                             case IM_VK_ENTER:
00293                                    return(RETURN_KEY);
00294 
00295                             case IM_VK_TAB:
00296                                    return(TAB_KEY);
00297 
00298                             case IM_VK_INSERT:
00299                                    return(INSERT_KEY);
00300 
00301                             case IM_VK_DELETE:
00302                                    return(DELETE_KEY);
00303 
00304                             case IM_VK_HOME:
00305                                    return(HOME_KEY);
00306 
00307                             case IM_VK_END:
00308                                    return(END_KEY);
00309 
00310                             case IM_VK_PAGE_UP:
00311                                    return(PAGEUP_KEY);
00312 
00313                             case IM_VK_PAGE_DOWN:
00314                                    return(PAGEDOWN_KEY);
00315 
00316                      }
00317                      return(IME_NOT_USED_KEY);
00318               } else {
00319                      /*  normal key  */
00320                      log_f("phonetic_filter:  Normal Key :0x%x, %c\n", keychar, keychar);
00321                      return(keychar);
00322               }
00323        }
00324 
00325        /*  Shift status */
00326        if (keystatus == IM_SHIFT_MASK) {
00327               if (keychar != 0) {
00328                      /*  normal key  */
00329                      log_f("phonetic_filter:  Ascii Key :0x%x, %c\n", keychar, keychar);
00330                      return(keychar);
00331               }
00332        }
00333        return (IME_NOT_USED_KEY);
00334 }