XKBCvt.c 9.35 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
/*

Copyright 1988, 1989, 1998  The Open Group

Permission to use, copy, modify, distribute, and sell this software and its
documentation for any purpose is hereby granted without fee, provided that
the above copyright notice appear in all copies and that both that
copyright notice and this permission notice appear in supporting
documentation.

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

Except as contained in this notice, the name of The Open Group shall
not be used in advertising or otherwise to promote the sale, use or
other dealings in this Software without prior written authorization
from The Open Group.

*/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>

36 37
#include <nx-X11/X.h>
#include <nx-X11/Xlib.h>
38 39 40 41
#include "Xlibint.h"
#include "Xlcint.h"
#include "XlcPubI.h"
#include "Ximint.h"
42 43
#include <nx-X11/Xutil.h>
#include <nx-X11/Xmd.h>
44 45
#define XK_LATIN1
#define XK_PUBLISHING
46 47
#include <nx-X11/keysym.h>
#include <nx-X11/extensions/XKBproto.h>
48
#include "XKBlibint.h"
49
#include <nx-X11/Xlocale.h>
50
#include <ctype.h>
51
#include <nx-X11/Xos.h>
52

53
static int
54 55 56 57 58
_XkbHandleSpecialSym(KeySym keysym, char *buffer, int nbytes, int *extra_rtrn)
{

    /* try to convert to Latin-1, handling ctrl */
    if (!(((keysym >= XK_BackSpace) && (keysym <= XK_Clear)) ||
59 60 61 62 63 64 65 66 67 68 69
          (keysym == XK_Return) || (keysym == XK_Escape) ||
          (keysym == XK_KP_Space) || (keysym == XK_KP_Tab) ||
          (keysym == XK_KP_Enter) ||
          ((keysym >= XK_KP_Multiply) && (keysym <= XK_KP_9)) ||
          (keysym == XK_KP_Equal) || (keysym == XK_Delete)))
        return 0;

    if (nbytes < 1) {
        if (extra_rtrn)
            *extra_rtrn = 1;
        return 0;
70 71 72
    }
    /* if X keysym, convert to ascii by grabbing low 7 bits */
    if (keysym == XK_KP_Space)
73
        buffer[0] = XK_space & 0x7F;            /* patch encoding botch */
74
    else if (keysym == XK_hyphen)
75 76 77
        buffer[0] = (char) (XK_minus & 0xFF);   /* map to equiv character */
    else
        buffer[0] = (char) (keysym & 0x7F);
78 79 80 81
    return 1;
}

/*ARGSUSED*/
82
static int
83 84 85 86 87
_XkbKSToKnownSet(XPointer priv,
                 KeySym keysym,
                 char *buffer,
                 int nbytes,
                 int *extra_rtrn)
88
{
89
    char tbuf[8], *buf;
90 91

    if (extra_rtrn)
92
        *extra_rtrn = 0;
93 94

    /* convert "dead" diacriticals for dumb applications */
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
    if ((keysym & 0xffffff00) == 0xfe00) {
        switch (keysym) {
        case XK_dead_grave:            keysym = XK_grave; break;
        case XK_dead_acute:            keysym = XK_acute; break;
        case XK_dead_circumflex:       keysym = XK_asciicircum; break;
        case XK_dead_tilde:            keysym = XK_asciitilde; break;
        case XK_dead_macron:           keysym = XK_macron; break;
        case XK_dead_breve:            keysym = XK_breve; break;
        case XK_dead_abovedot:         keysym = XK_abovedot; break;
        case XK_dead_diaeresis:        keysym = XK_diaeresis; break;
        case XK_dead_abovering:        keysym = XK_degree; break;
        case XK_dead_doubleacute:      keysym = XK_doubleacute; break;
        case XK_dead_caron:            keysym = XK_caron; break;
        case XK_dead_cedilla:          keysym = XK_cedilla; break;
        case XK_dead_ogonek:           keysym = XK_ogonek; break;
        case XK_dead_iota:             keysym = XK_Greek_iota; break;
111
#ifdef XK_KATAKANA
112 113
        case XK_dead_voiced_sound:     keysym = XK_voicedsound; break;
        case XK_dead_semivoiced_sound: keysym = XK_semivoicedsound; break;
114
#endif
115
        }
116 117
    }

118 119 120 121
    if (nbytes < 1)
        buf = tbuf;
    else
        buf = buffer;
122

123 124
    if ((keysym & 0xffffff00) == 0xff00) {
        return _XkbHandleSpecialSym(keysym, buf, nbytes, extra_rtrn);
125
    }
126
    return _XimGetCharCode(priv, keysym, (unsigned char *) buf, nbytes);
127 128 129
}

typedef struct _XkbToKS {
130 131
    unsigned prefix;
    char *map;
132 133 134 135
} XkbToKS;

/*ARGSUSED*/
static KeySym
136
_XkbKnownSetToKS(XPointer priv, char *buffer, int nbytes, Status *status)
137
{
138 139 140 141 142 143 144 145 146 147 148 149 150 151
    if (nbytes != 1)
        return NoSymbol;
    if (((buffer[0] & 0x80) == 0) && (buffer[0] >= 32))
        return buffer[0];
    else if ((buffer[0] & 0x7f) >= 32) {
        XkbToKS *map = (XkbToKS *) priv;

        if (map) {
            if (map->map)
                return map->prefix | map->map[buffer[0] & 0x7f];
            else
                return map->prefix | buffer[0];
        }
        return buffer[0];
152 153 154 155 156 157 158
    }
    return NoSymbol;
}

static KeySym
__XkbDefaultToUpper(KeySym sym)
{
159
    KeySym lower, upper;
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175

    XConvertCase(sym, &lower, &upper);
    return upper;
}

#ifdef XKB_EXTEND_LOOKUP_STRING
static int
Strcmp(char *str1, char *str2)
{
    char str[256];
    char c, *s;

    /*
     * unchecked strings from the environment can end up here, so check
     * the length before copying.
     */
176 177
    if (strlen(str1) >= sizeof(str))    /* almost certain it's a mismatch */
        return 1;
178

179 180 181 182
    for (s = str; (c = *str1++);) {
        if (isupper(c))
            c = tolower(c);
        *s++ = c;
183 184 185 186 187 188
    }
    *s = '\0';
    return (strcmp(str, str2));
}
#endif

189
int
190
_XkbGetConverters(const char *encoding_name, XkbConverters * cvt_rtrn)
191
{
192 193
    if (!cvt_rtrn)
        return 0;
194 195 196 197 198 199 200 201 202 203 204

    cvt_rtrn->KSToMB = _XkbKSToKnownSet;
    cvt_rtrn->KSToMBPriv = _XimGetLocaleCode(encoding_name);
    cvt_rtrn->MBToKS = _XkbKnownSetToKS;
    cvt_rtrn->MBToKSPriv = NULL;
    cvt_rtrn->KSToUpper = __XkbDefaultToUpper;
    return 1;
}

/***====================================================================***/

205
/*
206 207 208 209 210 211
 * The function _XkbGetCharset seems to be missnamed as what it seems to
 * be used for is to determine the encoding-name for the locale. ???
 */

#ifdef XKB_EXTEND_LOOKUP_STRING

212
/*
213 214 215 216 217 218
 * XKB_EXTEND_LOOKUP_STRING is not used by the SI. It is used by various
 * X Consortium/X Project Team members, so we leave it in the source as
 * an simplify integration by these companies.
 */

#define	CHARSET_FILE	"/usr/lib/X11/input/charsets"
219 220 221 222 223 224 225 226
static char *_XkbKnownLanguages =
    "c=ascii:da,de,en,es,fr,is,it,nl,no,pt,sv=iso8859-1:hu,pl,cs=iso8859-2:"
    "eo=iso8859-3:sp=iso8859-5:ar,ara=iso8859-6:el=iso8859-7:he=iso8859-8:"
    "tr=iso8859-9:lt,lv=iso8859-13:et,fi=iso8859-15:ru=koi8-r:uk=koi8-u:"
    "th,th_TH,th_TH.iso8859-11=iso8859-11:th_TH.TIS620=tis620:hy=armscii-8:"
    "vi=tcvn-5712:ka=georgian-academy:be,bg=microsoft-cp1251";

char *
227
_XkbGetCharset(void)
228 229 230 231 232 233 234
{
    /*
     * PAGE USAGE TUNING: explicitly initialize to move these to data
     * instead of bss
     */
    static char buf[100] = { 0 };
    char lang[256];
235 236
    char *start, *tmp, *end, *next, *set;
    char *country, *charset;
237 238
    char *locale;

239 240 241 242
    tmp = getenv("_XKB_CHARSET");
    if (tmp)
        return tmp;
    locale = setlocale(LC_CTYPE, NULL);
243

244 245
    if (locale == NULL)
        return NULL;
246 247

    if (strlen(locale) >= sizeof(lang))
248
        return NULL;
249 250

    for (tmp = lang; *tmp = *locale++; tmp++) {
251 252
        if (isupper(*tmp))
            *tmp = tolower(*tmp);
253
    }
254 255 256 257 258 259 260 261 262 263 264
    country = strchr(lang, '_');
    if (country) {
        *country++ = '\0';
        charset = strchr(country, '.');
        if (charset)
            *charset++ = '\0';
        if (charset) {
            strncpy(buf, charset, 99);
            buf[99] = '\0';
            return buf;
        }
265
    }
266
    else {
267
        charset = NULL;
268 269
    }

270 271 272 273 274 275 276 277 278
    if ((tmp = getenv("_XKB_LOCALE_CHARSETS")) != NULL) {
        start = _XkbAlloc(strlen(tmp) + 1);
        strcpy(start, tmp);
        tmp = start;
    }
    else {
        struct stat sbuf;
        FILE *file;
        char *cf = CHARSET_FILE;
279 280 281 282

#ifndef S_ISREG
# define S_ISREG(mode)   (((mode) & S_IFMT) == S_IFREG)
#endif
283

284 285 286 287 288 289 290 291 292
        if ((stat(cf, &sbuf) == 0) && S_ISREG(sbuf.st_mode) &&
            (file = fopen(cf, "r"))) {
            tmp = _XkbAlloc(sbuf.st_size + 1);
            if (tmp != NULL) {
                sbuf.st_size = (long) fread(tmp, 1, sbuf.st_size, file);
                tmp[sbuf.st_size] = '\0';
            }
            fclose(file);
        }
293 294
    }

295 296 297 298 299
    if (tmp == NULL) {
        tmp = _XkbAlloc(strlen(_XkbKnownLanguages) + 1);
        if (!tmp)
            return NULL;
        strcpy(tmp, _XkbKnownLanguages);
300 301 302
    }
    start = tmp;
    do {
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
        if ((set = strchr(tmp, '=')) == NULL)
            break;
        *set++ = '\0';
        if ((next = strchr(set, ':')) != NULL)
            *next++ = '\0';
        while (tmp && *tmp) {
            if ((end = strchr(tmp, ',')) != NULL)
                *end++ = '\0';
            if (Strcmp(tmp, lang) == 0) {
                strncpy(buf, set, 100);
                buf[99] = '\0';
                Xfree(start);
                return buf;
            }
            tmp = end;
        }
        tmp = next;
    } while (tmp && *tmp);
321 322 323 324
    Xfree(start);
    return NULL;
}
#else
325
char *
326
_XkbGetCharset(void)
327 328 329 330
{
    char *tmp;
    XLCd lcd;

331 332 333
    tmp = getenv("_XKB_CHARSET");
    if (tmp)
        return tmp;
334 335

    lcd = _XlcCurrentLC();
336 337
    if (lcd)
        return XLC_PUBLIC(lcd, encoding_name);
338 339 340 341

    return NULL;
}
#endif