Back to index

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