Back to index

im-sdk  12.3.91
le_keyevent.c
Go to the documentation of this file.
00001 #include <stdio.h>
00002 
00003 #include "le_info.h"
00004 #include "le_desktop.h"
00005 #include "le_session.h"
00006 
00007 extern LeInfoRec *le_info;
00008 
00009 int le_session_get_imm_key_type(iml_session_t * s,
00010                             IMKeyEventStruct * key_event);
00011 
00012 int le_session_process_key_event_for_ime_module(iml_session_t * s,
00013                                           IMKeyEventStruct * key_event);
00014 
00015 int le_session_process_key_event_for_ime_manager(iml_session_t * s,
00016                                            IMKeyEventStruct * key_event);
00017 
00018 /* process the keyboard event */
00019 LeResult le_session_process_key_event(iml_session_t * s,
00020                                   IMKeyListEvent * keylistevent)
00021 {
00022     LeSessionContextRec *le_session_context = NULL;
00023     LeDesktopContextRec *le_desktop_context = NULL;
00024 
00025     IMKeyEventStruct *key_event = (IMKeyEventStruct *) keylistevent->keylist;
00026     int key_ret;
00027 
00028     le_desktop_context =
00029        (LeDesktopContextRec *) le_session_get_desktop_context(s);
00030 
00031     DEBUG_printf
00032        (" le_session_proc_key_event: desktop: %p, current_session:%p\n",
00033         s->desktop, s);
00034 
00035     le_session_context =
00036        (LeSessionContextRec *) le_session_get_session_context(s);
00037     if (le_session_context == NULL) {
00038        le_iml_sendback_key(s, key_event);
00039     }
00040 
00041     key_ret = le_session_process_key_event_for_ime_manager(s, key_event);
00042     if (key_ret == IMM_KEY_USED)
00043        return (LE_OK);
00044 
00045     key_ret = le_session_process_key_event_for_ime_module(s, key_event);
00046     if (key_ret == IME_UNUSED_KEY)
00047        le_iml_sendback_key(s, key_event);
00048 
00049     return (LE_OK);
00050 }
00051 
00052 int le_session_process_key_event_for_ime_manager(iml_session_t * s,
00053                                            IMKeyEventStruct *
00054                                            key_event)
00055 {
00056     int imm_key_type, status;
00057 
00058     status = LE_OK;
00059 
00060     imm_key_type = le_session_get_imm_key_type(s, key_event);
00061     DEBUG_printf
00062        ("le_session_process_key_event_for_ime_manager, imm_key_type: %d\n",
00063         imm_key_type);
00064 
00065     if (imm_key_type == IMM_KEY_NOT_USED)
00066        return (IMM_KEY_NOT_USED);
00067 
00068     switch (imm_key_type) {
00069     case IMM_KEY_TOGGLE_CONVERSION:
00070        status = le_session_toggle_conversion_status(s);
00071        break;
00072     case IMM_KEY_SWITCH_IME_ROUND:
00073        status = le_session_switch_to_next_ime_module(s);
00074        break;
00075     case IMM_KEY_TOGGLE_QJBJ:
00076        status = le_session_toggle_qjbj_status(s);
00077        break;
00078     case IMM_KEY_TOGGLE_PUNCTUATION:
00079        status = le_session_toggle_punct_status(s);
00080        break;
00081     }
00082 
00083     return (status == LE_OK) ? (IMM_KEY_USED) : (IMM_KEY_NOT_USED);
00084 }
00085 
00086 int le_iml_commit_encode_string(iml_session_t * s, int encoding,
00087                             char *commit_str)
00088 {
00089     UTFCHAR tmp_buf[1024];
00090     UTFCHAR *tmp_ptr = tmp_buf;
00091     int from_len, to_left, ret;
00092 
00093     if (commit_str == NULL)
00094        return (IME_FAIL);
00095 
00096     if (encoding == ENCODE_INVALID)
00097        return (IME_FAIL);
00098 
00099     from_len = strlen(commit_str);
00100     to_left = 1024;
00101     memset(tmp_buf, 0, sizeof(UTFCHAR) * 1024);
00102     ret = Convert_Native_To_UTF16(encoding,
00103                               commit_str,
00104                               from_len,
00105                               (char **) &tmp_ptr,
00106                               (size_t *) & to_left);
00107     if (ret == -1)
00108        return (IME_FAIL);
00109 
00110     le_iml_commit(s, tmp_buf);
00111     return (IME_OK);
00112 }
00113 
00114 int le_session_proc_qjbj(iml_session_t * s, IMKeyEventStruct * key_event)
00115 {
00116     char *commit_buf;
00117     int keycode, keystatus, keychar;
00118 
00119     keycode = key_event->keyCode;
00120     keychar = key_event->keyChar;
00121     keystatus = key_event->modifier;
00122 
00123     DEBUG_printf("QJBJ PROCESS\n");
00124     /* normal ASCII key */
00125     if ((keychar != 0) && (keystatus == IM_SHIFT_MASK || keystatus == 0)) {
00126        commit_buf = (char *) get_qj_str(keychar);
00127        if (commit_buf != NULL) {
00128            le_iml_commit_encode_string(s, ENCODE_UTF8, commit_buf);
00129            return (IME_OK);
00130        }
00131     }
00132 
00133     return (IME_UNUSED_KEY);
00134 }
00135 
00136 int le_session_proc_punct(iml_session_t * s, IMKeyEventStruct * key_event)
00137 {
00138     char *commit_buf;
00139     int keycode, keystatus, keychar;
00140 
00141     keycode = key_event->keyCode;
00142     keychar = key_event->keyChar;
00143     keystatus = key_event->modifier;
00144 
00145     DEBUG_printf("QJBJ PROCESS\n");
00146     /* normal ASCII key */
00147     if ((keychar != 0) && (keystatus == IM_SHIFT_MASK || keystatus == 0)) {
00148        commit_buf = (char *) get_punct_str(keychar);
00149        if (commit_buf != NULL) {
00150            le_iml_commit_encode_string(s, ENCODE_UTF8, commit_buf);
00151            return (IME_OK);
00152        }
00153     }
00154 
00155     return (IME_UNUSED_KEY);
00156 }
00157 
00158 int le_session_process_key_event_for_ime_module(iml_session_t * s,
00159                                           IMKeyEventStruct * key_event)
00160 {
00161     int key_ret = IME_UNUSED_KEY;
00162     ImeKeyRec ime_keyevent;
00163     ImeModuleRec *current_ime_module;
00164 
00165     LeSessionContextRec *le_session_context = NULL;
00166 
00167     DEBUG_printf("  le_session_process_key_event_for_ime_module\n");
00168 
00169     current_ime_module =
00170        (ImeModuleRec *) le_session_get_current_ime_module(s);
00171     if (current_ime_module == NULL || current_ime_module->methods == NULL
00172        || current_ime_module->methods->ImeProcessKeyEvent == NULL)
00173        return (IME_UNUSED_KEY);
00174 
00175     le_session_context =
00176        (LeSessionContextRec *) le_session_get_session_context(s);
00177     if (le_session_context == NULL)
00178        return (IME_UNUSED_KEY);
00179 
00180     if (le_session_context->current_qjbj_status == QJBJ_FULLWIDTH) {
00181        key_ret = le_session_proc_qjbj(s, key_event);
00182        if (key_ret == IME_OK)
00183            return (IME_OK);
00184     }
00185 
00186     /* set ImeKeyRec for ime module to do filter keyevents */
00187     ime_keyevent.keycode = key_event->keyCode;
00188     ime_keyevent.keychar = key_event->keyChar;
00189     ime_keyevent.modifier = key_event->modifier;
00190 
00191     DEBUG_printf("    begin do ImeProcessKeyEvent for ImeModule: %p\n", current_ime_module);
00192     key_ret = current_ime_module->methods->ImeProcessKeyEvent((ImeInputContext) le_session_context, 
00193                                                                &ime_keyevent);
00194 
00195     DEBUG_printf("    After do ImeProcessKeyEvent for ImeModule: %p, return %d\n",
00196                   current_ime_module, key_ret);
00197 
00198     if (key_ret == IME_UNUSED_KEY) {
00199         if (le_session_context->current_punct_status == PUNCT_CHINESE) {
00200             key_ret = le_session_proc_punct(s, key_event);
00201         }
00202     }
00203     return (key_ret);
00204 }
00205 
00206 ImmKeybindingRec imm_keybindings_default[] = {
00207     {IME_VK_SPACE, 0, IME_CTRL_MASK, IMM_KEY_TOGGLE_CONVERSION}
00208     ,
00209     {IME_VK_SHIFT, 0, IME_CTRL_MASK, IMM_KEY_SWITCH_IME_ROUND}
00210     ,
00211     {IME_VK_CONTROL, 0, IME_SHIFT_MASK, IMM_KEY_SWITCH_IME_ROUND}
00212     ,
00213     {IME_VK_SPACE, 0, IME_SHIFT_MASK, IMM_KEY_TOGGLE_QJBJ}
00214     ,
00215     {IME_VK_PERIOD, 0, IME_CTRL_MASK, IMM_KEY_TOGGLE_PUNCTUATION}
00216     ,
00217 };
00218 
00219 int le_session_get_imm_key_type(iml_session_t * s,
00220                             IMKeyEventStruct * key_event)
00221 {
00222     int i, num_keybindings;
00223     ImmKeybindingRec **keybindings;
00224 
00225     int keycode, keychar, modifier;
00226 
00227     keycode = key_event->keyCode;
00228     keychar = key_event->keyChar;
00229     modifier = key_event->modifier;
00230 
00231     DEBUG_printf("keycode: 0x%x, keychar: 0x%x, modifier: 0x%x\n", keycode,
00232                keychar, modifier);
00233 
00234     if (le_info == NULL)
00235        return (IMM_KEY_NOT_USED);
00236 
00237     num_keybindings = le_info->num_imm_keybindings;
00238     keybindings = le_info->imm_keybindings;
00239 
00240     DEBUG_printf("num_keys: %d\n", num_keybindings);
00241     if (keybindings != NULL) {
00242        for (i = 0; i < num_keybindings; i++) {
00243            if (keycode == keybindings[i]->keycode &&
00244               modifier == keybindings[i]->modifier) {
00245               return (keybindings[i]->defined_type);
00246            }
00247        }
00248     }
00249 
00250     num_keybindings =
00251        sizeof(imm_keybindings_default) / sizeof(ImmKeybindingRec);
00252     for (i = 0; i < num_keybindings; i++) {
00253        if (keycode == imm_keybindings_default[i].keycode &&
00254            modifier == imm_keybindings_default[i].modifier) {
00255            return (imm_keybindings_default[i].defined_type);
00256        }
00257     }
00258 
00259     return (IMM_KEY_NOT_USED);
00260 }