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 UI_CHROMEOS_TOUCH_EXPLORATION_CONTROLLER_H_
6 #define UI_CHROMEOS_TOUCH_EXPLORATION_CONTROLLER_H_
8 #include "base/time/tick_clock.h"
9 #include "base/timer/timer.h"
10 #include "base/values.h"
11 #include "ui/chromeos/ui_chromeos_export.h"
12 #include "ui/events/event.h"
13 #include "ui/events/event_rewriter.h"
14 #include "ui/events/gesture_detection/gesture_detector.h"
15 #include "ui/events/gestures/gesture_provider_aura.h"
16 #include "ui/gfx/geometry/point.h"
27 class GestureProviderAura;
30 // A delegate to handle commands in response to detected accessibility gesture
32 class TouchExplorationControllerDelegate {
34 virtual ~TouchExplorationControllerDelegate() {}
36 // Takes an int from 0.0 to 100.0 that indicates the percent the volume
38 virtual void SetOutputLevel(int volume) = 0;
40 // Silences spoken feedback.
41 virtual void SilenceSpokenFeedback() = 0;
43 // This function should be called when the volume adjust earcon should be
45 virtual void PlayVolumeAdjustEarcon() = 0;
47 // This function should be called when the passthrough earcon should be
49 virtual void PlayPassthroughEarcon() = 0;
51 // This function should be called when the exit screen earcon should be
53 virtual void PlayExitScreenEarcon() = 0;
55 // This function should be called when the enter screen earcon should be
57 virtual void PlayEnterScreenEarcon() = 0;
60 // TouchExplorationController is used in tandem with "Spoken Feedback" to
61 // make the touch UI accessible. Gestures performed in the middle of the screen
62 // are mapped to accessibility key shortcuts while gestures performed on the
63 // edge of the screen can change settings.
65 // ** Short version **
67 // At a high-level, single-finger events are used for accessibility -
68 // exploring the screen gets turned into mouse moves (which can then be
69 // spoken by an accessibility service running), a single tap while the user
70 // is in touch exploration or a double-tap simulates a click, and gestures
71 // can be used to send high-level accessibility commands. For example, a swipe
72 // right would correspond to the keyboard short cut shift+search+right.
73 // Swipes with up to four fingers are also mapped to commands. Slide
74 // gestures performed on the edge of the screen can change settings
75 // continuously. For example, sliding a finger along the right side of the
76 // screen will change the volume. When a user double taps and holds with one
77 // finger, the finger is passed through as if accessibility was turned off. If
78 // the user taps the screen with two fingers, the user can silence spoken
79 // feedback if it is playing.
83 // Here are the details of the implementation:
85 // When the first touch is pressed, a 300 ms grace period timer starts.
87 // If the user keeps their finger down for more than 300 ms and doesn't
88 // perform a supported accessibility gesture in that time (e.g. swipe right),
89 // they enter touch exploration mode, and all movements are translated into
90 // synthesized mouse move events.
92 // Also, if the user moves their single finger outside a certain slop region
93 // (without performing a gesture), they enter touch exploration mode earlier
96 // If the user taps and releases their finger, after 300 ms from the initial
97 // touch, a single mouse move is fired.
99 // While in touch exploration mode, the user can perform a single tap
100 // if the user releases their finger and taps before 300 ms passes.
101 // This will result in a click on the last successful touch exploration
102 // location. This allows the user to perform a single tap
103 // anywhere to activate it.
105 // The user can perform swipe gestures in one of the four cardinal directions
106 // which will be interpreted and used to control the UI. All gestures will only
107 // be registered if the fingers move outside the slop, and all fingers will only
108 // be registered if they are completed within the grace period. If a single
109 // finger gesture fails to be completed within the grace period, the state
110 // changes to touch exploration mode. If a multi finger gesture fails to be
111 // completed within the grace period, the user must lift all fingers before
112 // completing any more actions.
114 // If the user double-taps, the second tap is passed through, allowing the
115 // user to click - however, the double-tap location is changed to the location
116 // of the last successful touch exploration - that allows the user to explore
117 // anywhere on the screen, hear its description, then double-tap anywhere
120 // If the user double taps and holds, any event from that finger is passed
121 // through. These events are passed through with an offset such that the first
122 // touch is offset to be at the location of the last touch exploration
123 // location, and every following event is offset by the same amount.
125 // If any other fingers are added or removed, they are ignored. Once the
126 // passthrough finger is released, passthrough stops and the user is reset
127 // to no fingers down state.
129 // If the user enters touch exploration mode, they can click without lifting
130 // their touch exploration finger by tapping anywhere else on the screen with
131 // a second finger, while the touch exploration finger is still pressed.
133 // Once touch exploration mode has been activated, it remains in that mode until
134 // all fingers have been released.
136 // If the user places a finger on the edge of the screen and moves their finger
137 // past slop, a slide gesture is performed. The user can then slide one finger
138 // along an edge of the screen and continuously control a setting. Once the user
139 // enters this state, the boundaries that define an edge expand so that the user
140 // can now adjust the setting within a slightly bigger width along the screen.
141 // If the user exits this area without lifting their finger, they will not be
142 // able to perform any actions, however if they keep their finger down and
143 // return to the "hot edge," then they can still adjust the setting. In order to
144 // perform other touch accessibility movements, the user must lift their finger.
145 // If additional fingers are added while in this state, the user will transition
148 // Currently, only the right edge is mapped to control the volume. Volume
149 // control along the edge of the screen is directly proportional to where the
150 // user's finger is located on the screen. The top right corner of the screen
151 // automatically sets the volume to 100% and the bottome right corner of the
152 // screen automatically sets the volume to 0% once the user has moved past slop.
154 // If the user taps the screen with two fingers and lifts both fingers before
155 // the grace period has passed, spoken feedback is silenced.
157 // The user can also enter passthrough by placing a finger on one of the bottom
158 // corners of the screen until an earcon sounds. After the earcon sounds, the
159 // user is in passthrough so all subsequent fingers placed on the screen will be
160 // passed through. Once the finger in the corner has been released, the state
161 // will switch to wait for no fingers.
163 // The caller is expected to retain ownership of instances of this class and
164 // destroy them before |root_window| is destroyed.
165 class UI_CHROMEOS_EXPORT TouchExplorationController
166 : public ui::EventRewriter,
167 public ui::GestureProviderAuraClient {
169 explicit TouchExplorationController(
170 aura::Window* root_window,
171 ui::TouchExplorationControllerDelegate* delegate);
172 virtual ~TouchExplorationController();
175 friend class TouchExplorationControllerTestApi;
177 // Overridden from ui::EventRewriter
178 virtual ui::EventRewriteStatus RewriteEvent(
179 const ui::Event& event,
180 scoped_ptr<ui::Event>* rewritten_event) OVERRIDE;
181 virtual ui::EventRewriteStatus NextDispatchEvent(
182 const ui::Event& last_event, scoped_ptr<ui::Event>* new_event) OVERRIDE;
184 // Event handlers based on the current state - see State, below.
185 ui::EventRewriteStatus InNoFingersDown(
186 const ui::TouchEvent& event, scoped_ptr<ui::Event>* rewritten_event);
187 ui::EventRewriteStatus InSingleTapPressed(
188 const ui::TouchEvent& event, scoped_ptr<ui::Event>* rewritten_event);
189 ui::EventRewriteStatus InSingleTapReleased(
190 const ui::TouchEvent& event, scoped_ptr<ui::Event>* rewritten_event);
191 ui::EventRewriteStatus InTouchExploreReleased(
192 const ui::TouchEvent& event, scoped_ptr<ui::Event>* rewritten_event);
193 ui::EventRewriteStatus InDoubleTapPending(
194 const ui::TouchEvent& event, scoped_ptr<ui::Event>* rewritten_event);
195 ui::EventRewriteStatus InTouchReleasePending(
196 const ui::TouchEvent& event, scoped_ptr<ui::Event>* rewritten_event);
197 ui::EventRewriteStatus InTouchExploration(
198 const ui::TouchEvent& event, scoped_ptr<ui::Event>* rewritten_event);
199 ui::EventRewriteStatus InCornerPassthrough(
200 const ui::TouchEvent& event, scoped_ptr<ui::Event>* rewritten_event);
201 ui::EventRewriteStatus InOneFingerPassthrough(
202 const ui::TouchEvent& event, scoped_ptr<ui::Event>* rewritten_event);
203 ui::EventRewriteStatus InGestureInProgress(
204 const ui::TouchEvent& event, scoped_ptr<ui::Event>* rewritten_event);
205 ui::EventRewriteStatus InTouchExploreSecondPress(
206 const ui::TouchEvent& event, scoped_ptr<ui::Event>* rewritten_event);
207 ui::EventRewriteStatus InWaitForNoFingers(
208 const ui::TouchEvent& event, scoped_ptr<ui::Event>* rewritten_event);
209 ui::EventRewriteStatus InSlideGesture(
210 const ui::TouchEvent& event, scoped_ptr<ui::Event>* rewritten_event);
211 ui::EventRewriteStatus InTwoFingerTap(
212 const ui::TouchEvent& event, scoped_ptr<ui::Event>* rewritten_event);
214 // Returns the current time of the tick clock.
215 base::TimeDelta Now();
217 // This timer is started every time we get the first press event, and
218 // it fires after the double-click timeout elapses (300 ms by default).
219 // If the user taps and releases within 300 ms and doesn't press again,
220 // we treat that as a single mouse move (touch exploration) event.
221 void StartTapTimer();
222 void OnTapTimerFired();
224 // This timer is started every timer we get the first press event and the
225 // finger is in the corner of the screen.
226 // It fires after the corner passthrough delay elapses. If the
227 // user is still in the corner by the time this timer fires, all subsequent
228 // fingers added on the screen will be passed through.
229 void OnPassthroughTimerFired();
231 // Dispatch a new event outside of the event rewriting flow.
232 void DispatchEvent(ui::Event* event);
234 // Overridden from GestureProviderAuraClient.
236 // The gesture provider keeps track of all the touch events after
237 // the user moves fast enough to trigger a gesture. After the user
238 // completes their gesture, this method will decide what keyboard
239 // input their gesture corresponded to.
240 virtual void OnGestureEvent(ui::GestureEvent* gesture) OVERRIDE;
242 // Process the gesture events that have been created.
243 void ProcessGestureEvents();
245 void OnSwipeEvent(ui::GestureEvent* swipe_gesture);
247 void SideSlideControl(ui::GestureEvent* gesture);
249 // Dispatches the keyboard short cut Shift+Search+<arrow key>
250 // outside the event rewritting flow.
251 void DispatchShiftSearchKeyEvent(const ui::KeyboardCode third_key);
253 // Binds DispatchShiftSearchKeyEvent to a specific third key.
254 base::Closure BindShiftSearchKeyEvent(const ui::KeyboardCode third_key);
256 // Dispatches a single key with the given flags.
257 void DispatchKeyWithFlags(const ui::KeyboardCode key, int flags);
259 // Binds DispatchKeyWithFlags to a specific key and flags.
260 base::Closure BindKeyEventWithFlags(const ui::KeyboardCode key, int flags);
262 scoped_ptr<ui::Event> CreateMouseMoveEvent(const gfx::PointF& location,
265 void EnterTouchToMouseMode();
267 void PlaySoundForTimer();
269 // Some constants used in touch_exploration_controller:
271 // Within this many dips of the screen edge, the release event generated will
272 // reset the state to NoFingersDown.
273 const float kLeavingScreenEdge = 6;
275 // Swipe/scroll gestures within these bounds (in DIPs) will change preset
277 const float kMaxDistanceFromEdge = 75;
279 // After a slide gesture has been triggered, if the finger is still within
280 // these bounds (in DIPs), the preset settings will still change.
281 const float kSlopDistanceFromEdge = kMaxDistanceFromEdge + 40;
283 // The split tap slop is a bit more generous since keeping two
284 // fingers in place is a bit harder.
285 const float GetSplitTapTouchSlop();
288 // No fingers are down and no events are pending.
291 // A single finger is down, but we're not yet sure if this is going
292 // to be touch exploration or something else.
295 // The user pressed and released a single finger - a tap - but we have
296 // to wait until the end of the grace period to allow the user to tap the
297 // second time. If the second tap doesn't occurs within the grace period,
298 // we dispatch a mouse move at the location of the first tap.
301 // The user was in touch explore mode and released the finger.
302 // If another touch press occurs within the grace period, a single
303 // tap click occurs. This state differs from SINGLE_TAP_RELEASED
304 // in that if a second tap doesn't occur within the grace period,
305 // there is no mouse move dispatched.
306 TOUCH_EXPLORE_RELEASED,
308 // The user tapped once, and before the grace period expired, pressed
309 // one finger down to begin a double-tap, but has not released it yet.
310 // This could become passthrough, so no touch press is dispatched yet.
313 // The user was doing touch exploration, started split tap, but lifted the
314 // touch exploration finger. Once they remove all fingers, a touch release
316 TOUCH_RELEASE_PENDING,
318 // We're in touch exploration mode. Anything other than the first finger
319 // is ignored, and movements of the first finger are rewritten as mouse
320 // move events. This mode is entered if a single finger is pressed and
321 // after the grace period the user hasn't added a second finger or
322 // moved the finger outside of the slop region. We'll stay in this
323 // mode until all fingers are lifted.
326 // If the user moves their finger faster than the threshold velocity after a
327 // single tap, the touch events that follow will be translated into gesture
328 // events. If the user successfully completes a gesture within the grace
329 // period, the gesture will be interpreted and used to control the UI via
330 // discrete actions - currently by synthesizing key events corresponding to
331 // each gesture Otherwise, the collected gestures are discarded and the
332 // state changes to touch_exploration.
335 // The user was in touch exploration, but has placed down another finger.
336 // If the user releases the second finger, a touch press and release
337 // will go through at the last touch explore location. If the user
338 // releases the touch explore finger, the touch press and release will
339 // still go through once the split tap finger is also lifted. If any
340 // fingers pressed past the first two, the touch press is cancelled and
341 // the user enters the wait state for the fingers to be removed.
342 TOUCH_EXPLORE_SECOND_PRESS,
344 // After the user double taps and holds with a single finger, all events
345 // for that finger are passed through, displaced by an offset. Adding
346 // extra fingers has no effect. This state is left when the user removes
348 ONE_FINGER_PASSTHROUGH,
350 // If the user has pressed and held down the left corner past long press,
351 // then as long as they are holding the corner, all subsequent fingers
352 // registered will be in passthrough.
355 // If the user added another finger in SINGLE_TAP_PRESSED, or if the user
356 // has multiple fingers fingers down in any other state between
357 // passthrough, touch exploration, and gestures, they must release
358 // all fingers before completing any more actions. This state is
359 // generally useful for developing new features, because it creates a
360 // simple way to handle a dead end in user flow.
363 // If the user is within the given bounds from an edge of the screen, not
364 // including corners, then the resulting movements will be interpreted as
368 // If the user taps the screen with two fingers and releases both fingers
369 // before the grace period has passed, spoken feedback will be silenced.
373 enum ScreenLocation {
374 // Hot "edges" of the screen are each represented by a respective bit.
379 BOTTOM_EDGE = 1 << 3,
380 BOTTOM_LEFT_CORNER = LEFT_EDGE | BOTTOM_EDGE,
381 BOTTOM_RIGHT_CORNER = RIGHT_EDGE | BOTTOM_EDGE,
384 // Given a point, if it is within the given bounds of an edge, returns the
385 // edge. If it is within the given bounds of two edges, returns an int with
386 // both bits that represent the respective edges turned on. Otherwise returns
388 int FindEdgesWithinBounds(gfx::Point point, float bounds);
390 // Set the state and modifies any variables related to the state change.
391 // (e.g. resetting the gesture provider).
392 void SetState(State new_state, const char* function_name);
394 void VlogState(const char* function_name);
396 void VlogEvent(const ui::TouchEvent& event, const char* function_name);
398 // Gets enum name from integer value.
399 const char* EnumStateToString(State state);
401 // Maps each single/multi finger swipe to the function that dispatches
402 // the corresponding key events.
403 void InitializeSwipeGestureMaps();
405 aura::Window* root_window_;
407 // Handles volume control. Not owned.
408 ui::TouchExplorationControllerDelegate* delegate_;
410 // A set of touch ids for fingers currently touching the screen.
411 std::vector<int> current_touch_ids_;
413 // Map of touch ids to their last known location.
414 std::map<int, gfx::PointF> touch_locations_;
416 // The current state.
419 // A copy of the event from the initial touch press.
420 scoped_ptr<ui::TouchEvent> initial_press_;
422 // Map of touch ids to where its initial press occurred relative to the
424 std::map<int, gfx::Point> initial_presses_;
426 // In one finger passthrough, the touch is displaced relative to the
427 // last touch exploration location.
428 gfx::Vector2d passthrough_offset_;
430 // Stores the most recent event from a finger that is currently not
431 // sending events through, but might in the future (e.g. before a finger
432 // enters double-tap-hold passthrough, we need to update its location.)
433 scoped_ptr<ui::TouchEvent> last_unused_finger_event_;
435 // The last synthesized mouse move event. When the user double-taps,
436 // we send the passed-through tap to the location of this event.
437 scoped_ptr<ui::TouchEvent> last_touch_exploration_;
439 // A timer that fires after the double-tap delay.
440 base::OneShotTimer<TouchExplorationController> tap_timer_;
442 // A timer that fires to enter passthrough.
443 base::OneShotTimer<TouchExplorationController> passthrough_timer_;
445 // A timer to fire an indicating sound when sliding to change volume.
446 base::RepeatingTimer<TouchExplorationController> sound_timer_;
448 // A default gesture detector config, so we can share the same
449 // timeout and pixel slop constants.
450 ui::GestureDetector::Config gesture_detector_config_;
452 // Gesture Handler to interpret the touch events.
453 scoped_ptr<ui::GestureProviderAura> gesture_provider_;
455 // The previous state entered.
458 // A copy of the previous event passed.
459 scoped_ptr<ui::TouchEvent> prev_event_;
461 // This toggles whether VLOGS are turned on or not.
464 // When touch_exploration_controller gets time relative to real time during
465 // testing, this clock is set to the simulated clock and used.
466 base::TickClock* tick_clock_;
468 // Maps the number of fingers in a swipe to the resulting functions that
469 // dispatch key events.
470 std::map<int, base::Closure> left_swipe_gestures_;
471 std::map<int, base::Closure> right_swipe_gestures_;
472 std::map<int, base::Closure> up_swipe_gestures_;
473 std::map<int, base::Closure> down_swipe_gestures_;
475 DISALLOW_COPY_AND_ASSIGN(TouchExplorationController);
480 #endif // UI_CHROMEOS_TOUCH_EXPLORATION_CONTROLLER_H_