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