Back to index

im-sdk  12.3.91
le_keyevent.c
Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include "le_info.h"
00003 #include "le_session.h"
00004 
00005 #include "wtt_isc.h"
00006 #include "thai_key.h"
00007 #include "thai_common.h"
00008 #include "encode.h"
00009 
00010 void encode_commit(iml_session_t * s, int encode,
00011                  unsigned char *commit_buf);
00012 void encode_draw_status(iml_session_t * s, int encode,
00013                      unsigned char *status_buf);
00014 void le_commit_key(iml_session_t * s, unsigned char key);
00015 void le_status_draw(iml_session_t * s, int on);
00016 int le_session_map_keyevent_to_immkey(IMKeyEventStruct * key_event);
00017 int le_session_map_keyevent_to_imekey(IMKeyEventStruct * key_event);
00018 void le_session_process_key_output(iml_session_t * s, unsigned char key);
00019 
00020 extern char *wtt_isc_mode_names[];
00021 extern LeInfoRec *le_info;
00022 
00023 /* process the keyboard event */
00024 void le_session_process_key_event(iml_session_t * s,
00025                               IMKeyListEvent * keylistevent)
00026 {
00027     int key_ret, input_mode_id;
00028     IMKeyEventStruct *key_event =
00029        (IMKeyEventStruct *) keylistevent->keylist;
00030 
00031     key_ret = le_session_map_keyevent_to_immkey(key_event);
00032 
00033     switch (key_ret) {
00034        /* English status <----> Thai status */
00035     case ENGLISH_THAI_SWITCH_KEY:
00036        le_session_set_conversion_status(s, CONVERSION_OFF);
00037        return;
00038 
00039        /* Roundly Switch Input methods */
00040     case MODE_SWITCH_KEY:
00041        input_mode_id = le_session_get_isc_mode(s);
00042        input_mode_id = (input_mode_id + 1) % WTT_ISC_MODES_NUM;
00043        le_session_save_isc_mode(s, input_mode_id);
00044        le_status_draw(s, CONVERSION_ON);
00045        return;
00046     }
00047 
00048     key_ret = le_session_map_keyevent_to_imekey(key_event);
00049     if (key_ret == IME_NOT_USED_KEY) {
00050        le_session_save_previous_char(s, 0);
00051        le_iml_sendback_key(s, key_event);
00052        return;
00053     }
00054 
00055     le_session_process_key_output(s, key_ret);
00056 }
00057 
00058 /* filter keys that used by Input Methods Manager */
00059 int le_session_map_keyevent_to_immkey(IMKeyEventStruct * key_event)
00060 {
00061     int keycode, keystatus, keychar;
00062 
00063     keycode = key_event->keyCode;
00064     keychar = key_event->keyChar;
00065     keystatus = key_event->modifier;
00066 
00067     DEBUG_printf("keycode: 0x%x, keychar:0x%x, keystatus: 0x%x\n", keycode,
00068                keychar, keystatus);
00069 
00070     /* Function Keys */
00071     if (keycode == IM_VK_F2)
00072        return (MODE_SWITCH_KEY);
00073 
00074     /* Ctrl + Space */
00075     if ((keycode == IM_VK_SPACE) && (keystatus & IM_CTRL_MASK))
00076        return (ENGLISH_THAI_SWITCH_KEY);
00077 
00078     return (IMM_NOT_USED_KEY);
00079 }
00080 
00081 /*  get the keys that IME will use */
00082 int le_session_map_keyevent_to_imekey(IMKeyEventStruct * key_event)
00083 {
00084     int keycode, keystatus, keychar;
00085 
00086     keycode = key_event->keyCode;
00087     keychar = key_event->keyChar;
00088     keystatus = key_event->modifier;
00089 
00090     /* fix bug 4700325: when switch to Thai IM, Numpad doesn't work as expected */
00091     if (keycode >= IM_VK_NUMPAD0 && keycode <= IM_VK_DIVIDE)
00092        return (IME_NOT_USED_KEY);
00093 
00094     /* fix bug 4708815: Thai IM gives English character */
00095     if (keycode >= IM_VK_COMMA && keycode <= IM_VK_CLOSE_BRACKET) {
00096        if (!(keystatus & IM_CTRL_MASK) &&
00097            !(keystatus & IM_META_MASK) &&
00098            !(keystatus & IM_ALT_MASK) && !(keystatus & IM_ALT_GRAPH_MASK))
00099            return (keychar);
00100     }
00101 
00102     return (IME_NOT_USED_KEY);
00103 }
00104 
00105 void le_status_draw(iml_session_t * s, int on)
00106 {
00107     char status_string[128];
00108     char *mode_string;
00109     int input_mode_id;
00110 
00111     sprintf(status_string, "[ %s ]", (char *) ENGLISH_LANG_NAME);
00112     if (on == CONVERSION_ON) {
00113         input_mode_id = le_session_get_isc_mode(s);
00114         mode_string = (char *) wtt_isc_mode_names[input_mode_id];
00115        sprintf(status_string, "[ %s ] [ %s ]", (char *) THAI_LANG_NAME,
00116                mode_string);
00117     }
00118 
00119     encode_draw_status(s, ENCODE_EUCTH, status_string);
00120 }
00121 
00122 void le_commit_key(iml_session_t * s, unsigned char key)
00123 {
00124     unsigned char key_string[2];
00125     key_string[0] = key;
00126     key_string[1] = 0;
00127 
00128     encode_commit(s, ENCODE_EUCTH, key_string);
00129 }
00130 
00131 void le_session_process_key_output(iml_session_t * s, unsigned char key)
00132 {
00133     int input_mode_id;
00134     int keyboard_id;
00135     int ret;
00136 
00137     unsigned char previous_char, following_char;
00138 
00139     input_mode_id = le_session_get_isc_mode(s);
00140     keyboard_id = le_session_get_keyboard_mode(s);
00141 
00142     DEBUG_printf
00143        ("le_session_process_key_output: Mode_ID:%d, nKeyboardID:%d\n",
00144         input_mode_id, keyboard_id);
00145 
00146     previous_char = le_session_get_previous_char(s);
00147     following_char =
00148        ThaiKeymapList_Item_Get_Symbol(le_info->keymap_list, keyboard_id,
00149                                    key);
00150 
00151     ret = THAI_isaccepted(following_char, previous_char, input_mode_id);
00152     if (ret == True) {
00153        le_session_save_previous_char(s, following_char);
00154        le_commit_key(s, following_char);
00155     }
00156 #if 0
00157     if (previous_char) {
00158        printf("previous_char: %c\t", previous_char);
00159     } else {
00160        printf("previous_char: 0\t");
00161     }
00162 
00163     if (following_char) {
00164        printf("following_char: %c\t", following_char);
00165     } else {
00166        printf("following_char: 0\t");
00167     }
00168 
00169     printf("accepted: %s\n", ret ? "True" : "False");
00170 #endif
00171 }
00172 
00173 int encode_to_unicode(int encode, unsigned char *from, int from_len,
00174                     UTFCHAR * to, int to_len)
00175 {
00176     int to_left, ret;
00177     char *to_ptr;
00178 
00179     DEBUG_printf("encode_to_unicode: from:%s, encode:%d\n", from, encode);
00180     to_left = to_len;
00181     ret =
00182        Convert_Native_To_UTF16(encode, from, from_len, (char **) &to,
00183                             (size_t *) & to_left);
00184     DEBUG_printf("encode_to_unicode: return:%d, to_left:%d\n", ret,
00185                to_left);
00186     if (ret == -1) {
00187        to[0] = (UTFCHAR) 0;
00188     } else {
00189        if (to_left > 2) {
00190            to_ptr = (char *) to;
00191            to_ptr[to_len - to_left] = 0;
00192            to_ptr[to_len - to_left + 1] = 0;
00193        }
00194     }
00195     return (ret);
00196 }
00197 
00198 void encode_draw_status(iml_session_t * s, int encode,
00199                      unsigned char *status_buf)
00200 {
00201 #define MAX_STATUS_CHAR_NUM   32
00202     static UTFCHAR Status_Buffer[MAX_STATUS_CHAR_NUM * 2];
00203     int status_len;
00204     int ret;
00205 
00206     status_len = strlen((char *) status_buf);
00207     if (status_len <= 0)
00208        return;
00209 
00210     ret =
00211        encode_to_unicode(encode, status_buf, status_len, Status_Buffer,
00212                        MAX_STATUS_CHAR_NUM * 2);
00213     if (ret != -1) {
00214        le_iml_status_draw(s, Status_Buffer);
00215     }
00216 }
00217 
00218 void encode_commit(iml_session_t * s, int encode,
00219                  unsigned char *commit_buf)
00220 {
00221 #define MAX_COMMIT_CHAR_NUM   32
00222     static UTFCHAR Commit_Buffer[MAX_COMMIT_CHAR_NUM * 2];
00223     int commit_len;
00224     int ret;
00225 
00226     commit_len = strlen((char *) commit_buf);
00227     if (commit_len <= 0)
00228        return;
00229 
00230     ret =
00231        encode_to_unicode(encode, commit_buf, commit_len, Commit_Buffer,
00232                        MAX_COMMIT_CHAR_NUM * 2);
00233     if (ret != -1) {
00234        le_iml_commit(s, Commit_Buffer);
00235     }
00236 }