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 "base/logging.h" |
| 10 #include "base/string_util.h" |
| 11 #include "ui/aura/event.h" |
| 12 #include "ui/base/keycodes/keyboard_codes.h" |
| 13 |
| 14 #if defined(OS_CHROMEOS) |
| 15 #include "base/chromeos/chromeos_version.h" |
| 16 #include "chrome/browser/chromeos/xinput_hierarchy_changed_event_listener.h" |
| 17 #include "ui/base/keycodes/keyboard_code_conversion_x.h" |
| 18 #include "ui/base/x/x11_util.h" |
| 19 |
| 20 #include <X11/extensions/XInput2.h> |
| 21 #include <X11/keysymdef.h> |
| 22 #include <X11/Xlib.h> |
| 23 #endif |
| 24 |
| 25 namespace { |
| 26 |
| 27 const int kBadDeviceId = -1; |
| 28 |
| 29 #if defined(OS_CHROMEOS) |
| 30 // TODO(yusukes): It's better not to use the hard coded values. However, I don't |
| 31 // know a good way (except scanning /usr/share/X11/xkb/keycodes/evdev in .gyp |
| 32 // which is also hacky) to get key codes from X key symbols like XK_Super_L. |
| 33 const unsigned int kKeyCodeSuperL = 133; // Left Command or Windows key. |
| 34 const unsigned int kKeyCodeSuperR = 134; // Right Command or Windows key. |
| 35 const unsigned int kKeyCodeControlL = 37; // Left Control key. |
| 36 const unsigned int kKeyCodeControlR = 105; // Right Control key. |
| 37 #endif |
| 38 |
| 39 } // namespace |
| 40 |
| 41 KeyRewriter::KeyRewriter() : last_device_id_(kBadDeviceId) { |
| 42 #if defined(OS_CHROMEOS) |
| 43 if (base::chromeos::IsRunningOnChromeOS()) { |
| 44 chromeos::XInputHierarchyChangedEventListener::GetInstance() |
| 45 ->AddObserver(this); |
| 46 } |
| 47 #endif |
| 48 } |
| 49 KeyRewriter::~KeyRewriter() { |
| 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 kDevicePcKeyboard; |
| 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 #if defined(OS_CHROMEOS) |
| 107 void KeyRewriter::DeviceAdded(int device_id) { |
| 108 DCHECK_NE(XIAllDevices, device_id); |
| 109 DCHECK_NE(XIAllMasterDevices, device_id); |
| 110 if (device_id == XIAllDevices || device_id == XIAllMasterDevices) { |
| 111 LOG(ERROR) << "Unexpected device_id passed: " << device_id; |
| 112 return; |
| 113 } |
| 114 |
| 115 int ndevices_return = 0; |
| 116 XIDeviceInfo* device_info = XIQueryDevice(ui::GetXDisplay(), |
| 117 device_id, |
| 118 &ndevices_return); |
| 119 |
| 120 // Since |device_id| is neither XIAllDevices nor XIAllMasterDevices, |
| 121 // The number of devices found should be either 0 (not found) or 1. |
| 122 if (!device_info) { |
| 123 LOG(ERROR) << "XIQueryDevice: Device ID " << device_id << " is unknown."; |
| 124 return; |
| 125 } |
| 126 |
| 127 DCHECK_EQ(1, ndevices_return); |
| 128 for (int i = 0; i < ndevices_return; ++i) { |
| 129 DCHECK_EQ(device_id, device_info[i].deviceid); // see the comment above. |
| 130 DCHECK(device_info[i].name); |
| 131 DeviceAddedInternal(device_info[i].deviceid, device_info[i].name); |
| 132 } |
| 133 |
| 134 XIFreeDeviceInfo(device_info); |
| 135 } |
| 136 |
| 137 void KeyRewriter::KeyPressedOrReleased(int device_id) { |
| 138 std::map<int, DeviceType>::const_iterator iter = |
| 139 device_id_to_type_.find(device_id); |
| 140 if (iter == device_id_to_type_.end()) { |
| 141 // |device_id| is unknown. This means the device was connected before |
| 142 // booting the OS. Query the name of the device and add it to the map. |
| 143 DeviceAdded(device_id); |
| 144 } |
| 145 |
| 146 last_device_id_ = device_id; |
| 147 } |
| 148 #endif |
| 149 |
| 150 void KeyRewriter::RewriteCommandToControl(aura::KeyEvent* event) { |
| 151 if (last_device_id_ == kBadDeviceId) |
| 152 return; |
| 153 |
| 154 // Check the device which generated the |event|. |
| 155 std::map<int, DeviceType>::const_iterator iter = |
| 156 device_id_to_type_.find(last_device_id_); |
| 157 if (iter == device_id_to_type_.end()) { |
| 158 LOG(ERROR) << "Device ID " << last_device_id_ << " is unknown."; |
| 159 return; |
| 160 } |
| 161 |
| 162 const DeviceType type = iter->second; |
| 163 if (type != kDeviceAppleKeyboard) |
| 164 return; |
| 165 |
| 166 #if defined(OS_CHROMEOS) |
| 167 XEvent* xev = event->native_event(); |
| 168 XKeyEvent* xkey = &(xev->xkey); |
| 169 |
| 170 // Remap Command key on an Apple keyboard to Control. |
| 171 // Handle Command-L key press/release. |
| 172 if (xkey->keycode == kKeyCodeSuperL) |
| 173 xkey->keycode = kKeyCodeControlL; |
| 174 // Handle Command-R key press/release. |
| 175 if (xkey->keycode == kKeyCodeSuperR) |
| 176 xkey->keycode = kKeyCodeControlR; |
| 177 // Handle Command+<other-key> press/release. |
| 178 if (xkey->state & Mod4Mask) { |
| 179 // Mod4 is Windows key on a PC keyboard or Command key on an Apple keyboard. |
| 180 xkey->state &= ~Mod4Mask; |
| 181 xkey->state |= ControlMask; |
| 182 } |
| 183 |
| 184 DCHECK_NE(ui::VKEY_LWIN, ui::KeyboardCodeFromXKeyEvent(xev)); |
| 185 DCHECK_NE(ui::VKEY_RWIN, ui::KeyboardCodeFromXKeyEvent(xev)); |
| 186 #else |
| 187 // TODO(yusukes): Support Ash on other platforms if needed. |
| 188 NOTIMPLEMENTED(); |
| 189 #endif |
| 190 } |
| 191 |
| 192 KeyRewriter::DeviceType KeyRewriter::DeviceAddedInternal( |
| 193 int device_id, |
| 194 const std::string& device_name) { |
| 195 const DeviceType type = KeyRewriter::GetDeviceType(device_name); |
| 196 if (type == kDeviceAppleKeyboard) { |
| 197 LOG(WARNING) << "An Apple keyboard '" << device_name << "' is connected: " |
| 198 << "device_id=" << device_id; |
| 199 } |
| 200 // Always overwrite the existing device_id since the X server may reuse a |
| 201 // device id for an unattached device. |
| 202 device_id_to_type_[device_id] = type; |
| 203 return type; |
| 204 } |
OLD | NEW |