/*
  Copyright 2002-2003 Sun Microsystems, Inc. All Rights Reserved.

  Permission is hereby granted, free of charge, to any person obtaining a
  copy of this software and associated documentation files (the
  "Software"), to deal in the Software without restriction, including
  without limitation the rights to use, copy, modify, merge, publish,
  distribute, sublicense, and/or sell copies of the Software, and to
  permit persons to whom the Software is furnished to do so, subject to
  the following conditions: The above copyright notice and this
  permission notice shall be included in all copies or substantial
  portions of the Software.


  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  IN NO EVENT SHALL THE OPEN GROUP OR SUN MICROSYSTEMS, INC. BE LIABLE
  FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
  THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE EVEN IF
  ADVISED IN ADVANCE OF THE POSSIBILITY OF SUCH DAMAGES.


  Except as contained in this notice, the names of The Open Group and/or
  Sun Microsystems, Inc. shall not be used in advertising or otherwise to
  promote the sale, use or other dealings in this Software without prior
  written authorization from The Open Group and/or Sun Microsystems,
  Inc., as applicable.


  X Window System is a trademark of The Open Group

  OSF/1, OSF/Motif and Motif are registered trademarks, and OSF, the OSF
  logo, LBX, X Window System, and Xinerama are trademarks of the Open
  Group. All other trademarks and registered trademarks mentioned herein
  are the property of their respective owners. No right, title or
  interest in or to any trademark, service mark, logo or trade name of
  Sun Microsystems, Inc. or its licensors is granted.

*/
#include <stdio.h>

#include "le.h"
#include "encode.h"
#include "xaux_locale.h"

int map_keyevent_to_immkey(IMKeyEventStruct *key_event);
void proc_key_round_switch_ime_event(unit_desktop_t *udp, iml_session_t *s);
void proc_key_switch_conversion_event(unit_desktop_t *udp, iml_session_t *s, int on, char *to_locale);
void proc_key_select_switch_ime_event(unit_desktop_t *udp, iml_session_t *s, int ime_id);
void proc_key_output(unit_desktop_t *udp, iml_session_t *s, IMKeyEventStruct *key_event);

/* process the keyboard event */
void proc_key_event(unit_desktop_t *udp, iml_session_t *s, IMKeyListEvent *keylistevent)
{
    MyDataPerSession *session_data = (MyDataPerSession *) s->specific_data;
    IMKeyEventStruct *key_event = (IMKeyEventStruct *) keylistevent->keylist;
    char *cur_locale_string;
    char *cur_engine_string;
    int i,  key_ret, engine_id;
    int feid, leid;
    int keyCode, Modifier, keyChar;

    keyCode = key_event->keyCode;
    keyChar = key_event->keyChar;
    Modifier = key_event->modifier;

    cur_locale_string = (char *)alloca(256 * sizeof(char));
    cur_engine_string = (char *)alloca(256 * sizeof(char));

    (void) memset((char *)cur_locale_string,'\0',256);
    (void) memset((char *)cur_engine_string,'\0',256);

    log_f(" proc_keyevent:   session:%x\n", s);

    /* if no input methods in language engine. */
    if (udp->gEngine_Num <=0) {
	log_f("udp->gEngine_Num is zero \n");
	iml_sendback_key(s, key_event);
	return;
    }

    key_ret = map_keyevent_to_immkey(key_event);

    switch (key_ret) {
	/* English status <----> Non-English status */
      case ENGLISH_NONENGLISH_SWITCH_KEY:
       proc_key_switch_conversion_event(udp, s, CONVERSION_OFF, (char *)NULL);
       return;

       /* Bound Switching between Input Methods */
      case ROUND_SWITCH_KEY:
       proc_key_round_switch_ime_event(udp, s);
       return;
    }

    /*  Changing Locale Name */
    if ((keyCode == udp->localeNameKeyCode) && (Modifier == udp->localeNameModifier)) {
	engine_id = session_data->paletteaux_status.engine_id;
	log_f("Change Language: engine_id [%d]\n",engine_id);
	(void) strncpy((char *)cur_locale_string, udp->modeList[engine_id]->locale_name, strlen(udp->modeList[engine_id]->locale_name));
	log_f("Change Language, locale_name [%s]\n",cur_locale_string);
	log_f("Change Language, udp->localeList[%d]: [%s]\n",udp->locale_Num, udp->localeList[udp->locale_Num - 1].locale_name);
	if(!strcmp(cur_locale_string, udp->localeList[udp->locale_Num - 1].locale_name)){
	    le_switch_engine(udp, s, 0);
	}else{
	    for (i = engine_id; i< udp->gEngine_Num; i++) {
		if(!strcmp(cur_locale_string, udp->modeList[i]->locale_name)){
		    log_f("EQUAL <%s>\n",udp->modeList[i]->locale_name);
		}else{
		    log_f("NOT EQUAL locale_name <%s>\n",udp->modeList[i]->locale_name);
		    log_f("NOT EQUAL engine_id <%d>\n",udp->modeList[i]->engine_id);
		    le_switch_engine(udp, s, udp->modeList[i]->engine_id);
		    break;
		}
	    }
	}
	return;
    }else if((keyCode == udp->layoutNameKeyCode) && (Modifier == udp->layoutNameModifier)){ 
	/*  Changing keyboard layout Name */
	engine_id = session_data->paletteaux_status.engine_id;
	log_f("Changing Layout: engine_id [%d]\n",engine_id);
	(void) strncpy((char *)cur_locale_string, udp->modeList[engine_id]->locale_name, strlen( udp->modeList[engine_id]->locale_name));
	log_f("Changing Layout: cur_locale_string [%s]\n",cur_locale_string);
	(void) strncpy((char *)cur_engine_string, udp->modeList[engine_id]->engine_name, strlen(udp->modeList[engine_id]->engine_name));
	log_f("Changing Layout: engine_name [%s]\n",cur_engine_string);

	for(i=0; i< udp->locale_Num; i++){
	    if(!strcmp(cur_locale_string, udp->localeList[i].locale_name)){
		feid = udp->localeList[i].firstEngineId;
		leid = udp->localeList[i].lastEngineId;
	    }
	}
	log_f("Changing Layout: feid [%d] leid [%d]\n",feid, leid);
	for (i = engine_id; i<= udp->gEngine_Num; i++) {
	    if(!strcmp(cur_locale_string, udp->modeList[i]->locale_name)){
		if(!strcmp(cur_engine_string, udp->modeList[i]->engine_name)){
		    log_f("Engine EQUAL <%s>\n",udp->modeList[i]->engine_name);
		    if(engine_id == leid){
			log_f("engine_id == leid \n");
			le_switch_engine(udp, s, feid);
			break;
		    }
		}else{
		    log_f("Engine NOT EQUAL engine_name <%s>\n",udp->modeList[i]->engine_name);
		    log_f("NOT EQUAL engine_id <%d>\n",udp->modeList[i]->engine_id);
		    le_switch_engine(udp, s, udp->modeList[i]->engine_id);
		    break;
		}
	    }
	}
	return;
    }
    proc_key_output(udp, s, key_event);
}

/* filter keys that used by Input Methods Manager */
int map_keyevent_to_immkey(IMKeyEventStruct *key_event) 
{
    int keycode, modifier, keychar;

    keycode = key_event->keyCode;
    keychar = key_event->keyChar;
    modifier = key_event->modifier;

    log_f("Dec: keycode: %d, keychar: %d, status: %d\n",keycode, keychar, modifier);
    log_f("Hex: keycode: %x, keychar: %x, modifier: %x\n",keycode, keychar, modifier);

    /*  Control status */
    if (modifier & IM_CTRL_MASK) {
	switch(keycode) {
	  case IM_VK_ESCAPE:	return(ROUND_SWITCH_KEY);
	  case IM_VK_SPACE:	return(ENGLISH_NONENGLISH_SWITCH_KEY);
	}
    }
    return (IMM_NOT_USED_KEY);
}

void proc_key_switch_conversion_event(unit_desktop_t *udp, iml_session_t *s, int on, char *to_locale)
{
    MyDataPerSession *session_data = (MyDataPerSession *) s->specific_data;
    MyDataPerDesktop *desktop_data = (MyDataPerDesktop *)(s->desktop->specific_data);
    int engine_id, i;
    char locale_id, system_locale_id;

    log_f("ENGLISH_NONENGLISH SWITCH IME KEY, on:%d\n", on);

    le_clear_ime_buffer(s, session_data->ime_buffer);

    session_data->paletteaux_status.on = on;
    engine_id = session_data->paletteaux_status.engine_id;
    log_f("proc_key_switch_conversion_event: engine_id <%d>\n", engine_id);
    if (on == CONVERSION_OFF) {
	log_f("Inside CONVERSION_OFF \n");
	le_status_draw(udp, s);
	iml_conversion_off(s);
    } else {
	log_f("Inside CONVERSION_ON \n");
	iml_conversion_on(s);
	if (engine_id != 0xff) {
	    log_f("engine_id != 0xff \n");
	    le_switch_engine(udp, s, engine_id);
	}
    }

    if (on == CONVERSION_ON && engine_id == 0xff) {
	if (to_locale == NULL || to_locale[0] == '\0'){
	    log_f("key_event: to_locale is NULL \n");
	    system_locale_id = desktop_data->aux_locale_id;
	    log_f("keyevent.c: system_locale_id <%d>\n",system_locale_id);
	    engine_id = 0;
	    for (i = 0; i< udp->gEngine_Num; i++) {
		locale_id = udp->gEngine_Info[i]->core.baseinfo.locale_id;
		log_f("keyevent.c: system_locale_id <%d> locale_id <%d>\n",system_locale_id,locale_id);
		if (locale_id == system_locale_id || locale_id == ENCODES_NUM) {
		    engine_id = i;
		    break;
		}
	    }
	}else{
	    log_f("key_event: to_locale is Not NULL \n");
	    for (i = 0; i< udp->gEngine_Num; i++) {
		if(!strcmp(to_locale, udp->modeList[i]->locale_name)){
		    engine_id = i;
		    break;
		}
	    }
	}
	log_f("engine_id:%d\n", engine_id);
	le_switch_engine(udp, s, engine_id);
    }
}

void proc_key_round_switch_ime_event(unit_desktop_t *udp, iml_session_t *s)
{
    MyDataPerSession *session_data = (MyDataPerSession *) s->specific_data;
    MyDataPerDesktop *desktop_data = (MyDataPerDesktop *)(s->desktop->specific_data);
    int engine_id, i;
    char locale_id, system_locale_id;

    log_f("ROUND SWITCH IME KEY\n");

    system_locale_id = desktop_data->aux_locale_id;
    engine_id = session_data->paletteaux_status.engine_id;
    for (i = 0; i< udp->gEngine_Num; i++) {
	engine_id = (engine_id + 1) % udp->gEngine_Num;
	locale_id = udp->gEngine_Info[engine_id]->core.baseinfo.locale_id;
	if (locale_id == system_locale_id || locale_id == ENCODES_NUM) {
	    break;
	}
    }
    log_f("engine_id:%d\n", engine_id);

    le_clear_ime_buffer(s, session_data->ime_buffer);
    le_switch_engine(udp, s, engine_id);
}

void proc_key_select_switch_ime_event(unit_desktop_t *udp, iml_session_t *s, int ime_id)
{
    MyDataPerSession *session_data = (MyDataPerSession *) s->specific_data;
    MyDataPerDesktop *desktop_data = (MyDataPerDesktop *)(s->desktop->specific_data);
    int engine_id, engine_num, i;
    char locale_id, system_locale_id;

    log_f("SELECT SWITCH IME KEY\n");
    log_f("selected ime_id:%d\n", ime_id);

    system_locale_id = desktop_data->aux_locale_id;
    engine_num = 0;
    engine_id = 0;
    for (i = 0; i< udp->gEngine_Num; i++) {
	locale_id = udp->gEngine_Info[i]->core.baseinfo.locale_id;
	if (locale_id == system_locale_id || locale_id == ENCODES_NUM) {
	    if (engine_num >= ime_id) {
		engine_id = i;
		break;
	    }
	    engine_num ++;
	}
    }

    log_f("engine_id:%d\n", engine_id);
    le_clear_ime_buffer(s, session_data->ime_buffer);
    le_switch_engine(udp, s, engine_id);
}

void proc_key_output(unit_desktop_t *udp, iml_session_t *s, IMKeyEventStruct *key_event)
{
    MyDataPerSession *session_data = (MyDataPerSession *) s->specific_data;
    MyDataPerDesktop *desktop_data = (MyDataPerDesktop *)(s->desktop->specific_data);
    IMEKeyRec  ime_keyevent;
    IMEArgList ime_args;
    int ret, cur_engine_id;

    cur_engine_id = session_data->paletteaux_status.engine_id;
    log_f("cur_engine_id:%d\n", cur_engine_id);
    if (cur_engine_id == 0xff) {
	iml_sendback_key(s, key_event);
	return;
    }

    ime_keyevent.keyCode = key_event->keyCode;
    ime_keyevent.keyChar = key_event->keyChar;
    ime_keyevent.modifier = key_event->modifier;

    /* get user defined arguments */
    ime_args = desktop_data->ime_args[cur_engine_id];
    udp->gEngine_Info[cur_engine_id]->core.envinfo.output_encode_id = desktop_data->aux_locale_id;
    ret = udp->gEngine_Info[cur_engine_id]->so_methods->IME_Filter(&udp->gEngine_Info[cur_engine_id]->core,
								   &ime_keyevent, ime_args, 
								   session_data->ime_buffer);

    if (ret == IME_NOT_USED_KEY)
	iml_sendback_key(s, key_event);
    else
	le_output_ime_buffer(s, session_data->ime_buffer);
}
/* Local Variables: */
/* c-file-style: "iiim-project" */
/* End: */
