| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_UI_VIEWS_ASH_KEY_REWRITER_H_ | 5 #ifndef CHROME_BROWSER_UI_VIEWS_ASH_KEY_REWRITER_H_ |
| 6 #define CHROME_BROWSER_UI_VIEWS_ASH_KEY_REWRITER_H_ | 6 #define CHROME_BROWSER_UI_VIEWS_ASH_KEY_REWRITER_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <map> | 9 #include <map> |
| 10 #include <string> | 10 #include <string> |
| 11 | 11 |
| 12 #include "ash/key_rewriter_delegate.h" | 12 #include "ash/key_rewriter_delegate.h" |
| 13 #include "base/basictypes.h" | 13 #include "base/basictypes.h" |
| 14 #include "base/compiler_specific.h" | 14 #include "base/compiler_specific.h" |
| 15 #include "ui/aura/root_window_observer.h" | 15 #include "ui/aura/root_window_observer.h" |
| 16 #include "ui/base/keycodes/keyboard_codes.h" | 16 #include "ui/base/keycodes/keyboard_codes.h" |
| 17 | 17 |
| 18 #if defined(OS_CHROMEOS) | 18 #if defined(OS_CHROMEOS) |
| 19 #include "chrome/browser/chromeos/device_hierarchy_observer.h" | 19 #include "chrome/browser/chromeos/device_hierarchy_observer.h" |
| 20 #endif | 20 #endif |
| 21 | 21 |
| 22 class PrefService; |
| 23 |
| 22 namespace aura { | 24 namespace aura { |
| 23 class RootWindow; | 25 class RootWindow; |
| 24 } | 26 } |
| 25 | 27 |
| 28 #if defined(OS_CHROMEOS) |
| 29 namespace chromeos { |
| 30 namespace input_method { |
| 31 class XKeyboard; |
| 32 } |
| 33 } |
| 34 #endif |
| 35 |
| 26 class KeyRewriter : public ash::KeyRewriterDelegate, | 36 class KeyRewriter : public ash::KeyRewriterDelegate, |
| 27 public aura::RootWindowObserver | 37 public aura::RootWindowObserver |
| 28 #if defined(OS_CHROMEOS) | 38 #if defined(OS_CHROMEOS) |
| 29 , public chromeos::DeviceHierarchyObserver | 39 , public chromeos::DeviceHierarchyObserver |
| 30 #endif | 40 #endif |
| 31 { | 41 { |
| 32 public: | 42 public: |
| 33 enum DeviceType { | 43 enum DeviceType { |
| 34 kDeviceUnknown = 0, | 44 kDeviceUnknown = 0, |
| 35 kDeviceAppleKeyboard, | 45 kDeviceAppleKeyboard, |
| 36 }; | 46 }; |
| 37 | 47 |
| 38 KeyRewriter(); | 48 KeyRewriter(); |
| 39 virtual ~KeyRewriter(); | 49 virtual ~KeyRewriter(); |
| 40 | 50 |
| 41 // Calls DeviceAddedInternal. | 51 // Calls DeviceAddedInternal. |
| 42 DeviceType DeviceAddedForTesting(int device_id, | 52 DeviceType DeviceAddedForTesting(int device_id, |
| 43 const std::string& device_name); | 53 const std::string& device_name); |
| 44 // Calls Rewrite. | 54 // Calls Rewrite. |
| 45 void RewriteForTesting(aura::KeyEvent* event); | 55 void RewriteForTesting(aura::KeyEvent* event); |
| 46 | 56 |
| 47 const std::map<int, DeviceType>& device_id_to_type_for_testing() const { | 57 const std::map<int, DeviceType>& device_id_to_type_for_testing() const { |
| 48 return device_id_to_type_; | 58 return device_id_to_type_; |
| 49 } | 59 } |
| 50 void set_last_device_id_for_testing(int device_id) { | 60 void set_last_device_id_for_testing(int device_id) { |
| 51 last_device_id_ = device_id; | 61 last_device_id_ = device_id; |
| 52 } | 62 } |
| 63 void set_pref_service_for_testing(const PrefService* pref_service) { |
| 64 pref_service_ = pref_service; |
| 65 } |
| 66 #if defined(OS_CHROMEOS) |
| 67 void set_xkeyboard_for_testing(chromeos::input_method::XKeyboard* xkeyboard) { |
| 68 xkeyboard_ = xkeyboard; |
| 69 } |
| 70 #endif |
| 53 | 71 |
| 54 // Gets DeviceType from the |device_name|. | 72 // Gets DeviceType from the |device_name|. |
| 55 static DeviceType GetDeviceType(const std::string& device_name); | 73 static DeviceType GetDeviceType(const std::string& device_name); |
| 56 | 74 |
| 57 private: | 75 private: |
| 58 // ash::KeyRewriterDelegate overrides: | 76 // ash::KeyRewriterDelegate overrides: |
| 59 virtual ash::KeyRewriterDelegate::Action RewriteOrFilterKeyEvent( | 77 virtual ash::KeyRewriterDelegate::Action RewriteOrFilterKeyEvent( |
| 60 aura::KeyEvent* event) OVERRIDE; | 78 aura::KeyEvent* event) OVERRIDE; |
| 61 | 79 |
| 62 // aura::RootWindowObserver overrides: | 80 // aura::RootWindowObserver overrides: |
| 63 virtual void OnKeyboardMappingChanged(const aura::RootWindow* root) OVERRIDE; | 81 virtual void OnKeyboardMappingChanged(const aura::RootWindow* root) OVERRIDE; |
| 64 | 82 |
| 65 #if defined(OS_CHROMEOS) | 83 #if defined(OS_CHROMEOS) |
| 66 // chromeos::DeviceHierarchyObserver overrides: | 84 // chromeos::DeviceHierarchyObserver overrides: |
| 67 virtual void DeviceHierarchyChanged() OVERRIDE {} | 85 virtual void DeviceHierarchyChanged() OVERRIDE {} |
| 68 virtual void DeviceAdded(int device_id) OVERRIDE; | 86 virtual void DeviceAdded(int device_id) OVERRIDE; |
| 69 virtual void DeviceRemoved(int device_id) OVERRIDE; | 87 virtual void DeviceRemoved(int device_id) OVERRIDE; |
| 70 virtual void DeviceKeyPressedOrReleased(int device_id) OVERRIDE; | 88 virtual void DeviceKeyPressedOrReleased(int device_id) OVERRIDE; |
| 71 | 89 |
| 72 // Updates |*_xkeycode_| in response to a keyboard map change. | 90 // Updates |*_xkeycode_| in response to a keyboard map change. |
| 73 void RefreshKeycodes(); | 91 void RefreshKeycodes(); |
| 92 // Converts an X key symbol like XK_Control_L to a key code. |
| 93 unsigned char NativeKeySymToNativeKeycode(unsigned long keysym); |
| 74 #endif | 94 #endif |
| 75 | 95 |
| 76 // Rewrites the |event| by applying all RewriteXXX functions as needed. | 96 // Rewrites the |event| by applying all RewriteXXX functions as needed. |
| 77 void Rewrite(aura::KeyEvent* event); | 97 void Rewrite(aura::KeyEvent* event); |
| 78 | 98 |
| 79 // Rewrites Comment-L/R key presses on an Apple keyboard to Control-L/R. Only | 99 // Rewrites Comment-L/R key presses on an Apple keyboard to Control-L/R. Only |
| 80 // OS_CHROMEOS implementation is available at this point. Returns true when | 100 // OS_CHROMEOS implementation is available at this point. Returns true when |
| 81 // |event| is rewritten. | 101 // |event| is rewritten. |
| 82 bool RewriteCommandToControl(aura::KeyEvent* event); | 102 bool RewriteCommandToControl(aura::KeyEvent* event); |
| 83 | 103 |
| 104 // Rewrites a modifier key press/release following the current user |
| 105 // preferences. |
| 106 bool RewriteModifiers(aura::KeyEvent* event); |
| 107 |
| 84 // Rewrites a NumPad key press/release without Num Lock to a corresponding key | 108 // Rewrites a NumPad key press/release without Num Lock to a corresponding key |
| 85 // press/release with the lock. Returns true when |event| is rewritten. | 109 // press/release with the lock. Returns true when |event| is rewritten. |
| 86 bool RewriteNumPadKeys(aura::KeyEvent* event); | 110 bool RewriteNumPadKeys(aura::KeyEvent* event); |
| 87 | 111 |
| 88 // Overwrites |event| with the keycodes and flags. | 112 // Overwrites |event| with the keycodes and flags. |
| 89 void OverwriteEvent(aura::KeyEvent* event, | 113 void OverwriteEvent(aura::KeyEvent* event, |
| 90 unsigned int new_native_keycode, | 114 unsigned int new_native_keycode, |
| 91 unsigned int new_native_state, | 115 unsigned int new_native_state, |
| 92 ui::KeyboardCode new_keycode, | 116 ui::KeyboardCode new_keycode, |
| 93 int new_flags); | 117 int new_flags); |
| 94 | 118 |
| 95 // Checks the type of the |device_name|, and inserts a new entry to | 119 // Checks the type of the |device_name|, and inserts a new entry to |
| 96 // |device_id_to_type_|. | 120 // |device_id_to_type_|. |
| 97 DeviceType DeviceAddedInternal(int device_id, const std::string& device_name); | 121 DeviceType DeviceAddedInternal(int device_id, const std::string& device_name); |
| 98 | 122 |
| 99 std::map<int, DeviceType> device_id_to_type_; | 123 std::map<int, DeviceType> device_id_to_type_; |
| 100 int last_device_id_; | 124 int last_device_id_; |
| 101 | 125 |
| 102 #if defined(OS_CHROMEOS) | 126 #if defined(OS_CHROMEOS) |
| 103 // X keycodes corresponding to various keysyms. | 127 // X keycodes corresponding to various keysyms. |
| 104 unsigned int control_l_xkeycode_; | 128 unsigned int control_l_xkeycode_; |
| 105 unsigned int control_r_xkeycode_; | 129 unsigned int control_r_xkeycode_; |
| 130 unsigned int alt_l_xkeycode_; |
| 131 unsigned int alt_r_xkeycode_; |
| 132 unsigned int meta_l_xkeycode_; |
| 133 unsigned int meta_r_xkeycode_; |
| 134 unsigned int windows_l_xkeycode_; |
| 135 unsigned int caps_lock_xkeycode_; |
| 136 unsigned int void_symbol_xkeycode_; |
| 106 unsigned int kp_0_xkeycode_; | 137 unsigned int kp_0_xkeycode_; |
| 107 unsigned int kp_1_xkeycode_; | 138 unsigned int kp_1_xkeycode_; |
| 108 unsigned int kp_2_xkeycode_; | 139 unsigned int kp_2_xkeycode_; |
| 109 unsigned int kp_3_xkeycode_; | 140 unsigned int kp_3_xkeycode_; |
| 110 unsigned int kp_4_xkeycode_; | 141 unsigned int kp_4_xkeycode_; |
| 111 unsigned int kp_5_xkeycode_; | 142 unsigned int kp_5_xkeycode_; |
| 112 unsigned int kp_6_xkeycode_; | 143 unsigned int kp_6_xkeycode_; |
| 113 unsigned int kp_7_xkeycode_; | 144 unsigned int kp_7_xkeycode_; |
| 114 unsigned int kp_8_xkeycode_; | 145 unsigned int kp_8_xkeycode_; |
| 115 unsigned int kp_9_xkeycode_; | 146 unsigned int kp_9_xkeycode_; |
| 116 unsigned int kp_decimal_xkeycode_; | 147 unsigned int kp_decimal_xkeycode_; |
| 148 |
| 149 chromeos::input_method::XKeyboard* xkeyboard_; // for testing. |
| 117 #endif | 150 #endif |
| 118 | 151 |
| 152 const PrefService* pref_service_; // for testing. |
| 153 |
| 119 DISALLOW_COPY_AND_ASSIGN(KeyRewriter); | 154 DISALLOW_COPY_AND_ASSIGN(KeyRewriter); |
| 120 }; | 155 }; |
| 121 | 156 |
| 122 #endif // CHROME_BROWSER_UI_VIEWS_ASH_KEY_REWRITER_H_ | 157 #endif // CHROME_BROWSER_UI_VIEWS_ASH_KEY_REWRITER_H_ |
| OLD | NEW |