#define NEED_EVENTS
#include <stdio.h>
#include "Xlibint.h"
#include "Xlcint.h"
#include "Ximint.h"
#include "XlcPubI.h"
#ifdef XKB
#include "XKBlib.h"
#define XLOOKUPSTRING lookup_string
#else
#define XLOOKUPSTRING XLookupString
#endif
typedef unsigned int ucs4_t;
typedef int (*ucstocsConvProc)(
XPointer,
unsigned char *,
ucs4_t,
int
);
struct SubstRec {
char* encoding_name;
char* charset_name;
};
static struct SubstRec SubstTable[] = {
{"STRING", "ISO8859-1"},
{"TIS620", "TIS620-0"},
{"UTF-8", "ISO10646-1"}
};
#define num_substitute (sizeof SubstTable / sizeof SubstTable[0])
XPointer
_XimGetLocaleCode (
_Xconst char* encoding_name)
{
XPointer cvt = _Utf8GetConvByName(encoding_name);
if (!cvt && encoding_name) {
int i;
for (i = 0; i < num_substitute; i++)
if (!strcmp(encoding_name, SubstTable[i].encoding_name))
return _Utf8GetConvByName(SubstTable[i].charset_name);
}
return cvt;
}
int
_XimGetCharCode (
XPointer ucs_conv,
KeySym keysym,
unsigned char* buf,
int nbytes)
{
int count = 0;
ucstocsConvProc cvt = (ucstocsConvProc) ucs_conv;
ucs4_t ucs4;
if (keysym < 0x80) {
buf[0] = (char) keysym;
count = 1;
} else if (cvt) {
ucs4 = KeySymToUcs4(keysym);
if (ucs4)
count = (*cvt)((XPointer)NULL, buf, ucs4, nbytes);
}
if (count < 0)
count = 0;
if (count>nbytes)
return nbytes;
if (count<nbytes)
buf[count]= '\0';
return count;
}
#ifdef XKB
static int lookup_string(
XKeyEvent* event,
char* buffer,
int nbytes,
KeySym* keysym,
XComposeStatus* status)
{
int ret;
unsigned ctrls = XkbGetXlibControls (event->display);
XkbSetXlibControls (event->display,
XkbLC_ForceLatin1Lookup, XkbLC_ForceLatin1Lookup);
ret = XLookupString(event, (char *)buffer, nbytes, keysym, status);
XkbSetXlibControls (event->display,
XkbLC_ForceLatin1Lookup, ctrls);
return ret;
}
#endif
#define BUF_SIZE (20)
int
_XimLookupMBText(ic, event, buffer, nbytes, keysym, status)
Xic ic;
XKeyEvent* event;
char* buffer;
int nbytes;
KeySym* keysym;
XComposeStatus* status;
{
int count;
KeySym symbol;
Status dummy;
Xim im = (Xim)ic->core.im;
XimCommonPrivateRec* private = &im->private.common;
unsigned char look[BUF_SIZE];
ucs4_t ucs4;
count = XLOOKUPSTRING(event, (char *)buffer, nbytes, &symbol, status);
if (keysym != NULL) *keysym = symbol;
if ((nbytes == 0) || (symbol == NoSymbol)) return count;
if (count > 1) {
memcpy(look, (char *)buffer,count);
look[count] = '\0';
if ((count = im->methods->ctstombs(ic->core.im,
(char*) look, count,
buffer, nbytes, &dummy)) < 0) {
count = 0;
}
} else if ((count == 0) ||
(count == 1 && (symbol > 0x7f && symbol < 0xff00))) {
XPointer from = (XPointer) &ucs4;
XPointer to = (XPointer) look;
int from_len = 1;
int to_len = BUF_SIZE;
XPointer args[1];
XlcCharSet charset;
args[0] = (XPointer) &charset;
ucs4 = (ucs4_t) KeySymToUcs4(symbol);
if (!ucs4)
return 0;
if (_XlcConvert(private->ucstoc_conv,
&from, &from_len, &to, &to_len,
args, 1 ) != 0) {
count = 0;
} else {
from = (XPointer) look;
to = (XPointer) buffer;
from_len = BUF_SIZE - to_len;
to_len = nbytes;
args[0] = (XPointer) charset;
if (_XlcConvert(private->cstomb_conv,
&from, &from_len, &to, &to_len,
args, 1 ) != 0) {
count = 0;
} else {
count = nbytes - to_len;
}
}
}
return count;
}
int
_XimLookupWCText(ic, event, buffer, nbytes, keysym, status)
Xic ic;
XKeyEvent* event;
wchar_t* buffer;
int nbytes;
KeySym* keysym;
XComposeStatus* status;
{
int count;
KeySym symbol;
Status dummy;
Xim im = (Xim)ic->core.im;
XimCommonPrivateRec* private = &im->private.common;
unsigned char look[BUF_SIZE];
ucs4_t ucs4;
count = XLOOKUPSTRING(event, (char *)look, nbytes, &symbol, status);
if (keysym != NULL) *keysym = symbol;
if ((nbytes == 0) || (symbol == NoSymbol)) return count;
if (count > 1) {
if ((count = im->methods->ctstowcs(ic->core.im,
(char*) look, count,
buffer, nbytes, &dummy)) < 0) {
count = 0;
}
} else if ((count == 0) ||
(count == 1 && (symbol > 0x7f && symbol < 0xff00))) {
XPointer from = (XPointer) &ucs4;
XPointer to = (XPointer) look;
int from_len = 1;
int to_len = BUF_SIZE;
XPointer args[1];
XlcCharSet charset;
args[0] = (XPointer) &charset;
ucs4 = (ucs4_t) KeySymToUcs4(symbol);
if (!ucs4)
return 0;
if (_XlcConvert(private->ucstoc_conv,
&from, &from_len, &to, &to_len,
args, 1 ) != 0) {
count = 0;
} else {
from = (XPointer) look;
to = (XPointer) buffer;
from_len = BUF_SIZE - to_len;
to_len = nbytes;
args[0] = (XPointer) charset;
if (_XlcConvert(private->cstowc_conv,
&from, &from_len, &to, &to_len,
args, 1 ) != 0) {
count = 0;
} else {
count = nbytes - to_len;
}
}
} else
buffer[0] = look[0];
return count;
}
int
_XimLookupUTF8Text(ic, event, buffer, nbytes, keysym, status)
Xic ic;
XKeyEvent* event;
char* buffer;
int nbytes;
KeySym* keysym;
XComposeStatus* status;
{
int count;
KeySym symbol;
Status dummy;
Xim im = (Xim)ic->core.im;
XimCommonPrivateRec* private = &im->private.common;
unsigned char look[BUF_SIZE];
ucs4_t ucs4;
count = XLOOKUPSTRING(event, (char *)buffer, nbytes, &symbol, status);
if (keysym != NULL) *keysym = symbol;
if ((nbytes == 0) || (symbol == NoSymbol)) return count;
if (count > 1) {
memcpy(look, (char *)buffer,count);
look[count] = '\0';
if ((count = im->methods->ctstoutf8(ic->core.im,
(char*) look, count,
buffer, nbytes, &dummy)) < 0) {
count = 0;
}
} else if ((count == 0) ||
(count == 1 && (symbol > 0x7f && symbol < 0xff00))) {
XPointer from = (XPointer) &ucs4;
int from_len = 1;
XPointer to = (XPointer) buffer;
int to_len = nbytes;
ucs4 = (ucs4_t) KeySymToUcs4(symbol);
if (!ucs4)
return 0;
if (_XlcConvert(private->ucstoutf8_conv,
&from, &from_len, &to, &to_len,
NULL, 0) != 0) {
count = 0;
} else {
count = nbytes - to_len;
}
}
return count;
}