xref: /aosp_15_r20/external/libxkbcommon/src/xkbcomp/xkbcomp.c (revision 2b949d0487e80d67f1fda82db69e101e761f8064)
1*2b949d04SAndroid Build Coastguard Worker /*
2*2b949d04SAndroid Build Coastguard Worker  * Copyright © 2009 Dan Nicholson
3*2b949d04SAndroid Build Coastguard Worker  * Copyright © 2012 Intel Corporation
4*2b949d04SAndroid Build Coastguard Worker  * Copyright © 2012 Ran Benita <[email protected]>
5*2b949d04SAndroid Build Coastguard Worker  *
6*2b949d04SAndroid Build Coastguard Worker  * Permission is hereby granted, free of charge, to any person obtaining a
7*2b949d04SAndroid Build Coastguard Worker  * copy of this software and associated documentation files (the "Software"),
8*2b949d04SAndroid Build Coastguard Worker  * to deal in the Software without restriction, including without limitation
9*2b949d04SAndroid Build Coastguard Worker  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10*2b949d04SAndroid Build Coastguard Worker  * and/or sell copies of the Software, and to permit persons to whom the
11*2b949d04SAndroid Build Coastguard Worker  * Software is furnished to do so, subject to the following conditions:
12*2b949d04SAndroid Build Coastguard Worker  *
13*2b949d04SAndroid Build Coastguard Worker  * The above copyright notice and this permission notice (including the next
14*2b949d04SAndroid Build Coastguard Worker  * paragraph) shall be included in all copies or substantial portions of the
15*2b949d04SAndroid Build Coastguard Worker  * Software.
16*2b949d04SAndroid Build Coastguard Worker  *
17*2b949d04SAndroid Build Coastguard Worker  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18*2b949d04SAndroid Build Coastguard Worker  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19*2b949d04SAndroid Build Coastguard Worker  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20*2b949d04SAndroid Build Coastguard Worker  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21*2b949d04SAndroid Build Coastguard Worker  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22*2b949d04SAndroid Build Coastguard Worker  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23*2b949d04SAndroid Build Coastguard Worker  * DEALINGS IN THE SOFTWARE.
24*2b949d04SAndroid Build Coastguard Worker  *
25*2b949d04SAndroid Build Coastguard Worker  * Authors: Dan Nicholson <[email protected]>
26*2b949d04SAndroid Build Coastguard Worker  *          Ran Benita <[email protected]>
27*2b949d04SAndroid Build Coastguard Worker  *          Daniel Stone <[email protected]>
28*2b949d04SAndroid Build Coastguard Worker  */
29*2b949d04SAndroid Build Coastguard Worker 
30*2b949d04SAndroid Build Coastguard Worker #include "config.h"
31*2b949d04SAndroid Build Coastguard Worker 
32*2b949d04SAndroid Build Coastguard Worker #include "xkbcomp-priv.h"
33*2b949d04SAndroid Build Coastguard Worker #include "rules.h"
34*2b949d04SAndroid Build Coastguard Worker 
35*2b949d04SAndroid Build Coastguard Worker static bool
compile_keymap_file(struct xkb_keymap * keymap,XkbFile * file)36*2b949d04SAndroid Build Coastguard Worker compile_keymap_file(struct xkb_keymap *keymap, XkbFile *file)
37*2b949d04SAndroid Build Coastguard Worker {
38*2b949d04SAndroid Build Coastguard Worker     if (file->file_type != FILE_TYPE_KEYMAP) {
39*2b949d04SAndroid Build Coastguard Worker         log_err(keymap->ctx,
40*2b949d04SAndroid Build Coastguard Worker                 "Cannot compile a %s file alone into a keymap\n",
41*2b949d04SAndroid Build Coastguard Worker                 xkb_file_type_to_string(file->file_type));
42*2b949d04SAndroid Build Coastguard Worker         return false;
43*2b949d04SAndroid Build Coastguard Worker     }
44*2b949d04SAndroid Build Coastguard Worker 
45*2b949d04SAndroid Build Coastguard Worker     if (!CompileKeymap(file, keymap, MERGE_OVERRIDE)) {
46*2b949d04SAndroid Build Coastguard Worker         log_err(keymap->ctx,
47*2b949d04SAndroid Build Coastguard Worker                 "Failed to compile keymap\n");
48*2b949d04SAndroid Build Coastguard Worker         return false;
49*2b949d04SAndroid Build Coastguard Worker     }
50*2b949d04SAndroid Build Coastguard Worker 
51*2b949d04SAndroid Build Coastguard Worker     return true;
52*2b949d04SAndroid Build Coastguard Worker }
53*2b949d04SAndroid Build Coastguard Worker 
54*2b949d04SAndroid Build Coastguard Worker static bool
text_v1_keymap_new_from_names(struct xkb_keymap * keymap,const struct xkb_rule_names * rmlvo)55*2b949d04SAndroid Build Coastguard Worker text_v1_keymap_new_from_names(struct xkb_keymap *keymap,
56*2b949d04SAndroid Build Coastguard Worker                               const struct xkb_rule_names *rmlvo)
57*2b949d04SAndroid Build Coastguard Worker {
58*2b949d04SAndroid Build Coastguard Worker     bool ok;
59*2b949d04SAndroid Build Coastguard Worker     struct xkb_component_names kccgst;
60*2b949d04SAndroid Build Coastguard Worker     XkbFile *file;
61*2b949d04SAndroid Build Coastguard Worker 
62*2b949d04SAndroid Build Coastguard Worker     log_dbg(keymap->ctx,
63*2b949d04SAndroid Build Coastguard Worker             "Compiling from RMLVO: rules '%s', model '%s', layout '%s', "
64*2b949d04SAndroid Build Coastguard Worker             "variant '%s', options '%s'\n",
65*2b949d04SAndroid Build Coastguard Worker             rmlvo->rules, rmlvo->model, rmlvo->layout, rmlvo->variant,
66*2b949d04SAndroid Build Coastguard Worker             rmlvo->options);
67*2b949d04SAndroid Build Coastguard Worker 
68*2b949d04SAndroid Build Coastguard Worker     ok = xkb_components_from_rules(keymap->ctx, rmlvo, &kccgst);
69*2b949d04SAndroid Build Coastguard Worker     if (!ok) {
70*2b949d04SAndroid Build Coastguard Worker         log_err(keymap->ctx,
71*2b949d04SAndroid Build Coastguard Worker                 "Couldn't look up rules '%s', model '%s', layout '%s', "
72*2b949d04SAndroid Build Coastguard Worker                 "variant '%s', options '%s'\n",
73*2b949d04SAndroid Build Coastguard Worker                 rmlvo->rules, rmlvo->model, rmlvo->layout, rmlvo->variant,
74*2b949d04SAndroid Build Coastguard Worker                 rmlvo->options);
75*2b949d04SAndroid Build Coastguard Worker         return false;
76*2b949d04SAndroid Build Coastguard Worker     }
77*2b949d04SAndroid Build Coastguard Worker 
78*2b949d04SAndroid Build Coastguard Worker     log_dbg(keymap->ctx,
79*2b949d04SAndroid Build Coastguard Worker             "Compiling from KcCGST: keycodes '%s', types '%s', "
80*2b949d04SAndroid Build Coastguard Worker             "compat '%s', symbols '%s'\n",
81*2b949d04SAndroid Build Coastguard Worker             kccgst.keycodes, kccgst.types, kccgst.compat, kccgst.symbols);
82*2b949d04SAndroid Build Coastguard Worker 
83*2b949d04SAndroid Build Coastguard Worker     file = XkbFileFromComponents(keymap->ctx, &kccgst);
84*2b949d04SAndroid Build Coastguard Worker 
85*2b949d04SAndroid Build Coastguard Worker     free(kccgst.keycodes);
86*2b949d04SAndroid Build Coastguard Worker     free(kccgst.types);
87*2b949d04SAndroid Build Coastguard Worker     free(kccgst.compat);
88*2b949d04SAndroid Build Coastguard Worker     free(kccgst.symbols);
89*2b949d04SAndroid Build Coastguard Worker 
90*2b949d04SAndroid Build Coastguard Worker     if (!file) {
91*2b949d04SAndroid Build Coastguard Worker         log_err(keymap->ctx,
92*2b949d04SAndroid Build Coastguard Worker                 "Failed to generate parsed XKB file from components\n");
93*2b949d04SAndroid Build Coastguard Worker         return false;
94*2b949d04SAndroid Build Coastguard Worker     }
95*2b949d04SAndroid Build Coastguard Worker 
96*2b949d04SAndroid Build Coastguard Worker     ok = compile_keymap_file(keymap, file);
97*2b949d04SAndroid Build Coastguard Worker     FreeXkbFile(file);
98*2b949d04SAndroid Build Coastguard Worker     return ok;
99*2b949d04SAndroid Build Coastguard Worker }
100*2b949d04SAndroid Build Coastguard Worker 
101*2b949d04SAndroid Build Coastguard Worker static bool
text_v1_keymap_new_from_string(struct xkb_keymap * keymap,const char * string,size_t len)102*2b949d04SAndroid Build Coastguard Worker text_v1_keymap_new_from_string(struct xkb_keymap *keymap,
103*2b949d04SAndroid Build Coastguard Worker                                const char *string, size_t len)
104*2b949d04SAndroid Build Coastguard Worker {
105*2b949d04SAndroid Build Coastguard Worker     bool ok;
106*2b949d04SAndroid Build Coastguard Worker     XkbFile *xkb_file;
107*2b949d04SAndroid Build Coastguard Worker 
108*2b949d04SAndroid Build Coastguard Worker     xkb_file = XkbParseString(keymap->ctx, string, len, "(input string)", NULL);
109*2b949d04SAndroid Build Coastguard Worker     if (!xkb_file) {
110*2b949d04SAndroid Build Coastguard Worker         log_err(keymap->ctx, "Failed to parse input xkb string\n");
111*2b949d04SAndroid Build Coastguard Worker         return false;
112*2b949d04SAndroid Build Coastguard Worker     }
113*2b949d04SAndroid Build Coastguard Worker 
114*2b949d04SAndroid Build Coastguard Worker     ok = compile_keymap_file(keymap, xkb_file);
115*2b949d04SAndroid Build Coastguard Worker     FreeXkbFile(xkb_file);
116*2b949d04SAndroid Build Coastguard Worker     return ok;
117*2b949d04SAndroid Build Coastguard Worker }
118*2b949d04SAndroid Build Coastguard Worker 
119*2b949d04SAndroid Build Coastguard Worker static bool
text_v1_keymap_new_from_file(struct xkb_keymap * keymap,FILE * file)120*2b949d04SAndroid Build Coastguard Worker text_v1_keymap_new_from_file(struct xkb_keymap *keymap, FILE *file)
121*2b949d04SAndroid Build Coastguard Worker {
122*2b949d04SAndroid Build Coastguard Worker     bool ok;
123*2b949d04SAndroid Build Coastguard Worker     XkbFile *xkb_file;
124*2b949d04SAndroid Build Coastguard Worker 
125*2b949d04SAndroid Build Coastguard Worker     xkb_file = XkbParseFile(keymap->ctx, file, "(unknown file)", NULL);
126*2b949d04SAndroid Build Coastguard Worker     if (!xkb_file) {
127*2b949d04SAndroid Build Coastguard Worker         log_err(keymap->ctx, "Failed to parse input xkb file\n");
128*2b949d04SAndroid Build Coastguard Worker         return false;
129*2b949d04SAndroid Build Coastguard Worker     }
130*2b949d04SAndroid Build Coastguard Worker 
131*2b949d04SAndroid Build Coastguard Worker     ok = compile_keymap_file(keymap, xkb_file);
132*2b949d04SAndroid Build Coastguard Worker     FreeXkbFile(xkb_file);
133*2b949d04SAndroid Build Coastguard Worker     return ok;
134*2b949d04SAndroid Build Coastguard Worker }
135*2b949d04SAndroid Build Coastguard Worker 
136*2b949d04SAndroid Build Coastguard Worker const struct xkb_keymap_format_ops text_v1_keymap_format_ops = {
137*2b949d04SAndroid Build Coastguard Worker     .keymap_new_from_names = text_v1_keymap_new_from_names,
138*2b949d04SAndroid Build Coastguard Worker     .keymap_new_from_string = text_v1_keymap_new_from_string,
139*2b949d04SAndroid Build Coastguard Worker     .keymap_new_from_file = text_v1_keymap_new_from_file,
140*2b949d04SAndroid Build Coastguard Worker     .keymap_get_as_string = text_v1_keymap_get_as_string,
141*2b949d04SAndroid Build Coastguard Worker };
142