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

Unified Diff: chrome/browser/ui/views/ash/key_rewriter.cc

Issue 9854025: Automatically remap Command key on an Apple keyboard to Control [part 2 of 2 - Chrome part] (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 8 years, 9 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « chrome/browser/ui/views/ash/key_rewriter.h ('k') | chrome/browser/ui/views/ash/key_rewriter_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/browser/ui/views/ash/key_rewriter.cc
diff --git a/chrome/browser/ui/views/ash/key_rewriter.cc b/chrome/browser/ui/views/ash/key_rewriter.cc
new file mode 100644
index 0000000000000000000000000000000000000000..9de31dc826f8076c9c39732df79a4b66bff2eb39
--- /dev/null
+++ b/chrome/browser/ui/views/ash/key_rewriter.cc
@@ -0,0 +1,204 @@
+// Copyright (c) 2012 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 "chrome/browser/ui/views/ash/key_rewriter.h"
+
+#include <vector>
+
+#include "base/logging.h"
+#include "base/string_util.h"
+#include "ui/aura/event.h"
+#include "ui/base/keycodes/keyboard_codes.h"
+
+#if defined(OS_CHROMEOS)
+#include "base/chromeos/chromeos_version.h"
+#include "chrome/browser/chromeos/xinput_hierarchy_changed_event_listener.h"
+#include "ui/base/keycodes/keyboard_code_conversion_x.h"
+#include "ui/base/x/x11_util.h"
+
+#include <X11/extensions/XInput2.h>
+#include <X11/keysymdef.h>
+#include <X11/Xlib.h>
+#endif
+
+namespace {
+
+const int kBadDeviceId = -1;
+
+#if defined(OS_CHROMEOS)
+// TODO(yusukes): It's better not to use the hard coded values. However, I don't
+// know a good way (except scanning /usr/share/X11/xkb/keycodes/evdev in .gyp
+// which is also hacky) to get key codes from X key symbols like XK_Super_L.
+const unsigned int kKeyCodeSuperL = 133; // Left Command or Windows key.
+const unsigned int kKeyCodeSuperR = 134; // Right Command or Windows key.
+const unsigned int kKeyCodeControlL = 37; // Left Control key.
+const unsigned int kKeyCodeControlR = 105; // Right Control key.
+#endif
+
+} // namespace
+
+KeyRewriter::KeyRewriter() : last_device_id_(kBadDeviceId) {
+#if defined(OS_CHROMEOS)
+ if (base::chromeos::IsRunningOnChromeOS()) {
+ chromeos::XInputHierarchyChangedEventListener::GetInstance()
+ ->AddObserver(this);
+ }
+#endif
+}
+KeyRewriter::~KeyRewriter() {
+#if defined(OS_CHROMEOS)
+ if (base::chromeos::IsRunningOnChromeOS()) {
+ chromeos::XInputHierarchyChangedEventListener::GetInstance()
+ ->RemoveObserver(this);
+ }
+#endif
+}
+
+KeyRewriter::DeviceType KeyRewriter::DeviceAddedForTesting(
+ int device_id,
+ const std::string& device_name) {
+ return DeviceAddedInternal(device_id, device_name);
+}
+
+// static
+KeyRewriter::DeviceType KeyRewriter::GetDeviceType(
+ const std::string& device_name) {
+ std::vector<std::string> tokens;
+ Tokenize(device_name, " .", &tokens);
+
+ // If the |device_name| contains the two words, "apple" and "keyboard", treat
+ // it as an Apple keyboard.
+ bool found_apple = false;
+ bool found_keyboard = false;
+ for (size_t i = 0; i < tokens.size(); ++i) {
+ if (!found_apple && LowerCaseEqualsASCII(tokens[i], "apple"))
+ found_apple = true;
+ if (!found_keyboard && LowerCaseEqualsASCII(tokens[i], "keyboard"))
+ found_keyboard = true;
+ if (found_apple && found_keyboard)
+ return kDeviceAppleKeyboard;
+ }
+
+ return kDevicePcKeyboard;
+}
+
+void KeyRewriter::RewriteCommandToControlForTesting(aura::KeyEvent* event) {
+ RewriteCommandToControl(event);
+}
+
+ash::KeyRewriterDelegate::Action KeyRewriter::RewriteOrFilterKeyEvent(
+ aura::KeyEvent* event) {
+ const ash::KeyRewriterDelegate::Action kActionRewrite =
+ ash::KeyRewriterDelegate::ACTION_REWRITE_EVENT;
+ if (!event->HasNativeEvent()) {
+ // Do not handle a fabricated event generated by tests.
+ return kActionRewrite;
+ }
+
+ RewriteCommandToControl(event);
+ // TODO(yusukes): Implement crbug.com/115112 (Search/Ctrl/Alt remapping) and
+ // crosbug.com/27167 (allow sending function keys) here.
+
+ return kActionRewrite; // Do not drop the event.
+}
+
+#if defined(OS_CHROMEOS)
+void KeyRewriter::DeviceAdded(int device_id) {
+ DCHECK_NE(XIAllDevices, device_id);
+ DCHECK_NE(XIAllMasterDevices, device_id);
+ if (device_id == XIAllDevices || device_id == XIAllMasterDevices) {
+ LOG(ERROR) << "Unexpected device_id passed: " << device_id;
+ return;
+ }
+
+ int ndevices_return = 0;
+ XIDeviceInfo* device_info = XIQueryDevice(ui::GetXDisplay(),
+ device_id,
+ &ndevices_return);
+
+ // Since |device_id| is neither XIAllDevices nor XIAllMasterDevices,
+ // The number of devices found should be either 0 (not found) or 1.
+ if (!device_info) {
+ LOG(ERROR) << "XIQueryDevice: Device ID " << device_id << " is unknown.";
+ return;
+ }
+
+ DCHECK_EQ(1, ndevices_return);
+ for (int i = 0; i < ndevices_return; ++i) {
+ DCHECK_EQ(device_id, device_info[i].deviceid); // see the comment above.
+ DCHECK(device_info[i].name);
+ DeviceAddedInternal(device_info[i].deviceid, device_info[i].name);
+ }
+
+ XIFreeDeviceInfo(device_info);
+}
+
+void KeyRewriter::KeyPressedOrReleased(int device_id) {
+ std::map<int, DeviceType>::const_iterator iter =
+ device_id_to_type_.find(device_id);
+ if (iter == device_id_to_type_.end()) {
+ // |device_id| is unknown. This means the device was connected before
+ // booting the OS. Query the name of the device and add it to the map.
+ DeviceAdded(device_id);
+ }
+
+ last_device_id_ = device_id;
+}
+#endif
+
+void KeyRewriter::RewriteCommandToControl(aura::KeyEvent* event) {
+ if (last_device_id_ == kBadDeviceId)
+ return;
+
+ // Check the device which generated the |event|.
+ std::map<int, DeviceType>::const_iterator iter =
+ device_id_to_type_.find(last_device_id_);
+ if (iter == device_id_to_type_.end()) {
+ LOG(ERROR) << "Device ID " << last_device_id_ << " is unknown.";
+ return;
+ }
+
+ const DeviceType type = iter->second;
+ if (type != kDeviceAppleKeyboard)
+ return;
+
+#if defined(OS_CHROMEOS)
+ XEvent* xev = event->native_event();
+ XKeyEvent* xkey = &(xev->xkey);
+
+ // Remap Command key on an Apple keyboard to Control.
+ // Handle Command-L key press/release.
+ if (xkey->keycode == kKeyCodeSuperL)
+ xkey->keycode = kKeyCodeControlL;
+ // Handle Command-R key press/release.
+ if (xkey->keycode == kKeyCodeSuperR)
+ xkey->keycode = kKeyCodeControlR;
+ // Handle Command+<other-key> press/release.
+ if (xkey->state & Mod4Mask) {
+ // Mod4 is Windows key on a PC keyboard or Command key on an Apple keyboard.
+ xkey->state &= ~Mod4Mask;
+ xkey->state |= ControlMask;
+ }
+
+ DCHECK_NE(ui::VKEY_LWIN, ui::KeyboardCodeFromXKeyEvent(xev));
+ DCHECK_NE(ui::VKEY_RWIN, ui::KeyboardCodeFromXKeyEvent(xev));
+#else
+ // TODO(yusukes): Support Ash on other platforms if needed.
+ NOTIMPLEMENTED();
+#endif
+}
+
+KeyRewriter::DeviceType KeyRewriter::DeviceAddedInternal(
+ int device_id,
+ const std::string& device_name) {
+ const DeviceType type = KeyRewriter::GetDeviceType(device_name);
+ if (type == kDeviceAppleKeyboard) {
+ LOG(WARNING) << "An Apple keyboard '" << device_name << "' is connected: "
+ << "device_id=" << device_id;
+ }
+ // Always overwrite the existing device_id since the X server may reuse a
+ // device id for an unattached device.
+ device_id_to_type_[device_id] = type;
+ return type;
+}
« no previous file with comments | « chrome/browser/ui/views/ash/key_rewriter.h ('k') | chrome/browser/ui/views/ash/key_rewriter_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698