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