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