| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 ASH_STICKY_KEYS_STICKY_KEYS_CONTROLLER_H_ | 5 #ifndef ASH_STICKY_KEYS_STICKY_KEYS_CONTROLLER_H_ |
| 6 #define ASH_STICKY_KEYS_STICKY_KEYS_CONTROLLER_H_ | 6 #define ASH_STICKY_KEYS_STICKY_KEYS_CONTROLLER_H_ |
| 7 | 7 |
| 8 #include "ash/ash_export.h" | 8 #include "ash/ash_export.h" |
| 9 #include "ash/sticky_keys/sticky_keys_state.h" | 9 #include "ash/sticky_keys/sticky_keys_state.h" |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "ui/events/event_constants.h" | 11 #include "ui/events/event_constants.h" |
| 12 #include "ui/events/event_handler.h" | 12 #include "ui/events/event_handler.h" |
| 13 #include "ui/events/event_rewriter.h" |
| 14 #include "ui/events/keycodes/keyboard_codes.h" |
| 13 | 15 |
| 14 namespace ui { | 16 namespace ui { |
| 15 class Event; | 17 class Event; |
| 16 class KeyEvent; | 18 class KeyEvent; |
| 17 class MouseEvent; | 19 class MouseEvent; |
| 18 } // namespace ui | 20 } // namespace ui |
| 19 | 21 |
| 20 namespace aura { | 22 namespace aura { |
| 21 class Window; | 23 class Window; |
| 22 } // namespace aura | 24 } // namespace aura |
| (...skipping 29 matching lines...) Expand all Loading... |
| 52 // 6. Release 1 key : 1 Keyup event with Ctrl modifier. | 54 // 6. Release 1 key : 1 Keyup event with Ctrl modifier. |
| 53 // 7. Press 2 key : 2 Keydown event with Ctrl modifier. | 55 // 7. Press 2 key : 2 Keydown event with Ctrl modifier. |
| 54 // 8. Release 2 key : 2 Keyup event with Ctrl modifier. | 56 // 8. Release 2 key : 2 Keyup event with Ctrl modifier. |
| 55 // 9. Press Ctrl key : No event | 57 // 9. Press Ctrl key : No event |
| 56 // 10. Release Ctrl key: Ctrl Keyup | 58 // 10. Release Ctrl key: Ctrl Keyup |
| 57 // | 59 // |
| 58 // In the case of Chrome OS, StickyKeysController supports Shift,Alt,Ctrl | 60 // In the case of Chrome OS, StickyKeysController supports Shift,Alt,Ctrl |
| 59 // modifiers. Each handling or state is performed independently. | 61 // modifiers. Each handling or state is performed independently. |
| 60 // | 62 // |
| 61 // StickyKeysController is disabled by default. | 63 // StickyKeysController is disabled by default. |
| 62 class ASH_EXPORT StickyKeysController : public ui::EventHandler { | 64 class ASH_EXPORT StickyKeysController { |
| 63 public: | 65 public: |
| 64 StickyKeysController(); | 66 StickyKeysController(); |
| 65 virtual ~StickyKeysController(); | 67 virtual ~StickyKeysController(); |
| 66 | 68 |
| 67 // Activate sticky keys to intercept and modify incoming events. | 69 // Activate sticky keys to intercept and modify incoming events. |
| 68 void Enable(bool enabled); | 70 void Enable(bool enabled); |
| 69 | 71 |
| 70 void SetModifiersEnabled(bool mod3_enabled, bool altgr_enabled); | 72 void SetModifiersEnabled(bool mod3_enabled, bool altgr_enabled); |
| 71 | 73 |
| 72 // Overridden from ui::EventHandler: | |
| 73 virtual void OnKeyEvent(ui::KeyEvent* event) OVERRIDE; | |
| 74 virtual void OnMouseEvent(ui::MouseEvent* event) OVERRIDE; | |
| 75 virtual void OnScrollEvent(ui::ScrollEvent* event) OVERRIDE; | |
| 76 | |
| 77 // Returns the StickyKeyOverlay used by the controller. Ownership is not | 74 // Returns the StickyKeyOverlay used by the controller. Ownership is not |
| 78 // passed. | 75 // passed. |
| 79 StickyKeysOverlay* GetOverlayForTest(); | 76 StickyKeysOverlay* GetOverlayForTest(); |
| 80 | 77 |
| 78 // Handles keyboard event. Returns an |EventRewriteStatus|: |
| 79 // - ui::EVENT_REWRITE_DISCARD if the event is consumed (i.e. a sticky |
| 80 // modifier press or release); |
| 81 // - ui::EVENT_REWRITE_REWRITTEN if the event needs to be modified |
| 82 // according to the changes to |state|; |
| 83 // - ui::EVENT_REWRITE_DISPATCH_ANOTHER if there are delayed modifier-up |
| 84 // events now to be retrieved using |NextDispatchEvent()|; |
| 85 // - ui::EVENT_REWRITE_CONTINUE otherwise (no external action necessary). |
| 86 ui::EventRewriteStatus RewriteKeyEvent(const ui::KeyEvent& event, |
| 87 ui::KeyboardCode key_code, |
| 88 int* flags); |
| 89 |
| 90 // Handles mouse event. |
| 91 ui::EventRewriteStatus RewriteMouseEvent(const ui::MouseEvent& event, |
| 92 int* flags); |
| 93 |
| 94 // Handles scroll event. |
| 95 ui::EventRewriteStatus RewriteScrollEvent(const ui::ScrollEvent& event, |
| 96 int* flags); |
| 97 |
| 98 // Obtains a pending modifier-up event. Returns: |
| 99 // - ui::EVENT_REWRITE_DISPATCH_ANOTHER if there is at least one more |
| 100 // pending modifier-up event; |
| 101 // - ui::EVENT_REWRITE_REWRITE if this is the last or only modifier-up event; |
| 102 // - ui::EVENT_REWRITE_CONTINUE if there are no pending modifier-up events. |
| 103 ui::EventRewriteStatus NextDispatchEvent(scoped_ptr<ui::Event>* new_event); |
| 104 |
| 81 private: | 105 private: |
| 82 // Handles keyboard event. Returns true if Sticky key consumes keyboard event. | 106 // Handles keyboard event. Returns true if Sticky key consumes keyboard event. |
| 83 bool HandleKeyEvent(ui::KeyEvent* event); | 107 // Adds to |mod_down_flags| any flag to be added to the key event. |
| 108 // Adds to |mod_up_flags| any flag to be released after the key event. |
| 109 bool HandleKeyEvent(const ui::KeyEvent& event, |
| 110 ui::KeyboardCode key_code, |
| 111 int* mod_down_flags, |
| 112 int* mod_up_flags); |
| 84 | 113 |
| 85 // Handles mouse event. Returns true if sticky key consumes mouse event. | 114 // Handles mouse event. Returns true if Sticky key consumes keyboard event. |
| 86 bool HandleMouseEvent(ui::MouseEvent* event); | 115 // Adds to |mod_up_flags| any flag to be released after the key event. |
| 116 bool HandleMouseEvent(const ui::MouseEvent& event, |
| 117 int* mod_down_flags, |
| 118 int* mod_up_flags); |
| 87 | 119 |
| 88 // Handles scroll event. Returns true if sticky key consumes scroll event. | 120 // Handles scroll event. Returns true if Sticky key consumes keyboard event. |
| 89 bool HandleScrollEvent(ui::ScrollEvent* event); | 121 // Adds to |mod_up_flags| any flag to be released after the key event. |
| 122 bool HandleScrollEvent(const ui::ScrollEvent& event, |
| 123 int* mod_down_flags, |
| 124 int* mod_up_flags); |
| 90 | 125 |
| 91 // Updates the overlay UI with the current state of the sticky keys. | 126 // Updates the overlay UI with the current state of the sticky keys. |
| 92 void UpdateOverlay(); | 127 void UpdateOverlay(); |
| 93 | 128 |
| 94 // Whether sticky keys is activated and modifying events. | 129 // Whether sticky keys is activated and modifying events. |
| 95 bool enabled_; | 130 bool enabled_; |
| 96 | 131 |
| 97 // Whether the current layout has a mod3 key. | 132 // Whether the current layout has a mod3 key. |
| 98 bool mod3_enabled_; | 133 bool mod3_enabled_; |
| 99 | 134 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 136 // Other KeyUp/Down | noop | noop | noop | | 171 // Other KeyUp/Down | noop | noop | noop | |
| 137 // Mouse Press | noop | noop(#) | noop(#) | | 172 // Mouse Press | noop | noop(#) | noop(#) | |
| 138 // Mouse Release | noop | To DISABLED(#) | noop(#) | | 173 // Mouse Release | noop | To DISABLED(#) | noop(#) | |
| 139 // Mouse Wheel | noop | To DISABLED(#) | noop(#) | | 174 // Mouse Wheel | noop | To DISABLED(#) | noop(#) | |
| 140 // Other Mouse Event| noop | noop | noop | | 175 // Other Mouse Event| noop | noop | noop | |
| 141 // | 176 // |
| 142 // Here, (*) means key event will be consumed by StickyKeys, and (#) means event | 177 // Here, (*) means key event will be consumed by StickyKeys, and (#) means event |
| 143 // is modified. | 178 // is modified. |
| 144 class ASH_EXPORT StickyKeysHandler { | 179 class ASH_EXPORT StickyKeysHandler { |
| 145 public: | 180 public: |
| 146 class StickyKeysHandlerDelegate { | 181 explicit StickyKeysHandler(ui::EventFlags modifier_flag); |
| 147 public: | |
| 148 StickyKeysHandlerDelegate(); | |
| 149 virtual ~StickyKeysHandlerDelegate(); | |
| 150 | |
| 151 // Dispatches keyboard event synchronously. |event| is an event that has | |
| 152 // been previously dispatched. | |
| 153 virtual void DispatchKeyEvent(ui::KeyEvent* event, | |
| 154 aura::Window* target) = 0; | |
| 155 | |
| 156 // Dispatches mouse event synchronously. |event| is an event that has | |
| 157 // been previously dispatched. | |
| 158 virtual void DispatchMouseEvent(ui::MouseEvent* event, | |
| 159 aura::Window* target) = 0; | |
| 160 | |
| 161 // Dispatches scroll event synchronously. |event| is an event that has | |
| 162 // been previously dispatched. | |
| 163 virtual void DispatchScrollEvent(ui::ScrollEvent* event, | |
| 164 aura::Window* target) = 0; | |
| 165 }; | |
| 166 | |
| 167 // This class takes an ownership of |delegate|. | |
| 168 StickyKeysHandler(ui::EventFlags modifier_flag, | |
| 169 StickyKeysHandlerDelegate* delegate); | |
| 170 ~StickyKeysHandler(); | 182 ~StickyKeysHandler(); |
| 171 | 183 |
| 172 // Handles key event. Returns true if key is consumed. | 184 // Handles keyboard event. Returns true if Sticky key consumes keyboard event. |
| 173 bool HandleKeyEvent(ui::KeyEvent* event); | 185 // Sets its own modifier flag in |mod_down_flags| if it is active and needs |
| 186 // to be added to the event; sets its own modifier flag in |mod_up_flags| if |
| 187 // releasing it. |
| 188 bool HandleKeyEvent(const ui::KeyEvent& event, |
| 189 ui::KeyboardCode key_code, |
| 190 int* mod_down_flags, |
| 191 int* mod_up_flags); |
| 174 | 192 |
| 175 // Handles a mouse event. Returns true if mouse event is consumed. | 193 // Handles mouse event. Returns true if sticky key consumes mouse event. |
| 176 bool HandleMouseEvent(ui::MouseEvent* event); | 194 // Sets its own modifier flag in |mod_down_flags| if it is active and needs |
| 195 // to be added to the event; sets its own modifier flag in |mod_up_flags| if |
| 196 // releasing it. |
| 197 bool HandleMouseEvent(const ui::MouseEvent& event, |
| 198 int* mod_down_flags, |
| 199 int* mod_up_flags); |
| 177 | 200 |
| 178 // Handles a scroll event. Returns true if scroll event is consumed. | 201 // Handles scroll event. Returns true if sticky key consumes scroll event. |
| 179 bool HandleScrollEvent(ui::ScrollEvent* event); | 202 // Sets its own modifier flag in |mod_down_flags| if it is active and needs |
| 203 // to be added to the event; sets its own modifier flag in |mod_up_flags| if |
| 204 // releasing it. |
| 205 bool HandleScrollEvent(const ui::ScrollEvent& event, |
| 206 int* mod_down_flags, |
| 207 int* mod_up_flags); |
| 208 |
| 209 // Fetches a pending modifier-up event if one exists and the return |
| 210 // parameter |new_event| is available (i.e. not set). Returns the number |
| 211 // of pending events still remaining to be returned. |
| 212 int GetModifierUpEvent(scoped_ptr<ui::Event>* new_event); |
| 180 | 213 |
| 181 // Returns current internal state. | 214 // Returns current internal state. |
| 182 StickyKeyState current_state() const { return current_state_; } | 215 StickyKeyState current_state() const { return current_state_; } |
| 183 | 216 |
| 184 private: | 217 private: |
| 185 // Represents event type in Sticky Key context. | 218 // Represents event type in Sticky Key context. |
| 186 enum KeyEventType { | 219 enum KeyEventType { |
| 187 TARGET_MODIFIER_DOWN, // The monitoring modifier key is down. | 220 TARGET_MODIFIER_DOWN, // The monitoring modifier key is down. |
| 188 TARGET_MODIFIER_UP, // The monitoring modifier key is up. | 221 TARGET_MODIFIER_UP, // The monitoring modifier key is up. |
| 189 NORMAL_KEY_DOWN, // The non modifier key is down. | 222 NORMAL_KEY_DOWN, // The non modifier key is down. |
| 190 NORMAL_KEY_UP, // The non modifier key is up. | 223 NORMAL_KEY_UP, // The non modifier key is up. |
| 191 OTHER_MODIFIER_DOWN, // The modifier key but not monitored key is down. | 224 OTHER_MODIFIER_DOWN, // The modifier key but not monitored key is down. |
| 192 OTHER_MODIFIER_UP, // The modifier key but not monitored key is up. | 225 OTHER_MODIFIER_UP, // The modifier key but not monitored key is up. |
| 193 }; | 226 }; |
| 194 | 227 |
| 195 // Translates |event| to sticky keys event type. | 228 // Translates event type and key code to sticky keys event type. |
| 196 KeyEventType TranslateKeyEvent(ui::KeyEvent* event); | 229 KeyEventType TranslateKeyEvent(ui::EventType type, ui::KeyboardCode key_code); |
| 197 | 230 |
| 198 // Handles key event in DISABLED state. | 231 // Handles key event in DISABLED state. Returns true if sticky keys |
| 199 bool HandleDisabledState(ui::KeyEvent* event); | 232 // consumes the keyboard event. |
| 233 bool HandleDisabledState(const ui::KeyEvent& event, |
| 234 ui::KeyboardCode key_code); |
| 200 | 235 |
| 201 // Handles key event in ENABLED state. | 236 // Handles key event in ENABLED state. Returns true if sticky keys |
| 202 bool HandleEnabledState(ui::KeyEvent* event); | 237 // consumes the keyboard event. |
| 238 bool HandleEnabledState(const ui::KeyEvent& event, |
| 239 ui::KeyboardCode key_code, |
| 240 int* mod_down_flags, |
| 241 int* mod_up_flags); |
| 203 | 242 |
| 204 // Handles key event in LOCKED state. | 243 // Handles key event in LOCKED state. Returns true if sticky keys |
| 205 bool HandleLockedState(ui::KeyEvent* event); | 244 // consumes the keyboard event. |
| 206 | 245 bool HandleLockedState(const ui::KeyEvent& event, |
| 207 // Dispatches |event| to its target and then dispatch a key released event | 246 ui::KeyboardCode key_code, |
| 208 // for the modifier key. This function is required to ensure that the events | 247 int* mod_down_flags, |
| 209 // are sent in the correct order when disabling sticky key after a key/mouse | 248 int* mod_up_flags); |
| 210 // button press. | |
| 211 void DispatchEventAndReleaseModifier(ui::Event* event); | |
| 212 | |
| 213 // Adds |modifier_flags_| to a native X11 event state mask. | |
| 214 void AppendNativeEventMask(unsigned int* state); | |
| 215 | |
| 216 // Adds |modifier_flags_| into |event|. | |
| 217 void AppendModifier(ui::KeyEvent* event); | |
| 218 void AppendModifier(ui::MouseEvent* event); | |
| 219 void AppendModifier(ui::ScrollEvent* event); | |
| 220 | 249 |
| 221 // The modifier flag to be monitored and appended to events. | 250 // The modifier flag to be monitored and appended to events. |
| 222 const ui::EventFlags modifier_flag_; | 251 const ui::EventFlags modifier_flag_; |
| 223 | 252 |
| 224 // The current sticky key status. | 253 // The current sticky key status. |
| 225 StickyKeyState current_state_; | 254 StickyKeyState current_state_; |
| 226 | 255 |
| 227 // True if the received key event is sent by StickyKeyHandler. | |
| 228 bool event_from_myself_; | |
| 229 | |
| 230 // True if we received the TARGET_MODIFIER_DOWN event while in the DISABLED | 256 // True if we received the TARGET_MODIFIER_DOWN event while in the DISABLED |
| 231 // state but before we receive the TARGET_MODIFIER_UP event. Normal | 257 // state but before we receive the TARGET_MODIFIER_UP event. Normal |
| 232 // shortcuts (eg. ctrl + t) during this time will prevent a transition to | 258 // shortcuts (eg. ctrl + t) during this time will prevent a transition to |
| 233 // the ENABLED state. | 259 // the ENABLED state. |
| 234 bool preparing_to_enable_; | 260 bool preparing_to_enable_; |
| 235 | 261 |
| 236 // Tracks the scroll direction of the current scroll sequence. Sticky keys | 262 // Tracks the scroll direction of the current scroll sequence. Sticky keys |
| 237 // stops modifying the scroll events of the sequence when the direction | 263 // stops modifying the scroll events of the sequence when the direction |
| 238 // changes. If no sequence is tracked, the value is 0. | 264 // changes. If no sequence is tracked, the value is 0. |
| 239 int scroll_delta_; | 265 int scroll_delta_; |
| 240 | 266 |
| 241 // The modifier up key event to be sent on non modifier key on ENABLED state. | 267 // The modifier up key event to be sent on non modifier key on ENABLED state. |
| 242 scoped_ptr<ui::KeyEvent> modifier_up_event_; | 268 scoped_ptr<ui::KeyEvent> modifier_up_event_; |
| 243 | 269 |
| 244 scoped_ptr<StickyKeysHandlerDelegate> delegate_; | |
| 245 | |
| 246 DISALLOW_COPY_AND_ASSIGN(StickyKeysHandler); | 270 DISALLOW_COPY_AND_ASSIGN(StickyKeysHandler); |
| 247 }; | 271 }; |
| 248 | 272 |
| 249 } // namespace ash | 273 } // namespace ash |
| 250 | 274 |
| 251 #endif // ASH_STICKY_KEYS_STICKY_KEYS_CONTROLLER_H_ | 275 #endif // ASH_STICKY_KEYS_STICKY_KEYS_CONTROLLER_H_ |
| OLD | NEW |