--- src/Makefile.in.orig	Fri Oct  9 16:49:02 1998
+++ src/Makefile.in	Fri Oct  9 16:49:43 1998
@@ -17,13 +17,14 @@
 first_rule: all
 dummy:
 
-SRCS =	command.c graphics.c grkelot.c main.c menubar.c misc.c \
+SRCS =	cli_xcin.c command.c graphics.c grkelot.c main.c menubar.c misc.c \
 	netdisp.c rmemset.c screen.c scrollbar.c utmp.c xdefaults.c xpm.c
 
-OBJS =	command.o graphics.o grkelot.o main.o menubar.o misc.o \
+OBJS =	cli_xcin.o command.o graphics.o grkelot.o main.o menubar.o misc.o \
 	netdisp.o rmemset.o screen.o scrollbar.o utmp.o xdefaults.o xpm.o
 
-HDRS =	feature.h protos.h grkelot.h rxvt.h rxvtgrx.h screen.h version.h
+HDRS =	feature.h protocol.h protos.h grkelot.h rxvt.h rxvtgrx.h screen.h \
+	state.h version.h
 
 PROS =	command.pro graphics.pro grkelot.pro main.pro menubar.pro misc.pro \
 	netdisp.pro rmemset.pro screen.pro scrollbar.pro utmp.pro xdefaults.pro xpm.pro
@@ -68,7 +69,7 @@
 	(cd $(srcdir); $(RMF) Makefile)
 
 install: allbin alldoc
-	$(INSTALL_PROGRAM) rxvt $(DESTDIR)$(bindir)/$(binprefix)rxvt
+	$(INSTALL_PROGRAM) rxvt $(DESTDIR)$(bindir)/$(binprefix)crxvt2
 
 uninstall:
 	(cd $(bindir); $(RMF) $(binprefix)rxvt)
--- src/cli_xcin.c.orig	Fri Oct  9 16:26:07 1998
+++ src/cli_xcin.c	Fri Oct  9 16:26:07 1998
@@ -0,0 +1,145 @@
+/*
+	Copyright (C) 1994,1995	Edward Der-Hua Liu, Hsin-Chu, Taiwan
+*/
+
+#include <stdio.h>
+#include <stdarg.h>
+#include <sys/types.h>
+#include <X11/Xlib.h>
+#include <X11/Xatom.h>
+#include "state.h"
+#include "protocol.h"
+
+static void p_err(char *fmt,...)
+{
+  va_list args;
+  
+  va_start(args, fmt);
+  fprintf(stderr,"%s:", "cli_xcin:");
+  vfprintf(stderr, fmt, args);
+  va_end(args);
+  fprintf(stderr,"\n");
+  exit(-1);
+} 
+
+
+static Atom xcin_atom=0;
+static Window xcin_win=None;
+static InmdState inmdstate;
+#define ENDIAN_TEST() (*(int *)"\x11\x22\x33\x44"==0x11223344)
+char my_endian;
+
+static connect_xcin(Display *display)
+{
+Window twin;
+xcin_atom=XInternAtom(display, XCIN_ATOM,False);
+my_endian=ENDIAN_TEST();  /* if == 11223344, it is big-endian */
+
+xcin_win=XGetSelectionOwner(display,xcin_atom);
+return xcin_win;
+}
+
+void send_FocusIn(Display *display, Window window)
+{
+	XClientMessageEvent event;
+	XEvent erreve;
+	char *tmp=event.data.b;
+
+	if (connect_xcin(display)==None) return;
+/* Ensure xcin exists, or the process will be hanged */
+	event.type=ClientMessage;
+	event.window=window;
+	event.message_type=xcin_atom;
+	event.format=8;
+	
+	tmp[0]=tmp[1]=tmp[2]=0xff;
+	tmp[3]=CLI_FOCUS_IN;
+	memcpy(&tmp[4],&inmdstate, sizeof(inmdstate));
+	XSendEvent(display, xcin_win, False, 0, (XEvent *)&event);
+}
+
+void send_FocusOut(Display *display, Window window)
+{
+	XClientMessageEvent event;
+	XEvent erreve;
+	char *tmp=event.data.b;
+
+	if (connect_xcin(display)==None) return;
+/* Ensure xcin exists, or the process will be hanged */
+	event.type=ClientMessage;
+	event.window=window;
+	event.message_type=xcin_atom;
+	event.format=8;
+	
+	tmp[0]=tmp[1]=tmp[2]=0xff;
+	tmp[3]=CLI_FOCUS_OUT;
+	memcpy(&tmp[4],&inmdstate, sizeof(inmdstate));
+	XSendEvent(display, xcin_win, False, 0, (XEvent *)&event);
+}
+
+static void big_little(char *i)
+{
+char t;
+t=*i; *i=*(i+3); *(i+3)=t;
+t=*(i+1); *(i+1)=*(i+2); *(i+2)=t;
+}
+static read_keys(Display *display, char *buf)
+{
+	Atom actual_type;
+	int actual_format,i;
+	u_long nitems,bytes_after;
+	char *ttt, *cp;
+	XCIN_RES res;
+	int ofs;
+ 
+	cp=(char *)&res;
+	ofs=0;
+	do { 
+	if (XGetWindowProperty(display,xcin_win,xcin_atom,
+		ofs/4,(sizeof(XCIN_RES)+3)/4,
+		True, AnyPropertyType, &actual_type,&actual_format,
+		&nitems,&bytes_after,(unsigned char **)&ttt) != Success)
+			puts("err property");
+		memcpy((char *)(&res)+(ofs & ~0x3), ttt, nitems);
+		XFree(ttt);
+		ofs=(ofs & ~0x3) + nitems;
+	} while ((!nitems && !ofs) || bytes_after>0);
+	if (my_endian) {
+		big_little((char *)&res.len);
+		big_little((char *)&res.status);
+	}
+	memcpy(buf,res.tkey, res.len);
+	inmdstate=res.inmdstate;
+	buf[res.len]=0;
+ 
+	return  res.status;
+}
+
+#include <X11/Xutil.h>
+
+static XComposeStatus compose_status = {NULL, 0};
+
+send_key(Display *display, Window win, XKeyEvent *eve, char *buf)
+{
+XClientMessageEvent event;
+XEvent erreve;
+char *tmp=event.data.b;
+u_char tttt[8];
+KeySym keysym;
+int count;
+
+if (xcin_win==None && connect_xcin(display)==None)
+	return K_REJECT;
+if ((xcin_win=XGetSelectionOwner(display,xcin_atom))==None)
+	return K_REJECT;
+
+event.type=ClientMessage;
+event.window=win;
+event.message_type=xcin_atom;
+event.format=32;
+count = XLookupString (eve, tttt, sizeof(tmp), (KeySym *)tmp, &compose_status);
+memcpy(&tmp[4],&eve->state,4);
+XSendEvent(display, xcin_win, False, 0, (XEvent *)&event);
+XSync(display,False);
+return (read_keys(display, buf));
+}
--- src/command.c.orig	Fri Oct  9 16:19:21 1998
+++ src/command.c	Fri Oct  9 16:23:11 1998
@@ -502,7 +502,7 @@
     ttydev = tty_name;
 
 # define PTYCHAR1	"pqrstuvwxyz"
-# define PTYCHAR2	"0123456789abcdef"
+# define PTYCHAR2	"0123456789abcdefghijklmnopqrstuv"
     for (c1 = PTYCHAR1; *c1; c1++) {
 	ptydev[len - 2] = ttydev[len - 2] = *c1;
 	for (c2 = PTYCHAR2; *c2; c2++) {
@@ -1285,6 +1285,11 @@
     int             ctrl, meta, shft, len;
     KeySym          keysym;
 
+#ifdef XCIN
+    char chstr[256];
+    int result;
+#endif /* XCIN */
+
 /*
  * use Num_Lock to toggle Keypad on/off.  If Num_Lock is off, allow an
  * escape sequence to toggle the Keypad.
@@ -1790,7 +1795,22 @@
 	fprintf(stderr, "'\n");
     }
 #endif				/* DEBUG_CMD */
+
+#ifdef XCIN    
+    result = send_key(Xdisplay, TermWin.parent, ev, chstr);
+    if (!result)
+    {  
+        tt_write (kbuf, len);
+    }
+    else
+    if (chstr[0])
+    {
+        tt_write (chstr, strlen(chstr));
+    }
+#else
     tt_write(kbuf, len);
+#endif /* XCIN */
+
 }
 /*}}} */
 
@@ -2127,6 +2147,9 @@
 	    if (Input_Context != NULL)
 		XSetICFocus(Input_Context);
 #endif
+#ifdef XCIN
+		send_FocusIn(Xdisplay, TermWin.parent);
+#endif
 	}
 	break;
 
@@ -2136,6 +2159,9 @@
 #ifndef NO_XLOCALE
 	    if (Input_Context != NULL)
 		XUnsetICFocus(Input_Context);
+#endif
+#ifdef XCIN
+		send_FocusOut(Xdisplay, TermWin.parent);
 #endif
 	}
 	break;
--- src/protocol.h.orig	Fri Oct  9 16:26:07 1998
+++ src/protocol.h	Fri Oct  9 16:26:08 1998
@@ -0,0 +1,13 @@
+/*
+	Copyright (C) 1994	Edward Der-Hua Liu, Hsin-Chu, Taiwan
+*/
+
+#define K_REJECT (0)
+#define K_ACCEPT (1) 
+
+#define CLI_FOCUS_IN (1)
+#define CLI_FOCUS_OUT (3)
+
+#define LOAD_KEYWORD (2)
+
+#define XCIN_ATOM "XCIN_A"
--- src/rxvt.h.orig	Fri Oct  9 16:23:21 1998
+++ src/rxvt.h	Fri Oct  9 16:25:56 1998
@@ -379,6 +379,12 @@
 # undef DEFINE_XTERM_COLOR	/* since kterm-color doesn't exist?   */
 #endif
 
+#ifdef XCIN
+#ifndef ZH                  /* must be with BIG5 support          */
+# define ZH
+#endif 
+#endif
+
 #ifdef ZH 
 # undef KANJI			/* can't put Chinese/Kanji together   */
 # undef GREEK_SUPPORT		
@@ -390,8 +396,8 @@
 #endif
 
 #define APL_CLASS	"XTerm"	/* class name */
-#define APL_SUBCLASS	"Rxvt"	/* also check resources under this name */
-#define APL_NAME	"rxvt"	/* normal name */
+#define APL_SUBCLASS	"crxvt"	/* also check resources under this name */
+#define APL_NAME	"crxvt"	/* normal name */
 
 /* COLORTERM, TERM environment variables */
 #define COLORTERMENV	"rxvt"
--- src/state.h.orig	Fri Oct  9 16:26:08 1998
+++ src/state.h	Fri Oct  9 16:26:08 1998
@@ -0,0 +1,23 @@
+/*
+	Copyright (C) 1994	Edward Der-Hua Liu, Hsin-Chu, Taiwan
+*/
+
+typedef struct {
+	char  kb_state;
+        char _CurInMethod;
+} InmdState;
+
+#define EngChiMask (1)
+#define HalfFullMask (2)
+
+typedef struct {
+	int len;
+	int status;
+	InmdState inmdstate;
+	char tkey[512];
+} XCIN_RES;
+
+#define EngChi ((inmdstate.kb_state & EngChiMask) ? 1:0)
+#define HalfFull ((inmdstate.kb_state & HalfFullMask) ? 1:0)
+#define CurInMethod (inmdstate._CurInMethod)
+/* extern InmdState inmdstate; */
