/*
-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.
-*/
+ * 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"),
+ * 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 (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 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.
+ *
+ * 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"
-#include "parseutils.h"
-/* Global warning level */
-unsigned int warningLevel = 0;
-
-#define ISEMPTY(str) (!(str) || (strlen(str) == 0))
-
-static XkbFile *
-keymap_file_from_components(struct xkb_context *ctx,
- const struct xkb_component_names *ktcsg)
-{
- XkbFile *keycodes, *types, *compat, *symbols;
- IncludeStmt *inc;
-
- inc = IncludeCreate(ktcsg->keycodes, MERGE_DEFAULT);
- keycodes = CreateXKBFile(ctx, FILE_TYPE_KEYCODES, NULL,
- (ParseCommon *)inc, 0);
-
- inc = IncludeCreate(ktcsg->types, MERGE_DEFAULT);
- types = CreateXKBFile(ctx, FILE_TYPE_TYPES, NULL,
- (ParseCommon *)inc, 0);
- AppendStmt(&keycodes->common, &types->common);
-
- inc = IncludeCreate(ktcsg->compat, MERGE_DEFAULT);
- compat = CreateXKBFile(ctx, FILE_TYPE_COMPAT, NULL,
- (ParseCommon *)inc, 0);
- AppendStmt(&keycodes->common, &compat->common);
-
- inc = IncludeCreate(ktcsg->symbols, MERGE_DEFAULT);
- symbols = CreateXKBFile(ctx, FILE_TYPE_SYMBOLS, NULL,
- (ParseCommon *)inc, 0);
- AppendStmt(&keycodes->common, &symbols->common);
-
- return CreateXKBFile(ctx, FILE_TYPE_KEYMAP, strdup(""),
- &keycodes->common, 0);
-}
-
-static struct xkb_keymap *
-compile_keymap(struct xkb_context *ctx, XkbFile *file)
+static bool
+compile_keymap_file(struct xkb_keymap *keymap, XkbFile *file)
{
- struct xkb_keymap *keymap;
+ if (file->file_type != FILE_TYPE_KEYMAP) {
+ 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));
+ return false;
+ }
- keymap = CompileKeymap(ctx, file);
+ if (!CompileKeymap(file, keymap, MERGE_OVERRIDE)) {
+ log_err(keymap->ctx, XKB_LOG_MESSAGE_NO_ID,
+ "Failed to compile keymap\n");
+ return false;
+ }
- FreeXKBFile(file);
- return keymap;
+ return true;
}
-struct xkb_keymap *
-xkb_map_new_from_kccgst(struct xkb_context *ctx,
- const struct xkb_component_names *kccgst,
- 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;
XkbFile *file;
- if (!kccgst) {
- ERROR("no components specified\n");
- return NULL;
+ 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(keymap->ctx, rmlvo, &kccgst);
+ if (!ok) {
+ 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 false;
}
- 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;
- }
+ 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);
- if (ISEMPTY(kccgst->types)) {
- ERROR("types required to generate XKB keymap\n");
- return NULL;
- }
+ file = XkbFileFromComponents(keymap->ctx, &kccgst);
- if (ISEMPTY(kccgst->symbols)) {
- ERROR("symbols required to generate XKB keymap\n");
- return NULL;
- }
+ free(kccgst.keycodes);
+ free(kccgst.types);
+ free(kccgst.compat);
+ free(kccgst.symbols);
- file = keymap_file_from_components(ctx, kccgst);
if (!file) {
- ERROR("failed to generate parsed XKB file from components\n");
- return NULL;
+ log_err(keymap->ctx, XKB_LOG_MESSAGE_NO_ID,
+ "Failed to generate parsed XKB file from components\n");
+ return false;
}
- return compile_keymap(ctx, file);
+ ok = compile_keymap_file(keymap, file);
+ FreeXkbFile(file);
+ return ok;
}
-_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)
+static bool
+text_v1_keymap_new_from_string(struct xkb_keymap *keymap,
+ const char *string, size_t len)
{
- 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->keycodes);
- free(kkctgs->types);
- free(kkctgs->compat);
- free(kkctgs->symbols);
- free(kkctgs);
-
- return keymap;
-}
-
-_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;
- }
+ bool ok;
+ XkbFile *xkb_file;
- if (!XKBParseString(ctx, string, "input", &file)) {
- ERROR("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;
}
- return compile_keymap(ctx, file);
+ ok = compile_keymap_file(keymap, xkb_file);
+ FreeXkbFile(xkb_file);
+ return ok;
}
-_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)
+static bool
+text_v1_keymap_new_from_file(struct xkb_keymap *keymap, FILE *file)
{
+ bool ok;
XkbFile *xkb_file;
- if (format != XKB_KEYMAP_FORMAT_TEXT_V1) {
- ERROR("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) {
- 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);
+ ok = compile_keymap_file(keymap, xkb_file);
+ FreeXkbFile(xkb_file);
+ return ok;
}
-_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);
-}
+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,
+};