Back to index

im-sdk  12.3.91
key_event.c
Go to the documentation of this file.
00001 /*
00002  * Copyright 2003 Sun Microsystems Inc.
00003  *
00004  * This is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU Lesser General Public
00006  * License as published by the Free Software Foundation; either
00007  * version 2 of the License, or (at your option) any later version.
00008  *
00009  * This library is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * Lesser General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU Lesser General Public
00015  * License along with this library; if not, write to the
00016  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00017  * Boston, MA 02111-1307, USA.
00018  *
00019  * Authors: Karl Park <karl.park@sun.com>
00020  */
00021 
00022 #ifdef HAVE_CONFIG_H
00023 #include <config.h>
00024 #endif
00025 #include <stdio.h>
00026 #include <assert.h>
00027 #include "koinput.h"
00028 #include "key_event.h"
00029 #include "le.h"
00030 #include "le_proc.h"
00031 #include "iml.h"
00032 #include "method.h"
00033 #include "encode.h"
00034 #include "xaux_locale.h"
00035 #include "fullwidth.h"
00036 #include "util.h"
00037 #include "kolelog.h"
00038 
00039 #if 0
00040 void proc_symbole_input_event(iml_session_t *s, IMKeyEventStruct *key_event);
00041 #endif
00042 
00043 static void keyevent_toggle_junjabanja_handler(iml_session_t *s, int junjabanja);
00044 static void keyevent_input_handler(iml_session_t *s, IMKeyEventStruct *key_event);
00045 #if 0
00046 static void proc_key_switch_keyboard_layout_event(iml_session_t *s, int nKeyboardID);
00047 #endif
00048 
00049 extern int          g_number_of_engines;
00050 extern int                    default_engine_id;
00051 extern IMEEngineRec *g_engines[MAX_ENGINE_NUM];
00052 static Bool imekeycmp (IMEKeyEventStruct *key_a, IMEKeyEventStruct *key_b);
00053 static Bool is_hotkey (iml_session_t *s, IMEKeyEventStruct *key_event);
00054 
00055 void
00056 keyevent_handler (iml_session_t *s, IMKeyListEvent *keylistevent)
00057 {
00058   MyDataPerSession *session_data = (MyDataPerSession *) s->specific_data;
00059   MyDataPerDesktop *desktop_data =
00060     (MyDataPerDesktop *)(s->desktop->specific_data);
00061   IMKeyEventStruct *key_event =
00062     (IMKeyEventStruct *) keylistevent->keylist;
00063   IMEBufferMethods mthds = session_data->session_core.ime_methods_table;
00064   IMEBuffer a_buffer = session_data->session_core.ime_buffer;
00065   
00066   INPUT_MODE_T imode_return;
00067   Bool mthd_return;
00068 
00069   KOLE_LOG (LOGDEST_STDOUT, " proc_keyevent:   session:%x\n", s);
00070 
00071   keyevent_input_handler (s, key_event);
00072   
00073   mthd_return = (*mthds->ime_buffer_get_input_mode) (a_buffer,  &imode_return);
00074   if (!mthd_return){
00075     KOLE_LOG (LOGDEST_STDERR,
00076              "keyevent_handler: error in getting current input mode\n");
00077     return;
00078   }
00079   return;
00080 }
00081 
00082 /* filter keys that used by Input Methods Manager */
00083 #if 0
00084 int
00085 map_keyevent_to_immkey(IMKeyEventStruct *key_event) 
00086 {
00087   int keycode, keystatus, keychar;
00088 
00089   keycode = key_event->keyCode;
00090   keychar = key_event->keyChar;
00091   keystatus = key_event->modifier;
00092 
00093   KOLE_LOG (LOGDEST_STDOUT,
00094            "keycode: %d, keychar: %d, status: %d", keycode, keychar, keystatus);
00095 
00096   /*  Shift status */
00097   if (keystatus == IM_SHIFT_MASK) {
00098     switch(keycode){
00099     case IM_VK_SPACE:       return(FULL_HALF_WIDTH_SWITCH_KEY);
00100     case IM_VK_F2:
00101       return(KEYBOARD_LAYOUT_TOGGLE_KEY);
00102     }
00103   }
00104   /*  Control status */
00105   if (keystatus & IM_CTRL_MASK) {
00106     switch(keycode) {
00107     case IM_VK_SPACE:       return(ENGLISH_KOREAN_SWITCH_KEY);
00108     case IM_VK_X:
00109       if(keystatus & IM_SHIFT_MASK){
00110        return(HEX_INPUT_IN_UTF8_MODE_KEY);
00111       } else {
00112        return(HEX_INPUT_IN_EUC_MODE_KEY);
00113       }
00114     case IM_VK_J:           return(SYMBOL_INPUT_MODE_KEY);
00115     case IM_VK_Q:           return(HANJA_CONVERSION_HANJA_KEY);
00116       /*
00117        case IM_VK_W:        return(HANJA_CONVERSION_DEFAULT_KEY);
00118       */
00119     case IM_VK_H:           return(HANJA_CONVERSION_DEFAULT_KEY);
00120     case IM_VK_E:           return(HANJA_CONVERSION_HANGUL_HANJA_KEY);
00121     case IM_VK_R:           return(HANJA_CONVERSION_HANJA_HANGUL_KEY);
00122     }
00123   }
00124 
00125   return (IMM_NOT_USED_KEY);
00126 }
00127 #endif
00128 
00129 void
00130 keyevent_switch_conversion_handler (iml_session_t *s, int on)
00131 {
00132   MyDataPerSession *sd = (MyDataPerSession *) s->specific_data;
00133   MyDataPerDesktop *dd = (MyDataPerDesktop *)(s->desktop->specific_data);
00134   int engine_id;
00135   IMEBufferMethods ime_methods =  sd->session_core.ime_methods_table;
00136   INPUT_MODE_T imt;
00137   Bool mthd_return;
00138   
00139   KOLE_LOG (LOGDEST_STDOUT,
00140            "ENGLISH_KOREAN SWITCH IME KEY, on:%d", on);
00141 
00142   sd->paletteaux_status.on = on;
00143   engine_id = sd->paletteaux_status.engine_id;
00144   ime_methods = g_engines[engine_id]->so_methods;
00145 
00146   if (on == CONVERSION_OFF) {
00147     
00148     (*ime_methods->ime_buffer_conversion_end)(sd->session_core.ime_buffer);
00149 
00150     iml_conversion_off (s);
00151     iml_status_draw (s);
00152     
00153     mthd_return =
00154       (*ime_methods->ime_buffer_get_input_mode)(sd->session_core.ime_buffer, &imt);
00155     if (mthd_return)
00156       le_update_paletteaux_input_mode_info (s, imt);
00157 
00158   } else {
00159 
00160     (*ime_methods->ime_buffer_conversion_start)(sd->session_core.ime_buffer);
00161     
00162     iml_conversion_on(s);
00163     iml_status_draw (s);
00164 
00165     mthd_return =
00166       (*ime_methods->ime_buffer_get_input_mode)(sd->session_core.ime_buffer, &imt);
00167     if (mthd_return)
00168       le_update_paletteaux_input_mode_info (s, imt);
00169     /*
00170       iml_status_draw (s);
00171       if (engine_id != 0xff)
00172       le_switch_engine(s, engine_id);
00173     */
00174   }
00175 #if 0
00176   /* test code */
00177   iml_inst *lp;
00178   iml_inst *rrv = NULL;
00179   lp = s->If->m->iml_make_lookup_done_inst(s);
00180   s->If->m->iml_link_inst_tail (&rrv, lp);
00181   s->If->m->iml_execute (s, &rrv);
00182 
00183   le_change_focus_notify(s);
00184 
00185   /* If PaletteAux can be started */
00186   if (dd->paletteaux_ready == True) {
00187     return;
00188   }
00189 
00190   /* PaletteAux can not be started */
00191   if (on == CONVERSION_ON && engine_id == 0xff) {
00192     system_locale_id = dd->aux_locale_id;
00193     engine_id = 0;
00194     for (i = 0; i< g_number_of_engines; i++) {
00195       locale_id = g_engines[i]->core.baseinfo.locale_id;
00196       if (locale_id == system_locale_id || locale_id == ENCODES_NUM) {
00197        engine_id = i;
00198        break;
00199       }
00200     }
00201     KOLE_LOG (LOGDEST_STDOUT, "engine_id:%d", engine_id);
00202     le_switch_engine(s, engine_id);
00203   }
00204 #endif
00205 }
00206 
00207 
00208 void
00209 proc_key_switch_keyboard_layout_event (iml_session_t *s, int nKeyboardID)
00210 {
00211   MyDataPerSession *session_data = (MyDataPerSession *)s->specific_data;
00212   if(nKeyboardID == -1){ /* no change, this would be for hiding the keyboad aux */
00213     iml_status_draw (s);
00214     return;
00215   }
00216 
00217   if(nKeyboardID < -1 || nKeyboardID > 3)
00218     nKeyboardID = 0;
00219 
00220   iml_status_draw (s);
00221 }
00222 
00223 void
00224 proc_symbol_input_event (iml_session_t *s, IMKeyEventStruct *key_event)
00225 {
00226        iml_status_draw (s);
00227        keyevent_input_handler (s, key_event);
00228        iml_status_draw (s);
00229 }
00230 
00231 void
00232 keyevent_toggle_junjabanja_handler (iml_session_t *s, int junjabanja)
00233 {
00234   MyDataPerSession *session_data = (MyDataPerSession *) s->specific_data;
00235   IMEBufferMethods methods_table = session_data->session_core.ime_methods_table;
00236   IMEBuffer session_ime_buffer = session_data->session_core.ime_buffer;
00237 
00238   KOLE_LOG (LOGDEST_STDOUT, "FULL_HALF_WIDTH_SWITCH_KEY: %d\n", junjabanja);
00239 
00240   session_data->paletteaux_status.junjabanja = junjabanja;
00241   (*methods_table->ime_buffer_initialize)(session_ime_buffer);
00242   le_switch_junjabanja_notify (s, junjabanja);
00243 }
00244 
00245 int
00246 proc_junja (iml_session_t *s, IMKeyEventStruct *key_event)
00247 {
00248   MyDataPerDesktop *desktop_data =
00249     (MyDataPerDesktop *)(s->desktop->specific_data);
00250   int lang_id, locale_id;
00251   char *commit_buf;
00252   int keycode, keystatus, keychar;
00253 
00254   keycode = key_event->keyCode;
00255   keychar = key_event->keyChar;
00256   keystatus = key_event->modifier;
00257 
00258   /* normal ASCII key */
00259   if ((keychar != 0) && (keystatus == IM_SHIFT_MASK || keystatus == 0)) {
00260     locale_id = desktop_data->aux_locale_id;
00261     lang_id = get_langid_from_localeid(locale_id);
00262     commit_buf = (char *)get_junja_str(keychar);
00263     if (commit_buf != NULL) {
00264 #if 0
00265       encode_commit(s, ENCODE_EUC_KO, (unsigned char *)commit_buf);
00266 #endif
00267       return (IMM_USED_KEY);
00268     }
00269   }
00270 
00271   return (IMM_NOT_USED_KEY);
00272 }
00273 
00274 void
00275 keyevent_input_handler (iml_session_t *s, IMKeyEventStruct *key_event)
00276 {
00277 
00278   MyDataPerSession *session_data = (MyDataPerSession *) s->specific_data;
00279   MyDataPerDesktop *desktop_data = (MyDataPerDesktop *)(s->desktop->specific_data);
00280   IMEBufferMethods ime_methods =  session_data->session_core.ime_methods_table;
00281   IMEBuffer pbuffer = session_data->session_core.ime_buffer;
00282   IMEKeyEventStruct  ime_keyevent;
00283   int ret, cur_engine_id, junjabanja, punct;
00284   Bool method_return;
00285   
00286 
00287   punct = session_data->paletteaux_status.punct;
00288   KOLE_LOG (LOGDEST_STDOUT, "punct:%d", punct);
00289        
00290   junjabanja = session_data->paletteaux_status.junjabanja;
00291 
00292   KOLE_LOG (LOGDEST_STDOUT, "junjabanja:%d", junjabanja);
00293 
00294   if (junjabanja == True) {
00295     ret = proc_junja(s, key_event);
00296     if (ret == IMM_USED_KEY) 
00297       return;
00298   }
00299        
00300   cur_engine_id = session_data->paletteaux_status.engine_id;
00301   KOLE_LOG (LOGDEST_STDOUT, "cur_engine_id:%d", cur_engine_id);
00302   if (cur_engine_id == 0xff) {
00303     iml_sendback_key(s, key_event);
00304     return;
00305   }
00306 
00307   ime_keyevent.key_code = key_event->keyCode;
00308   ime_keyevent.key_char = key_event->keyChar;
00309   ime_keyevent.key_modifier = key_event->modifier;
00310 
00311   method_return =
00312     (*ime_methods->ime_buffer_process_key_input) (pbuffer, &ime_keyevent);
00313 
00314   if (!method_return)
00315     iml_sendback_key (s, key_event);
00316   else
00317     {
00318       if (is_hotkey (s, &ime_keyevent)){
00319        auxhandler_send_session_property (s, XAUX_PALETTE_CLASS_NAME);
00320       }
00321       le_output_ime_buffer(s, session_data->session_core.ime_buffer, &ime_keyevent);
00322     }
00323 }
00324 
00325 static Bool
00326 is_hotkey (iml_session_t *s, IMEKeyEventStruct *key_event)
00327 {
00328   int n;
00329   IMEHotKey photkey;
00330   IMEEngine p_engine;
00331   IMECore p_core;
00332   MyDataPerSession *sd = (MyDataPerSession *) s->specific_data;
00333   p_engine = sd->p_engine;
00334   p_core = &p_engine->core;
00335   photkey = &p_core->hotkeys;
00336 
00337   assert (photkey != NULL);
00338   
00339   if (photkey->n_count){
00340     for (n = 0 ;n < photkey->n_count; n++){
00341       if (!imekeycmp (key_event, photkey->keylist[n]))
00342        return True;
00343     }
00344     return False;
00345   }
00346   return False;
00347 }
00348 
00349 static Bool
00350 imekeycmp (IMEKeyEventStruct *key_a, IMEKeyEventStruct *key_b)
00351 {
00352   assert (key_a != NULL);
00353   assert (key_b != NULL);
00354 
00355   if ( (key_a->key_code == key_b->key_code) &&
00356        (key_a->key_char == key_b->key_char) &&
00357        (key_a->key_modifier == key_b->key_modifier))
00358     return 0;
00359   else
00360     return -1;
00361 }