Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ui / events / ozone / layout / xkb / xkb_keyboard_layout_engine.cc
blobaa410e11ddf1f6655f7c35e158b55d28242cfe69
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ui/events/ozone/layout/xkb/xkb_keyboard_layout_engine.h"
7 #include <xkbcommon/xkbcommon-names.h>
9 #include "base/bind.h"
10 #include "base/location.h"
11 #include "base/logging.h"
12 #include "base/single_thread_task_runner.h"
13 #include "base/task_runner.h"
14 #include "base/thread_task_runner_handle.h"
15 #include "base/threading/worker_pool.h"
16 #include "ui/events/event_constants.h"
17 #include "ui/events/keycodes/dom/dom_code.h"
18 #include "ui/events/keycodes/dom/dom_key.h"
19 #include "ui/events/keycodes/dom/keycode_converter.h"
20 #include "ui/events/keycodes/keyboard_code_conversion.h"
21 #include "ui/events/keycodes/keyboard_code_conversion_xkb.h"
23 namespace ui {
25 namespace {
27 typedef base::Callback<void(const std::string&,
28 scoped_ptr<char, base::FreeDeleter>)>
29 LoadKeymapCallback;
31 KeyboardCode AlphanumericKeyboardCode(base::char16 character) {
32 // Plain ASCII letters and digits map directly to VKEY values.
33 if ((character >= '0') && (character <= '9'))
34 return static_cast<KeyboardCode>(VKEY_0 + character - '0');
35 if ((character >= 'a') && (character <= 'z'))
36 return static_cast<KeyboardCode>(VKEY_A + character - 'a');
37 if ((character >= 'A') && (character <= 'Z'))
38 return static_cast<KeyboardCode>(VKEY_A + character - 'A');
39 return VKEY_UNKNOWN;
42 // These tables map layout-dependent printable characters/codes
43 // to legacy Windows-based VKEY values.
45 // VKEYs are determined by the character produced from a DomCode without
46 // any modifiers, plus zero or more of the DomCode itself, the character
47 // produced with the Shift modifier, and the character produced with the
48 // AltGr modifier.
50 // A table of one or more PrintableSubEntry cases applies when the VKEY is
51 // not determined by the unmodified character value alone. Each such table
52 // corresponds to one unmodified character value. For an entry to match,
53 // the dom_code must match, and, if test_X is set, then the character for
54 // the key plus modifier X must also match.
55 struct PrintableSubEntry {
56 DomCode dom_code;
57 bool test_shift : 1;
58 bool test_altgr : 1;
59 base::char16 shift_character;
60 base::char16 altgr_character;
61 KeyboardCode key_code;
64 const base::char16 kNone = 0xFFFE;
65 const base::char16 kAny = 0xFFFF;
67 // U+0021 exclamation mark
68 const PrintableSubEntry kU0021[] = {
69 {DomCode::DIGIT1, 0, 0, kAny, kAny, VKEY_1},
70 {DomCode::DIGIT8, 0, 0, kAny, kAny, VKEY_8},
71 {DomCode::SLASH, 0, 0, kAny, kAny, VKEY_OEM_8}};
73 // U+0022 quote
74 const PrintableSubEntry kU0022[] = {
75 {DomCode::DIGIT2, 0, 0, kAny, kAny, VKEY_2},
76 {DomCode::DIGIT3, 0, 0, kAny, kAny, VKEY_3}};
78 // U+0023 number sign
79 const PrintableSubEntry kU0023[] = {
80 {DomCode::BACKQUOTE, 0, 0, kAny, kAny, VKEY_OEM_7},
81 {DomCode::BACKSLASH, 1, 0, 0x0027, kAny, VKEY_OEM_2}, // apostrophe
82 {DomCode::BACKSLASH, 1, 0, 0x007E, kAny, VKEY_OEM_7}}; // ~, NoSymbol
84 // U+0024 dollar sign
85 const PrintableSubEntry kU0024[] = {
86 {DomCode::BRACKET_RIGHT, 0, 0, kAny, kAny, VKEY_OEM_1},
87 {DomCode::BACKSLASH, 0, 0, kAny, kAny, VKEY_OEM_8}};
89 // U+0027 apostrophe
90 const PrintableSubEntry kU0027[] = {
91 {DomCode::DIGIT4, 0, 0, kAny, kAny, VKEY_4},
92 {DomCode::KEY_Q, 0, 0, kAny, kAny, VKEY_OEM_7},
93 {DomCode::BRACKET_RIGHT, 0, 0, kAny, kAny, VKEY_OEM_1},
94 {DomCode::SLASH, 0, 0, kAny, kAny, VKEY_OEM_7},
95 {DomCode::QUOTE, 1, 0, 0x0022, kAny, VKEY_OEM_7}, // quote
96 {DomCode::BACKQUOTE, 1, 0, 0x0022, kAny, VKEY_OEM_3}, // quote
97 {DomCode::BACKQUOTE, 1, 0, 0x00B7, kAny, VKEY_OEM_5}, // middle dot
98 {DomCode::BACKSLASH, 1, 0, kNone, kAny, VKEY_OEM_5}, // NoSymbol
99 {DomCode::MINUS, 1, 0, 0x003F, kAny, VKEY_OEM_4}, // ?
100 {DomCode::EQUAL, 1, 0, 0x002A, kAny, VKEY_OEM_PLUS}, // *
101 {DomCode::QUOTE, 1, 0, 0x0040, kAny, VKEY_OEM_3}, // @
102 {DomCode::BACKSLASH, 1, 1, 0x002A, 0x00BD, VKEY_OEM_5}, // *, one half
103 {DomCode::BACKSLASH, 1, 0, 0x002A, kAny, VKEY_OEM_2}, // *, NoSymbol
104 {DomCode::KEY_Z, 1, 1, 0x0022, 0x0158, VKEY_OEM_7}, // quote, R caron
105 {DomCode::KEY_Z, 1, 0, 0x0022, kAny, VKEY_Z}}; // quote
107 // U+0028 left parenthesis
108 const PrintableSubEntry kU0028[] = {
109 {DomCode::DIGIT5, 0, 0, kAny, kAny, VKEY_5},
110 {DomCode::DIGIT9, 0, 0, kAny, kAny, VKEY_9},
111 {DomCode::BACKSLASH, 0, 0, kAny, kAny, VKEY_OEM_5}};
113 // U+0029 right parenthesis
114 const PrintableSubEntry kU0029[] = {
115 {DomCode::DIGIT0, 0, 0, kAny, kAny, VKEY_0},
116 {DomCode::MINUS, 0, 0, kAny, kAny, VKEY_OEM_4},
117 {DomCode::BRACKET_RIGHT, 0, 0, kAny, kAny, VKEY_OEM_6}};
119 // U+002A *
120 const PrintableSubEntry kU002A[] = {
121 {DomCode::BRACKET_RIGHT, 0, 0, kAny, kAny, VKEY_OEM_1},
122 {DomCode::BACKSLASH, 0, 0, kAny, kAny, VKEY_OEM_5}};
124 // U+002B plus sign
125 const PrintableSubEntry kU002B[] = {
126 {DomCode::DIGIT1, 0, 0, kAny, kAny, VKEY_1},
127 {DomCode::EQUAL, 0, 0, kAny, kAny, VKEY_OEM_PLUS},
128 {DomCode::BRACKET_LEFT, 0, 0, kAny, kAny, VKEY_OEM_PLUS},
129 {DomCode::BRACKET_RIGHT, 0, 0, kAny, kAny, VKEY_OEM_PLUS},
130 {DomCode::SEMICOLON, 0, 0, kAny, kAny, VKEY_OEM_PLUS},
131 {DomCode::BACKSLASH, 0, 0, kAny, kAny, VKEY_OEM_2},
132 {DomCode::MINUS, 1, 1, 0x003F, 0x005C, VKEY_OEM_MINUS}, // ?, backslash
133 {DomCode::MINUS, 1, 0, 0x003F, kAny, VKEY_OEM_PLUS}}; // ?
135 // U+002C comma
136 const PrintableSubEntry kU002C[] = {
137 {DomCode::DIGIT3, 0, 0, kAny, kAny, VKEY_3},
138 {DomCode::DIGIT5, 0, 0, kAny, kAny, VKEY_5},
139 {DomCode::DIGIT6, 0, 0, kAny, kAny, VKEY_6},
140 {DomCode::DIGIT9, 0, 0, kAny, kAny, VKEY_9},
141 {DomCode::KEY_W, 0, 0, kAny, kAny, VKEY_OEM_COMMA},
142 {DomCode::KEY_V, 0, 0, kAny, kAny, VKEY_OEM_COMMA},
143 {DomCode::KEY_M, 0, 0, kAny, kAny, VKEY_OEM_COMMA},
144 {DomCode::COMMA, 0, 0, kAny, kAny, VKEY_OEM_COMMA}};
146 // U+002D hyphen-minus
147 const PrintableSubEntry kU002D[] = {
148 {DomCode::DIGIT2, 0, 0, kAny, kAny, VKEY_2},
149 {DomCode::DIGIT6, 0, 0, kAny, kAny, VKEY_6},
150 {DomCode::MINUS, 0, 0, kAny, kAny, VKEY_OEM_MINUS},
151 {DomCode::KEY_A, 0, 0, kAny, kAny, VKEY_OEM_MINUS},
152 {DomCode::QUOTE, 0, 0, kAny, kAny, VKEY_OEM_MINUS},
153 {DomCode::SLASH, 1, 0, 0x003D, kAny, VKEY_OEM_MINUS}, // =
154 {DomCode::EQUAL, 1, 1, 0x005F, 0x0157, VKEY_OEM_4}, // _, r cedilla
155 {DomCode::EQUAL, 1, 0, 0x005F, kAny, VKEY_OEM_MINUS}, // _
156 {DomCode::SLASH, 1, 1, 0x005F, 0x002F, VKEY_OEM_2}, // _, /
157 {DomCode::SLASH, 1, 0, 0x005F, kAny, VKEY_OEM_MINUS}}; // _
159 // U+002E full stop
160 const PrintableSubEntry kU002E[] = {
161 {DomCode::DIGIT7, 0, 0, kAny, kAny, VKEY_7},
162 {DomCode::DIGIT8, 0, 0, kAny, kAny, VKEY_8},
163 {DomCode::KEY_E, 0, 0, kAny, kAny, VKEY_OEM_PERIOD},
164 {DomCode::KEY_R, 0, 0, kAny, kAny, VKEY_OEM_PERIOD},
165 {DomCode::KEY_O, 0, 0, kAny, kAny, VKEY_OEM_PERIOD},
166 {DomCode::QUOTE, 0, 0, kAny, kAny, VKEY_OEM_7},
167 {DomCode::PERIOD, 0, 0, kAny, kAny, VKEY_OEM_PERIOD},
168 {DomCode::SLASH, 0, 0, kAny, kAny, VKEY_OEM_2}};
170 // U+002F /
171 const PrintableSubEntry kU002F[] = {
172 {DomCode::MINUS, 0, 0, kAny, kAny, VKEY_OEM_4},
173 {DomCode::BRACKET_LEFT, 0, 0, kAny, kAny, VKEY_OEM_2},
174 {DomCode::BACKQUOTE, 0, 0, kAny, kAny, VKEY_OEM_7},
175 {DomCode::BACKSLASH, 0, 0, kAny, kAny, VKEY_OEM_5},
176 {DomCode::SLASH, 0, 0, kAny, kAny, VKEY_OEM_2},
177 {DomCode::DIGIT3, 1, 0, 0x0033, kAny, VKEY_3}, // 3
178 {DomCode::DIGIT3, 1, 0, 0x003F, kAny, VKEY_OEM_2}, // ?
179 {DomCode::DIGIT0, 1, 0, 0x0030, kAny, VKEY_0}, // 0
180 {DomCode::DIGIT0, 1, 0, 0x003F, kAny, VKEY_OEM_2}}; // ?
182 // U+003A colon
183 const PrintableSubEntry kU003A[] = {
184 {DomCode::DIGIT1, 0, 0, kAny, kAny, VKEY_1},
185 {DomCode::DIGIT5, 0, 0, kAny, kAny, VKEY_5},
186 {DomCode::DIGIT6, 0, 0, kAny, kAny, VKEY_6},
187 {DomCode::PERIOD, 0, 0, kAny, kAny, VKEY_OEM_2}};
189 // U+003B semicolon
190 const PrintableSubEntry kU003B[] = {
191 {DomCode::DIGIT4, 0, 0, kAny, kAny, VKEY_4},
192 {DomCode::DIGIT8, 0, 0, kAny, kAny, VKEY_8},
193 {DomCode::KEY_Q, 0, 0, kAny, kAny, VKEY_OEM_1},
194 {DomCode::BRACKET_LEFT, 0, 0, kAny, kAny, VKEY_OEM_1},
195 {DomCode::BRACKET_RIGHT, 0, 0, kAny, kAny, VKEY_OEM_6},
196 {DomCode::SEMICOLON, 0, 0, kAny, kAny, VKEY_OEM_1},
197 {DomCode::BACKQUOTE, 0, 0, kAny, kAny, VKEY_OEM_3},
198 {DomCode::KEY_Z, 0, 0, kAny, kAny, VKEY_OEM_1},
199 {DomCode::COMMA, 0, 0, kAny, kAny, VKEY_OEM_PERIOD},
200 {DomCode::SLASH, 0, 0, kAny, kAny, VKEY_OEM_2}};
201 // U+003D =
202 const PrintableSubEntry kU003D[] = {
203 {DomCode::DIGIT8, 0, 0, kAny, kAny, VKEY_8},
204 {DomCode::EQUAL, 0, 0, kAny, kAny, VKEY_OEM_PLUS},
205 {DomCode::BRACKET_RIGHT, 0, 0, kAny, kAny, VKEY_OEM_PLUS},
206 {DomCode::SLASH, 1, 0, 0x0025, kAny, VKEY_OEM_8}, // %
207 {DomCode::SLASH, 1, 0, 0x002B, kAny, VKEY_OEM_PLUS}, // +
208 {DomCode::MINUS, 1, 1, 0x0025, 0x002D, VKEY_OEM_MINUS}, // %, -
209 {DomCode::MINUS, 1, 0, 0x0025, kAny, VKEY_OEM_PLUS}}; // %, NoSymbol
211 // U+003F ?
212 const PrintableSubEntry kU003F[] = {
213 {DomCode::DIGIT2, 0, 0, kAny, kAny, VKEY_2},
214 {DomCode::DIGIT7, 0, 0, kAny, kAny, VKEY_7},
215 {DomCode::DIGIT8, 0, 0, kAny, kAny, VKEY_8},
216 {DomCode::MINUS, 0, 0, kAny, kAny, VKEY_OEM_PLUS}};
218 // U+0040 @
219 const PrintableSubEntry kU0040[] = {
220 {DomCode::BRACKET_RIGHT, 0, 0, kAny, kAny, VKEY_OEM_6},
221 {DomCode::BACKQUOTE, 0, 0, kAny, kAny, VKEY_OEM_7}};
223 // U+005B left square bracket
224 const PrintableSubEntry kU005B[] = {
225 {DomCode::DIGIT1, 0, 0, kAny, kAny, VKEY_OEM_4},
226 {DomCode::MINUS, 0, 0, kAny, kAny, VKEY_OEM_4},
227 {DomCode::BRACKET_LEFT, 0, 0, kAny, kAny, VKEY_OEM_4},
228 {DomCode::BRACKET_RIGHT, 0, 0, kAny, kAny, VKEY_OEM_6},
229 {DomCode::QUOTE, 0, 0, kAny, kAny, VKEY_OEM_7}};
231 // U+005C backslash
232 const PrintableSubEntry kU005C[] = {
233 {DomCode::EQUAL, 0, 0, kAny, kAny, VKEY_OEM_4},
234 {DomCode::BACKSLASH, 1, 0, 0x002F, kAny, VKEY_OEM_7}, // /
235 {DomCode::BACKSLASH, 1, 0, 0x007C, kAny, VKEY_OEM_5}, // |
236 {DomCode::BACKQUOTE, 1, 1, 0x007C, 0x0031, VKEY_OEM_5}, // |, 1
237 {DomCode::BACKQUOTE, 1, 1, 0x007C, 0x0145, VKEY_OEM_3}}; // |, N cedilla
239 // U+005D right square bracket
240 const PrintableSubEntry kU005D[] = {
241 {DomCode::DIGIT2, 0, 0, kAny, kAny, VKEY_OEM_6},
242 {DomCode::EQUAL, 0, 0, kAny, kAny, VKEY_OEM_6},
243 {DomCode::BRACKET_RIGHT, 0, 0, kAny, kAny, VKEY_OEM_6},
244 {DomCode::BACKQUOTE, 0, 0, kAny, kAny, VKEY_OEM_3},
245 {DomCode::BACKSLASH, 0, 0, kAny, kAny, VKEY_OEM_5}};
247 // U+005F _
248 const PrintableSubEntry kU005F[] = {
249 {DomCode::DIGIT8, 0, 0, kAny, kAny, VKEY_8},
250 {DomCode::MINUS, 0, 0, kAny, kAny, VKEY_OEM_MINUS}};
252 // U+0060 grave accent
253 const PrintableSubEntry kU0060[] = {
254 {DomCode::BACKQUOTE, 1, 0, kNone, kAny, VKEY_OEM_3}, // NoSymbol
255 {DomCode::BACKQUOTE, 1, 0, 0x00AC, kAny, VKEY_OEM_8}, // not
256 {DomCode::BACKQUOTE, 1, 0, 0x007E, kAny, VKEY_OEM_3}}; // ~
258 // U+00A7 section
259 const PrintableSubEntry kU00A7[] = {
260 {DomCode::DIGIT4, 0, 0, kAny, kAny, VKEY_4},
261 {DomCode::DIGIT6, 0, 0, kAny, kAny, VKEY_6},
262 {DomCode::QUOTE, 0, 0, kAny, kAny, VKEY_OEM_7},
263 {DomCode::BACKQUOTE, 1, 0, 0x00B0, kAny, VKEY_OEM_2}, // degree
264 {DomCode::BACKQUOTE, 1, 0, 0x00BD, kAny, VKEY_OEM_5}}; // one half
266 // U+00AB left-pointing double angle quote
267 const PrintableSubEntry kU00AB[] = {
268 {DomCode::DIGIT8, 0, 0, kAny, kAny, VKEY_8},
269 {DomCode::EQUAL, 0, 0, kAny, kAny, VKEY_OEM_6}};
271 // U+00B0 degree
272 const PrintableSubEntry kU00B0[] = {
273 {DomCode::EQUAL, 0, 0, kAny, kAny, VKEY_OEM_2},
274 {DomCode::BACKQUOTE, 0, 0, kAny, kAny, VKEY_OEM_7}};
276 // U+00BA masculine ordinal indicator
277 const PrintableSubEntry kU00BA[] = {
278 {DomCode::QUOTE, 0, 0, kAny, kAny, VKEY_OEM_7},
279 {DomCode::BACKQUOTE, 0, 0, kAny, kAny, VKEY_OEM_5}};
281 // U+00E0 a grave
282 const PrintableSubEntry kU00E0[] = {
283 {DomCode::DIGIT0, 0, 0, kAny, kAny, VKEY_0},
284 {DomCode::BACKSLASH, 0, 0, kAny, kAny, VKEY_OEM_5},
285 {DomCode::QUOTE, 1, 0, 0x00B0, kAny, VKEY_OEM_7}, // degree
286 {DomCode::QUOTE, 1, 0, 0x00E4, kAny, VKEY_OEM_5}}; // a diaeresis
288 // U+00E1 a acute
289 const PrintableSubEntry kU00E1[] = {
290 {DomCode::DIGIT8, 0, 0, kAny, kAny, VKEY_8},
291 {DomCode::QUOTE, 0, 0, kAny, kAny, VKEY_OEM_7}};
293 // U+00E2 a circumflex
294 const PrintableSubEntry kU00E2[] = {
295 {DomCode::DIGIT2, 0, 0, kAny, kAny, VKEY_2},
296 {DomCode::BACKSLASH, 0, 0, kAny, kAny, VKEY_OEM_5}};
298 // U+00E4 a diaeresis
299 const PrintableSubEntry kU00E4[] = {
300 {DomCode::BRACKET_RIGHT, 0, 0, kAny, kAny, VKEY_OEM_6},
301 {DomCode::QUOTE, 1, 0, 0x00E0, kAny, VKEY_OEM_5}, // a grave
302 {DomCode::QUOTE, 1, 0, 0x00C4, kAny, VKEY_OEM_7}}; // A dia.
304 // U+00E6 ae
305 const PrintableSubEntry kU00E6[] = {
306 {DomCode::SEMICOLON, 0, 0, kAny, kAny, VKEY_OEM_3},
307 {DomCode::QUOTE, 0, 0, kAny, kAny, VKEY_OEM_7}};
309 // U+00E7 c cedilla
310 const PrintableSubEntry kU00E7[] = {
311 {DomCode::DIGIT9, 0, 0, kAny, kAny, VKEY_9},
312 {DomCode::BRACKET_LEFT, 0, 0, kAny, kAny, VKEY_OEM_4},
313 {DomCode::BRACKET_RIGHT, 0, 0, kAny, kAny, VKEY_OEM_6},
314 {DomCode::QUOTE, 0, 0, kAny, kAny, VKEY_OEM_7},
315 {DomCode::BACKSLASH, 0, 0, kAny, kAny, VKEY_OEM_2},
316 {DomCode::COMMA, 0, 0, kAny, kAny, VKEY_OEM_COMMA},
317 {DomCode::SEMICOLON, 1, 1, 0x00C7, 0x00DE, VKEY_OEM_3}, // C ced., Thorn
318 {DomCode::SEMICOLON, 1, 0, 0x00C7, kAny, VKEY_OEM_1}}; // C ced., NoSy
320 // U+00E8 e grave
321 const PrintableSubEntry kU00E8[] = {
322 {DomCode::DIGIT7, 0, 0, kAny, kAny, VKEY_7},
323 {DomCode::BRACKET_LEFT, 0, 0, kAny, kAny, VKEY_OEM_1},
324 {DomCode::QUOTE, 0, 0, kAny, kAny, VKEY_OEM_3}};
326 // U+00E9 e acute
327 const PrintableSubEntry kU00E9[] = {
328 {DomCode::DIGIT2, 0, 0, kAny, kAny, VKEY_2},
329 {DomCode::DIGIT0, 0, 0, kAny, kAny, VKEY_0},
330 {DomCode::SLASH, 0, 0, kAny, kAny, VKEY_OEM_2},
331 {DomCode::SEMICOLON, 1, 0, 0x00C9, kAny, VKEY_OEM_1}, // E acute
332 {DomCode::SEMICOLON, 1, 0, 0x00F6, kAny, VKEY_OEM_7}}; // o diaeresis
334 // U+00ED i acute
335 const PrintableSubEntry kU00ED[] = {
336 {DomCode::DIGIT9, 0, 0, kAny, kAny, VKEY_9},
337 {DomCode::BACKQUOTE, 0, 0, kAny, kAny, VKEY_0}};
339 // U+00F0 eth
340 const PrintableSubEntry kU00F0[] = {
341 {DomCode::BRACKET_LEFT, 0, 0, kAny, kAny, VKEY_OEM_6},
342 {DomCode::BRACKET_RIGHT, 0, 0, kAny, kAny, VKEY_OEM_1}};
344 // U+00F3 o acute
345 const PrintableSubEntry kU00F3[] = {
346 {DomCode::EQUAL, 0, 0, kAny, kAny, VKEY_OEM_PLUS},
347 {DomCode::BACKSLASH, 0, 0, kAny, kAny, VKEY_OEM_5}};
349 // U+00F4 o circumflex
350 const PrintableSubEntry kU00F4[] = {
351 {DomCode::DIGIT4, 0, 0, kAny, kAny, VKEY_4},
352 {DomCode::SEMICOLON, 0, 0, kAny, kAny, VKEY_OEM_1}};
354 // U+00F6 o diaeresis
355 const PrintableSubEntry kU00F6[] = {
356 {DomCode::DIGIT0, 0, 0, kAny, kAny, VKEY_OEM_3},
357 {DomCode::MINUS, 0, 0, kAny, kAny, VKEY_OEM_PLUS},
358 {DomCode::BRACKET_LEFT, 0, 0, kAny, kAny, VKEY_OEM_4},
359 {DomCode::SEMICOLON, 1, 0, 0x00E9, kAny, VKEY_OEM_7}, // e acute
360 {DomCode::SEMICOLON, 1, 1, 0x00D6, 0x0162, VKEY_OEM_3}, // O dia., T ced.
361 {DomCode::SEMICOLON, 1, 0, 0x00D6, kAny, VKEY_OEM_3}}; // O diaresis
363 // U+00F8 o stroke
364 const PrintableSubEntry kU00F8[] = {
365 {DomCode::SEMICOLON, 0, 0, kAny, kAny, VKEY_OEM_3},
366 {DomCode::QUOTE, 0, 0, kAny, kAny, VKEY_OEM_7}};
368 // U+00F9 u grave
369 const PrintableSubEntry kU00F9[] = {
370 {DomCode::QUOTE, 0, 0, kAny, kAny, VKEY_OEM_3},
371 {DomCode::BACKSLASH, 0, 0, kAny, kAny, VKEY_OEM_2}};
373 // U+00FA u acute
374 const PrintableSubEntry kU00FA[] = {
375 {DomCode::BRACKET_LEFT, 0, 0, kAny, kAny, VKEY_OEM_4},
376 {DomCode::BRACKET_RIGHT, 0, 0, kAny, kAny, VKEY_OEM_6}};
378 // U+00FC u diaeresis
379 const PrintableSubEntry kU00FC[] = {
380 {DomCode::KEY_W, 0, 0, kAny, kAny, VKEY_W},
381 {DomCode::BRACKET_LEFT, 1, 0, 0x00E8, kAny, VKEY_OEM_1}, // e grave
382 {DomCode::MINUS, 1, 0, 0x00DC, kAny, VKEY_OEM_2}, // U diaresis
383 {DomCode::BRACKET_LEFT, 1, 1, 0x00DC, 0x0141, VKEY_OEM_3}, // U dia., L-
384 {DomCode::BRACKET_LEFT, 1, 0, 0x00DC, kAny, VKEY_OEM_1}}; // U diaresis
386 // U+0103 a breve
387 const PrintableSubEntry kU0103[] = {
388 {DomCode::DIGIT1, 0, 0, kAny, kAny, VKEY_1},
389 {DomCode::BRACKET_LEFT, 0, 0, kAny, kAny, VKEY_OEM_4}};
391 // U+0105 a ogonek
392 const PrintableSubEntry kU0105[] = {
393 {DomCode::DIGIT1, 0, 0, kAny, kAny, VKEY_1},
394 {DomCode::KEY_Q, 0, 0, kAny, kAny, VKEY_Q},
395 {DomCode::QUOTE, 0, 0, kAny, kAny, VKEY_OEM_7}};
397 // U+010D c caron
398 const PrintableSubEntry kU010D[] = {
399 {DomCode::DIGIT2, 0, 0, kAny, kAny, VKEY_2},
400 {DomCode::DIGIT4, 0, 0, kAny, kAny, VKEY_4},
401 {DomCode::KEY_P, 0, 0, kAny, kAny, VKEY_X},
402 {DomCode::SEMICOLON, 0, 0, kAny, kAny, VKEY_OEM_1},
403 {DomCode::COMMA, 0, 0, kAny, kAny, VKEY_OEM_COMMA}};
405 // U+0111 d stroke
406 const PrintableSubEntry kU0111[] = {
407 {DomCode::DIGIT0, 0, 0, kAny, kAny, VKEY_0},
408 {DomCode::BRACKET_RIGHT, 0, 0, kAny, kAny, VKEY_OEM_6}};
410 // U+0117 e dot above
411 const PrintableSubEntry kU0117[] = {
412 {DomCode::DIGIT4, 0, 0, kAny, kAny, VKEY_4},
413 {DomCode::QUOTE, 0, 0, kAny, kAny, VKEY_OEM_7}};
415 // U+0119 e ogonek
416 const PrintableSubEntry kU0119[] = {
417 {DomCode::DIGIT3, 0, 0, kAny, kAny, VKEY_3},
418 {DomCode::SLASH, 1, 1, 0x0118, 0x006E, VKEY_OEM_2}, // E ogonek, n
419 {DomCode::SLASH, 1, 0, 0x0118, kAny, VKEY_OEM_MINUS}}; // E ogonek
421 // U+012F i ogonek
422 const PrintableSubEntry kU012F[] = {
423 {DomCode::DIGIT5, 0, 0, kAny, kAny, VKEY_5},
424 {DomCode::BRACKET_LEFT, 1, 0, 0x012E, kAny, VKEY_OEM_4}}; // Iogonek
426 // U+0142 l stroke
427 const PrintableSubEntry kU0142[] = {
428 {DomCode::SEMICOLON, 0, 0, kAny, kAny, VKEY_OEM_1},
429 {DomCode::BACKSLASH, 0, 0, kAny, kAny, VKEY_OEM_2}};
431 // U+015F s cedilla
432 const PrintableSubEntry kU015F[] = {
433 {DomCode::SEMICOLON, 0, 0, kAny, kAny, VKEY_OEM_1},
434 {DomCode::PERIOD, 0, 0, kAny, kAny, VKEY_OEM_PERIOD}};
436 // U+0161 s caron
437 const PrintableSubEntry kU0161[] = {
438 {DomCode::DIGIT3, 0, 0, kAny, kAny, VKEY_3},
439 {DomCode::DIGIT6, 0, 0, kAny, kAny, VKEY_6},
440 {DomCode::BRACKET_LEFT, 0, 0, kAny, kAny, VKEY_OEM_4},
441 {DomCode::KEY_A, 0, 0, kAny, kAny, VKEY_OEM_1},
442 {DomCode::KEY_F, 0, 0, kAny, kAny, VKEY_F},
443 {DomCode::PERIOD, 0, 0, kAny, kAny, VKEY_OEM_PERIOD}};
445 // U+016B u macron
446 const PrintableSubEntry kU016B[] = {
447 {DomCode::DIGIT8, 0, 0, kAny, kAny, VKEY_8},
448 {DomCode::KEY_Q, 0, 0, kAny, kAny, VKEY_Q},
449 {DomCode::KEY_X, 0, 0, kAny, kAny, VKEY_X}};
451 // U+0173 u ogonek
452 const PrintableSubEntry kU0173[] = {
453 {DomCode::DIGIT7, 0, 0, kAny, kAny, VKEY_7},
454 {DomCode::SEMICOLON, 1, 1, 0x0172, 0x0162, VKEY_OEM_1}, // U ogo., T ced.
455 {DomCode::SEMICOLON, 1, 0, 0x0172, kAny, VKEY_OEM_3}}; // U ogonek
457 // U+017C z dot above
458 const PrintableSubEntry kU017C[] = {
459 {DomCode::BRACKET_LEFT, 0, 0, kAny, kAny, VKEY_OEM_4},
460 {DomCode::BACKSLASH, 0, 0, kAny, kAny, VKEY_OEM_5}};
462 // U+017E z caron
463 const PrintableSubEntry kU017E[] = {
464 {DomCode::DIGIT6, 0, 0, kAny, kAny, VKEY_6},
465 {DomCode::EQUAL, 0, 0, kAny, kAny, VKEY_OEM_PLUS},
466 {DomCode::KEY_W, 0, 0, kAny, kAny, VKEY_W},
467 {DomCode::BRACKET_LEFT, 0, 0, kAny, kAny, VKEY_Y},
468 {DomCode::BACKSLASH, 0, 0, kAny, kAny, VKEY_OEM_5}};
470 // Table mapping unshifted characters to PrintableSubEntry tables.
471 struct PrintableMultiEntry {
472 base::char16 plain_character;
473 const PrintableSubEntry* subtable;
474 size_t subtable_size;
477 // Entries are ordered by character value.
478 const PrintableMultiEntry kMultiMap[] = {
479 {0x0021, kU0021, arraysize(kU0021)}, // exclamation mark
480 {0x0022, kU0022, arraysize(kU0022)}, // quotation mark
481 {0x0023, kU0023, arraysize(kU0023)}, // number sign
482 {0x0024, kU0024, arraysize(kU0024)}, // dollar sign
483 {0x0027, kU0027, arraysize(kU0027)}, // apostrophe
484 {0x0028, kU0028, arraysize(kU0028)}, // left parenthesis
485 {0x0029, kU0029, arraysize(kU0029)}, // right parenthesis
486 {0x002A, kU002A, arraysize(kU002A)}, // asterisk
487 {0x002B, kU002B, arraysize(kU002B)}, // plus sign
488 {0x002C, kU002C, arraysize(kU002C)}, // comma
489 {0x002D, kU002D, arraysize(kU002D)}, // hyphen-minus
490 {0x002E, kU002E, arraysize(kU002E)}, // full stop
491 {0x002F, kU002F, arraysize(kU002F)}, // solidus
492 {0x003A, kU003A, arraysize(kU003A)}, // colon
493 {0x003B, kU003B, arraysize(kU003B)}, // semicolon
494 {0x003D, kU003D, arraysize(kU003D)}, // equals sign
495 {0x003F, kU003F, arraysize(kU003F)}, // question mark
496 {0x0040, kU0040, arraysize(kU0040)}, // commercial at
497 {0x005B, kU005B, arraysize(kU005B)}, // left square bracket
498 {0x005C, kU005C, arraysize(kU005C)}, // reverse solidus
499 {0x005D, kU005D, arraysize(kU005D)}, // right square bracket
500 {0x005F, kU005F, arraysize(kU005F)}, // low line
501 {0x0060, kU0060, arraysize(kU0060)}, // grave accent
502 {0x00A7, kU00A7, arraysize(kU00A7)}, // section sign
503 {0x00AB, kU00AB, arraysize(kU00AB)}, // left double angle quotation mark
504 {0x00B0, kU00B0, arraysize(kU00B0)}, // degree sign
505 {0x00BA, kU00BA, arraysize(kU00BA)}, // masculine ordinal indicator
506 {0x00E0, kU00E0, arraysize(kU00E0)}, // a grave
507 {0x00E1, kU00E1, arraysize(kU00E1)}, // a acute
508 {0x00E2, kU00E2, arraysize(kU00E2)}, // a circumflex
509 {0x00E4, kU00E4, arraysize(kU00E4)}, // a diaeresis
510 {0x00E6, kU00E6, arraysize(kU00E6)}, // ae
511 {0x00E7, kU00E7, arraysize(kU00E7)}, // c cedilla
512 {0x00E8, kU00E8, arraysize(kU00E8)}, // e grave
513 {0x00E9, kU00E9, arraysize(kU00E9)}, // e acute
514 {0x00ED, kU00ED, arraysize(kU00ED)}, // i acute
515 {0x00F0, kU00F0, arraysize(kU00F0)}, // eth
516 {0x00F3, kU00F3, arraysize(kU00F3)}, // o acute
517 {0x00F4, kU00F4, arraysize(kU00F4)}, // o circumflex
518 {0x00F6, kU00F6, arraysize(kU00F6)}, // o diaeresis
519 {0x00F8, kU00F8, arraysize(kU00F8)}, // o stroke
520 {0x00F9, kU00F9, arraysize(kU00F9)}, // u grave
521 {0x00FA, kU00FA, arraysize(kU00FA)}, // u acute
522 {0x00FC, kU00FC, arraysize(kU00FC)}, // u diaeresis
523 {0x0103, kU0103, arraysize(kU0103)}, // a breve
524 {0x0105, kU0105, arraysize(kU0105)}, // a ogonek
525 {0x010D, kU010D, arraysize(kU010D)}, // c caron
526 {0x0111, kU0111, arraysize(kU0111)}, // d stroke
527 {0x0117, kU0117, arraysize(kU0117)}, // e dot above
528 {0x0119, kU0119, arraysize(kU0119)}, // e ogonek
529 {0x012F, kU012F, arraysize(kU012F)}, // i ogonek
530 {0x0142, kU0142, arraysize(kU0142)}, // l stroke
531 {0x015F, kU015F, arraysize(kU015F)}, // s cedilla
532 {0x0161, kU0161, arraysize(kU0161)}, // s caron
533 {0x016B, kU016B, arraysize(kU016B)}, // u macron
534 {0x0173, kU0173, arraysize(kU0173)}, // u ogonek
535 {0x017C, kU017C, arraysize(kU017C)}, // z dot above
536 {0x017E, kU017E, arraysize(kU017E)}, // z caron
539 // Table mapping unshifted characters to VKEY values.
540 struct PrintableSimpleEntry {
541 base::char16 plain_character;
542 KeyboardCode key_code;
545 // Entries are ordered by character value.
546 const PrintableSimpleEntry kSimpleMap[] = {
547 {0x0025, VKEY_5}, // percent sign
548 {0x0026, VKEY_1}, // ampersand
549 {0x003C, VKEY_OEM_5}, // less-than sign
550 {0x007B, VKEY_OEM_7}, // left curly bracket
551 {0x007C, VKEY_OEM_5}, // vertical line
552 {0x007D, VKEY_OEM_2}, // right curly bracket
553 {0x007E, VKEY_OEM_5}, // tilde
554 {0x00A1, VKEY_OEM_6}, // inverted exclamation mark
555 {0x00AD, VKEY_OEM_3}, // soft hyphen
556 {0x00B2, VKEY_OEM_7}, // superscript two
557 {0x00B5, VKEY_OEM_5}, // micro sign
558 {0x00BB, VKEY_9}, // right-pointing double angle quotation mark
559 {0x00BD, VKEY_OEM_5}, // vulgar fraction one half
560 {0x00BF, VKEY_OEM_6}, // inverted question mark
561 {0x00DF, VKEY_OEM_4}, // sharp s
562 {0x00E5, VKEY_OEM_6}, // a ring above
563 {0x00EA, VKEY_3}, // e circumflex
564 {0x00EB, VKEY_OEM_1}, // e diaeresis
565 {0x00EC, VKEY_OEM_6}, // i grave
566 {0x00EE, VKEY_OEM_6}, // i circumflex
567 {0x00F1, VKEY_OEM_3}, // n tilde
568 {0x00F2, VKEY_OEM_3}, // o grave
569 {0x00F5, VKEY_OEM_4}, // o tilde
570 {0x00F7, VKEY_OEM_6}, // division sign
571 {0x00FD, VKEY_7}, // y acute
572 {0x00FE, VKEY_OEM_MINUS}, // thorn
573 {0x0101, VKEY_OEM_8}, // a macron
574 {0x0107, VKEY_OEM_7}, // c acute
575 {0x010B, VKEY_OEM_3}, // c dot above
576 {0x0113, VKEY_W}, // e macron
577 {0x011B, VKEY_2}, // e caron
578 {0x011F, VKEY_OEM_6}, // g breve
579 {0x0121, VKEY_OEM_4}, // g dot above
580 {0x0127, VKEY_OEM_6}, // h stroke
581 {0x012B, VKEY_OEM_6}, // i macron
582 {0x0131, VKEY_OEM_1}, // dotless i
583 {0x0137, VKEY_OEM_5}, // k cedilla
584 {0x013C, VKEY_OEM_2}, // l cedilla
585 {0x013E, VKEY_2}, // l caron
586 {0x0146, VKEY_OEM_4}, // n cedilla
587 {0x0148, VKEY_OEM_5}, // n caron
588 {0x0151, VKEY_OEM_4}, // o double acute
589 {0x0159, VKEY_5}, // r caron
590 {0x0163, VKEY_OEM_7}, // t cedilla
591 {0x0165, VKEY_5}, // t caron
592 {0x016F, VKEY_OEM_1}, // u ring above
593 {0x0171, VKEY_OEM_5}, // u double acute
594 {0x01A1, VKEY_OEM_6}, // o horn
595 {0x01B0, VKEY_OEM_4}, // u horn
596 {0x01B6, VKEY_OEM_6}, // z stroke
597 {0x0259, VKEY_OEM_3}, // schwa
600 #if defined(OS_CHROMEOS)
601 void LoadKeymap(const std::string& layout_name,
602 scoped_refptr<base::SingleThreadTaskRunner> reply_runner,
603 const LoadKeymapCallback& reply_callback) {
604 std::string layout_id;
605 std::string layout_variant;
606 XkbKeyboardLayoutEngine::ParseLayoutName(layout_name, &layout_id,
607 &layout_variant);
608 xkb_rule_names names = {.rules = NULL,
609 .model = "pc101",
610 .layout = layout_id.c_str(),
611 .variant = layout_variant.c_str(),
612 .options = ""};
613 scoped_ptr<xkb_context, XkbContextDeleter> context;
614 context.reset(xkb_context_new(XKB_CONTEXT_NO_DEFAULT_INCLUDES));
615 xkb_context_include_path_append(context.get(), "/usr/share/X11/xkb");
616 scoped_ptr<xkb_keymap, XkbKeymapDeleter> keymap;
617 keymap.reset(xkb_keymap_new_from_names(context.get(), &names,
618 XKB_KEYMAP_COMPILE_NO_FLAGS));
619 if (keymap) {
620 scoped_ptr<char, base::FreeDeleter> keymap_str(
621 xkb_keymap_get_as_string(keymap.get(), XKB_KEYMAP_FORMAT_TEXT_V1));
622 reply_runner->PostTask(FROM_HERE, base::Bind(reply_callback, layout_name,
623 base::Passed(&keymap_str)));
624 } else {
625 LOG(FATAL) << "Keymap file failed to load: " << layout_name;
628 #endif
630 } // anonymous namespace
632 XkbKeyCodeConverter::XkbKeyCodeConverter() {
635 XkbKeyCodeConverter::~XkbKeyCodeConverter() {
638 XkbKeyboardLayoutEngine::XkbKeyboardLayoutEngine(
639 const XkbKeyCodeConverter& converter)
640 : key_code_converter_(converter), weak_ptr_factory_(this) {
641 // TODO: add XKB_CONTEXT_NO_ENVIRONMENT_NAMES
642 xkb_context_.reset(xkb_context_new(XKB_CONTEXT_NO_DEFAULT_INCLUDES));
643 xkb_context_include_path_append(xkb_context_.get(),
644 "/usr/share/X11/xkb");
647 XkbKeyboardLayoutEngine::~XkbKeyboardLayoutEngine() {
648 for (const auto& entry : xkb_keymaps_) {
649 xkb_keymap_unref(entry.keymap);
653 bool XkbKeyboardLayoutEngine::CanSetCurrentLayout() const {
654 #if defined(OS_CHROMEOS)
655 return true;
656 #else
657 return false;
658 #endif
661 bool XkbKeyboardLayoutEngine::SetCurrentLayoutByName(
662 const std::string& layout_name) {
663 #if defined(OS_CHROMEOS)
664 current_layout_name_ = layout_name;
665 for (const auto& entry : xkb_keymaps_) {
666 if (entry.layout_name == layout_name) {
667 SetKeymap(entry.keymap);
668 return true;
671 LoadKeymapCallback reply_callback = base::Bind(
672 &XkbKeyboardLayoutEngine::OnKeymapLoaded, weak_ptr_factory_.GetWeakPtr());
673 base::WorkerPool::PostTask(
674 FROM_HERE,
675 base::Bind(&LoadKeymap, layout_name, base::ThreadTaskRunnerHandle::Get(),
676 reply_callback),
677 true);
678 #else
679 xkb_keymap* keymap = xkb_map_new_from_string(
680 xkb_context_.get(), layout_name.c_str(), XKB_KEYMAP_FORMAT_TEXT_V1,
681 XKB_KEYMAP_COMPILE_NO_FLAGS);
682 if (!keymap)
683 return false;
684 SetKeymap(keymap);
685 #endif // defined(OS_CHROMEOS)
686 return true;
689 void XkbKeyboardLayoutEngine::OnKeymapLoaded(
690 const std::string& layout_name,
691 scoped_ptr<char, base::FreeDeleter> keymap_str) {
692 if (keymap_str) {
693 xkb_keymap* keymap = xkb_map_new_from_string(
694 xkb_context_.get(), keymap_str.get(), XKB_KEYMAP_FORMAT_TEXT_V1,
695 XKB_KEYMAP_COMPILE_NO_FLAGS);
696 XkbKeymapEntry entry = {layout_name, keymap};
697 xkb_keymaps_.push_back(entry);
698 if (layout_name == current_layout_name_)
699 SetKeymap(keymap);
700 } else {
701 LOG(FATAL) << "Keymap file failed to load: " << layout_name;
705 bool XkbKeyboardLayoutEngine::UsesISOLevel5Shift() const {
706 // NOTIMPLEMENTED();
707 return false;
710 bool XkbKeyboardLayoutEngine::UsesAltGr() const {
711 // NOTIMPLEMENTED();
712 return false;
715 bool XkbKeyboardLayoutEngine::Lookup(DomCode dom_code,
716 int flags,
717 DomKey* dom_key,
718 KeyboardCode* key_code) const {
719 if (dom_code == DomCode::NONE)
720 return false;
721 // Convert DOM physical key to XKB representation.
722 xkb_keycode_t xkb_keycode = key_code_converter_.DomCodeToXkbKeyCode(dom_code);
723 if (xkb_keycode == key_code_converter_.InvalidXkbKeyCode()) {
724 LOG(ERROR) << "No XKB keycode for DomCode 0x" << std::hex
725 << static_cast<int>(dom_code) << " '"
726 << KeycodeConverter::DomCodeToCodeString(dom_code) << "'";
727 return false;
729 xkb_mod_mask_t xkb_flags = EventFlagsToXkbFlags(flags);
730 // Obtain keysym and character.
731 xkb_keysym_t xkb_keysym;
732 uint32_t character = 0;
733 if (!XkbLookup(xkb_keycode, xkb_flags, &xkb_keysym, &character))
734 return false;
735 // Classify the keysym and convert to DOM and VKEY representations.
736 *dom_key = NonPrintableXKeySymToDomKey(xkb_keysym);
737 if (*dom_key == DomKey::NONE) {
738 *dom_key = DomKey::FromCharacter(character);
739 *key_code = AlphanumericKeyboardCode(character);
740 if (*key_code == VKEY_UNKNOWN) {
741 *key_code = DifficultKeyboardCode(dom_code, flags, xkb_keycode, xkb_flags,
742 xkb_keysym, character);
743 if (*key_code == VKEY_UNKNOWN)
744 *key_code = LocatedToNonLocatedKeyboardCode(
745 DomCodeToUsLayoutKeyboardCode(dom_code));
747 // If the Control key is down, only allow ASCII control characters to be
748 // returned, regardless of the key layout. crbug.com/450849
749 if ((flags & EF_CONTROL_DOWN) && (character >= 0x20))
750 *dom_key = DomKey::UNIDENTIFIED;
751 } else {
752 *key_code = NonPrintableDomKeyToKeyboardCode(*dom_key);
753 if (*key_code == VKEY_UNKNOWN) {
754 *key_code = LocatedToNonLocatedKeyboardCode(
755 DomCodeToUsLayoutKeyboardCode(dom_code));
758 return true;
761 void XkbKeyboardLayoutEngine::SetKeymap(xkb_keymap* keymap) {
762 xkb_state_.reset(xkb_state_new(keymap));
763 // Update flag map.
764 static const struct {
765 int ui_flag;
766 const char* xkb_name;
767 } flags[] = {{ui::EF_CONTROL_DOWN, XKB_MOD_NAME_CTRL},
768 {ui::EF_SHIFT_DOWN, XKB_MOD_NAME_SHIFT},
769 {ui::EF_ALT_DOWN, XKB_MOD_NAME_ALT},
770 {ui::EF_CAPS_LOCK_DOWN, XKB_MOD_NAME_CAPS},
771 {ui::EF_COMMAND_DOWN, XKB_MOD_NAME_LOGO},
772 {ui::EF_MOD3_DOWN, "Mod3"},
773 {ui::EF_ALTGR_DOWN, "Mod5"}};
774 xkb_flag_map_.clear();
775 xkb_flag_map_.resize(arraysize(flags));
776 for (size_t i = 0; i < arraysize(flags); ++i) {
777 xkb_mod_index_t index = xkb_keymap_mod_get_index(keymap, flags[i].xkb_name);
778 if (index == XKB_MOD_INVALID) {
779 DVLOG(3) << "XKB keyboard layout does not contain " << flags[i].xkb_name;
780 } else {
781 xkb_mod_mask_t flag = static_cast<xkb_mod_mask_t>(1) << index;
782 XkbFlagMapEntry e = {flags[i].ui_flag, flag};
783 xkb_flag_map_.push_back(e);
787 // Update num lock mask.
788 num_lock_mod_mask_ = 0;
789 xkb_mod_index_t num_mod_index =
790 xkb_keymap_mod_get_index(keymap, XKB_MOD_NAME_NUM);
791 if (num_mod_index != XKB_MOD_INVALID)
792 num_lock_mod_mask_ = static_cast<xkb_mod_mask_t>(1) << num_mod_index;
795 xkb_mod_mask_t XkbKeyboardLayoutEngine::EventFlagsToXkbFlags(
796 int ui_flags) const {
797 xkb_mod_mask_t xkb_flags = 0;
798 for (const auto& entry : xkb_flag_map_) {
799 if (ui_flags & entry.ui_flag)
800 xkb_flags |= entry.xkb_flag;
802 // NumLock is always on.
803 xkb_flags |= num_lock_mod_mask_;
804 return xkb_flags;
807 bool XkbKeyboardLayoutEngine::XkbLookup(xkb_keycode_t xkb_keycode,
808 xkb_mod_mask_t xkb_flags,
809 xkb_keysym_t* xkb_keysym,
810 uint32_t* character) const {
811 if (!xkb_state_) {
812 LOG(ERROR) << "No current XKB state";
813 return false;
815 xkb_state_update_mask(xkb_state_.get(), xkb_flags, 0, 0, 0, 0, 0);
816 *xkb_keysym = xkb_state_key_get_one_sym(xkb_state_.get(), xkb_keycode);
817 if (*xkb_keysym == XKB_KEY_NoSymbol)
818 return false;
819 *character = xkb_state_key_get_utf32(xkb_state_.get(), xkb_keycode);
820 DLOG_IF(ERROR, *character != (*character & 0xFFFF))
821 << "Non-BMP character:" << *character;
822 return true;
825 KeyboardCode XkbKeyboardLayoutEngine::DifficultKeyboardCode(
826 DomCode dom_code,
827 int ui_flags,
828 xkb_keycode_t xkb_keycode,
829 xkb_mod_mask_t xkb_flags,
830 xkb_keysym_t xkb_keysym,
831 base::char16 character) const {
832 // Get the layout interpretation without modifiers, so that
833 // e.g. Ctrl+D correctly generates VKEY_D.
834 xkb_keysym_t plain_keysym;
835 uint32_t plain_character;
836 if (!XkbLookup(xkb_keycode, 0, &plain_keysym, &plain_character))
837 return VKEY_UNKNOWN;
839 // If the plain key is non-printable, that determines the VKEY.
840 DomKey plain_key = NonPrintableXKeySymToDomKey(plain_keysym);
841 if (plain_key != ui::DomKey::NONE)
842 return NonPrintableDomKeyToKeyboardCode(plain_key);
844 // Plain ASCII letters and digits map directly to VKEY values.
845 KeyboardCode key_code = AlphanumericKeyboardCode(plain_character);
846 if (key_code != VKEY_UNKNOWN)
847 return key_code;
849 // Check the multi-character tables.
850 const PrintableMultiEntry* multi_end = kMultiMap + arraysize(kMultiMap);
851 const PrintableMultiEntry* multi =
852 std::lower_bound(kMultiMap, multi_end, plain_character,
853 [](const PrintableMultiEntry& e, base::char16 c) {
854 return e.plain_character < c;
856 if ((multi != multi_end) && (multi->plain_character == plain_character)) {
857 const base::char16 kNonCharacter = kAny;
858 base::char16 shift_character = kNonCharacter;
859 base::char16 altgr_character = kNonCharacter;
860 for (size_t i = 0; i < multi->subtable_size; ++i) {
861 if (multi->subtable[i].dom_code != dom_code)
862 continue;
863 if (multi->subtable[i].test_shift) {
864 if (shift_character == kNonCharacter) {
865 shift_character = XkbSubCharacter(xkb_keycode, xkb_flags, character,
866 ui::EF_SHIFT_DOWN);
868 if (shift_character != multi->subtable[i].shift_character)
869 continue;
871 if (multi->subtable[i].test_altgr) {
872 if (altgr_character == kNonCharacter) {
873 altgr_character = XkbSubCharacter(xkb_keycode, xkb_flags, character,
874 ui::EF_ALTGR_DOWN);
876 if (altgr_character != multi->subtable[i].altgr_character)
877 continue;
879 return multi->subtable[i].key_code;
883 // Check the simple character table.
884 const PrintableSimpleEntry* simple_end = kSimpleMap + arraysize(kSimpleMap);
885 const PrintableSimpleEntry* simple =
886 std::lower_bound(kSimpleMap, simple_end, plain_character,
887 [](const PrintableSimpleEntry& e, base::char16 c) {
888 return e.plain_character < c;
890 if ((simple != simple_end) && (simple->plain_character == plain_character))
891 return simple->key_code;
893 return VKEY_UNKNOWN;
896 base::char16 XkbKeyboardLayoutEngine::XkbSubCharacter(
897 xkb_keycode_t xkb_keycode,
898 xkb_mod_mask_t base_flags,
899 base::char16 base_character,
900 int ui_flags) const {
901 xkb_mod_mask_t flags = EventFlagsToXkbFlags(ui_flags);
902 if (flags == base_flags)
903 return base_character;
904 xkb_keysym_t keysym;
905 uint32_t character = 0;
906 if (!XkbLookup(xkb_keycode, flags, &keysym, &character))
907 character = kNone;
908 return character;
911 void XkbKeyboardLayoutEngine::ParseLayoutName(const std::string& layout_name,
912 std::string* layout_id,
913 std::string* layout_variant) {
914 size_t dash_index = layout_name.find('-');
915 size_t parentheses_index = layout_name.find('(');
916 *layout_id = layout_name;
917 *layout_variant = "";
918 if (parentheses_index != std::string::npos) {
919 *layout_id = layout_name.substr(0, parentheses_index);
920 size_t close_index = layout_name.find(')', parentheses_index);
921 if (close_index == std::string::npos)
922 close_index = layout_name.size();
923 *layout_variant = layout_name.substr(parentheses_index + 1,
924 close_index - parentheses_index - 1);
925 } else if (dash_index != std::string::npos) {
926 *layout_id = layout_name.substr(0, dash_index);
927 *layout_variant = layout_name.substr(dash_index + 1);
931 } // namespace ui