/*
###
### 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/
###
*/
#include "all.h"

void chinput_enter_loop (void)
{
	XEvent report;

	/* get events, use first to display text and graphics */
	while (1)
	{
		XNextEvent(display, &report);
		if (XFilterEvent(&report, None) == True)
		{
			// fprintf(stderr, "window %ld\n",report.xany.window);
			continue;
		}
		switch (report.type)
		{
			case Expose:
				/* get all other Expose events on the queue */
				while (XCheckTypedEvent(display, Expose, &report))
					;
				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 ();
				}
				//redraw virtual keypad, if mapped
				if(HZServer.hzVKwin.onflag){
					chinput_vk_draw_panel ();
					chinput_vk_draw_window ();
				}
				break;

			/* it seems that no input is available.... but I think
			   one can add a hack to this, ... e.g. create a virtual
			   window and set focus to that window, send keypress event
			   to this window */
			case KeyPress:
				chinput_root_proc_key (report);	
				break;

			case ButtonPress:
				/* Left mouse button may drag the window or
				   press down a button on panel */
				if (report.xbutton.button == Button1)
					chinput_util_proc_left_btn(report);
				/* Right mouse button popup virtual keyboard */
				else if(report.xbutton.button == Button3)
					chinput_util_proc_right_btn();
				break;

			case VisibilityNotify:
			case ConfigureNotify:
				//XRaiseWindow(display, window1);
				//XRaiseWindow(display, window4);
				//XRaiseWindow(display, window5);
				//XRaiseWindow(display, window6);
				break;

			case ClientMessage:
				chinput_util_print_dbg_msg (">> Got ClientMessage. \n");
#if 0
				if (report.xclient.message_type != hz_input_atom)
				{
					printf ("ClientMessage != hz_input_atom\n");
					break;
				}
				else
				{
					long *items;
					int nitem, cnt;
					char buf [17] = {0};
					char *buf_ptr = buf;
					items = report.xclient.data.l;
					nitem = report.xclient.data.b[0];
#endif

#if 0
					/* testing the forward string ability */
					chinput_xim_forward_string (buf, 2);
#endif
#if 0
					/* This can test the data we got */
					/* remember to reverse the byte order for print out */
					for (cnt = 1; cnt <= nitem; cnt++)
						printf ("ClientMessage : item l[%d] is %c%c.\n",
								cnt,
								(char) ((items[cnt] & 0x0000FF00) >> 8),
								(char) items[cnt] & 0x000000FF);
#endif

#if 0
					/* We're really packup the char string and forward it now*/
					for (cnt = 1; cnt <= nitem; cnt++)
					{
						if ((items [cnt] & 0xFF000000) >> 24)
						{
							printf ("4 byte\n");
							*(buf_ptr++) = (char) (items [cnt] >> 24);
							*(buf_ptr++) = (char) (items [cnt] >> 16);
							*(buf_ptr++) = (char) (items [cnt] >> 8);
							*(buf_ptr++) = (char) items [cnt];
						}
						else if ((items [cnt] & 0x0000FF00) >> 8)
						{
							printf ("2 byte\n");
							*(buf_ptr++) = (char) (items [cnt] >> 8);
							*(buf_ptr++) = (char) items [cnt];
						}
						else if ((char) items [cnt])
							*(buf_ptr++) = (char) items [cnt];
						else
							printf ("Warning NULL Byte from XClientMessage\n");
					}
					chinput_xim_forward_string (buf, buf_ptr - buf);
//					chinput_xim_forward_event (XK_Left, False, False, False);
				}
#endif
				break;
			case EnterNotify:
			case FocusIn:
#if 0
				XRaiseWindow (display, window4);
				XRaiseWindow (display, window5);
				XRaiseWindow (display, window6);
#endif
				break;
			case MotionNotify:
#if 0
				XRaiseWindow (display, window4);
				XRaiseWindow (display, window5);
				XRaiseWindow (display, window6);
#endif
				if (report.xmotion.window == window2)
					chinput_overspot_proc_caret_window (report.xmotion.x, report.xmotion.y);
				break;
			default:
				break;
		} /* end switch */
	} /* end while */
}

