| 1 |
/* |
| 2 |
* Copyright © 2009 Dan Nicholson |
| 3 |
* Copyright © 2012 Intel Corporation |
| 4 |
* Copyright © 2012 Ran Benita <[email protected]> |
| 5 |
* SPDX-License-Identifier: MIT |
| 6 |
* |
| 7 |
* Author: Dan Nicholson <[email protected]> |
| 8 |
* Author: Ran Benita <[email protected]> |
| 9 |
* Author: Daniel Stone <[email protected]> |
| 10 |
*/ |
| 11 |
|
| 12 |
#include "config.h" |
| 13 |
|
| 14 |
#include <stdbool.h> |
| 15 |
|
| 16 |
#include "darray.h" |
| 17 |
#include "utils.h" |
| 18 |
#include "xkbcommon/xkbcommon.h" |
| 19 |
#include "xkbcomp-priv.h" |
| 20 |
#include "keymap.h" |
| 21 |
#include "rules.h" |
| 22 |
#include "rmlvo.h" |
| 23 |
|
| 24 |
bool |
| 25 |
xkb_components_names_from_rules(struct xkb_context *ctx, |
| 26 |
const struct xkb_rule_names *rmlvo_in, |
| 27 |
struct xkb_rule_names *rmlvo_out, |
| 28 |
struct xkb_component_names *components_out) |
| 29 |
{ |
| 30 |
/* Resolve default RMLVO values. We need a mutable copy of the input. */ |
| 31 |
struct xkb_rule_names rmlvo = *rmlvo_in; |
| 32 |
xkb_context_sanitize_rule_names(ctx, &rmlvo); |
| 33 |
|
| 34 |
if (rmlvo_out) { |
| 35 |
/* Copy the sanitized RMLVO, if required. */ |
| 36 |
*rmlvo_out = rmlvo; |
| 37 |
} |
| 38 |
if (!components_out) { |
| 39 |
/* KcCGST not required. |
| 40 |
* While RMLVO and KcCGST outputs are both optional, at least one must |
| 41 |
* be provided. */ |
| 42 |
return !!rmlvo_out; |
| 43 |
} |
| 44 |
|
| 45 |
/* Resolve the RMLVO names to KcCGST components */ |
| 46 |
*components_out = (struct xkb_component_names){ 0 }; |
| 47 |
return xkb_components_from_rules_names(ctx, &rmlvo, components_out, NULL); |
| 48 |
} |
| 49 |
|
| 50 |
static bool |
| 51 |
compile_keymap_file(struct xkb_keymap *keymap, XkbFile *file) |
| 52 |
{ |
| 53 |
if (file->file_type != FILE_TYPE_KEYMAP) { |
| 54 |
log_err(keymap->ctx, XKB_ERROR_KEYMAP_COMPILATION_FAILED, |
| 55 |
"Cannot compile a %s file alone into a keymap\n", |
| 56 |
xkb_file_type_to_string(file->file_type)); |
| 57 |
return false; |
| 58 |
} |
| 59 |
|
| 60 |
if (!CompileKeymap(file, keymap)) { |
| 61 |
log_err(keymap->ctx, XKB_ERROR_KEYMAP_COMPILATION_FAILED, |
| 62 |
"Failed to compile keymap\n"); |
| 63 |
return false; |
| 64 |
} |
| 65 |
|
| 66 |
return true; |
| 67 |
} |
| 68 |
|
| 69 |
static bool |
| 70 |
text_v1_keymap_new_from_rmlvo(struct xkb_keymap *keymap, |
| 71 |
const struct xkb_rmlvo_builder *rmlvo) |
| 72 |
{ |
| 73 |
bool ok; |
| 74 |
struct xkb_component_names kccgst; |
| 75 |
XkbFile *file; |
| 76 |
|
| 77 |
if (keymap->ctx->log_level >= XKB_LOG_LEVEL_DEBUG) { |
| 78 |
struct xkb_rule_names names = { 0 }; |
| 79 |
const size_t buf_size = sizeof(rmlvo->ctx->text_buffer) - 1; |
| 80 |
char *buf = xkb_context_get_buffer(rmlvo->ctx, buf_size); |
| 81 |
if (unlikely(!buf)) |
| 82 |
return false; |
| 83 |
ok = xkb_rmlvo_builder_to_rules_names(rmlvo, &names, buf, buf_size); |
| 84 |
if (unlikely(!ok)) |
| 85 |
return false; |
| 86 |
log_dbg(keymap->ctx, XKB_LOG_MESSAGE_NO_ID, |
| 87 |
"Compiling from RMLVO builder: rules '%s', model '%s', layout '%s', " |
| 88 |
"variant '%s', options '%s'\n", |
| 89 |
names.rules, names.model, names.layout, names.variant, |
| 90 |
names.options); |
| 91 |
} |
| 92 |
|
| 93 |
/* Resolve the RMLVO components to KcCGST components and get the |
| 94 |
* expected number of layouts */ |
| 95 |
ok = xkb_components_from_rmlvo_builder(rmlvo, &kccgst, &keymap->num_groups); |
| 96 |
if (!ok) { |
| 97 |
struct xkb_rule_names names = { 0 }; |
| 98 |
const size_t buf_size = sizeof(rmlvo->ctx->text_buffer); |
| 99 |
char *buf = xkb_context_get_buffer(rmlvo->ctx, buf_size); |
| 100 |
if (unlikely(!buf)) |
| 101 |
return false; |
| 102 |
ok = xkb_rmlvo_builder_to_rules_names(rmlvo, &names, buf, buf_size); |
| 103 |
if (unlikely(!ok)) |
| 104 |
return false; |
| 105 |
log_err(keymap->ctx, XKB_ERROR_KEYMAP_COMPILATION_FAILED, |
| 106 |
"Couldn't look up rules '%s', model '%s', layout '%s', " |
| 107 |
"variant '%s', options '%s'\n", |
| 108 |
names.rules, names.model, names.layout, names.variant, |
| 109 |
names.options); |
| 110 |
return false; |
| 111 |
} |
| 112 |
|
| 113 |
const xkb_layout_index_t max_groups = format_max_groups(keymap->format); |
| 114 |
if (keymap->num_groups > max_groups) |
| 115 |
keymap->num_groups = max_groups; |
| 116 |
|
| 117 |
log_dbg(keymap->ctx, XKB_LOG_MESSAGE_NO_ID, |
| 118 |
"Compiling from KcCGST: keycodes '%s', types '%s', " |
| 119 |
"compat '%s', symbols '%s'\n", |
| 120 |
kccgst.keycodes, kccgst.types, kccgst.compatibility, kccgst.symbols); |
| 121 |
|
| 122 |
file = XkbFileFromComponents(keymap->ctx, &kccgst); |
| 123 |
|
| 124 |
free(kccgst.keycodes); |
| 125 |
free(kccgst.types); |
| 126 |
free(kccgst.compatibility); |
| 127 |
free(kccgst.symbols); |
| 128 |
free(kccgst.geometry); |
| 129 |
|
| 130 |
if (!file) { |
| 131 |
log_err(keymap->ctx, XKB_ERROR_KEYMAP_COMPILATION_FAILED, |
| 132 |
"Failed to generate parsed XKB file from components\n"); |
| 133 |
return false; |
| 134 |
} |
| 135 |
|
| 136 |
ok = compile_keymap_file(keymap, file); |
| 137 |
FreeXkbFile(file); |
| 138 |
return ok; |
| 139 |
} |
| 140 |
|
| 141 |
static bool |
| 142 |
text_v1_keymap_new_from_names(struct xkb_keymap *keymap, |
| 143 |
const struct xkb_rule_names *rmlvo) |
| 144 |
{ |
| 145 |
bool ok; |
| 146 |
struct xkb_component_names kccgst; |
| 147 |
XkbFile *file; |
| 148 |
|
| 149 |
log_dbg(keymap->ctx, XKB_LOG_MESSAGE_NO_ID, |
| 150 |
"Compiling from RMLVO: rules '%s', model '%s', layout '%s', " |
| 151 |
"variant '%s', options '%s'\n", |
| 152 |
rmlvo->rules, rmlvo->model, rmlvo->layout, rmlvo->variant, |
| 153 |
rmlvo->options); |
| 154 |
|
| 155 |
/* Resolve the RMLVO components to KcCGST components and get the |
| 156 |
* expected number of layouts */ |
| 157 |
ok = xkb_components_from_rules_names(keymap->ctx, rmlvo, &kccgst, |
| 158 |
&keymap->num_groups); |
| 159 |
if (!ok) { |
| 160 |
log_err(keymap->ctx, XKB_ERROR_KEYMAP_COMPILATION_FAILED, |
| 161 |
"Couldn't look up rules '%s', model '%s', layout '%s', " |
| 162 |
"variant '%s', options '%s'\n", |
| 163 |
rmlvo->rules, rmlvo->model, rmlvo->layout, rmlvo->variant, |
| 164 |
rmlvo->options); |
| 165 |
return false; |
| 166 |
} |
| 167 |
|
| 168 |
const xkb_layout_index_t max_groups = format_max_groups(keymap->format); |
| 169 |
if (keymap->num_groups > max_groups) |
| 170 |
keymap->num_groups = max_groups; |
| 171 |
|
| 172 |
log_dbg(keymap->ctx, XKB_LOG_MESSAGE_NO_ID, |
| 173 |
"Compiling from KcCGST: keycodes '%s', types '%s', " |
| 174 |
"compat '%s', symbols '%s'\n", |
| 175 |
kccgst.keycodes, kccgst.types, kccgst.compatibility, kccgst.symbols); |
| 176 |
|
| 177 |
file = XkbFileFromComponents(keymap->ctx, &kccgst); |
| 178 |
|
| 179 |
free(kccgst.keycodes); |
| 180 |
free(kccgst.types); |
| 181 |
free(kccgst.compatibility); |
| 182 |
free(kccgst.symbols); |
| 183 |
free(kccgst.geometry); |
| 184 |
|
| 185 |
if (!file) { |
| 186 |
log_err(keymap->ctx, XKB_ERROR_KEYMAP_COMPILATION_FAILED, |
| 187 |
"Failed to generate parsed XKB file from components\n"); |
| 188 |
return false; |
| 189 |
} |
| 190 |
|
| 191 |
ok = compile_keymap_file(keymap, file); |
| 192 |
FreeXkbFile(file); |
| 193 |
return ok; |
| 194 |
} |
| 195 |
|
| 196 |
static bool |
| 197 |
text_v1_keymap_new_from_string(struct xkb_keymap *keymap, |
| 198 |
const char *string, size_t len) |
| 199 |
{ |
| 200 |
bool ok; |
| 201 |
XkbFile *xkb_file; |
| 202 |
|
| 203 |
xkb_file = XkbParseString(keymap->ctx, string, len, "(input string)", NULL); |
| 204 |
if (!xkb_file) { |
| 205 |
log_err(keymap->ctx, XKB_ERROR_KEYMAP_COMPILATION_FAILED, |
| 206 |
"Failed to parse input xkb string\n"); |
| 207 |
return false; |
| 208 |
} |
| 209 |
|
| 210 |
ok = compile_keymap_file(keymap, xkb_file); |
| 211 |
FreeXkbFile(xkb_file); |
| 212 |
return ok; |
| 213 |
} |
| 214 |
|
| 215 |
static bool |
| 216 |
text_v1_keymap_new_from_file(struct xkb_keymap *keymap, FILE *file) |
| 217 |
{ |
| 218 |
bool ok; |
| 219 |
XkbFile *xkb_file; |
| 220 |
|
| 221 |
xkb_file = XkbParseFile(keymap->ctx, file, "(unknown file)", NULL); |
| 222 |
if (!xkb_file) { |
| 223 |
log_err(keymap->ctx, XKB_ERROR_KEYMAP_COMPILATION_FAILED, |
| 224 |
"Failed to parse input xkb file\n"); |
| 225 |
return false; |
| 226 |
} |
| 227 |
|
| 228 |
ok = compile_keymap_file(keymap, xkb_file); |
| 229 |
FreeXkbFile(xkb_file); |
| 230 |
return ok; |
| 231 |
} |
| 232 |
|
| 233 |
const struct xkb_keymap_format_ops text_v1_keymap_format_ops = { |
| 234 |
.keymap_new_from_rmlvo = text_v1_keymap_new_from_rmlvo, |
| 235 |
.keymap_new_from_names = text_v1_keymap_new_from_names, |
| 236 |
.keymap_new_from_string = text_v1_keymap_new_from_string, |
| 237 |
.keymap_new_from_file = text_v1_keymap_new_from_file, |
| 238 |
.keymap_get_as_string = text_v1_keymap_get_as_string, |
| 239 |
}; |