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