11ab64890Smrg/******************************************************************
21ab64890Smrg
31ab64890Smrg           Copyright 1992, 1993, 1994 by FUJITSU LIMITED
41ab64890Smrg
51ab64890SmrgPermission to use, copy, modify, distribute, and sell this software
61ab64890Smrgand its documentation for any purpose is hereby granted without fee,
71ab64890Smrgprovided that the above copyright notice appear in all copies and
81ab64890Smrgthat both that copyright notice and this permission notice appear
91ab64890Smrgin supporting documentation, and that the name of FUJITSU LIMITED
101ab64890Smrgnot be used in advertising or publicity pertaining to distribution
111ab64890Smrgof the software without specific, written prior permission.
121ab64890SmrgFUJITSU LIMITED makes no representations about the suitability of
1361b2299dSmrgthis software for any purpose.
141ab64890SmrgIt is provided "as is" without express or implied warranty.
151ab64890Smrg
161ab64890SmrgFUJITSU LIMITED DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
171ab64890SmrgINCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
181ab64890SmrgEVENT SHALL FUJITSU LIMITED BE LIABLE FOR ANY SPECIAL, INDIRECT OR
191ab64890SmrgCONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
201ab64890SmrgUSE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
211ab64890SmrgOTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
221ab64890SmrgPERFORMANCE OF THIS SOFTWARE.
231ab64890Smrg
2461b2299dSmrg  Author: Takashi Fujiwara     FUJITSU LIMITED
251ab64890Smrg                               fujiwara@a80.tech.yk.fujitsu.co.jp
261ab64890Smrg
271ab64890Smrg******************************************************************/
281ab64890Smrg
291ab64890Smrg#ifdef HAVE_CONFIG_H
301ab64890Smrg#include <config.h>
311ab64890Smrg#endif
321ab64890Smrg#include <X11/Xatom.h>
331ab64890Smrg#include "Xlibint.h"
341ab64890Smrg#include "Xlcint.h"
351ab64890Smrg#include "Ximint.h"
361ab64890Smrg
37eb411b4bSmrgXic
381ab64890Smrg_XimICOfXICID(
391ab64890Smrg    Xim		  im,
401ab64890Smrg    XICID	  icid)
411ab64890Smrg{
421ab64890Smrg    Xic		  pic;
431ab64890Smrg
441ab64890Smrg    for (pic = (Xic)im->core.ic_chain; pic; pic = (Xic)pic->core.next) {
451ab64890Smrg	if (pic->private.proto.icid == icid)
461ab64890Smrg	    return pic;
471ab64890Smrg    }
481ab64890Smrg    return (Xic)0;
491ab64890Smrg}
501ab64890Smrg
51eb411b4bSmrgstatic void
521ab64890Smrg_XimProcIMSetEventMask(
531ab64890Smrg    Xim		 im,
541ab64890Smrg    XPointer	 buf)
551ab64890Smrg{
561ab64890Smrg    EVENTMASK	*buf_l = (EVENTMASK *)buf;
571ab64890Smrg
581ab64890Smrg    im->private.proto.forward_event_mask     = buf_l[0];
591ab64890Smrg    im->private.proto.synchronous_event_mask = buf_l[1];
601ab64890Smrg    return;
611ab64890Smrg}
621ab64890Smrg
63eb411b4bSmrgstatic void
641ab64890Smrg_XimProcICSetEventMask(
651ab64890Smrg    Xic		 ic,
661ab64890Smrg    XPointer	 buf)
671ab64890Smrg{
681ab64890Smrg    EVENTMASK	*buf_l = (EVENTMASK *)buf;
691ab64890Smrg
701ab64890Smrg    ic->private.proto.forward_event_mask     = buf_l[0];
711ab64890Smrg    ic->private.proto.synchronous_event_mask = buf_l[1];
721ab64890Smrg    _XimReregisterFilter(ic);
731ab64890Smrg    return;
741ab64890Smrg}
751ab64890Smrg
76eb411b4bSmrgBool
771ab64890Smrg_XimSetEventMaskCallback(
781ab64890Smrg    Xim		 xim,
791ab64890Smrg    INT16	 len,
801ab64890Smrg    XPointer	 data,
811ab64890Smrg    XPointer	 call_data)
821ab64890Smrg{
831ab64890Smrg    CARD16	*buf_s = (CARD16 *)((CARD8 *)data + XIM_HEADER_SIZE);
841ab64890Smrg    XIMID        imid = buf_s[0];
851ab64890Smrg    XICID        icid = buf_s[1];
861ab64890Smrg    Xim		 im = (Xim)call_data;
871ab64890Smrg    Xic		 ic;
881ab64890Smrg
891ab64890Smrg    if (imid == im->private.proto.imid) {
901ab64890Smrg	if (icid) {
91e9628295Smrg	    if (!(ic = _XimICOfXICID(im, icid)))
92e9628295Smrg		return False;
931ab64890Smrg	    _XimProcICSetEventMask(ic, (XPointer)&buf_s[2]);
941ab64890Smrg	} else {
951ab64890Smrg	    _XimProcIMSetEventMask(im, (XPointer)&buf_s[2]);
961ab64890Smrg	}
971ab64890Smrg	return True;
981ab64890Smrg    }
991ab64890Smrg    return False;
1001ab64890Smrg}
1011ab64890Smrg
102eb411b4bSmrgstatic Bool
1031ab64890Smrg_XimSyncCheck(
1041ab64890Smrg    Xim          im,
1051ab64890Smrg    INT16        len,
1061ab64890Smrg    XPointer	 data,
1071ab64890Smrg    XPointer     arg)
1081ab64890Smrg{
1091ab64890Smrg    Xic		 ic  = (Xic)arg;
1101ab64890Smrg    CARD16	*buf_s = (CARD16 *)((CARD8 *)data + XIM_HEADER_SIZE);
1111ab64890Smrg    CARD8	 major_opcode = *((CARD8 *)data);
1121ab64890Smrg    CARD8	 minor_opcode = *((CARD8 *)data + 1);
1131ab64890Smrg    XIMID	 imid = buf_s[0];
1141ab64890Smrg    XICID	 icid = buf_s[1];
1151ab64890Smrg
1161ab64890Smrg    if ((major_opcode == XIM_SYNC_REPLY)
1171ab64890Smrg     && (minor_opcode == 0)
1181ab64890Smrg     && (imid == im->private.proto.imid)
1191ab64890Smrg     && (icid == ic->private.proto.icid))
1201ab64890Smrg	return True;
1211ab64890Smrg    if ((major_opcode == XIM_ERROR)
1221ab64890Smrg     && (minor_opcode == 0)
1231ab64890Smrg     && (buf_s[2] & XIM_IMID_VALID)
1241ab64890Smrg     && (imid == im->private.proto.imid)
1251ab64890Smrg     && (buf_s[2] & XIM_ICID_VALID)
1261ab64890Smrg     && (icid == ic->private.proto.icid))
1271ab64890Smrg	return True;
1281ab64890Smrg    return False;
1291ab64890Smrg}
1301ab64890Smrg
131eb411b4bSmrgBool
1321ab64890Smrg_XimSync(
1331ab64890Smrg    Xim		 im,
1341ab64890Smrg    Xic		 ic)
1351ab64890Smrg{
1361ab64890Smrg    CARD32	 buf32[BUFSIZE/4];
1371ab64890Smrg    CARD8	*buf = (CARD8 *)buf32;
1381ab64890Smrg    CARD16	*buf_s = (CARD16 *)&buf[XIM_HEADER_SIZE];
1391ab64890Smrg    INT16	 len;
1401ab64890Smrg    CARD32	 reply32[BUFSIZE/4];
1411ab64890Smrg    char	*reply = (char *)reply32;
1421ab64890Smrg    XPointer	 preply;
1431ab64890Smrg    int		 buf_size;
1441ab64890Smrg    int		 ret_code;
1451ab64890Smrg
1461ab64890Smrg    buf_s[0] = im->private.proto.imid;		/* imid */
1471ab64890Smrg    buf_s[1] = ic->private.proto.icid;		/* icid */
1481ab64890Smrg
1491ab64890Smrg    len = sizeof(CARD16)			/* sizeof imid */
1501ab64890Smrg	+ sizeof(CARD16);			/* sizeof icid */
1511ab64890Smrg
1521ab64890Smrg    _XimSetHeader((XPointer)buf, XIM_SYNC, 0, &len);
1531ab64890Smrg    if (!(_XimWrite(im, len, (XPointer)buf)))
1541ab64890Smrg	return False;
1551ab64890Smrg    _XimFlush(im);
1561ab64890Smrg    buf_size = BUFSIZE;
1571ab64890Smrg    ret_code = _XimRead(im, &len, (XPointer)reply, buf_size,
1581ab64890Smrg					_XimSyncCheck, (XPointer)ic);
1591ab64890Smrg    if(ret_code == XIM_TRUE) {
1601ab64890Smrg	preply = reply;
1611ab64890Smrg    } else if(ret_code == XIM_OVERFLOW) {
1621ab64890Smrg	if(len <= 0) {
1631ab64890Smrg	    preply = reply;
1641ab64890Smrg	} else {
1651ab64890Smrg	    buf_size = len;
166818534a1Smrg	    preply = Xmalloc(len);
1671ab64890Smrg	    ret_code = _XimRead(im, &len, preply, buf_size,
1681ab64890Smrg					_XimSyncCheck, (XPointer)ic);
1691ab64890Smrg	    if(ret_code != XIM_TRUE) {
1701ab64890Smrg		Xfree(preply);
1711ab64890Smrg		return False;
1721ab64890Smrg	    }
1731ab64890Smrg	}
1741ab64890Smrg    } else {
1751ab64890Smrg	return False;
1761ab64890Smrg    }
1771ab64890Smrg    buf_s = (CARD16 *)((char *)preply + XIM_HEADER_SIZE);
1781ab64890Smrg    if (*((CARD8 *)preply) == XIM_ERROR) {
1791ab64890Smrg	_XimProcError(im, 0, (XPointer)&buf_s[3]);
1801ab64890Smrg	if(reply != preply)
1811ab64890Smrg	    Xfree(preply);
1821ab64890Smrg	return False;
1831ab64890Smrg    }
1841ab64890Smrg    if(reply != preply)
1851ab64890Smrg	Xfree(preply);
1861ab64890Smrg    return True;
1871ab64890Smrg}
1881ab64890Smrg
189eb411b4bSmrgBool
1901ab64890Smrg_XimProcSyncReply(
1911ab64890Smrg    Xim		 im,
1921ab64890Smrg    Xic		 ic)
1931ab64890Smrg{
1941ab64890Smrg    CARD32	 buf32[BUFSIZE/4];
1951ab64890Smrg    CARD8	*buf = (CARD8 *)buf32;
1961ab64890Smrg    CARD16	*buf_s = (CARD16 *)&buf[XIM_HEADER_SIZE];
1971ab64890Smrg    INT16	 len;
1981ab64890Smrg
1991ab64890Smrg    buf_s[0] = im->private.proto.imid;		/* imid */
2001ab64890Smrg    buf_s[1] = ic->private.proto.icid;		/* icid */
2011ab64890Smrg
2021ab64890Smrg    len = sizeof(CARD16)			/* sizeof imid */
2031ab64890Smrg	+ sizeof(CARD16);			/* sizeof icid */
2041ab64890Smrg
2051ab64890Smrg    _XimSetHeader((XPointer)buf, XIM_SYNC_REPLY, 0, &len);
2061ab64890Smrg    if (!(_XimWrite(im, len, (XPointer)buf)))
2071ab64890Smrg	return False;
2081ab64890Smrg    _XimFlush(im);
2091ab64890Smrg    return True;
2101ab64890Smrg}
2111ab64890Smrg
212eb411b4bSmrgBool
2131ab64890Smrg_XimRespSyncReply(
2141ab64890Smrg    Xic		 ic,
2151ab64890Smrg    BITMASK16	 mode)
2161ab64890Smrg{
217eb411b4bSmrg    if (mode & XimSYNCHRONUS) /* SYNC Request */
218eb411b4bSmrg	MARK_NEED_SYNC_REPLY(ic->core.im);
21961b2299dSmrg
2201ab64890Smrg    return True;
2211ab64890Smrg}
2221ab64890Smrg
223eb411b4bSmrgBool
2241ab64890Smrg_XimSyncCallback(
2251ab64890Smrg    Xim		 xim,
2261ab64890Smrg    INT16	 len,
2271ab64890Smrg    XPointer	 data,
2281ab64890Smrg    XPointer	 call_data)
2291ab64890Smrg{
2301ab64890Smrg    CARD16	*buf_s = (CARD16 *)((CARD8 *)data + XIM_HEADER_SIZE);
2311ab64890Smrg    XIMID        imid = buf_s[0];
2321ab64890Smrg    XICID        icid = buf_s[1];
2331ab64890Smrg    Xim		 im = (Xim)call_data;
2341ab64890Smrg    Xic		 ic;
2351ab64890Smrg
2361ab64890Smrg    if ((imid == im->private.proto.imid)
2371ab64890Smrg     && (ic = _XimICOfXICID(im, icid))) {
2381ab64890Smrg	(void)_XimProcSyncReply(im, ic);
2391ab64890Smrg	return True;
2401ab64890Smrg    }
2411ab64890Smrg    return False;
2421ab64890Smrg}
2431ab64890Smrg
244eb411b4bSmrgstatic INT16
2451ab64890Smrg_XimSetEventToWire(
2461ab64890Smrg    XEvent	*ev,
2471ab64890Smrg    xEvent	*event)
2481ab64890Smrg{
2491ab64890Smrg    if (!(_XimProtoEventToWire(ev, event, False)))
2501ab64890Smrg	return 0;
2511ab64890Smrg    event->u.u.sequenceNumber =
2521ab64890Smrg		((XAnyEvent *)ev)->serial & (unsigned long)0xffff;
2531ab64890Smrg    return sz_xEvent;
2541ab64890Smrg}
2551ab64890Smrg
256eb411b4bSmrgstatic Bool
2571ab64890Smrg_XimForwardEventCore(
2581ab64890Smrg    Xic		 ic,
2591ab64890Smrg    XEvent	*ev,
2601ab64890Smrg    Bool	 sync)
2611ab64890Smrg{
2621ab64890Smrg    Xim		 im = (Xim)ic->core.im;
2631ab64890Smrg    CARD32	 buf32[BUFSIZE/4];
2641ab64890Smrg    CARD8	*buf = (CARD8 *)buf32;
2651ab64890Smrg    CARD16	*buf_s = (CARD16 *)&buf[XIM_HEADER_SIZE];
2661ab64890Smrg    CARD32	 reply32[BUFSIZE/4];
2671ab64890Smrg    char	*reply = (char *)reply32;
2681ab64890Smrg    XPointer	 preply;
2691ab64890Smrg    int		 buf_size;
2701ab64890Smrg    int		 ret_code;
2711ab64890Smrg    INT16	 len;
2721ab64890Smrg
273818534a1Smrg    bzero(buf32, sizeof(buf32)); /* valgrind noticed uninitialized memory use! */
274818534a1Smrg
2751ab64890Smrg    if (!(len = _XimSetEventToWire(ev, (xEvent *)&buf_s[4])))
2761ab64890Smrg	return False;				/* X event */
2771ab64890Smrg
2781ab64890Smrg    buf_s[0] = im->private.proto.imid;		/* imid */
2791ab64890Smrg    buf_s[1] = ic->private.proto.icid;		/* icid */
2801ab64890Smrg    buf_s[2] = sync ? XimSYNCHRONUS : 0;	/* flag */
2811ab64890Smrg    buf_s[3] =
2821ab64890Smrg        (CARD16)((((XAnyEvent *)ev)->serial & ~((unsigned long)0xffff)) >> 16);
2831ab64890Smrg						/* serial number */
2841ab64890Smrg
2851ab64890Smrg    len += sizeof(CARD16)			/* sizeof imid */
2861ab64890Smrg	 + sizeof(CARD16)			/* sizeof icid */
2871ab64890Smrg	 + sizeof(BITMASK16)			/* sizeof flag */
2881ab64890Smrg	 + sizeof(CARD16);			/* sizeof serila number */
2891ab64890Smrg
2901ab64890Smrg    _XimSetHeader((XPointer)buf, XIM_FORWARD_EVENT, 0, &len);
2911ab64890Smrg    if (!(_XimWrite(im, len, (XPointer)buf)))
2921ab64890Smrg	return False;
2931ab64890Smrg    _XimFlush(im);
2941ab64890Smrg
2951ab64890Smrg    if (sync) {
2961ab64890Smrg	buf_size = BUFSIZE;
2971ab64890Smrg	ret_code = _XimRead(im, &len, (XPointer)reply, buf_size,
2981ab64890Smrg					_XimSyncCheck, (XPointer)ic);
2991ab64890Smrg	if(ret_code == XIM_TRUE) {
3001ab64890Smrg	    preply = reply;
3011ab64890Smrg	} else if(ret_code == XIM_OVERFLOW) {
3021ab64890Smrg	    if(len <= 0) {
3031ab64890Smrg		preply = reply;
3041ab64890Smrg	    } else {
3051ab64890Smrg		buf_size = len;
306818534a1Smrg		preply = Xmalloc(len);
3071ab64890Smrg		ret_code = _XimRead(im, &len, preply, buf_size,
3081ab64890Smrg					_XimSyncCheck, (XPointer)ic);
3091ab64890Smrg		if(ret_code != XIM_TRUE) {
3101ab64890Smrg		    Xfree(preply);
3111ab64890Smrg		    return False;
3121ab64890Smrg		}
3131ab64890Smrg	    }
3141ab64890Smrg	} else {
3151ab64890Smrg	    return False;
3161ab64890Smrg	}
3171ab64890Smrg	buf_s = (CARD16 *)((char *)preply + XIM_HEADER_SIZE);
3181ab64890Smrg	if (*((CARD8 *)preply) == XIM_ERROR) {
3191ab64890Smrg	    _XimProcError(im, 0, (XPointer)&buf_s[3]);
3201ab64890Smrg	    if(reply != preply)
3211ab64890Smrg		Xfree(preply);
3221ab64890Smrg	    return False;
3231ab64890Smrg	}
3241ab64890Smrg	if(reply != preply)
3251ab64890Smrg	    Xfree(preply);
3261ab64890Smrg    }
3271ab64890Smrg    return True;
3281ab64890Smrg}
3291ab64890Smrg
330eb411b4bSmrgBool
3311ab64890Smrg_XimForwardEvent(
3321ab64890Smrg    Xic		 ic,
3331ab64890Smrg    XEvent	*ev,
3341ab64890Smrg    Bool	 sync)
3351ab64890Smrg{
3361ab64890Smrg#ifdef EXT_FORWARD
3371ab64890Smrg    if (((ev->type == KeyPress) || (ev->type == KeyRelease)))
3381ab64890Smrg	if (_XimExtForwardKeyEvent(ic, (XKeyEvent *)ev, sync))
3391ab64890Smrg	    return True;
3401ab64890Smrg#endif
3411ab64890Smrg    return _XimForwardEventCore(ic, ev, sync);
3421ab64890Smrg}
3431ab64890Smrg
3443b4ba46cSmrgBool
3453b4ba46cSmrg_XimFabricateSerial(
3463b4ba46cSmrg    Xim			 im,
3473b4ba46cSmrg    XKeyEvent		*event)
3483b4ba46cSmrg{
3493b4ba46cSmrg    /* GTK2 XIM module sets serial=0. */
3503b4ba46cSmrg    if (!event->serial || !im->private.proto.enable_fabricated_order) {
3513b4ba46cSmrg	MARK_FABRICATED(im);
3523b4ba46cSmrg	return True;
3533b4ba46cSmrg    }
3543b4ba46cSmrg    if (event->serial == im->private.proto.fabricated_serial) {
3553b4ba46cSmrg	fprintf(stderr, "%s,%d: The key event is already fabricated.\n", __FILE__, __LINE__);
3563b4ba46cSmrg	return False;
3573b4ba46cSmrg    }
3583b4ba46cSmrg    if (im->private.proto.fabricated_serial)
3593b4ba46cSmrg	fprintf(stderr, "%s,%d: Tried to fabricate a wrong key event.\n", __FILE__, __LINE__);
3603b4ba46cSmrg
3613b4ba46cSmrg    MARK_FABRICATED(im);
3623b4ba46cSmrg    im->private.proto.fabricated_serial = event->serial;
3633b4ba46cSmrg    im->private.proto.fabricated_time = event->time;
3643b4ba46cSmrg    return True;
3653b4ba46cSmrg}
3663b4ba46cSmrg
3673b4ba46cSmrgBool
3683b4ba46cSmrg_XimUnfabricateSerial(
3693b4ba46cSmrg    Xim			 im,
3703b4ba46cSmrg    Xic			 ic,
3713b4ba46cSmrg    XKeyEvent		*event)
3723b4ba46cSmrg{
3733b4ba46cSmrg    if (!im->private.proto.enable_fabricated_order) {
3743b4ba46cSmrg	UNMARK_FABRICATED(im);
3753b4ba46cSmrg	return True;
3763b4ba46cSmrg    }
3773b4ba46cSmrg    /* GTK2 XIM module sets serial=0. */
3783b4ba46cSmrg    if (!event->serial) {
3793b4ba46cSmrg	/* _XimCommitRecv() sets event->serial and call _XimFabricateSerial()
3803b4ba46cSmrg	 * but GTK2 XIM always reset event->serial=0 with XFilterEvent().
3813b4ba46cSmrg	 */
3823b4ba46cSmrg	if (ic && ic->private.proto.commit_info)
3833b4ba46cSmrg	    im->private.proto.fabricated_serial = 0;
3843b4ba46cSmrg	UNMARK_FABRICATED(im);
3853b4ba46cSmrg	return True;
3863b4ba46cSmrg    }
3873b4ba46cSmrg    if (!im->private.proto.fabricated_serial) {
3883b4ba46cSmrg	fprintf(stderr, "%s,%d: The key event is already unfabricated.\n", __FILE__, __LINE__);
3893b4ba46cSmrg	return False;
3903b4ba46cSmrg    }
3913b4ba46cSmrg    if (event->serial != im->private.proto.fabricated_serial)
3923b4ba46cSmrg	fprintf(stderr, "%s,%d: Tried to unfabricate a wrong key event.\n", __FILE__, __LINE__);
3933b4ba46cSmrg
3943b4ba46cSmrg    im->private.proto.fabricated_serial = 0;
3953b4ba46cSmrg    im->private.proto.fabricated_time = 0;
3963b4ba46cSmrg    UNMARK_FABRICATED(im);
3973b4ba46cSmrg    return True;
3983b4ba46cSmrg}
3993b4ba46cSmrg
4003b4ba46cSmrgBool
4013b4ba46cSmrg_XimIsFabricatedSerial(
4023b4ba46cSmrg    Xim			 im,
4033b4ba46cSmrg    XKeyEvent		*event)
4043b4ba46cSmrg{
4053b4ba46cSmrg    /* GTK2 XIM module sets serial=0. */
4063b4ba46cSmrg    if (!event->serial || !im->private.proto.enable_fabricated_order)
4073b4ba46cSmrg	return IS_FABRICATED(im) ? True : False;
4083b4ba46cSmrg    if (event->serial == im->private.proto.fabricated_serial)
4093b4ba46cSmrg	return True;
4103b4ba46cSmrg    if (!im->private.proto.fabricated_serial)
4113b4ba46cSmrg	return False;
4123b4ba46cSmrg    /* Rotate time */
4133b4ba46cSmrg    if (event->time < im->private.proto.fabricated_time) {
4143b4ba46cSmrg	if (event->time >= 1000)
4153b4ba46cSmrg	    im->private.proto.fabricated_time = 0;
4163b4ba46cSmrg    } else if (event->time - im->private.proto.fabricated_time > 1000) {
4173b4ba46cSmrg	fprintf(stderr,
4183b4ba46cSmrg	        "%s,%d: The application disposed a key event with %ld serial.\n",
4193b4ba46cSmrg	        __FILE__, __LINE__,
4203b4ba46cSmrg	        im->private.proto.fabricated_serial);
4213b4ba46cSmrg	im->private.proto.enable_fabricated_order = False;
4223b4ba46cSmrg	if (IS_FABRICATED(im)) {
4233b4ba46cSmrg	    if (event->serial)
4243b4ba46cSmrg		im->private.proto.fabricated_serial = event->serial;
4253b4ba46cSmrg	    return True;
4263b4ba46cSmrg	}
4273b4ba46cSmrg    }
4283b4ba46cSmrg    return False;
4293b4ba46cSmrg}
4303b4ba46cSmrg
431eb411b4bSmrgstatic void
4321ab64890Smrg_XimProcEvent(
4331ab64890Smrg    Display		*d,
4341ab64890Smrg    Xic			 ic,
4351ab64890Smrg    XEvent		*ev,
4361ab64890Smrg    CARD16		*buf)
4371ab64890Smrg{
4381ab64890Smrg    INT16	 serial = buf[0];
4391ab64890Smrg    xEvent	*xev = (xEvent *)&buf[1];
4401ab64890Smrg
4411ab64890Smrg    _XimProtoWireToEvent(ev, xev, False);
4421ab64890Smrg    ev->xany.serial |= serial << 16;
4431ab64890Smrg    ev->xany.send_event = False;
4441ab64890Smrg    ev->xany.display = d;
4453b4ba46cSmrg    _XimFabricateSerial((Xim)ic->core.im, &ev->xkey);
4461ab64890Smrg    return;
4471ab64890Smrg}
4481ab64890Smrg
449eb411b4bSmrgstatic Bool
4501ab64890Smrg_XimForwardEventRecv(
4511ab64890Smrg    Xim		 im,
4521ab64890Smrg    Xic		 ic,
4531ab64890Smrg    XPointer	 buf)
4541ab64890Smrg{
4551ab64890Smrg    CARD16	*buf_s = (CARD16 *)buf;
4561ab64890Smrg    Display	*d = im->core.display;
4571ab64890Smrg    XEvent	 ev;
4581ab64890Smrg
4591ab64890Smrg    _XimProcEvent(d, ic, &ev, &buf_s[1]);
4601ab64890Smrg
4611ab64890Smrg    (void)_XimRespSyncReply(ic, buf_s[0]);
4621ab64890Smrg
4631ab64890Smrg    XPutBackEvent(d, &ev);
4641ab64890Smrg
4651ab64890Smrg    return True;
4661ab64890Smrg}
4671ab64890Smrg
468eb411b4bSmrgBool
4691ab64890Smrg_XimForwardEventCallback(
4701ab64890Smrg    Xim		 xim,
4711ab64890Smrg    INT16	 len,
4721ab64890Smrg    XPointer	 data,
4731ab64890Smrg    XPointer	 call_data)
4741ab64890Smrg{
4751ab64890Smrg    CARD16	*buf_s = (CARD16 *)((CARD8 *)data + XIM_HEADER_SIZE);
4761ab64890Smrg    XIMID        imid = buf_s[0];
4771ab64890Smrg    XICID        icid = buf_s[1];
4781ab64890Smrg    Xim		 im = (Xim)call_data;
4791ab64890Smrg    Xic		 ic;
4801ab64890Smrg
4811ab64890Smrg    if ((imid == im->private.proto.imid)
4821ab64890Smrg     && (ic = _XimICOfXICID(im, icid))) {
4831ab64890Smrg	(void)_XimForwardEventRecv(im, ic, (XPointer)&buf_s[2]);
4841ab64890Smrg	return True;
4851ab64890Smrg    }
4861ab64890Smrg    return False;
4871ab64890Smrg}
4881ab64890Smrg
489eb411b4bSmrgstatic Bool
4901ab64890Smrg_XimRegisterTriggerkey(
4911ab64890Smrg    Xim			 im,
4921ab64890Smrg    XPointer		 buf)
4931ab64890Smrg{
4941ab64890Smrg    CARD32		*buf_l = (CARD32 *)buf;
4951ab64890Smrg    CARD32		 len;
4961ab64890Smrg    CARD32 		*key;
4971ab64890Smrg
4981ab64890Smrg    if (IS_DYNAMIC_EVENT_FLOW(im))	/* already Dynamic event flow mode */
4991ab64890Smrg	return True;
5001ab64890Smrg
5011ab64890Smrg    /*
5021ab64890Smrg     *  register onkeylist
5031ab64890Smrg     */
5041ab64890Smrg
5051ab64890Smrg    len = buf_l[0];				/* length of on-keys */
5061ab64890Smrg    len += sizeof(INT32);			/* sizeof length of on-keys */
5071ab64890Smrg
508818534a1Smrg    if (!(key = Xmalloc(len))) {
5091ab64890Smrg	_XimError(im, 0, XIM_BadAlloc, (INT16)0, (CARD16)0, (char *)NULL);
5101ab64890Smrg	return False;
5111ab64890Smrg    }
5121ab64890Smrg    memcpy((char *)key, (char *)buf_l, len);
5131ab64890Smrg    im->private.proto.im_onkeylist = key;
5141ab64890Smrg
5151ab64890Smrg    MARK_DYNAMIC_EVENT_FLOW(im);
5161ab64890Smrg
5171ab64890Smrg    /*
5181ab64890Smrg     *  register offkeylist
5191ab64890Smrg     */
5201ab64890Smrg
5211ab64890Smrg    buf_l = (CARD32 *)((char *)buf + len);
5221ab64890Smrg    len = buf_l[0];				/* length of off-keys */
5231ab64890Smrg    len += sizeof(INT32);			/* sizeof length of off-keys */
5241ab64890Smrg
525818534a1Smrg    if (!(key = Xmalloc(len))) {
5261ab64890Smrg	_XimError(im, 0, XIM_BadAlloc, (INT16)0, (CARD16)0, (char *)NULL);
5271ab64890Smrg	return False;
5281ab64890Smrg    }
5291ab64890Smrg
5301ab64890Smrg    memcpy((char *)key, (char *)buf_l, len);
5311ab64890Smrg    im->private.proto.im_offkeylist = key;
5321ab64890Smrg
5331ab64890Smrg    return True;
5341ab64890Smrg}
5351ab64890Smrg
536eb411b4bSmrgBool
5371ab64890Smrg_XimRegisterTriggerKeysCallback(
5381ab64890Smrg    Xim		 xim,
5391ab64890Smrg    INT16	 len,
5401ab64890Smrg    XPointer	 data,
5411ab64890Smrg    XPointer	 call_data)
5421ab64890Smrg{
5431ab64890Smrg    CARD16	*buf_s = (CARD16 *)((CARD8 *)data + XIM_HEADER_SIZE);
5441ab64890Smrg    Xim		 im = (Xim)call_data;
5451ab64890Smrg
5461ab64890Smrg    (void )_XimRegisterTriggerkey(im, (XPointer)&buf_s[2]);
5471ab64890Smrg    return True;
5481ab64890Smrg}
5491ab64890Smrg
550eb411b4bSmrgEVENTMASK
5511ab64890Smrg_XimGetWindowEventmask(
5521ab64890Smrg    Xic		 ic)
5531ab64890Smrg{
5541ab64890Smrg    Xim			im = (Xim )ic->core.im;
5551ab64890Smrg    XWindowAttributes	atr;
5561ab64890Smrg
5571ab64890Smrg    if (!XGetWindowAttributes(im->core.display, ic->core.focus_window, &atr))
5581ab64890Smrg	return 0;
5591ab64890Smrg    return (EVENTMASK)atr.your_event_mask;
5601ab64890Smrg}
5611ab64890Smrg
5621ab64890Smrg
563eb411b4bSmrgstatic Bool
5641ab64890Smrg_XimTriggerNotifyCheck(
5651ab64890Smrg    Xim          im,
5661ab64890Smrg    INT16        len,
5671ab64890Smrg    XPointer	 data,
5681ab64890Smrg    XPointer     arg)
5691ab64890Smrg{
5701ab64890Smrg    Xic		 ic  = (Xic)arg;
5711ab64890Smrg    CARD16	*buf_s = (CARD16 *)((CARD8 *)data + XIM_HEADER_SIZE);
5721ab64890Smrg    CARD8	 major_opcode = *((CARD8 *)data);
5731ab64890Smrg    CARD8	 minor_opcode = *((CARD8 *)data + 1);
5741ab64890Smrg    XIMID	 imid = buf_s[0];
5751ab64890Smrg    XICID	 icid = buf_s[1];
5761ab64890Smrg
5771ab64890Smrg    if ((major_opcode == XIM_TRIGGER_NOTIFY_REPLY)
5781ab64890Smrg     && (minor_opcode == 0)
5791ab64890Smrg     && (imid == im->private.proto.imid)
5801ab64890Smrg     && (icid == ic->private.proto.icid))
5811ab64890Smrg	return True;
5821ab64890Smrg    if ((major_opcode == XIM_ERROR)
5831ab64890Smrg     && (minor_opcode == 0)
5841ab64890Smrg     && (buf_s[2] & XIM_IMID_VALID)
5851ab64890Smrg     && (imid == im->private.proto.imid)
5861ab64890Smrg     && (buf_s[2] & XIM_ICID_VALID)
5871ab64890Smrg     && (icid == ic->private.proto.icid))
5881ab64890Smrg	return True;
5891ab64890Smrg    return False;
5901ab64890Smrg}
5911ab64890Smrg
592eb411b4bSmrgBool
5931ab64890Smrg_XimTriggerNotify(
5941ab64890Smrg    Xim		 im,
5951ab64890Smrg    Xic		 ic,
5961ab64890Smrg    int		 mode,
5971ab64890Smrg    CARD32	 idx)
5981ab64890Smrg{
5991ab64890Smrg    CARD32	 buf32[BUFSIZE/4];
6001ab64890Smrg    CARD8	*buf = (CARD8 *)buf32;
6011ab64890Smrg    CARD16	*buf_s = (CARD16 *)&buf[XIM_HEADER_SIZE];
6021ab64890Smrg    CARD32	*buf_l = (CARD32 *)&buf[XIM_HEADER_SIZE];
6031ab64890Smrg    CARD32	 reply32[BUFSIZE/4];
6041ab64890Smrg    char	*reply = (char *)reply32;
6051ab64890Smrg    XPointer	 preply;
6061ab64890Smrg    int		 buf_size;
6071ab64890Smrg    int		 ret_code;
6081ab64890Smrg    INT16	 len;
6091ab64890Smrg    EVENTMASK	 mask = _XimGetWindowEventmask(ic);
6101ab64890Smrg
6111ab64890Smrg    buf_s[0] = im->private.proto.imid;	/* imid */
6121ab64890Smrg    buf_s[1] = ic->private.proto.icid;	/* icid */
6131ab64890Smrg    buf_l[1] = mode;			/* flag */
6141ab64890Smrg    buf_l[2] = idx;			/* index of keys list */
6151ab64890Smrg    buf_l[3] = mask;			/* select-event-mask */
6161ab64890Smrg
6171ab64890Smrg    len = sizeof(CARD16)		/* sizeof imid */
6181ab64890Smrg	+ sizeof(CARD16)		/* sizeof icid */
6191ab64890Smrg	+ sizeof(CARD32)		/* sizeof flag */
6201ab64890Smrg	+ sizeof(CARD32)		/* sizeof index of key list */
6211ab64890Smrg	+ sizeof(EVENTMASK);		/* sizeof select-event-mask */
6221ab64890Smrg
6231ab64890Smrg    _XimSetHeader((XPointer)buf, XIM_TRIGGER_NOTIFY, 0, &len);
6241ab64890Smrg    if (!(_XimWrite(im, len, (XPointer)buf)))
6251ab64890Smrg	return False;
6261ab64890Smrg    _XimFlush(im);
6271ab64890Smrg    buf_size = BUFSIZE;
6281ab64890Smrg    ret_code = _XimRead(im, &len, (XPointer)reply, buf_size,
6291ab64890Smrg				_XimTriggerNotifyCheck, (XPointer)ic);
6301ab64890Smrg    if(ret_code == XIM_TRUE) {
6311ab64890Smrg	preply = reply;
6321ab64890Smrg    } else if(ret_code == XIM_OVERFLOW) {
6331ab64890Smrg	if(len <= 0) {
6341ab64890Smrg	    preply = reply;
6351ab64890Smrg	} else {
6361ab64890Smrg	    buf_size = len;
637818534a1Smrg	    preply = Xmalloc(len);
6383b4ba46cSmrg	    ret_code = _XimRead(im, &len, preply, buf_size,
6391ab64890Smrg				_XimTriggerNotifyCheck, (XPointer)ic);
6401ab64890Smrg	    if(ret_code != XIM_TRUE) {
6411ab64890Smrg		Xfree(preply);
6421ab64890Smrg		return False;
6431ab64890Smrg	    }
6441ab64890Smrg	}
6451ab64890Smrg    } else {
6461ab64890Smrg	return False;
6471ab64890Smrg    }
6481ab64890Smrg    buf_s = (CARD16 *)((char *)preply + XIM_HEADER_SIZE);
6491ab64890Smrg    if (*((CARD8 *)preply) == XIM_ERROR) {
6501ab64890Smrg	_XimProcError(im, 0, (XPointer)&buf_s[3]);
6511ab64890Smrg	if(reply != preply)
6521ab64890Smrg	    Xfree(preply);
6531ab64890Smrg	return False;
6541ab64890Smrg    }
6551ab64890Smrg    if(reply != preply)
6561ab64890Smrg	Xfree(preply);
6571ab64890Smrg    return True;
6581ab64890Smrg}
6591ab64890Smrg
660eb411b4bSmrgstatic Bool
6611ab64890Smrg_XimRegCommitInfo(
6621ab64890Smrg    Xic			 ic,
6631ab64890Smrg    char		*string,
6641ab64890Smrg    int			 string_len,
6651ab64890Smrg    KeySym		*keysym,
6661ab64890Smrg    int			 keysym_len)
6671ab64890Smrg{
6681ab64890Smrg    XimCommitInfo	info;
6691ab64890Smrg
670818534a1Smrg    if (!(info = Xmalloc(sizeof(XimCommitInfoRec))))
6711ab64890Smrg	return False;
6721ab64890Smrg    info->string	= string;
6731ab64890Smrg    info->string_len	= string_len;
6741ab64890Smrg    info->keysym	= keysym;
6751ab64890Smrg    info->keysym_len	= keysym_len;
6761ab64890Smrg    info->next = ic->private.proto.commit_info;
6771ab64890Smrg    ic->private.proto.commit_info = info;
6781ab64890Smrg    return True;
6791ab64890Smrg}
6801ab64890Smrg
681eb411b4bSmrgstatic void
6823b4ba46cSmrg_XimUnregRealCommitInfo(
6833b4ba46cSmrg    Xic			ic,
6843b4ba46cSmrg    Bool		reverse)
6851ab64890Smrg{
6861ab64890Smrg    XimCommitInfo	info;
6873b4ba46cSmrg    XimCommitInfo	prev_info = NULL;
6881ab64890Smrg
6893b4ba46cSmrg    info = ic->private.proto.commit_info;
6903b4ba46cSmrg    while (reverse && info) {
6913b4ba46cSmrg	if (!info->next)
6923b4ba46cSmrg	    break;
6933b4ba46cSmrg	prev_info = info;
6943b4ba46cSmrg	info = info->next;
6953b4ba46cSmrg    }
6963b4ba46cSmrg    if (!info)
6971ab64890Smrg	return;
6981ab64890Smrg
6990f8248bfSmrg    Xfree(info->string);
7000f8248bfSmrg    Xfree(info->keysym);
7013b4ba46cSmrg    if (prev_info)
7023b4ba46cSmrg	prev_info->next = info->next;
7033b4ba46cSmrg    else
7043b4ba46cSmrg	ic->private.proto.commit_info = info->next;
7051ab64890Smrg    Xfree(info);
7061ab64890Smrg    return;
7071ab64890Smrg}
7081ab64890Smrg
7093b4ba46cSmrgstatic void
7103b4ba46cSmrg_XimUnregCommitInfo(
7113b4ba46cSmrg    Xic			ic)
7123b4ba46cSmrg{
7133b4ba46cSmrg    _XimUnregRealCommitInfo(ic, False);
7143b4ba46cSmrg}
7153b4ba46cSmrg
7163b4ba46cSmrgstatic void
7173b4ba46cSmrg_XimUnregFirstCommitInfo(
7183b4ba46cSmrg    Xic			ic)
7193b4ba46cSmrg{
7203b4ba46cSmrg    _XimUnregRealCommitInfo(ic, True);
7213b4ba46cSmrg}
7223b4ba46cSmrg
723eb411b4bSmrgvoid
7241ab64890Smrg_XimFreeCommitInfo(
7251ab64890Smrg    Xic			ic)
7261ab64890Smrg{
7271ab64890Smrg    while (ic->private.proto.commit_info)
7281ab64890Smrg	_XimUnregCommitInfo(ic);
7291ab64890Smrg    return;
7301ab64890Smrg}
7311ab64890Smrg
7323b4ba46cSmrgstatic XimCommitInfo
7333b4ba46cSmrg_XimFirstCommitInfo(
7343b4ba46cSmrg    Xic			ic)
7353b4ba46cSmrg{
7363b4ba46cSmrg    XimCommitInfo info = ic->private.proto.commit_info;
7373b4ba46cSmrg    while (info) {
7383b4ba46cSmrg	if (!info->next)
7393b4ba46cSmrg	    break;
7403b4ba46cSmrg	info = info->next;
7413b4ba46cSmrg    }
7423b4ba46cSmrg    return info;
7433b4ba46cSmrg}
7443b4ba46cSmrg
745eb411b4bSmrgstatic Bool
7461ab64890Smrg_XimProcKeySym(
7471ab64890Smrg    Xic			  ic,
7481ab64890Smrg    CARD32		  sym,
7491ab64890Smrg    KeySym		**xim_keysym,
7501ab64890Smrg    int			 *xim_keysym_len)
7511ab64890Smrg{
7521ab64890Smrg    Xim			 im = (Xim)ic->core.im;
7531ab64890Smrg
754818534a1Smrg    if (!(*xim_keysym = Xmalloc(sizeof(KeySym)))) {
7551ab64890Smrg	_XimError(im, ic, XIM_BadAlloc, (INT16)0, (CARD16)0, (char *)NULL);
7561ab64890Smrg	return False;
7571ab64890Smrg    }
7581ab64890Smrg
7591ab64890Smrg    **xim_keysym = (KeySym)sym;
7601ab64890Smrg    *xim_keysym_len = 1;
7611ab64890Smrg
7621ab64890Smrg    return True;
7631ab64890Smrg}
7641ab64890Smrg
765eb411b4bSmrgstatic Bool
7661ab64890Smrg_XimProcCommit(
7671ab64890Smrg    Xic		  ic,
7681ab64890Smrg    BYTE	 *buf,
7691ab64890Smrg    int		  len,
7701ab64890Smrg    char	**xim_string,
7711ab64890Smrg    int		 *xim_string_len)
7721ab64890Smrg{
7731ab64890Smrg    Xim		 im = (Xim)ic->core.im;
7741ab64890Smrg    char	*string;
7751ab64890Smrg
776818534a1Smrg    if (!(string = Xmalloc(len + 1))) {
7771ab64890Smrg	_XimError(im, ic, XIM_BadAlloc, (INT16)0, (CARD16)0, (char *)NULL);
7781ab64890Smrg	return False;
7791ab64890Smrg    }
7801ab64890Smrg
7811ab64890Smrg    (void)memcpy(string, (char *)buf, len);
7821ab64890Smrg    string[len] = '\0';
7831ab64890Smrg
7841ab64890Smrg    *xim_string = string;
7851ab64890Smrg    *xim_string_len = len;
7861ab64890Smrg    return True;
7871ab64890Smrg}
7881ab64890Smrg
789eb411b4bSmrgstatic Bool
7901ab64890Smrg_XimCommitRecv(
7911ab64890Smrg    Xim		 im,
7921ab64890Smrg    Xic		 ic,
7931ab64890Smrg    XPointer	 buf)
7941ab64890Smrg{
7951ab64890Smrg    CARD16	*buf_s = (CARD16 *)buf;
7961ab64890Smrg    BITMASK16	 flag = buf_s[0];
7971ab64890Smrg    XKeyEvent	 ev;
7981ab64890Smrg    char	*string = NULL;
7991ab64890Smrg    int		 string_len = 0;
8001ab64890Smrg    KeySym	*keysym = NULL;
8011ab64890Smrg    int		 keysym_len = 0;
8021ab64890Smrg
8031ab64890Smrg    if ((flag & XimLookupBoth) == XimLookupChars) {
8041ab64890Smrg	if (!(_XimProcCommit(ic, (BYTE *)&buf_s[2],
8051ab64890Smrg			 		(int)buf_s[1], &string, &string_len)))
8061ab64890Smrg	    return False;
8071ab64890Smrg
8081ab64890Smrg    } else if ((flag & XimLookupBoth) == XimLookupKeySym) {
8091ab64890Smrg	if (!(_XimProcKeySym(ic, *(CARD32 *)&buf_s[2], &keysym, &keysym_len)))
8101ab64890Smrg	    return False;
8111ab64890Smrg
8121ab64890Smrg    } else if ((flag & XimLookupBoth) == XimLookupBoth) {
8131ab64890Smrg	if (!(_XimProcKeySym(ic, *(CARD32 *)&buf_s[2], &keysym, &keysym_len)))
8141ab64890Smrg	    return False;
8151ab64890Smrg
8161ab64890Smrg	if (!(_XimProcCommit(ic, (BYTE *)&buf_s[5],
8176cc2b21fSmrg					(int)buf_s[4], &string, &string_len))) {
8186cc2b21fSmrg	    Xfree(keysym);
8191ab64890Smrg	    return False;
8206cc2b21fSmrg	}
8211ab64890Smrg    }
8221ab64890Smrg
8231ab64890Smrg    if (!(_XimRegCommitInfo(ic, string, string_len, keysym, keysym_len))) {
8240f8248bfSmrg       Xfree(string);
8250f8248bfSmrg	Xfree(keysym);
8261ab64890Smrg	_XimError(im, ic, XIM_BadAlloc, (INT16)0, (CARD16)0, (char *)NULL);
8271ab64890Smrg	return False;
8281ab64890Smrg    }
8291ab64890Smrg
8301ab64890Smrg    (void)_XimRespSyncReply(ic, flag);
8311ab64890Smrg
832818534a1Smrg    bzero(&ev, sizeof(ev));	/* uninitialized : found when running kterm under valgrind */
8331ab64890Smrg
8341ab64890Smrg    ev.type = KeyPress;
8351ab64890Smrg    ev.send_event = False;
8361ab64890Smrg    ev.display = im->core.display;
8371ab64890Smrg    ev.window = ic->core.focus_window;
8381ab64890Smrg    ev.keycode = 0;
8391ab64890Smrg    ev.state = 0;
8401ab64890Smrg
841818534a1Smrg    ev.time = 0L;
842818534a1Smrg    ev.serial = LastKnownRequestProcessed(im->core.display);
8433b4ba46cSmrg
8443b4ba46cSmrg    if (ic->private.proto.registed_filter_event
8453b4ba46cSmrg	& (KEYPRESS_MASK | KEYRELEASE_MASK))
8463b4ba46cSmrg	    _XimFabricateSerial(im, &ev);
847818534a1Smrg    /* FIXME :
848818534a1Smrg       I wish there were COMMENTs (!) about the data passed around.
849818534a1Smrg    */
850818534a1Smrg#if 0
851818534a1Smrg    fprintf(stderr,"%s,%d: putback k press   FIXED ev.time=0 ev.serial=%lu\n", __FILE__, __LINE__, ev.serial);
852818534a1Smrg#endif
853818534a1Smrg
8541ab64890Smrg    XPutBackEvent(im->core.display, (XEvent *)&ev);
8551ab64890Smrg
8561ab64890Smrg    return True;
8571ab64890Smrg}
8581ab64890Smrg
859eb411b4bSmrgBool
8601ab64890Smrg_XimCommitCallback(
8611ab64890Smrg    Xim		 xim,
8621ab64890Smrg    INT16	 len,
8631ab64890Smrg    XPointer	 data,
8641ab64890Smrg    XPointer	 call_data)
8651ab64890Smrg{
8661ab64890Smrg    CARD16	*buf_s = (CARD16 *)((CARD8 *)data + XIM_HEADER_SIZE);
8671ab64890Smrg    XIMID        imid = buf_s[0];
8681ab64890Smrg    XICID        icid = buf_s[1];
8691ab64890Smrg    Xim		 im = (Xim)call_data;
8701ab64890Smrg    Xic		 ic;
8711ab64890Smrg
8721ab64890Smrg    if ((imid == im->private.proto.imid)
8731ab64890Smrg     && (ic = _XimICOfXICID(im, icid))) {
8741ab64890Smrg	(void)_XimCommitRecv(im, ic, (XPointer)&buf_s[2]);
8751ab64890Smrg	return True;
8761ab64890Smrg    }
8771ab64890Smrg    return False;
8781ab64890Smrg}
8791ab64890Smrg
880eb411b4bSmrgvoid
8811ab64890Smrg_XimProcError(
8821ab64890Smrg    Xim		 im,
8831ab64890Smrg    Xic		 ic,
8841ab64890Smrg    XPointer	 data)
8851ab64890Smrg{
8861ab64890Smrg    return;
8871ab64890Smrg}
8881ab64890Smrg
889eb411b4bSmrgBool
8901ab64890Smrg_XimErrorCallback(
8911ab64890Smrg    Xim		 xim,
8921ab64890Smrg    INT16	 len,
8931ab64890Smrg    XPointer	 data,
8941ab64890Smrg    XPointer	 call_data)
8951ab64890Smrg{
8961ab64890Smrg    CARD16	*buf_s = (CARD16 *)((CARD8 *)data + XIM_HEADER_SIZE);
8971ab64890Smrg    BITMASK16	 flag = buf_s[2];
8981ab64890Smrg    XIMID        imid;
8991ab64890Smrg    XICID        icid;
9001ab64890Smrg    Xim		 im = (Xim)call_data;
9011ab64890Smrg    Xic		 ic = NULL;
9021ab64890Smrg
9031ab64890Smrg    if (flag & XIM_IMID_VALID) {
9041ab64890Smrg	imid = buf_s[0];
9051ab64890Smrg	if (imid != im->private.proto.imid)
9061ab64890Smrg	    return False;
9071ab64890Smrg    }
9081ab64890Smrg    if (flag & XIM_ICID_VALID) {
9091ab64890Smrg	icid = buf_s[1];
9101ab64890Smrg	if (!(ic = _XimICOfXICID(im, icid)))
9111ab64890Smrg	    return False;
9121ab64890Smrg    }
9131ab64890Smrg    _XimProcError(im, ic, (XPointer)&buf_s[3]);
9141ab64890Smrg
9151ab64890Smrg    return True;
9161ab64890Smrg}
9171ab64890Smrg
918eb411b4bSmrgBool
9191ab64890Smrg_XimError(
9201ab64890Smrg    Xim		 im,
9211ab64890Smrg    Xic		 ic,
9221ab64890Smrg    CARD16	 error_code,
9231ab64890Smrg    INT16	 detail_length,
9241ab64890Smrg    CARD16	 type,
9251ab64890Smrg    char	*detail)
9261ab64890Smrg{
9271ab64890Smrg    CARD32	 buf32[BUFSIZE/4];
9281ab64890Smrg    CARD8	*buf = (CARD8 *)buf32;
9291ab64890Smrg    CARD16	*buf_s = (CARD16 *)&buf[XIM_HEADER_SIZE];
9301ab64890Smrg    INT16	 len = 0;
9311ab64890Smrg
9321ab64890Smrg    buf_s[0] = im->private.proto.imid;	/* imid */
9331ab64890Smrg    buf_s[2] = XIM_IMID_VALID;		/* flag */
9341ab64890Smrg    if (ic) {
9351ab64890Smrg    	buf_s[1] = ic->private.proto.icid;	/* icid */
9361ab64890Smrg	buf_s[2] |= XIM_ICID_VALID;		/* flag */
9371ab64890Smrg    }
9381ab64890Smrg    buf_s[3] = error_code;			/* Error Code */
9391ab64890Smrg    buf_s[4] = detail_length;			/* length of error detail */
9401ab64890Smrg    buf_s[5] = type;				/* type of error detail */
9411ab64890Smrg
9421ab64890Smrg    if (detail_length && detail) {
9431ab64890Smrg	len = detail_length;
9441ab64890Smrg	memcpy((char *)&buf_s[6], detail, len);
9451ab64890Smrg	XIM_SET_PAD(&buf_s[6], len);
9461ab64890Smrg    }
9471ab64890Smrg
9481ab64890Smrg    len += sizeof(CARD16)		/* sizeof imid */
9491ab64890Smrg	 + sizeof(CARD16)		/* sizeof icid */
9501ab64890Smrg	 + sizeof(BITMASK16)		/* sizeof flag */
9511ab64890Smrg	 + sizeof(CARD16)		/* sizeof error_code */
9521ab64890Smrg	 + sizeof(INT16)		/* sizeof length of detail */
9531ab64890Smrg	 + sizeof(CARD16);		/* sizeof type */
9541ab64890Smrg
9551ab64890Smrg    _XimSetHeader((XPointer)buf, XIM_ERROR, 0, &len);
9561ab64890Smrg    if (!(_XimWrite(im, len, (XPointer)buf)))
9571ab64890Smrg	return False;
9581ab64890Smrg    _XimFlush(im);
9591ab64890Smrg    return True;
9601ab64890Smrg}
9611ab64890Smrg
962eb411b4bSmrgstatic int
9631ab64890Smrg_Ximctsconvert(
9641ab64890Smrg    XlcConv	 conv,
9651ab64890Smrg    char	*from,
9661ab64890Smrg    int		 from_len,
9671ab64890Smrg    char	*to,
9681ab64890Smrg    int		 to_len,
9691ab64890Smrg    Status	*state)
9701ab64890Smrg{
9711ab64890Smrg    int		 from_left;
9721ab64890Smrg    int		 to_left;
9731ab64890Smrg    int		 from_savelen;
9741ab64890Smrg    int		 to_savelen;
9751ab64890Smrg    int		 from_cnvlen;
9761ab64890Smrg    int		 to_cnvlen;
9771ab64890Smrg    char	*from_buf;
9781ab64890Smrg    char	*to_buf;
9791ab64890Smrg    char	 scratchbuf[BUFSIZ];
9801ab64890Smrg    Status	 tmp_state;
9811ab64890Smrg
9821ab64890Smrg    if (!state)
9831ab64890Smrg	state = &tmp_state;
9841ab64890Smrg
9851ab64890Smrg    if (!conv || !from || !from_len) {
9861ab64890Smrg	*state = XLookupNone;
9871ab64890Smrg	return 0;
9881ab64890Smrg    }
9891ab64890Smrg
9901ab64890Smrg    /* Reset the converter.  The CompoundText at 'from' starts in
9911ab64890Smrg       initial state.  */
9921ab64890Smrg    _XlcResetConverter(conv);
9931ab64890Smrg
9941ab64890Smrg    from_left = from_len;
9951ab64890Smrg    to_left = BUFSIZ;
9961ab64890Smrg    from_cnvlen = 0;
9971ab64890Smrg    to_cnvlen = 0;
9981ab64890Smrg    for (;;) {
9991ab64890Smrg	from_buf = &from[from_cnvlen];
10001ab64890Smrg	from_savelen = from_left;
10011ab64890Smrg	to_buf = &scratchbuf[to_cnvlen];
10021ab64890Smrg	to_savelen = to_left;
10031ab64890Smrg	if (_XlcConvert(conv, (XPointer *)&from_buf, &from_left,
10041ab64890Smrg				 (XPointer *)&to_buf, &to_left, NULL, 0) < 0) {
10051ab64890Smrg	    *state = XLookupNone;
10061ab64890Smrg	    return 0;
10071ab64890Smrg	}
10081ab64890Smrg	from_cnvlen += (from_savelen - from_left);
10091ab64890Smrg	to_cnvlen += (to_savelen - to_left);
10101ab64890Smrg	if (from_left == 0) {
10111ab64890Smrg	    if (!to_cnvlen) {
10121ab64890Smrg		*state = XLookupNone;
10131ab64890Smrg		return 0;
10141ab64890Smrg           }
10151ab64890Smrg	   break;
10161ab64890Smrg	}
10171ab64890Smrg    }
10181ab64890Smrg
10191ab64890Smrg    if (!to || !to_len || (to_len < to_cnvlen)) {
10201ab64890Smrg       *state = XBufferOverflow;
10211ab64890Smrg    } else {
10221ab64890Smrg       memcpy(to, scratchbuf, to_cnvlen);
10231ab64890Smrg       *state = XLookupChars;
10241ab64890Smrg    }
10251ab64890Smrg    return to_cnvlen;
10261ab64890Smrg}
10271ab64890Smrg
1028eb411b4bSmrgint
102961b2299dSmrg_Ximctstombs(XIM xim, char *from, int from_len,
103061b2299dSmrg	     char *to, int to_len, Status *state)
10311ab64890Smrg{
10321ab64890Smrg    return _Ximctsconvert(((Xim)xim)->private.proto.ctom_conv,
10331ab64890Smrg			  from, from_len, to, to_len, state);
10341ab64890Smrg}
10351ab64890Smrg
1036eb411b4bSmrgint
10371ab64890Smrg_Ximctstowcs(
10381ab64890Smrg    XIM		 xim,
10391ab64890Smrg    char	*from,
10401ab64890Smrg    int		 from_len,
10411ab64890Smrg    wchar_t	*to,
10421ab64890Smrg    int		 to_len,
10431ab64890Smrg    Status	*state)
10441ab64890Smrg{
10451ab64890Smrg    Xim		 im = (Xim)xim;
10461ab64890Smrg    XlcConv	 conv = im->private.proto.ctow_conv;
10471ab64890Smrg    int		 from_left;
10481ab64890Smrg    int		 to_left;
10491ab64890Smrg    int		 from_savelen;
10501ab64890Smrg    int		 to_savelen;
10511ab64890Smrg    int		 from_cnvlen;
10521ab64890Smrg    int		 to_cnvlen;
10531ab64890Smrg    char	*from_buf;
10541ab64890Smrg    wchar_t	*to_buf;
10551ab64890Smrg    wchar_t	 scratchbuf[BUFSIZ];
10561ab64890Smrg    Status	 tmp_state;
10571ab64890Smrg
10581ab64890Smrg    if (!state)
10591ab64890Smrg	state = &tmp_state;
10601ab64890Smrg
10611ab64890Smrg    if (!conv || !from || !from_len) {
10621ab64890Smrg	*state = XLookupNone;
10631ab64890Smrg	return 0;
10641ab64890Smrg    }
10651ab64890Smrg
10661ab64890Smrg    /* Reset the converter.  The CompoundText at 'from' starts in
10671ab64890Smrg       initial state.  */
10681ab64890Smrg    _XlcResetConverter(conv);
106961b2299dSmrg
10701ab64890Smrg    from_left = from_len;
10711ab64890Smrg    to_left = BUFSIZ;
10721ab64890Smrg    from_cnvlen = 0;
10731ab64890Smrg    to_cnvlen = 0;
10741ab64890Smrg    for (;;) {
10751ab64890Smrg	from_buf = &from[from_cnvlen];
10761ab64890Smrg       from_savelen = from_left;
10771ab64890Smrg       to_buf = &scratchbuf[to_cnvlen];
10781ab64890Smrg       to_savelen = to_left;
10791ab64890Smrg	if (_XlcConvert(conv, (XPointer *)&from_buf, &from_left,
10801ab64890Smrg				 (XPointer *)&to_buf, &to_left, NULL, 0) < 0) {
10811ab64890Smrg	    *state = XLookupNone;
10821ab64890Smrg	    return 0;
10831ab64890Smrg	}
10841ab64890Smrg	from_cnvlen += (from_savelen - from_left);
10851ab64890Smrg       to_cnvlen += (to_savelen - to_left);
10861ab64890Smrg	if (from_left == 0) {
10871ab64890Smrg           if (!to_cnvlen){
10881ab64890Smrg		*state = XLookupNone;
10891ab64890Smrg               return 0;
10901ab64890Smrg           }
10911ab64890Smrg	    break;
10921ab64890Smrg	}
10931ab64890Smrg    }
10941ab64890Smrg
10951ab64890Smrg    if (!to || !to_len || (to_len < to_cnvlen)) {
10961ab64890Smrg       *state = XBufferOverflow;
10971ab64890Smrg    } else {
10981ab64890Smrg       memcpy(to, scratchbuf, to_cnvlen * sizeof(wchar_t));
10991ab64890Smrg       *state = XLookupChars;
11001ab64890Smrg    }
11011ab64890Smrg    return to_cnvlen;
11021ab64890Smrg}
11031ab64890Smrg
1104eb411b4bSmrgint
11051ab64890Smrg_Ximctstoutf8(
11061ab64890Smrg    XIM		 xim,
11071ab64890Smrg    char	*from,
11081ab64890Smrg    int		 from_len,
11091ab64890Smrg    char	*to,
11101ab64890Smrg    int		 to_len,
11111ab64890Smrg    Status	*state)
11121ab64890Smrg{
11131ab64890Smrg    return _Ximctsconvert(((Xim)xim)->private.proto.ctoutf8_conv,
11141ab64890Smrg			  from, from_len, to, to_len, state);
11151ab64890Smrg}
11161ab64890Smrg
1117eb411b4bSmrgint
11181ab64890Smrg_XimProtoMbLookupString(
11191ab64890Smrg    XIC			 xic,
11201ab64890Smrg    XKeyEvent		*ev,
11211ab64890Smrg    char		*buffer,
11221ab64890Smrg    int			 bytes,
11231ab64890Smrg    KeySym		*keysym,
11241ab64890Smrg    Status		*state)
11251ab64890Smrg{
11261ab64890Smrg    Xic			 ic = (Xic)xic;
11271ab64890Smrg    Xim			 im = (Xim)ic->core.im;
11281ab64890Smrg    int			 ret;
11291ab64890Smrg    Status		 tmp_state;
11301ab64890Smrg    XimCommitInfo	 info;
11311ab64890Smrg
11321ab64890Smrg    if (!IS_SERVER_CONNECTED(im))
11331ab64890Smrg	return 0;
11341ab64890Smrg
11351ab64890Smrg    if (!state)
11361ab64890Smrg	state = &tmp_state;
11371ab64890Smrg
11381ab64890Smrg    if ((ev->type == KeyPress) && (ev->keycode == 0)) { /* Filter function */
11393b4ba46cSmrg	if (!(info = _XimFirstCommitInfo(ic))) {
11401ab64890Smrg	    *state = XLookupNone;
11411ab64890Smrg	    return 0;
11421ab64890Smrg	}
11431ab64890Smrg
11441ab64890Smrg	ret = im->methods->ctstombs((XIM)im, info->string,
11451ab64890Smrg			 	info->string_len, buffer, bytes, state);
11461ab64890Smrg	if (*state == XBufferOverflow)
11471ab64890Smrg            return ret;
11481ab64890Smrg	if (keysym && (info->keysym && *(info->keysym))) {
11491ab64890Smrg	    *keysym = *(info->keysym);
11501ab64890Smrg	    if (*state == XLookupChars)
11511ab64890Smrg		*state = XLookupBoth;
11521ab64890Smrg	    else
11531ab64890Smrg		*state = XLookupKeySym;
11541ab64890Smrg	}
11553b4ba46cSmrg	_XimUnregFirstCommitInfo(ic);
11561ab64890Smrg
11571ab64890Smrg    } else  if (ev->type == KeyPress) {
11581ab64890Smrg	ret = _XimLookupMBText(ic, ev, buffer, bytes, keysym, NULL);
11591ab64890Smrg	if (ret > 0) {
11601ab64890Smrg           if (ret > bytes)
11611ab64890Smrg               *state = XBufferOverflow;
11621ab64890Smrg           else if (keysym && *keysym != NoSymbol)
11631ab64890Smrg		*state = XLookupBoth;
11641ab64890Smrg	    else
11651ab64890Smrg		*state = XLookupChars;
11661ab64890Smrg	} else {
11671ab64890Smrg	    if (keysym && *keysym != NoSymbol)
11681ab64890Smrg		*state = XLookupKeySym;
11691ab64890Smrg	    else
11701ab64890Smrg		*state = XLookupNone;
11711ab64890Smrg	}
11721ab64890Smrg    } else {
11731ab64890Smrg	*state = XLookupNone;
11741ab64890Smrg	ret = 0;
11751ab64890Smrg    }
11761ab64890Smrg
11771ab64890Smrg    return ret;
11781ab64890Smrg}
11791ab64890Smrg
1180eb411b4bSmrgint
11811ab64890Smrg_XimProtoWcLookupString(
11821ab64890Smrg    XIC			 xic,
11831ab64890Smrg    XKeyEvent		*ev,
11841ab64890Smrg    wchar_t		*buffer,
11851ab64890Smrg    int			 bytes,
11861ab64890Smrg    KeySym		*keysym,
11871ab64890Smrg    Status		*state)
11881ab64890Smrg{
11891ab64890Smrg    Xic			 ic = (Xic)xic;
11901ab64890Smrg    Xim			 im = (Xim)ic->core.im;
11911ab64890Smrg    int			 ret;
11921ab64890Smrg    Status		 tmp_state;
11931ab64890Smrg    XimCommitInfo	 info;
11941ab64890Smrg
11951ab64890Smrg    if (!IS_SERVER_CONNECTED(im))
11961ab64890Smrg	return 0;
11971ab64890Smrg
11981ab64890Smrg    if (!state)
11991ab64890Smrg	state = &tmp_state;
12001ab64890Smrg
12011ab64890Smrg    if (ev->type == KeyPress && ev->keycode == 0) { /* Filter function */
12023b4ba46cSmrg	if (!(info = _XimFirstCommitInfo(ic))) {
12031ab64890Smrg           *state = XLookupNone;
12041ab64890Smrg	    return 0;
12051ab64890Smrg	}
12061ab64890Smrg
12071ab64890Smrg	ret = im->methods->ctstowcs((XIM)im, info->string,
12081ab64890Smrg			 	info->string_len, buffer, bytes, state);
12091ab64890Smrg	if (*state == XBufferOverflow)
12101ab64890Smrg           return ret;
12111ab64890Smrg	if (keysym && (info->keysym && *(info->keysym))) {
12121ab64890Smrg	    *keysym = *(info->keysym);
12131ab64890Smrg	    if (*state == XLookupChars)
12141ab64890Smrg		*state = XLookupBoth;
12151ab64890Smrg	    else
12161ab64890Smrg		*state = XLookupKeySym;
12171ab64890Smrg	}
12183b4ba46cSmrg	_XimUnregFirstCommitInfo(ic);
12191ab64890Smrg
12201ab64890Smrg    } else if (ev->type == KeyPress) {
12211ab64890Smrg	ret = _XimLookupWCText(ic, ev, buffer, bytes, keysym, NULL);
12221ab64890Smrg	if (ret > 0) {
12231ab64890Smrg           if (ret > bytes)
12241ab64890Smrg               *state = XBufferOverflow;
12251ab64890Smrg           else if (keysym && *keysym != NoSymbol)
12261ab64890Smrg		*state = XLookupBoth;
12271ab64890Smrg	    else
12281ab64890Smrg		*state = XLookupChars;
12291ab64890Smrg	} else {
12301ab64890Smrg	    if (keysym && *keysym != NoSymbol)
12311ab64890Smrg		*state = XLookupKeySym;
12321ab64890Smrg	    else
12331ab64890Smrg		*state = XLookupNone;
12341ab64890Smrg	}
12351ab64890Smrg    } else {
12361ab64890Smrg	*state = XLookupNone;
12371ab64890Smrg	ret = 0;
12381ab64890Smrg    }
12391ab64890Smrg
12401ab64890Smrg    return ret;
12411ab64890Smrg}
12421ab64890Smrg
1243eb411b4bSmrgint
12441ab64890Smrg_XimProtoUtf8LookupString(
12451ab64890Smrg    XIC			 xic,
12461ab64890Smrg    XKeyEvent		*ev,
12471ab64890Smrg    char		*buffer,
12481ab64890Smrg    int			 bytes,
12491ab64890Smrg    KeySym		*keysym,
12501ab64890Smrg    Status		*state)
12511ab64890Smrg{
12521ab64890Smrg    Xic			 ic = (Xic)xic;
12531ab64890Smrg    Xim			 im = (Xim)ic->core.im;
12541ab64890Smrg    int			 ret;
12551ab64890Smrg    Status		 tmp_state;
12561ab64890Smrg    XimCommitInfo	 info;
12571ab64890Smrg
12581ab64890Smrg    if (!IS_SERVER_CONNECTED(im))
12591ab64890Smrg	return 0;
12601ab64890Smrg
12611ab64890Smrg    if (!state)
12621ab64890Smrg	state = &tmp_state;
12631ab64890Smrg
12641ab64890Smrg    if (ev->type == KeyPress && ev->keycode == 0) { /* Filter function */
12653b4ba46cSmrg	if (!(info = _XimFirstCommitInfo(ic))) {
12661ab64890Smrg           *state = XLookupNone;
12671ab64890Smrg	    return 0;
12681ab64890Smrg	}
12691ab64890Smrg
12701ab64890Smrg	ret = im->methods->ctstoutf8((XIM)im, info->string,
12711ab64890Smrg			 	info->string_len, buffer, bytes, state);
12721ab64890Smrg	if (*state == XBufferOverflow)
12731ab64890Smrg           return ret;
12741ab64890Smrg	if (keysym && (info->keysym && *(info->keysym))) {
12751ab64890Smrg	    *keysym = *(info->keysym);
12761ab64890Smrg	    if (*state == XLookupChars)
12771ab64890Smrg		*state = XLookupBoth;
12781ab64890Smrg	    else
12791ab64890Smrg		*state = XLookupKeySym;
12801ab64890Smrg	}
12813b4ba46cSmrg	_XimUnregFirstCommitInfo(ic);
12821ab64890Smrg
12831ab64890Smrg    } else if (ev->type == KeyPress) {
12841ab64890Smrg	ret = _XimLookupUTF8Text(ic, ev, buffer, bytes, keysym, NULL);
12851ab64890Smrg	if (ret > 0) {
12861ab64890Smrg           if (ret > bytes)
12871ab64890Smrg               *state = XBufferOverflow;
12881ab64890Smrg           else if (keysym && *keysym != NoSymbol)
12891ab64890Smrg		*state = XLookupBoth;
12901ab64890Smrg	    else
12911ab64890Smrg		*state = XLookupChars;
12921ab64890Smrg	} else {
12931ab64890Smrg	    if (keysym && *keysym != NoSymbol)
12941ab64890Smrg		*state = XLookupKeySym;
12951ab64890Smrg	    else
12961ab64890Smrg		*state = XLookupNone;
12971ab64890Smrg	}
12981ab64890Smrg    } else {
12991ab64890Smrg	*state = XLookupNone;
13001ab64890Smrg	ret = 0;
13011ab64890Smrg    }
13021ab64890Smrg
13031ab64890Smrg    return ret;
13041ab64890Smrg}
1305