imConv.c revision 1ab64890
11ab64890Smrg/* $TOG: imConv.c /main/20 1998/06/17 15:46:41 kaleb $ */
21ab64890Smrg/******************************************************************
31ab64890Smrg
41ab64890Smrg              Copyright 1991, 1992 by Fuji Xerox Co.,Ltd.
51ab64890Smrg	      Copyright 1993, 1994 by FUJITSU LIMITED
61ab64890Smrg
71ab64890SmrgPermission to use, copy, modify, distribute, and sell this software
81ab64890Smrgand its documentation for any purpose is hereby granted without fee,
91ab64890Smrgprovided that the above copyright notice appear in all copies and
101ab64890Smrgthat both that copyright notice and this permission notice appear
111ab64890Smrgin supporting documentation, and that the name of Fuji Xerox Co.,Ltd.
121ab64890Smrg, and that the name of FUJITSU LIMITED not be used in advertising or
131ab64890Smrgpublicity pertaining to distribution of the software without specific,
141ab64890Smrg written prior permission.
151ab64890SmrgFuji Xerox Co.,Ltd. , and FUJITSU LIMITED makes no representations about
161ab64890Smrgthe suitability of this software for any purpose.
171ab64890SmrgIt is provided "as is" without express or implied warranty.
181ab64890Smrg
191ab64890SmrgFUJI XEROX CO.,LTD. AND FUJITSU LIMITED DISCLAIMS ALL WARRANTIES WITH
201ab64890SmrgREGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
211ab64890SmrgMERCHANTABILITY AND FITNESS, IN NO EVENT SHALL FUJI XEROX CO.,LTD.
221ab64890SmrgAND FUJITSU LIMITED BE LIABLE FOR ANY SPECIAL, INDIRECT
231ab64890SmrgOR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
241ab64890SmrgLOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
251ab64890SmrgNEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH
261ab64890SmrgTHE USE OR PERFORMANCE OF THIS SOFTWARE.
271ab64890Smrg
281ab64890Smrg  Auther:   Kazunori Nishihara, Fuji Xerox Co.,Ltd.
291ab64890Smrg                                kaz@ssdev.ksp.fujixerox.co.jp
301ab64890Smrg  Modifier: Takashi Fujiwara    FUJITSU LIMITED
311ab64890Smrg                                fujiwara@a80.tech.yk.fujitsu.co.jp
321ab64890Smrg
331ab64890Smrg******************************************************************/
341ab64890Smrg/* 2000 Modifier: Ivan Pascal	The XFree86 Project.
351ab64890Smrg */
361ab64890Smrg/* $XFree86: xc/lib/X11/imConv.c,v 1.32 2003/04/13 19:22:20 dawes Exp $ */
371ab64890Smrg
381ab64890Smrg#define NEED_EVENTS
391ab64890Smrg#ifdef HAVE_CONFIG_H
401ab64890Smrg#include <config.h>
411ab64890Smrg#endif
421ab64890Smrg#include <stdio.h>
431ab64890Smrg#include "Xlibint.h"
441ab64890Smrg#include "Xlcint.h"
451ab64890Smrg#include "Ximint.h"
461ab64890Smrg#include "XlcPubI.h"
471ab64890Smrg
481ab64890Smrg#ifdef XKB
491ab64890Smrg/*
501ab64890Smrg * rather than just call _XLookupString (i.e. the pre-XKB XLookupString)
511ab64890Smrg * do this because with XKB the event may have some funky modifiers that
521ab64890Smrg * _XLookupString doesn't grok.
531ab64890Smrg */
541ab64890Smrg#include "XKBlib.h"
551ab64890Smrg#define	XLOOKUPSTRING lookup_string
561ab64890Smrg#else
571ab64890Smrg#define	XLOOKUPSTRING XLookupString
581ab64890Smrg#endif
591ab64890Smrg
601ab64890Smrgtypedef unsigned int ucs4_t;
611ab64890Smrg
621ab64890Smrgtypedef int (*ucstocsConvProc)(
631ab64890Smrg    XPointer,
641ab64890Smrg    unsigned char *,
651ab64890Smrg    ucs4_t,
661ab64890Smrg    int
671ab64890Smrg);
681ab64890Smrg
691ab64890Smrgstruct SubstRec {
701ab64890Smrg    const char encoding_name[8];
711ab64890Smrg    const char charset_name[12];
721ab64890Smrg};
731ab64890Smrg
741ab64890Smrgstatic const struct SubstRec SubstTable[] = {
751ab64890Smrg    {"STRING", "ISO8859-1"},
761ab64890Smrg    {"TIS620", "TIS620-0"},
771ab64890Smrg    {"UTF-8",  "ISO10646-1"}
781ab64890Smrg};
791ab64890Smrg#define num_substitute (sizeof SubstTable / sizeof SubstTable[0])
801ab64890Smrg
811ab64890Smrg/*
821ab64890Smrg * Given the name of a charset, returns the pointer to convertors
831ab64890Smrg * from UCS char to specified charset char.
841ab64890Smrg * This converter is needed for _XimGetCharCode subroutine.
851ab64890Smrg */
861ab64890SmrgXPointer
871ab64890Smrg_XimGetLocaleCode (
881ab64890Smrg    _Xconst char*	encoding_name)
891ab64890Smrg{
901ab64890Smrg    XPointer cvt = _Utf8GetConvByName(encoding_name);
911ab64890Smrg    if (!cvt && encoding_name) {
921ab64890Smrg       int i;
931ab64890Smrg       for (i = 0; i < num_substitute; i++)
941ab64890Smrg           if (!strcmp(encoding_name, SubstTable[i].encoding_name))
951ab64890Smrg               return _Utf8GetConvByName(SubstTable[i].charset_name);
961ab64890Smrg    }
971ab64890Smrg    return cvt;
981ab64890Smrg}
991ab64890Smrg
1001ab64890Smrg/*
1011ab64890Smrg * Returns the locale dependent representation of a keysym.
1021ab64890Smrg * The locale's encoding is passed in form of pointer to UCS convertor.
1031ab64890Smrg * The resulting multi-byte sequence is placed starting at buf (a buffer
1041ab64890Smrg * with nbytes bytes, nbytes should be >= 8) and is NUL terminated.
1051ab64890Smrg * Returns the length of the resulting multi-byte sequence, excluding the
1061ab64890Smrg * terminating NUL byte. Return 0 if the keysym is not representable in the
1071ab64890Smrg * locale
1081ab64890Smrg */
1091ab64890Smrg/*ARGSUSED*/
1101ab64890Smrgint
1111ab64890Smrg_XimGetCharCode (
1121ab64890Smrg    XPointer            ucs_conv,
1131ab64890Smrg    KeySym 		keysym,
1141ab64890Smrg    unsigned char*	buf,
1151ab64890Smrg    int 		nbytes)
1161ab64890Smrg{
1171ab64890Smrg    int count = 0;
1181ab64890Smrg    ucstocsConvProc cvt = (ucstocsConvProc) ucs_conv;
1191ab64890Smrg    ucs4_t ucs4;
1201ab64890Smrg
1211ab64890Smrg    if (keysym < 0x80) {
1221ab64890Smrg        buf[0] = (char) keysym;
1231ab64890Smrg        count = 1;
1241ab64890Smrg    } else if (cvt) {
1251ab64890Smrg        ucs4 = KeySymToUcs4(keysym);
1261ab64890Smrg        if (ucs4)
1271ab64890Smrg            count = (*cvt)((XPointer)NULL, buf, ucs4, nbytes);
1281ab64890Smrg    }
1291ab64890Smrg
1301ab64890Smrg    if (count < 0)
1311ab64890Smrg       	count = 0;
1321ab64890Smrg    if (count>nbytes)
1331ab64890Smrg        return nbytes;
1341ab64890Smrg    if (count<nbytes)
1351ab64890Smrg        buf[count]= '\0';
1361ab64890Smrg    return count;
1371ab64890Smrg}
1381ab64890Smrg
1391ab64890Smrg#ifdef XKB
1401ab64890Smrgstatic int lookup_string(
1411ab64890Smrg    XKeyEvent*		event,
1421ab64890Smrg    char*		buffer,
1431ab64890Smrg    int			nbytes,
1441ab64890Smrg    KeySym*		keysym,
1451ab64890Smrg    XComposeStatus*	status)
1461ab64890Smrg{
1471ab64890Smrg    int ret;
1481ab64890Smrg    unsigned ctrls = XkbGetXlibControls (event->display);
1491ab64890Smrg    XkbSetXlibControls (event->display,
1501ab64890Smrg			XkbLC_ForceLatin1Lookup, XkbLC_ForceLatin1Lookup);
1511ab64890Smrg    ret = XLookupString(event, (char *)buffer, nbytes, keysym, status);
1521ab64890Smrg    XkbSetXlibControls (event->display,
1531ab64890Smrg			XkbLC_ForceLatin1Lookup, ctrls);
1541ab64890Smrg    return ret;
1551ab64890Smrg}
1561ab64890Smrg#endif
1571ab64890Smrg
1581ab64890Smrg#define BUF_SIZE (20)
1591ab64890Smrg
1601ab64890Smrgint
1611ab64890Smrg_XimLookupMBText(
1621ab64890Smrg    Xic			 ic,
1631ab64890Smrg    XKeyEvent*		event,
1641ab64890Smrg    char*		buffer,
1651ab64890Smrg    int			nbytes,
1661ab64890Smrg    KeySym*		keysym,
1671ab64890Smrg    XComposeStatus*	status)
1681ab64890Smrg{
1691ab64890Smrg    int count;
1701ab64890Smrg    KeySym symbol;
1711ab64890Smrg    Status	dummy;
1721ab64890Smrg    Xim	im = (Xim)ic->core.im;
1731ab64890Smrg    XimCommonPrivateRec* private = &im->private.common;
1741ab64890Smrg    unsigned char look[BUF_SIZE];
1751ab64890Smrg    ucs4_t ucs4;
1761ab64890Smrg
1771ab64890Smrg    /* force a latin-1 lookup for compatibility */
1781ab64890Smrg    count = XLOOKUPSTRING(event, (char *)buffer, nbytes, &symbol, status);
1791ab64890Smrg    if (keysym != NULL) *keysym = symbol;
1801ab64890Smrg    if ((nbytes == 0) || (symbol == NoSymbol)) return count;
1811ab64890Smrg
1821ab64890Smrg    if (count > 1) {
1831ab64890Smrg	memcpy(look, (char *)buffer,count);
1841ab64890Smrg	look[count] = '\0';
1851ab64890Smrg	if ((count = im->methods->ctstombs(ic->core.im,
1861ab64890Smrg				(char*) look, count,
1871ab64890Smrg				buffer, nbytes, &dummy)) < 0) {
1881ab64890Smrg	    count = 0;
1891ab64890Smrg	}
1901ab64890Smrg    } else if ((count == 0) ||
1911ab64890Smrg	       (count == 1 && (symbol > 0x7f && symbol < 0xff00))) {
1921ab64890Smrg
1931ab64890Smrg        XPointer from = (XPointer) &ucs4;
1941ab64890Smrg        XPointer to =   (XPointer) look;
1951ab64890Smrg        int from_len = 1;
1961ab64890Smrg        int to_len = BUF_SIZE;
1971ab64890Smrg        XPointer args[1];
1981ab64890Smrg        XlcCharSet charset;
1991ab64890Smrg        args[0] = (XPointer) &charset;
2001ab64890Smrg        ucs4 = (ucs4_t) KeySymToUcs4(symbol);
2011ab64890Smrg        if (!ucs4)
2021ab64890Smrg            return 0;
2031ab64890Smrg
2041ab64890Smrg	if (_XlcConvert(private->ucstoc_conv,
2051ab64890Smrg                        &from, &from_len, &to, &to_len,
2061ab64890Smrg                        args, 1 ) != 0) {
2071ab64890Smrg	    count = 0;
2081ab64890Smrg	} else {
2091ab64890Smrg            from = (XPointer) look;
2101ab64890Smrg            to =   (XPointer) buffer;
2111ab64890Smrg            from_len = BUF_SIZE - to_len;
2121ab64890Smrg            to_len = nbytes;
2131ab64890Smrg            args[0] = (XPointer) charset;
2141ab64890Smrg	    if (_XlcConvert(private->cstomb_conv,
2151ab64890Smrg                            &from, &from_len, &to, &to_len,
2161ab64890Smrg			    args, 1 ) != 0) {
2171ab64890Smrg		count = 0;
2181ab64890Smrg	    } else {
2191ab64890Smrg                count = nbytes - to_len;
2201ab64890Smrg	    }
2211ab64890Smrg	}
2221ab64890Smrg    }
2231ab64890Smrg    /* FIXME:
2241ab64890Smrg     * we should make sure that if the character is a Latin1 character
2251ab64890Smrg     * and it's on the right side, and we're in a non-Latin1 locale
2261ab64890Smrg     * that this is a valid Latin1 character for this locale.
2271ab64890Smrg     */
2281ab64890Smrg    return count;
2291ab64890Smrg}
2301ab64890Smrg
2311ab64890Smrgint
2321ab64890Smrg_XimLookupWCText(
2331ab64890Smrg    Xic			 ic,
2341ab64890Smrg    XKeyEvent*		event,
2351ab64890Smrg    wchar_t*		buffer,
2361ab64890Smrg    int			nbytes,
2371ab64890Smrg    KeySym*		keysym,
2381ab64890Smrg    XComposeStatus*	status)
2391ab64890Smrg{
2401ab64890Smrg    int count;
2411ab64890Smrg    KeySym symbol;
2421ab64890Smrg    Status	dummy;
2431ab64890Smrg    Xim	im = (Xim)ic->core.im;
2441ab64890Smrg    XimCommonPrivateRec* private = &im->private.common;
2451ab64890Smrg    unsigned char look[BUF_SIZE];
2461ab64890Smrg    ucs4_t ucs4;
2471ab64890Smrg
2481ab64890Smrg    /* force a latin-1 lookup for compatibility */
2491ab64890Smrg    count = XLOOKUPSTRING(event, (char *)look, nbytes, &symbol, status);
2501ab64890Smrg    if (keysym != NULL) *keysym = symbol;
2511ab64890Smrg    if ((nbytes == 0) || (symbol == NoSymbol)) return count;
2521ab64890Smrg
2531ab64890Smrg    if (count > 1) {
2541ab64890Smrg	if ((count = im->methods->ctstowcs(ic->core.im,
2551ab64890Smrg				(char*) look, count,
2561ab64890Smrg				buffer, nbytes, &dummy)) < 0) {
2571ab64890Smrg	    count = 0;
2581ab64890Smrg	}
2591ab64890Smrg    } else if ((count == 0) ||
2601ab64890Smrg	       (count == 1 && (symbol > 0x7f && symbol < 0xff00))) {
2611ab64890Smrg
2621ab64890Smrg        XPointer from = (XPointer) &ucs4;
2631ab64890Smrg        XPointer to =   (XPointer) look;
2641ab64890Smrg        int from_len = 1;
2651ab64890Smrg        int to_len = BUF_SIZE;
2661ab64890Smrg        XPointer args[1];
2671ab64890Smrg        XlcCharSet charset;
2681ab64890Smrg        args[0] = (XPointer) &charset;
2691ab64890Smrg        ucs4 = (ucs4_t) KeySymToUcs4(symbol);
2701ab64890Smrg        if (!ucs4)
2711ab64890Smrg            return 0;
2721ab64890Smrg
2731ab64890Smrg	if (_XlcConvert(private->ucstoc_conv,
2741ab64890Smrg                        &from, &from_len, &to, &to_len,
2751ab64890Smrg                        args, 1 ) != 0) {
2761ab64890Smrg	    count = 0;
2771ab64890Smrg	} else {
2781ab64890Smrg            from = (XPointer) look;
2791ab64890Smrg            to =   (XPointer) buffer;
2801ab64890Smrg            from_len = BUF_SIZE - to_len;
2811ab64890Smrg            to_len = nbytes;
2821ab64890Smrg            args[0] = (XPointer) charset;
2831ab64890Smrg
2841ab64890Smrg	    if (_XlcConvert(private->cstowc_conv,
2851ab64890Smrg                            &from, &from_len, &to, &to_len,
2861ab64890Smrg			    args, 1 ) != 0) {
2871ab64890Smrg		count = 0;
2881ab64890Smrg	    } else {
2891ab64890Smrg                count = nbytes - to_len;
2901ab64890Smrg	    }
2911ab64890Smrg	}
2921ab64890Smrg    } else
2931ab64890Smrg	/* FIXME:
2941ab64890Smrg	 * we should make sure that if the character is a Latin1 character
2951ab64890Smrg	 * and it's on the right side, and we're in a non-Latin1 locale
2961ab64890Smrg	 * that this is a valid Latin1 character for this locale.
2971ab64890Smrg	 */
2981ab64890Smrg	buffer[0] = look[0];
2991ab64890Smrg
3001ab64890Smrg    return count;
3011ab64890Smrg}
3021ab64890Smrg
3031ab64890Smrgint
3041ab64890Smrg_XimLookupUTF8Text(
3051ab64890Smrg    Xic			 ic,
3061ab64890Smrg    XKeyEvent*		event,
3071ab64890Smrg    char*		buffer,
3081ab64890Smrg    int			nbytes,
3091ab64890Smrg    KeySym*		keysym,
3101ab64890Smrg    XComposeStatus*	status)
3111ab64890Smrg{
3121ab64890Smrg    int count;
3131ab64890Smrg    KeySym symbol;
3141ab64890Smrg    Status	dummy;
3151ab64890Smrg    Xim	im = (Xim)ic->core.im;
3161ab64890Smrg    XimCommonPrivateRec* private = &im->private.common;
3171ab64890Smrg    unsigned char look[BUF_SIZE];
3181ab64890Smrg    ucs4_t ucs4;
3191ab64890Smrg
3201ab64890Smrg    /* force a latin-1 lookup for compatibility */
3211ab64890Smrg    count = XLOOKUPSTRING(event, (char *)buffer, nbytes, &symbol, status);
3221ab64890Smrg    if (keysym != NULL) *keysym = symbol;
3231ab64890Smrg    if ((nbytes == 0) || (symbol == NoSymbol)) return count;
3241ab64890Smrg
3251ab64890Smrg    if (count > 1) {
3261ab64890Smrg	memcpy(look, (char *)buffer,count);
3271ab64890Smrg	look[count] = '\0';
3281ab64890Smrg	if ((count = im->methods->ctstoutf8(ic->core.im,
3291ab64890Smrg				(char*) look, count,
3301ab64890Smrg				buffer, nbytes, &dummy)) < 0) {
3311ab64890Smrg	    count = 0;
3321ab64890Smrg	}
3331ab64890Smrg    } else if ((count == 0) ||
3341ab64890Smrg	       (count == 1 && (symbol > 0x7f && symbol < 0xff00))) {
3351ab64890Smrg
3361ab64890Smrg        XPointer from = (XPointer) &ucs4;
3371ab64890Smrg        int from_len = 1;
3381ab64890Smrg        XPointer to = (XPointer) buffer;
3391ab64890Smrg        int to_len = nbytes;
3401ab64890Smrg
3411ab64890Smrg        ucs4 = (ucs4_t) KeySymToUcs4(symbol);
3421ab64890Smrg        if (!ucs4)
3431ab64890Smrg            return 0;
3441ab64890Smrg
3451ab64890Smrg	if (_XlcConvert(private->ucstoutf8_conv,
3461ab64890Smrg                        &from, &from_len, &to, &to_len,
3471ab64890Smrg                        NULL, 0) != 0) {
3481ab64890Smrg	    count = 0;
3491ab64890Smrg	} else {
3501ab64890Smrg            count = nbytes - to_len;
3511ab64890Smrg	}
3521ab64890Smrg    }
3531ab64890Smrg    /* FIXME:
3541ab64890Smrg     * we should make sure that if the character is a Latin1 character
3551ab64890Smrg     * and it's on the right side, and we're in a non-Latin1 locale
3561ab64890Smrg     * that this is a valid Latin1 character for this locale.
3571ab64890Smrg     */
3581ab64890Smrg    return count;
3591ab64890Smrg}
360