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 char *iiim_keycode_to_name(short keycode);
00006 MSymbol le_session_map_keyevent_to_m17n_key(IMKeyEventStruct * key_event);
00007 
00008 extern LeInfoRec *le_info;
00009 
00010 /* process the keyboard event */
00011 void le_session_process_key_event(iml_session_t * s,
00012                               IMKeyListEvent * keylistevent)
00013 {
00014     LeSessionContextRec *le_session_context;
00015 
00016     MInputContext *ic;
00017     MSymbol m17n_key;
00018     MConverter *converter;
00019     MText *produced;
00020 
00021     int ret;
00022     UTFCHAR buf[256];
00023 
00024     IMKeyEventStruct *key_event =
00025        (IMKeyEventStruct *) keylistevent->keylist;
00026 
00027     le_session_context =
00028        (LeSessionContextRec *) le_session_get_session_context(s);
00029     m17n_key = le_session_map_keyevent_to_m17n_key(key_event);
00030 
00031     if (le_session_context == NULL ||
00032        le_session_context->ic == (MInputContext *) Mnil
00033        || m17n_key == Mnil) {
00034        le_iml_sendback_key(s, key_event);
00035        return;
00036     }
00037 
00038     DEBUG_printf("m17n_key: %s\n", msymbol_name(m17n_key));
00039     if (m17n_key == le_info->Mkey_switch_next_im) {
00040         le_session_switch_to_next_input_method(s);
00041         return;
00042     }
00043 
00044     if (m17n_key == le_info->Mkey_english_native) {
00045        le_session_set_conversion_status(s, CONVERSION_OFF);
00046        return;
00047     }
00048 
00049     ic = le_session_context->ic;
00050     ret = minput_filter(ic, m17n_key, NULL);
00051     if (ret) {
00052        DEBUG_printf("minput_filter return 1\n");
00053        return;
00054     }
00055 
00056     produced = mtext();
00057 
00058     ret = minput_lookup(ic, m17n_key, NULL, produced);
00059     if (mtext_len(produced) <= 0) {
00060        DEBUG_printf("minput_lookup return -1\n");
00061        le_iml_sendback_key(s, key_event);
00062        return;
00063     }
00064 
00065     converter = le_info->converter;
00066 
00067     memset(buf, 0, sizeof(buf));
00068     mconv_rebind_buffer(converter, (unsigned char *) buf, sizeof(buf));
00069     mconv_encode(converter, produced);
00070     buf[converter->nbytes] = 0;
00071     m17n_object_unref(produced);
00072 
00073     if (buf[0]) {
00074        DEBUG_printf("commit buf: 0x%x\n", buf[0]);
00075        le_iml_commit(s, buf);
00076     }
00077 
00078     return;
00079 }
00080 
00081 MSymbol le_session_map_keyevent_to_m17n_key(IMKeyEventStruct * key_event)
00082 {
00083     int keycode, keychar, modifier;
00084     char key_name[256];
00085     char key_string[256];
00086 
00087     keycode = key_event->keyCode;
00088     keychar = key_event->keyChar;
00089     modifier = key_event->modifier;
00090 
00091     DEBUG_printf("keycode: 0x%x, keychar:0x%x, modifier: 0x%x\n",
00092                keycode, keychar, modifier);
00093 
00094     /* Ctrl + Space */
00095     if ((keycode == IM_VK_SPACE) && (modifier & IM_CTRL_MASK))
00096        return le_info->Mkey_english_native;
00097 
00098     /* Ctrl + Shift */
00099     if (((keycode == IM_VK_CONTROL) && (modifier & IM_SHIFT_MASK)) ||
00100         ((keycode == IM_VK_SHIFT) && (modifier & IM_CTRL_MASK)))
00101        return le_info->Mkey_switch_next_im;
00102 
00103     memset(key_name, 0, sizeof(key_name));
00104 
00105     /* Normal Alphameric key */
00106     if (keychar >= 0x20 && keychar <= 0x7e) {
00107 
00108             if ((modifier & IM_CTRL_MASK) &&
00109                 (keychar  >= 'a' && keychar <= 'z'))
00110                 keychar += 'A' - 'a';
00111            key_name[0] = keychar;
00112 
00113     } else if (keychar == 0) {
00114         char *key_str;
00115 
00116         if (keycode == IM_VK_SHIFT ||
00117             keycode == IM_VK_CONTROL ||
00118             keycode == IM_VK_ALT ||
00119             keycode == IM_VK_CAPS_LOCK)
00120             return Mnil;
00121 
00122         key_str = iiim_keycode_to_name(keycode);
00123         DEBUG_printf("iiim_keycode_to_name: key_str: %s\n", key_str? key_str: "NULL");
00124         if (! key_str)
00125             return Mnil;
00126 
00127         strncpy(key_name, key_str, sizeof(key_name));
00128     }
00129 
00130     if (!*key_name)
00131         return Mnil;
00132 
00133     memset(key_string, 0, sizeof(key_string));
00134 
00135     if (modifier & IM_SHIFT_MASK)
00136        strncat(key_string, "S-", sizeof(key_string));
00137     if (modifier & IM_CTRL_MASK)
00138        strncat(key_string, "C-", sizeof(key_string));
00139     if (modifier & IM_META_MASK)
00140        strncat(key_string, "M-", sizeof(key_string));
00141     if (modifier & IM_ALT_MASK)
00142        strncat(key_string, "A-", sizeof(key_string));
00143 
00144     strncat(key_string, key_name, sizeof(key_string));
00145     return msymbol(key_string);
00146 }
00147 
00148 typedef struct {
00149     short keycode;
00150     char *name;
00151 } keycode_name_pair_t;
00152 
00153 static keycode_name_pair_t keycode_name_pairs[] = {
00154     { IM_VK_ENTER,         "Return"},
00155     { IM_VK_BACK_SPACE,    "BackSpace"},
00156     { IM_VK_TAB,           "Tab"},
00157     { IM_VK_CANCEL,        "Cancel"},
00158     { IM_VK_CLEAR,         "Clear"},
00159     { IM_VK_SHIFT,         "Shift"},
00160     { IM_VK_CONTROL,       "Control"},
00161     { IM_VK_ALT,           "Alt"},
00162     { IM_VK_PAUSE,         "Pause"},
00163     { IM_VK_CAPS_LOCK,     "Caps_Lock"},
00164     { IM_VK_ESCAPE,        "Escape"},
00165     { IM_VK_SPACE,         "Space"},
00166     { IM_VK_PAGE_UP,       "Prior"},
00167     { IM_VK_PAGE_DOWN,     "Next"},
00168     { IM_VK_END,           "End"},
00169     { IM_VK_HOME,          "Home"},
00170     { IM_VK_LEFT,          "Left"},
00171     { IM_VK_UP,            "Up"},
00172     { IM_VK_RIGHT,         "Right"},
00173     { IM_VK_DOWN,          "Down"},
00174     { IM_VK_COMMA,         "comma"},
00175     { IM_VK_MINUS,         "minus"},
00176     { IM_VK_PERIOD,        "period"},
00177     { IM_VK_SLASH,         "slash"},
00178     { IM_VK_0,             "0"},
00179     { IM_VK_1,             "1"},
00180     { IM_VK_2,             "2"},
00181     { IM_VK_3,             "3"},
00182     { IM_VK_4,             "4"},
00183     { IM_VK_5,             "5"},
00184     { IM_VK_6,             "6"},
00185     { IM_VK_7,             "7"},
00186     { IM_VK_8,             "8"},
00187     { IM_VK_9,             "9"},
00188     { IM_VK_SEMICOLON,     "semicolon"},
00189     { IM_VK_EQUALS,        "equal"},
00190     { IM_VK_A,             "A"},
00191     { IM_VK_B,             "B"},
00192     { IM_VK_C,             "C"},
00193     { IM_VK_D,             "D"},
00194     { IM_VK_E,             "E"},
00195     { IM_VK_F,             "F"},
00196     { IM_VK_G,             "G"},
00197     { IM_VK_H,             "H"},
00198     { IM_VK_I,             "I"},
00199     { IM_VK_J,             "J"},
00200     { IM_VK_K,             "K"},
00201     { IM_VK_L,             "L"},
00202     { IM_VK_M,             "M"},
00203     { IM_VK_N,             "N"},
00204     { IM_VK_O,             "O"},
00205     { IM_VK_P,             "P"},
00206     { IM_VK_Q,             "Q"},
00207     { IM_VK_R,             "R"},
00208     { IM_VK_S,             "S"},
00209     { IM_VK_T,             "T"},
00210     { IM_VK_U,             "U"},
00211     { IM_VK_V,             "V"},
00212     { IM_VK_W,             "W"},
00213     { IM_VK_X,             "X"},
00214     { IM_VK_Y,             "Y"},
00215     { IM_VK_Z,             "Z"},
00216     { IM_VK_OPEN_BRACKET,  "bracketleft"},
00217     { IM_VK_BACK_SLASH,    "backslash"},
00218     { IM_VK_CLOSE_BRACKET, "bracketright"},
00219     { IM_VK_NUMPAD0,       "KP_0"},
00220     { IM_VK_NUMPAD1,       "KP_1"},
00221     { IM_VK_NUMPAD2,       "KP_2"},
00222     { IM_VK_NUMPAD3,       "KP_3"},
00223     { IM_VK_NUMPAD4,       "KP_4"},
00224     { IM_VK_NUMPAD5,       "KP_5"},
00225     { IM_VK_NUMPAD6,       "KP_6"},
00226     { IM_VK_NUMPAD7,       "KP_7"},
00227     { IM_VK_NUMPAD8,       "KP_8"},
00228     { IM_VK_NUMPAD9,       "KP_9"},
00229     { IM_VK_MULTIPLY,      "KP_Multiply"},
00230     { IM_VK_ADD,           "KP_Add"},
00231     { IM_VK_SEPARATER,     "KP_Separator"},
00232     { IM_VK_SUBTRACT,      "KP_Subtract"},
00233     { IM_VK_DECIMAL,       "KP_Decimal"},
00234     { IM_VK_DIVIDE,        "KP_Divide"},
00235     { IM_VK_DELETE,        "KP_Delete"},
00236     { IM_VK_NUM_LOCK,      "Num_Lock"},
00237     { IM_VK_SCROLL_LOCK,   "Scroll_Lock"},
00238     { IM_VK_F1,            "F1"},
00239     { IM_VK_F2,            "F2"},
00240     { IM_VK_F3,            "F3"},
00241     { IM_VK_F4,            "F4"},
00242     { IM_VK_F5,            "F5"},
00243     { IM_VK_F6,            "F6"},
00244     { IM_VK_F7,            "F7"},
00245     { IM_VK_F8,            "F8"},
00246     { IM_VK_F9,            "F9"},
00247     { IM_VK_F10,           "F10"},
00248     { IM_VK_F11,           "F11"},
00249     { IM_VK_F12,           "F12"},
00250     { IM_VK_F13,           "F13"},
00251     { IM_VK_F14,           "F14"},
00252     { IM_VK_F15,           "F15"},
00253     { IM_VK_F16,           "F16"},
00254     { IM_VK_F17,           "F17"},
00255     { IM_VK_F18,           "F18"},
00256     { IM_VK_F19,           "F19"},
00257     { IM_VK_F20,           "F20"},
00258     { IM_VK_F21,           "F21"},
00259     { IM_VK_F22,           "F22"},
00260     { IM_VK_F23,           "F23"},
00261     { IM_VK_F24,           "F24"},
00262     { IM_VK_PRINTSCREEN,   "PrintScreen"},
00263     { IM_VK_INSERT,        "Insert"},
00264     { IM_VK_HELP,          "Help"},
00265     { IM_VK_META,          "Meta_L"},
00266     { IM_VK_BACK_QUOTE,    "BackQuote"},
00267     { IM_VK_QUOTE,         "Quote"},
00268     { IM_VK_KP_UP,         "KP_Up"},
00269     { IM_VK_KP_DOWN,       "KP_Down"},
00270     { IM_VK_KP_LEFT,       "KP_Left"},
00271     { IM_VK_KP_RIGHT,      "KP_Right"},
00272     { IM_VK_DEAD_GRAVE,              "dead_grave"},
00273     { IM_VK_DEAD_ACUTE,              "dead_acute"},
00274     { IM_VK_DEAD_CIRCUMFLEX,         "dead_circumflex"},
00275     { IM_VK_DEAD_TILDE,              "dead_tilde"},
00276     { IM_VK_DEAD_MACRON,             "dead_macron"},
00277     { IM_VK_DEAD_BREVE,              "dead_breve"},
00278     { IM_VK_DEAD_ABOVEDOT,           "dead_abovedot"},
00279     { IM_VK_DEAD_DIAERESIS,          "dead_diaeresis"},
00280     { IM_VK_DEAD_ABOVERING,          "dead_abovering"},
00281     { IM_VK_DEAD_DOUBLEACUTE,        "dead_doubleacute"},
00282     { IM_VK_DEAD_CARON,              "dead_caron"},
00283     { IM_VK_DEAD_CEDILLA,            "dead_cedilla"},
00284     { IM_VK_DEAD_OGONEK,             "dead_ogonek"},
00285     { IM_VK_DEAD_IOTA,               "dead_iota"},
00286     { IM_VK_DEAD_VOICED_SOUND,       "dead_voiced_sound"},
00287     { IM_VK_DEAD_SEMIVOICED_SOUND,   "dead_semivoiced_sound"},
00288     { IM_VK_AMPERSAND,               "ampersand"},
00289     { IM_VK_ASTERISK,                "asterisk"},
00290     { IM_VK_QUOTEDBL,                "quotedbl"},
00291     { IM_VK_LESS,                    "less"},
00292     { IM_VK_GREATER,                 "greater"},
00293     { IM_VK_BRACELEFT,               "braceleft"},
00294     { IM_VK_BRACERIGHT,              "braceright"},
00295     { IM_VK_AT,                      "at"},
00296     { IM_VK_COLON,                   "colon"},
00297     { IM_VK_CIRCUMFLEX,              "circumflex"},
00298     { IM_VK_DOLLAR,                  "dollar"},
00299     { IM_VK_EURO_SIGN,               "euro"},  /* ? */
00300     { IM_VK_EXCLAMATION_MARK,        "exclam"},
00301     { IM_VK_INVERTED_EXCLAMATION_MARK,"inverted_exclam"}, /* ? */
00302     { IM_VK_LEFT_PARENTHESIS,        "parenleft"},
00303     { IM_VK_NUMBER_SIGN,             "numbersign"},
00304     { IM_VK_PLUS,                    "plus"},
00305     { IM_VK_RIGHT_PARENTHESIS,       "parenright"},
00306     { IM_VK_UNDERSCORE,              "underscore"},
00307     { IM_VK_FINAL,                   "final"},         /* ? */
00308     { IM_VK_CONVERT,                 "convert"},       /* ? */
00309     { IM_VK_NONCONVERT,              "noconvert"},       /* ? */
00310     { IM_VK_ACCEPT,                  "accept"},       /* ? */
00311     { IM_VK_MODECHANGE,              "modechange"},       /* ? */
00312     { IM_VK_KANA,                    "kana"},       /* ? */
00313     { IM_VK_HANGUL,               "hangul"},       /* ? */
00314     { IM_VK_JUNJA,                "junja"},       /* ? */
00315     { IM_VK_FINAL,                   "final"},       /* ? */
00316     { IM_VK_KANJI,                   "kanji"},       /* ? */
00317     { IM_VK_HANJA,                   "hanja"},       /* ? */
00318     { IM_VK_ALPHANUMERIC,            "alphanumberic"},       /* ? */
00319     { IM_VK_KATAKANA,                "katakana"},       /* ? */
00320     { IM_VK_HIRAGANA,                "rhkiragana"},       /* ? */
00321     { IM_VK_FULL_WIDTH,              "fullwidth"},       /* ? */
00322     { IM_VK_HALF_WIDTH,              "halfwidth"},       /* ? */
00323     { IM_VK_ROMAN_CHARACTERS,        "romancandidates"},       /* ? */
00324     { IM_VK_ALL_CANDIDATES,          "allcandidates"},       /* ? */
00325     { IM_VK_PREVIOUS_CANDIDATE,      "previouscandidate"},       /* ? */
00326     { IM_VK_CODE_INPUT,              "codeinput"},       /* ? */
00327     { IM_VK_JAPANESE_KATAKANA,       "japanese_katakana"},       /* ? */
00328     { IM_VK_JAPANESE_HIRAGANA,       "japanese_hiragana"},       /* ? */
00329     { IM_VK_JAPANESE_ROMAN,          "japanese_roman"},       /* ? */
00330     { IM_VK_CUT,                     "cut"},       /* ? */
00331     { IM_VK_COPY,                    "copy"},       /* ? */
00332     { IM_VK_PASTE,                   "paste"},       /* ? */
00333     { IM_VK_UNDO,                    "undo"},       /* ? */
00334     { IM_VK_AGAIN,                   "again"},       /* ? */
00335     { IM_VK_FIND,                    "find"},       /* ? */
00336     { IM_VK_PROPS,                   "props"},       /* ? */
00337     { IM_VK_STOP,                    "stop"},       /* ? */
00338     { IM_VK_COMPOSE,                 "compose"},       /* ? */
00339     { IM_VK_ALT_GRAPH,               "alt_graph"}       /* ? */
00340 };
00341 
00342 char *iiim_keycode_to_name(short keycode)
00343 {
00344     int total, i;
00345 
00346     total = sizeof(keycode_name_pairs)/sizeof(keycode_name_pair_t);
00347 
00348     for (i = 0; i < total; i++) {
00349        if (keycode_name_pairs[i].keycode == keycode)
00350            return (keycode_name_pairs[i].name);
00351     }
00352 
00353     return NULL;
00354 }