Home | History | Annotate | Download | only in src
      1 /**
      2  * Copyright  2012 Intel Corporation
      3  * Copyright  2012 Ran Benita <ran234 (at) gmail.com>
      4  *
      5  * Permission is hereby granted, free of charge, to any person obtaining a
      6  * copy of this software and associated documentation files (the "Software"),
      7  * to deal in the Software without restriction, including without limitation
      8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
      9  * and/or sell copies of the Software, and to permit persons to whom the
     10  * Software is furnished to do so, subject to the following conditions:
     11  *
     12  * The above copyright notice and this permission notice (including the next
     13  * paragraph) shall be included in all copies or substantial portions of the
     14  * Software.
     15  *
     16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
     21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
     22  * DEALINGS IN THE SOFTWARE.
     23  *
     24  * Author: Daniel Stone <daniel (at) fooishbar.org>
     25  */
     26 
     27 #include "keymap.h"
     28 
     29 static void
     30 update_builtin_keymap_fields(struct xkb_keymap *keymap)
     31 {
     32     /* Predefined (AKA real, core, X11) modifiers. The order is important! */
     33     static const char *const builtin_mods[] = {
     34         [0] = "Shift",
     35         [1] = "Lock",
     36         [2] = "Control",
     37         [3] = "Mod1",
     38         [4] = "Mod2",
     39         [5] = "Mod3",
     40         [6] = "Mod4",
     41         [7] = "Mod5"
     42     };
     43 
     44     for (unsigned i = 0; i < ARRAY_SIZE(builtin_mods); i++) {
     45         keymap->mods.mods[i].name = xkb_atom_intern(keymap->ctx,
     46                                                     builtin_mods[i],
     47                                                     strlen(builtin_mods[i]));
     48         keymap->mods.mods[i].type = MOD_REAL;
     49     }
     50     keymap->mods.num_mods = ARRAY_SIZE(builtin_mods);
     51 }
     52 
     53 struct xkb_keymap *
     54 xkb_keymap_new(struct xkb_context *ctx,
     55                enum xkb_keymap_format format,
     56                enum xkb_keymap_compile_flags flags)
     57 {
     58     struct xkb_keymap *keymap;
     59 
     60     keymap = calloc(1, sizeof(*keymap));
     61     if (!keymap)
     62         return NULL;
     63 
     64     keymap->refcnt = 1;
     65     keymap->ctx = xkb_context_ref(ctx);
     66 
     67     keymap->format = format;
     68     keymap->flags = flags;
     69 
     70     update_builtin_keymap_fields(keymap);
     71 
     72     return keymap;
     73 }
     74 
     75 struct xkb_key *
     76 XkbKeyByName(struct xkb_keymap *keymap, xkb_atom_t name, bool use_aliases)
     77 {
     78     struct xkb_key *key;
     79 
     80     xkb_keys_foreach(key, keymap)
     81         if (key->name == name)
     82             return key;
     83 
     84     if (use_aliases) {
     85         xkb_atom_t new_name = XkbResolveKeyAlias(keymap, name);
     86         if (new_name != XKB_ATOM_NONE)
     87             return XkbKeyByName(keymap, new_name, false);
     88     }
     89 
     90     return NULL;
     91 }
     92 
     93 xkb_atom_t
     94 XkbResolveKeyAlias(const struct xkb_keymap *keymap, xkb_atom_t name)
     95 {
     96     for (unsigned i = 0; i < keymap->num_key_aliases; i++)
     97         if (keymap->key_aliases[i].alias == name)
     98             return keymap->key_aliases[i].real;
     99 
    100     return XKB_ATOM_NONE;
    101 }
    102 
    103 void
    104 XkbEscapeMapName(char *name)
    105 {
    106     /*
    107      * All latin-1 alphanumerics, plus parens, slash, minus, underscore and
    108      * wildcards.
    109      */
    110     static const unsigned char legal[] = {
    111         0x00, 0x00, 0x00, 0x00, 0x00, 0xa7, 0xff, 0x83,
    112         0xfe, 0xff, 0xff, 0x87, 0xfe, 0xff, 0xff, 0x07,
    113         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    114         0xff, 0xff, 0x7f, 0xff, 0xff, 0xff, 0x7f, 0xff
    115     };
    116 
    117     if (!name)
    118         return;
    119 
    120     while (*name) {
    121         if (!(legal[*name / 8] & (1 << (*name % 8))))
    122             *name = '_';
    123         name++;
    124     }
    125 }
    126 
    127 xkb_mod_index_t
    128 XkbModNameToIndex(const struct xkb_mod_set *mods, xkb_atom_t name,
    129                   enum mod_type type)
    130 {
    131     xkb_mod_index_t i;
    132     const struct xkb_mod *mod;
    133 
    134     xkb_mods_enumerate(i, mod, mods)
    135         if ((mod->type & type) && name == mod->name)
    136             return i;
    137 
    138     return XKB_MOD_INVALID;
    139 }
    140