Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / events / event_rewriter.h
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 <set>
10 #include <string>
11
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
18 class PrefService;
19
20 namespace ash {
21 class StickyKeysController;
22 }
23
24 namespace chromeos {
25 namespace input_method {
26 class ImeKeyboard;
27 }
28
29 // EventRewriter makes various changes to keyboard-related events,
30 // including KeyEvents and some other events with keyboard modifier flags:
31 // - maps modifiers keys (Control, Alt, Search, Caps, Diamond) according
32 //   to user preferences;
33 // - maps Command to Control on Apple keyboards;
34 // - converts numeric pad editing keys to their numeric forms;
35 // - converts top-row function keys to special keys where necessary;
36 // - handles various key combinations like Search+Backspace -> Delete
37 //   and Search+number to Fnumber;
38 // - handles key/pointer combinations like Alt+Button1 -> Button3.
39 class EventRewriter : public ui::EventRewriter {
40  public:
41   enum DeviceType {
42     kDeviceUnknown = 0,
43     kDeviceAppleKeyboard,
44     kDeviceHotrodRemote,
45     kDeviceVirtualCoreKeyboard,  // X-server generated events.
46   };
47
48   // Does not take ownership of the |sticky_keys_controller|, which may also
49   // be NULL (for testing without ash), in which case sticky key operations
50   // don't happen.
51   explicit EventRewriter(ash::StickyKeysController* sticky_keys_controller);
52   virtual ~EventRewriter();
53
54   // Calls KeyboardDeviceAddedInternal.
55   DeviceType KeyboardDeviceAddedForTesting(int device_id,
56                                            const std::string& device_name);
57
58   // Calls RewriteMouseEvent().
59   void RewriteMouseButtonEventForTesting(
60       const ui::MouseEvent& event,
61       scoped_ptr<ui::Event>* rewritten_event);
62
63   const std::map<int, DeviceType>& device_id_to_type_for_testing() const {
64     return device_id_to_type_;
65   }
66   void set_last_keyboard_device_id_for_testing(int device_id) {
67     last_keyboard_device_id_ = device_id;
68   }
69   void set_pref_service_for_testing(const PrefService* pref_service) {
70     pref_service_for_testing_ = pref_service;
71   }
72   void set_ime_keyboard_for_testing(
73       chromeos::input_method::ImeKeyboard* ime_keyboard) {
74     ime_keyboard_for_testing_ = ime_keyboard;
75   }
76
77   // EventRewriter overrides:
78   virtual ui::EventRewriteStatus RewriteEvent(
79       const ui::Event& event,
80       scoped_ptr<ui::Event>* rewritten_event) override;
81   virtual ui::EventRewriteStatus NextDispatchEvent(
82       const ui::Event& last_event,
83       scoped_ptr<ui::Event>* new_event) override;
84
85   // Generate a new key event from an original key event and the replacement
86   // key code and flags determined by a key rewriter.
87   static void BuildRewrittenKeyEvent(const ui::KeyEvent& key_event,
88                                      ui::KeyboardCode key_code,
89                                      int flags,
90                                      scoped_ptr<ui::Event>* rewritten_event);
91
92  private:
93   // Things that keyboard-related rewriter phases can change about an Event.
94   struct MutableKeyState {
95     int flags;
96     ui::KeyboardCode key_code;
97   };
98
99   // Tables of direct remappings for |RewriteWithKeyboardRemappingsByKeyCode()|.
100   struct KeyboardRemapping {
101     ui::KeyboardCode input_key_code;
102     int input_flags;
103     ui::KeyboardCode output_key_code;
104     int output_flags;
105   };
106
107   void DeviceKeyPressedOrReleased(int device_id);
108
109   // Returns the PrefService that should be used.
110   const PrefService* GetPrefService() const;
111
112   // Adds a device to |device_id_to_type_|.
113   DeviceType KeyboardDeviceAdded(int device_id);
114
115   // Checks the type of the |device_name|, |vendor_id| and |product_id|, and
116   // inserts a new entry to |device_id_to_type_|.
117   DeviceType KeyboardDeviceAddedInternal(int device_id,
118                                          const std::string& device_name,
119                                          int vendor_id,
120                                          int product_id);
121
122   // Returns true if |last_keyboard_device_id_| is Apple's.
123   bool IsAppleKeyboard() const;
124   // Returns true if |last_keyboard_device_id_| is Hotrod remote.
125   bool IsHotrodRemote() const;
126   // Returns true if |last_keyboard_device_id_| is of given |device_type|.
127   bool IsLastKeyboardOfType(DeviceType device_type) const;
128
129   // Returns true if the target for |event| would prefer to receive raw function
130   // keys instead of having them rewritten into back, forward, brightness,
131   // volume, etc. or if the user has specified that they desire top-row keys to
132   // be treated as function keys globally.
133   bool TopRowKeysAreFunctionKeys(const ui::KeyEvent& event) const;
134
135   // Given modifier flags |original_flags|, returns the remapped modifiers
136   // according to user preferences and/or event properties.
137   int GetRemappedModifierMasks(const PrefService& pref_service,
138                                const ui::Event& event,
139                                int original_flags) const;
140
141   // Given a set of KeyboardRemapping structs, it finds a matching struct
142   // if possible, and updates the remapped event values. Returns true if a
143   // remapping was found and remapped values were updated.
144   bool RewriteWithKeyboardRemappingsByKeyCode(
145       const KeyboardRemapping* remappings,
146       size_t num_remappings,
147       const MutableKeyState& input,
148       MutableKeyState* remapped_state);
149
150   // Rewrite a particular kind of event.
151   ui::EventRewriteStatus RewriteKeyEvent(
152       const ui::KeyEvent& key_event,
153       scoped_ptr<ui::Event>* rewritten_event);
154   ui::EventRewriteStatus RewriteMouseButtonEvent(
155       const ui::MouseEvent& mouse_event,
156       scoped_ptr<ui::Event>* rewritten_event);
157   ui::EventRewriteStatus RewriteMouseWheelEvent(
158       const ui::MouseWheelEvent& mouse_event,
159       scoped_ptr<ui::Event>* rewritten_event);
160   ui::EventRewriteStatus RewriteTouchEvent(
161       const ui::TouchEvent& touch_event,
162       scoped_ptr<ui::Event>* rewritten_event);
163   ui::EventRewriteStatus RewriteScrollEvent(
164       const ui::ScrollEvent& scroll_event,
165       scoped_ptr<ui::Event>* rewritten_event);
166
167   // Rewriter phases. These can inspect the original |event|, but operate using
168   // the current |state|, which may have been modified by previous phases.
169   void RewriteModifierKeys(const ui::KeyEvent& event, MutableKeyState* state);
170   void RewriteNumPadKeys(const ui::KeyEvent& event, MutableKeyState* state);
171   void RewriteExtendedKeys(const ui::KeyEvent& event, MutableKeyState* state);
172   void RewriteFunctionKeys(const ui::KeyEvent& event, MutableKeyState* state);
173   void RewriteLocatedEvent(const ui::Event& event, int* flags);
174   int RewriteModifierClick(const ui::MouseEvent& event, int* flags);
175
176   // A set of device IDs whose press event has been rewritten.
177   // This is to ensure that press and release events are rewritten consistently.
178   std::set<int> pressed_device_ids_;
179
180   std::map<int, DeviceType> device_id_to_type_;
181
182   // The |source_device_id()| of the most recent keyboard event,
183   // used to interpret modifiers on pointer events.
184   int last_keyboard_device_id_;
185
186   chromeos::input_method::ImeKeyboard* ime_keyboard_for_testing_;
187   const PrefService* pref_service_for_testing_;
188
189   // The sticky keys controller is not owned here;
190   // at time of writing it is a singleton in ash::Shell.
191   ash::StickyKeysController* sticky_keys_controller_;
192
193   // The ChromeOS Diamond key arrives as F15. Since F15 is not a modifier,
194   // we need to track its pressed state explicitly, and apply the selected
195   // modifier flag to key and mouse presses that arrive while F15 is down.
196   // While the Diamond key is down, this holds the corresponding modifier
197   // ui::EventFlags; otherwise it is EF_NONE.
198   int current_diamond_key_modifier_flags_;
199
200   DISALLOW_COPY_AND_ASSIGN(EventRewriter);
201 };
202
203 }  // namespace chromeos
204
205 #endif  // CHROME_BROWSER_CHROMEOS_EVENTS_EVENT_REWRITER_H_