xkbfmisc.c 15.3 KB
Newer Older
1 2 3 4 5 6 7 8
/************************************************************
 Copyright (c) 1995 by Silicon Graphics Computer Systems, Inc.

 Permission to use, copy, modify, and distribute this
 software and its documentation for any purpose and without
 fee is hereby granted, provided that the above copyright
 notice appear in all copies and that both that copyright
 notice and this permission notice appear in supporting
9 10
 documentation, and that the name of Silicon Graphics not be
 used in advertising or publicity pertaining to distribution
11
 of the software without specific prior written permission.
12
 Silicon Graphics makes no representation about the suitability
13 14
 of this software for any purpose. It is provided "as is"
 without any express or implied warranty.
15 16 17

 SILICON GRAPHICS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
 SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
18
 AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
19 20 21
 GRAPHICS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
 DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
22 23 24 25 26
 OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION  WITH
 THE USE OR PERFORMANCE OF THIS SOFTWARE.

 ********************************************************/

27 28
#ifdef HAVE_DIX_CONFIG_H
#include <dix-config.h>
29
#endif
30

31 32 33 34 35 36
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>

#include <X11/Xos.h>
#include <X11/Xfuncs.h>
37
#include <X11/extensions/XKMformat.h>
38

39
#include <X11/X.h>
40
#include <X11/keysym.h>
41
#include <X11/Xproto.h>
42 43 44
#include "misc.h"
#include "inputstr.h"
#include "dix.h"
45
#include "xkbstr.h"
46
#define XKBSRV_NEED_FILE_FUNCS	1
47
#include <xkbsrv.h>
48
#include "xkbgeom.h"
49
#include "xkb.h"
50

51
unsigned
52 53
_XkbKSCheckCase(KeySym ks)
{
54
    unsigned set, rtrn;
55

56 57
    set = (ks & (~0xff)) >> 8;
    rtrn = 0;
58
    switch (set) {
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
    case 0:                    /* latin 1 */
        if (((ks >= XK_A) && (ks <= XK_Z)) ||
            ((ks >= XK_Agrave) && (ks <= XK_THORN) && (ks != XK_multiply))) {
            rtrn |= _XkbKSUpper;
        }
        if (((ks >= XK_a) && (ks <= XK_z)) ||
            ((ks >= XK_ssharp) && (ks <= XK_ydiaeresis) &&
             (ks != XK_division))) {
            rtrn |= _XkbKSLower;
        }
        break;
    case 1:                    /* latin 2 */
        if (((ks >= XK_Aogonek) && (ks <= XK_Zabovedot) && (ks != XK_breve)) ||
            ((ks >= XK_Racute) && (ks <= XK_Tcedilla))) {
            rtrn |= _XkbKSUpper;
        }
        if (((ks >= XK_aogonek) && (ks <= XK_zabovedot) && (ks != XK_ogonek) &&
             (ks != XK_caron) && (ks != XK_doubleacute)) || ((ks >= XK_racute)
                                                             && (ks <=
                                                                 XK_tcedilla)))
        {
            rtrn |= _XkbKSLower;
        }
        break;
    case 2:                    /* latin 3 */
        if (((ks >= XK_Hstroke) && (ks <= XK_Jcircumflex)) ||
            ((ks >= XK_Cabovedot) && (ks <= XK_Scircumflex))) {
            rtrn |= _XkbKSUpper;
        }
        if (((ks >= XK_hstroke) && (ks <= XK_jcircumflex)) ||
            ((ks >= XK_cabovedot) && (ks <= XK_scircumflex))) {
            rtrn |= _XkbKSLower;
        }
        break;
    case 3:                    /* latin 4 */
        if (((ks >= XK_Rcedilla) && (ks <= XK_Tslash)) ||
            (ks == XK_ENG) || ((ks >= XK_Amacron) && (ks <= XK_Umacron))) {
            rtrn |= _XkbKSUpper;
        }
        if ((ks == XK_kra) ||
            ((ks >= XK_rcedilla) && (ks <= XK_tslash)) ||
            (ks == XK_eng) || ((ks >= XK_amacron) && (ks <= XK_umacron))) {
            rtrn |= _XkbKSLower;
        }
        break;
    case 18:                   /* latin 8 */
        if ((ks == XK_Wcircumflex) ||
            (ks == XK_Ycircumflex) ||
            (ks == XK_Babovedot) ||
            (ks == XK_Dabovedot) ||
            (ks == XK_Fabovedot) ||
            (ks == XK_Mabovedot) ||
            (ks == XK_Pabovedot) ||
            (ks == XK_Sabovedot) ||
            (ks == XK_Tabovedot) ||
            (ks == XK_Wgrave) ||
            (ks == XK_Wacute) || (ks == XK_Wdiaeresis) || (ks == XK_Ygrave)) {
            rtrn |= _XkbKSUpper;
        }
        if ((ks == XK_wcircumflex) ||
            (ks == XK_ycircumflex) ||
            (ks == XK_babovedot) ||
            (ks == XK_dabovedot) ||
            (ks == XK_fabovedot) ||
            (ks == XK_mabovedot) ||
            (ks == XK_pabovedot) ||
            (ks == XK_sabovedot) ||
            (ks == XK_tabovedot) ||
            (ks == XK_wgrave) ||
            (ks == XK_wacute) || (ks == XK_wdiaeresis) || (ks == XK_ygrave)) {
            rtrn |= _XkbKSLower;
        }
        break;
    case 19:                   /* latin 9 */
        if ((ks == XK_OE) || (ks == XK_Ydiaeresis)) {
            rtrn |= _XkbKSUpper;
        }
        if (ks == XK_oe) {
            rtrn |= _XkbKSLower;
        }
        break;
140 141 142 143 144 145
    }
    return rtrn;
}

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

146
static Bool
147
XkbWriteSectionFromName(FILE * file, const char *sectionName, const char *name)
148
{
149
    fprintf(file, "    xkb_%-20s { include \"%s\" };\n", sectionName, name);
150
    return TRUE;
151 152
}

153 154 155 156 157
#define	NEED_DESC(n) ((!n)||((n)[0]=='+')||((n)[0]=='|')||(strchr((n),'%')))
#define	COMPLETE(n)  ((n)&&(!NEED_DESC(n)))

/* ARGSUSED */
static void
158 159 160
_AddIncl(FILE * file,
         XkbDescPtr xkb,
         Bool topLevel, Bool showImplicit, int index, void *priv)
161
{
162 163
    if ((priv) && (strcmp((char *) priv, "%") != 0))
        fprintf(file, "    include \"%s\"\n", (char *) priv);
164 165 166
    return;
}

167
Bool
168 169 170
XkbWriteXKBKeymapForNames(FILE * file,
                          XkbComponentNamesPtr names,
                          XkbDescPtr xkb, unsigned want, unsigned need)
171
{
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
    const char *tmp;
    unsigned complete;
    XkbNamesPtr old_names;
    int multi_section;
    unsigned wantNames, wantConfig, wantDflts;

    complete = 0;
    if (COMPLETE(names->keycodes))
        complete |= XkmKeyNamesMask;
    if (COMPLETE(names->types))
        complete |= XkmTypesMask;
    if (COMPLETE(names->compat))
        complete |= XkmCompatMapMask;
    if (COMPLETE(names->symbols))
        complete |= XkmSymbolsMask;
    if (COMPLETE(names->geometry))
        complete |= XkmGeometryMask;
    want |= (complete | need);
    if (want & XkmSymbolsMask)
        want |= XkmKeyNamesMask | XkmTypesMask;

    if (want == 0)
        return FALSE;
195

196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
    if (xkb) {
        old_names = xkb->names;

        xkb->defined = 0;
        /* Wow would it ever be neat if we didn't need this noise. */
        if (xkb->names && xkb->names->keys)
            xkb->defined |= XkmKeyNamesMask;
        if (xkb->map && xkb->map->types)
            xkb->defined |= XkmTypesMask;
        if (xkb->compat)
            xkb->defined |= XkmCompatMapMask;
        if (xkb->map && xkb->map->num_syms)
            xkb->defined |= XkmSymbolsMask;
        if (xkb->indicators)
            xkb->defined |= XkmIndicatorsMask;
        if (xkb->geom)
            xkb->defined |= XkmGeometryMask;
    }
    else {
215
        old_names = NULL;
216
    }
217

218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
    wantConfig = want & (~complete);
    if (xkb != NULL) {
        if (wantConfig & XkmTypesMask) {
            if ((!xkb->map) || (xkb->map->num_types < XkbNumRequiredTypes))
                wantConfig &= ~XkmTypesMask;
        }
        if (wantConfig & XkmCompatMapMask) {
            if ((!xkb->compat) || (xkb->compat->num_si < 1))
                wantConfig &= ~XkmCompatMapMask;
        }
        if (wantConfig & XkmSymbolsMask) {
            if ((!xkb->map) || (!xkb->map->key_sym_map))
                wantConfig &= ~XkmSymbolsMask;
        }
        if (wantConfig & XkmIndicatorsMask) {
            if (!xkb->indicators)
                wantConfig &= ~XkmIndicatorsMask;
        }
        if (wantConfig & XkmKeyNamesMask) {
            if ((!xkb->names) || (!xkb->names->keys))
                wantConfig &= ~XkmKeyNamesMask;
        }
        if ((wantConfig & XkmGeometryMask) && (!xkb->geom))
            wantConfig &= ~XkmGeometryMask;
242 243
    }
    else {
244
        wantConfig = 0;
245
    }
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
    complete |= wantConfig;

    wantDflts = 0;
    wantNames = want & (~complete);
    if ((xkb != NULL) && (old_names != NULL)) {
        if (wantNames & XkmTypesMask) {
            if (old_names->types != None) {
                tmp = NameForAtom(old_names->types);
                names->types = Xstrdup(tmp);
            }
            else {
                wantDflts |= XkmTypesMask;
            }
            complete |= XkmTypesMask;
        }
        if (wantNames & XkmCompatMapMask) {
            if (old_names->compat != None) {
                tmp = NameForAtom(old_names->compat);
                names->compat = Xstrdup(tmp);
            }
            else
                wantDflts |= XkmCompatMapMask;
            complete |= XkmCompatMapMask;
        }
        if (wantNames & XkmSymbolsMask) {
            if (old_names->symbols == None)
                return FALSE;
            tmp = NameForAtom(old_names->symbols);
            names->symbols = Xstrdup(tmp);
            complete |= XkmSymbolsMask;
        }
        if (wantNames & XkmKeyNamesMask) {
            if (old_names->keycodes != None) {
                tmp = NameForAtom(old_names->keycodes);
                names->keycodes = Xstrdup(tmp);
            }
            else
                wantDflts |= XkmKeyNamesMask;
            complete |= XkmKeyNamesMask;
        }
        if (wantNames & XkmGeometryMask) {
            if (old_names->geometry == None)
                return FALSE;
            tmp = NameForAtom(old_names->geometry);
            names->geometry = Xstrdup(tmp);
            complete |= XkmGeometryMask;
            wantNames &= ~XkmGeometryMask;
        }
294
    }
295 296 297 298
    if (complete & XkmCompatMapMask)
        complete |= XkmIndicatorsMask | XkmVirtualModsMask;
    else if (complete & (XkmSymbolsMask | XkmTypesMask))
        complete |= XkmVirtualModsMask;
299
    if (need & (~complete))
300 301 302 303 304 305 306 307 308
        return FALSE;
    if ((complete & XkmSymbolsMask) &&
        ((XkmKeyNamesMask | XkmTypesMask) & (~complete)))
        return FALSE;

    multi_section = 1;
    if (((complete & XkmKeymapRequired) == XkmKeymapRequired) &&
        ((complete & (~XkmKeymapLegal)) == 0)) {
        fprintf(file, "xkb_keymap \"default\" {\n");
309
    }
310 311 312
    else if (((complete & XkmSemanticsRequired) == XkmSemanticsRequired) &&
             ((complete & (~XkmSemanticsLegal)) == 0)) {
        fprintf(file, "xkb_semantics \"default\" {\n");
313
    }
314 315 316
    else if (((complete & XkmLayoutRequired) == XkmLayoutRequired) &&
             ((complete & (~XkmLayoutLegal)) == 0)) {
        fprintf(file, "xkb_layout \"default\" {\n");
317
    }
318 319
    else if (XkmSingleSection(complete & (~XkmVirtualModsMask))) {
        multi_section = 0;
320 321
    }
    else {
322
        return FALSE;
323 324
    }

325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
    wantNames = complete & (~(wantConfig | wantDflts));
    if (wantConfig & XkmKeyNamesMask)
        XkbWriteXKBKeycodes(file, xkb, FALSE, FALSE, _AddIncl, names->keycodes);
    else if (wantDflts & XkmKeyNamesMask)
        fprintf(stderr, "Default symbols not implemented yet!\n");
    else if (wantNames & XkmKeyNamesMask)
        XkbWriteSectionFromName(file, "keycodes", names->keycodes);

    if (wantConfig & XkmTypesMask)
        XkbWriteXKBKeyTypes(file, xkb, FALSE, FALSE, _AddIncl, names->types);
    else if (wantDflts & XkmTypesMask)
        fprintf(stderr, "Default types not implemented yet!\n");
    else if (wantNames & XkmTypesMask)
        XkbWriteSectionFromName(file, "types", names->types);

    if (wantConfig & XkmCompatMapMask)
        XkbWriteXKBCompatMap(file, xkb, FALSE, FALSE, _AddIncl, names->compat);
    else if (wantDflts & XkmCompatMapMask)
        fprintf(stderr, "Default interps not implemented yet!\n");
    else if (wantNames & XkmCompatMapMask)
        XkbWriteSectionFromName(file, "compatibility", names->compat);

    if (wantConfig & XkmSymbolsMask)
        XkbWriteXKBSymbols(file, xkb, FALSE, FALSE, _AddIncl, names->symbols);
    else if (wantNames & XkmSymbolsMask)
        XkbWriteSectionFromName(file, "symbols", names->symbols);

    if (wantConfig & XkmGeometryMask)
        XkbWriteXKBGeometry(file, xkb, FALSE, FALSE, _AddIncl, names->geometry);
    else if (wantNames & XkmGeometryMask)
        XkbWriteSectionFromName(file, "geometry", names->geometry);
356 357

    if (multi_section)
358
        fprintf(file, "};\n");
359
    return TRUE;
360 361
}

362 363
/***====================================================================***/

364
int
365
XkbFindKeycodeByName(XkbDescPtr xkb, char *name, Bool use_aliases)
366
{
367
    register int i;
368

369 370 371 372 373
    if ((!xkb) || (!xkb->names) || (!xkb->names->keys))
        return 0;
    for (i = xkb->min_key_code; i <= xkb->max_key_code; i++) {
        if (strncmp(xkb->names->keys[i].name, name, XkbKeyNameLength) == 0)
            return i;
374 375
    }
    if (!use_aliases)
376
        return 0;
377
    if (xkb->geom && xkb->geom->key_aliases) {
378 379 380 381 382 383 384
        XkbKeyAliasPtr a;

        a = xkb->geom->key_aliases;
        for (i = 0; i < xkb->geom->num_key_aliases; i++, a++) {
            if (strncmp(name, a->alias, XkbKeyNameLength) == 0)
                return XkbFindKeycodeByName(xkb, a->real, FALSE);
        }
385 386
    }
    if (xkb->names && xkb->names->key_aliases) {
387 388 389 390 391 392 393
        XkbKeyAliasPtr a;

        a = xkb->names->key_aliases;
        for (i = 0; i < xkb->names->num_key_aliases; i++, a++) {
            if (strncmp(name, a->alias, XkbKeyNameLength) == 0)
                return XkbFindKeycodeByName(xkb, a->real, FALSE);
        }
394 395 396 397
    }
    return 0;
}

398
unsigned
399
XkbConvertGetByNameComponents(Bool toXkm, unsigned orig)
400
{
401
    unsigned rtrn;
402

403
    rtrn = 0;
404
    if (toXkm) {
405 406 407 408 409 410 411 412 413 414 415 416
        if (orig & XkbGBN_TypesMask)
            rtrn |= XkmTypesMask;
        if (orig & XkbGBN_CompatMapMask)
            rtrn |= XkmCompatMapMask;
        if (orig & XkbGBN_SymbolsMask)
            rtrn |= XkmSymbolsMask;
        if (orig & XkbGBN_IndicatorMapMask)
            rtrn |= XkmIndicatorsMask;
        if (orig & XkbGBN_KeyNamesMask)
            rtrn |= XkmKeyNamesMask;
        if (orig & XkbGBN_GeometryMask)
            rtrn |= XkmGeometryMask;
417 418
    }
    else {
419 420 421 422 423 424 425 426 427 428 429 430 431 432
        if (orig & XkmTypesMask)
            rtrn |= XkbGBN_TypesMask;
        if (orig & XkmCompatMapMask)
            rtrn |= XkbGBN_CompatMapMask;
        if (orig & XkmSymbolsMask)
            rtrn |= XkbGBN_SymbolsMask;
        if (orig & XkmIndicatorsMask)
            rtrn |= XkbGBN_IndicatorMapMask;
        if (orig & XkmKeyNamesMask)
            rtrn |= XkbGBN_KeyNamesMask;
        if (orig & XkmGeometryMask)
            rtrn |= XkbGBN_GeometryMask;
        if (orig != 0)
            rtrn |= XkbGBN_OtherNamesMask;
433 434 435 436 437 438 439 440
    }
    return rtrn;
}

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

#define	UNMATCHABLE(c)	(((c)=='(')||((c)==')')||((c)=='/'))

441
Bool
442
XkbNameMatchesPattern(char *name, char *ptrn)
443
{
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
    while (ptrn[0] != '\0') {
        if (name[0] == '\0') {
            if (ptrn[0] == '*') {
                ptrn++;
                continue;
            }
            return FALSE;
        }
        if (ptrn[0] == '?') {
            if (UNMATCHABLE(name[0]))
                return FALSE;
        }
        else if (ptrn[0] == '*') {
            if ((!UNMATCHABLE(name[0])) &&
                XkbNameMatchesPattern(name + 1, ptrn))
                return TRUE;
            return XkbNameMatchesPattern(name, ptrn + 1);
        }
        else if (ptrn[0] != name[0])
            return FALSE;
        name++;
        ptrn++;
466 467
    }
    /* if we get here, the pattern is exhausted (-:just like me:-) */
468
    return name[0] == '\0';
469
}