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.
5 #ifndef CHROME_BROWSER_CHROMEOS_EVENTS_EVENT_REWRITER_H_
6 #define CHROME_BROWSER_CHROMEOS_EVENTS_EVENT_REWRITER_H_
12 #include "base/compiler_specific.h"
13 #include "base/containers/hash_tables.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "ui/events/event.h"
16 #include "ui/events/event_rewriter.h"
17 #include "ui/events/keycodes/dom/dom_key.h"
22 class StickyKeysController
;
30 namespace input_method
{
34 // EventRewriter makes various changes to keyboard-related events,
35 // including KeyEvents and some other events with keyboard modifier flags:
36 // - maps modifiers keys (Control, Alt, Search, Caps, Diamond) according
37 // to user preferences;
38 // - maps Command to Control on Apple keyboards;
39 // - converts numeric pad editing keys to their numeric forms;
40 // - converts top-row function keys to special keys where necessary;
41 // - handles various key combinations like Search+Backspace -> Delete
42 // and Search+number to Fnumber;
43 // - handles key/pointer combinations like Alt+Button1 -> Button3.
44 class EventRewriter
: public ui::EventRewriter
{
50 kDeviceVirtualCoreKeyboard
, // X-server generated events.
53 // Things that keyboard-related rewriter phases can change about an Event.
54 struct MutableKeyState
{
58 ui::KeyboardCode key_code
;
61 // Does not take ownership of the |sticky_keys_controller|, which may also
62 // be NULL (for testing without ash), in which case sticky key operations
64 explicit EventRewriter(ash::StickyKeysController
* sticky_keys_controller
);
65 ~EventRewriter() override
;
67 // Calls KeyboardDeviceAddedInternal.
68 DeviceType
KeyboardDeviceAddedForTesting(int device_id
,
69 const std::string
& device_name
);
71 // Calls RewriteMouseEvent().
72 void RewriteMouseButtonEventForTesting(
73 const ui::MouseEvent
& event
,
74 scoped_ptr
<ui::Event
>* rewritten_event
);
76 const std::map
<int, DeviceType
>& device_id_to_type_for_testing() const {
77 return device_id_to_type_
;
79 void set_last_keyboard_device_id_for_testing(int device_id
) {
80 last_keyboard_device_id_
= device_id
;
82 void set_pref_service_for_testing(const PrefService
* pref_service
) {
83 pref_service_for_testing_
= pref_service
;
85 void set_ime_keyboard_for_testing(
86 chromeos::input_method::ImeKeyboard
* ime_keyboard
) {
87 ime_keyboard_for_testing_
= ime_keyboard
;
90 // EventRewriter overrides:
91 ui::EventRewriteStatus
RewriteEvent(
92 const ui::Event
& event
,
93 scoped_ptr
<ui::Event
>* rewritten_event
) override
;
94 ui::EventRewriteStatus
NextDispatchEvent(
95 const ui::Event
& last_event
,
96 scoped_ptr
<ui::Event
>* new_event
) override
;
98 // Generate a new key event from an original key event and the replacement
99 // state determined by a key rewriter.
100 static void BuildRewrittenKeyEvent(const ui::KeyEvent
& key_event
,
101 const MutableKeyState
& state
,
102 scoped_ptr
<ui::Event
>* rewritten_event
);
105 void DeviceKeyPressedOrReleased(int device_id
);
107 // Returns the PrefService that should be used.
108 const PrefService
* GetPrefService() const;
110 // Adds a device to |device_id_to_type_|.
111 DeviceType
KeyboardDeviceAdded(int device_id
);
113 // Checks the type of the |device_name|, |vendor_id| and |product_id|, and
114 // inserts a new entry to |device_id_to_type_|.
115 DeviceType
KeyboardDeviceAddedInternal(int device_id
,
116 const std::string
& device_name
,
120 // Returns true if |last_keyboard_device_id_| is Apple's.
121 bool IsAppleKeyboard() const;
122 // Returns true if |last_keyboard_device_id_| is Hotrod remote.
123 bool IsHotrodRemote() const;
124 // Returns true if |last_keyboard_device_id_| is of given |device_type|.
125 bool IsLastKeyboardOfType(DeviceType device_type
) const;
127 // Returns true if the target for |event| would prefer to receive raw function
128 // keys instead of having them rewritten into back, forward, brightness,
129 // volume, etc. or if the user has specified that they desire top-row keys to
130 // be treated as function keys globally.
131 bool TopRowKeysAreFunctionKeys(const ui::KeyEvent
& event
) const;
133 // Given modifier flags |original_flags|, returns the remapped modifiers
134 // according to user preferences and/or event properties.
135 int GetRemappedModifierMasks(const PrefService
& pref_service
,
136 const ui::Event
& event
,
137 int original_flags
) const;
139 // Rewrite a particular kind of event.
140 ui::EventRewriteStatus
RewriteKeyEvent(
141 const ui::KeyEvent
& key_event
,
142 scoped_ptr
<ui::Event
>* rewritten_event
);
143 ui::EventRewriteStatus
RewriteMouseButtonEvent(
144 const ui::MouseEvent
& mouse_event
,
145 scoped_ptr
<ui::Event
>* rewritten_event
);
146 ui::EventRewriteStatus
RewriteMouseWheelEvent(
147 const ui::MouseWheelEvent
& mouse_event
,
148 scoped_ptr
<ui::Event
>* rewritten_event
);
149 ui::EventRewriteStatus
RewriteTouchEvent(
150 const ui::TouchEvent
& touch_event
,
151 scoped_ptr
<ui::Event
>* rewritten_event
);
152 ui::EventRewriteStatus
RewriteScrollEvent(
153 const ui::ScrollEvent
& scroll_event
,
154 scoped_ptr
<ui::Event
>* rewritten_event
);
156 // Rewriter phases. These can inspect the original |event|, but operate using
157 // the current |state|, which may have been modified by previous phases.
158 bool RewriteModifierKeys(const ui::KeyEvent
& event
, MutableKeyState
* state
);
159 void RewriteNumPadKeys(const ui::KeyEvent
& event
, MutableKeyState
* state
);
160 void RewriteExtendedKeys(const ui::KeyEvent
& event
, MutableKeyState
* state
);
161 void RewriteFunctionKeys(const ui::KeyEvent
& event
, MutableKeyState
* state
);
162 void RewriteLocatedEvent(const ui::Event
& event
, int* flags
);
163 int RewriteModifierClick(const ui::MouseEvent
& event
, int* flags
);
165 // A set of device IDs whose press event has been rewritten.
166 // This is to ensure that press and release events are rewritten consistently.
167 std::set
<int> pressed_device_ids_
;
169 std::map
<int, DeviceType
> device_id_to_type_
;
171 // The |source_device_id()| of the most recent keyboard event,
172 // used to interpret modifiers on pointer events.
173 int last_keyboard_device_id_
;
175 chromeos::input_method::ImeKeyboard
* ime_keyboard_for_testing_
;
176 const PrefService
* pref_service_for_testing_
;
178 // The sticky keys controller is not owned here;
179 // at time of writing it is a singleton in ash::Shell.
180 ash::StickyKeysController
* sticky_keys_controller_
;
182 // The ChromeOS Diamond key arrives as F15. Since F15 is not a modifier,
183 // we need to track its pressed state explicitly, and apply the selected
184 // modifier flag to key and mouse presses that arrive while F15 is down.
185 // While the Diamond key is down, this holds the corresponding modifier
186 // ui::EventFlags; otherwise it is EF_NONE.
187 int current_diamond_key_modifier_flags_
;
189 // Some keyboard layouts have 'latching' keys, which either apply
190 // a modifier while held down (like normal modifiers), or, if no
191 // non-modifier is pressed while the latching key is down, apply the
192 // modifier to the next non-modifier keypress. Under Ozone the stateless
193 // layout model requires this to be handled explicitly. See crbug.com/518237
194 // Pragmatically this, like the Diamond key, is handled here in
195 // EventRewriter, but modifier state management is scattered between
196 // here, sticky keys, and the system layer (X11 or Ozone), and could
197 // do with refactoring.
198 // - |pressed_modifier_latches_| records the latching keys currently pressed.
199 // - |latched_modifier_latches_| records the latching keys just released,
200 // to be applied to the next non-modifier key.
201 // - |used_modifier_latches_| records the latching keys applied to a non-
202 // modifier while pressed, so that they do not get applied after release.
203 int pressed_modifier_latches_
;
204 int latched_modifier_latches_
;
205 int used_modifier_latches_
;
207 DISALLOW_COPY_AND_ASSIGN(EventRewriter
);
210 } // namespace chromeos
212 #endif // CHROME_BROWSER_CHROMEOS_EVENTS_EVENT_REWRITER_H_