// I am the main program, I own all the globals

#define		MAIN_PROGRAM
#include	"all.h"
#undef		MAIN_PROGRAM

//locale now set by environment
void chinput_usage (void)
{
	printf("Usage: chinput [options]\n");
	printf("             -gb        -- enable GB encoding.\n");
	printf("             -big5      -- enable BIG5 encoding.\n");
	printf("             -big5hkscs -- enable BIG5HKSCS encoding.\n");
	printf("             -root      -- Root window Style.\n");
	printf("             -overspot  -- OverTheSpot style.\n");
	printf("             -onspot    -- OnTheSpot style.\n");
	printf("             -offspot   -- OffTheSpot style.\n");
	exit(0);
}

static void chinput_print_info (void)
{
	chinput_util_print_sys_msg ("\033[34;1m");
	chinput_util_print_sys_msg ("%s Version %s -- XIM Server\n", 
			HZSERVERNAME, HZSERVERVERSION);
	chinput_util_print_sys_msg ("Distributed under the terms of the GNU General Public License (GPL)\n");
	chinput_util_print_sys_msg ("\033[0m");
}

static int chinput_check_xlocale (void)
{
	char  olocale [20] = {0};
	char *olocalep = NULL;

	olocalep = setlocale (LC_CTYPE, NULL);
	strcpy (olocale, olocalep);

	setlocale (LC_CTYPE, "zh_HK.big5hkscs");
	if ( !XSupportsLocale ())
	{
		printf ("Error: locale \"zh_HK.big5hkscs\" not support by X\n");
		return -1;
	}

	setlocale (LC_CTYPE, "zh_TW.Big5");
	if ( !XSupportsLocale ())
	{
		printf ("Error: locale \"zh_Tw.Big5\" not support by X\n");
		return -1;
	}

	setlocale (LC_CTYPE, "zh_CN.GB2312");
	if ( !XSupportsLocale ())
	{
		printf ("Error: locale \"zh_CN.GB2312\" not support by X\n");
		return -1;
	}

	setlocale (LC_CTYPE, "zh_CN.GB18030");
	if ( !XSupportsLocale ())
	{
		printf ("Error: locale \"zh_CN.GB18030\" not support by X\n");
		return -1;
	}

	// Restore orignal locale
	setlocale (LC_CTYPE, olocale);
	return 0;
}

int main(int argc, char **argv)
{
	if (chinput_check_xlocale ())
		printf ("Locale Not Supported\n");
	chinput_print_info ();			//print out server information
	chinput_check_pyinput ();		//check ZNPY user dict
	chinput_init_resource ();		//internal resource init
	chinput_load_rcfile ();			//resource file
	chinput_load_rcime ();			//IME lists from Chinput.ad
	chinput_parse_parameters (argc, argv);		//parse parameters
	setlocale (LC_ALL, deflocale);	//locale set
	chinput_init_display ();		//display init
	printf ("Query font metrics\n");
	chinput_init_server_font ();	//font init
	chinput_init_windows ();		//windows geometry init
	printf ("Initialize XIM connection with X\n");
	chinput_xim_init ();			//xim init
	chinput_init_server_atom ();	//atom init
	printf ("Creating FontSet\n");
	chinput_init_server_gc ();		//font set
	chinput_init_icon ();			//load icons(imlib only)
	chinput_init_ime ();			//input method init
	chinput_init_daemon ();			//become a daemon
	chinput_init_signal_handler ();
	chinput_dock_init ();			//Initialize socket, for docking features.
	chinput_util_print_sys_msg ("%s initializing"
		   " ...................................... "
		   "[\033[32;1mOK\033[0m]\n",
		   HZSERVERNAME);
	chinput_enter_loop ();			//event processing

	//never reach here, but make compiler happy
	return 0;
}
