OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2009 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 "chrome/renderer/mock_keyboard_driver_win.h" |
| 6 |
| 7 #include "base/basictypes.h" |
| 8 #include "base/logging.h" |
| 9 #include "chrome/renderer/mock_keyboard.h" |
| 10 |
| 11 MockKeyboardDriverWin::MockKeyboardDriverWin() { |
| 12 // Save the keyboard layout and status of the application. |
| 13 // This class changes the keyboard layout and status of this application. |
| 14 // This change may break succeeding tests. To prevent this possible break, we |
| 15 // should save the layout and status here to restore when this instance is |
| 16 // destroyed. |
| 17 original_keyboard_layout_ = GetKeyboardLayout(0); |
| 18 GetKeyboardState(&original_keyboard_states_[0]); |
| 19 |
| 20 keyboard_handle_ = NULL; |
| 21 memset(&keyboard_states_[0], 0, sizeof(keyboard_states_)); |
| 22 } |
| 23 |
| 24 MockKeyboardDriverWin::~MockKeyboardDriverWin() { |
| 25 // Unload the keyboard-layout driver, restore the keyboard state, and reset |
| 26 // the keyboard layout for succeeding tests. |
| 27 if (keyboard_handle_) |
| 28 UnloadKeyboardLayout(keyboard_handle_); |
| 29 SetKeyboardState(&original_keyboard_states_[0]); |
| 30 ActivateKeyboardLayout(original_keyboard_layout_, KLF_RESET); |
| 31 } |
| 32 |
| 33 bool MockKeyboardDriverWin::SetLayout(int layout) { |
| 34 // Unload the current keyboard-layout driver and load a new keyboard-layout |
| 35 // driver for mapping a virtual key-code to a Unicode character. |
| 36 if (keyboard_handle_) { |
| 37 UnloadKeyboardLayout(keyboard_handle_); |
| 38 keyboard_handle_ = NULL; |
| 39 } |
| 40 |
| 41 // Scan the mapping table and retrieve a Language ID for the input layout. |
| 42 // Load the keyboard-layout driver when we find a Language ID. |
| 43 // This Language IDs are copied from the registry |
| 44 // "HKLM\SYSTEM\CurrentControlSet\Control\Keyboard layouts". |
| 45 // TODO(hbono): Add more keyboard-layout drivers. |
| 46 static const struct { |
| 47 const wchar_t* language; |
| 48 MockKeyboard::Layout keyboard_layout; |
| 49 } kLanguageIDs[] = { |
| 50 {L"00000401", MockKeyboard::LAYOUT_ARABIC}, |
| 51 {L"00000402", MockKeyboard::LAYOUT_BULGARIAN}, |
| 52 {L"00000404", MockKeyboard::LAYOUT_CHINESE_TRADITIONAL}, |
| 53 {L"00000405", MockKeyboard::LAYOUT_CZECH}, |
| 54 {L"00000406", MockKeyboard::LAYOUT_DANISH}, |
| 55 {L"00000407", MockKeyboard::LAYOUT_GERMAN}, |
| 56 {L"00000408", MockKeyboard::LAYOUT_GREEK}, |
| 57 {L"00000409", MockKeyboard::LAYOUT_UNITED_STATES}, |
| 58 {L"0000040a", MockKeyboard::LAYOUT_SPANISH}, |
| 59 {L"0000040b", MockKeyboard::LAYOUT_FINNISH}, |
| 60 {L"0000040c", MockKeyboard::LAYOUT_FRENCH}, |
| 61 {L"0000040d", MockKeyboard::LAYOUT_HEBREW}, |
| 62 {L"0000040e", MockKeyboard::LAYOUT_HUNGARIAN}, |
| 63 {L"00000410", MockKeyboard::LAYOUT_ITALIAN}, |
| 64 {L"00000411", MockKeyboard::LAYOUT_JAPANESE}, |
| 65 {L"00000412", MockKeyboard::LAYOUT_KOREAN}, |
| 66 {L"00000415", MockKeyboard::LAYOUT_POLISH}, |
| 67 {L"00000416", MockKeyboard::LAYOUT_PORTUGUESE_BRAZILIAN}, |
| 68 {L"00000418", MockKeyboard::LAYOUT_ROMANIAN}, |
| 69 {L"00000419", MockKeyboard::LAYOUT_RUSSIAN}, |
| 70 {L"0000041a", MockKeyboard::LAYOUT_CROATIAN}, |
| 71 {L"0000041b", MockKeyboard::LAYOUT_SLOVAK}, |
| 72 {L"0000041e", MockKeyboard::LAYOUT_THAI}, |
| 73 {L"0000041d", MockKeyboard::LAYOUT_SWEDISH}, |
| 74 {L"0000041f", MockKeyboard::LAYOUT_TURKISH_Q}, |
| 75 {L"0000042a", MockKeyboard::LAYOUT_VIETNAMESE}, |
| 76 {L"00000439", MockKeyboard::LAYOUT_DEVANAGARI_INSCRIPT}, |
| 77 {L"00000816", MockKeyboard::LAYOUT_PORTUGUESE}, |
| 78 {L"00001409", MockKeyboard::LAYOUT_UNITED_STATES_DVORAK}, |
| 79 {L"00001009", MockKeyboard::LAYOUT_CANADIAN_FRENCH}, |
| 80 }; |
| 81 |
| 82 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kLanguageIDs); ++i) { |
| 83 if (layout == kLanguageIDs[i].keyboard_layout) { |
| 84 keyboard_handle_ = LoadKeyboardLayout(kLanguageIDs[i].language, |
| 85 KLF_ACTIVATE); |
| 86 if (!keyboard_handle_) |
| 87 return false; |
| 88 return true; |
| 89 } |
| 90 } |
| 91 |
| 92 // Return false if there are not any matching drivers. |
| 93 return false; |
| 94 } |
| 95 |
| 96 bool MockKeyboardDriverWin::SetModifiers(int modifiers) { |
| 97 // Over-write the keyboard status with our modifier-key status. |
| 98 // WebInputEventFactory::keyboardEvent() uses GetKeyState() to retrive |
| 99 // modifier-key status. So, we update the modifier-key status with this |
| 100 // SetKeyboardState() call before creating NativeWebKeyboardEvent |
| 101 // instances. |
| 102 memset(&keyboard_states_[0], 0, sizeof(keyboard_states_)); |
| 103 static const struct { |
| 104 int key_code; |
| 105 int mask; |
| 106 } kModifierMasks[] = { |
| 107 {VK_SHIFT, MockKeyboard::LEFT_SHIFT | MockKeyboard::RIGHT_SHIFT}, |
| 108 {VK_CONTROL, MockKeyboard::LEFT_CONTROL | MockKeyboard::RIGHT_CONTROL}, |
| 109 {VK_MENU, MockKeyboard::LEFT_ALT | MockKeyboard::RIGHT_ALT}, |
| 110 {VK_LSHIFT, MockKeyboard::LEFT_SHIFT}, |
| 111 {VK_LCONTROL, MockKeyboard::LEFT_CONTROL}, |
| 112 {VK_LMENU, MockKeyboard::LEFT_ALT}, |
| 113 {VK_RSHIFT, MockKeyboard::RIGHT_SHIFT}, |
| 114 {VK_RCONTROL, MockKeyboard::RIGHT_CONTROL}, |
| 115 {VK_RMENU, MockKeyboard::RIGHT_ALT}, |
| 116 }; |
| 117 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kModifierMasks); ++i) { |
| 118 const int kKeyDownMask = 0x80; |
| 119 if (modifiers & kModifierMasks[i].mask) |
| 120 keyboard_states_[kModifierMasks[i].key_code] = kKeyDownMask; |
| 121 } |
| 122 SetKeyboardState(&keyboard_states_[0]); |
| 123 |
| 124 return true; |
| 125 } |
| 126 |
| 127 int MockKeyboardDriverWin::GetCharacters(int key_code, |
| 128 std::wstring* output) { |
| 129 // Retrieve Unicode characters composed from the input key-code and |
| 130 // the mofifiers. |
| 131 CHECK(output); |
| 132 wchar_t code[16]; |
| 133 int length = ToUnicodeEx(key_code, MapVirtualKey(key_code, 0), |
| 134 &keyboard_states_[0], &code[0], |
| 135 ARRAYSIZE_UNSAFE(code), 0, |
| 136 keyboard_handle_); |
| 137 if (length > 0) |
| 138 output->assign(code); |
| 139 return length; |
| 140 } |
OLD | NEW |