| 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 #ifndef CHROME_BROWSER_UI_ASH_EVENT_REWRITER_H_ | |
| 6 #define CHROME_BROWSER_UI_ASH_EVENT_REWRITER_H_ | |
| 7 | |
| 8 #include <map> | |
| 9 #include <set> | |
| 10 #include <string> | |
| 11 | |
| 12 #include "ash/event_rewriter_delegate.h" | |
| 13 #include "base/basictypes.h" | |
| 14 #include "base/compiler_specific.h" | |
| 15 #include "base/containers/hash_tables.h" | |
| 16 #include "base/memory/scoped_ptr.h" | |
| 17 #include "ui/aura/root_window_observer.h" | |
| 18 #include "ui/events/keycodes/keyboard_codes.h" | |
| 19 | |
| 20 #if defined(OS_CHROMEOS) | |
| 21 #include "chrome/browser/chromeos/device_hierarchy_observer.h" | |
| 22 #endif | |
| 23 | |
| 24 class PrefService; | |
| 25 | |
| 26 namespace aura { | |
| 27 class RootWindow; | |
| 28 } | |
| 29 | |
| 30 #if defined(OS_CHROMEOS) | |
| 31 namespace chromeos { | |
| 32 | |
| 33 class KeyboardDrivenEventRewriter; | |
| 34 | |
| 35 namespace input_method { | |
| 36 class XKeyboard; | |
| 37 } | |
| 38 } | |
| 39 #endif | |
| 40 | |
| 41 class EventRewriter : public ash::EventRewriterDelegate, | |
| 42 public aura::RootWindowObserver | |
| 43 #if defined(OS_CHROMEOS) | |
| 44 , public chromeos::DeviceHierarchyObserver | |
| 45 #endif | |
| 46 { | |
| 47 public: | |
| 48 enum DeviceType { | |
| 49 kDeviceUnknown = 0, | |
| 50 kDeviceAppleKeyboard, | |
| 51 }; | |
| 52 | |
| 53 EventRewriter(); | |
| 54 virtual ~EventRewriter(); | |
| 55 | |
| 56 // Calls DeviceAddedInternal. | |
| 57 DeviceType DeviceAddedForTesting(int device_id, | |
| 58 const std::string& device_name); | |
| 59 // Calls Rewrite. | |
| 60 void RewriteForTesting(ui::KeyEvent* event); | |
| 61 | |
| 62 const std::map<int, DeviceType>& device_id_to_type_for_testing() const { | |
| 63 return device_id_to_type_; | |
| 64 } | |
| 65 void set_last_device_id_for_testing(int device_id) { | |
| 66 last_device_id_ = device_id; | |
| 67 } | |
| 68 void set_pref_service_for_testing(const PrefService* pref_service) { | |
| 69 pref_service_for_testing_ = pref_service; | |
| 70 } | |
| 71 #if defined(OS_CHROMEOS) | |
| 72 void set_xkeyboard_for_testing(chromeos::input_method::XKeyboard* xkeyboard) { | |
| 73 xkeyboard_for_testing_ = xkeyboard; | |
| 74 } | |
| 75 #endif | |
| 76 | |
| 77 // Gets DeviceType from the |device_name|. | |
| 78 static DeviceType GetDeviceType(const std::string& device_name); | |
| 79 | |
| 80 private: | |
| 81 friend class EventRewriterAshTest; | |
| 82 friend class EventRewriterTest; | |
| 83 | |
| 84 // ash::EventRewriterDelegate overrides: | |
| 85 virtual ash::EventRewriterDelegate::Action RewriteOrFilterKeyEvent( | |
| 86 ui::KeyEvent* event) OVERRIDE; | |
| 87 virtual ash::EventRewriterDelegate::Action RewriteOrFilterLocatedEvent( | |
| 88 ui::LocatedEvent* event) OVERRIDE; | |
| 89 | |
| 90 // aura::RootWindowObserver overrides: | |
| 91 virtual void OnKeyboardMappingChanged(const aura::RootWindow* root) OVERRIDE; | |
| 92 | |
| 93 #if defined(OS_CHROMEOS) | |
| 94 // chromeos::DeviceHierarchyObserver overrides: | |
| 95 virtual void DeviceHierarchyChanged() OVERRIDE {} | |
| 96 virtual void DeviceAdded(int device_id) OVERRIDE; | |
| 97 virtual void DeviceRemoved(int device_id) OVERRIDE; | |
| 98 virtual void DeviceKeyPressedOrReleased(int device_id) OVERRIDE; | |
| 99 | |
| 100 // We don't want to include Xlib.h here since it has polluting macros, so | |
| 101 // define these locally. | |
| 102 typedef unsigned long KeySym; | |
| 103 typedef unsigned char KeyCode; | |
| 104 | |
| 105 // Updates |*_xkeycode_| in response to a keyboard map change. | |
| 106 void RefreshKeycodes(); | |
| 107 // Converts an X key symbol like XK_Control_L to a key code. | |
| 108 unsigned char NativeKeySymToNativeKeycode(KeySym keysym); | |
| 109 | |
| 110 struct KeyboardRemapping { | |
| 111 KeySym input_keysym; | |
| 112 unsigned int input_mods; | |
| 113 unsigned int input_native_mods; | |
| 114 KeySym output_keysym; | |
| 115 ui::KeyboardCode output_keycode; | |
| 116 unsigned int output_mods; | |
| 117 unsigned int output_native_mods; | |
| 118 }; | |
| 119 | |
| 120 // Returns true if the target for |event| would prefer to receive raw function | |
| 121 // keys instead of having them rewritten into back, forward, brightness, | |
| 122 // volume, etc. or if the user has specified that they desire top-row keys to | |
| 123 // be treated as function keys globally. | |
| 124 bool TopRowKeysAreFunctionKeys(ui::KeyEvent* event) const; | |
| 125 | |
| 126 // Given a set of KeyboardRemapping structs, it finds a matching struct | |
| 127 // if possible, and updates the remapped event values. Returns true if a | |
| 128 // remapping was found and remapped values were updated. | |
| 129 bool RewriteWithKeyboardRemappingsByKeySym( | |
| 130 const KeyboardRemapping* remappings, | |
| 131 size_t num_remappings, | |
| 132 KeySym keysym, | |
| 133 unsigned int native_mods, | |
| 134 unsigned int mods, | |
| 135 KeySym* remapped_native_keysym, | |
| 136 unsigned int* remapped_native_mods, | |
| 137 ui::KeyboardCode* remapped_keycode, | |
| 138 unsigned int* remapped_mods); | |
| 139 | |
| 140 // Given a set of KeyboardRemapping structs, it finds a matching struct | |
| 141 // if possible, and updates the remapped event values. This function converts | |
| 142 // the KeySym in the KeyboardRemapping struct into the KeyCode before matching | |
| 143 // to allow any KeyCode on the same physical key as the given KeySym to match. | |
| 144 // Returns true if a remapping was found and remapped values were updated. | |
| 145 bool RewriteWithKeyboardRemappingsByKeyCode( | |
| 146 const KeyboardRemapping* remappings, | |
| 147 size_t num_remappings, | |
| 148 KeyCode keycode, | |
| 149 unsigned int native_mods, | |
| 150 unsigned int mods, | |
| 151 KeySym* remapped_native_keysym, | |
| 152 unsigned int* remapped_native_mods, | |
| 153 ui::KeyboardCode* remapped_keycode, | |
| 154 unsigned int* remapped_mods); | |
| 155 #endif | |
| 156 | |
| 157 // Returns the PrefService that should be used. | |
| 158 const PrefService* GetPrefService() const; | |
| 159 | |
| 160 // Rewrites the |event| by applying all RewriteXXX functions as needed. | |
| 161 void Rewrite(ui::KeyEvent* event); | |
| 162 | |
| 163 // Rewrites a modifier key press/release following the current user | |
| 164 // preferences. | |
| 165 bool RewriteModifiers(ui::KeyEvent* event); | |
| 166 | |
| 167 // Rewrites Fn key press/release to Control. In some cases, Fn key is not | |
| 168 // intercepted by the EC, but generates a key event like "XK_F15 + Mod3Mask" | |
| 169 // as shown in crosbug.com/p/14339. | |
| 170 bool RewriteFnKey(ui::KeyEvent* event); | |
| 171 | |
| 172 // Rewrites a NumPad key press/release without Num Lock to a corresponding key | |
| 173 // press/release with the lock. Returns true when |event| is rewritten. | |
| 174 bool RewriteNumPadKeys(ui::KeyEvent* event); | |
| 175 | |
| 176 // Rewrites Backspace and Arrow keys following the Chrome OS keyboard spec. | |
| 177 // * Alt+Backspace -> Delete | |
| 178 // * Alt+Up -> Prior (aka PageUp) | |
| 179 // * Alt+Down -> Next (aka PageDown) | |
| 180 // * Ctrl+Alt+Up -> Home | |
| 181 // * Ctrl+Alt+Down -> End | |
| 182 // When the Search key acts as a function key, it instead maps: | |
| 183 // * Search+Backspace -> Delete | |
| 184 // * Search+Up -> Prior (aka PageUp) | |
| 185 // * Search+Down -> Next (aka PageDown) | |
| 186 // * Search+Left -> Home | |
| 187 // * Search+Right -> End | |
| 188 // * Search+. -> Insert | |
| 189 // Returns true when the |event| is rewritten. | |
| 190 bool RewriteExtendedKeys(ui::KeyEvent* event); | |
| 191 | |
| 192 // When the Search key acts as a function key, it remaps Search+1 | |
| 193 // through Search+= to F1 through F12. Returns true when the |event| is | |
| 194 // rewritten. | |
| 195 bool RewriteFunctionKeys(ui::KeyEvent* event); | |
| 196 | |
| 197 // Rewrites the located |event|. | |
| 198 void RewriteLocatedEvent(ui::LocatedEvent* event); | |
| 199 | |
| 200 // Overwrites |event| with the keycodes and flags. | |
| 201 void OverwriteEvent(ui::KeyEvent* event, | |
| 202 unsigned int new_native_keycode, | |
| 203 unsigned int new_native_state, | |
| 204 ui::KeyboardCode new_keycode, | |
| 205 int new_flags); | |
| 206 | |
| 207 // Checks the type of the |device_name|, and inserts a new entry to | |
| 208 // |device_id_to_type_|. | |
| 209 DeviceType DeviceAddedInternal(int device_id, const std::string& device_name); | |
| 210 | |
| 211 // Returns true if |last_device_id_| is Apple's. | |
| 212 bool IsAppleKeyboard() const; | |
| 213 | |
| 214 // Remaps |original_flags| to |remapped_flags| and |original_native_modifiers| | |
| 215 // to |remapped_native_modifiers| following the current user prefs. | |
| 216 void GetRemappedModifierMasks(int original_flags, | |
| 217 unsigned int original_native_modifiers, | |
| 218 int* remapped_flags, | |
| 219 unsigned int* remapped_native_modifiers) const; | |
| 220 | |
| 221 std::map<int, DeviceType> device_id_to_type_; | |
| 222 int last_device_id_; | |
| 223 | |
| 224 #if defined(OS_CHROMEOS) | |
| 225 // A mapping from X11 KeySym keys to KeyCode values. | |
| 226 base::hash_map<unsigned long, unsigned long> keysym_to_keycode_map_; | |
| 227 | |
| 228 // A set of device IDs whose press event has been rewritten. | |
| 229 std::set<int> pressed_device_ids_; | |
| 230 | |
| 231 chromeos::input_method::XKeyboard* xkeyboard_for_testing_; | |
| 232 | |
| 233 scoped_ptr<chromeos::KeyboardDrivenEventRewriter> | |
| 234 keyboard_driven_event_rewriter_; | |
| 235 #endif | |
| 236 | |
| 237 const PrefService* pref_service_for_testing_; | |
| 238 | |
| 239 DISALLOW_COPY_AND_ASSIGN(EventRewriter); | |
| 240 }; | |
| 241 | |
| 242 #endif // CHROME_BROWSER_UI_ASH_EVENT_REWRITER_H_ | |
| OLD | NEW |