/*
###
### This file is part of
###
###                        TurboLinux  ZWinPro
###
###                 Copyright (C) 1999-2000 TurboLinux, Inc. 
###                        All Rights Reserved
### Distributed under the terms of the GNU General Public License (GPL)
###
###
### Authors:     TurboLinux Chinese Development Team:
###              Justin Yu   <justiny@turbolinux.com.cn>
###              Sean Chen   <seanc@turbolinux.com.cn>
###              Daniel Fang <danf@turbolinux.com.cn>
### WWW:         http://www.turbolinux.com.cn/ZWinPro/
### FTP:         ftp://ftp.turbolinux.com.cn/pub/ZWinPro/
###
*/

/*
 * Copyright 1997 by Yu Mingjian.          All Rights Reserved
 * Distributed under the terms of the GNU General Public License (GPL)
 *
 * Permission to retain, use, modify, copy, and distribute Chinput1.0
 * in source or binary and its documentation (hereafter, the Software)
 * for noncommercial purpose is hereby granted to you without a fee,
 * provided that this entire copyright and permission notice appear in
 * all such copies, that no charge be associated with such copies,
 * that distribution of derivative works (including value-added
 * distributions such as with additional input dictionaries or fonts)
 * include clarification that such added or derived parts are not from
 * the original Software, and that the names of the author(s) not be
 * used to endorse or promote such works. The author(s) of the software
 * makes no representations about the suitability of this software for any
 * purpose.  It is provided "as is" without express or implied warranty.
 *
 * THE AUTHOR(S) OF THE SOFTWARE DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS,
 * IN NO EVENT SHALL THE AUTHOR(S) OF THE SOFTWARE BE LIABLE FOR ANY SPECIAL,
 * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 * PERFORMANCE OF THIS SOFTWARE.
 *
 * Author: Yu Mingjian, Institute of High Energy Physics, Academia Sinica
 * 
 */

#include "all.h"
#include "symbol.h"

chinput_symbol *chinput_corner;

#define STRBUFLEN 64

static int chinput_server_symbol_lookup (char c, char *buf)
{
	unsigned char cnt;
	// Lookup 'c' in chinput_corner array.
	for (cnt = 0; cnt < SYMBOL_TABLE_MAX; cnt++)
	{
		if (chinput_corner[cnt].c == c)
		{
			unsigned char mod;
			
			// Copy lookup result to buf, and return correct len.
			mod = chinput_corner[cnt].cur % chinput_corner[cnt].num;
			strncpy (buf, chinput_corner[cnt].s + mod * 2, SYMBOL_SIZE);
			buf [2] = '\0';

			// Round up the counter.
			chinput_corner[cnt].cur += 1;
			if (chinput_corner[cnt].cur == chinput_corner[cnt].num)
				chinput_corner[cnt].cur = 0;
			else

			return 2;
		}
	}
	// Return -1, if lookup fails.
	return -1;
}

static int chinput_server_proc_reply (char c, char *buf)
{
	// Determine the further action by "flag_corner" and "flag_punct"

	if (flag_found)
		return chinput_server_set_reply (c, buf);

	if (flag_corner && (ispunct (c) || isalnum (c) || c == ' '))
		return chinput_server_symbol_lookup (c, buf);

	// Lookup 2 Byte punct
	if (flag_punct && ispunct (c)) 
		return chinput_server_symbol_lookup (c, buf);

	return chinput_server_set_reply (c, buf);
}

void chinput_server_proc_xim (XKeyEvent *kev, char *buf, int *charcount)
{
	char strbuf[STRBUFLEN];
	KeySym keysym;
	int count;

	memset(strbuf, 0, STRBUFLEN);
//	printf ("KeySym : %#x\n", XLookupKeysym (kev, 0));

	count = XLookupString (kev, strbuf, STRBUFLEN, &keysym, NULL);
//	printf ("After XLookupString, strbuf is %s\n", strbuf);

	if (HZServer.encoding == HZSERVER_ENCODING_GB2312)
		chinput_corner = chinput_corner_gb;
	else if (HZServer.encoding == HZSERVER_ENCODING_GB18030)
		chinput_corner = chinput_corner_gb;
	else if (HZServer.encoding == HZSERVER_ENCODING_BIG5)
		chinput_corner = chinput_corner_big5;
	else if (HZServer.encoding == HZSERVER_ENCODING_BIG5HKSCS)
		chinput_corner = chinput_corner_big5;

	*charcount = chinput_server_proc_reply (strbuf[0], buf);
}

int chinput_server_set_reply (char ch, char *buf)
{
	int n;
	int charcount;
	char mbuf[256];
	Boolean flag_znpy;

	bzero (mbuf, 256);

	flag_znpy = False;

	// What happens for THIS ime?
	if (!strcmp (chinputime[cur_inputmethod].namegb, "ÖÇÄÜÆ´Òô"))
	{
		IMM_GetInputDisplay (chinput_imm, mbuf, sizeof (mbuf));
		flag_znpy = True;
	}

	// What is 'v'
	// see if 'v' before filtering
	if (flag_znpy && flag_english == True && ch != ' ' && ch != 0x08)
	{	//append char
		buf[0] = ch;
		buf[1] = '\0';
		strcat (input_buf, buf);
		charcount =  0;
	}
	else if (flag_znpy && flag_english == True && ch == 0x08)
	{	//delete char
		int num;
		num = strlen (input_buf);
		if (num > 1)
		{
			input_buf[num-1] = '\0';
			charcount = 0;
		}
		else
		{
			flag_english = False;
			input_buf[0] = '\0';
			charcount = 0;
		}
	}
	else if (flag_znpy && strlen (mbuf) == 0 && ch == 'v')
	{
		flag_english = True;
		input_buf[0] = 'v';
		input_buf[1] = '\0';
		charcount = 0;
	}
	else if (flag_znpy && flag_english == True && ch == ' ')
	{
		flag_english = False;
		strcpy (buf, input_buf+1);
		bzero (input_buf, 80);
		charcount = strlen(buf) + 1;
	}
	else
	{
		if (flag_znpy && strlen (mbuf) != 0)
		{
			if (ch == '[')
			{
				flag_single = 1;
				return 0;
			}
			else if (ch == ']')
			{
				flag_single = 2;
				return 0;
			}
		}
//		printf ("calling IMM_KeyFilter, ch is %c\n", ch);
		n = IMM_KeyFilter (chinput_imm, ch, buf, &charcount);
		switch (n)
		{
			case 0:
				buf[0] = ch;
				buf[1] = '\0';
				charcount = 1;
				break;
			case 1:
				charcount = 0;
				break;
			case 2:
				buf[charcount] = '\0';
				break;
			case -1:
				chinput_util_err_exit ();
		}		
	}	
	if (charcount == 0)
		flag_refresh = 0;

	if (dmode == HZSERVER_DMODE_ROOT)
		chinput_root_flush ();
	else if (dmode == HZSERVER_DMODE_OVERSPOT)
		chinput_overspot_flush ();
	else if (dmode == HZSERVER_DMODE_ONSPOT)
		chinput_onspot_flush ();
	else if (dmode == HZSERVER_DMODE_OFFSPOT)
		chinput_offspot_flush ();

//	printf ("charcount is %d\n", charcount);
	return charcount;
}
