Back to index

im-sdk  12.3.91
boshiamy_filter.c
Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include "ctim.h"
00003 
00004 #define LOOKUP_CHOICE_NUM   6
00005 
00006 #define IME_Status          ime_buffer->return_status
00007 #define Input_Buf           ime_buffer->input_buf
00008 #define Input_Len           ime_buffer->input_len
00009 #define Preedit_Buf         ime_buffer->preedit_buf
00010 #define Preedit_Len         ime_buffer->preedit_len
00011 #define Preedit_CaretPos    ime_buffer->preedit_caretpos
00012 #define Commit_Buf          ime_buffer->commit_buf
00013 #define Commit_Len          ime_buffer->commit_len
00014 #define Status_Buf          ime_buffer->status_buf
00015 #define Status_Len          ime_buffer->status_len
00016 #define Lookups_Num         ime_buffer->num_candidates
00017 #define Lookup_Pos          ime_buffer->cur_lookup_pos
00018 #define Candidates          ime_buffer->candidates
00019 #define Comments            ime_buffer->comments
00020 #define Lookups                    ime_buffer->lookups
00021 
00022 #define BOSHIAMY_SELECT_KEYS "vrsfwlcbk"
00023 
00024 int get_pos_in_string(char *str, int ch)
00025 {
00026        int pos = 0;
00027        char *p = str;
00028 
00029        while(*p) {
00030               if (*p == ch) {
00031                      return(pos);
00032               }
00033               pos++;
00034               p++;
00035        }
00036 
00037        return(-1);
00038 }
00039 
00040 int Is_Boshiamy_Select_Key(int key)
00041 {
00042        int ret = get_pos_in_string(BOSHIAMY_SELECT_KEYS, key);
00043        if (ret == -1)
00044               return(0);
00045        else
00046               return(1);
00047 }
00048 
00049 int get_boshiamy_select_id(int key)
00050 {
00051        int ret = get_pos_in_string(BOSHIAMY_SELECT_KEYS, key);
00052        return(ret + 1);
00053 }
00054 
00055 int Is_UsedCodes_Key(CodeTableStruct *hztbl, int key)
00056 {
00057        if (index(hztbl->UsedCodes, key))
00058               return(1);
00059        else
00060               return(0);
00061 }
00062 
00063 int get_select_num(CodeTableStruct *hztbl, int key)
00064 {
00065        int ret = -1;
00066        switch(hztbl->nSelectKeyMode) {
00067               case NUMBER0_MODE:
00068                      if (key >= '0' && key <= '9')
00069                             ret = (key - '0') % 10;
00070                      break;
00071 
00072               case NUMBER_MODE:
00073                      if (key >= '0' && key <= '9')
00074                             ret = (key - '0' + 9) % 10;
00075                      break;
00076 
00077               case LOWER_MODE:
00078                      if (key >= 'a' && key <= 'a' + 10)
00079                             ret = key - 'a';
00080                      break;
00081 
00082               case UPPER_MODE:
00083                      if (key >= 'A' && key <= 'A' + 10)
00084                             ret = key - 'A';
00085                      break;
00086        }
00087        return(ret);
00088 }
00089 
00090 int Is_Select_Key(CodeTableStruct *hztbl, int key)
00091 {
00092        int ret = get_select_num(hztbl, key);
00093        if (ret == -1)
00094               return(0);
00095        else
00096               return(1);
00097 }
00098 
00099 int Is_Return_Key(CodeTableStruct *hztbl, int key)
00100 {
00101        if (key == IME_FILTERED_KEY_ENTER)
00102               return(1);
00103        else
00104               return(0);
00105 }
00106 
00107 int Is_Space_Key(CodeTableStruct *hztbl, int key)
00108 {
00109        if (key == IME_FILTERED_KEY_SPACEBAR)
00110               return(1);
00111        else
00112               return(0);
00113 }
00114 
00115 int Is_BackSpace_Key(CodeTableStruct *hztbl, int key)
00116 {
00117        int i;
00118        unsigned char *keylist = hztbl->functionkey[BACKSPACE_KEY_ID].keylist;
00119 
00120        if (key == IME_FILTERED_KEY_BACKSPACE || key == IME_FILTERED_KEY_DELETE)
00121               return(1);
00122 
00123        if (keylist[0]) {
00124               for (i=0; i<MAX_FUNCTIONKEY_LEN; i++)
00125                      if (keylist[i] == key)
00126                             return(1);
00127        }
00128 
00129        return(0);
00130 }
00131 
00132 int Is_ClearAll_Key(CodeTableStruct *hztbl, int key)
00133 {
00134        int i;
00135        unsigned char *keylist = hztbl->functionkey[CLEARALL_KEY_ID].keylist;
00136 
00137        if (key == IME_FILTERED_KEY_ESCAPE)
00138               return(1);
00139 
00140        if (keylist[0]) {
00141               for (i=0; i<MAX_FUNCTIONKEY_LEN; i++)
00142                      if (keylist[i] == key)
00143                             return(1);
00144        }
00145        return(0);
00146 }
00147 
00148 int Is_NextPage_Key(CodeTableStruct *hztbl, int key)
00149 {
00150        int i;
00151        unsigned char *keylist = hztbl->functionkey[PAGEDOWN_KEY_ID].keylist;
00152 
00153        if (key == IME_FILTERED_KEY_PAGEDOWN)
00154               return(1);
00155 
00156        if (keylist[0]) {
00157               for (i=0; i<MAX_FUNCTIONKEY_LEN; i++)
00158                      if (keylist[i] == key)
00159                             return(1);
00160        }
00161        return(0);
00162 }
00163 
00164 int Is_PrevPage_Key(CodeTableStruct *hztbl, int key)
00165 {
00166        int i;
00167        unsigned char *keylist = hztbl->functionkey[PAGEUP_KEY_ID].keylist;
00168 
00169        if (key == IME_FILTERED_KEY_PAGEUP)
00170               return(1);
00171        
00172        if (keylist[0]) {
00173               for (i=0; i<MAX_FUNCTIONKEY_LEN; i++)
00174                      if (keylist[i] == key)
00175                             return(1);
00176        }
00177        return(0);
00178 }
00179 
00180 int get_lookup_result(CodeTableStruct *hztbl, ImeBufferRec *ime_buffer, 
00181 int nHelpInfoMode, int pos, int max_lookup_num)
00182 {
00183        int i, lookup_num;
00184 
00185        lookup_num = codetable_search(hztbl, Input_Buf, Candidates, Comments, pos, max_lookup_num); 
00186        if (lookup_num > 0) {
00187               for (i=0; i<lookup_num; i++) {
00188                      DEBUG_printf("%d: %s %s\n", i, Candidates[i], Comments[i]);
00189                      if (nHelpInfoMode)
00190                             sprintf((char *)Lookups[i], "%s %s", Candidates[i], Comments[i]);
00191                      else
00192                             strcpy(Lookups[i], Candidates[i]);
00193               }
00194        }
00195 
00196        return(lookup_num);
00197 
00198 }
00199 
00200 int Commit_And_Clean_All_Area(ImeBufferRec *ime_buffer)
00201 {
00202        Input_Len = 0;
00203        Preedit_Len = 0;
00204        Lookups_Num = 0;
00205        Preedit_CaretPos = 0;
00206        IME_Status = IME_COMMIT | IME_PREEDIT_AREA | IME_LOOKUP_AREA;
00207 }
00208 
00209 int Clean_All_Area(ImeBufferRec *ime_buffer)
00210 {
00211        Input_Len = 0;
00212        Preedit_Len = 0;
00213        Lookups_Num = 0;
00214        Preedit_CaretPos = 0;
00215        IME_Status = IME_PREEDIT_AREA | IME_LOOKUP_AREA;
00216 }
00217 
00218 int commit_candidate(ImeBufferRec *ime_buffer, int id)
00219 {
00220        if (id >= Lookups_Num)
00221               return(-1);
00222 
00223        strcpy((char *)Commit_Buf, (char *)Candidates[id]);
00224        Commit_Len = strlen((char *)Commit_Buf);
00225        Commit_And_Clean_All_Area(ime_buffer);
00226 
00227        return(0);
00228 }
00229 
00230 void warning_bell()
00231 {
00232 }
00233 
00234 /* return value:  IME_UNUSED_KEY:  if IME not use this key, return this key to systerm directly */
00235 /*                IME_OK:      if IME has used this key */
00236 int ctim_filter(CodeTableStruct *hztbl, unsigned char key, ImeBufferRec *ime_buffer)
00237 {
00238        char    ch;
00239        int     i, k, hznum;
00240        int     pos, select_num;
00241 
00242        int    Max_Lookups_Num = LOOKUP_CHOICE_NUM;
00243        int    nKeyByKeyMode = hztbl->nKeyByKeyMode;
00244        int    nHelpInfoMode = hztbl->nHelpInfoMode;
00245        int    nAutoSelectMode = hztbl->nAutoSelectMode;
00246        int    nKeyPromptMode = hztbl->nKeyPromptMode;
00247 
00248        hztbl->nKeyByKeyMode = 0;
00249        ime_buffer->encoding = hztbl->Encode;
00250 
00251        IME_Status = 0;
00252 
00253 /*
00254        switch(hztbl->nSelectKeyMode) {
00255               case NUMBER0_MODE:
00256                      ime_buffer->lookup_label_type = NUMERIC0_LABEL;
00257                      break;
00258               case NUMBER_MODE:
00259                      ime_buffer->lookup_label_type = NUMERIC_LABEL;
00260                      break;
00261               case LOWER_MODE:
00262                      ime_buffer->lookup_label_type = LOWER_LABEL;
00263                      break;
00264               case UPPER_MODE:
00265                      ime_buffer->lookup_label_type = UPPER_LABEL;
00266                      break;
00267               default:
00268                      ime_buffer->lookup_label_type = NUMERIC_LABEL;
00269        }
00270 */
00271        if(Is_Select_Key(hztbl, key)) {
00272               if (Lookups_Num > 0) {
00273                      select_num = get_select_num(hztbl, key);
00274                      if (select_num >= 0 && select_num < Lookups_Num) {
00275                             commit_candidate(ime_buffer, select_num);
00276                      }
00277                      return(IME_OK);
00278               }
00279        }
00280 
00281        if((Is_UsedCodes_Key(hztbl, key)) || (Is_Wildchar_Key(hztbl, key))) { 
00282               /* if inputted keys length has over the limit */
00283               if (Input_Len >= hztbl->MaxCodes + 1) {
00284                      return(IME_OK);
00285               }
00286 
00287               Input_Buf[Input_Len] = key;
00288               Input_Len ++;
00289               Input_Buf[Input_Len] = '\0';
00290               
00291               if (nKeyPromptMode) {
00292                      strcpy(Preedit_Buf+Preedit_Len, hztbl->keyprompt[key].prompt);
00293                      Preedit_Len += strlen(hztbl->keyprompt[key].prompt);
00294                      Preedit_Buf[Preedit_Len] = '\0';
00295               } else {
00296                      Preedit_Buf[Preedit_Len] = key;
00297                      Preedit_Len += 1;
00298                      Preedit_Buf[Preedit_Len] = '\0';
00299               }
00300               Preedit_CaretPos = Preedit_Len;
00301               IME_Status |= IME_PREEDIT_AREA;
00302 
00303               Lookups_Num = 0;
00304               if (nKeyByKeyMode) {
00305                      Lookup_Pos = 0;
00306                      hznum = get_lookup_result(hztbl, ime_buffer, nHelpInfoMode, Lookup_Pos, Max_Lookups_Num); 
00307                      if (hznum > 1) {
00308                             Lookups_Num = hznum;
00309                      }
00310               }
00311               IME_Status |= IME_LOOKUP_AREA;
00312 
00313               return(IME_OK);
00314        }
00315 
00316        if(Is_ClearAll_Key(hztbl, key)) { 
00317               /* Esc , clear preedit area, lookup choice area */
00318 
00319               /*  if no inputted key, directly return this key */
00320               if (Input_Len==0) return(IME_UNUSED_KEY);
00321 
00322               Clean_All_Area(ime_buffer);
00323 
00324               return (IME_OK);
00325        }
00326 
00327        if (Is_BackSpace_Key(hztbl, key)) { /* Back Space & Delete */
00328               /*  if no inputted key, directly return this key */
00329               if (Input_Len==0) return(IME_UNUSED_KEY);
00330 
00331               /* Delete characters in pre-edit region */
00332               Input_Len --;
00333               if (nKeyPromptMode) {
00334                      ch = Input_Buf[Input_Len];
00335                      Preedit_Len -= strlen(hztbl->keyprompt[ch].prompt);
00336                      Preedit_Buf[Preedit_Len] = '\0';
00337               } else {
00338                      Preedit_Len -= 1;
00339                      Preedit_Buf[Preedit_Len] = '\0';
00340               }
00341 
00342               Input_Buf[Input_Len] = '\0';
00343               Preedit_CaretPos = Preedit_Len;
00344               IME_Status = IME_PREEDIT_AREA;
00345 
00346               Lookups_Num = 0;
00347               if (Input_Len > 0 && nKeyByKeyMode) {
00348                      Lookup_Pos = 0;
00349                      hznum = get_lookup_result(hztbl, ime_buffer, nHelpInfoMode, Lookup_Pos, Max_Lookups_Num); 
00350                      if (hznum > 1) {
00351                             Lookups_Num = hznum;
00352                      }
00353               }
00354               IME_Status |= IME_LOOKUP_AREA;
00355 
00356               return(IME_OK);
00357        }
00358 
00359        if(Is_NextPage_Key(hztbl, key)) {
00360               /*  if no inputted key, directly return this key */
00361               if (Input_Len==0) return(IME_UNUSED_KEY);
00362 
00363               /* if any preedit key and not in Lookup status */
00364               if (Lookups_Num==0) return(IME_OK);
00365 
00366               pos = Lookup_Pos + Max_Lookups_Num;
00367               hznum = get_lookup_result(hztbl, ime_buffer, nHelpInfoMode, pos, Max_Lookups_Num); 
00368               if (hznum > 0) {
00369                      Lookups_Num = hznum;
00370                      Lookup_Pos = pos;
00371                      IME_Status = IME_LOOKUP_AREA;
00372               } else {
00373                             warning_bell();
00374               }
00375 
00376               return(IME_OK);
00377        }
00378 
00379        if(Is_PrevPage_Key(hztbl, key)) {
00380               /*  if no inputted key, directly return this key */
00381               if (Input_Len==0) return(IME_UNUSED_KEY);
00382 
00383               /* if any preedit key and not in Lookup status */
00384               if (Lookups_Num==0) return(IME_OK);
00385 
00386               /* if in beginning pos */
00387               if (Lookup_Pos<=0) {
00388                             warning_bell();
00389                      return(IME_OK);
00390               }
00391 
00392               pos = Lookup_Pos - Max_Lookups_Num;
00393               hznum = get_lookup_result(hztbl, ime_buffer, nHelpInfoMode, pos, Max_Lookups_Num); 
00394               if (hznum > 0) {
00395                      Lookups_Num = hznum;
00396                      Lookup_Pos = pos;
00397                      IME_Status = IME_LOOKUP_AREA;
00398               } else {
00399                             warning_bell();
00400               }
00401 
00402               return(IME_OK);
00403        }
00404 
00405        if(Is_Space_Key(hztbl, key)) {
00406               /* if no input keys */
00407               if (Input_Len==0) 
00408                      return(IME_UNUSED_KEY);
00409 
00410               if (Lookups_Num==0) {
00411                      Lookup_Pos = 0;
00412                      Lookups_Num = get_lookup_result(hztbl, ime_buffer, nHelpInfoMode, Lookup_Pos, Max_Lookups_Num); 
00413                      if (Lookups_Num >= 1) {
00414                             commit_candidate(ime_buffer, 0);
00415                             return(IME_OK);
00416                      }
00417 
00418                      if (Input_Len <= 1 || !Is_Boshiamy_Select_Key(Input_Buf[Input_Len-1])) {
00419                             Clean_All_Area(ime_buffer);
00420                             return(IME_OK);
00421                      }
00422 
00423                      key = Input_Buf[Input_Len-1];
00424                      Input_Buf[Input_Len-1] = '\0';
00425                      select_num = get_boshiamy_select_id(key);
00426               
00427                      /* Search from beginning */
00428                      Lookups_Num = get_lookup_result(hztbl, ime_buffer, nHelpInfoMode, 0, 10);
00429                      if (select_num < Lookups_Num) {
00430                             commit_candidate(ime_buffer, select_num);
00431                      } else {
00432                             Clean_All_Area(ime_buffer);
00433                      }
00434                      return(IME_OK);
00435               }
00436 
00437               /* commit the first candidate */
00438               commit_candidate(ime_buffer, 0);
00439               return(IME_OK);
00440        }
00441 
00442        if(Is_Return_Key(hztbl, key)) { 
00443               Clean_All_Area(ime_buffer);
00444               return (IME_UNUSED_KEY);
00445        }
00446 
00447        /* for unnormal keys */
00448        if (Lookups_Num > 0) {
00449               /* need commit the first candidate */
00450               strcpy((char *)Commit_Buf, (char *)Candidates[0]);
00451               Commit_Len = strlen((char *)Commit_Buf);
00452               Commit_And_Clean_All_Area(ime_buffer);
00453        }
00454 
00455         return(IME_UNUSED_KEY);
00456 }