Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 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_CHROMEOS_EVENTS_EVENT_REWRITER_H_ | 5 #ifndef UI_CHROMEOS_EVENTS_EVENT_REWRITER_H_ |
| 6 #define CHROME_BROWSER_CHROMEOS_EVENTS_EVENT_REWRITER_H_ | 6 #define UI_CHROMEOS_EVENTS_EVENT_REWRITER_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <set> | 10 #include <set> |
| 11 #include <string> | 11 #include <string> |
| 12 | 12 |
| 13 #include "base/compiler_specific.h" | 13 #include "base/compiler_specific.h" |
| 14 #include "base/containers/hash_tables.h" | 14 #include "base/containers/hash_tables.h" |
| 15 #include "base/macros.h" | 15 #include "base/macros.h" |
| 16 #include "ui/chromeos/ui_chromeos_export.h" | |
| 16 #include "ui/events/event.h" | 17 #include "ui/events/event.h" |
| 17 #include "ui/events/event_rewriter.h" | 18 #include "ui/events/event_rewriter.h" |
| 18 #include "ui/events/keycodes/dom/dom_key.h" | 19 #include "ui/events/keycodes/dom/dom_key.h" |
| 19 | 20 |
| 20 class PrefService; | |
| 21 | |
| 22 namespace ash { | |
| 23 class StickyKeysController; | |
| 24 } | |
| 25 | |
| 26 namespace ui { | |
| 27 enum class DomCode; | |
| 28 }; | |
| 29 | |
| 30 namespace chromeos { | 21 namespace chromeos { |
| 31 namespace input_method { | 22 namespace input_method { |
| 32 class ImeKeyboard; | 23 class ImeKeyboard; |
| 33 } | 24 } |
| 25 } | |
| 26 | |
| 27 namespace ui { | |
| 28 | |
| 29 enum class DomCode; | |
| 30 | |
| 31 namespace chromeos { | |
| 34 | 32 |
| 35 // EventRewriter makes various changes to keyboard-related events, | 33 // EventRewriter makes various changes to keyboard-related events, |
| 36 // including KeyEvents and some other events with keyboard modifier flags: | 34 // including KeyEvents and some other events with keyboard modifier flags: |
| 37 // - maps certain non-character keys according to user preferences | 35 // - maps certain non-character keys according to user preferences |
| 38 // (Control, Alt, Search, Caps Lock, Escape, Backspace, Diamond); | 36 // (Control, Alt, Search, Caps Lock, Escape, Backspace, Diamond); |
| 39 // - maps Command to Control on Apple keyboards; | 37 // - maps Command to Control on Apple keyboards; |
| 40 // - converts numeric pad editing keys to their numeric forms; | 38 // - converts numeric pad editing keys to their numeric forms; |
| 41 // - converts top-row function keys to special keys where necessary; | 39 // - converts top-row function keys to special keys where necessary; |
| 42 // - handles various key combinations like Search+Backspace -> Delete | 40 // - handles various key combinations like Search+Backspace -> Delete |
| 43 // and Search+number to Fnumber; | 41 // and Search+number to Fnumber; |
| 44 // - handles key/pointer combinations like Alt+Button1 -> Button3. | 42 // - handles key/pointer combinations like Alt+Button1 -> Button3. |
| 45 class EventRewriter : public ui::EventRewriter { | 43 class UI_CHROMEOS_EXPORT EventRewriter : public ui::EventRewriter { |
| 46 public: | 44 public: |
| 47 enum DeviceType { | 45 enum DeviceType { |
| 48 kDeviceUnknown = 0, | 46 kDeviceUnknown = 0, |
| 49 kDeviceAppleKeyboard, | 47 kDeviceAppleKeyboard, |
| 50 kDeviceHotrodRemote, | 48 kDeviceHotrodRemote, |
| 51 kDeviceVirtualCoreKeyboard, // X-server generated events. | 49 kDeviceVirtualCoreKeyboard, // X-server generated events. |
| 52 }; | 50 }; |
| 53 | 51 |
| 54 // Things that keyboard-related rewriter phases can change about an Event. | 52 // Things that keyboard-related rewriter phases can change about an Event. |
| 55 struct MutableKeyState { | 53 struct MutableKeyState { |
| 56 int flags; | 54 int flags; |
| 57 ui::DomCode code; | 55 ui::DomCode code; |
| 58 ui::DomKey::Base key; | 56 ui::DomKey::Base key; |
| 59 ui::KeyboardCode key_code; | 57 ui::KeyboardCode key_code; |
| 60 }; | 58 }; |
| 61 | 59 |
| 60 class StickyKeysController { | |
| 61 public: | |
| 62 StickyKeysController() {} | |
| 63 virtual ~StickyKeysController() {} | |
| 64 | |
| 65 // Handles keyboard event. Returns an |EventRewriteStatus|, and may | |
| 66 // modify |flags|: | |
| 67 // - Returns ui::EVENT_REWRITE_DISCARD, and leaves |flags| untouched, | |
| 68 // if the event is consumed (i.e. a sticky modifier press or release); | |
| 69 // - Returns ui::EVENT_REWRITE_REWRITTEN if the event needs to be modified | |
| 70 // according to the returned |flags| (i.e. a sticky-modified key); | |
| 71 // - Returns ui::EVENT_REWRITE_DISPATCH_ANOTHER if the event needs to be | |
| 72 // modified according to the returned |flags|, and there are delayed | |
| 73 // modifier-up events now to be retrieved using |NextDispatchEvent()| | |
| 74 // (i.e. a sticky-modified key that ends a sticky state); | |
| 75 // - Otherwise returns ui::EVENT_REWRITE_CONTINUE and leaves |flags| | |
| 76 // unchanged. | |
| 77 virtual ui::EventRewriteStatus RewriteKeyEvent(const ui::KeyEvent& event, | |
| 78 ui::KeyboardCode key_code, | |
| 79 int* flags) = 0; | |
| 80 | |
| 81 // Handles mouse event. | |
| 82 virtual ui::EventRewriteStatus RewriteMouseEvent( | |
| 83 const ui::MouseEvent& event, | |
| 84 int* flags) = 0; | |
| 85 | |
| 86 // Handles scroll event. | |
| 87 virtual ui::EventRewriteStatus RewriteScrollEvent( | |
| 88 const ui::ScrollEvent& event, | |
| 89 int* flags) = 0; | |
|
sadrul
2017/03/03 15:11:41
It looks to me like we don't actually need the Sti
Peng
2017/03/03 16:07:05
The signatures of EventRewriter is different. It i
sadrul
2017/03/06 16:54:26
Separate CL makes sense, but I think we should tha
| |
| 90 | |
| 91 // Obtains a pending modifier-up event. If the immediately previous | |
| 92 // call to |Rewrite...Event()| or |NextDispatchEvent()| returned | |
| 93 // ui::EVENT_REWRITE_DISPATCH_ANOTHER, this sets |new_event| and returns: | |
| 94 // - ui::EVENT_REWRITE_DISPATCH_ANOTHER if there is at least one more | |
| 95 // pending modifier-up event; | |
| 96 // - ui::EVENT_REWRITE_REWRITE if this is the last or only modifier-up | |
| 97 // event; Otherwise, there is no pending modifier-up event, and this | |
| 98 // function returns ui::EVENT_REWRITE_CONTINUE and sets |new_event| to NULL. | |
| 99 virtual ui::EventRewriteStatus NextDispatchEvent( | |
| 100 std::unique_ptr<ui::Event>* new_event) = 0; | |
| 101 | |
| 102 private: | |
| 103 DISALLOW_COPY_AND_ASSIGN(StickyKeysController); | |
| 104 }; | |
| 105 | |
| 106 class Delegate { | |
| 107 public: | |
| 108 Delegate() {} | |
| 109 virtual ~Delegate() {} | |
| 110 | |
| 111 virtual bool RewriteModifierKeys() = 0; | |
| 112 | |
| 113 // Returns true if get keyboard remapped preference value successfully and | |
| 114 // the value will be stored in |value|. | |
| 115 virtual bool GetKeyboardRemappedPrefValue(const std::string& pref_name, | |
| 116 int* value) const = 0; | |
| 117 | |
| 118 // Returns true if the target would prefer to receive raw | |
| 119 // function keys instead of having them rewritten into back, forward, | |
| 120 // brightness, volume, etc. or if the user has specified that they desire | |
| 121 // top-row keys to be treated as function keys globally. | |
| 122 virtual bool TopRowKeysAreFunctionKeys() const = 0; | |
| 123 | |
| 124 // Retunrs true if the key_code and flags have been resgistered for | |
| 125 // extensions. | |
| 126 virtual bool IsExtensionCommandRegistered(ui::KeyboardCode key_code, | |
| 127 int flags) const = 0; | |
| 128 | |
| 129 private: | |
| 130 DISALLOW_COPY_AND_ASSIGN(Delegate); | |
| 131 }; | |
| 132 | |
| 62 // Does not take ownership of the |sticky_keys_controller|, which may also | 133 // Does not take ownership of the |sticky_keys_controller|, which may also |
| 63 // be NULL (for testing without ash), in which case sticky key operations | 134 // be NULL (for testing without ash), in which case sticky key operations |
| 64 // don't happen. | 135 // don't happen. |
| 65 explicit EventRewriter(ash::StickyKeysController* sticky_keys_controller); | 136 EventRewriter(Delegate* delegate, |
| 137 StickyKeysController* sticky_keys_controller); | |
| 66 ~EventRewriter() override; | 138 ~EventRewriter() override; |
| 67 | 139 |
| 68 // Calls KeyboardDeviceAddedInternal. | 140 // Calls KeyboardDeviceAddedInternal. |
| 69 DeviceType KeyboardDeviceAddedForTesting(int device_id, | 141 DeviceType KeyboardDeviceAddedForTesting(int device_id, |
| 70 const std::string& device_name); | 142 const std::string& device_name); |
| 71 | 143 |
| 72 // Calls RewriteMouseEvent(). | 144 // Calls RewriteMouseEvent(). |
| 73 void RewriteMouseButtonEventForTesting( | 145 void RewriteMouseButtonEventForTesting( |
| 74 const ui::MouseEvent& event, | 146 const ui::MouseEvent& event, |
| 75 std::unique_ptr<ui::Event>* rewritten_event); | 147 std::unique_ptr<ui::Event>* rewritten_event); |
| 76 | 148 |
| 77 const std::map<int, DeviceType>& device_id_to_type_for_testing() const { | 149 const std::map<int, DeviceType>& device_id_to_type_for_testing() const { |
| 78 return device_id_to_type_; | 150 return device_id_to_type_; |
| 79 } | 151 } |
| 80 void set_last_keyboard_device_id_for_testing(int device_id) { | 152 void set_last_keyboard_device_id_for_testing(int device_id) { |
| 81 last_keyboard_device_id_ = device_id; | 153 last_keyboard_device_id_ = device_id; |
| 82 } | 154 } |
| 83 void set_pref_service_for_testing(const PrefService* pref_service) { | |
| 84 pref_service_for_testing_ = pref_service; | |
| 85 } | |
| 86 void set_ime_keyboard_for_testing( | 155 void set_ime_keyboard_for_testing( |
| 87 chromeos::input_method::ImeKeyboard* ime_keyboard) { | 156 ::chromeos::input_method::ImeKeyboard* ime_keyboard) { |
| 88 ime_keyboard_for_testing_ = ime_keyboard; | 157 ime_keyboard_for_testing_ = ime_keyboard; |
| 89 } | 158 } |
| 90 | 159 |
| 91 // EventRewriter overrides: | 160 // EventRewriter overrides: |
| 92 ui::EventRewriteStatus RewriteEvent( | 161 ui::EventRewriteStatus RewriteEvent( |
| 93 const ui::Event& event, | 162 const ui::Event& event, |
| 94 std::unique_ptr<ui::Event>* rewritten_event) override; | 163 std::unique_ptr<ui::Event>* rewritten_event) override; |
| 95 ui::EventRewriteStatus NextDispatchEvent( | 164 ui::EventRewriteStatus NextDispatchEvent( |
| 96 const ui::Event& last_event, | 165 const ui::Event& last_event, |
| 97 std::unique_ptr<ui::Event>* new_event) override; | 166 std::unique_ptr<ui::Event>* new_event) override; |
| 98 | 167 |
| 99 // Generate a new key event from an original key event and the replacement | 168 // Generate a new key event from an original key event and the replacement |
| 100 // state determined by a key rewriter. | 169 // state determined by a key rewriter. |
| 101 static void BuildRewrittenKeyEvent( | 170 static void BuildRewrittenKeyEvent( |
| 102 const ui::KeyEvent& key_event, | 171 const ui::KeyEvent& key_event, |
| 103 const MutableKeyState& state, | 172 const MutableKeyState& state, |
| 104 std::unique_ptr<ui::Event>* rewritten_event); | 173 std::unique_ptr<ui::Event>* rewritten_event); |
| 105 | 174 |
| 106 private: | 175 private: |
| 107 void DeviceKeyPressedOrReleased(int device_id); | 176 void DeviceKeyPressedOrReleased(int device_id); |
| 108 | 177 |
| 109 // Returns the PrefService that should be used. | |
| 110 const PrefService* GetPrefService() const; | |
| 111 | |
| 112 // Adds a device to |device_id_to_type_|. | 178 // Adds a device to |device_id_to_type_|. |
| 113 DeviceType KeyboardDeviceAdded(int device_id); | 179 DeviceType KeyboardDeviceAdded(int device_id); |
| 114 | 180 |
| 115 // Checks the type of the |device_name|, |vendor_id| and |product_id|, and | 181 // Checks the type of the |device_name|, |vendor_id| and |product_id|, and |
| 116 // inserts a new entry to |device_id_to_type_|. | 182 // inserts a new entry to |device_id_to_type_|. |
| 117 DeviceType KeyboardDeviceAddedInternal(int device_id, | 183 DeviceType KeyboardDeviceAddedInternal(int device_id, |
| 118 const std::string& device_name, | 184 const std::string& device_name, |
| 119 int vendor_id, | 185 int vendor_id, |
| 120 int product_id); | 186 int product_id); |
| 121 | 187 |
| 122 // Returns true if |last_keyboard_device_id_| is Apple's. | 188 // Returns true if |last_keyboard_device_id_| is Apple's. |
| 123 bool IsAppleKeyboard() const; | 189 bool IsAppleKeyboard() const; |
| 124 // Returns true if |last_keyboard_device_id_| is Hotrod remote. | 190 // Returns true if |last_keyboard_device_id_| is Hotrod remote. |
| 125 bool IsHotrodRemote() const; | 191 bool IsHotrodRemote() const; |
| 126 // Returns true if |last_keyboard_device_id_| is of given |device_type|. | 192 // Returns true if |last_keyboard_device_id_| is of given |device_type|. |
| 127 bool IsLastKeyboardOfType(DeviceType device_type) const; | 193 bool IsLastKeyboardOfType(DeviceType device_type) const; |
| 128 | 194 |
| 129 // Returns true if the target for |event| would prefer to receive raw function | |
| 130 // keys instead of having them rewritten into back, forward, brightness, | |
| 131 // volume, etc. or if the user has specified that they desire top-row keys to | |
| 132 // be treated as function keys globally. | |
| 133 bool TopRowKeysAreFunctionKeys(const ui::KeyEvent& event) const; | |
| 134 | |
| 135 // Given modifier flags |original_flags|, returns the remapped modifiers | 195 // Given modifier flags |original_flags|, returns the remapped modifiers |
| 136 // according to user preferences and/or event properties. | 196 // according to user preferences and/or event properties. |
| 137 int GetRemappedModifierMasks(const PrefService& pref_service, | 197 int GetRemappedModifierMasks(const ui::Event& event, |
| 138 const ui::Event& event, | |
| 139 int original_flags) const; | 198 int original_flags) const; |
| 140 | 199 |
| 141 // Rewrite a particular kind of event. | 200 // Rewrite a particular kind of event. |
| 142 ui::EventRewriteStatus RewriteKeyEvent( | 201 ui::EventRewriteStatus RewriteKeyEvent( |
| 143 const ui::KeyEvent& key_event, | 202 const ui::KeyEvent& key_event, |
| 144 std::unique_ptr<ui::Event>* rewritten_event); | 203 std::unique_ptr<ui::Event>* rewritten_event); |
| 145 ui::EventRewriteStatus RewriteMouseButtonEvent( | 204 ui::EventRewriteStatus RewriteMouseButtonEvent( |
| 146 const ui::MouseEvent& mouse_event, | 205 const ui::MouseEvent& mouse_event, |
| 147 std::unique_ptr<ui::Event>* rewritten_event); | 206 std::unique_ptr<ui::Event>* rewritten_event); |
| 148 ui::EventRewriteStatus RewriteMouseWheelEvent( | 207 ui::EventRewriteStatus RewriteMouseWheelEvent( |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 167 // A set of device IDs whose press event has been rewritten. | 226 // A set of device IDs whose press event has been rewritten. |
| 168 // This is to ensure that press and release events are rewritten consistently. | 227 // This is to ensure that press and release events are rewritten consistently. |
| 169 std::set<int> pressed_device_ids_; | 228 std::set<int> pressed_device_ids_; |
| 170 | 229 |
| 171 std::map<int, DeviceType> device_id_to_type_; | 230 std::map<int, DeviceType> device_id_to_type_; |
| 172 | 231 |
| 173 // The |source_device_id()| of the most recent keyboard event, | 232 // The |source_device_id()| of the most recent keyboard event, |
| 174 // used to interpret modifiers on pointer events. | 233 // used to interpret modifiers on pointer events. |
| 175 int last_keyboard_device_id_; | 234 int last_keyboard_device_id_; |
| 176 | 235 |
| 177 chromeos::input_method::ImeKeyboard* ime_keyboard_for_testing_; | 236 ::chromeos::input_method::ImeKeyboard* ime_keyboard_for_testing_; |
| 178 const PrefService* pref_service_for_testing_; | 237 |
| 238 Delegate* const delegate_; | |
| 179 | 239 |
| 180 // The sticky keys controller is not owned here; | 240 // The sticky keys controller is not owned here; |
| 181 // at time of writing it is a singleton in ash::Shell. | 241 // at time of writing it is a singleton in ash::Shell. |
| 182 ash::StickyKeysController* sticky_keys_controller_; | 242 StickyKeysController* sticky_keys_controller_; |
| 183 | 243 |
| 184 // Some keyboard layouts have 'latching' keys, which either apply | 244 // Some keyboard layouts have 'latching' keys, which either apply |
| 185 // a modifier while held down (like normal modifiers), or, if no | 245 // a modifier while held down (like normal modifiers), or, if no |
| 186 // non-modifier is pressed while the latching key is down, apply the | 246 // non-modifier is pressed while the latching key is down, apply the |
| 187 // modifier to the next non-modifier keypress. Under Ozone the stateless | 247 // modifier to the next non-modifier keypress. Under Ozone the stateless |
| 188 // layout model requires this to be handled explicitly. See crbug.com/518237 | 248 // layout model requires this to be handled explicitly. See crbug.com/518237 |
| 189 // Pragmatically this, like the Diamond key, is handled here in | 249 // Pragmatically this, like the Diamond key, is handled here in |
| 190 // EventRewriter, but modifier state management is scattered between | 250 // EventRewriter, but modifier state management is scattered between |
| 191 // here, sticky keys, and the system layer (X11 or Ozone), and could | 251 // here, sticky keys, and the system layer (X11 or Ozone), and could |
| 192 // do with refactoring. | 252 // do with refactoring. |
| 193 // - |pressed_modifier_latches_| records the latching keys currently pressed. | 253 // - |pressed_modifier_latches_| records the latching keys currently pressed. |
| 194 // It also records the active modifier flags for non-modifier keys that are | 254 // It also records the active modifier flags for non-modifier keys that are |
| 195 // remapped to modifiers, e.g. Diamond/F15. | 255 // remapped to modifiers, e.g. Diamond/F15. |
| 196 // - |latched_modifier_latches_| records the latching keys just released, | 256 // - |latched_modifier_latches_| records the latching keys just released, |
| 197 // to be applied to the next non-modifier key. | 257 // to be applied to the next non-modifier key. |
| 198 // - |used_modifier_latches_| records the latching keys applied to a non- | 258 // - |used_modifier_latches_| records the latching keys applied to a non- |
| 199 // modifier while pressed, so that they do not get applied after release. | 259 // modifier while pressed, so that they do not get applied after release. |
| 200 int pressed_modifier_latches_; | 260 int pressed_modifier_latches_; |
| 201 int latched_modifier_latches_; | 261 int latched_modifier_latches_; |
| 202 int used_modifier_latches_; | 262 int used_modifier_latches_; |
| 203 | 263 |
| 204 DISALLOW_COPY_AND_ASSIGN(EventRewriter); | 264 DISALLOW_COPY_AND_ASSIGN(EventRewriter); |
| 205 }; | 265 }; |
| 206 | 266 |
| 207 } // namespace chromeos | 267 } // namespace chromeos |
| 268 } // namespace ui | |
| 208 | 269 |
| 209 #endif // CHROME_BROWSER_CHROMEOS_EVENTS_EVENT_REWRITER_H_ | 270 #endif // UI_CHROMEOS_EVENTS_EVENT_REWRITER_H_ |
| OLD | NEW |