OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 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/browser/ui/views/ash/key_rewriter.h" |
| 6 |
| 7 #include <vector> |
| 8 |
| 9 #include "ash/shell.h" |
| 10 #include "base/logging.h" |
| 11 #include "base/string_util.h" |
| 12 #include "ui/aura/event.h" |
| 13 #include "ui/aura/root_window.h" |
| 14 #include "ui/base/keycodes/keyboard_code_conversion.h" |
| 15 #include "ui/base/keycodes/keyboard_codes.h" |
| 16 |
| 17 #if defined(OS_CHROMEOS) |
| 18 #include <X11/extensions/XInput2.h> |
| 19 #include <X11/keysym.h> |
| 20 #include <X11/Xlib.h> |
| 21 |
| 22 #include "base/chromeos/chromeos_version.h" |
| 23 #include "chrome/browser/chromeos/xinput_hierarchy_changed_event_listener.h" |
| 24 #include "ui/base/keycodes/keyboard_code_conversion_x.h" |
| 25 #include "ui/base/x/x11_util.h" |
| 26 #endif |
| 27 |
| 28 namespace { |
| 29 |
| 30 const int kBadDeviceId = -1; |
| 31 |
| 32 } // namespace |
| 33 |
| 34 KeyRewriter::KeyRewriter() : last_device_id_(kBadDeviceId) { |
| 35 // The ash shell isn't instantiated for our unit tests. |
| 36 if (ash::Shell::HasInstance()) |
| 37 ash::Shell::GetInstance()->GetRootWindow()->AddRootWindowObserver(this); |
| 38 #if defined(OS_CHROMEOS) |
| 39 if (base::chromeos::IsRunningOnChromeOS()) { |
| 40 chromeos::XInputHierarchyChangedEventListener::GetInstance() |
| 41 ->AddObserver(this); |
| 42 } |
| 43 RefreshKeycodes(); |
| 44 #endif |
| 45 } |
| 46 |
| 47 KeyRewriter::~KeyRewriter() { |
| 48 if (ash::Shell::HasInstance()) |
| 49 ash::Shell::GetInstance()->GetRootWindow()->RemoveRootWindowObserver(this); |
| 50 #if defined(OS_CHROMEOS) |
| 51 if (base::chromeos::IsRunningOnChromeOS()) { |
| 52 chromeos::XInputHierarchyChangedEventListener::GetInstance() |
| 53 ->RemoveObserver(this); |
| 54 } |
| 55 #endif |
| 56 } |
| 57 |
| 58 KeyRewriter::DeviceType KeyRewriter::DeviceAddedForTesting( |
| 59 int device_id, |
| 60 const std::string& device_name) { |
| 61 return DeviceAddedInternal(device_id, device_name); |
| 62 } |
| 63 |
| 64 // static |
| 65 KeyRewriter::DeviceType KeyRewriter::GetDeviceType( |
| 66 const std::string& device_name) { |
| 67 std::vector<std::string> tokens; |
| 68 Tokenize(device_name, " .", &tokens); |
| 69 |
| 70 // If the |device_name| contains the two words, "apple" and "keyboard", treat |
| 71 // it as an Apple keyboard. |
| 72 bool found_apple = false; |
| 73 bool found_keyboard = false; |
| 74 for (size_t i = 0; i < tokens.size(); ++i) { |
| 75 if (!found_apple && LowerCaseEqualsASCII(tokens[i], "apple")) |
| 76 found_apple = true; |
| 77 if (!found_keyboard && LowerCaseEqualsASCII(tokens[i], "keyboard")) |
| 78 found_keyboard = true; |
| 79 if (found_apple && found_keyboard) |
| 80 return kDeviceAppleKeyboard; |
| 81 } |
| 82 |
| 83 return kDeviceUnknown; |
| 84 } |
| 85 |
| 86 void KeyRewriter::RewriteCommandToControlForTesting(aura::KeyEvent* event) { |
| 87 RewriteCommandToControl(event); |
| 88 } |
| 89 |
| 90 ash::KeyRewriterDelegate::Action KeyRewriter::RewriteOrFilterKeyEvent( |
| 91 aura::KeyEvent* event) { |
| 92 const ash::KeyRewriterDelegate::Action kActionRewrite = |
| 93 ash::KeyRewriterDelegate::ACTION_REWRITE_EVENT; |
| 94 if (!event->HasNativeEvent()) { |
| 95 // Do not handle a fabricated event generated by tests. |
| 96 return kActionRewrite; |
| 97 } |
| 98 |
| 99 RewriteCommandToControl(event); |
| 100 // TODO(yusukes): Implement crbug.com/115112 (Search/Ctrl/Alt remapping) and |
| 101 // crosbug.com/27167 (allow sending function keys) here. |
| 102 |
| 103 return kActionRewrite; // Do not drop the event. |
| 104 } |
| 105 |
| 106 void KeyRewriter::OnKeyboardMappingChanged(const aura::RootWindow* root) { |
| 107 #if defined(OS_CHROMEOS) |
| 108 RefreshKeycodes(); |
| 109 #endif |
| 110 } |
| 111 |
| 112 #if defined(OS_CHROMEOS) |
| 113 void KeyRewriter::DeviceAdded(int device_id) { |
| 114 DCHECK_NE(XIAllDevices, device_id); |
| 115 DCHECK_NE(XIAllMasterDevices, device_id); |
| 116 if (device_id == XIAllDevices || device_id == XIAllMasterDevices) { |
| 117 LOG(ERROR) << "Unexpected device_id passed: " << device_id; |
| 118 return; |
| 119 } |
| 120 |
| 121 int ndevices_return = 0; |
| 122 XIDeviceInfo* device_info = XIQueryDevice(ui::GetXDisplay(), |
| 123 device_id, |
| 124 &ndevices_return); |
| 125 |
| 126 // Since |device_id| is neither XIAllDevices nor XIAllMasterDevices, |
| 127 // the number of devices found should be either 0 (not found) or 1. |
| 128 if (!device_info) { |
| 129 LOG(ERROR) << "XIQueryDevice: Device ID " << device_id << " is unknown."; |
| 130 return; |
| 131 } |
| 132 |
| 133 DCHECK_EQ(1, ndevices_return); |
| 134 for (int i = 0; i < ndevices_return; ++i) { |
| 135 DCHECK_EQ(device_id, device_info[i].deviceid); // see the comment above. |
| 136 DCHECK(device_info[i].name); |
| 137 DeviceAddedInternal(device_info[i].deviceid, device_info[i].name); |
| 138 } |
| 139 |
| 140 XIFreeDeviceInfo(device_info); |
| 141 } |
| 142 |
| 143 void KeyRewriter::DeviceRemoved(int device_id) { |
| 144 device_id_to_type_.erase(device_id); |
| 145 } |
| 146 |
| 147 void KeyRewriter::DeviceKeyPressedOrReleased(int device_id) { |
| 148 std::map<int, DeviceType>::const_iterator iter = |
| 149 device_id_to_type_.find(device_id); |
| 150 if (iter == device_id_to_type_.end()) { |
| 151 // |device_id| is unknown. This means the device was connected before |
| 152 // booting the OS. Query the name of the device and add it to the map. |
| 153 DeviceAdded(device_id); |
| 154 } |
| 155 |
| 156 last_device_id_ = device_id; |
| 157 } |
| 158 |
| 159 void KeyRewriter::RefreshKeycodes() { |
| 160 control_l_xkeycode_ = XKeysymToKeycode(ui::GetXDisplay(), XK_Control_L); |
| 161 control_r_xkeycode_ = XKeysymToKeycode(ui::GetXDisplay(), XK_Control_R); |
| 162 } |
| 163 #endif |
| 164 |
| 165 void KeyRewriter::RewriteCommandToControl(aura::KeyEvent* event) { |
| 166 if (last_device_id_ == kBadDeviceId) |
| 167 return; |
| 168 |
| 169 // Check which device generated |event|. |
| 170 std::map<int, DeviceType>::const_iterator iter = |
| 171 device_id_to_type_.find(last_device_id_); |
| 172 if (iter == device_id_to_type_.end()) { |
| 173 LOG(ERROR) << "Device ID " << last_device_id_ << " is unknown."; |
| 174 return; |
| 175 } |
| 176 |
| 177 const DeviceType type = iter->second; |
| 178 if (type != kDeviceAppleKeyboard) |
| 179 return; |
| 180 |
| 181 #if defined(OS_CHROMEOS) |
| 182 XEvent* xev = event->native_event(); |
| 183 XKeyEvent* xkey = &(xev->xkey); |
| 184 |
| 185 // Mod4 is the Windows key on a PC keyboard or Command key on an Apple |
| 186 // keyboard. |
| 187 if (xkey->state & Mod4Mask) { |
| 188 xkey->state &= ~Mod4Mask; |
| 189 xkey->state |= ControlMask; |
| 190 event->set_flags(event->flags() | ui::EF_CONTROL_DOWN); |
| 191 } |
| 192 |
| 193 KeySym keysym = XLookupKeysym(xkey, 0); |
| 194 switch (keysym) { |
| 195 case XK_Super_L: |
| 196 // left Command -> left Control |
| 197 xkey->keycode = control_l_xkeycode_; |
| 198 event->set_key_code(ui::VKEY_LCONTROL); |
| 199 event->set_character(ui::GetCharacterFromKeyCode(event->key_code(), |
| 200 event->flags())); |
| 201 break; |
| 202 case XK_Super_R: |
| 203 // right Command -> right Control |
| 204 xkey->keycode = control_r_xkeycode_; |
| 205 event->set_key_code(ui::VKEY_RCONTROL); |
| 206 event->set_character(ui::GetCharacterFromKeyCode(event->key_code(), |
| 207 event->flags())); |
| 208 break; |
| 209 default: |
| 210 break; |
| 211 } |
| 212 |
| 213 DCHECK_NE(ui::VKEY_LWIN, ui::KeyboardCodeFromXKeyEvent(xev)); |
| 214 DCHECK_NE(ui::VKEY_RWIN, ui::KeyboardCodeFromXKeyEvent(xev)); |
| 215 #else |
| 216 // TODO(yusukes): Support Ash on other platforms if needed. |
| 217 NOTIMPLEMENTED(); |
| 218 #endif |
| 219 } |
| 220 |
| 221 KeyRewriter::DeviceType KeyRewriter::DeviceAddedInternal( |
| 222 int device_id, |
| 223 const std::string& device_name) { |
| 224 const DeviceType type = KeyRewriter::GetDeviceType(device_name); |
| 225 if (type == kDeviceAppleKeyboard) { |
| 226 VLOG(1) << "Apple keyboard '" << device_name << "' connected: " |
| 227 << "id=" << device_id; |
| 228 } |
| 229 // Always overwrite the existing device_id since the X server may reuse a |
| 230 // device id for an unattached device. |
| 231 device_id_to_type_[device_id] = type; |
| 232 return type; |
| 233 } |
OLD | NEW |