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