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"
21 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 base::char16 character
;
59 ui::KeyboardCode key_code
;
62 // Does not take ownership of the |sticky_keys_controller|, which may also
63 // be NULL (for testing without ash), in which case sticky key operations
65 explicit EventRewriter(ash::StickyKeysController
* sticky_keys_controller
);
66 ~EventRewriter() override
;
68 // Calls KeyboardDeviceAddedInternal.
69 DeviceType
KeyboardDeviceAddedForTesting(int device_id
,
70 const std::string
& device_name
);
72 // Calls RewriteMouseEvent().
73 void RewriteMouseButtonEventForTesting(
74 const ui::MouseEvent
& event
,
75 scoped_ptr
<ui::Event
>* rewritten_event
);
77 const std::map
<int, DeviceType
>& device_id_to_type_for_testing() const {
78 return device_id_to_type_
;
80 void set_last_keyboard_device_id_for_testing(int device_id
) {
81 last_keyboard_device_id_
= device_id
;
83 void set_pref_service_for_testing(const PrefService
* pref_service
) {
84 pref_service_for_testing_
= pref_service
;
86 void set_ime_keyboard_for_testing(
87 chromeos::input_method::ImeKeyboard
* ime_keyboard
) {
88 ime_keyboard_for_testing_
= ime_keyboard
;
91 // EventRewriter overrides:
92 ui::EventRewriteStatus
RewriteEvent(
93 const ui::Event
& event
,
94 scoped_ptr
<ui::Event
>* rewritten_event
) override
;
95 ui::EventRewriteStatus
NextDispatchEvent(
96 const ui::Event
& last_event
,
97 scoped_ptr
<ui::Event
>* new_event
) override
;
99 // Generate a new key event from an original key event and the replacement
100 // state determined by a key rewriter.
101 static void BuildRewrittenKeyEvent(const ui::KeyEvent
& key_event
,
102 const MutableKeyState
& state
,
103 scoped_ptr
<ui::Event
>* rewritten_event
);
106 void DeviceKeyPressedOrReleased(int device_id
);
108 // Returns the PrefService that should be used.
109 const PrefService
* GetPrefService() const;
111 // Adds a device to |device_id_to_type_|.
112 DeviceType
KeyboardDeviceAdded(int device_id
);
114 // Checks the type of the |device_name|, |vendor_id| and |product_id|, and
115 // inserts a new entry to |device_id_to_type_|.
116 DeviceType
KeyboardDeviceAddedInternal(int device_id
,
117 const std::string
& device_name
,
121 // Returns true if |last_keyboard_device_id_| is Apple's.
122 bool IsAppleKeyboard() const;
123 // Returns true if |last_keyboard_device_id_| is Hotrod remote.
124 bool IsHotrodRemote() const;
125 // Returns true if |last_keyboard_device_id_| is of given |device_type|.
126 bool IsLastKeyboardOfType(DeviceType device_type
) const;
128 // Returns true if the target for |event| would prefer to receive raw function
129 // keys instead of having them rewritten into back, forward, brightness,
130 // volume, etc. or if the user has specified that they desire top-row keys to
131 // be treated as function keys globally.
132 bool TopRowKeysAreFunctionKeys(const ui::KeyEvent
& event
) const;
134 // Given modifier flags |original_flags|, returns the remapped modifiers
135 // according to user preferences and/or event properties.
136 int GetRemappedModifierMasks(const PrefService
& pref_service
,
137 const ui::Event
& event
,
138 int original_flags
) const;
140 // Rewrite a particular kind of event.
141 ui::EventRewriteStatus
RewriteKeyEvent(
142 const ui::KeyEvent
& key_event
,
143 scoped_ptr
<ui::Event
>* rewritten_event
);
144 ui::EventRewriteStatus
RewriteMouseButtonEvent(
145 const ui::MouseEvent
& mouse_event
,
146 scoped_ptr
<ui::Event
>* rewritten_event
);
147 ui::EventRewriteStatus
RewriteMouseWheelEvent(
148 const ui::MouseWheelEvent
& mouse_event
,
149 scoped_ptr
<ui::Event
>* rewritten_event
);
150 ui::EventRewriteStatus
RewriteTouchEvent(
151 const ui::TouchEvent
& touch_event
,
152 scoped_ptr
<ui::Event
>* rewritten_event
);
153 ui::EventRewriteStatus
RewriteScrollEvent(
154 const ui::ScrollEvent
& scroll_event
,
155 scoped_ptr
<ui::Event
>* rewritten_event
);
157 // Rewriter phases. These can inspect the original |event|, but operate using
158 // the current |state|, which may have been modified by previous phases.
159 bool RewriteModifierKeys(const ui::KeyEvent
& event
, MutableKeyState
* state
);
160 void RewriteNumPadKeys(const ui::KeyEvent
& event
, MutableKeyState
* state
);
161 void RewriteExtendedKeys(const ui::KeyEvent
& event
, MutableKeyState
* state
);
162 void RewriteFunctionKeys(const ui::KeyEvent
& event
, MutableKeyState
* state
);
163 void RewriteLocatedEvent(const ui::Event
& event
, int* flags
);
164 int RewriteModifierClick(const ui::MouseEvent
& event
, int* flags
);
166 // A set of device IDs whose press event has been rewritten.
167 // This is to ensure that press and release events are rewritten consistently.
168 std::set
<int> pressed_device_ids_
;
170 std::map
<int, DeviceType
> device_id_to_type_
;
172 // The |source_device_id()| of the most recent keyboard event,
173 // used to interpret modifiers on pointer events.
174 int last_keyboard_device_id_
;
176 chromeos::input_method::ImeKeyboard
* ime_keyboard_for_testing_
;
177 const PrefService
* pref_service_for_testing_
;
179 // The sticky keys controller is not owned here;
180 // at time of writing it is a singleton in ash::Shell.
181 ash::StickyKeysController
* sticky_keys_controller_
;
183 // The ChromeOS Diamond key arrives as F15. Since F15 is not a modifier,
184 // we need to track its pressed state explicitly, and apply the selected
185 // modifier flag to key and mouse presses that arrive while F15 is down.
186 // While the Diamond key is down, this holds the corresponding modifier
187 // ui::EventFlags; otherwise it is EF_NONE.
188 int current_diamond_key_modifier_flags_
;
190 // Some keyboard layouts have 'latching' keys, which either apply
191 // a modifier while held down (like normal modifiers), or, if no
192 // non-modifier is pressed while the latching key is down, apply the
193 // modifier to the next non-modifier keypress. Under Ozone the stateless
194 // layout model requires this to be handled explicitly. See crbug.com/518237
195 // Pragmatically this, like the Diamond key, is handled here in
196 // EventRewriter, but modifier state management is scattered between
197 // here, sticky keys, and the system layer (X11 or Ozone), and could
198 // do with refactoring.
199 // - |pressed_modifier_latches_| records the latching keys currently pressed.
200 // - |latched_modifier_latches_| records the latching keys just released,
201 // to be applied to the next non-modifier key.
202 // - |used_modifier_latches_| records the latching keys applied to a non-
203 // modifier while pressed, so that they do not get applied after release.
204 int pressed_modifier_latches_
;
205 int latched_modifier_latches_
;
206 int used_modifier_latches_
;
208 DISALLOW_COPY_AND_ASSIGN(EventRewriter
);
211 } // namespace chromeos
213 #endif // CHROME_BROWSER_CHROMEOS_EVENTS_EVENT_REWRITER_H_