Edit

kc3-lang/libxkbcommon/src/xkbcomp/xkbcomp.c

Branch :

  • Browse

    Commit

  • Author : Ran Benita
    Date : 2012-05-13 09:49:08
    Hash : b73bd676
    Message : rules: only export a single function Really all we need from this file is a way to get xkb_component_names from an xkb_rule_names, which is now the only thing being exposed. This should allow for some much needed refactoring of this code. Since this is only used by xkbcomp.c and uses xkbcomp functions, also move rules.{c,h} under the xkbcomp dir. Signed-off-by: Ran Benita <ran234@gmail.com>

  • src/xkbcomp/xkbcomp.c
  • /*
    Copyright 2009  Dan Nicholson
    
    Permission is hereby granted, free of charge, to any person obtaining a
    copy of this software and associated documentation files (the "Software"),
    to deal in the Software without restriction, including without limitation
    the rights to use, copy, modify, merge, publish, distribute, sublicense,
    and/or sell copies of the Software, and to permit persons to whom the
    Software is furnished to do so, subject to the following conditions:
    
    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
    AUTHORS 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 names of the authors or their
    institutions 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 authors.
    */
    
    #include "xkbcomp-priv.h"
    #include "rules.h"
    #include "parseutils.h"
    
    /* Global warning level */
    unsigned int warningLevel = 0;
    
    #define ISEMPTY(str) (!(str) || (strlen(str) == 0))
    
    static XkbFile *
    XkbKeymapFileFromComponents(struct xkb_context *ctx,
                                const struct xkb_component_names *ktcsg)
    {
        XkbFile *keycodes, *types, *compat, *symbols;
        IncludeStmt *inc;
    
        inc = IncludeCreate(ktcsg->keycodes, MergeDefault);
        keycodes = CreateXKBFile(ctx, XkmKeyNamesIndex, NULL,
                                 (ParseCommon *)inc, 0);
    
        inc = IncludeCreate(ktcsg->types, MergeDefault);
        types = CreateXKBFile(ctx, XkmTypesIndex, NULL,
                              (ParseCommon *)inc, 0);
        AppendStmt(&keycodes->common, &types->common);
    
        inc = IncludeCreate(ktcsg->compat, MergeDefault);
        compat = CreateXKBFile(ctx, XkmCompatMapIndex, NULL,
                               (ParseCommon *)inc, 0);
        AppendStmt(&keycodes->common, &compat->common);
    
        inc = IncludeCreate(ktcsg->symbols, MergeDefault);
        symbols = CreateXKBFile(ctx, XkmSymbolsIndex, NULL,
                                (ParseCommon *)inc, 0);
        AppendStmt(&keycodes->common, &symbols->common);
    
        return CreateXKBFile(ctx, XkmKeymapFile,
                             ktcsg->keymap ? ktcsg->keymap : strdup(""),
                             &keycodes->common, 0);
    }
    
    _X_EXPORT struct xkb_keymap *
    xkb_map_new_from_names(struct xkb_context *ctx,
                           const struct xkb_rule_names *rmlvo,
                           enum xkb_map_compile_flags flags)
    {
        struct xkb_component_names *kkctgs;
        struct xkb_keymap *keymap;
    
        if (!rmlvo || ISEMPTY(rmlvo->rules) || ISEMPTY(rmlvo->layout)) {
            ERROR("rules and layout required to generate XKB keymap\n");
            return NULL;
        }
    
        kkctgs = xkb_components_from_rules(ctx, rmlvo);
        if (!kkctgs) {
            ERROR("failed to generate XKB components from rules \"%s\"\n",
                  rmlvo->rules);
            return NULL;
        }
    
        keymap = xkb_map_new_from_kccgst(ctx, kkctgs, 0);
    
        free(kkctgs->keymap);
        free(kkctgs->keycodes);
        free(kkctgs->types);
        free(kkctgs->compat);
        free(kkctgs->symbols);
        free(kkctgs);
    
        return keymap;
    }
    
    static XkbFile *
    XkbChooseMap(XkbFile *file, const char *name)
    {
        XkbFile *map = file;
    
        /* map specified? */
        if (name) {
            while (map) {
                if (map->name && strcmp(map->name, name) == 0)
                    break;
                map = (XkbFile *) map->common.next;
            }
    
            if (!map)
                ERROR("no map named \"%s\" in input file\n", name);
        }
        else if (file->common.next) {
            /* look for map with XkbLC_Default flag. */
            for (; map; map = (XkbFile *) map->common.next) {
                if (map->flags & XkbLC_Default)
                    break;
            }
    
            if (!map) {
                map = file;
                WARN("no map specified, but components have several\n");
                WARN("using the first defined map, \"%s\"\n",
                     map->name ? map->name : "");
            }
        }
    
        return map;
    }
    
    static struct xkb_keymap *
    compile_keymap(struct xkb_context *ctx, XkbFile *file)
    {
        XkbFile *mapToUse;
        struct xkb_keymap *keymap = NULL;
    
        /* Find map to use */
        mapToUse = XkbChooseMap(file, NULL);
        if (!mapToUse)
            goto err;
    
        if (mapToUse->type != XkmKeymapFile) {
            ERROR("file type %d not handled\n", mapToUse->type);
            goto err;
        }
    
        keymap = CompileKeymap(ctx, mapToUse);
        if (!keymap)
            goto err;
    
    err:
        FreeXKBFile(file);
        return keymap;
    }
    
    _X_EXPORT struct xkb_keymap *
    xkb_map_new_from_kccgst(struct xkb_context *ctx,
                            const struct xkb_component_names *kccgst,
                            enum xkb_map_compile_flags flags)
    {
        XkbFile *file;
    
        if (!kccgst) {
            ERROR("no components specified\n");
            return NULL;
        }
    
        if (ISEMPTY(kccgst->keycodes)) {
            ERROR("keycodes required to generate XKB keymap\n");
            return NULL;
        }
    
        if (ISEMPTY(kccgst->compat)) {
            ERROR("compat map required to generate XKB keymap\n");
            return NULL;
        }
    
        if (ISEMPTY(kccgst->types)) {
            ERROR("types required to generate XKB keymap\n");
            return NULL;
        }
    
        if (ISEMPTY(kccgst->symbols)) {
            ERROR("symbols required to generate XKB keymap\n");
            return NULL;
        }
    
        if (!(file = XkbKeymapFileFromComponents(ctx, kccgst))) {
            ERROR("failed to generate parsed XKB file from components\n");
            return NULL;
        }
    
        return compile_keymap(ctx, file);
    }
    
    _X_EXPORT struct xkb_keymap *
    xkb_map_new_from_string(struct xkb_context *ctx,
                            const char *string,
                            enum xkb_keymap_format format,
                            enum xkb_map_compile_flags flags)
    {
        XkbFile *file;
    
        if (format != XKB_KEYMAP_FORMAT_TEXT_V1) {
            ERROR("unsupported keymap format %d\n", format);
            return NULL;
        }
    
        if (!string) {
            ERROR("no string specified to generate XKB keymap\n");
            return NULL;
        }
    
        if (!XKBParseString(ctx, string, "input", &file)) {
            ERROR("failed to parse input xkb file\n");
            return NULL;
        }
    
        return compile_keymap(ctx, file);
    }
    
    _X_EXPORT struct xkb_keymap *
    xkb_map_new_from_file(struct xkb_context *ctx,
                          FILE *file,
                          enum xkb_keymap_format format,
                          enum xkb_map_compile_flags flags)
    {
        XkbFile *xkb_file;
    
        if (format != XKB_KEYMAP_FORMAT_TEXT_V1) {
            ERROR("unsupported keymap format %d\n", format);
            return NULL;
        }
    
        if (!file) {
            ERROR("no file specified to generate XKB keymap\n");
            return NULL;
        }
    
        if (!XKBParseFile(ctx, file, "(unknown file)", &xkb_file)) {
            ERROR("failed to parse input xkb file\n");
            return NULL;
        }
    
        return compile_keymap(ctx, xkb_file);
    }
    
    _X_EXPORT struct xkb_keymap *
    xkb_map_ref(struct xkb_keymap *keymap)
    {
        keymap->refcnt++;
        return keymap;
    }
    
    _X_EXPORT void
    xkb_map_unref(struct xkb_keymap *keymap)
    {
        if (--keymap->refcnt > 0)
            return;
    
        XkbcFreeKeyboard(keymap);
    }