Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(580)

Side by Side Diff: ui/events/keycodes/platform_key_map_win.cc

Issue 1585193002: Build key map DomCodeToKey() for Windows (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix unittest Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2016 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.
4
5 #include "ui/events/keycodes/platform_key_map_win.h"
6
7 #include <utility>
8
9 #include "base/lazy_instance.h"
10 #include "base/logging.h"
11 #include "base/macros.h"
12 #include "base/threading/thread_local_storage.h"
13
14 #include "ui/events/event_constants.h"
15 #include "ui/events/keycodes/dom/dom_code.h"
16
17 namespace ui {
18
19 namespace {
20
21 struct DomCodeEntry {
22 DomCode dom_code;
23 int scan_code;
24 };
25 #define USB_KEYMAP_DECLARATION const DomCodeEntry supported_dom_code_list[] =
26 #define USB_KEYMAP(usb, evdev, xkb, win, mac, code, id) {DomCode::id, win}
27 #include "ui/events/keycodes/dom/keycode_converter_data.inc"
28 #undef USB_KEYMAP
29 #undef USB_KEYMAP_DECLARATION
30
31 // List of modifiers mentioned in https://w3c.github.io/uievents/#keys-modifiers
32 // Some modifiers are commented out because they usually don't change keys.
33 const EventFlags modifier_flags[] = {
34 EF_SHIFT_DOWN,
35 EF_CONTROL_DOWN,
36 EF_ALT_DOWN,
37 // EF_COMMAND_DOWN,
38 EF_ALTGR_DOWN,
39 // EF_NUM_LOCK_ON,
40 EF_CAPS_LOCK_ON,
41 // EF_SCROLL_LOCK_ON
42 };
43 const int kModifierFlagsCombinations = (1 << arraysize(modifier_flags)) - 1;
44
45 int GetModifierFlags(int combination) {
46 int flags = EF_NONE;
47 for (int i = 0; i < arraysize(modifier_flags); ++i) {
48 if (combination & (1 << i))
49 flags |= modifier_flags[i];
50 }
51 return flags;
52 }
53
54 void SetModifierState(BYTE* keyboard_state, int flags) {
55 // According to MSDN GetKeyState():
56 // 1. If the high-order bit is 1, the key is down; otherwise, it is up.
57 // 2. If the low-order bit is 1, the key is toggled. A key, such as the
58 // CAPS LOCK key, is toggled if it is turned on. The key is off and
59 // untoggled if the low-order bit is 0.
60 // See https://msdn.microsoft.com/en-us/library/windows/desktop/ms646301.aspx
61 if (flags & EF_SHIFT_DOWN)
62 keyboard_state[VK_SHIFT] |= 0x80;
63
64 if (flags & EF_CONTROL_DOWN)
65 keyboard_state[VK_CONTROL] |= 0x80;
66
67 if (flags & EF_ALT_DOWN)
68 keyboard_state[VK_MENU] |= 0x80;
69
70 if (flags & EF_ALTGR_DOWN) {
71 // AltGr should be RightAlt+LeftControl within Windows, but actually only
72 // the non-located keys will work here.
73 keyboard_state[VK_MENU] |= 0x80;
74 keyboard_state[VK_CONTROL] |= 0x80;
75 }
76
77 if (flags & EF_COMMAND_DOWN)
78 keyboard_state[VK_LWIN] |= 0x80;
79
80 if (flags & EF_NUM_LOCK_ON)
81 keyboard_state[VK_NUMLOCK] |= 0x01;
82
83 if (flags & EF_CAPS_LOCK_ON)
84 keyboard_state[VK_CAPITAL] |= 0x01;
85
86 if (flags & EF_SCROLL_LOCK_ON)
87 keyboard_state[VK_SCROLL] |= 0x01;
88 }
89
90 void CleanupKeyMapTls(void* data) {
91 PlatformKeyMap* key_map = reinterpret_cast<PlatformKeyMap*>(data);
92 delete key_map;
93 }
94
95 struct PlatformKeyMapInstanceTlsTraits
96 : public base::DefaultLazyInstanceTraits<base::ThreadLocalStorage::Slot> {
97 static base::ThreadLocalStorage::Slot* New(void* instance) {
98 // Use placement new to initialize our instance in our preallocated space.
99 // TODO(chongz): Use std::default_delete instead of providing own function.
100 return new (instance) base::ThreadLocalStorage::Slot(CleanupKeyMapTls);
101 }
102 };
103
104 base::LazyInstance<base::ThreadLocalStorage::Slot,
105 PlatformKeyMapInstanceTlsTraits>
106 g_platform_key_map_tls_lazy = LAZY_INSTANCE_INITIALIZER;
107
108 } // anonymous namespace
109
110 PlatformKeyMap::PlatformKeyMap() {}
111
112 PlatformKeyMap::PlatformKeyMap(HKL layout) {
113 UpdateLayout(layout);
114 }
115
116 DomKey PlatformKeyMap::DomCodeAndFlagsToDomKey(DomCode code, int flags) const {
117 const int flags_to_try[] = {
118 // Trying to match Firefox's behavior and UIEvents DomKey guidelines.
119 // If the combination doesn't produce a printable character, the key value
120 // should be the key with no modifiers except for Shift and AltGr.
121 // See https://w3c.github.io/uievents/#keys-guidelines
122 flags,
123 flags & (EF_SHIFT_DOWN | EF_ALTGR_DOWN | EF_CAPS_LOCK_ON),
124 flags & (EF_SHIFT_DOWN | EF_CAPS_LOCK_ON),
125 EF_NONE,
126 };
127
128 DomKey key = DomKey::NONE;
129 for (auto try_flags : flags_to_try) {
130 const auto& it = code_to_key_.find(std::make_pair(static_cast<int>(code),
131 try_flags));
132 if (it != code_to_key_.end()) {
133 key = it->second;
134 if (key != DomKey::NONE)
135 break;
136 }
137 }
138 return key;
139 }
140
141 // static
142 DomKey PlatformKeyMap::DomCodeAndFlagsToDomKeyStatic(DomCode code, int flags) {
143 // Use TLS because KeyboardLayout is per thread.
144 // However currently PlatformKeyMap will only be used by the host application,
145 // which is just one process and one thread.
146 base::ThreadLocalStorage::Slot* platform_key_map_tls =
147 g_platform_key_map_tls_lazy.Pointer();
148 PlatformKeyMap* platform_key_map =
149 reinterpret_cast<PlatformKeyMap*>(platform_key_map_tls->Get());
150 if (!platform_key_map) {
151 platform_key_map = new PlatformKeyMap();
152 platform_key_map_tls->Set(platform_key_map);
153 }
154
155 HKL current_layout = ::GetKeyboardLayout(0);
156 platform_key_map->UpdateLayout(current_layout);
157 return platform_key_map->DomCodeAndFlagsToDomKey(code, flags);
158 }
159
160 void PlatformKeyMap::UpdateLayout(HKL layout) {
161 if (layout == keyboard_layout_)
162 return;
163
164 // TODO(chongz): Optimize layout switching (see crbug.com/587147).
165 keyboard_layout_ = layout;
166 code_to_key_.clear();
167 // Map size for some sample keyboard layouts:
168 // US: 428, French: 554, Persian: 434, Vietnamese: 1388
169 code_to_key_.reserve(500);
170
171 BYTE keyboard_state_to_restore[256];
172 ::GetKeyboardState(keyboard_state_to_restore);
brucedawson 2016/02/19 19:17:54 The /analyze builder points out that this function
Wez 2016/02/19 21:26:42 Agreed that that seems a sensible precaution - at
173
174 for (int eindex = 0; eindex <= kModifierFlagsCombinations; ++eindex) {
175 BYTE keyboard_state[256];
176 memset(keyboard_state, 0, sizeof(keyboard_state));
177 int flags = GetModifierFlags(eindex);
178 SetModifierState(keyboard_state, flags);
179 for (const auto& dom_code_entry : supported_dom_code_list) {
180 wchar_t translated_chars[5];
181 int key_code = ::MapVirtualKeyEx(dom_code_entry.scan_code,
182 MAPVK_VSC_TO_VK, keyboard_layout_);
183 int rv = ::ToUnicodeEx(key_code, 0, keyboard_state, translated_chars,
184 arraysize(translated_chars), 0, keyboard_layout_);
185
186 if (rv == -1) {
187 // Dead key, injecting VK_SPACE to get character representation.
188 BYTE empty_state[256];
189 memset(empty_state, 0, sizeof(empty_state));
190 rv = ::ToUnicodeEx(VK_SPACE, 0, empty_state, translated_chars,
191 arraysize(translated_chars), 0, keyboard_layout_);
192 // Expecting a dead key character (not followed by a space).
193 if (rv == 1) {
194 code_to_key_[std::make_pair(static_cast<int>(dom_code_entry.dom_code),
195 flags)] =
196 DomKey::DeadKeyFromCombiningCharacter(translated_chars[0]);
197 } else {
198 // TODO(chongz): Check if this will actually happen.
199 }
200 } else if (rv == 1) {
201 if (translated_chars[0] >= 0x20) {
202 code_to_key_[std::make_pair(static_cast<int>(dom_code_entry.dom_code),
203 flags)] =
204 DomKey::FromCharacter(translated_chars[0]);
205 } else {
206 // Ignores legacy non-printable control characters.
207 }
208 } else {
209 // TODO(chongz): Handle rv <= -2 and rv >= 2.
210 }
211 }
212 }
213 ::SetKeyboardState(keyboard_state_to_restore);
214 }
215
216 } // namespace ui
OLDNEW
« no previous file with comments | « ui/events/keycodes/platform_key_map_win.h ('k') | ui/events/keycodes/platform_key_map_win_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698