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 3 // found in the LICENSE file. 4 5 #ifndef CHROME_BROWSER_CHROMEOS_EVENTS_EVENT_REWRITER_H_ 6 #define CHROME_BROWSER_CHROMEOS_EVENTS_EVENT_REWRITER_H_ 7 8 #include <map> 9 #include <set> 10 #include <string> 11 12 #include "base/compiler_specific.h" 13 #include "base/containers/hash_tables.h" 14 #include "base/memory/scoped_ptr.h" 15 #include "ui/events/event.h" 16 #include "ui/events/event_rewriter.h" 17 18 class PrefService; 19 20 namespace ash { 21 class StickyKeysController; 22 } 23 24 namespace chromeos { 25 namespace input_method { 26 class ImeKeyboard; 27 } 28 29 // EventRewriter makes various changes to keyboard-related events, 30 // including KeyEvents and some other events with keyboard modifier flags: 31 // - maps modifiers keys (Control, Alt, Search, Caps, Diamond) according 32 // to user preferences; 33 // - maps Command to Control on Apple keyboards; 34 // - converts numeric pad editing keys to their numeric forms; 35 // - converts top-row function keys to special keys where necessary; 36 // - handles various key combinations like Search+Backspace -> Delete 37 // and Search+number to Fnumber; 38 // - handles key/pointer combinations like Alt+Button1 -> Button3. 39 class EventRewriter : public ui::EventRewriter { 40 public: 41 enum DeviceType { 42 kDeviceUnknown = 0, 43 kDeviceAppleKeyboard, 44 }; 45 46 // Does not take ownership of the |sticky_keys_controller|, which may also 47 // be NULL (for testing without ash), in which case sticky key operations 48 // don't happen. 49 explicit EventRewriter(ash::StickyKeysController* sticky_keys_controller); 50 virtual ~EventRewriter(); 51 52 // Calls KeyboardDeviceAddedInternal. 53 DeviceType KeyboardDeviceAddedForTesting(int device_id, 54 const std::string& device_name); 55 56 // Calls RewriteMouseEvent(). 57 void RewriteMouseButtonEventForTesting( 58 const ui::MouseEvent& event, 59 scoped_ptr<ui::Event>* rewritten_event); 60 device_id_to_type_for_testing()61 const std::map<int, DeviceType>& device_id_to_type_for_testing() const { 62 return device_id_to_type_; 63 } set_last_keyboard_device_id_for_testing(int device_id)64 void set_last_keyboard_device_id_for_testing(int device_id) { 65 last_keyboard_device_id_ = device_id; 66 } set_pref_service_for_testing(const PrefService * pref_service)67 void set_pref_service_for_testing(const PrefService* pref_service) { 68 pref_service_for_testing_ = pref_service; 69 } set_ime_keyboard_for_testing(chromeos::input_method::ImeKeyboard * ime_keyboard)70 void set_ime_keyboard_for_testing( 71 chromeos::input_method::ImeKeyboard* ime_keyboard) { 72 ime_keyboard_for_testing_ = ime_keyboard; 73 } 74 75 // EventRewriter overrides: 76 virtual ui::EventRewriteStatus RewriteEvent( 77 const ui::Event& event, 78 scoped_ptr<ui::Event>* rewritten_event) OVERRIDE; 79 virtual ui::EventRewriteStatus NextDispatchEvent( 80 const ui::Event& last_event, 81 scoped_ptr<ui::Event>* new_event) OVERRIDE; 82 83 // Generate a new key event from an original key event and the replacement 84 // key code and flags determined by a key rewriter. 85 static void BuildRewrittenKeyEvent(const ui::KeyEvent& key_event, 86 ui::KeyboardCode key_code, 87 int flags, 88 scoped_ptr<ui::Event>* rewritten_event); 89 90 private: 91 // Things that keyboard-related rewriter phases can change about an Event. 92 struct MutableKeyState { 93 int flags; 94 ui::KeyboardCode key_code; 95 }; 96 97 // Tables of direct remappings for |RewriteWithKeyboardRemappingsByKeyCode()|. 98 struct KeyboardRemapping { 99 ui::KeyboardCode input_key_code; 100 int input_flags; 101 ui::KeyboardCode output_key_code; 102 int output_flags; 103 }; 104 105 void DeviceKeyPressedOrReleased(int device_id); 106 107 // Returns the PrefService that should be used. 108 const PrefService* GetPrefService() const; 109 110 // Adds a device to |device_id_to_type_|. 111 void KeyboardDeviceAdded(int device_id); 112 113 // Checks the type of the |device_name|, and inserts a new entry to 114 // |device_id_to_type_|. 115 DeviceType KeyboardDeviceAddedInternal(int device_id, 116 const std::string& device_name); 117 118 // Returns true if |last_keyboard_device_id_| is Apple's. 119 bool IsAppleKeyboard() const; 120 121 // Returns true if the target for |event| would prefer to receive raw function 122 // keys instead of having them rewritten into back, forward, brightness, 123 // volume, etc. or if the user has specified that they desire top-row keys to 124 // be treated as function keys globally. 125 bool TopRowKeysAreFunctionKeys(const ui::KeyEvent& event) const; 126 127 // Given modifier flags |original_flags|, returns the remapped modifiers 128 // according to user preferences and/or event properties. 129 int GetRemappedModifierMasks(const PrefService& pref_service, 130 const ui::Event& event, 131 int original_flags) const; 132 133 // Given a set of KeyboardRemapping structs, it finds a matching struct 134 // if possible, and updates the remapped event values. Returns true if a 135 // remapping was found and remapped values were updated. 136 bool RewriteWithKeyboardRemappingsByKeyCode( 137 const KeyboardRemapping* remappings, 138 size_t num_remappings, 139 const MutableKeyState& input, 140 MutableKeyState* remapped_state); 141 142 // Rewrite a particular kind of event. 143 ui::EventRewriteStatus RewriteKeyEvent( 144 const ui::KeyEvent& key_event, 145 scoped_ptr<ui::Event>* rewritten_event); 146 ui::EventRewriteStatus RewriteMouseButtonEvent( 147 const ui::MouseEvent& mouse_event, 148 scoped_ptr<ui::Event>* rewritten_event); 149 ui::EventRewriteStatus RewriteMouseWheelEvent( 150 const ui::MouseWheelEvent& mouse_event, 151 scoped_ptr<ui::Event>* rewritten_event); 152 ui::EventRewriteStatus RewriteTouchEvent( 153 const ui::TouchEvent& touch_event, 154 scoped_ptr<ui::Event>* rewritten_event); 155 ui::EventRewriteStatus RewriteScrollEvent( 156 const ui::ScrollEvent& scroll_event, 157 scoped_ptr<ui::Event>* rewritten_event); 158 159 // Rewriter phases. These can inspect the original |event|, but operate using 160 // the current |state|, which may have been modified by previous phases. 161 void RewriteModifierKeys(const ui::KeyEvent& event, MutableKeyState* state); 162 void RewriteNumPadKeys(const ui::KeyEvent& event, MutableKeyState* state); 163 void RewriteExtendedKeys(const ui::KeyEvent& event, MutableKeyState* state); 164 void RewriteFunctionKeys(const ui::KeyEvent& event, MutableKeyState* state); 165 void RewriteLocatedEvent(const ui::Event& event, int* flags); 166 int RewriteModifierClick(const ui::MouseEvent& event, int* flags); 167 168 // A set of device IDs whose press event has been rewritten. 169 // This is to ensure that press and release events are rewritten consistently. 170 std::set<int> pressed_device_ids_; 171 172 std::map<int, DeviceType> device_id_to_type_; 173 174 // The |source_device_id()| of the most recent keyboard event, 175 // used to interpret modifiers on pointer events. 176 int last_keyboard_device_id_; 177 178 chromeos::input_method::ImeKeyboard* ime_keyboard_for_testing_; 179 const PrefService* pref_service_for_testing_; 180 181 // The sticky keys controller is not owned here; 182 // at time of writing it is a singleton in ash::Shell. 183 ash::StickyKeysController* sticky_keys_controller_; 184 185 // The ChromeOS Diamond key arrives as F15. Since F15 is not a modifier, 186 // we need to track its pressed state explicitly, and apply the selected 187 // modifier flag to key and mouse presses that arrive while F15 is down. 188 // While the Diamond key is down, this holds the corresponding modifier 189 // ui::EventFlags; otherwise it is EF_NONE. 190 int current_diamond_key_modifier_flags_; 191 192 DISALLOW_COPY_AND_ASSIGN(EventRewriter); 193 }; 194 195 } // namespace chromeos 196 197 #endif // CHROME_BROWSER_CHROMEOS_EVENTS_EVENT_REWRITER_H_ 198