/*
Copyright 1990-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 <X11/Xlib.h>
#include <X11/Xmd.h>
#include <Xm/Xm.h>

#include "xaux_common.h"
#include "xaux_ext_common.h"
#include "thai_aux.h"
#include "thai_common.h"
#include "thai_protocol.h"
#include "OptionAux.h"
#include "OptionConfig.h"

/* Define Aux class */
xaux_class_t *xc_ = 0;
int im_ = 0;
int ic_ = 0;

/* Define Aux class */
xaux_class_t xaux_class = {
	XAUX_OPTION_CLASS_NAME, 1,
	NULL,
	(Window)0, (Window)0,
	(Atom)0, (Atom)0, (Atom)0,
	{ (Atom)0 }, ATOM_NUM_OPTIONAUX, 0,
	{ (Atom)0 }, ATOM_NUM_OPTIONAUX, 0,
	NULL,
};

extern Display          *gDisplay;
extern char             gLocaleID;
extern OptionAuxData    gOptData;

void xaux_ext_event_handler(Widget, XtPointer, XEvent *, Boolean *);
void OptionAux_Connect_To_LE_Request();
void OptionAux_Proc_LE_Notify();
void OptionAux_Proc_LE_Show_Notify();
void OptionAux_Proc_LE_Update_ModeNameList_Notify(aux_ext_data_t *aux_ext_data);
void OptionAux_Proc_LE_Update_KeyboardNameList_Notify(aux_ext_data_t *aux_ext_data);
void OptionAux_Proc_LE_Change_Focus_Notify(aux_ext_data_t *aux_ext_data);

/*   
	Event Handler 
*/
void xaux_ext_event_handler(Widget w, XtPointer client_data, XEvent * event, Boolean * b)
{
	switch (event->type) {
		case ClientMessage:
			xaux_ext_process_client_message(XtDisplay(w),
				(XClientMessageEvent *) event);
			break;
	}
	return;
}

/*
	xaux_ext_init_classes(): Initialize the Atom of Auxiliary Window.
	It is in ../xaux_common/xaux_ext_common.c
*/
int xaux_ext_register_classes(Widget shell)
{
	/* Register aux class */
	if (xaux_ext_init_classes(XtDisplay(shell), &xaux_class, XtWindow(shell)) == False)
		return(-1);

	/* Register event handler */
	XtAddEventHandler(shell, PropertyChangeMask, False,
		xaux_ext_event_handler, NULL);
	XtAddEventHandler(shell, NoEventMask, True,
		xaux_ext_event_handler, NULL);

	return(0);
}

/* 
Note:
     xaux_ext_Start(), xaux_ext_Draw(), xaux_ext_Done() 
     These three functions are all called by xaux_ext_process_client_message()

     xaux_ext_SetValue() will send auxiliary window information to language engine;
*/

/* Method - Start */
Bool xaux_ext_Start(xaux_class_t *xc, aux_ext_data_t *aux_ext_data)
{
	DEBUG_printf("ext_Start: im:%d, ic:%d\n", aux_ext_data->im, aux_ext_data->ic);
	im_ = aux_ext_data->im;
	ic_ = aux_ext_data->ic;
	xc_ = xc;

/*
	OptionAux_Connect_To_LE_Request(gLocaleID);
*/
	return True;
}

/* Method - Draw */
Bool xaux_ext_Draw(xaux_class_t *xc, aux_ext_data_t *aux_ext_data)
{
	DEBUG_printf("ext_Draw == im:0x%x, ic:0x%x\n",aux_ext_data->im, aux_ext_data->ic);
	im_ = aux_ext_data->im;
	ic_ = aux_ext_data->ic;
	xc_ = xc;

        OptionAux_Proc_LE_Notify(aux_ext_data);
	return True;
}

/* Method - Done */
Bool xaux_ext_Done(xaux_class_t *xc, aux_ext_data_t *aux_ext_data)
{
	DEBUG_printf("ext_Done im:0x%x, ic_id:0x%x\n", aux_ext_data->im, aux_ext_data->ic);
	return True;
}


/* ================================================================= */
/*       Process Notify information From Language Engine             */
/* ================================================================= */
void OptionAux_Proc_LE_Notify(aux_ext_data_t *aux_ext_data)
{
	int nIntegerCount, notifyType;

	nIntegerCount = aux_ext_data->integer_count;
	if (nIntegerCount <= 0) return;

	notifyType = aux_ext_data->integer_list[0];

	switch (notifyType)
	{
		case COMMONAUX_MODE_NAME_NOTIFY:
			DEBUG_printf("COMMONAUX_MODE_NAME_NOTIFY Notify Received\n");
			OptionAux_Proc_LE_Update_ModeNameList_Notify(aux_ext_data);
			break;
		case COMMONAUX_KEYBOARD_NAME_NOTIFY:
			DEBUG_printf("COMMONAUX_KEYBOARD_NAME_NOTIFY Notify Received\n");
			OptionAux_Proc_LE_Update_KeyboardNameList_Notify(aux_ext_data);
			break;
		case OPTIONAUX_SHOW_NOTIFY:
			DEBUG_printf("OPTIONAUX_SHOW_NOTIFY Notify Received\n");
			OptionAux_Proc_LE_Show_Notify(aux_ext_data);
			break;
		case COMMONAUX_CHANGE_FOCUS_NOTIFY:
			DEBUG_printf("COMMONAUX_CHANGE_FOCUS_NOTIFY Notify Received\n");
			OptionAux_Proc_LE_Change_Focus_Notify(aux_ext_data);
			break;
	}
}

void OptionAux_Proc_LE_Update_ModeNameList_Notify(aux_ext_data_t *aux_ext_data)
{
	int nInputMode_Num = aux_ext_data->integer_list[1];
	int i;

	DEBUG_printf("nInputMode_Num: %d\n", nInputMode_Num);
	if (nInputMode_Num <= 0)
		return;

	if (gOptData.ppArrInputModeNames != NULL) {
		for (i = 0; i < gOptData.nInputMode_Num; i ++) {
			if (gOptData.ppArrInputModeNames[i]) free((char *)gOptData.ppArrInputModeNames[i]);
		}
		free((char *)gOptData.ppArrInputModeNames);
	}

	gOptData.nInputMode_Num = nInputMode_Num;
	gOptData.ppArrInputModeNames = (char **)calloc(nInputMode_Num, sizeof(char *));
	for (i = 0; i < nInputMode_Num; i ++) {
		gOptData.ppArrInputModeNames[i] = (char *)strdup((char *)aux_ext_data->string_list[i].ptr);
	}

	OptionAux_SetMenuItems(gOptData.wdtInputModeMenu, gOptData.ppArrInputModeNames, nInputMode_Num, MENUTYPE_INPUTMODE);
}

void OptionAux_Proc_LE_Update_KeyboardNameList_Notify(aux_ext_data_t *aux_ext_data)
{
	int nKeyboard_Num = aux_ext_data->integer_list[1];
	int i;

	DEBUG_printf("nKeyboard_Num: %d\n", nKeyboard_Num);
	if (nKeyboard_Num <= 0)
		return;

	if (gOptData.ppArrKeyboardNames != NULL) {
		for (i = 0; i < gOptData.nKeyboard_Num; i ++) {
			if (gOptData.ppArrKeyboardNames[i]) free((char *)gOptData.ppArrKeyboardNames[i]);
		}
		free((char *)gOptData.ppArrKeyboardNames);
	}

	gOptData.nKeyboard_Num = nKeyboard_Num;
	gOptData.ppArrKeyboardNames = (char **)calloc(nKeyboard_Num, sizeof(char *));
	for (i = 0; i < nKeyboard_Num; i ++) {
		gOptData.ppArrKeyboardNames[i] = (char *)strdup((char *)aux_ext_data->string_list[i].ptr);
	}

	OptionAux_SetMenuItems(gOptData.wdtKeyboardMenu, gOptData.ppArrKeyboardNames, nKeyboard_Num, MENUTYPE_KEYBOARD);
}

void OptionAux_Proc_LE_Show_Notify(aux_ext_data_t *aux_ext_data)
{
	int nInputMode_ID = aux_ext_data->integer_list[1];
	int nKeyboard_ID = aux_ext_data->integer_list[2];

	DEBUG_printf("OptionAux_Proc_LE_Show_Notify, input_mode: %d, keyboard_id: %d\n", nInputMode_ID, nKeyboard_ID);
	OptionAux_Update_Status(nInputMode_ID, nKeyboard_ID);
}

void OptionAux_Proc_LE_Change_Focus_Notify(aux_ext_data_t *aux_ext_data)
{
	int paletteaux_on, optionaux_on;
	int input_mode_id, keyboard_id;

	optionaux_on  = aux_ext_data->integer_list[1];
	paletteaux_on = aux_ext_data->integer_list[2];
	input_mode_id = aux_ext_data->integer_list[3];
	keyboard_id = aux_ext_data->integer_list[4];

	DEBUG_printf("OptionAux_Proc_Change_Focus_Notify: \n");
	DEBUG_printf("optionaux_on:%d, paletteaux_on:%d\n", optionaux_on, paletteaux_on);
	DEBUG_printf("input_mode_id:%d, keyboard_id:%d\n", input_mode_id, keyboard_id);
	if (paletteaux_on && optionaux_on)
		OptionAux_Update_Status(input_mode_id, keyboard_id);
	else
		OptionAux_Hide();
}

/* ================================================================= */
/*             Send information to Language Engine                   */
/* ================================================================= */
void OptionAux_Connect_To_LE_Request(int locale_id)
{
	int nIntegerCount, pIntegerList[2];

	nIntegerCount = 2;
	pIntegerList[0] = OPTIONAUX_CONNECT;
	pIntegerList[1] = locale_id;
	NotifyIntegerListDataToLanguageEngine(gDisplay, nIntegerCount, pIntegerList);
}

void OptionAux_Change_Status_Request(int bShowed)
{
	int nIntegerCount, pIntegerList[2];

	nIntegerCount = 2;
	pIntegerList[0] = OPTIONAUX_CHANGE_STATUS;
	pIntegerList[1] = bShowed;
	NotifyIntegerListDataToLanguageEngine(gDisplay, nIntegerCount, pIntegerList);
}

void PaletteAux_Set_Default_Request(int isc_mode_id, int keyboard_id)
{
	int nIntegerCount, pIntegerList[3];

	nIntegerCount = 3;
	pIntegerList[0] = COMMONAUX_SET_DEFAULT_VALUE;
	pIntegerList[1] = isc_mode_id;
	pIntegerList[2] = keyboard_id;

	NotifyIntegerListDataToLanguageEngine(gDisplay, nIntegerCount, pIntegerList);
}

NotifyIntegerListDataToLanguageEngine(Display *display, 
int nIntegerCount, int *pIntegerList)
{
	aux_ext_data_t aux_ext_data;

	DEBUG_printf("im_:%d, ic_:%d\n", im_, ic_);

	if (!im_ || !ic_ || !xc_)
		return;

	aux_ext_data.im = im_;
	aux_ext_data.ic = ic_;
	aux_ext_data.integer_count = nIntegerCount;
	aux_ext_data.integer_list = pIntegerList;
	aux_ext_data.string_count = 0;
	aux_ext_data.string_list = NULL;
	aux_ext_data.string_ptr = NULL;
	aux_ext_data.point.x = 0;
	aux_ext_data.point.y = 0;

	xaux_ext_SetValue(display, xc_, &aux_ext_data);
	XFlush(display);
}

NotifyStringListDataToLanguageEngine(Display *display, 
int nIntegerCount, int *pIntegerList,
int nStringCount,  int *pStringLen,
char **pStringList)
{
	int i;

	aux_ext_data_t aux_ext_data;
	aux_ext_string_t *p;

	DEBUG_printf("im_:%d, ic_:%d\n", im_, ic_);

	if (!im_ || !ic_ || !xc_)
		return;

	aux_ext_data.im = im_;
	aux_ext_data.ic = ic_;
	aux_ext_data.integer_count = nIntegerCount;
	aux_ext_data.integer_list = pIntegerList;
	aux_ext_data.point.x = 0;
	aux_ext_data.point.y = 0;

	aux_ext_data.string_count = nStringCount;
	aux_ext_data.string_ptr = NULL;

	if (nStringCount > 0) {
		aux_ext_data.string_list = (aux_ext_string_t *) calloc(nStringCount, sizeof(aux_ext_string_t));
		for (i = 0; i < nStringCount; i++) {
			aux_ext_string_t *p = &aux_ext_data.string_list[i];
			p->length = pStringLen[i];
			p->ptr = (unsigned char *) (pStringList[i]);
		}
	}

	xaux_ext_SetValue(display, xc_, &aux_ext_data);
	XFlush(display);

	free(aux_ext_data.string_list);
}

