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

Side by Side Diff: chrome/browser/ui/ash/event_rewriter.h

Issue 11417144: Use rewriting to make ChromeOS keyboard F<number> keys produce extended keycodes. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Helper method do keysym mapping Created 8 years 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
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_ASH_EVENT_REWRITER_H_ 5 #ifndef CHROME_BROWSER_UI_ASH_EVENT_REWRITER_H_
6 #define CHROME_BROWSER_UI_ASH_EVENT_REWRITER_H_ 6 #define CHROME_BROWSER_UI_ASH_EVENT_REWRITER_H_
7 7
8 #include <map> 8 #include <map>
9 #include <string> 9 #include <string>
10 10
11 #include "ash/event_rewriter_delegate.h" 11 #include "ash/event_rewriter_delegate.h"
12 #include "base/basictypes.h" 12 #include "base/basictypes.h"
13 #include "base/compiler_specific.h" 13 #include "base/compiler_specific.h"
14 #include "base/hash_tables.h"
14 #include "ui/aura/root_window_observer.h" 15 #include "ui/aura/root_window_observer.h"
15 #include "ui/base/keycodes/keyboard_codes.h" 16 #include "ui/base/keycodes/keyboard_codes.h"
16 17
17 #if defined(OS_CHROMEOS) 18 #if defined(OS_CHROMEOS)
18 #include "chrome/browser/chromeos/device_hierarchy_observer.h" 19 #include "chrome/browser/chromeos/device_hierarchy_observer.h"
19 #endif 20 #endif
20 21
21 class PrefService; 22 class PrefService;
22 23
23 namespace aura { 24 namespace aura {
(...skipping 11 matching lines...) Expand all
35 class EventRewriter : public ash::EventRewriterDelegate, 36 class EventRewriter : public ash::EventRewriterDelegate,
36 public aura::RootWindowObserver 37 public aura::RootWindowObserver
37 #if defined(OS_CHROMEOS) 38 #if defined(OS_CHROMEOS)
38 , public chromeos::DeviceHierarchyObserver 39 , public chromeos::DeviceHierarchyObserver
39 #endif 40 #endif
40 { 41 {
41 public: 42 public:
42 enum DeviceType { 43 enum DeviceType {
43 kDeviceUnknown = 0, 44 kDeviceUnknown = 0,
44 kDeviceAppleKeyboard, 45 kDeviceAppleKeyboard,
46 kDeviceChromeOSKeyboard,
45 }; 47 };
46 48
47 EventRewriter(); 49 EventRewriter();
48 virtual ~EventRewriter(); 50 virtual ~EventRewriter();
49 51
50 // Calls DeviceAddedInternal. 52 // Calls DeviceAddedInternal.
51 DeviceType DeviceAddedForTesting(int device_id, 53 DeviceType DeviceAddedForTesting(int device_id,
52 const std::string& device_name); 54 const std::string& device_name);
53 // Calls Rewrite. 55 // Calls Rewrite.
54 void RewriteForTesting(ui::KeyEvent* event); 56 void RewriteForTesting(ui::KeyEvent* event);
55 57
56 const std::map<int, DeviceType>& device_id_to_type_for_testing() const { 58 const std::map<int, DeviceType>& device_id_to_type_for_testing() const {
57 return device_id_to_type_; 59 return device_id_to_type_;
58 } 60 }
59 void set_last_device_id_for_testing(int device_id) { 61 void set_last_device_id_for_testing(int device_id) {
60 last_device_id_ = device_id; 62 last_device_id_ = device_id;
61 } 63 }
62 void set_pref_service_for_testing(const PrefService* pref_service) { 64 void set_pref_service_for_testing(const PrefService* pref_service) {
63 pref_service_ = pref_service; 65 pref_service_ = pref_service;
64 } 66 }
65 #if defined(OS_CHROMEOS) 67 #if defined(OS_CHROMEOS)
66 void set_xkeyboard_for_testing(chromeos::input_method::XKeyboard* xkeyboard) { 68 void set_xkeyboard_for_testing(chromeos::input_method::XKeyboard* xkeyboard) {
67 xkeyboard_ = xkeyboard; 69 xkeyboard_ = xkeyboard;
68 } 70 }
71 void set_force_chromeos_keyboard_for_testing(bool chromeos_keyboard) {
72 force_chromeos_keyboard_for_testing_ = chromeos_keyboard;
73 }
69 #endif 74 #endif
70 75
71 // Gets DeviceType from the |device_name|. 76 // Gets DeviceType from the |device_name|.
72 static DeviceType GetDeviceType(const std::string& device_name); 77 static DeviceType GetDeviceType(const std::string& device_name);
73 78
74 private: 79 private:
75 // ash::EventRewriterDelegate overrides: 80 // ash::EventRewriterDelegate overrides:
76 virtual ash::EventRewriterDelegate::Action RewriteOrFilterKeyEvent( 81 virtual ash::EventRewriterDelegate::Action RewriteOrFilterKeyEvent(
77 ui::KeyEvent* event) OVERRIDE; 82 ui::KeyEvent* event) OVERRIDE;
78 virtual ash::EventRewriterDelegate::Action RewriteOrFilterLocatedEvent( 83 virtual ash::EventRewriterDelegate::Action RewriteOrFilterLocatedEvent(
79 ui::LocatedEvent* event) OVERRIDE; 84 ui::LocatedEvent* event) OVERRIDE;
80 85
81 // aura::RootWindowObserver overrides: 86 // aura::RootWindowObserver overrides:
82 virtual void OnKeyboardMappingChanged(const aura::RootWindow* root) OVERRIDE; 87 virtual void OnKeyboardMappingChanged(const aura::RootWindow* root) OVERRIDE;
83 88
84 #if defined(OS_CHROMEOS) 89 #if defined(OS_CHROMEOS)
85 // chromeos::DeviceHierarchyObserver overrides: 90 // chromeos::DeviceHierarchyObserver overrides:
86 virtual void DeviceHierarchyChanged() OVERRIDE {} 91 virtual void DeviceHierarchyChanged() OVERRIDE {}
87 virtual void DeviceAdded(int device_id) OVERRIDE; 92 virtual void DeviceAdded(int device_id) OVERRIDE;
88 virtual void DeviceRemoved(int device_id) OVERRIDE; 93 virtual void DeviceRemoved(int device_id) OVERRIDE;
89 virtual void DeviceKeyPressedOrReleased(int device_id) OVERRIDE; 94 virtual void DeviceKeyPressedOrReleased(int device_id) OVERRIDE;
90 95
96 bool EventSourceIsChromeOSKeyboard() const;
97
91 // Updates |*_xkeycode_| in response to a keyboard map change. 98 // Updates |*_xkeycode_| in response to a keyboard map change.
92 void RefreshKeycodes(); 99 void RefreshKeycodes();
93 // Converts an X key symbol like XK_Control_L to a key code. 100 // Converts an X key symbol like XK_Control_L to a key code.
94 unsigned char NativeKeySymToNativeKeycode(unsigned long keysym); 101 unsigned char NativeKeySymToNativeKeycode(unsigned long keysym);
102
103 // We don't want to include Xlib.h here since it has polluting macros, so
104 // define these locally.
105 typedef unsigned long KeySym;
Daniel Erat 2012/11/29 04:54:23 this approach seems fine to me, although i'd proba
danakj 2012/11/30 03:07:40 Ah, okay. I was worried about "already defined" co
danakj 2012/11/30 03:49:13 Actually, it did cause compiler errors, so I moved
106 typedef unsigned char KeyCode;
107
108 struct KeyboardRemapping {
109 KeySym input_keysym;
110 unsigned int input_mods;
111 unsigned int input_native_mods;
112 KeySym output_keysym;
113 ui::KeyboardCode output_keycode;
114 };
115
116 // Given a set of KeyboardRemapping structs, it finds a matching struct
117 // if possible, and returns the remapped event values.
Daniel Erat 2012/11/29 04:54:23 nit: s/returns/updates/, also add "Returns true if
danakj 2012/11/30 03:07:40 Done.
118 bool RewriteWithKeyboardRemappingsByKeySym(
119 const KeyboardRemapping* remappings,
120 size_t num_remappings,
121 KeySym keysym,
122 unsigned int native_mods,
123 unsigned int mods,
124 KeySym* remapped_native_keysym,
125 unsigned int* remapped_native_mods,
126 ui::KeyboardCode* remapped_keycode,
127 unsigned int* remapped_mods);
128
129 // Given a set of KeyboardRemapping structs, it finds a matching struct
130 // if possible, and returns the remapped event values. This function converts
131 // the KeySym in the KeyboardRemapping struct into the KeyCode before matching
132 // to allow any KeyCode on the same physical key as the given KeySym to match.
133 bool RewriteWithKeyboardRemappingsByKeyCode(
134 const KeyboardRemapping* remappings,
135 size_t num_remappings,
136 KeyCode keycode,
137 unsigned int native_mods,
138 unsigned int mods,
139 KeySym* remapped_native_keysym,
140 unsigned int* remapped_native_mods,
141 ui::KeyboardCode* remapped_keycode,
142 unsigned int* remapped_mods);
95 #endif 143 #endif
96 144
97 // Rewrites the |event| by applying all RewriteXXX functions as needed. 145 // Rewrites the |event| by applying all RewriteXXX functions as needed.
98 void Rewrite(ui::KeyEvent* event); 146 void Rewrite(ui::KeyEvent* event);
99 147
100 // Rewrites a modifier key press/release following the current user 148 // Rewrites a modifier key press/release following the current user
101 // preferences. 149 // preferences.
102 bool RewriteModifiers(ui::KeyEvent* event); 150 bool RewriteModifiers(ui::KeyEvent* event);
103 151
104 // Rewrites Fn key press/release to Control. In some cases, Fn key is not 152 // Rewrites Fn key press/release to Control. In some cases, Fn key is not
(...skipping 13 matching lines...) Expand all
118 // * Ctrl+Alt+Down -> End 166 // * Ctrl+Alt+Down -> End
119 // When the Search key acts as a function key, it instead maps: 167 // When the Search key acts as a function key, it instead maps:
120 // * Search+Backspace -> Delete 168 // * Search+Backspace -> Delete
121 // * Search+Up -> Prior (aka PageUp) 169 // * Search+Up -> Prior (aka PageUp)
122 // * Search+Down -> Next (aka PageDown) 170 // * Search+Down -> Next (aka PageDown)
123 // * Search+Left -> Home 171 // * Search+Left -> Home
124 // * Search+right -> End 172 // * Search+right -> End
125 // Returns true when the |event| is rewritten. 173 // Returns true when the |event| is rewritten.
126 bool RewriteBackspaceAndArrowKeys(ui::KeyEvent* event); 174 bool RewriteBackspaceAndArrowKeys(ui::KeyEvent* event);
127 175
176 // When the Search key acts as a function key, it remaps Search+1
177 // through Search+= to F1 through F12. Returns true when the |event| is
178 // rewritten.
179 bool RewriteFunctionKeys(ui::KeyEvent* event);
180
128 // Rewrites the located |event|. 181 // Rewrites the located |event|.
129 void RewriteLocatedEvent(ui::LocatedEvent* event); 182 void RewriteLocatedEvent(ui::LocatedEvent* event);
130 183
131 // Overwrites |event| with the keycodes and flags. 184 // Overwrites |event| with the keycodes and flags.
132 void OverwriteEvent(ui::KeyEvent* event, 185 void OverwriteEvent(ui::KeyEvent* event,
133 unsigned int new_native_keycode, 186 unsigned int new_native_keycode,
134 unsigned int new_native_state, 187 unsigned int new_native_state,
135 ui::KeyboardCode new_keycode, 188 ui::KeyboardCode new_keycode,
136 int new_flags); 189 int new_flags);
137 190
138 // Checks the type of the |device_name|, and inserts a new entry to 191 // Checks the type of the |device_name|, and inserts a new entry to
139 // |device_id_to_type_|. 192 // |device_id_to_type_|.
140 DeviceType DeviceAddedInternal(int device_id, const std::string& device_name); 193 DeviceType DeviceAddedInternal(int device_id, const std::string& device_name);
141 194
142 // Returns true if |last_device_id_| is Apple's. 195 // Returns true if |last_device_id_| is Apple's.
143 bool IsAppleKeyboard() const; 196 bool IsAppleKeyboard() const;
144 197
145 // Remaps |original_flags| to |remapped_flags| and |original_native_modifiers| 198 // Remaps |original_flags| to |remapped_flags| and |original_native_modifiers|
146 // to |remapped_native_modifiers| following the current user prefs. 199 // to |remapped_native_modifiers| following the current user prefs.
147 void GetRemappedModifierMasks(int original_flags, 200 void GetRemappedModifierMasks(int original_flags,
148 unsigned int original_native_modifiers, 201 unsigned int original_native_modifiers,
149 int* remapped_flags, 202 int* remapped_flags,
150 unsigned int* remapped_native_modifiers) const; 203 unsigned int* remapped_native_modifiers) const;
151 204
152 std::map<int, DeviceType> device_id_to_type_; 205 std::map<int, DeviceType> device_id_to_type_;
153 int last_device_id_; 206 int last_device_id_;
154 207
155 #if defined(OS_CHROMEOS) 208 #if defined(OS_CHROMEOS)
156 // X keycodes corresponding to various keysyms. 209 bool force_chromeos_keyboard_for_testing_;
157 unsigned int control_l_xkeycode_; 210
158 unsigned int control_r_xkeycode_; 211 // A mapping from X11 KeySym keys to KeyCode values.
159 unsigned int alt_l_xkeycode_; 212 base::hash_map<unsigned long, unsigned long> keysym_to_keycode_map_;
160 unsigned int alt_r_xkeycode_;
161 unsigned int meta_l_xkeycode_;
162 unsigned int meta_r_xkeycode_;
163 unsigned int windows_l_xkeycode_;
164 unsigned int caps_lock_xkeycode_;
165 unsigned int void_symbol_xkeycode_;
166 unsigned int delete_xkeycode_;
167 unsigned int home_xkeycode_;
168 unsigned int end_xkeycode_;
169 unsigned int prior_xkeycode_;
170 unsigned int next_xkeycode_;
171 unsigned int kp_0_xkeycode_;
172 unsigned int kp_1_xkeycode_;
173 unsigned int kp_2_xkeycode_;
174 unsigned int kp_3_xkeycode_;
175 unsigned int kp_4_xkeycode_;
176 unsigned int kp_5_xkeycode_;
177 unsigned int kp_6_xkeycode_;
178 unsigned int kp_7_xkeycode_;
179 unsigned int kp_8_xkeycode_;
180 unsigned int kp_9_xkeycode_;
181 unsigned int kp_decimal_xkeycode_;
182 213
183 chromeos::input_method::XKeyboard* xkeyboard_; // for testing. 214 chromeos::input_method::XKeyboard* xkeyboard_; // for testing.
184 #endif 215 #endif
185 216
186 const PrefService* pref_service_; // for testing. 217 const PrefService* pref_service_; // for testing.
187 218
188 DISALLOW_COPY_AND_ASSIGN(EventRewriter); 219 DISALLOW_COPY_AND_ASSIGN(EventRewriter);
189 }; 220 };
190 221
191 #endif // CHROME_BROWSER_UI_ASH_EVENT_REWRITER_H_ 222 #endif // CHROME_BROWSER_UI_ASH_EVENT_REWRITER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698