Back to index

im-sdk  12.3.91
codepoint_filter.c
Go to the documentation of this file.
00001 /*
00002   Copyright 2002-2003 Sun Microsystems, Inc. All Rights Reserved.
00003 
00004   Permission is hereby granted, free of charge, to any person obtaining a
00005   copy of this software and associated documentation files (the
00006   "Software"), to deal in the Software without restriction, including
00007   without limitation the rights to use, copy, modify, merge, publish,
00008   distribute, sublicense, and/or sell copies of the Software, and to
00009   permit persons to whom the Software is furnished to do so, subject to
00010   the following conditions: The above copyright notice and this
00011   permission notice shall be included in all copies or substantial
00012   portions of the Software.
00013 
00014 
00015   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00016   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00017   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00018   IN NO EVENT SHALL THE OPEN GROUP OR SUN MICROSYSTEMS, INC. BE LIABLE
00019   FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
00020   CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
00021   THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE EVEN IF
00022   ADVISED IN ADVANCE OF THE POSSIBILITY OF SUCH DAMAGES.
00023 
00024 
00025   Except as contained in this notice, the names of The Open Group and/or
00026   Sun Microsystems, Inc. shall not be used in advertising or otherwise to
00027   promote the sale, use or other dealings in this Software without prior
00028   written authorization from The Open Group and/or Sun Microsystems,
00029   Inc., as applicable.
00030 
00031 
00032   X Window System is a trademark of The Open Group
00033 
00034   OSF/1, OSF/Motif and Motif are registered trademarks, and OSF, the OSF
00035   logo, LBX, X Window System, and Xinerama are trademarks of the Open
00036   Group. All other trademarks and registered trademarks mentioned herein
00037   are the property of their respective owners. No right, title or
00038   interest in or to any trademark, service mark, logo or trade name of
00039   Sun Microsystems, Inc. or its licensors is granted.
00040 
00041 */
00042 
00043 #ifdef HAVE_CONFIG_H
00044 #include <config.h>
00045 #endif
00046 
00047 #ifdef HAVE_STRING_H
00048 #include <string.h>
00049 #endif
00050 
00051 #include <stdio.h>
00052 #include <strings.h>
00053 #include <stdlib.h>
00054 #include <wchar.h>
00055 #include "SunIM.h"
00056 #include "codepoint_im.h"
00057 #include "unit_input.h"
00058 #include "codepoint_filter.h"
00059 #include "logf.h"
00060 
00061 int Check_Input_Type(unit_table) 
00062      TableStruct *unit_table;
00063 {
00064   if (!strcmp((char *)unit_table->InputType, (char *)"HEX"))
00065     return(0); 
00066   return(1); 
00067 }
00068 
00069 int Is_UsedCodes_Key(unit_table, key)
00070      TableStruct *unit_table;
00071      int key;
00072 {
00073   if ( index(unit_table->UsedCodes, key) )
00074     return(1);
00075   else
00076     return(0);
00077 }
00078 
00079 int Is_Commit_Key(unit_table, key)
00080      TableStruct *unit_table;
00081      int key;
00082 {
00083   if (key == RETURN_KEY || key == SPACE_KEY)
00084     return(1);
00085   else
00086     return(0);
00087 }
00088 
00089 int Is_BackSpace_Key(unit_table, key)
00090      TableStruct *unit_table;
00091      int key;
00092 {
00093   if (key == BACKSPACE_KEY || key == DELETE_KEY)
00094     return(1);
00095   else
00096     return(0);
00097 }
00098 
00099 int Is_ClearAll_Key(unit_table, key)
00100      TableStruct *unit_table;
00101      int key;
00102 {
00103   if (key == ESC_KEY)
00104     return(1);
00105   else
00106     return(0);
00107 }
00108 
00109 int commit_candidate(ime_buffer, unit_table)
00110      IMEBufferRec *ime_buffer;
00111      TableStruct *unit_table;
00112 {
00113   char *ret;
00114   char tmpstr[80];
00115   unsigned int *from_buf;
00116   unsigned char *to_buf;
00117 
00118   esc_key_flag = 0;
00119   from_buf = (unsigned int *)calloc(10, sizeof(unsigned int));
00120   to_buf = (unsigned char *)calloc(256, sizeof(unsigned char));
00121 
00122   (void)memset((char *)tmpstr,'\0',sizeof(tmpstr));
00123   if (!Check_Input_Type(unit_table)){
00124     sprintf(tmpstr,"0X%s",Preedit_Buf);
00125   }else{
00126     sprintf(tmpstr,"0%s",Preedit_Buf);
00127   }
00128   from_buf[0] = (wchar_t)strtol(tmpstr,&ret, 0);
00129   if (from_buf[0] == IM_VK_ENTER) {
00130     return RETURN_KEY;
00131   }
00132   from_buf[1] = L'\0'; 
00133   convert_UCS4_to_UTF8(from_buf, to_buf);
00134   (void)strcpy((char *)Commit_Buf, (char *)to_buf);
00135   Commit_Len = strlen((char *)Commit_Buf);
00136   log_f("Inside commit_candidate, Commit_Buf <%s>\n",Commit_Buf);
00137   IME_Status = IME_COMMIT;
00138   free(from_buf);
00139   free(to_buf);
00140   return(0);
00141 }
00142 
00143 void warning_bell()
00144 {
00145 }
00146 
00147 /* return value:  IME_NOT_USED_KEY:  if IME not use this key, return */ 
00148 /*                                   this key to systerm directly    */
00149 /*                IME_USED_KEY:      if IME has used this key        */
00150 
00151 int codepoint_filter(unit_table, key_event, ime_buffer)
00152      TableStruct *unit_table;
00153      IMEKey key_event;
00154      IMEBufferRec *ime_buffer;
00155 {
00156   int     key, default_input_len;
00157  
00158   ime_buffer->encode = unit_table->Encode;
00159   default_input_len = unit_table->Default_Input;
00160 
00161   IME_Status = 0;
00162 
00163   key = map_keyevent_to_imekey(unit_table, key_event);
00164   log_f("codepoint_filter: map_keyevent_to_imekey: return key:0x%x\n", key);
00165  
00166   if (key == IME_NOT_USED_KEY){
00167     log_f("IME_NOT_USED_KEY \n");
00168     return(IME_NOT_USED_KEY);
00169   }
00170 
00171   if (esc_key_flag)
00172     default_input_len = unit_table->Max_Input; 
00173 
00174   if (Is_UsedCodes_Key(unit_table, key)){
00175     /* if not key by key, and is selectkey, and Lookup Mode */
00176     /* directly select */
00177   
00178     Input_Buf[Input_Len] = key;
00179     Input_Len ++;
00180     Input_Buf[Input_Len] = 0;
00181   
00182     log_f("Input_Len:%d\n",Input_Len);
00183 
00184     if (Input_Len == default_input_len){
00185       Preedit_Buf[Preedit_Len] = key;
00186       Preedit_Len += 1;
00187       Preedit_Buf[Preedit_Len] = '\0';
00188       if (commit_candidate(ime_buffer, unit_table) == RETURN_KEY) {
00189         Preedit_Len = 0;
00190         Preedit_CaretPos = 0;
00191         Input_Len = 0;
00192         Input_Buf[Input_Len]='\0';
00193         IME_Status |= IME_PREEDIT_AREA; 
00194         return RETURN_KEY;
00195       }
00196       Preedit_Len = 0;
00197       Preedit_CaretPos = 0;
00198       Input_Len = 0;
00199       Input_Buf[Input_Len]='\0';
00200       IME_Status |= IME_PREEDIT_AREA; 
00201       return(IME_USED_KEY);
00202     }
00203 
00204     Preedit_Buf[Preedit_Len] = key;
00205     Preedit_Len += 1;
00206     Preedit_Buf[Preedit_Len] = '\0';
00207     Preedit_CaretPos = Preedit_Len;
00208     IME_Status = IME_PREEDIT_AREA;
00209     return(IME_USED_KEY);
00210   }
00211 
00212   if (Is_ClearAll_Key(unit_table, key)) { 
00213     /* Esc , clear preedit and so on */
00214     log_f("ESC_KEY\n");
00215     if (!esc_key_flag)
00216       esc_key_flag = 1;
00217     else
00218       esc_key_flag = 0;
00219 
00220     return(IME_NOT_USED_KEY);
00221   }
00222 
00223   if (Is_BackSpace_Key(unit_table, key)) { /* Back Space & Delete */
00224     log_f("BACKSPACE_KEY, Input_Len:%d, Preedit_Len:%d\n", Input_Len, Preedit_Len);
00225     /*  if no inputted key, directly return this key */
00226     if (Input_Len==0) {
00227       return(IME_NOT_USED_KEY);
00228     }
00229 
00230     Input_Len --;
00231     Input_Buf[Input_Len] = '\0';
00232     Preedit_Len -= 1;
00233     Preedit_Buf[Preedit_Len] = '\0';
00234     Preedit_CaretPos = Preedit_Len;
00235     IME_Status = IME_PREEDIT_AREA;
00236 
00237     return(IME_USED_KEY);
00238   }
00239 
00240   if (Is_Commit_Key(unit_table, key)) { /* space or return key */
00241     log_f("SPACE/RETURN KEY\n");
00242     if (Input_Len==0){
00243       return(IME_NOT_USED_KEY);
00244     }
00245 
00246     /* if any keys and key is Space key and mode is not keybykey */
00247     /* then search and display the candidates */
00248 
00249     if (Preedit_Len > 0){
00250       log_f("RETURN/SPACE key: Input_Buf:%s\n",Input_Buf);
00251       log_f("RETURN/SPACE key: Preedit_Buf:%s\n",Preedit_Buf);
00252       commit_candidate(ime_buffer, unit_table);
00253     }
00254     /* clear Preedit_Buf and Input_Buf */ 
00255     Preedit_Len = 0;
00256     Preedit_CaretPos = 0;
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   return(IME_USED_KEY);
00268 }
00269 
00270 /*********************** Key Mapping ***********************/
00271 
00272 /*  get the keys that IME will use */
00273 int map_keyevent_to_imekey(TableStruct *unit_table, IMEKey key_event)
00274 {
00275   int keycode, keystatus, keychar;
00276 
00277   keycode = key_event->keyCode;
00278   keychar = key_event->keyChar;
00279   keystatus = key_event->modifier;
00280 
00281   log_f("codepoint_filter: keycode: 0x%x, keychar:0x%x, keystatus: 0x%x\n", keycode, keychar, keystatus);
00282   /* normal status */
00283   if (keystatus  == 0) {
00284     /* Function Keys */
00285     if (keychar == 0) {
00286       log_f(" Function Key :%d\n", keycode);
00287       switch (keycode) {
00288       case IM_VK_ESCAPE:
00289        return(ESC_KEY);
00290 
00291       case IM_VK_BACK_SPACE:
00292        return(BACKSPACE_KEY);
00293 
00294       case IM_VK_ENTER:
00295        return(RETURN_KEY);
00296 
00297       case IM_VK_INSERT:
00298        return(INSERT_KEY);
00299 
00300       case IM_VK_DELETE:
00301        return(DELETE_KEY);
00302 
00303       case IM_VK_HOME:
00304        return(HOME_KEY);
00305 
00306       case IM_VK_END:
00307        return(END_KEY);
00308 
00309       case IM_VK_PAGE_UP:
00310        return(PAGEUP_KEY);
00311 
00312       case IM_VK_PAGE_DOWN:
00313        return(PAGEDOWN_KEY);
00314 
00315       }
00316       return(IME_NOT_USED_KEY);
00317     } else {
00318       /*  normal key  */
00319       log_f("codepoint_filter:  Normal Key :0x%x, %c\n", keychar, keychar);
00320       return(keychar);
00321     }
00322   }
00323 
00324   /*  Shift status */
00325   if (keystatus == IM_SHIFT_MASK) {
00326     if (keychar != 0) {
00327       /*  normal key  */
00328       log_f("codepoint_filter:  Ascii Key :0x%x, %c\n", keychar, keychar);
00329       return(keychar);
00330     }
00331   }
00332   return (IME_NOT_USED_KEY);
00333 }
00334 
00335 int convert_UCS4_to_UTF8 (unsigned int *from_buf, unsigned char *to_buf) {
00336   int to_len = 0;
00337   unsigned char *ptr = to_buf;
00338                                                                                               
00339   if ( *from_buf <= 0x7F) {
00340     *ptr++ = (unsigned char) *from_buf;
00341     to_len++;
00342     from_buf++;
00343   } else if ( *from_buf <= 0x07FF ) {
00344     *ptr++= (unsigned char) ((*from_buf >> 6) | 0xC0 );
00345     *ptr++= (unsigned char) ((*from_buf & 0x3F) | 0x80 );
00346     to_len+=2;
00347     from_buf++;
00348   } else if ( *from_buf <= 0xFFFF ) {
00349     *ptr++= (unsigned char) ((*from_buf >> 12) | 0xE0 );
00350     *ptr++= (unsigned char) ((*from_buf >> 6 & 0x3F) | 0x80 );
00351     *ptr++= (unsigned char) ((*from_buf & 0x3F) | 0x80 );
00352     to_len+=3;
00353     from_buf++;
00354   } else if ( *from_buf <= 0x1FFFFF ) {
00355     *ptr++= (unsigned char) ((*from_buf >> 18) | 0xF0 );
00356     *ptr++= (unsigned char) (((*from_buf >> 12) & 0x3F) | 0x80 );
00357     *ptr++= (unsigned char) ((*from_buf >> 6 & 0x3F) | 0x80 );
00358     *ptr++= (unsigned char) ((*from_buf & 0x3F)| 0x80 );
00359     to_len+=4;
00360     from_buf++;
00361   }
00362   log_f("to_len [%d]\n",to_len);
00363   return (to_len);
00364 }