OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 #include "ash/content/display/screen_orientation_delegate_chromeos.h" | 5 #include "ash/content/display/screen_orientation_controller_chromeos.h" |
6 | 6 |
7 #include "ash/ash_switches.h" | 7 #include "ash/ash_switches.h" |
8 #include "ash/display/display_info.h" | 8 #include "ash/display/display_info.h" |
9 #include "ash/display/display_manager.h" | 9 #include "ash/display/display_manager.h" |
10 #include "ash/shell.h" | 10 #include "ash/shell.h" |
11 #include "ash/wm/maximize_mode/maximize_mode_controller.h" | 11 #include "ash/wm/maximize_mode/maximize_mode_controller.h" |
| 12 #include "base/auto_reset.h" |
12 #include "base/command_line.h" | 13 #include "base/command_line.h" |
| 14 #include "chromeos/accelerometer/accelerometer_reader.h" |
13 #include "content/public/browser/screen_orientation_provider.h" | 15 #include "content/public/browser/screen_orientation_provider.h" |
14 #include "content/public/browser/web_contents.h" | 16 #include "content/public/browser/web_contents.h" |
15 #include "ui/aura/window.h" | 17 #include "ui/aura/window.h" |
16 #include "ui/gfx/display.h" | 18 #include "ui/gfx/display.h" |
17 #include "ui/gfx/geometry/size.h" | 19 #include "ui/gfx/geometry/size.h" |
18 | 20 |
19 namespace { | 21 namespace { |
20 | 22 |
| 23 // The angle which the screen has to be rotated past before the display will |
| 24 // rotate to match it (i.e. 45.0f is no stickiness). |
| 25 const float kDisplayRotationStickyAngleDegrees = 60.0f; |
| 26 |
| 27 // The minimum acceleration in m/s^2 in a direction required to trigger screen |
| 28 // rotation. This prevents rapid toggling of rotation when the device is near |
| 29 // flat and there is very little screen aligned force on it. The value is |
| 30 // effectively the sine of the rise angle required times the acceleration due |
| 31 // to gravity, with the current value requiring at least a 25 degree rise. |
| 32 const float kMinimumAccelerationScreenRotation = 4.2f; |
| 33 |
21 blink::WebScreenOrientationLockType GetDisplayNaturalOrientation() { | 34 blink::WebScreenOrientationLockType GetDisplayNaturalOrientation() { |
22 ash::DisplayManager* display_manager = | 35 ash::DisplayManager* display_manager = |
23 ash::Shell::GetInstance()->display_manager(); | 36 ash::Shell::GetInstance()->display_manager(); |
24 if (!display_manager->HasInternalDisplay()) | 37 if (!display_manager->HasInternalDisplay()) |
25 return blink::WebScreenOrientationLockLandscape; | 38 return blink::WebScreenOrientationLockLandscape; |
26 | 39 |
27 ash::DisplayInfo info = | 40 ash::DisplayInfo info = |
28 display_manager->GetDisplayInfo(gfx::Display::InternalDisplayId()); | 41 display_manager->GetDisplayInfo(gfx::Display::InternalDisplayId()); |
29 gfx::Size size = info.size_in_pixel(); | 42 gfx::Size size = info.size_in_pixel(); |
30 switch (info.rotation()) { | 43 switch (info.rotation()) { |
31 case gfx::Display::ROTATE_0: | 44 case gfx::Display::ROTATE_0: |
32 case gfx::Display::ROTATE_180: | 45 case gfx::Display::ROTATE_180: |
33 return size.height() >= size.width() | 46 return size.height() >= size.width() |
34 ? blink::WebScreenOrientationLockPortrait | 47 ? blink::WebScreenOrientationLockPortrait |
35 : blink::WebScreenOrientationLockLandscape; | 48 : blink::WebScreenOrientationLockLandscape; |
36 case gfx::Display::ROTATE_90: | 49 case gfx::Display::ROTATE_90: |
37 case gfx::Display::ROTATE_270: | 50 case gfx::Display::ROTATE_270: |
38 return size.height() < size.width() | 51 return size.height() < size.width() |
39 ? blink::WebScreenOrientationLockPortrait | 52 ? blink::WebScreenOrientationLockPortrait |
40 : blink::WebScreenOrientationLockLandscape; | 53 : blink::WebScreenOrientationLockLandscape; |
41 } | 54 } |
42 NOTREACHED(); | 55 NOTREACHED(); |
43 return blink::WebScreenOrientationLockLandscape; | 56 return blink::WebScreenOrientationLockLandscape; |
44 } | 57 } |
45 | 58 |
46 } // namespace | 59 } // namespace |
47 | 60 |
48 namespace ash { | 61 namespace ash { |
49 | 62 |
50 ScreenOrientationDelegate::ScreenOrientationDelegate() | 63 ScreenOrientationController::ScreenOrientationController() |
51 : locking_window_(NULL), | 64 : locking_window_(NULL), |
52 natural_orientation_(GetDisplayNaturalOrientation()) { | 65 natural_orientation_(GetDisplayNaturalOrientation()), |
| 66 ignore_display_configuration_updates_(false), |
| 67 rotation_locked_(false), |
| 68 user_rotation_(gfx::Display::ROTATE_0), |
| 69 current_rotation_(gfx::Display::ROTATE_0) { |
53 content::ScreenOrientationProvider::SetDelegate(this); | 70 content::ScreenOrientationProvider::SetDelegate(this); |
| 71 Shell::GetInstance()->AddShellObserver(this); |
54 } | 72 } |
55 | 73 |
56 ScreenOrientationDelegate::~ScreenOrientationDelegate() { | 74 ScreenOrientationController::~ScreenOrientationController() { |
57 content::ScreenOrientationProvider::SetDelegate(NULL); | 75 content::ScreenOrientationProvider::SetDelegate(NULL); |
| 76 Shell::GetInstance()->RemoveShellObserver(this); |
| 77 Shell::GetInstance()->accelerometer_reader()->RemoveObserver(this); |
| 78 Shell::GetInstance()->display_controller()->RemoveObserver(this); |
58 } | 79 } |
59 | 80 |
60 bool ScreenOrientationDelegate::FullScreenRequired( | 81 void ScreenOrientationController::AddObserver(Observer* observer) { |
| 82 observers_.AddObserver(observer); |
| 83 } |
| 84 |
| 85 void ScreenOrientationController::RemoveObserver(Observer* observer) { |
| 86 observers_.RemoveObserver(observer); |
| 87 } |
| 88 |
| 89 void ScreenOrientationController::SetRotationLocked(bool rotation_locked) { |
| 90 if (rotation_locked_ == rotation_locked) |
| 91 return; |
| 92 rotation_locked_ = rotation_locked; |
| 93 FOR_EACH_OBSERVER(Observer, observers_, |
| 94 OnRotationLockChanged(rotation_locked_)); |
| 95 DisplayManager* display_manager = Shell::GetInstance()->display_manager(); |
| 96 if (!display_manager->HasInternalDisplay()) |
| 97 return; |
| 98 base::AutoReset<bool> auto_ignore_display_configuration_updates( |
| 99 &ignore_display_configuration_updates_, true); |
| 100 display_manager->RegisterDisplayRotationProperties(rotation_locked_, |
| 101 current_rotation_); |
| 102 } |
| 103 |
| 104 void ScreenOrientationController::SetDisplayRotation( |
| 105 gfx::Display::Rotation rotation) { |
| 106 DisplayManager* display_manager = Shell::GetInstance()->display_manager(); |
| 107 if (!display_manager->HasInternalDisplay()) |
| 108 return; |
| 109 current_rotation_ = rotation; |
| 110 base::AutoReset<bool> auto_ignore_display_configuration_updates( |
| 111 &ignore_display_configuration_updates_, true); |
| 112 display_manager->SetDisplayRotation(gfx::Display::InternalDisplayId(), |
| 113 rotation); |
| 114 } |
| 115 |
| 116 void ScreenOrientationController::OnAccelerometerUpdated( |
| 117 const ui::AccelerometerUpdate& update) { |
| 118 if (rotation_locked_) |
| 119 return; |
| 120 if (!update.has(ui::ACCELEROMETER_SOURCE_SCREEN)) |
| 121 return; |
| 122 // Ignore the reading if it appears unstable. The reading is considered |
| 123 // unstable if it deviates too much from gravity |
| 124 if (chromeos::AccelerometerReader::IsReadingStable( |
| 125 update, ui::ACCELEROMETER_SOURCE_SCREEN)) { |
| 126 HandleScreenRotation(update.get(ui::ACCELEROMETER_SOURCE_SCREEN)); |
| 127 } |
| 128 } |
| 129 |
| 130 bool ScreenOrientationController::FullScreenRequired( |
61 content::WebContents* web_contents) { | 131 content::WebContents* web_contents) { |
62 return true; | 132 return true; |
63 } | 133 } |
64 | 134 |
65 void ScreenOrientationDelegate::Lock( | 135 void ScreenOrientationController::Lock( |
66 content::WebContents* web_contents, | 136 content::WebContents* web_contents, |
67 blink::WebScreenOrientationLockType lock_orientation) { | 137 blink::WebScreenOrientationLockType lock_orientation) { |
68 aura::Window* requesting_window = web_contents->GetNativeView(); | 138 aura::Window* requesting_window = web_contents->GetNativeView(); |
69 | |
70 // TODO(jonross): Make ScreenOrientationDelegate responsible for rotation | |
71 // lock. Have MaximizeModeController, and TrayRotationLock both use it | |
72 // instead. | |
73 MaximizeModeController* controller = | |
74 Shell::GetInstance()->maximize_mode_controller(); | |
75 | |
76 // TODO(jonross): Track one rotation lock per window. When the active window | 139 // TODO(jonross): Track one rotation lock per window. When the active window |
77 // changes apply any corresponding rotation lock. | 140 // changes apply any corresponding rotation lock. |
78 if (!locking_window_) | 141 if (!locking_window_) |
79 locking_window_ = requesting_window; | 142 locking_window_ = requesting_window; |
80 else if (requesting_window != locking_window_) | 143 else if (requesting_window != locking_window_) |
81 return; | 144 return; |
82 | 145 |
83 switch (lock_orientation) { | 146 switch (lock_orientation) { |
84 case blink::WebScreenOrientationLockAny: | 147 case blink::WebScreenOrientationLockAny: |
85 controller->SetRotationLocked(false); | 148 SetRotationLocked(false); |
86 locking_window_ = NULL; | 149 locking_window_ = NULL; |
87 break; | 150 break; |
88 case blink::WebScreenOrientationLockDefault: | 151 case blink::WebScreenOrientationLockDefault: |
89 NOTREACHED(); | 152 NOTREACHED(); |
90 break; | 153 break; |
91 case blink::WebScreenOrientationLockPortraitPrimary: | 154 case blink::WebScreenOrientationLockPortraitPrimary: |
92 LockRotationToPrimaryOrientation(blink::WebScreenOrientationLockPortrait); | 155 LockRotationToPrimaryOrientation(blink::WebScreenOrientationLockPortrait); |
93 break; | 156 break; |
94 case blink::WebScreenOrientationLockLandscape: | 157 case blink::WebScreenOrientationLockLandscape: |
95 case blink::WebScreenOrientationLockPortrait: | 158 case blink::WebScreenOrientationLockPortrait: |
96 LockToRotationMatchingOrientation(lock_orientation); | 159 LockToRotationMatchingOrientation(lock_orientation); |
97 break; | 160 break; |
98 case blink::WebScreenOrientationLockPortraitSecondary: | 161 case blink::WebScreenOrientationLockPortraitSecondary: |
99 LockRotationToSecondaryOrientation( | 162 LockRotationToSecondaryOrientation( |
100 blink::WebScreenOrientationLockPortrait); | 163 blink::WebScreenOrientationLockPortrait); |
101 break; | 164 break; |
102 case blink::WebScreenOrientationLockLandscapeSecondary: | 165 case blink::WebScreenOrientationLockLandscapeSecondary: |
103 LockRotationToSecondaryOrientation( | 166 LockRotationToSecondaryOrientation( |
104 blink::WebScreenOrientationLockLandscape); | 167 blink::WebScreenOrientationLockLandscape); |
105 break; | 168 break; |
106 case blink::WebScreenOrientationLockLandscapePrimary: | 169 case blink::WebScreenOrientationLockLandscapePrimary: |
107 LockRotationToPrimaryOrientation( | 170 LockRotationToPrimaryOrientation( |
108 blink::WebScreenOrientationLockLandscape); | 171 blink::WebScreenOrientationLockLandscape); |
109 break; | 172 break; |
110 case blink::WebScreenOrientationLockNatural: | 173 case blink::WebScreenOrientationLockNatural: |
111 controller->LockRotation(gfx::Display::ROTATE_0); | 174 LockRotation(gfx::Display::ROTATE_0); |
112 break; | 175 break; |
113 default: | 176 default: |
114 NOTREACHED(); | 177 NOTREACHED(); |
115 break; | 178 break; |
116 } | 179 } |
117 } | 180 } |
118 | 181 |
119 bool ScreenOrientationDelegate::ScreenOrientationProviderSupported() { | 182 bool ScreenOrientationController::ScreenOrientationProviderSupported() { |
120 return Shell::GetInstance() | 183 return Shell::GetInstance() |
121 ->maximize_mode_controller() | 184 ->maximize_mode_controller() |
122 ->IsMaximizeModeWindowManagerEnabled() && | 185 ->IsMaximizeModeWindowManagerEnabled() && |
123 base::CommandLine::ForCurrentProcess()->HasSwitch( | 186 base::CommandLine::ForCurrentProcess()->HasSwitch( |
124 switches::kAshEnableTouchViewTesting); | 187 switches::kAshEnableTouchViewTesting); |
125 } | 188 } |
126 | 189 |
127 void ScreenOrientationDelegate::Unlock(content::WebContents* web_contents) { | 190 void ScreenOrientationController::Unlock(content::WebContents* web_contents) { |
128 aura::Window* requesting_window = web_contents->GetNativeView(); | 191 aura::Window* requesting_window = web_contents->GetNativeView(); |
129 if (requesting_window != locking_window_) | 192 if (requesting_window != locking_window_) |
130 return; | 193 return; |
131 locking_window_ = NULL; | 194 locking_window_ = NULL; |
132 Shell::GetInstance()->maximize_mode_controller()->SetRotationLocked(false); | 195 SetRotationLocked(false); |
133 } | 196 } |
134 | 197 |
135 void ScreenOrientationDelegate::LockRotationToPrimaryOrientation( | 198 void ScreenOrientationController::OnDisplayConfigurationChanged() { |
136 blink::WebScreenOrientationLockType lock_orientation) { | 199 if (ignore_display_configuration_updates_) |
137 Shell::GetInstance()->maximize_mode_controller()->LockRotation( | 200 return; |
138 natural_orientation_ == lock_orientation ? gfx::Display::ROTATE_0 | 201 gfx::Display::Rotation user_rotation = |
139 : gfx::Display::ROTATE_90); | 202 Shell::GetInstance() |
| 203 ->display_manager() |
| 204 ->GetDisplayInfo(gfx::Display::InternalDisplayId()) |
| 205 .rotation(); |
| 206 if (user_rotation != current_rotation_) { |
| 207 // A user may change other display configuration settings. When the user |
| 208 // does change the rotation setting, then lock rotation to prevent the |
| 209 // accelerometer from erasing their change. |
| 210 SetRotationLocked(true); |
| 211 user_rotation_ = current_rotation_ = user_rotation; |
| 212 } |
140 } | 213 } |
141 | 214 |
142 void ScreenOrientationDelegate::LockRotationToSecondaryOrientation( | 215 void ScreenOrientationController::OnMaximizeModeStarted() { |
143 blink::WebScreenOrientationLockType lock_orientation) { | 216 DisplayManager* display_manager = Shell::GetInstance()->display_manager(); |
144 Shell::GetInstance()->maximize_mode_controller()->LockRotation( | 217 if (!display_manager->HasInternalDisplay()) |
145 natural_orientation_ == lock_orientation ? gfx::Display::ROTATE_180 | 218 return; |
146 : gfx::Display::ROTATE_270); | 219 current_rotation_ = user_rotation_ = |
| 220 display_manager->GetDisplayInfo(gfx::Display::InternalDisplayId()) |
| 221 .rotation(); |
| 222 LoadDisplayRotationProperties(); |
| 223 Shell::GetInstance()->accelerometer_reader()->AddObserver(this); |
| 224 Shell::GetInstance()->display_controller()->AddObserver(this); |
147 } | 225 } |
148 | 226 |
149 void ScreenOrientationDelegate::LockToRotationMatchingOrientation( | 227 void ScreenOrientationController::OnMaximizeModeEnded() { |
| 228 if (!Shell::GetInstance()->display_manager()->HasInternalDisplay()) |
| 229 return; |
| 230 Shell::GetInstance()->accelerometer_reader()->RemoveObserver(this); |
| 231 Shell::GetInstance()->display_controller()->RemoveObserver(this); |
| 232 if (current_rotation_ != user_rotation_) |
| 233 SetDisplayRotation(user_rotation_); |
| 234 } |
| 235 |
| 236 void ScreenOrientationController::LockRotation( |
| 237 gfx::Display::Rotation rotation) { |
| 238 SetRotationLocked(true); |
| 239 SetDisplayRotation(rotation); |
| 240 } |
| 241 |
| 242 void ScreenOrientationController::LockRotationToPrimaryOrientation( |
| 243 blink::WebScreenOrientationLockType lock_orientation) { |
| 244 LockRotation(natural_orientation_ == lock_orientation |
| 245 ? gfx::Display::ROTATE_0 |
| 246 : gfx::Display::ROTATE_90); |
| 247 } |
| 248 |
| 249 void ScreenOrientationController::LockRotationToSecondaryOrientation( |
| 250 blink::WebScreenOrientationLockType lock_orientation) { |
| 251 LockRotation(natural_orientation_ == lock_orientation |
| 252 ? gfx::Display::ROTATE_180 |
| 253 : gfx::Display::ROTATE_270); |
| 254 } |
| 255 |
| 256 void ScreenOrientationController::LockToRotationMatchingOrientation( |
150 blink::WebScreenOrientationLockType lock_orientation) { | 257 blink::WebScreenOrientationLockType lock_orientation) { |
151 // TODO(jonross): Update MaximizeModeController to allow rotation between | 258 // TODO(jonross): Update MaximizeModeController to allow rotation between |
152 // two angles of an orientation (e.g. from ROTATE_0 to ROTATE_180, and from | 259 // two angles of an orientation (e.g. from ROTATE_0 to ROTATE_180, and from |
153 // ROTATE_90 to ROTATE_270) | 260 // ROTATE_90 to ROTATE_270) |
154 DisplayManager* display_manager = Shell::GetInstance()->display_manager(); | 261 DisplayManager* display_manager = Shell::GetInstance()->display_manager(); |
155 if (!display_manager->HasInternalDisplay()) | 262 if (!display_manager->HasInternalDisplay()) |
156 return; | 263 return; |
157 | 264 |
158 gfx::Display::Rotation rotation = | 265 gfx::Display::Rotation rotation = |
159 display_manager->GetDisplayInfo(gfx::Display::InternalDisplayId()) | 266 display_manager->GetDisplayInfo(gfx::Display::InternalDisplayId()) |
160 .rotation(); | 267 .rotation(); |
161 MaximizeModeController* controller = | |
162 Shell::GetInstance()->maximize_mode_controller(); | |
163 if (natural_orientation_ == lock_orientation) { | 268 if (natural_orientation_ == lock_orientation) { |
164 if (rotation == gfx::Display::ROTATE_0 || | 269 if (rotation == gfx::Display::ROTATE_0 || |
165 rotation == gfx::Display::ROTATE_180) { | 270 rotation == gfx::Display::ROTATE_180) { |
166 controller->SetRotationLocked(true); | 271 SetRotationLocked(true); |
167 } else { | 272 } else { |
168 controller->LockRotation(gfx::Display::ROTATE_0); | 273 LockRotation(gfx::Display::ROTATE_0); |
169 } | 274 } |
170 } else { | 275 } else { |
171 if (rotation == gfx::Display::ROTATE_90 || | 276 if (rotation == gfx::Display::ROTATE_90 || |
172 rotation == gfx::Display::ROTATE_270) { | 277 rotation == gfx::Display::ROTATE_270) { |
173 controller->SetRotationLocked(true); | 278 SetRotationLocked(true); |
174 } else { | 279 } else { |
175 controller->LockRotation(gfx::Display::ROTATE_90); | 280 LockRotation(gfx::Display::ROTATE_90); |
176 } | 281 } |
177 } | 282 } |
178 } | 283 } |
179 | 284 |
| 285 void ScreenOrientationController::HandleScreenRotation( |
| 286 const gfx::Vector3dF& lid) { |
| 287 gfx::Vector3dF lid_flattened(lid.x(), lid.y(), 0.0f); |
| 288 float lid_flattened_length = lid_flattened.Length(); |
| 289 // When the lid is close to being flat, don't change rotation as it is too |
| 290 // sensitive to slight movements. |
| 291 if (lid_flattened_length < kMinimumAccelerationScreenRotation) |
| 292 return; |
| 293 |
| 294 // The reference vector is the angle of gravity when the device is rotated |
| 295 // clockwise by 45 degrees. Computing the angle between this vector and |
| 296 // gravity we can easily determine the expected display rotation. |
| 297 static const gfx::Vector3dF rotation_reference(-1.0f, -1.0f, 0.0f); |
| 298 |
| 299 // Set the down vector to match the expected direction of gravity given the |
| 300 // last configured rotation. This is used to enforce a stickiness that the |
| 301 // user must overcome to rotate the display and prevents frequent rotations |
| 302 // when holding the device near 45 degrees. |
| 303 gfx::Vector3dF down(0.0f, 0.0f, 0.0f); |
| 304 if (current_rotation_ == gfx::Display::ROTATE_0) |
| 305 down.set_y(-1.0f); |
| 306 else if (current_rotation_ == gfx::Display::ROTATE_90) |
| 307 down.set_x(-1.0f); |
| 308 else if (current_rotation_ == gfx::Display::ROTATE_180) |
| 309 down.set_y(1.0f); |
| 310 else |
| 311 down.set_x(1.0f); |
| 312 |
| 313 // Don't rotate if the screen has not passed the threshold. |
| 314 if (gfx::AngleBetweenVectorsInDegrees(down, lid_flattened) < |
| 315 kDisplayRotationStickyAngleDegrees) { |
| 316 return; |
| 317 } |
| 318 |
| 319 float angle = gfx::ClockwiseAngleBetweenVectorsInDegrees( |
| 320 rotation_reference, lid_flattened, gfx::Vector3dF(0.0f, 0.0f, -1.0f)); |
| 321 |
| 322 gfx::Display::Rotation new_rotation = gfx::Display::ROTATE_90; |
| 323 if (angle < 90.0f) |
| 324 new_rotation = gfx::Display::ROTATE_0; |
| 325 else if (angle < 180.0f) |
| 326 new_rotation = gfx::Display::ROTATE_270; |
| 327 else if (angle < 270.0f) |
| 328 new_rotation = gfx::Display::ROTATE_180; |
| 329 |
| 330 if (new_rotation != current_rotation_) |
| 331 SetDisplayRotation(new_rotation); |
| 332 } |
| 333 |
| 334 void ScreenOrientationController::LoadDisplayRotationProperties() { |
| 335 DisplayManager* display_manager = Shell::GetInstance()->display_manager(); |
| 336 if (!display_manager->registered_internal_display_rotation_lock()) |
| 337 return; |
| 338 SetDisplayRotation(display_manager->registered_internal_display_rotation()); |
| 339 SetRotationLocked(true); |
| 340 } |
| 341 |
180 } // namespace ash | 342 } // namespace ash |
OLD | NEW |