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