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