Messages: merge macros with and without message code
[platform/upstream/libxkbcommon.git] / src / xkbcomp / xkbcomp.c
index 9ceaa0c..c3ff76d 100644 (file)
@@ -1,5 +1,7 @@
 /*
- * Copyright 2009  Dan Nicholson
+ * Copyright © 2009 Dan Nicholson
+ * Copyright © 2012 Intel Corporation
+ * Copyright © 2012 Ran Benita <ran234@gmail.com>
  *
  * Permission is hereby granted, free of charge, to any person obtaining a
  * copy of this software and associated documentation files (the "Software"),
  * 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 above copyright notice and this permission notice (including the next
+ * paragraph) 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.
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS 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.
+ * Authors: Dan Nicholson <dbn.lists@gmail.com>
+ *          Ran Benita <ran234@gmail.com>
+ *          Daniel Stone <daniel@fooishbar.org>
  */
 
+#include "config.h"
+
 #include "xkbcomp-priv.h"
 #include "rules.h"
 
-typedef bool (*compile_file_fn)(XkbFile *file,
-                                struct xkb_keymap *keymap,
-                                enum merge_mode merge);
-
-static const compile_file_fn compile_file_fns[LAST_KEYMAP_FILE_TYPE + 1] = {
-    [FILE_TYPE_KEYCODES] = CompileKeycodes,
-    [FILE_TYPE_TYPES] = CompileKeyTypes,
-    [FILE_TYPE_COMPAT] = CompileCompatMap,
-    [FILE_TYPE_SYMBOLS] = CompileSymbols,
-};
-
-static struct xkb_keymap *
-compile_keymap_file(struct xkb_context *ctx, XkbFile *file)
+static bool
+compile_keymap_file(struct xkb_keymap *keymap, XkbFile *file)
 {
-    bool ok;
-    const char *main_name;
-    struct xkb_keymap *keymap;
-    XkbFile *files[LAST_KEYMAP_FILE_TYPE + 1] = { NULL };
-    enum xkb_file_type type;
-
-    keymap = xkb_map_new(ctx);
-    if (!keymap)
-        goto err;
-
-    main_name = file->name ? file->name : "(unnamed)";
-
     if (file->file_type != FILE_TYPE_KEYMAP) {
-        log_err(ctx, "Cannot compile a %s file alone into a keymap\n",
+        log_err(keymap->ctx, XKB_LOG_MESSAGE_NO_ID,
+                "Cannot compile a %s file alone into a keymap\n",
                 xkb_file_type_to_string(file->file_type));
-        goto err;
+        return false;
     }
 
-    /* Collect section files and check for duplicates. */
-    for (file = (XkbFile *) file->defs; file;
-         file = (XkbFile *) file->common.next) {
-        if (file->file_type < FIRST_KEYMAP_FILE_TYPE ||
-            file->file_type > LAST_KEYMAP_FILE_TYPE) {
-            log_err(ctx, "Cannot define %s in a keymap file\n",
-                    xkb_file_type_to_string(file->file_type));
-            continue;
-        }
-
-        if (files[file->file_type]) {
-            log_err(ctx,
-                    "More than one %s section in keymap file; "
-                    "All sections after the first ignored\n",
-                    xkb_file_type_to_string(file->file_type));
-            continue;
-        }
-
-        if (!file->topName) {
-            free(file->topName);
-            file->topName = strdup(main_name);
-        }
-
-        files[file->file_type] = file;
+    if (!CompileKeymap(file, keymap, MERGE_OVERRIDE)) {
+        log_err(keymap->ctx, XKB_LOG_MESSAGE_NO_ID,
+                "Failed to compile keymap\n");
+        return false;
     }
 
-    /*
-     * Check that all required section were provided.
-     * Report everything before failing.
-     */
-    ok = true;
-    for (type = FIRST_KEYMAP_FILE_TYPE;
-         type <= LAST_KEYMAP_FILE_TYPE;
-         type++) {
-        if (files[type] == NULL) {
-            log_err(ctx, "Required section %s missing from keymap\n",
-                    xkb_file_type_to_string(type));
-            ok = false;
-        }
-    }
-    if (!ok)
-        goto err;
-
-    /* Compile sections. */
-    for (type = FIRST_KEYMAP_FILE_TYPE;
-         type <= LAST_KEYMAP_FILE_TYPE;
-         type++) {
-        log_dbg(ctx, "Compiling %s \"%s\"\n",
-                xkb_file_type_to_string(type), files[type]->topName);
-        ok = compile_file_fns[type](files[type], keymap, MERGE_OVERRIDE);
-        if (!ok) {
-            log_err(ctx, "Failed to compile %s\n",
-                    xkb_file_type_to_string(type));
-            goto err;
-        }
-    }
-
-    ok = UpdateModifiersFromCompat(keymap);
-    if (!ok)
-        goto err;
-
-    return keymap;
-
-err:
-    log_err(ctx, "Failed to compile keymap\n");
-    xkb_map_unref(keymap);
-    return NULL;
+    return true;
 }
 
-XKB_EXPORT struct xkb_keymap *
-xkb_map_new_from_names(struct xkb_context *ctx,
-                       const struct xkb_rule_names *rmlvo_in,
-                       enum xkb_map_compile_flags flags)
+static bool
+text_v1_keymap_new_from_names(struct xkb_keymap *keymap,
+                              const struct xkb_rule_names *rmlvo)
 {
     bool ok;
     struct xkb_component_names kccgst;
-    struct xkb_rule_names rmlvo = *rmlvo_in;
     XkbFile *file;
-    struct xkb_keymap *keymap;
 
-    if (isempty(rmlvo.rules))
-        rmlvo.rules = DEFAULT_XKB_RULES;
-    if (isempty(rmlvo.model))
-        rmlvo.model = DEFAULT_XKB_MODEL;
-    if (isempty(rmlvo.layout))
-        rmlvo.layout = DEFAULT_XKB_LAYOUT;
+    log_dbg(keymap->ctx, XKB_LOG_MESSAGE_NO_ID,
+            "Compiling from RMLVO: rules '%s', model '%s', layout '%s', "
+            "variant '%s', options '%s'\n",
+            rmlvo->rules, rmlvo->model, rmlvo->layout, rmlvo->variant,
+            rmlvo->options);
 
-    ok = xkb_components_from_rules(ctx, &rmlvo, &kccgst);
+    ok = xkb_components_from_rules(keymap->ctx, rmlvo, &kccgst);
     if (!ok) {
-        log_err(ctx,
+        log_err(keymap->ctx, XKB_LOG_MESSAGE_NO_ID,
                 "Couldn't look up rules '%s', model '%s', layout '%s', "
                 "variant '%s', options '%s'\n",
-                rmlvo.rules, rmlvo.model, rmlvo.layout, rmlvo.variant,
-                rmlvo.options);
-        return NULL;
+                rmlvo->rules, rmlvo->model, rmlvo->layout, rmlvo->variant,
+                rmlvo->options);
+        return false;
     }
 
-    file = XkbFileFromComponents(ctx, &kccgst);
+    log_dbg(keymap->ctx, XKB_LOG_MESSAGE_NO_ID,
+            "Compiling from KcCGST: keycodes '%s', types '%s', "
+            "compat '%s', symbols '%s'\n",
+            kccgst.keycodes, kccgst.types, kccgst.compat, kccgst.symbols);
+
+    file = XkbFileFromComponents(keymap->ctx, &kccgst);
 
     free(kccgst.keycodes);
     free(kccgst.types);
@@ -164,74 +88,56 @@ xkb_map_new_from_names(struct xkb_context *ctx,
     free(kccgst.symbols);
 
     if (!file) {
-        log_err(ctx,
+        log_err(keymap->ctx, XKB_LOG_MESSAGE_NO_ID,
                 "Failed to generate parsed XKB file from components\n");
-        return NULL;
+        return false;
     }
 
-    keymap = compile_keymap_file(ctx, file);
+    ok = compile_keymap_file(keymap, file);
     FreeXkbFile(file);
-    return keymap;
+    return ok;
 }
 
-XKB_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)
+static bool
+text_v1_keymap_new_from_string(struct xkb_keymap *keymap,
+                               const char *string, size_t len)
 {
     bool ok;
-    XkbFile *file;
-    struct xkb_keymap *keymap;
-
-    if (format != XKB_KEYMAP_FORMAT_TEXT_V1) {
-        log_err(ctx, "Unsupported keymap format %d\n", format);
-        return NULL;
-    }
-
-    if (!string) {
-        log_err(ctx, "No string specified to generate XKB keymap\n");
-        return NULL;
-    }
+    XkbFile *xkb_file;
 
-    ok = XkbParseString(ctx, string, "input", &file);
-    if (!ok) {
-        log_err(ctx, "Failed to parse input xkb file\n");
-        return NULL;
+    xkb_file = XkbParseString(keymap->ctx, string, len, "(input string)", NULL);
+    if (!xkb_file) {
+        log_err(keymap->ctx, XKB_LOG_MESSAGE_NO_ID,
+                "Failed to parse input xkb string\n");
+        return false;
     }
 
-    keymap = compile_keymap_file(ctx, file);
-    FreeXkbFile(file);
-    return keymap;
+    ok = compile_keymap_file(keymap, xkb_file);
+    FreeXkbFile(xkb_file);
+    return ok;
 }
 
-XKB_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)
+static bool
+text_v1_keymap_new_from_file(struct xkb_keymap *keymap, FILE *file)
 {
     bool ok;
     XkbFile *xkb_file;
-    struct xkb_keymap *keymap;
 
-    if (format != XKB_KEYMAP_FORMAT_TEXT_V1) {
-        log_err(ctx, "Unsupported keymap format %d\n", format);
-        return NULL;
+    xkb_file = XkbParseFile(keymap->ctx, file, "(unknown file)", NULL);
+    if (!xkb_file) {
+        log_err(keymap->ctx, XKB_LOG_MESSAGE_NO_ID,
+                "Failed to parse input xkb file\n");
+        return false;
     }
 
-    if (!file) {
-        log_err(ctx, "No file specified to generate XKB keymap\n");
-        return NULL;
-    }
-
-    ok = XkbParseFile(ctx, file, "(unknown file)", &xkb_file);
-    if (!ok) {
-        log_err(ctx, "Failed to parse input xkb file\n");
-        return NULL;
-    }
-
-    keymap = compile_keymap_file(ctx, xkb_file);
+    ok = compile_keymap_file(keymap, xkb_file);
     FreeXkbFile(xkb_file);
-    return keymap;
+    return ok;
 }
+
+const struct xkb_keymap_format_ops text_v1_keymap_format_ops = {
+    .keymap_new_from_names = text_v1_keymap_new_from_names,
+    .keymap_new_from_string = text_v1_keymap_new_from_string,
+    .keymap_new_from_file = text_v1_keymap_new_from_file,
+    .keymap_get_as_string = text_v1_keymap_get_as_string,
+};