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>
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"
22 #include "ui/events/ozone/layout/xkb/xkb_keyboard_code_conversion.h"
28 typedef base::Callback
<void(const std::string
&,
29 scoped_ptr
<char, base::FreeDeleter
>)>
32 KeyboardCode
AlphanumericKeyboardCode(base::char16 character
) {
33 // Plain ASCII letters and digits map directly to VKEY values.
34 if ((character
>= '0') && (character
<= '9'))
35 return static_cast<KeyboardCode
>(VKEY_0
+ character
- '0');
36 if ((character
>= 'a') && (character
<= 'z'))
37 return static_cast<KeyboardCode
>(VKEY_A
+ character
- 'a');
38 if ((character
>= 'A') && (character
<= 'Z'))
39 return static_cast<KeyboardCode
>(VKEY_A
+ character
- 'A');
43 // These tables map layout-dependent printable characters/codes
44 // to legacy Windows-based VKEY values.
46 // VKEYs are determined by the character produced from a DomCode without
47 // any modifiers, plus zero or more of the DomCode itself, the character
48 // produced with the Shift modifier, and the character produced with the
51 // A table of one or more PrintableSubEntry cases applies when the VKEY is
52 // not determined by the unmodified character value alone. Each such table
53 // corresponds to one unmodified character value. For an entry to match,
54 // the dom_code must match, and, if test_X is set, then the character for
55 // the key plus modifier X must also match.
56 struct PrintableSubEntry
{
60 base::char16 shift_character
;
61 base::char16 altgr_character
;
62 KeyboardCode key_code
;
65 const base::char16 kNone
= 0xFFFE;
66 const base::char16 kAny
= 0xFFFF;
68 // U+0021 exclamation mark
69 const PrintableSubEntry kU0021
[] = {
70 {DomCode::DIGIT1
, 0, 0, kAny
, kAny
, VKEY_1
},
71 {DomCode::DIGIT8
, 0, 0, kAny
, kAny
, VKEY_8
},
72 {DomCode::SLASH
, 0, 0, kAny
, kAny
, VKEY_OEM_8
}};
75 const PrintableSubEntry kU0022
[] = {
76 {DomCode::DIGIT2
, 0, 0, kAny
, kAny
, VKEY_2
},
77 {DomCode::DIGIT3
, 0, 0, kAny
, kAny
, VKEY_3
}};
80 const PrintableSubEntry kU0023
[] = {
81 {DomCode::BACKQUOTE
, 0, 0, kAny
, kAny
, VKEY_OEM_7
},
82 {DomCode::BACKSLASH
, 1, 0, 0x0027, kAny
, VKEY_OEM_2
}, // apostrophe
83 {DomCode::BACKSLASH
, 1, 0, 0x007E, kAny
, VKEY_OEM_7
}}; // ~, NoSymbol
86 const PrintableSubEntry kU0024
[] = {
87 {DomCode::BRACKET_RIGHT
, 0, 0, kAny
, kAny
, VKEY_OEM_1
},
88 {DomCode::BACKSLASH
, 0, 0, kAny
, kAny
, VKEY_OEM_8
}};
91 const PrintableSubEntry kU0027
[] = {
92 {DomCode::DIGIT4
, 0, 0, kAny
, kAny
, VKEY_4
},
93 {DomCode::KEY_Q
, 0, 0, kAny
, kAny
, VKEY_OEM_7
},
94 {DomCode::BRACKET_RIGHT
, 0, 0, kAny
, kAny
, VKEY_OEM_1
},
95 {DomCode::SLASH
, 0, 0, kAny
, kAny
, VKEY_OEM_7
},
96 {DomCode::QUOTE
, 1, 0, 0x0022, kAny
, VKEY_OEM_7
}, // quote
97 {DomCode::BACKQUOTE
, 1, 0, 0x0022, kAny
, VKEY_OEM_3
}, // quote
98 {DomCode::BACKQUOTE
, 1, 0, 0x00B7, kAny
, VKEY_OEM_5
}, // middle dot
99 {DomCode::BACKSLASH
, 1, 0, kNone
, kAny
, VKEY_OEM_5
}, // NoSymbol
100 {DomCode::MINUS
, 1, 0, 0x003F, kAny
, VKEY_OEM_4
}, // ?
101 {DomCode::EQUAL
, 1, 0, 0x002A, kAny
, VKEY_OEM_PLUS
}, // *
102 {DomCode::QUOTE
, 1, 0, 0x0040, kAny
, VKEY_OEM_3
}, // @
103 {DomCode::BACKSLASH
, 1, 1, 0x002A, 0x00BD, VKEY_OEM_5
}, // *, one half
104 {DomCode::BACKSLASH
, 1, 0, 0x002A, kAny
, VKEY_OEM_2
}, // *, NoSymbol
105 {DomCode::KEY_Z
, 1, 1, 0x0022, 0x0158, VKEY_OEM_7
}, // quote, R caron
106 {DomCode::KEY_Z
, 1, 0, 0x0022, kAny
, VKEY_Z
}}; // quote
108 // U+0028 left parenthesis
109 const PrintableSubEntry kU0028
[] = {
110 {DomCode::DIGIT5
, 0, 0, kAny
, kAny
, VKEY_5
},
111 {DomCode::DIGIT9
, 0, 0, kAny
, kAny
, VKEY_9
},
112 {DomCode::BACKSLASH
, 0, 0, kAny
, kAny
, VKEY_OEM_5
}};
114 // U+0029 right parenthesis
115 const PrintableSubEntry kU0029
[] = {
116 {DomCode::DIGIT0
, 0, 0, kAny
, kAny
, VKEY_0
},
117 {DomCode::MINUS
, 0, 0, kAny
, kAny
, VKEY_OEM_4
},
118 {DomCode::BRACKET_RIGHT
, 0, 0, kAny
, kAny
, VKEY_OEM_6
}};
121 const PrintableSubEntry kU002A
[] = {
122 {DomCode::BRACKET_RIGHT
, 0, 0, kAny
, kAny
, VKEY_OEM_1
},
123 {DomCode::BACKSLASH
, 0, 0, kAny
, kAny
, VKEY_OEM_5
}};
126 const PrintableSubEntry kU002B
[] = {
127 {DomCode::DIGIT1
, 0, 0, kAny
, kAny
, VKEY_1
},
128 {DomCode::EQUAL
, 0, 0, kAny
, kAny
, VKEY_OEM_PLUS
},
129 {DomCode::BRACKET_LEFT
, 0, 0, kAny
, kAny
, VKEY_OEM_PLUS
},
130 {DomCode::BRACKET_RIGHT
, 0, 0, kAny
, kAny
, VKEY_OEM_PLUS
},
131 {DomCode::SEMICOLON
, 0, 0, kAny
, kAny
, VKEY_OEM_PLUS
},
132 {DomCode::BACKSLASH
, 0, 0, kAny
, kAny
, VKEY_OEM_2
},
133 {DomCode::MINUS
, 1, 1, 0x003F, 0x005C, VKEY_OEM_MINUS
}, // ?, backslash
134 {DomCode::MINUS
, 1, 0, 0x003F, kAny
, VKEY_OEM_PLUS
}}; // ?
137 const PrintableSubEntry kU002C
[] = {
138 {DomCode::DIGIT3
, 0, 0, kAny
, kAny
, VKEY_3
},
139 {DomCode::DIGIT5
, 0, 0, kAny
, kAny
, VKEY_5
},
140 {DomCode::DIGIT6
, 0, 0, kAny
, kAny
, VKEY_6
},
141 {DomCode::DIGIT9
, 0, 0, kAny
, kAny
, VKEY_9
},
142 {DomCode::KEY_W
, 0, 0, kAny
, kAny
, VKEY_OEM_COMMA
},
143 {DomCode::KEY_V
, 0, 0, kAny
, kAny
, VKEY_OEM_COMMA
},
144 {DomCode::KEY_M
, 0, 0, kAny
, kAny
, VKEY_OEM_COMMA
},
145 {DomCode::COMMA
, 0, 0, kAny
, kAny
, VKEY_OEM_COMMA
}};
147 // U+002D hyphen-minus
148 const PrintableSubEntry kU002D
[] = {
149 {DomCode::DIGIT2
, 0, 0, kAny
, kAny
, VKEY_2
},
150 {DomCode::DIGIT6
, 0, 0, kAny
, kAny
, VKEY_6
},
151 {DomCode::MINUS
, 0, 0, kAny
, kAny
, VKEY_OEM_MINUS
},
152 {DomCode::KEY_A
, 0, 0, kAny
, kAny
, VKEY_OEM_MINUS
},
153 {DomCode::QUOTE
, 0, 0, kAny
, kAny
, VKEY_OEM_MINUS
},
154 {DomCode::SLASH
, 1, 0, 0x003D, kAny
, VKEY_OEM_MINUS
}, // =
155 {DomCode::EQUAL
, 1, 1, 0x005F, 0x0157, VKEY_OEM_4
}, // _, r cedilla
156 {DomCode::EQUAL
, 1, 0, 0x005F, kAny
, VKEY_OEM_MINUS
}, // _
157 {DomCode::SLASH
, 1, 1, 0x005F, 0x002F, VKEY_OEM_2
}, // _, /
158 {DomCode::SLASH
, 1, 0, 0x005F, kAny
, VKEY_OEM_MINUS
}}; // _
161 const PrintableSubEntry kU002E
[] = {
162 {DomCode::DIGIT7
, 0, 0, kAny
, kAny
, VKEY_7
},
163 {DomCode::DIGIT8
, 0, 0, kAny
, kAny
, VKEY_8
},
164 {DomCode::KEY_E
, 0, 0, kAny
, kAny
, VKEY_OEM_PERIOD
},
165 {DomCode::KEY_R
, 0, 0, kAny
, kAny
, VKEY_OEM_PERIOD
},
166 {DomCode::KEY_O
, 0, 0, kAny
, kAny
, VKEY_OEM_PERIOD
},
167 {DomCode::QUOTE
, 0, 0, kAny
, kAny
, VKEY_OEM_7
},
168 {DomCode::PERIOD
, 0, 0, kAny
, kAny
, VKEY_OEM_PERIOD
},
169 {DomCode::SLASH
, 0, 0, kAny
, kAny
, VKEY_OEM_2
}};
172 const PrintableSubEntry kU002F
[] = {
173 {DomCode::MINUS
, 0, 0, kAny
, kAny
, VKEY_OEM_4
},
174 {DomCode::BRACKET_LEFT
, 0, 0, kAny
, kAny
, VKEY_OEM_2
},
175 {DomCode::BACKQUOTE
, 0, 0, kAny
, kAny
, VKEY_OEM_7
},
176 {DomCode::BACKSLASH
, 0, 0, kAny
, kAny
, VKEY_OEM_5
},
177 {DomCode::SLASH
, 0, 0, kAny
, kAny
, VKEY_OEM_2
},
178 {DomCode::DIGIT3
, 1, 0, 0x0033, kAny
, VKEY_3
}, // 3
179 {DomCode::DIGIT3
, 1, 0, 0x003F, kAny
, VKEY_OEM_2
}, // ?
180 {DomCode::DIGIT0
, 1, 0, 0x0030, kAny
, VKEY_0
}, // 0
181 {DomCode::DIGIT0
, 1, 0, 0x003F, kAny
, VKEY_OEM_2
}}; // ?
184 const PrintableSubEntry kU003A
[] = {
185 {DomCode::DIGIT1
, 0, 0, kAny
, kAny
, VKEY_1
},
186 {DomCode::DIGIT5
, 0, 0, kAny
, kAny
, VKEY_5
},
187 {DomCode::DIGIT6
, 0, 0, kAny
, kAny
, VKEY_6
},
188 {DomCode::PERIOD
, 0, 0, kAny
, kAny
, VKEY_OEM_2
}};
191 const PrintableSubEntry kU003B
[] = {
192 {DomCode::DIGIT4
, 0, 0, kAny
, kAny
, VKEY_4
},
193 {DomCode::DIGIT8
, 0, 0, kAny
, kAny
, VKEY_8
},
194 {DomCode::KEY_Q
, 0, 0, kAny
, kAny
, VKEY_OEM_1
},
195 {DomCode::BRACKET_LEFT
, 0, 0, kAny
, kAny
, VKEY_OEM_1
},
196 {DomCode::BRACKET_RIGHT
, 0, 0, kAny
, kAny
, VKEY_OEM_6
},
197 {DomCode::SEMICOLON
, 0, 0, kAny
, kAny
, VKEY_OEM_1
},
198 {DomCode::BACKQUOTE
, 0, 0, kAny
, kAny
, VKEY_OEM_3
},
199 {DomCode::KEY_Z
, 0, 0, kAny
, kAny
, VKEY_OEM_1
},
200 {DomCode::COMMA
, 0, 0, kAny
, kAny
, VKEY_OEM_PERIOD
},
201 {DomCode::SLASH
, 0, 0, kAny
, kAny
, VKEY_OEM_2
}};
203 const PrintableSubEntry kU003D
[] = {
204 {DomCode::DIGIT8
, 0, 0, kAny
, kAny
, VKEY_8
},
205 {DomCode::EQUAL
, 0, 0, kAny
, kAny
, VKEY_OEM_PLUS
},
206 {DomCode::BRACKET_RIGHT
, 0, 0, kAny
, kAny
, VKEY_OEM_PLUS
},
207 {DomCode::SLASH
, 1, 0, 0x0025, kAny
, VKEY_OEM_8
}, // %
208 {DomCode::SLASH
, 1, 0, 0x002B, kAny
, VKEY_OEM_PLUS
}, // +
209 {DomCode::MINUS
, 1, 1, 0x0025, 0x002D, VKEY_OEM_MINUS
}, // %, -
210 {DomCode::MINUS
, 1, 0, 0x0025, kAny
, VKEY_OEM_PLUS
}}; // %, NoSymbol
213 const PrintableSubEntry kU003F
[] = {
214 {DomCode::DIGIT2
, 0, 0, kAny
, kAny
, VKEY_2
},
215 {DomCode::DIGIT7
, 0, 0, kAny
, kAny
, VKEY_7
},
216 {DomCode::DIGIT8
, 0, 0, kAny
, kAny
, VKEY_8
},
217 {DomCode::MINUS
, 0, 0, kAny
, kAny
, VKEY_OEM_PLUS
}};
220 const PrintableSubEntry kU0040
[] = {
221 {DomCode::BRACKET_RIGHT
, 0, 0, kAny
, kAny
, VKEY_OEM_6
},
222 {DomCode::BACKQUOTE
, 0, 0, kAny
, kAny
, VKEY_OEM_7
}};
224 // U+005B left square bracket
225 const PrintableSubEntry kU005B
[] = {
226 {DomCode::DIGIT1
, 0, 0, kAny
, kAny
, VKEY_OEM_4
},
227 {DomCode::MINUS
, 0, 0, kAny
, kAny
, VKEY_OEM_4
},
228 {DomCode::BRACKET_LEFT
, 0, 0, kAny
, kAny
, VKEY_OEM_4
},
229 {DomCode::BRACKET_RIGHT
, 0, 0, kAny
, kAny
, VKEY_OEM_6
},
230 {DomCode::QUOTE
, 0, 0, kAny
, kAny
, VKEY_OEM_7
}};
233 const PrintableSubEntry kU005C
[] = {
234 {DomCode::EQUAL
, 0, 0, kAny
, kAny
, VKEY_OEM_4
},
235 {DomCode::BACKSLASH
, 1, 0, 0x002F, kAny
, VKEY_OEM_7
}, // /
236 {DomCode::BACKSLASH
, 1, 0, 0x007C, kAny
, VKEY_OEM_5
}, // |
237 {DomCode::BACKQUOTE
, 1, 1, 0x007C, 0x0031, VKEY_OEM_5
}, // |, 1
238 {DomCode::BACKQUOTE
, 1, 1, 0x007C, 0x0145, VKEY_OEM_3
}}; // |, N cedilla
240 // U+005D right square bracket
241 const PrintableSubEntry kU005D
[] = {
242 {DomCode::DIGIT2
, 0, 0, kAny
, kAny
, VKEY_OEM_6
},
243 {DomCode::EQUAL
, 0, 0, kAny
, kAny
, VKEY_OEM_6
},
244 {DomCode::BRACKET_RIGHT
, 0, 0, kAny
, kAny
, VKEY_OEM_6
},
245 {DomCode::BACKQUOTE
, 0, 0, kAny
, kAny
, VKEY_OEM_3
},
246 {DomCode::BACKSLASH
, 0, 0, kAny
, kAny
, VKEY_OEM_5
}};
249 const PrintableSubEntry kU005F
[] = {
250 {DomCode::DIGIT8
, 0, 0, kAny
, kAny
, VKEY_8
},
251 {DomCode::MINUS
, 0, 0, kAny
, kAny
, VKEY_OEM_MINUS
}};
253 // U+0060 grave accent
254 const PrintableSubEntry kU0060
[] = {
255 {DomCode::BACKQUOTE
, 1, 0, kNone
, kAny
, VKEY_OEM_3
}, // NoSymbol
256 {DomCode::BACKQUOTE
, 1, 0, 0x00AC, kAny
, VKEY_OEM_8
}, // not
257 {DomCode::BACKQUOTE
, 1, 0, 0x007E, kAny
, VKEY_OEM_3
}}; // ~
260 const PrintableSubEntry kU00A7
[] = {
261 {DomCode::DIGIT4
, 0, 0, kAny
, kAny
, VKEY_4
},
262 {DomCode::DIGIT6
, 0, 0, kAny
, kAny
, VKEY_6
},
263 {DomCode::QUOTE
, 0, 0, kAny
, kAny
, VKEY_OEM_7
},
264 {DomCode::BACKQUOTE
, 1, 0, 0x00B0, kAny
, VKEY_OEM_2
}, // degree
265 {DomCode::BACKQUOTE
, 1, 0, 0x00BD, kAny
, VKEY_OEM_5
}}; // one half
267 // U+00AB left-pointing double angle quote
268 const PrintableSubEntry kU00AB
[] = {
269 {DomCode::DIGIT8
, 0, 0, kAny
, kAny
, VKEY_8
},
270 {DomCode::EQUAL
, 0, 0, kAny
, kAny
, VKEY_OEM_6
}};
273 const PrintableSubEntry kU00B0
[] = {
274 {DomCode::EQUAL
, 0, 0, kAny
, kAny
, VKEY_OEM_2
},
275 {DomCode::BACKQUOTE
, 0, 0, kAny
, kAny
, VKEY_OEM_7
}};
277 // U+00BA masculine ordinal indicator
278 const PrintableSubEntry kU00BA
[] = {
279 {DomCode::QUOTE
, 0, 0, kAny
, kAny
, VKEY_OEM_7
},
280 {DomCode::BACKQUOTE
, 0, 0, kAny
, kAny
, VKEY_OEM_5
}};
283 const PrintableSubEntry kU00E0
[] = {
284 {DomCode::DIGIT0
, 0, 0, kAny
, kAny
, VKEY_0
},
285 {DomCode::BACKSLASH
, 0, 0, kAny
, kAny
, VKEY_OEM_5
},
286 {DomCode::QUOTE
, 1, 0, 0x00B0, kAny
, VKEY_OEM_7
}, // degree
287 {DomCode::QUOTE
, 1, 0, 0x00E4, kAny
, VKEY_OEM_5
}}; // a diaeresis
290 const PrintableSubEntry kU00E1
[] = {
291 {DomCode::DIGIT8
, 0, 0, kAny
, kAny
, VKEY_8
},
292 {DomCode::QUOTE
, 0, 0, kAny
, kAny
, VKEY_OEM_7
}};
294 // U+00E2 a circumflex
295 const PrintableSubEntry kU00E2
[] = {
296 {DomCode::DIGIT2
, 0, 0, kAny
, kAny
, VKEY_2
},
297 {DomCode::BACKSLASH
, 0, 0, kAny
, kAny
, VKEY_OEM_5
}};
299 // U+00E4 a diaeresis
300 const PrintableSubEntry kU00E4
[] = {
301 {DomCode::BRACKET_RIGHT
, 0, 0, kAny
, kAny
, VKEY_OEM_6
},
302 {DomCode::QUOTE
, 1, 0, 0x00E0, kAny
, VKEY_OEM_5
}, // a grave
303 {DomCode::QUOTE
, 1, 0, 0x00C4, kAny
, VKEY_OEM_7
}}; // A dia.
306 const PrintableSubEntry kU00E6
[] = {
307 {DomCode::SEMICOLON
, 0, 0, kAny
, kAny
, VKEY_OEM_3
},
308 {DomCode::QUOTE
, 0, 0, kAny
, kAny
, VKEY_OEM_7
}};
311 const PrintableSubEntry kU00E7
[] = {
312 {DomCode::DIGIT9
, 0, 0, kAny
, kAny
, VKEY_9
},
313 {DomCode::BRACKET_LEFT
, 0, 0, kAny
, kAny
, VKEY_OEM_4
},
314 {DomCode::BRACKET_RIGHT
, 0, 0, kAny
, kAny
, VKEY_OEM_6
},
315 {DomCode::QUOTE
, 0, 0, kAny
, kAny
, VKEY_OEM_7
},
316 {DomCode::BACKSLASH
, 0, 0, kAny
, kAny
, VKEY_OEM_2
},
317 {DomCode::COMMA
, 0, 0, kAny
, kAny
, VKEY_OEM_COMMA
},
318 {DomCode::SEMICOLON
, 1, 1, 0x00C7, 0x00DE, VKEY_OEM_3
}, // C ced., Thorn
319 {DomCode::SEMICOLON
, 1, 0, 0x00C7, kAny
, VKEY_OEM_1
}}; // C ced., NoSy
322 const PrintableSubEntry kU00E8
[] = {
323 {DomCode::DIGIT7
, 0, 0, kAny
, kAny
, VKEY_7
},
324 {DomCode::BRACKET_LEFT
, 0, 0, kAny
, kAny
, VKEY_OEM_1
},
325 {DomCode::QUOTE
, 0, 0, kAny
, kAny
, VKEY_OEM_3
}};
328 const PrintableSubEntry kU00E9
[] = {
329 {DomCode::DIGIT2
, 0, 0, kAny
, kAny
, VKEY_2
},
330 {DomCode::DIGIT0
, 0, 0, kAny
, kAny
, VKEY_0
},
331 {DomCode::SLASH
, 0, 0, kAny
, kAny
, VKEY_OEM_2
},
332 {DomCode::SEMICOLON
, 1, 0, 0x00C9, kAny
, VKEY_OEM_1
}, // E acute
333 {DomCode::SEMICOLON
, 1, 0, 0x00F6, kAny
, VKEY_OEM_7
}}; // o diaeresis
336 const PrintableSubEntry kU00ED
[] = {
337 {DomCode::DIGIT9
, 0, 0, kAny
, kAny
, VKEY_9
},
338 {DomCode::BACKQUOTE
, 0, 0, kAny
, kAny
, VKEY_0
}};
341 const PrintableSubEntry kU00F0
[] = {
342 {DomCode::BRACKET_LEFT
, 0, 0, kAny
, kAny
, VKEY_OEM_6
},
343 {DomCode::BRACKET_RIGHT
, 0, 0, kAny
, kAny
, VKEY_OEM_1
}};
346 const PrintableSubEntry kU00F3
[] = {
347 {DomCode::EQUAL
, 0, 0, kAny
, kAny
, VKEY_OEM_PLUS
},
348 {DomCode::BACKSLASH
, 0, 0, kAny
, kAny
, VKEY_OEM_5
}};
350 // U+00F4 o circumflex
351 const PrintableSubEntry kU00F4
[] = {
352 {DomCode::DIGIT4
, 0, 0, kAny
, kAny
, VKEY_4
},
353 {DomCode::SEMICOLON
, 0, 0, kAny
, kAny
, VKEY_OEM_1
}};
355 // U+00F6 o diaeresis
356 const PrintableSubEntry kU00F6
[] = {
357 {DomCode::DIGIT0
, 0, 0, kAny
, kAny
, VKEY_OEM_3
},
358 {DomCode::MINUS
, 0, 0, kAny
, kAny
, VKEY_OEM_PLUS
},
359 {DomCode::BRACKET_LEFT
, 0, 0, kAny
, kAny
, VKEY_OEM_4
},
360 {DomCode::SEMICOLON
, 1, 0, 0x00E9, kAny
, VKEY_OEM_7
}, // e acute
361 {DomCode::SEMICOLON
, 1, 1, 0x00D6, 0x0162, VKEY_OEM_3
}, // O dia., T ced.
362 {DomCode::SEMICOLON
, 1, 0, 0x00D6, kAny
, VKEY_OEM_3
}}; // O diaresis
365 const PrintableSubEntry kU00F8
[] = {
366 {DomCode::SEMICOLON
, 0, 0, kAny
, kAny
, VKEY_OEM_3
},
367 {DomCode::QUOTE
, 0, 0, kAny
, kAny
, VKEY_OEM_7
}};
370 const PrintableSubEntry kU00F9
[] = {
371 {DomCode::QUOTE
, 0, 0, kAny
, kAny
, VKEY_OEM_3
},
372 {DomCode::BACKSLASH
, 0, 0, kAny
, kAny
, VKEY_OEM_2
}};
375 const PrintableSubEntry kU00FA
[] = {
376 {DomCode::BRACKET_LEFT
, 0, 0, kAny
, kAny
, VKEY_OEM_4
},
377 {DomCode::BRACKET_RIGHT
, 0, 0, kAny
, kAny
, VKEY_OEM_6
}};
379 // U+00FC u diaeresis
380 const PrintableSubEntry kU00FC
[] = {
381 {DomCode::KEY_W
, 0, 0, kAny
, kAny
, VKEY_W
},
382 {DomCode::BRACKET_LEFT
, 1, 0, 0x00E8, kAny
, VKEY_OEM_1
}, // e grave
383 {DomCode::MINUS
, 1, 0, 0x00DC, kAny
, VKEY_OEM_2
}, // U diaresis
384 {DomCode::BRACKET_LEFT
, 1, 1, 0x00DC, 0x0141, VKEY_OEM_3
}, // U dia., L-
385 {DomCode::BRACKET_LEFT
, 1, 0, 0x00DC, kAny
, VKEY_OEM_1
}}; // U diaresis
388 const PrintableSubEntry kU0103
[] = {
389 {DomCode::DIGIT1
, 0, 0, kAny
, kAny
, VKEY_1
},
390 {DomCode::BRACKET_LEFT
, 0, 0, kAny
, kAny
, VKEY_OEM_4
}};
393 const PrintableSubEntry kU0105
[] = {
394 {DomCode::DIGIT1
, 0, 0, kAny
, kAny
, VKEY_1
},
395 {DomCode::KEY_Q
, 0, 0, kAny
, kAny
, VKEY_Q
},
396 {DomCode::QUOTE
, 0, 0, kAny
, kAny
, VKEY_OEM_7
}};
399 const PrintableSubEntry kU010D
[] = {
400 {DomCode::DIGIT2
, 0, 0, kAny
, kAny
, VKEY_2
},
401 {DomCode::DIGIT4
, 0, 0, kAny
, kAny
, VKEY_4
},
402 {DomCode::KEY_P
, 0, 0, kAny
, kAny
, VKEY_X
},
403 {DomCode::SEMICOLON
, 0, 0, kAny
, kAny
, VKEY_OEM_1
},
404 {DomCode::COMMA
, 0, 0, kAny
, kAny
, VKEY_OEM_COMMA
}};
407 const PrintableSubEntry kU0111
[] = {
408 {DomCode::DIGIT0
, 0, 0, kAny
, kAny
, VKEY_0
},
409 {DomCode::BRACKET_RIGHT
, 0, 0, kAny
, kAny
, VKEY_OEM_6
}};
411 // U+0117 e dot above
412 const PrintableSubEntry kU0117
[] = {
413 {DomCode::DIGIT4
, 0, 0, kAny
, kAny
, VKEY_4
},
414 {DomCode::QUOTE
, 0, 0, kAny
, kAny
, VKEY_OEM_7
}};
417 const PrintableSubEntry kU0119
[] = {
418 {DomCode::DIGIT3
, 0, 0, kAny
, kAny
, VKEY_3
},
419 {DomCode::SLASH
, 1, 1, 0x0118, 0x006E, VKEY_OEM_2
}, // E ogonek, n
420 {DomCode::SLASH
, 1, 0, 0x0118, kAny
, VKEY_OEM_MINUS
}}; // E ogonek
423 const PrintableSubEntry kU012F
[] = {
424 {DomCode::DIGIT5
, 0, 0, kAny
, kAny
, VKEY_5
},
425 {DomCode::BRACKET_LEFT
, 1, 0, 0x012E, kAny
, VKEY_OEM_4
}}; // Iogonek
428 const PrintableSubEntry kU0142
[] = {
429 {DomCode::SEMICOLON
, 0, 0, kAny
, kAny
, VKEY_OEM_1
},
430 {DomCode::BACKSLASH
, 0, 0, kAny
, kAny
, VKEY_OEM_2
}};
433 const PrintableSubEntry kU015F
[] = {
434 {DomCode::SEMICOLON
, 0, 0, kAny
, kAny
, VKEY_OEM_1
},
435 {DomCode::PERIOD
, 0, 0, kAny
, kAny
, VKEY_OEM_PERIOD
}};
438 const PrintableSubEntry kU0161
[] = {
439 {DomCode::DIGIT3
, 0, 0, kAny
, kAny
, VKEY_3
},
440 {DomCode::DIGIT6
, 0, 0, kAny
, kAny
, VKEY_6
},
441 {DomCode::BRACKET_LEFT
, 0, 0, kAny
, kAny
, VKEY_OEM_4
},
442 {DomCode::KEY_A
, 0, 0, kAny
, kAny
, VKEY_OEM_1
},
443 {DomCode::KEY_F
, 0, 0, kAny
, kAny
, VKEY_F
},
444 {DomCode::PERIOD
, 0, 0, kAny
, kAny
, VKEY_OEM_PERIOD
}};
447 const PrintableSubEntry kU016B
[] = {
448 {DomCode::DIGIT8
, 0, 0, kAny
, kAny
, VKEY_8
},
449 {DomCode::KEY_Q
, 0, 0, kAny
, kAny
, VKEY_Q
},
450 {DomCode::KEY_X
, 0, 0, kAny
, kAny
, VKEY_X
}};
453 const PrintableSubEntry kU0173
[] = {
454 {DomCode::DIGIT7
, 0, 0, kAny
, kAny
, VKEY_7
},
455 {DomCode::SEMICOLON
, 1, 1, 0x0172, 0x0162, VKEY_OEM_1
}, // U ogo., T ced.
456 {DomCode::SEMICOLON
, 1, 0, 0x0172, kAny
, VKEY_OEM_3
}}; // U ogonek
458 // U+017C z dot above
459 const PrintableSubEntry kU017C
[] = {
460 {DomCode::BRACKET_LEFT
, 0, 0, kAny
, kAny
, VKEY_OEM_4
},
461 {DomCode::BACKSLASH
, 0, 0, kAny
, kAny
, VKEY_OEM_5
}};
464 const PrintableSubEntry kU017E
[] = {
465 {DomCode::DIGIT6
, 0, 0, kAny
, kAny
, VKEY_6
},
466 {DomCode::EQUAL
, 0, 0, kAny
, kAny
, VKEY_OEM_PLUS
},
467 {DomCode::KEY_W
, 0, 0, kAny
, kAny
, VKEY_W
},
468 {DomCode::BRACKET_LEFT
, 0, 0, kAny
, kAny
, VKEY_Y
},
469 {DomCode::BACKSLASH
, 0, 0, kAny
, kAny
, VKEY_OEM_5
}};
471 // Table mapping unshifted characters to PrintableSubEntry tables.
472 struct PrintableMultiEntry
{
473 base::char16 plain_character
;
474 const PrintableSubEntry
* subtable
;
475 size_t subtable_size
;
478 // Entries are ordered by character value.
479 const PrintableMultiEntry kMultiMap
[] = {
480 {0x0021, kU0021
, arraysize(kU0021
)}, // exclamation mark
481 {0x0022, kU0022
, arraysize(kU0022
)}, // quotation mark
482 {0x0023, kU0023
, arraysize(kU0023
)}, // number sign
483 {0x0024, kU0024
, arraysize(kU0024
)}, // dollar sign
484 {0x0027, kU0027
, arraysize(kU0027
)}, // apostrophe
485 {0x0028, kU0028
, arraysize(kU0028
)}, // left parenthesis
486 {0x0029, kU0029
, arraysize(kU0029
)}, // right parenthesis
487 {0x002A, kU002A
, arraysize(kU002A
)}, // asterisk
488 {0x002B, kU002B
, arraysize(kU002B
)}, // plus sign
489 {0x002C, kU002C
, arraysize(kU002C
)}, // comma
490 {0x002D, kU002D
, arraysize(kU002D
)}, // hyphen-minus
491 {0x002E, kU002E
, arraysize(kU002E
)}, // full stop
492 {0x002F, kU002F
, arraysize(kU002F
)}, // solidus
493 {0x003A, kU003A
, arraysize(kU003A
)}, // colon
494 {0x003B, kU003B
, arraysize(kU003B
)}, // semicolon
495 {0x003D, kU003D
, arraysize(kU003D
)}, // equals sign
496 {0x003F, kU003F
, arraysize(kU003F
)}, // question mark
497 {0x0040, kU0040
, arraysize(kU0040
)}, // commercial at
498 {0x005B, kU005B
, arraysize(kU005B
)}, // left square bracket
499 {0x005C, kU005C
, arraysize(kU005C
)}, // reverse solidus
500 {0x005D, kU005D
, arraysize(kU005D
)}, // right square bracket
501 {0x005F, kU005F
, arraysize(kU005F
)}, // low line
502 {0x0060, kU0060
, arraysize(kU0060
)}, // grave accent
503 {0x00A7, kU00A7
, arraysize(kU00A7
)}, // section sign
504 {0x00AB, kU00AB
, arraysize(kU00AB
)}, // left double angle quotation mark
505 {0x00B0, kU00B0
, arraysize(kU00B0
)}, // degree sign
506 {0x00BA, kU00BA
, arraysize(kU00BA
)}, // masculine ordinal indicator
507 {0x00E0, kU00E0
, arraysize(kU00E0
)}, // a grave
508 {0x00E1, kU00E1
, arraysize(kU00E1
)}, // a acute
509 {0x00E2, kU00E2
, arraysize(kU00E2
)}, // a circumflex
510 {0x00E4, kU00E4
, arraysize(kU00E4
)}, // a diaeresis
511 {0x00E6, kU00E6
, arraysize(kU00E6
)}, // ae
512 {0x00E7, kU00E7
, arraysize(kU00E7
)}, // c cedilla
513 {0x00E8, kU00E8
, arraysize(kU00E8
)}, // e grave
514 {0x00E9, kU00E9
, arraysize(kU00E9
)}, // e acute
515 {0x00ED, kU00ED
, arraysize(kU00ED
)}, // i acute
516 {0x00F0, kU00F0
, arraysize(kU00F0
)}, // eth
517 {0x00F3, kU00F3
, arraysize(kU00F3
)}, // o acute
518 {0x00F4, kU00F4
, arraysize(kU00F4
)}, // o circumflex
519 {0x00F6, kU00F6
, arraysize(kU00F6
)}, // o diaeresis
520 {0x00F8, kU00F8
, arraysize(kU00F8
)}, // o stroke
521 {0x00F9, kU00F9
, arraysize(kU00F9
)}, // u grave
522 {0x00FA, kU00FA
, arraysize(kU00FA
)}, // u acute
523 {0x00FC, kU00FC
, arraysize(kU00FC
)}, // u diaeresis
524 {0x0103, kU0103
, arraysize(kU0103
)}, // a breve
525 {0x0105, kU0105
, arraysize(kU0105
)}, // a ogonek
526 {0x010D, kU010D
, arraysize(kU010D
)}, // c caron
527 {0x0111, kU0111
, arraysize(kU0111
)}, // d stroke
528 {0x0117, kU0117
, arraysize(kU0117
)}, // e dot above
529 {0x0119, kU0119
, arraysize(kU0119
)}, // e ogonek
530 {0x012F, kU012F
, arraysize(kU012F
)}, // i ogonek
531 {0x0142, kU0142
, arraysize(kU0142
)}, // l stroke
532 {0x015F, kU015F
, arraysize(kU015F
)}, // s cedilla
533 {0x0161, kU0161
, arraysize(kU0161
)}, // s caron
534 {0x016B, kU016B
, arraysize(kU016B
)}, // u macron
535 {0x0173, kU0173
, arraysize(kU0173
)}, // u ogonek
536 {0x017C, kU017C
, arraysize(kU017C
)}, // z dot above
537 {0x017E, kU017E
, arraysize(kU017E
)}, // z caron
540 // Table mapping unshifted characters to VKEY values.
541 struct PrintableSimpleEntry
{
542 base::char16 plain_character
;
543 KeyboardCode key_code
;
546 // Entries are ordered by character value.
547 const PrintableSimpleEntry kSimpleMap
[] = {
548 {0x0025, VKEY_5
}, // percent sign
549 {0x0026, VKEY_1
}, // ampersand
550 {0x003C, VKEY_OEM_5
}, // less-than sign
551 {0x007B, VKEY_OEM_7
}, // left curly bracket
552 {0x007C, VKEY_OEM_5
}, // vertical line
553 {0x007D, VKEY_OEM_2
}, // right curly bracket
554 {0x007E, VKEY_OEM_5
}, // tilde
555 {0x00A1, VKEY_OEM_6
}, // inverted exclamation mark
556 {0x00AD, VKEY_OEM_3
}, // soft hyphen
557 {0x00B2, VKEY_OEM_7
}, // superscript two
558 {0x00B5, VKEY_OEM_5
}, // micro sign
559 {0x00BB, VKEY_9
}, // right-pointing double angle quotation mark
560 {0x00BD, VKEY_OEM_5
}, // vulgar fraction one half
561 {0x00BF, VKEY_OEM_6
}, // inverted question mark
562 {0x00DF, VKEY_OEM_4
}, // sharp s
563 {0x00E5, VKEY_OEM_6
}, // a ring above
564 {0x00EA, VKEY_3
}, // e circumflex
565 {0x00EB, VKEY_OEM_1
}, // e diaeresis
566 {0x00EC, VKEY_OEM_6
}, // i grave
567 {0x00EE, VKEY_OEM_6
}, // i circumflex
568 {0x00F1, VKEY_OEM_3
}, // n tilde
569 {0x00F2, VKEY_OEM_3
}, // o grave
570 {0x00F5, VKEY_OEM_4
}, // o tilde
571 {0x00F7, VKEY_OEM_6
}, // division sign
572 {0x00FD, VKEY_7
}, // y acute
573 {0x00FE, VKEY_OEM_MINUS
}, // thorn
574 {0x0101, VKEY_OEM_8
}, // a macron
575 {0x0107, VKEY_OEM_7
}, // c acute
576 {0x010B, VKEY_OEM_3
}, // c dot above
577 {0x0113, VKEY_W
}, // e macron
578 {0x011B, VKEY_2
}, // e caron
579 {0x011F, VKEY_OEM_6
}, // g breve
580 {0x0121, VKEY_OEM_4
}, // g dot above
581 {0x0127, VKEY_OEM_6
}, // h stroke
582 {0x012B, VKEY_OEM_6
}, // i macron
583 {0x0131, VKEY_OEM_1
}, // dotless i
584 {0x0137, VKEY_OEM_5
}, // k cedilla
585 {0x013C, VKEY_OEM_2
}, // l cedilla
586 {0x013E, VKEY_2
}, // l caron
587 {0x0146, VKEY_OEM_4
}, // n cedilla
588 {0x0148, VKEY_OEM_5
}, // n caron
589 {0x0151, VKEY_OEM_4
}, // o double acute
590 {0x0159, VKEY_5
}, // r caron
591 {0x0163, VKEY_OEM_7
}, // t cedilla
592 {0x0165, VKEY_5
}, // t caron
593 {0x016F, VKEY_OEM_1
}, // u ring above
594 {0x0171, VKEY_OEM_5
}, // u double acute
595 {0x01A1, VKEY_OEM_6
}, // o horn
596 {0x01B0, VKEY_OEM_4
}, // u horn
597 {0x01B6, VKEY_OEM_6
}, // z stroke
598 {0x0259, VKEY_OEM_3
}, // schwa
601 #if defined(OS_CHROMEOS)
602 void LoadKeymap(const std::string
& layout_name
,
603 scoped_refptr
<base::SingleThreadTaskRunner
> reply_runner
,
604 const LoadKeymapCallback
& reply_callback
) {
605 std::string layout_id
;
606 std::string layout_variant
;
607 XkbKeyboardLayoutEngine::ParseLayoutName(layout_name
, &layout_id
,
609 xkb_rule_names names
= {.rules
= NULL
,
611 .layout
= layout_id
.c_str(),
612 .variant
= layout_variant
.c_str(),
614 scoped_ptr
<xkb_context
, XkbContextDeleter
> context
;
615 context
.reset(xkb_context_new(XKB_CONTEXT_NO_DEFAULT_INCLUDES
));
616 xkb_context_include_path_append(context
.get(), "/usr/share/X11/xkb");
617 scoped_ptr
<xkb_keymap
, XkbKeymapDeleter
> keymap
;
618 keymap
.reset(xkb_keymap_new_from_names(context
.get(), &names
,
619 XKB_KEYMAP_COMPILE_NO_FLAGS
));
621 scoped_ptr
<char, base::FreeDeleter
> keymap_str(
622 xkb_keymap_get_as_string(keymap
.get(), XKB_KEYMAP_FORMAT_TEXT_V1
));
623 reply_runner
->PostTask(FROM_HERE
, base::Bind(reply_callback
, layout_name
,
624 base::Passed(&keymap_str
)));
626 LOG(FATAL
) << "Keymap file failed to load: " << layout_name
;
631 } // anonymous namespace
633 XkbKeyCodeConverter::XkbKeyCodeConverter() {
636 XkbKeyCodeConverter::~XkbKeyCodeConverter() {
639 XkbKeyboardLayoutEngine::XkbKeyboardLayoutEngine(
640 const XkbKeyCodeConverter
& converter
)
641 : key_code_converter_(converter
), weak_ptr_factory_(this) {
642 // TODO: add XKB_CONTEXT_NO_ENVIRONMENT_NAMES
643 xkb_context_
.reset(xkb_context_new(XKB_CONTEXT_NO_DEFAULT_INCLUDES
));
644 xkb_context_include_path_append(xkb_context_
.get(),
645 "/usr/share/X11/xkb");
648 XkbKeyboardLayoutEngine::~XkbKeyboardLayoutEngine() {
649 for (const auto& entry
: xkb_keymaps_
) {
650 xkb_keymap_unref(entry
.keymap
);
654 bool XkbKeyboardLayoutEngine::CanSetCurrentLayout() const {
655 #if defined(OS_CHROMEOS)
662 bool XkbKeyboardLayoutEngine::SetCurrentLayoutByName(
663 const std::string
& layout_name
) {
664 #if defined(OS_CHROMEOS)
665 current_layout_name_
= layout_name
;
666 for (const auto& entry
: xkb_keymaps_
) {
667 if (entry
.layout_name
== layout_name
) {
668 SetKeymap(entry
.keymap
);
672 LoadKeymapCallback reply_callback
= base::Bind(
673 &XkbKeyboardLayoutEngine::OnKeymapLoaded
, weak_ptr_factory_
.GetWeakPtr());
674 base::WorkerPool::PostTask(
676 base::Bind(&LoadKeymap
, layout_name
, base::ThreadTaskRunnerHandle::Get(),
680 xkb_keymap
* keymap
= xkb_map_new_from_string(
681 xkb_context_
.get(), layout_name
.c_str(), XKB_KEYMAP_FORMAT_TEXT_V1
,
682 XKB_KEYMAP_COMPILE_NO_FLAGS
);
686 #endif // defined(OS_CHROMEOS)
690 void XkbKeyboardLayoutEngine::OnKeymapLoaded(
691 const std::string
& layout_name
,
692 scoped_ptr
<char, base::FreeDeleter
> keymap_str
) {
694 xkb_keymap
* keymap
= xkb_map_new_from_string(
695 xkb_context_
.get(), keymap_str
.get(), XKB_KEYMAP_FORMAT_TEXT_V1
,
696 XKB_KEYMAP_COMPILE_NO_FLAGS
);
697 XkbKeymapEntry entry
= {layout_name
, keymap
};
698 xkb_keymaps_
.push_back(entry
);
699 if (layout_name
== current_layout_name_
)
702 LOG(FATAL
) << "Keymap file failed to load: " << layout_name
;
706 bool XkbKeyboardLayoutEngine::UsesISOLevel5Shift() const {
711 bool XkbKeyboardLayoutEngine::UsesAltGr() const {
716 bool XkbKeyboardLayoutEngine::Lookup(DomCode dom_code
,
719 base::char16
* character
,
720 KeyboardCode
* key_code
,
721 uint32
* platform_keycode
) const {
722 if (dom_code
== DomCode::NONE
)
724 // Convert DOM physical key to XKB representation.
725 xkb_keycode_t xkb_keycode
= key_code_converter_
.DomCodeToXkbKeyCode(dom_code
);
726 if (xkb_keycode
== key_code_converter_
.InvalidXkbKeyCode()) {
727 LOG(ERROR
) << "No XKB keycode for DomCode 0x" << std::hex
728 << static_cast<int>(dom_code
) << " '"
729 << KeycodeConverter::DomCodeToCodeString(dom_code
) << "'";
732 xkb_mod_mask_t xkb_flags
= EventFlagsToXkbFlags(flags
);
733 // Obtain keysym and character.
734 xkb_keysym_t xkb_keysym
;
735 if (!XkbLookup(xkb_keycode
, xkb_flags
, &xkb_keysym
, character
))
737 *platform_keycode
= xkb_keysym
;
738 // Classify the keysym and convert to DOM and VKEY representations.
739 *dom_key
= NonPrintableXKeySymToDomKey(xkb_keysym
);
740 if (*dom_key
== DomKey::NONE
) {
741 *dom_key
= CharacterToDomKey(*character
);
742 *key_code
= AlphanumericKeyboardCode(*character
);
743 if (*key_code
== VKEY_UNKNOWN
) {
744 *key_code
= DifficultKeyboardCode(dom_code
, flags
, xkb_keycode
, xkb_flags
,
745 xkb_keysym
, *dom_key
, *character
);
746 if (*key_code
== VKEY_UNKNOWN
) {
747 *key_code
= LocatedToNonLocatedKeyboardCode(
748 DomCodeToUsLayoutKeyboardCode(dom_code
));
751 // If the Control key is down, only allow ASCII control characters to be
752 // returned, regardless of the key layout. crbug.com/450849
753 if ((flags
& EF_CONTROL_DOWN
) && (*character
>= 0x20))
755 } else if (*dom_key
== DomKey::DEAD
) {
756 *character
= DeadXkbKeySymToCombiningCharacter(xkb_keysym
);
757 *key_code
= LocatedToNonLocatedKeyboardCode(
758 DomCodeToUsLayoutKeyboardCode(dom_code
));
760 *key_code
= NonPrintableDomKeyToKeyboardCode(*dom_key
);
761 if (*key_code
== VKEY_UNKNOWN
) {
762 *key_code
= LocatedToNonLocatedKeyboardCode(
763 DomCodeToUsLayoutKeyboardCode(dom_code
));
769 void XkbKeyboardLayoutEngine::SetKeymap(xkb_keymap
* keymap
) {
770 xkb_state_
.reset(xkb_state_new(keymap
));
772 static const struct {
774 const char* xkb_name
;
775 } flags
[] = {{ui::EF_CONTROL_DOWN
, XKB_MOD_NAME_CTRL
},
776 {ui::EF_SHIFT_DOWN
, XKB_MOD_NAME_SHIFT
},
777 {ui::EF_ALT_DOWN
, XKB_MOD_NAME_ALT
},
778 {ui::EF_CAPS_LOCK_DOWN
, XKB_MOD_NAME_CAPS
},
779 {ui::EF_COMMAND_DOWN
, XKB_MOD_NAME_LOGO
},
780 {ui::EF_MOD3_DOWN
, "Mod3"},
781 {ui::EF_ALTGR_DOWN
, "Mod5"}};
782 xkb_flag_map_
.clear();
783 xkb_flag_map_
.resize(arraysize(flags
));
784 for (size_t i
= 0; i
< arraysize(flags
); ++i
) {
785 xkb_mod_index_t index
= xkb_keymap_mod_get_index(keymap
, flags
[i
].xkb_name
);
786 if (index
== XKB_MOD_INVALID
) {
787 DVLOG(3) << "XKB keyboard layout does not contain " << flags
[i
].xkb_name
;
789 xkb_mod_mask_t flag
= static_cast<xkb_mod_mask_t
>(1) << index
;
790 XkbFlagMapEntry e
= {flags
[i
].ui_flag
, flag
};
791 xkb_flag_map_
.push_back(e
);
795 // Update num lock mask.
796 num_lock_mod_mask_
= 0;
797 xkb_mod_index_t num_mod_index
=
798 xkb_keymap_mod_get_index(keymap
, XKB_MOD_NAME_NUM
);
799 if (num_mod_index
!= XKB_MOD_INVALID
)
800 num_lock_mod_mask_
= static_cast<xkb_mod_mask_t
>(1) << num_mod_index
;
803 xkb_mod_mask_t
XkbKeyboardLayoutEngine::EventFlagsToXkbFlags(
804 int ui_flags
) const {
805 xkb_mod_mask_t xkb_flags
= 0;
806 for (const auto& entry
: xkb_flag_map_
) {
807 if (ui_flags
& entry
.ui_flag
)
808 xkb_flags
|= entry
.xkb_flag
;
810 // NumLock is always on.
811 xkb_flags
|= num_lock_mod_mask_
;
815 bool XkbKeyboardLayoutEngine::XkbLookup(xkb_keycode_t xkb_keycode
,
816 xkb_mod_mask_t xkb_flags
,
817 xkb_keysym_t
* xkb_keysym
,
818 base::char16
* character
) const {
820 LOG(ERROR
) << "No current XKB state";
823 xkb_state_update_mask(xkb_state_
.get(), xkb_flags
, 0, 0, 0, 0, 0);
824 *xkb_keysym
= xkb_state_key_get_one_sym(xkb_state_
.get(), xkb_keycode
);
825 if (*xkb_keysym
== XKB_KEY_NoSymbol
)
827 uint32_t c
= xkb_state_key_get_utf32(xkb_state_
.get(), xkb_keycode
);
828 DLOG_IF(ERROR
, c
!= (c
& 0xFFFF)) << "Non-BMP character:" << c
;
829 *character
= static_cast<base::char16
>(c
);
833 KeyboardCode
XkbKeyboardLayoutEngine::DifficultKeyboardCode(
836 xkb_keycode_t xkb_keycode
,
837 xkb_mod_mask_t xkb_flags
,
838 xkb_keysym_t xkb_keysym
,
840 base::char16 character
) const {
841 // Get the layout interpretation without modifiers, so that
842 // e.g. Ctrl+D correctly generates VKEY_D.
843 xkb_keysym_t plain_keysym
;
844 base::char16 plain_character
;
845 if (!XkbLookup(xkb_keycode
, 0, &plain_keysym
, &plain_character
))
848 // If the plain key is non-printable, that determines the VKEY.
849 DomKey plain_key
= NonPrintableXKeySymToDomKey(plain_keysym
);
850 if (plain_key
!= ui::DomKey::NONE
)
851 return NonPrintableDomKeyToKeyboardCode(dom_key
);
853 // Plain ASCII letters and digits map directly to VKEY values.
854 KeyboardCode key_code
= AlphanumericKeyboardCode(plain_character
);
855 if (key_code
!= VKEY_UNKNOWN
)
858 // Check the multi-character tables.
859 const PrintableMultiEntry
* multi_end
= kMultiMap
+ arraysize(kMultiMap
);
860 const PrintableMultiEntry
* multi
=
861 std::lower_bound(kMultiMap
, multi_end
, plain_character
,
862 [](const PrintableMultiEntry
& e
, base::char16 c
) {
863 return e
.plain_character
< c
;
865 if ((multi
!= multi_end
) && (multi
->plain_character
== plain_character
)) {
866 const base::char16 kNonCharacter
= kAny
;
867 base::char16 shift_character
= kNonCharacter
;
868 base::char16 altgr_character
= kNonCharacter
;
869 for (size_t i
= 0; i
< multi
->subtable_size
; ++i
) {
870 if (multi
->subtable
[i
].dom_code
!= dom_code
)
872 if (multi
->subtable
[i
].test_shift
) {
873 if (shift_character
== kNonCharacter
) {
874 shift_character
= XkbSubCharacter(xkb_keycode
, xkb_flags
, character
,
877 if (shift_character
!= multi
->subtable
[i
].shift_character
)
880 if (multi
->subtable
[i
].test_altgr
) {
881 if (altgr_character
== kNonCharacter
) {
882 altgr_character
= XkbSubCharacter(xkb_keycode
, xkb_flags
, character
,
885 if (altgr_character
!= multi
->subtable
[i
].altgr_character
)
888 return multi
->subtable
[i
].key_code
;
892 // Check the simple character table.
893 const PrintableSimpleEntry
* simple_end
= kSimpleMap
+ arraysize(kSimpleMap
);
894 const PrintableSimpleEntry
* simple
=
895 std::lower_bound(kSimpleMap
, simple_end
, plain_character
,
896 [](const PrintableSimpleEntry
& e
, base::char16 c
) {
897 return e
.plain_character
< c
;
899 if ((simple
!= simple_end
) && (simple
->plain_character
== plain_character
))
900 return simple
->key_code
;
905 base::char16
XkbKeyboardLayoutEngine::XkbSubCharacter(
906 xkb_keycode_t xkb_keycode
,
907 xkb_mod_mask_t base_flags
,
908 base::char16 base_character
,
909 int ui_flags
) const {
910 xkb_mod_mask_t flags
= EventFlagsToXkbFlags(ui_flags
);
911 if (flags
== base_flags
)
912 return base_character
;
914 base::char16 character
= 0;
915 if (!XkbLookup(xkb_keycode
, flags
, &keysym
, &character
))
920 void XkbKeyboardLayoutEngine::ParseLayoutName(const std::string
& layout_name
,
921 std::string
* layout_id
,
922 std::string
* layout_variant
) {
923 size_t dash_index
= layout_name
.find('-');
924 size_t parentheses_index
= layout_name
.find('(');
925 *layout_id
= layout_name
;
926 *layout_variant
= "";
927 if (parentheses_index
!= std::string::npos
) {
928 *layout_id
= layout_name
.substr(0, parentheses_index
);
929 size_t close_index
= layout_name
.find(')', parentheses_index
);
930 if (close_index
== std::string::npos
)
931 close_index
= layout_name
.size();
932 *layout_variant
= layout_name
.substr(parentheses_index
+ 1,
933 close_index
- parentheses_index
- 1);
934 } else if (dash_index
!= std::string::npos
) {
935 *layout_id
= layout_name
.substr(0, dash_index
);
936 *layout_variant
= layout_name
.substr(dash_index
+ 1);