/*
###
### 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 1994, 1995 by Sun Microsystems, Inc.
         Copyright 1993, 1994 by Hewlett-Packard Company
 
Permission to use, copy, modify, distribute, and sell this software
and its documentation for any purpose is hereby granted without fee,
provided that the above copyright notice appear in all copies and
that both that copyright notice and this permission notice appear
in supporting documentation, and that the name of Sun Microsystems, Inc.
and Hewlett-Packard not be used in advertising or publicity pertaining to
distribution of the software without specific, written prior permission.
Sun Microsystems, Inc. and Hewlett-Packard make no representations about
the suitability of this software for any purpose.  It is provided "as is"
without express or implied warranty.
 
SUN MICROSYSTEMS INC. AND HEWLETT-PACKARD COMPANY DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
SUN MICROSYSTEMS, INC. AND HEWLETT-PACKARD COMPANY 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: Hidetoshi Tajima(tajima@Eng.Sun.COM) Sun Microsystems, Inc.
 
******************************************************************/

#include "all.h"

static IC *ic_list = (IC *)NULL;
static IC *free_list = (IC *)NULL;
//static unsigned int malloc_count = 0;
//static unsigned int free_count = 0;

// return True if attr == attr_list->name
static int chinput_match_ic_attr (char *attr, XICAttribute *attr_list)
{
	return !strcmp (attr, attr_list->name);
}

static IC *chinput_new_ic ()
{
    static CARD16 icid = 0;
    IC *rec;

//	printf ("chinput_new_ic () :: icid == %d\n", (int) icid);

	if (free_list != NULL)
	{
		rec = free_list;
		free_list = free_list->next;
	}
	else
	{
		rec = (IC *)malloc (sizeof(IC));
//		if (rec)
//			printf ("NewIC () :: malloc_count == %d\n", ++malloc_count);
	}
	memset (rec, 0, sizeof(IC));
	rec->id = ++icid;

	rec->next = ic_list;
	ic_list = rec;
	return rec;
}

static void chinput_delete_ic (CARD16 icid)
{
	IC *rec, *last;
	last = NULL;
	for (rec = ic_list; rec != NULL; last = rec, rec = rec->next)
	{
		if (rec->id == icid)
		{
			if (last != NULL)
				last->next = rec->next;
			else
				ic_list = rec->next;

			rec->next = free_list;
			free_list = rec;

		//free resource
		if (rec->resource_name)
			free(rec->resource_name);
		if (rec->resource_class)
			free(rec->resource_class);
		if (rec->status_win)
			XDestroyWindow (display, rec->status_win);
		if (rec->preedit_win)
			XDestroyWindow (display, rec->preedit_win);

#if 0
		if (rec->pre_attr.gc) XFreeGC(display, rec->pre_attr.gc);
		if (rec->sts_attr.gc) XFreeGC(display, rec->sts_attr.gc);
		if (rec->pre_attr.fs) XFreeFontSet(display, rec->pre_attr.fs);
		if (rec->sts_attr.fs) XFreeFontSet(display, rec->sts_attr.fs);
#endif

		return;
		}
	}
	return;
}

static void chinput_store_ic (IC *rec, IMChangeICStruct *call_data)
{
	XICAttribute *ic_attr = call_data->ic_attr;
	XICAttribute *pre_attr = call_data->preedit_attr;
	XICAttribute *sts_attr = call_data->status_attr;
	register int i;

	for (i = 0; i < (int)call_data->ic_attr_num; i++, ic_attr++)
	{
		if (chinput_match_ic_attr (XNInputStyle, ic_attr))
		{
			rec->input_style = *(INT32*)ic_attr->value;
		}
		else if (chinput_match_ic_attr (XNClientWindow, ic_attr))
		{
			rec->client_win = *(Window*)ic_attr->value;
		}
		else if (chinput_match_ic_attr (XNFocusWindow, ic_attr))
		{
			rec->focus_win = *(Window*)ic_attr->value;
		}
		else 
		{
//			fprintf (stderr, "Warning [chinput_store_ic ()]: Unknown IC Attr. %s\n", ic_attr->name);
		}
	}

	for (i = 0; i < (int)call_data->preedit_attr_num; i++, pre_attr++)
	{
		if (chinput_match_ic_attr (XNArea, pre_attr))
		{
			rec->pre_attr.area = *(XRectangle*)pre_attr->value;
#if 0
			fprintf (stderr, "Preedit Area (x,y,w,h): %d %d %d %d\n",
					rec->pre_attr.area.x,
					rec->pre_attr.area.y,
					rec->pre_attr.area.width,
					rec->pre_attr.area.height);
#endif
		}
		else if (chinput_match_ic_attr (XNAreaNeeded, pre_attr))
		{
			rec->pre_attr.area_needed = *(XRectangle*)pre_attr->value;
#if 0
			fprintf (stderr, "Info [chinput_store_ic ()]: Preedit Area (x, y, w, h): %d %d %d %d\n", 
				rec->pre_attr.area.x,
				rec->pre_attr.area.y,
				rec->pre_attr.area_needed.width,
				rec->pre_attr.area_needed.height);
#endif
		}
		else if (chinput_match_ic_attr (XNSpotLocation, pre_attr))
		{
#if 0
			XPoint *xpt = (XPoint*) pre_attr->value;
			fprintf (stderr, "Info [chinput_store_ic ()]: pre_attr XNSpotLocation\n");
			fprintf (stderr, "... spot_location.x is %d\n", xpt->x);
			fprintf (stderr, "... spot_location.y is %d\n", xpt->y);
#endif
			rec->pre_attr.spot_location = *(XPoint*)pre_attr->value;


			// WE SHOULD ONLY SET IT IF some one set focus ~~
			if(rec == current_focus_ic && dmode == HZSERVER_DMODE_OVERSPOT)
				chinput_overspot_set_location (rec);
		}
		else if (chinput_match_ic_attr (XNColormap, pre_attr))
		{
//			fprintf (stderr, "Info [chinput_store_ic ()]: pre_attr XNColormap\n");
			rec->pre_attr.cmap = *(Colormap*) pre_attr->value;
		}
		else if (chinput_match_ic_attr (XNStdColormap, pre_attr))
		{
//			fprintf (stderr, "Info [chinput_store_ic ()]: pre_attr XNStdColormap\n");
			rec->pre_attr.cmap = *(Colormap*)pre_attr->value;
		}
		else if (chinput_match_ic_attr (XNForeground, pre_attr))
		{
//			fprintf (stderr, "Info [chinput_store_ic ()]: pre_attr XNForeground\n");
			rec->pre_attr.foreground = *(CARD32*)pre_attr->value;
		}
		else if (chinput_match_ic_attr (XNBackground, pre_attr))
		{
//			fprintf (stderr, "Info [chinput_store_ic ()]: pre_attr XNBackground\n");
			rec->pre_attr.background = *(CARD32*)pre_attr->value;
		}
		else if (chinput_match_ic_attr (XNBackgroundPixmap, pre_attr))
		{
//			fprintf (stderr, "Info [chinput_store_ic ()]: pre_attr XNBackgroundPixmap\n");
			rec->pre_attr.bg_pixmap = *(Pixmap*)pre_attr->value;
		}
		else if (chinput_match_ic_attr (XNFontSet, pre_attr))
		{
			int str_length = strlen (pre_attr->value);

			if (rec->pre_attr.base_font != NULL)
			{
				if (chinput_match_ic_attr (rec->pre_attr.base_font, pre_attr))
					continue;
				XFree(rec->pre_attr.base_font);
			}
			rec->pre_attr.base_font = malloc(str_length + 1);
			strcpy(rec->pre_attr.base_font, pre_attr->value);
#if 0
			printf("fontset:%s-\n", rec->pre_attr.base_font);
#endif
		}
		else if (chinput_match_ic_attr (XNLineSpace, pre_attr))
		{
			rec->pre_attr.line_space = *(CARD32*)pre_attr->value;
		}
		else if (chinput_match_ic_attr (XNCursor, pre_attr))
		{
			rec->pre_attr.cursor = *(Cursor*)pre_attr->value;
		}
		else
		{
			fprintf(stderr, "Unknown attr: %s\n", ic_attr->name);
		}
	}

	for (i = 0; i < (int)call_data->status_attr_num; i++, sts_attr++)
	{
		if (chinput_match_ic_attr (XNArea, sts_attr))
		{
			rec->sts_attr.area = *(XRectangle*)sts_attr->value;
#if 0
			printf("Status Area: %d %d %d %d\n", 
			rec->sts_attr.area.x,
			rec->sts_attr.area.y,
			rec->sts_attr.area.width,
			rec->sts_attr.area.height);
#endif
		}
		else if (chinput_match_ic_attr (XNAreaNeeded, sts_attr))
		{
			rec->sts_attr.area_needed = *(XRectangle*)sts_attr->value;
#if 0
			printf("Status Area Needed: %d %d %d %d\n", 
			rec->sts_attr.area.x,
			rec->sts_attr.area.y,
			rec->sts_attr.area_needed.width,
			rec->sts_attr.area_needed.height);
#endif
		}
		else if (chinput_match_ic_attr (XNColormap, sts_attr))
		{
			rec->sts_attr.cmap = *(Colormap*)sts_attr->value;
		}
		else if (chinput_match_ic_attr (XNStdColormap, sts_attr))
		{
			rec->sts_attr.cmap = *(Colormap*)sts_attr->value;
		}
		else if (chinput_match_ic_attr (XNForeground, sts_attr))
		{
			rec->sts_attr.foreground = *(CARD32*)sts_attr->value;
		}
		else if (chinput_match_ic_attr (XNBackground, sts_attr)){
			rec->sts_attr.background = *(CARD32*)sts_attr->value;
		}
		else if (chinput_match_ic_attr (XNBackgroundPixmap, sts_attr))
		{
			rec->sts_attr.bg_pixmap = *(Pixmap*)sts_attr->value;
		}
		else if (chinput_match_ic_attr (XNFontSet, sts_attr))
		{
			int str_length = strlen(sts_attr->value);
		
			if (rec->sts_attr.base_font != NULL)
			{
				if (chinput_match_ic_attr (rec->sts_attr.base_font, sts_attr))
					continue;
				XFree(rec->sts_attr.base_font);
			}
			rec->sts_attr.base_font = malloc(str_length + 1);
			strcpy(rec->sts_attr.base_font, sts_attr->value);
#if 0
			printf("sts fontset:%s-\n", rec->sts_attr.base_font);
#endif
		}
		else if (chinput_match_ic_attr (XNLineSpace, sts_attr))
		{
			rec->sts_attr.line_space= *(CARD32*)sts_attr->value;
		}
		else if (chinput_match_ic_attr (XNCursor, sts_attr))
		{
			rec->sts_attr.cursor = *(Cursor*)sts_attr->value;
		}
		else 
		{
			fprintf(stderr, "Unknown attr: %s\n", ic_attr->name);
		}
	}
	rec->encoding = xim_connect[call_data->connect_id];
}

IC *chinput_find_ic (CARD16 icid)
{
	IC *rec = ic_list;

	while (rec != NULL)
	{
		if (rec->id == icid)
		  return rec;
		rec = rec->next;
	}

	return NULL;
}

int chinput_find_enc (IMForwardEventStruct *call_data)
{
	IC *rec = ic_list;

	while (rec != NULL)
	{
		if (rec->id == call_data->icid)
			return rec->encoding;
		rec = rec->next;
	}
	printf ("Warning: IC.c >> chinput_find_enc () >> Cannot find enc for calldata\n");
	return HZSERVER_ENCODING_DEFAULT;
}

int chinput_find_enc_by_ic (int icid)
{
	IC *rec = ic_list;

	while (rec != NULL)
	{
		if (rec->id == icid)
			return rec->encoding;
		rec = rec->next;
	}   
	return HZSERVER_ENCODING_DEFAULT;
}

void chinput_free_all_ic (void)
{
	IC *ICToFree = NULL;

	// Free the list by iteration is better the recursive..

	// ic_list server as the list head
	for (ICToFree = ic_list, ic_list = ic_list -> next;
			ICToFree != NULL;
			ICToFree = ic_list, ic_list = ic_list -> next)
	{
		free (ICToFree);
//		printf ("chinput_free_all_ic () :: (ic_list) free_count == %d\n", ++free_count);
	}

	// free_list server as the list head
	for (ICToFree = free_list, free_list = free_list -> next;
			ICToFree != NULL;
			ICToFree = free_list, free_list = free_list -> next)
	{
		free (ICToFree);
//		printf ("chinput_free_all_ic () :: (free_list) free_count == %d\n", ++free_count);
	}
}

void chinput_create_ic (IMChangeICStruct *call_data)
{
	IC *rec;
#if 0	
	char **missing_list;
	int  missing_count;
	char *def_string;
	char fsname[256];
#endif	
	rec = chinput_new_ic ();
	if (rec == NULL)
	  return;

	chinput_store_ic (rec, call_data);
	call_data->icid = rec->id;
	rec->connect_id = call_data->connect_id;
	rec->len = 0;
	rec->sts_attr.area_needed.x = 0;
	rec->sts_attr.area_needed.y = FONT_HEIGHT;
	rec->sts_attr.area_needed.width = AREA_CHARS * FONT_WIDTH / 2;
	rec->sts_attr.area_needed.height = FONT_HEIGHT;
	rec->pre_attr.area_needed.x = 0;
	rec->pre_attr.area_needed.y = FONT_HEIGHT;
	rec->pre_attr.area_needed.width = AREA_CHARS * FONT_WIDTH / 2;
	rec->pre_attr.area_needed.height = FONT_HEIGHT;

	if (rec->input_style & XIMStatusArea)
	{
		rec->status_win = chinput_util_create_win(0, 0, 1, 1);
	}
	else
	{
		rec->status_win = 0;
	}

	if (rec->input_style & XIMPreeditArea)
	{
		rec->preedit_win = chinput_util_create_win(0, 0, 1, 1);
	}
	else
	{
		rec->preedit_win = 0;
	}
	rec->reparented = 0;

	//create gcs and fontsets
#if 0
	chinput_get_color_gc (window1, &rec->pre_attr.gc, "#000000");
	chinput_get_color_gc (window1, &rec->sts_attr.gc, "#000000");

	if ((rec->pre_attr.fs = XCreateFontSet (display, 
			rec->pre_attr.base_font, &missing_list,
			&missing_count, &def_string)) == NULL)
	{
		sprintf (fsname, "%s,%s", font_latin, font_gb);
		rec->pre_attr.fs = XCreateFontSet (display,
				fsname, &missing_list, &missing_count, &def_string);
	}
	if ((rec->sts_attr.fs = XCreateFontSet (display, 
			rec->sts_attr.base_font, &missing_list,
			&missing_count, &def_string)) == NULL) {
		sprintf (fsname, "%s,%s", font_latin, font_gb);
		rec->sts_attr.fs = XCreateFontSet (display,
					fsname, &missing_list, &missing_count, &def_string);
	}
#endif
	return;
}

void chinput_destroy_ic (IMChangeICStruct *call_data)
{
	chinput_delete_ic (call_data->icid);
	return;
}

void chinput_set_ic (IMChangeICStruct *call_data)
{
	IC *rec = chinput_find_ic (call_data->icid);
	if (rec == NULL)
		return;
	chinput_store_ic (rec, call_data);
	return;
}

void chinput_get_ic (IMChangeICStruct *call_data)
{
	XICAttribute *ic_attr = call_data->ic_attr;
	XICAttribute *pre_attr = call_data->preedit_attr;
	XICAttribute *sts_attr = call_data->status_attr;
	register int i;
	IC *rec = chinput_find_ic (call_data->icid);
	if (rec == NULL)
	{
	  return;
	}

	for (i = 0; i < (int)call_data->ic_attr_num; i++, ic_attr++)
	{
		if (chinput_match_ic_attr (XNFilterEvents, ic_attr))
		{
			ic_attr->value = (void *)malloc (sizeof(CARD32));
			*(CARD32*)ic_attr->value = KeyPressMask|KeyReleaseMask;
			ic_attr->value_length = sizeof(CARD32);
		}
	}

	/* preedit attributes */
	for (i = 0; i < (int)call_data->preedit_attr_num; i++, pre_attr++)
	{
		if (chinput_match_ic_attr (XNArea, pre_attr))
		{
			pre_attr->value = (void *)malloc (sizeof(XRectangle));
			*(XRectangle*)pre_attr->value = rec->pre_attr.area;
			pre_attr->value_length = sizeof(XRectangle);
		}
		else if (chinput_match_ic_attr (XNAreaNeeded, pre_attr))
		{
			pre_attr->value = (void *)malloc (sizeof(XRectangle));
			*(XRectangle*)pre_attr->value = rec->pre_attr.area_needed;
			pre_attr->value_length = sizeof(XRectangle);
		}
		else if (chinput_match_ic_attr (XNSpotLocation, pre_attr))
		{
			pre_attr->value = (void *)malloc (sizeof(XPoint));
			*(XPoint*)pre_attr->value = rec->pre_attr.spot_location;
			pre_attr->value_length = sizeof(XPoint);
		}
		else if (chinput_match_ic_attr (XNFontSet, pre_attr))
		{
			CARD16 base_len = (CARD16)strlen (rec->pre_attr.base_font);
			int total_len = sizeof(CARD16) + (CARD16)base_len;
			char *p;

			pre_attr->value = (void *)malloc (total_len);
			p = (char *)pre_attr->value;
			memmove(p, &base_len, sizeof(CARD16));
			p += sizeof(CARD16);
			strncpy(p, rec->pre_attr.base_font, base_len);
			pre_attr->value_length = total_len;

		}
		else if (chinput_match_ic_attr (XNForeground, pre_attr))
		{
			pre_attr->value = (void *)malloc (sizeof(long));
			*(long*)pre_attr->value = rec->pre_attr.foreground;
			pre_attr->value_length = sizeof(long);
		}
		else if (chinput_match_ic_attr (XNBackground, pre_attr))
		{
			pre_attr->value = (void *)malloc (sizeof(long));
			*(long*)pre_attr->value = rec->pre_attr.background;
			pre_attr->value_length = sizeof(long);
		}
		else if (chinput_match_ic_attr (XNLineSpace, pre_attr))
		{
			pre_attr->value = (void *)malloc (sizeof(long));
#if 0
			*(long*)pre_attr->value = rec->pre_attr.line_space;
#endif
			*(long*)pre_attr->value = 18;
			pre_attr->value_length = sizeof(long);
		}
	}

	/* status attributes */
	for (i = 0; i < (int)call_data->status_attr_num; i++, sts_attr++)
	{
		if (chinput_match_ic_attr (XNArea, sts_attr))
		{
			sts_attr->value = (void *)malloc (sizeof(XRectangle));
			*(XRectangle*)sts_attr->value = rec->sts_attr.area;
			sts_attr->value_length = sizeof(XRectangle);
		}
		else if (chinput_match_ic_attr (XNAreaNeeded, sts_attr))
		{
			sts_attr->value = (void *)malloc (sizeof(XRectangle));
			*(XRectangle*)sts_attr->value = rec->sts_attr.area_needed;
			sts_attr->value_length = sizeof(XRectangle);
		}
		else if (chinput_match_ic_attr (XNFontSet, sts_attr))
		{
			CARD16 base_len = (CARD16)strlen (rec->sts_attr.base_font);
			int total_len = sizeof(CARD16) + (CARD16)base_len;
			char *p;

			sts_attr->value = (void *)malloc (total_len);
			p = (char *)sts_attr->value;
			memmove(p, &base_len, sizeof(CARD16));
			p += sizeof(CARD16);
			strncpy (p, rec->sts_attr.base_font, base_len);
			sts_attr->value_length = total_len;
		}
		else if (chinput_match_ic_attr (XNForeground, sts_attr))
		{
			sts_attr->value = (void *)malloc (sizeof(long));
			*(long*)sts_attr->value = rec->sts_attr.foreground;
			sts_attr->value_length = sizeof(long);
		}
		else if (chinput_match_ic_attr (XNBackground, sts_attr))
		{
			sts_attr->value = (void *)malloc (sizeof(long));
			*(long*)sts_attr->value = rec->sts_attr.background;
			sts_attr->value_length = sizeof(long);
		}
		else if (chinput_match_ic_attr (XNLineSpace, sts_attr))
		{
			sts_attr->value = (void *)malloc (sizeof(long));
#if 0
			*(long*)sts_attr->value = rec->sts_attr.line_space;
#endif
			*(long*)sts_attr->value = 18;
			sts_attr->value_length = sizeof(long);
		}
	}
}
