Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(292)

Side by Side Diff: ash/display/screen_orientation_controller_chromeos.cc

Issue 2776283002: User orientation lock (Closed)
Patch Set: Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/display/screen_orientation_controller_chromeos.h" 5 #include "ash/display/screen_orientation_controller_chromeos.h"
6 6
7 #include "ash/common/ash_switches.h" 7 #include "ash/common/ash_switches.h"
8 #include "ash/common/wm/maximize_mode/maximize_mode_controller.h" 8 #include "ash/common/wm/maximize_mode/maximize_mode_controller.h"
9 #include "ash/common/wm/mru_window_tracker.h"
10 #include "ash/common/wm/window_state.h"
9 #include "ash/common/wm_shell.h" 11 #include "ash/common/wm_shell.h"
10 #include "ash/common/wm_window.h" 12 #include "ash/common/wm_window.h"
11 #include "ash/display/display_configuration_controller.h" 13 #include "ash/display/display_configuration_controller.h"
14 #include "ash/shared/app_types.h"
12 #include "ash/shell.h" 15 #include "ash/shell.h"
13 #include "base/auto_reset.h" 16 #include "base/auto_reset.h"
14 #include "base/command_line.h" 17 #include "base/command_line.h"
15 #include "chromeos/accelerometer/accelerometer_reader.h" 18 #include "chromeos/accelerometer/accelerometer_reader.h"
16 #include "chromeos/accelerometer/accelerometer_types.h" 19 #include "chromeos/accelerometer/accelerometer_types.h"
17 #include "ui/chromeos/accelerometer/accelerometer_util.h" 20 #include "ui/chromeos/accelerometer/accelerometer_util.h"
18 #include "ui/display/display.h" 21 #include "ui/display/display.h"
19 #include "ui/display/manager/display_manager.h" 22 #include "ui/display/manager/display_manager.h"
20 #include "ui/display/manager/managed_display_info.h" 23 #include "ui/display/manager/managed_display_info.h"
21 #include "ui/gfx/geometry/size.h" 24 #include "ui/gfx/geometry/size.h"
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
106 WmShell::Get()->RemoveActivationObserver(this); 109 WmShell::Get()->RemoveActivationObserver(this);
107 window->aura_window()->RemoveObserver(this); 110 window->aura_window()->RemoveObserver(this);
108 ApplyLockForActiveWindow(); 111 ApplyLockForActiveWindow();
109 } 112 }
110 113
111 void ScreenOrientationController::UnlockAll() { 114 void ScreenOrientationController::UnlockAll() {
112 for (auto pair : locking_windows_) 115 for (auto pair : locking_windows_)
113 pair.first->aura_window()->RemoveObserver(this); 116 pair.first->aura_window()->RemoveObserver(this);
114 locking_windows_.clear(); 117 locking_windows_.clear();
115 WmShell::Get()->RemoveActivationObserver(this); 118 WmShell::Get()->RemoveActivationObserver(this);
116 SetRotationLocked(false); 119 SetRotationLockedInternal(false);
117 if (user_rotation_ != current_rotation_) 120 if (user_rotation_ != current_rotation_)
118 SetDisplayRotation(user_rotation_, display::Display::ROTATION_SOURCE_USER); 121 SetDisplayRotation(user_rotation_, display::Display::ROTATION_SOURCE_USER);
119 } 122 }
120 123
121 bool ScreenOrientationController::ScreenOrientationProviderSupported() const { 124 bool ScreenOrientationController::ScreenOrientationProviderSupported() const {
122 return WmShell::Get() 125 return WmShell::Get()
123 ->maximize_mode_controller() 126 ->maximize_mode_controller()
124 ->IsMaximizeModeWindowManagerEnabled() && 127 ->IsMaximizeModeWindowManagerEnabled() &&
125 !base::CommandLine::ForCurrentProcess()->HasSwitch( 128 !base::CommandLine::ForCurrentProcess()->HasSwitch(
126 switches::kAshDisableScreenOrientationLock); 129 switches::kAshDisableScreenOrientationLock);
127 } 130 }
128 131
129 void ScreenOrientationController::SetRotationLocked(bool rotation_locked) { 132 void ScreenOrientationController::ToggleUserRotationLock() {
130 if (rotation_locked_ == rotation_locked) 133 if (user_rotation_locked()) {
131 return; 134 user_locked_orientation_ = blink::WebScreenOrientationLockAny;
132 rotation_locked_ = rotation_locked; 135 } else {
133 if (!rotation_locked_) 136 display::Display::Rotation current_rotation =
134 rotation_locked_orientation_ = blink::WebScreenOrientationLockAny; 137 WmShell::Get()
138 ->GetDisplayInfo(display::Display::InternalDisplayId())
139 .GetActiveRotation();
140 blink::WebScreenOrientationLockType natural =
141 GetDisplayNaturalOrientation();
142 if (natural == blink::WebScreenOrientationLockLandscape) {
143 switch (current_rotation) {
144 case display::Display::ROTATE_0:
145 user_locked_orientation_ =
146 blink::WebScreenOrientationLockLandscapePrimary;
147 break;
148 case display::Display::ROTATE_90:
149 user_locked_orientation_ =
150 blink::WebScreenOrientationLockPortraitPrimary;
151 break;
152 case display::Display::ROTATE_180:
153 user_locked_orientation_ =
154 blink::WebScreenOrientationLockLandscapeSecondary;
155 break;
156 case display::Display::ROTATE_270:
157 user_locked_orientation_ =
158 blink::WebScreenOrientationLockPortraitSecondary;
159 break;
160 }
161 } else { // Natural portrait
162 switch (current_rotation) {
163 case display::Display::ROTATE_0:
164 user_locked_orientation_ =
165 blink::WebScreenOrientationLockPortraitPrimary;
166 break;
167 case display::Display::ROTATE_90:
168 user_locked_orientation_ =
169 blink::WebScreenOrientationLockLandscapePrimary;
170 break;
171 case display::Display::ROTATE_180:
172 user_locked_orientation_ =
173 blink::WebScreenOrientationLockPortraitSecondary;
174 break;
175 case display::Display::ROTATE_270:
176 user_locked_orientation_ =
177 blink::WebScreenOrientationLockLandscapeSecondary;
178 break;
179 }
180 }
181 }
182 ApplyLockForActiveWindow();
183
135 for (auto& observer : observers_) 184 for (auto& observer : observers_)
136 observer.OnRotationLockChanged(rotation_locked_); 185 observer.OnUserRotationLockChanged();
137 if (!display::Display::HasInternalDisplay())
138 return;
139 base::AutoReset<bool> auto_ignore_display_configuration_updates(
140 &ignore_display_configuration_updates_, true);
141 Shell::GetInstance()->display_manager()->RegisterDisplayRotationProperties(
142 rotation_locked_, current_rotation_);
143 }
144
145 void ScreenOrientationController::SetDisplayRotation(
146 display::Display::Rotation rotation,
147 display::Display::RotationSource source) {
148 if (!display::Display::HasInternalDisplay())
149 return;
150 current_rotation_ = rotation;
151 base::AutoReset<bool> auto_ignore_display_configuration_updates(
152 &ignore_display_configuration_updates_, true);
153
154 Shell::GetInstance()->display_configuration_controller()->SetDisplayRotation(
155 display::Display::InternalDisplayId(), rotation, source);
156 } 186 }
157 187
158 void ScreenOrientationController::OnWindowActivated(WmWindow* gained_active, 188 void ScreenOrientationController::OnWindowActivated(WmWindow* gained_active,
159 WmWindow* lost_active) { 189 WmWindow* lost_active) {
160 ApplyLockForActiveWindow(); 190 ApplyLockForActiveWindow();
161 } 191 }
162 192
163 void ScreenOrientationController::OnWindowDestroying(aura::Window* window) { 193 void ScreenOrientationController::OnWindowDestroying(aura::Window* window) {
164 UnlockOrientationForWindow(WmWindow::Get(window)); 194 UnlockOrientationForWindow(WmWindow::Get(window));
165 } 195 }
(...skipping 30 matching lines...) Expand all
196 void ScreenOrientationController::OnDisplayConfigurationChanged() { 226 void ScreenOrientationController::OnDisplayConfigurationChanged() {
197 if (ignore_display_configuration_updates_) 227 if (ignore_display_configuration_updates_)
198 return; 228 return;
199 if (!display::Display::HasInternalDisplay()) 229 if (!display::Display::HasInternalDisplay())
200 return; 230 return;
201 display::Display::Rotation user_rotation = 231 display::Display::Rotation user_rotation =
202 WmShell::Get() 232 WmShell::Get()
203 ->GetDisplayInfo(display::Display::InternalDisplayId()) 233 ->GetDisplayInfo(display::Display::InternalDisplayId())
204 .GetActiveRotation(); 234 .GetActiveRotation();
205 if (user_rotation != current_rotation_) { 235 if (user_rotation != current_rotation_) {
236 // TODO(oshima): We should disable the orientation change in settings
237 // because application may not work correctly.
238
206 // A user may change other display configuration settings. When the user 239 // A user may change other display configuration settings. When the user
207 // does change the rotation setting, then lock rotation to prevent the 240 // does change the rotation setting, then lock rotation to prevent the
208 // accelerometer from erasing their change. 241 // accelerometer from erasing their change.
209 SetRotationLocked(true); 242 SetRotationLockedInternal(true);
210 user_rotation_ = current_rotation_ = user_rotation; 243 user_rotation_ = current_rotation_ = user_rotation;
211 } 244 }
212 } 245 }
213 246
214 void ScreenOrientationController::OnMaximizeModeStarted() { 247 void ScreenOrientationController::OnMaximizeModeStarted() {
215 // Do not exit early, as the internal display can be determined after Maximize 248 // Do not exit early, as the internal display can be determined after Maximize
216 // Mode has started. (chrome-os-partner:38796) 249 // Mode has started. (chrome-os-partner:38796)
217 // Always start observing. 250 // Always start observing.
218 if (display::Display::HasInternalDisplay()) { 251 if (display::Display::HasInternalDisplay()) {
219 current_rotation_ = user_rotation_ = 252 current_rotation_ = user_rotation_ =
220 WmShell::Get() 253 WmShell::Get()
221 ->GetDisplayInfo(display::Display::InternalDisplayId()) 254 ->GetDisplayInfo(display::Display::InternalDisplayId())
222 .GetActiveRotation(); 255 .GetActiveRotation();
223 } 256 }
224 if (!rotation_locked_) 257 if (!rotation_locked_)
225 LoadDisplayRotationProperties(); 258 LoadDisplayRotationProperties();
226 chromeos::AccelerometerReader::GetInstance()->AddObserver(this); 259 chromeos::AccelerometerReader::GetInstance()->AddObserver(this);
227 WmShell::Get()->AddDisplayObserver(this); 260 WmShell::Get()->AddDisplayObserver(this);
228 } 261 }
229 262
230 void ScreenOrientationController::OnMaximizeModeEnded() { 263 void ScreenOrientationController::OnMaximizeModeEnded() {
231 chromeos::AccelerometerReader::GetInstance()->RemoveObserver(this); 264 chromeos::AccelerometerReader::GetInstance()->RemoveObserver(this);
232 WmShell::Get()->RemoveDisplayObserver(this); 265 WmShell::Get()->RemoveDisplayObserver(this);
233 if (current_rotation_ != user_rotation_) 266 if (current_rotation_ != user_rotation_)
234 SetDisplayRotation(user_rotation_, display::Display::ROTATION_SOURCE_USER); 267 SetDisplayRotation(user_rotation_, display::Display::ROTATION_SOURCE_USER);
235 } 268 }
236 269
270 void ScreenOrientationController::SetDisplayRotation(
271 display::Display::Rotation rotation,
272 display::Display::RotationSource source) {
273 if (!display::Display::HasInternalDisplay())
274 return;
275 current_rotation_ = rotation;
276 base::AutoReset<bool> auto_ignore_display_configuration_updates(
277 &ignore_display_configuration_updates_, true);
278
279 Shell::GetInstance()->display_configuration_controller()->SetDisplayRotation(
280 display::Display::InternalDisplayId(), rotation, source);
281 }
282
283 void ScreenOrientationController::SetRotationLockedInternal(
284 bool rotation_locked) {
285 if (rotation_locked_ == rotation_locked)
286 return;
287 rotation_locked_ = rotation_locked;
288 if (!rotation_locked_)
289 rotation_locked_orientation_ = blink::WebScreenOrientationLockAny;
290 if (!display::Display::HasInternalDisplay())
291 return;
292 base::AutoReset<bool> auto_ignore_display_configuration_updates(
293 &ignore_display_configuration_updates_, true);
294 Shell::GetInstance()->display_manager()->RegisterDisplayRotationProperties(
295 rotation_locked_, current_rotation_);
296 }
297
237 void ScreenOrientationController::LockRotation( 298 void ScreenOrientationController::LockRotation(
238 display::Display::Rotation rotation, 299 display::Display::Rotation rotation,
239 display::Display::RotationSource source) { 300 display::Display::RotationSource source) {
240 SetRotationLocked(true); 301 SetRotationLockedInternal(true);
241 SetDisplayRotation(rotation, source); 302 SetDisplayRotation(rotation, source);
242 } 303 }
243 304
244 void ScreenOrientationController::LockRotationToOrientation( 305 void ScreenOrientationController::LockRotationToOrientation(
245 blink::WebScreenOrientationLockType lock_orientation) { 306 blink::WebScreenOrientationLockType lock_orientation) {
246 rotation_locked_orientation_ = lock_orientation; 307 rotation_locked_orientation_ = lock_orientation;
247 switch (lock_orientation) { 308 switch (lock_orientation) {
248 case blink::WebScreenOrientationLockAny: 309 case blink::WebScreenOrientationLockAny:
249 SetRotationLocked(false); 310 SetRotationLockedInternal(false);
250 break; 311 break;
251 case blink::WebScreenOrientationLockDefault: 312 case blink::WebScreenOrientationLockDefault:
252 NOTREACHED(); 313 NOTREACHED();
253 break; 314 break;
254 case blink::WebScreenOrientationLockPortraitPrimary: 315 case blink::WebScreenOrientationLockPortraitPrimary:
255 LockRotationToPrimaryOrientation(blink::WebScreenOrientationLockPortrait); 316 LockRotationToPrimaryOrientation(blink::WebScreenOrientationLockPortrait);
256 break; 317 break;
257 case blink::WebScreenOrientationLockLandscape: 318 case blink::WebScreenOrientationLockLandscape:
258 case blink::WebScreenOrientationLockPortrait: 319 case blink::WebScreenOrientationLockPortrait:
259 LockToRotationMatchingOrientation(lock_orientation); 320 LockToRotationMatchingOrientation(lock_orientation);
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
301 if (!display::Display::HasInternalDisplay()) 362 if (!display::Display::HasInternalDisplay())
302 return; 363 return;
303 364
304 display::Display::Rotation rotation = 365 display::Display::Rotation rotation =
305 WmShell::Get() 366 WmShell::Get()
306 ->GetDisplayInfo(display::Display::InternalDisplayId()) 367 ->GetDisplayInfo(display::Display::InternalDisplayId())
307 .GetActiveRotation(); 368 .GetActiveRotation();
308 if (natural_orientation_ == lock_orientation) { 369 if (natural_orientation_ == lock_orientation) {
309 if (rotation == display::Display::ROTATE_0 || 370 if (rotation == display::Display::ROTATE_0 ||
310 rotation == display::Display::ROTATE_180) { 371 rotation == display::Display::ROTATE_180) {
311 SetRotationLocked(true); 372 SetRotationLockedInternal(true);
312 } else { 373 } else {
313 LockRotation(display::Display::ROTATE_0, 374 LockRotation(display::Display::ROTATE_0,
314 display::Display::ROTATION_SOURCE_ACTIVE); 375 display::Display::ROTATION_SOURCE_ACTIVE);
315 } 376 }
316 } else { 377 } else {
317 if (rotation == display::Display::ROTATE_90 || 378 if (rotation == display::Display::ROTATE_90 ||
318 rotation == display::Display::ROTATE_270) { 379 rotation == display::Display::ROTATE_270) {
319 SetRotationLocked(true); 380 SetRotationLockedInternal(true);
320 } else { 381 } else {
321 LockRotation(display::Display::ROTATE_90, 382 LockRotation(display::Display::ROTATE_90,
322 display::Display::ROTATION_SOURCE_ACTIVE); 383 display::Display::ROTATION_SOURCE_ACTIVE);
323 } 384 }
324 } 385 }
325 } 386 }
326 387
327 void ScreenOrientationController::HandleScreenRotation( 388 void ScreenOrientationController::HandleScreenRotation(
328 const chromeos::AccelerometerReading& lid) { 389 const chromeos::AccelerometerReading& lid) {
329 gfx::Vector3dF lid_flattened(lid.x, lid.y, 0.0f); 390 gfx::Vector3dF lid_flattened(lid.x, lid.y, 0.0f);
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
376 } 437 }
377 } 438 }
378 439
379 void ScreenOrientationController::LoadDisplayRotationProperties() { 440 void ScreenOrientationController::LoadDisplayRotationProperties() {
380 display::DisplayManager* display_manager = 441 display::DisplayManager* display_manager =
381 Shell::GetInstance()->display_manager(); 442 Shell::GetInstance()->display_manager();
382 if (!display_manager->registered_internal_display_rotation_lock()) 443 if (!display_manager->registered_internal_display_rotation_lock())
383 return; 444 return;
384 SetDisplayRotation(display_manager->registered_internal_display_rotation(), 445 SetDisplayRotation(display_manager->registered_internal_display_rotation(),
385 display::Display::ROTATION_SOURCE_ACCELEROMETER); 446 display::Display::ROTATION_SOURCE_ACCELEROMETER);
386 SetRotationLocked(true); 447 SetRotationLockedInternal(true);
387 } 448 }
388 449
389 void ScreenOrientationController::ApplyLockForActiveWindow() { 450 void ScreenOrientationController::ApplyLockForActiveWindow() {
390 WmWindow* active_window = WmShell::Get()->GetActiveWindow(); 451 MruWindowTracker::WindowList mru_windows(
391 if (active_window) { 452 WmShell::Get()->mru_window_tracker()->BuildMruWindowList());
392 for (auto const& windows : locking_windows_) { 453
393 if (windows.first->GetTargetVisibility() && 454 for (WmWindow* window : mru_windows) {
394 active_window->Contains(windows.first)) { 455 if (!window->GetTargetVisibility())
395 LockRotationToOrientation(windows.second); 456 continue;
457 // TODO(oshima): If the application's orientation is landscape
458 // and user_locked_orientation_ is landscape primary or secondary,
459 // it should lock to the specific orientation. (and same for portrait).
460 for (auto const& pair : locking_windows_) {
461 if (pair.first->GetTargetVisibility() && window->Contains(pair.first)) {
462 LockRotationToOrientation(pair.second ==
463 blink::WebScreenOrientationLockAny
464 ? user_locked_orientation_
465 : pair.second);
396 return; 466 return;
397 } 467 }
398 } 468 }
469 // The default orientation for all chrome browser/apps windows is
470 // ANY, so use the user_locked_orientation_;
471 if (window->GetTargetVisibility() &&
472 static_cast<AppType>(window->GetAppType()) != AppType::OTHERS) {
473 LockRotationToOrientation(user_locked_orientation_);
474 return;
475 }
399 } 476 }
400 SetRotationLocked(false); 477 LockRotationToOrientation(user_locked_orientation_);
401 } 478 }
402 479
403 bool ScreenOrientationController::IsRotationAllowedInLockedState( 480 bool ScreenOrientationController::IsRotationAllowedInLockedState(
404 display::Display::Rotation rotation) { 481 display::Display::Rotation rotation) {
405 if (!rotation_locked_) 482 if (!rotation_locked_)
406 return true; 483 return true;
407 484
408 if (!CanRotateInLockedState()) 485 if (!CanRotateInLockedState())
409 return false; 486 return false;
410 487
411 if (natural_orientation_ == rotation_locked_orientation_) { 488 if (natural_orientation_ == rotation_locked_orientation_) {
412 return rotation == display::Display::ROTATE_0 || 489 return rotation == display::Display::ROTATE_0 ||
413 rotation == display::Display::ROTATE_180; 490 rotation == display::Display::ROTATE_180;
414 } else { 491 } else {
415 return rotation == display::Display::ROTATE_90 || 492 return rotation == display::Display::ROTATE_90 ||
416 rotation == display::Display::ROTATE_270; 493 rotation == display::Display::ROTATE_270;
417 } 494 }
418 return false; 495 return false;
419 } 496 }
420 497
421 bool ScreenOrientationController::CanRotateInLockedState() { 498 bool ScreenOrientationController::CanRotateInLockedState() {
422 return rotation_locked_orientation_ == 499 return rotation_locked_orientation_ ==
423 blink::WebScreenOrientationLockLandscape || 500 blink::WebScreenOrientationLockLandscape ||
424 rotation_locked_orientation_ == 501 rotation_locked_orientation_ ==
425 blink::WebScreenOrientationLockPortrait; 502 blink::WebScreenOrientationLockPortrait;
426 } 503 }
427 504
428 } // namespace ash 505 } // namespace ash
OLDNEW
« no previous file with comments | « ash/display/screen_orientation_controller_chromeos.h ('k') | ash/system/chromeos/rotation/tray_rotation_lock.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698