Index: ui/events/keycodes/key_map_win.cc |
diff --git a/ui/events/keycodes/key_map_win.cc b/ui/events/keycodes/key_map_win.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..f5b631e19d61ab35fb19e658cce302f20df55258 |
--- /dev/null |
+++ b/ui/events/keycodes/key_map_win.cc |
@@ -0,0 +1,197 @@ |
+// Copyright (c) 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "ui/events/keycodes/key_map_win.h" |
+ |
+#include <utility> |
+ |
+#include "base/lazy_instance.h" |
+#include "base/logging.h" |
+#include "base/macros.h" |
+#include "base/threading/thread_local_storage.h" |
+ |
+#include "ui/events/event_constants.h" |
+#include "ui/events/keycodes/dom/dom_code.h" |
+ |
+namespace ui { |
+ |
+namespace { |
+ |
+struct DomCodeEntry { |
+ DomCode dom_code; |
+ int scan_code; |
+}; |
+#define USB_KEYMAP_DECLARATION const DomCodeEntry supported_dom_code_list[] = |
+#define USB_KEYMAP(usb, evdev, xkb, win, mac, code, id) {DomCode::id, win} |
+#include "ui/events/keycodes/dom/keycode_converter_data.inc" |
+#undef USB_KEYMAP |
+#undef USB_KEYMAP_DECLARATION |
+ |
+// List of modifiers mentioned in https://w3c.github.io/uievents/#keys-modifiers |
+// Some modifiers are commented out because they usually don't change keys. |
+const EventFlags modifier_flags[] = { |
+ EF_SHIFT_DOWN, |
Wez
2016/02/11 23:14:45
nit: This indentation doesn't match DomCodeEntry,
|
+ EF_CONTROL_DOWN, |
+ EF_ALT_DOWN, |
+ // EF_COMMAND_DOWN, |
+ EF_ALTGR_DOWN, |
+ // EF_NUM_LOCK_ON, |
+ EF_CAPS_LOCK_ON, |
+ // EF_SCROLL_LOCK_ON |
+}; |
+const int kModifierFlagsCombinations = (1 << arraysize(modifier_flags)) - 1; |
+ |
+int GetModifierFlags(int combination) { |
+ int flags = EF_NONE; |
+ for (int i = 0; i < arraysize(modifier_flags); ++i) { |
+ if (combination & (1 << i)) |
+ flags |= modifier_flags[i]; |
+ } |
+ return flags; |
+} |
+ |
+void SetModifierState(BYTE* keyboard_state, int flags) { |
+ if (flags & EF_SHIFT_DOWN) |
+ keyboard_state[VK_SHIFT] |= 0x80; |
+ |
+ if (flags & EF_CONTROL_DOWN) |
+ keyboard_state[VK_CONTROL] |= 0x80; |
+ |
+ if (flags & EF_ALT_DOWN) |
+ keyboard_state[VK_MENU] |= 0x80; |
+ |
+ if (flags & EF_ALTGR_DOWN) { |
+ // AltGr should be RightAlt+LeftControl within Windows, but actually only |
+ // the non-located keys will work here. |
+ keyboard_state[VK_MENU] |= 0x80; |
+ keyboard_state[VK_CONTROL] |= 0x80; |
+ } |
+ |
+ if (flags & EF_COMMAND_DOWN) |
+ keyboard_state[VK_LWIN] |= 0x80; |
+ |
+ if (flags & EF_NUM_LOCK_ON) |
Wez
2016/02/11 23:14:45
nit: Consider adding a block comment to explain th
|
+ keyboard_state[VK_NUMLOCK] |= 0x01; |
+ |
+ if (flags & EF_CAPS_LOCK_ON) |
+ keyboard_state[VK_CAPITAL] |= 0x01; |
+ |
+ if (flags & EF_SCROLL_LOCK_ON) |
+ keyboard_state[VK_SCROLL] |= 0x01; |
+} |
+ |
+// Use TLS because KeyboardLayout is per thread. |
+// However currently WindowsKeyMap will only be used by the host application, |
+// which is just one process and one thread. |
+void TLSCleanupFunction(void* data) { |
Wez
2016/02/11 23:14:45
nit: Style-guide prefers that even abbreviations o
|
+ WindowsKeyMap* key_map = static_cast<WindowsKeyMap*>(data); |
Wez
2016/02/11 23:14:44
reinterpret_cast<> would be more appropriate here,
|
+ if (key_map) |
Wez
2016/02/11 23:14:44
This if() is redundant - delete is fine to call on
|
+ delete key_map; |
+} |
+ |
+struct TLSWindowsKeyMapInstanceTraits |
+ : public base::DefaultLazyInstanceTraits<base::ThreadLocalStorage::Slot> { |
+ static base::ThreadLocalStorage::Slot* New(void* instance) { |
+ // Use placement new to initialize our instance in our preallocated space. |
+ return new (instance) base::ThreadLocalStorage::Slot(TLSCleanupFunction); |
+ } |
+}; |
+ |
+base::LazyInstance<base::ThreadLocalStorage::Slot, |
+ TLSWindowsKeyMapInstanceTraits> |
+ lazy_tls_key_map = LAZY_INSTANCE_INITIALIZER; |
+ |
+} // anonymous namespace |
+ |
+DomKey WindowsKeyMap::DomCodeAndFlagsToDomKey(DomCode code, int flags) const { |
+ const int flags_to_try[] = { |
+ // Trying to match Firefox's behavior and UIEvents DomKey guidelines. |
Wez
2016/02/11 23:14:45
nit: Indentation looks wrong here? Is this what g
|
+ // If the combination doesn't produce a printable character, the key value |
+ // should be the key with no modifiers except for Shift and AltGr. |
+ // See https://w3c.github.io/uievents/#keys-guidelines |
+ flags, |
+ flags & (EF_SHIFT_DOWN | EF_ALTGR_DOWN | EF_CAPS_LOCK_ON), |
+ flags & (EF_SHIFT_DOWN | EF_CAPS_LOCK_ON), |
+ EF_NONE, |
+ }; |
+ |
+ DomKey key = DomKey::NONE; |
+ for (auto try_flags : flags_to_try) { |
+ const auto& it = |
+ key_map_.find(std::make_pair(static_cast<int>(code), try_flags)); |
+ if (it != key_map_.end()) { |
+ key = it->second; |
+ if (key != DomKey::NONE) |
+ break; |
+ } |
+ } |
+ return key; |
+} |
+ |
+// static |
+DomKey WindowsKeyMap::DomCodeAndFlagsToDomKeyStatic(DomCode code, int flags) { |
+ base::ThreadLocalStorage::Slot* tls_slot = lazy_tls_key_map.Pointer(); |
+ WindowsKeyMap* tls_key_map = static_cast<WindowsKeyMap*>(tls_slot->Get()); |
+ if (!tls_key_map) { |
+ tls_key_map = new WindowsKeyMap(); |
+ tls_slot->Set(tls_key_map); |
+ } |
+ |
+ HKL current_layout = ::GetKeyboardLayout(0); |
+ tls_key_map->UpdateLayout(current_layout); |
+ return tls_key_map->DomCodeAndFlagsToDomKey(code, flags); |
+} |
+ |
+void WindowsKeyMap::UpdateLayout(HKL layout) { |
+ if (layout == keyboard_layout_) |
+ return; |
+ |
+ keyboard_layout_ = layout; |
+ key_map_.clear(); |
+ // Usually the map has ~1000 entries. |
Wez
2016/02/11 23:14:45
nit: Have we actually verified exactly how many we
|
+ key_map_.reserve(1000); |
+ |
+ BYTE keyboard_state_to_restore[256]; |
+ ::GetKeyboardState(keyboard_state_to_restore); |
+ |
+ for (int eindex = 0; eindex <= kModifierFlagsCombinations; ++eindex) { |
+ BYTE keyboard_state[256]; |
+ memset(keyboard_state, 0, sizeof(keyboard_state)); |
+ int flags = GetModifierFlags(eindex); |
+ SetModifierState(keyboard_state, flags); |
+ for (const auto& dom_code_entry : supported_dom_code_list) { |
+ wchar_t translated_chars[5]; |
+ int key_code = ::MapVirtualKeyEx(dom_code_entry.scan_code, |
+ MAPVK_VSC_TO_VK, keyboard_layout_); |
+ int rv = ::ToUnicodeEx(key_code, 0, keyboard_state, translated_chars, |
+ arraysize(translated_chars), 0, keyboard_layout_); |
+ |
+ if (rv < 0) { |
+ // Dead key, injecting VK_SPACE to get character representation. |
+ BYTE empty_state[256]; |
+ memset(empty_state, 0, sizeof(empty_state)); |
+ rv = ::ToUnicodeEx(VK_SPACE, 0, empty_state, translated_chars, |
+ arraysize(translated_chars), 0, keyboard_layout_); |
+ // Must be a repeat of dead key character. |
+ DCHECK(rv == 2); |
+ key_map_[std::make_pair(static_cast<int>(dom_code_entry.dom_code), |
+ flags)] = |
+ DomKey::DeadKeyFromCombiningCharacter(translated_chars[0]); |
+ } else if (rv == 1) { |
+ if (translated_chars[0] >= 0x20) { |
+ key_map_[std::make_pair(static_cast<int>(dom_code_entry.dom_code), |
+ flags)] = |
+ DomKey::FromCharacter(translated_chars[0]); |
+ } else { |
+ // Ignores legacy non-printable control characters. |
+ } |
+ } else { |
+ // TODO(chongz): Handle multiple characters case. |
+ } |
+ } |
+ } |
+ ::SetKeyboardState(keyboard_state_to_restore); |
+} |
+ |
+} // namespace ui |