display_controller.cc revision effb81e5f8246d0db0270817048dc992db66e9fb
1// Copyright (c) 2012 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/display/display_controller.h" 6 7#include <algorithm> 8#include <cmath> 9#include <map> 10 11#include "ash/ash_switches.h" 12#include "ash/display/cursor_window_controller.h" 13#include "ash/display/display_layout_store.h" 14#include "ash/display/display_manager.h" 15#include "ash/display/mirror_window_controller.h" 16#include "ash/display/root_window_transformers.h" 17#include "ash/display/virtual_keyboard_window_controller.h" 18#include "ash/host/window_tree_host_factory.h" 19#include "ash/root_window_controller.h" 20#include "ash/root_window_settings.h" 21#include "ash/screen_util.h" 22#include "ash/shell.h" 23#include "ash/shell_delegate.h" 24#include "ash/wm/coordinate_conversion.h" 25#include "base/command_line.h" 26#include "base/strings/stringprintf.h" 27#include "ui/aura/client/capture_client.h" 28#include "ui/aura/client/focus_client.h" 29#include "ui/aura/client/screen_position_client.h" 30#include "ui/aura/root_window_transformer.h" 31#include "ui/aura/window.h" 32#include "ui/aura/window_event_dispatcher.h" 33#include "ui/aura/window_property.h" 34#include "ui/aura/window_tracker.h" 35#include "ui/compositor/compositor.h" 36#include "ui/compositor/compositor_vsync_manager.h" 37#include "ui/gfx/display.h" 38#include "ui/gfx/screen.h" 39#include "ui/wm/public/activation_client.h" 40 41#if defined(OS_CHROMEOS) 42#include "ash/display/output_configurator_animation.h" 43#include "base/sys_info.h" 44#include "base/time/time.h" 45#if defined(USE_X11) 46#include "ui/base/x/x11_util.h" 47#include "ui/gfx/x/x11_types.h" 48 49// Including this at the bottom to avoid other 50// potential conflict with chrome headers. 51#include <X11/extensions/Xrandr.h> 52#undef RootWindow 53#endif // defined(USE_X11) 54#endif // defined(OS_CHROMEOS) 55 56namespace ash { 57namespace { 58 59// Primary display stored in global object as it can be 60// accessed after Shell is deleted. A separate display instance is created 61// during the shutdown instead of always keeping two display instances 62// (one here and another one in display_manager) in sync, which is error prone. 63int64 primary_display_id = gfx::Display::kInvalidDisplayID; 64 65// Specifies how long the display change should have been disabled 66// after each display change operations. 67// |kCycleDisplayThrottleTimeoutMs| is set to be longer to avoid 68// changing the settings while the system is still configurating 69// displays. It will be overriden by |kAfterDisplayChangeThrottleTimeoutMs| 70// when the display change happens, so the actual timeout is much shorter. 71const int64 kAfterDisplayChangeThrottleTimeoutMs = 500; 72const int64 kCycleDisplayThrottleTimeoutMs = 4000; 73const int64 kSwapDisplayThrottleTimeoutMs = 500; 74 75internal::DisplayManager* GetDisplayManager() { 76 return Shell::GetInstance()->display_manager(); 77} 78 79void SetDisplayPropertiesOnHost(aura::WindowTreeHost* host, 80 const gfx::Display& display) { 81 internal::DisplayInfo info = 82 GetDisplayManager()->GetDisplayInfo(display.id()); 83#if defined(OS_CHROMEOS) && defined(USE_X11) 84 // Native window property (Atom in X11) that specifies the display's 85 // rotation, scale factor and if it's internal display. They are 86 // read and used by touchpad/mouse driver directly on X (contact 87 // adlr@ for more details on touchpad/mouse driver side). The value 88 // of the rotation is one of 0 (normal), 1 (90 degrees clockwise), 2 89 // (180 degree) or 3 (270 degrees clockwise). The value of the 90 // scale factor is in percent (100, 140, 200 etc). 91 const char kRotationProp[] = "_CHROME_DISPLAY_ROTATION"; 92 const char kScaleFactorProp[] = "_CHROME_DISPLAY_SCALE_FACTOR"; 93 const char kInternalProp[] = "_CHROME_DISPLAY_INTERNAL"; 94 const char kCARDINAL[] = "CARDINAL"; 95 int xrandr_rotation = RR_Rotate_0; 96 switch (info.rotation()) { 97 case gfx::Display::ROTATE_0: 98 xrandr_rotation = RR_Rotate_0; 99 break; 100 case gfx::Display::ROTATE_90: 101 xrandr_rotation = RR_Rotate_90; 102 break; 103 case gfx::Display::ROTATE_180: 104 xrandr_rotation = RR_Rotate_180; 105 break; 106 case gfx::Display::ROTATE_270: 107 xrandr_rotation = RR_Rotate_270; 108 break; 109 } 110 111 int internal = display.IsInternal() ? 1 : 0; 112 gfx::AcceleratedWidget xwindow = host->GetAcceleratedWidget(); 113 ui::SetIntProperty(xwindow, kInternalProp, kCARDINAL, internal); 114 ui::SetIntProperty(xwindow, kRotationProp, kCARDINAL, xrandr_rotation); 115 ui::SetIntProperty(xwindow, 116 kScaleFactorProp, 117 kCARDINAL, 118 100 * display.device_scale_factor()); 119#endif 120 scoped_ptr<aura::RootWindowTransformer> transformer( 121 internal::CreateRootWindowTransformerForDisplay(host->window(), 122 display)); 123 host->SetRootWindowTransformer(transformer.Pass()); 124 125 internal::DisplayMode mode; 126 if (GetDisplayManager()->GetSelectedModeForDisplayId(display.id(), &mode) && 127 mode.refresh_rate > 0.0f) { 128 host->compositor()->vsync_manager()->SetAuthoritativeVSyncInterval( 129 base::TimeDelta::FromMicroseconds( 130 base::Time::kMicrosecondsPerSecond / mode.refresh_rate)); 131 } 132} 133 134} // namespace 135 136namespace internal { 137 138// A utility class to store/restore focused/active window 139// when the display configuration has changed. 140class FocusActivationStore { 141 public: 142 FocusActivationStore() 143 : activation_client_(NULL), 144 capture_client_(NULL), 145 focus_client_(NULL), 146 focused_(NULL), 147 active_(NULL) { 148 } 149 150 void Store(bool clear_focus) { 151 if (!activation_client_) { 152 aura::Window* root = Shell::GetPrimaryRootWindow(); 153 activation_client_ = aura::client::GetActivationClient(root); 154 capture_client_ = aura::client::GetCaptureClient(root); 155 focus_client_ = aura::client::GetFocusClient(root); 156 } 157 focused_ = focus_client_->GetFocusedWindow(); 158 if (focused_) 159 tracker_.Add(focused_); 160 active_ = activation_client_->GetActiveWindow(); 161 if (active_ && focused_ != active_) 162 tracker_.Add(active_); 163 164 // Deactivate the window to close menu / bubble windows. 165 if (clear_focus) 166 activation_client_->DeactivateWindow(active_); 167 168 // Release capture if any. 169 capture_client_->SetCapture(NULL); 170 // Clear the focused window if any. This is necessary because a 171 // window may be deleted when losing focus (fullscreen flash for 172 // example). If the focused window is still alive after move, it'll 173 // be re-focused below. 174 if (clear_focus) 175 focus_client_->FocusWindow(NULL); 176 } 177 178 void Restore() { 179 // Restore focused or active window if it's still alive. 180 if (focused_ && tracker_.Contains(focused_)) { 181 focus_client_->FocusWindow(focused_); 182 } else if (active_ && tracker_.Contains(active_)) { 183 activation_client_->ActivateWindow(active_); 184 } 185 if (focused_) 186 tracker_.Remove(focused_); 187 if (active_) 188 tracker_.Remove(active_); 189 focused_ = NULL; 190 active_ = NULL; 191 } 192 193 private: 194 aura::client::ActivationClient* activation_client_; 195 aura::client::CaptureClient* capture_client_; 196 aura::client::FocusClient* focus_client_; 197 aura::WindowTracker tracker_; 198 aura::Window* focused_; 199 aura::Window* active_; 200 201 DISALLOW_COPY_AND_ASSIGN(FocusActivationStore); 202}; 203 204} // namespace internal 205 206//////////////////////////////////////////////////////////////////////////////// 207// DisplayChangeLimiter 208 209DisplayController::DisplayChangeLimiter::DisplayChangeLimiter() 210 : throttle_timeout_(base::Time::Now()) { 211} 212 213void DisplayController::DisplayChangeLimiter::SetThrottleTimeout( 214 int64 throttle_ms) { 215 throttle_timeout_ = 216 base::Time::Now() + base::TimeDelta::FromMilliseconds(throttle_ms); 217} 218 219bool DisplayController::DisplayChangeLimiter::IsThrottled() const { 220 return base::Time::Now() < throttle_timeout_; 221} 222 223//////////////////////////////////////////////////////////////////////////////// 224// DisplayController 225 226DisplayController::DisplayController() 227 : primary_root_window_for_replace_(NULL), 228 focus_activation_store_(new internal::FocusActivationStore()), 229 cursor_window_controller_(new internal::CursorWindowController()), 230 mirror_window_controller_(new internal::MirrorWindowController()) { 231#if defined(OS_CHROMEOS) 232 if (base::SysInfo::IsRunningOnChromeOS()) 233 limiter_.reset(new DisplayChangeLimiter); 234#endif 235 // Reset primary display to make sure that tests don't use 236 // stale display info from previous tests. 237 primary_display_id = gfx::Display::kInvalidDisplayID; 238} 239 240DisplayController::~DisplayController() { 241} 242 243void DisplayController::Start() { 244 // Created here so that Shell has finished being created. Adds itself 245 // as a ShellObserver. 246 virtual_keyboard_window_controller_.reset( 247 new internal::VirtualKeyboardWindowController); 248 Shell::GetScreen()->AddObserver(this); 249 Shell::GetInstance()->display_manager()->set_delegate(this); 250 251 FOR_EACH_OBSERVER(Observer, observers_, OnDisplaysInitialized()); 252} 253 254void DisplayController::Shutdown() { 255 // Unset the display manager's delegate here because 256 // DisplayManager outlives DisplayController. 257 Shell::GetInstance()->display_manager()->set_delegate(NULL); 258 259 cursor_window_controller_.reset(); 260 mirror_window_controller_.reset(); 261 virtual_keyboard_window_controller_.reset(); 262 263 Shell::GetScreen()->RemoveObserver(this); 264 // Delete all root window controllers, which deletes root window 265 // from the last so that the primary root window gets deleted last. 266 for (std::map<int64, aura::Window*>::const_reverse_iterator it = 267 root_windows_.rbegin(); it != root_windows_.rend(); ++it) { 268 internal::RootWindowController* controller = 269 internal::GetRootWindowController(it->second); 270 DCHECK(controller); 271 delete controller; 272 } 273} 274 275void DisplayController::InitPrimaryDisplay() { 276 const gfx::Display& primary_candidate = 277 GetDisplayManager()->GetPrimaryDisplayCandidate(); 278 primary_display_id = primary_candidate.id(); 279 AddWindowTreeHostForDisplay(primary_candidate); 280} 281 282void DisplayController::InitSecondaryDisplays() { 283 internal::DisplayManager* display_manager = GetDisplayManager(); 284 for (size_t i = 0; i < display_manager->GetNumDisplays(); ++i) { 285 const gfx::Display& display = display_manager->GetDisplayAt(i); 286 if (primary_display_id != display.id()) { 287 aura::WindowTreeHost* host = AddWindowTreeHostForDisplay(display); 288 internal::RootWindowController::CreateForSecondaryDisplay(host); 289 } 290 } 291 UpdateHostWindowNames(); 292} 293 294void DisplayController::AddObserver(Observer* observer) { 295 observers_.AddObserver(observer); 296} 297 298void DisplayController::RemoveObserver(Observer* observer) { 299 observers_.RemoveObserver(observer); 300} 301 302// static 303int64 DisplayController::GetPrimaryDisplayId() { 304 return primary_display_id; 305} 306 307aura::Window* DisplayController::GetPrimaryRootWindow() { 308 DCHECK(!root_windows_.empty()); 309 return root_windows_[primary_display_id]; 310} 311 312aura::Window* DisplayController::GetRootWindowForDisplayId(int64 id) { 313 return root_windows_[id]; 314} 315 316void DisplayController::CloseChildWindows() { 317 for (std::map<int64, aura::Window*>::const_iterator it = 318 root_windows_.begin(); it != root_windows_.end(); ++it) { 319 aura::Window* root_window = it->second; 320 internal::RootWindowController* controller = 321 internal::GetRootWindowController(root_window); 322 if (controller) { 323 controller->CloseChildWindows(); 324 } else { 325 while (!root_window->children().empty()) { 326 aura::Window* child = root_window->children()[0]; 327 delete child; 328 } 329 } 330 } 331} 332 333aura::Window::Windows DisplayController::GetAllRootWindows() { 334 aura::Window::Windows windows; 335 for (std::map<int64, aura::Window*>::const_iterator it = 336 root_windows_.begin(); it != root_windows_.end(); ++it) { 337 DCHECK(it->second); 338 if (internal::GetRootWindowController(it->second)) 339 windows.push_back(it->second); 340 } 341 return windows; 342} 343 344gfx::Insets DisplayController::GetOverscanInsets(int64 display_id) const { 345 return GetDisplayManager()->GetOverscanInsets(display_id); 346} 347 348void DisplayController::SetOverscanInsets(int64 display_id, 349 const gfx::Insets& insets_in_dip) { 350 GetDisplayManager()->SetOverscanInsets(display_id, insets_in_dip); 351} 352 353std::vector<internal::RootWindowController*> 354DisplayController::GetAllRootWindowControllers() { 355 std::vector<internal::RootWindowController*> controllers; 356 for (std::map<int64, aura::Window*>::const_iterator it = 357 root_windows_.begin(); it != root_windows_.end(); ++it) { 358 internal::RootWindowController* controller = 359 internal::GetRootWindowController(it->second); 360 if (controller) 361 controllers.push_back(controller); 362 } 363 return controllers; 364} 365 366void DisplayController::ToggleMirrorMode() { 367 internal::DisplayManager* display_manager = GetDisplayManager(); 368 if (display_manager->num_connected_displays() <= 1) 369 return; 370 371 if (limiter_) { 372 if (limiter_->IsThrottled()) 373 return; 374 limiter_->SetThrottleTimeout(kCycleDisplayThrottleTimeoutMs); 375 } 376#if defined(OS_CHROMEOS) 377 Shell* shell = Shell::GetInstance(); 378 internal::OutputConfiguratorAnimation* animation = 379 shell->output_configurator_animation(); 380 animation->StartFadeOutAnimation(base::Bind( 381 base::IgnoreResult(&internal::DisplayManager::SetMirrorMode), 382 base::Unretained(display_manager), 383 !display_manager->IsMirrored())); 384#endif 385} 386 387void DisplayController::SwapPrimaryDisplay() { 388 if (limiter_) { 389 if (limiter_->IsThrottled()) 390 return; 391 limiter_->SetThrottleTimeout(kSwapDisplayThrottleTimeoutMs); 392 } 393 394 if (Shell::GetScreen()->GetNumDisplays() > 1) { 395#if defined(OS_CHROMEOS) 396 internal::OutputConfiguratorAnimation* animation = 397 Shell::GetInstance()->output_configurator_animation(); 398 if (animation) { 399 animation->StartFadeOutAnimation(base::Bind( 400 &DisplayController::OnFadeOutForSwapDisplayFinished, 401 base::Unretained(this))); 402 } else { 403 SetPrimaryDisplay(ScreenUtil::GetSecondaryDisplay()); 404 } 405#else 406 SetPrimaryDisplay(ScreenUtil::GetSecondaryDisplay()); 407#endif 408 } 409} 410 411void DisplayController::SetPrimaryDisplayId(int64 id) { 412 DCHECK_NE(gfx::Display::kInvalidDisplayID, id); 413 if (id == gfx::Display::kInvalidDisplayID || primary_display_id == id) 414 return; 415 416 const gfx::Display& display = GetDisplayManager()->GetDisplayForId(id); 417 if (display.is_valid()) 418 SetPrimaryDisplay(display); 419} 420 421void DisplayController::SetPrimaryDisplay( 422 const gfx::Display& new_primary_display) { 423 internal::DisplayManager* display_manager = GetDisplayManager(); 424 DCHECK(new_primary_display.is_valid()); 425 DCHECK(display_manager->IsActiveDisplay(new_primary_display)); 426 427 if (!new_primary_display.is_valid() || 428 !display_manager->IsActiveDisplay(new_primary_display)) { 429 LOG(ERROR) << "Invalid or non-existent display is requested:" 430 << new_primary_display.ToString(); 431 return; 432 } 433 434 if (primary_display_id == new_primary_display.id() || 435 root_windows_.size() < 2) { 436 return; 437 } 438 439 aura::Window* non_primary_root = root_windows_[new_primary_display.id()]; 440 LOG_IF(ERROR, !non_primary_root) 441 << "Unknown display is requested in SetPrimaryDisplay: id=" 442 << new_primary_display.id(); 443 if (!non_primary_root) 444 return; 445 446 gfx::Display old_primary_display = Shell::GetScreen()->GetPrimaryDisplay(); 447 448 // Swap root windows between current and new primary display. 449 aura::Window* primary_root = root_windows_[primary_display_id]; 450 DCHECK(primary_root); 451 DCHECK_NE(primary_root, non_primary_root); 452 453 root_windows_[new_primary_display.id()] = primary_root; 454 internal::GetRootWindowSettings(primary_root)->display_id = 455 new_primary_display.id(); 456 457 root_windows_[old_primary_display.id()] = non_primary_root; 458 internal::GetRootWindowSettings(non_primary_root)->display_id = 459 old_primary_display.id(); 460 461 primary_display_id = new_primary_display.id(); 462 GetDisplayManager()->layout_store()->UpdatePrimaryDisplayId( 463 display_manager->GetCurrentDisplayIdPair(), primary_display_id); 464 465 UpdateWorkAreaOfDisplayNearestWindow( 466 primary_root, old_primary_display.GetWorkAreaInsets()); 467 UpdateWorkAreaOfDisplayNearestWindow( 468 non_primary_root, new_primary_display.GetWorkAreaInsets()); 469 470 // Update the dispay manager with new display info. 471 std::vector<internal::DisplayInfo> display_info_list; 472 display_info_list.push_back(display_manager->GetDisplayInfo( 473 primary_display_id)); 474 display_info_list.push_back(display_manager->GetDisplayInfo( 475 ScreenUtil::GetSecondaryDisplay().id())); 476 GetDisplayManager()->set_force_bounds_changed(true); 477 GetDisplayManager()->UpdateDisplays(display_info_list); 478 GetDisplayManager()->set_force_bounds_changed(false); 479} 480 481void DisplayController::EnsurePointerInDisplays() { 482 // If the mouse is currently on a display in native location, 483 // use the same native location. Otherwise find the display closest 484 // to the current cursor location in screen coordinates. 485 486 gfx::Point point_in_screen = Shell::GetScreen()->GetCursorScreenPoint(); 487 gfx::Point target_location_in_native; 488 int64 closest_distance_squared = -1; 489 internal::DisplayManager* display_manager = GetDisplayManager(); 490 491 aura::Window* dst_root_window = NULL; 492 for (size_t i = 0; i < display_manager->GetNumDisplays(); ++i) { 493 const gfx::Display& display = display_manager->GetDisplayAt(i); 494 const internal::DisplayInfo display_info = 495 display_manager->GetDisplayInfo(display.id()); 496 aura::Window* root_window = GetRootWindowForDisplayId(display.id()); 497 if (display_info.bounds_in_native().Contains( 498 cursor_location_in_native_coords_for_restore_)) { 499 dst_root_window = root_window; 500 target_location_in_native = cursor_location_in_native_coords_for_restore_; 501 break; 502 } 503 gfx::Point center = display.bounds().CenterPoint(); 504 // Use the distance squared from the center of the dislay. This is not 505 // exactly "closest" display, but good enough to pick one 506 // appropriate (and there are at most two displays). 507 // We don't care about actual distance, only relative to other displays, so 508 // using the LengthSquared() is cheaper than Length(). 509 510 int64 distance_squared = (center - point_in_screen).LengthSquared(); 511 if (closest_distance_squared < 0 || 512 closest_distance_squared > distance_squared) { 513 aura::Window* root_window = GetRootWindowForDisplayId(display.id()); 514 aura::client::ScreenPositionClient* client = 515 aura::client::GetScreenPositionClient(root_window); 516 client->ConvertPointFromScreen(root_window, ¢er); 517 root_window->GetHost()->ConvertPointToNativeScreen(¢er); 518 dst_root_window = root_window; 519 target_location_in_native = center; 520 closest_distance_squared = distance_squared; 521 } 522 } 523 dst_root_window->GetHost()->ConvertPointFromNativeScreen( 524 &target_location_in_native); 525 dst_root_window->MoveCursorTo(target_location_in_native); 526} 527 528bool DisplayController::UpdateWorkAreaOfDisplayNearestWindow( 529 const aura::Window* window, 530 const gfx::Insets& insets) { 531 const aura::Window* root_window = window->GetRootWindow(); 532 int64 id = internal::GetRootWindowSettings(root_window)->display_id; 533 // if id is |kInvaildDisplayID|, it's being deleted. 534 DCHECK(id != gfx::Display::kInvalidDisplayID); 535 return GetDisplayManager()->UpdateWorkAreaOfDisplay(id, insets); 536} 537 538void DisplayController::OnDisplayBoundsChanged(const gfx::Display& display) { 539 const internal::DisplayInfo& display_info = 540 GetDisplayManager()->GetDisplayInfo(display.id()); 541 DCHECK(!display_info.bounds_in_native().IsEmpty()); 542 aura::WindowTreeHost* host = root_windows_[display.id()]->GetHost(); 543 host->SetBounds(display_info.bounds_in_native()); 544 SetDisplayPropertiesOnHost(host, display); 545} 546 547void DisplayController::OnDisplayAdded(const gfx::Display& display) { 548 if (primary_root_window_for_replace_) { 549 DCHECK(root_windows_.empty()); 550 primary_display_id = display.id(); 551 root_windows_[display.id()] = primary_root_window_for_replace_; 552 internal::GetRootWindowSettings(primary_root_window_for_replace_)-> 553 display_id = display.id(); 554 primary_root_window_for_replace_ = NULL; 555 const internal::DisplayInfo& display_info = 556 GetDisplayManager()->GetDisplayInfo(display.id()); 557 aura::WindowTreeHost* host = root_windows_[display.id()]->GetHost(); 558 host->SetBounds(display_info.bounds_in_native()); 559 SetDisplayPropertiesOnHost(host, display); 560 } else { 561 if (primary_display_id == gfx::Display::kInvalidDisplayID) 562 primary_display_id = display.id(); 563 DCHECK(!root_windows_.empty()); 564 aura::WindowTreeHost* host = AddWindowTreeHostForDisplay(display); 565 internal::RootWindowController::CreateForSecondaryDisplay(host); 566 } 567} 568 569void DisplayController::OnDisplayRemoved(const gfx::Display& display) { 570 aura::Window* root_to_delete = root_windows_[display.id()]; 571 DCHECK(root_to_delete) << display.ToString(); 572 573 // Display for root window will be deleted when the Primary RootWindow 574 // is deleted by the Shell. 575 root_windows_.erase(display.id()); 576 577 // When the primary root window's display is removed, move the primary 578 // root to the other display. 579 if (primary_display_id == display.id()) { 580 // Temporarily store the primary root window in 581 // |primary_root_window_for_replace_| when replacing the display. 582 if (root_windows_.size() == 0) { 583 primary_display_id = gfx::Display::kInvalidDisplayID; 584 primary_root_window_for_replace_ = root_to_delete; 585 return; 586 } 587 DCHECK_EQ(1U, root_windows_.size()); 588 primary_display_id = ScreenUtil::GetSecondaryDisplay().id(); 589 aura::Window* primary_root = root_to_delete; 590 591 // Delete the other root instead. 592 root_to_delete = root_windows_[primary_display_id]; 593 internal::GetRootWindowSettings(root_to_delete)->display_id = display.id(); 594 595 // Setup primary root. 596 root_windows_[primary_display_id] = primary_root; 597 internal::GetRootWindowSettings(primary_root)->display_id = 598 primary_display_id; 599 600 OnDisplayBoundsChanged( 601 GetDisplayManager()->GetDisplayForId(primary_display_id)); 602 } 603 internal::RootWindowController* controller = 604 internal::GetRootWindowController(root_to_delete); 605 DCHECK(controller); 606 controller->MoveWindowsTo(GetPrimaryRootWindow()); 607 // Delete most of root window related objects, but don't delete 608 // root window itself yet because the stack may be using it. 609 controller->Shutdown(); 610 base::MessageLoop::current()->DeleteSoon(FROM_HERE, controller); 611} 612 613void DisplayController::OnHostResized(const aura::WindowTreeHost* host) { 614 gfx::Display display = Shell::GetScreen()->GetDisplayNearestWindow( 615 const_cast<aura::Window*>(host->window())); 616 617 internal::DisplayManager* display_manager = GetDisplayManager(); 618 if (display_manager->UpdateDisplayBounds(display.id(), host->GetBounds())) { 619 mirror_window_controller_->UpdateWindow(); 620 cursor_window_controller_->UpdateContainer(); 621 } 622} 623 624void DisplayController::CreateOrUpdateNonDesktopDisplay( 625 const internal::DisplayInfo& info) { 626 switch (GetDisplayManager()->second_display_mode()) { 627 case internal::DisplayManager::MIRRORING: 628 mirror_window_controller_->UpdateWindow(info); 629 cursor_window_controller_->UpdateContainer(); 630 virtual_keyboard_window_controller_->Close(); 631 break; 632 case internal::DisplayManager::VIRTUAL_KEYBOARD: 633 mirror_window_controller_->Close(); 634 cursor_window_controller_->UpdateContainer(); 635 virtual_keyboard_window_controller_->UpdateWindow(info); 636 break; 637 case internal::DisplayManager::EXTENDED: 638 NOTREACHED(); 639 } 640} 641 642void DisplayController::CloseNonDesktopDisplay() { 643 mirror_window_controller_->Close(); 644 cursor_window_controller_->UpdateContainer(); 645 virtual_keyboard_window_controller_->Close(); 646} 647 648void DisplayController::PreDisplayConfigurationChange(bool clear_focus) { 649 FOR_EACH_OBSERVER(Observer, observers_, OnDisplayConfigurationChanging()); 650 focus_activation_store_->Store(clear_focus); 651 gfx::Screen* screen = Shell::GetScreen(); 652 gfx::Point point_in_screen = screen->GetCursorScreenPoint(); 653 gfx::Display display = screen->GetDisplayNearestPoint(point_in_screen); 654 aura::Window* root_window = GetRootWindowForDisplayId(display.id()); 655 656 aura::client::ScreenPositionClient* client = 657 aura::client::GetScreenPositionClient(root_window); 658 client->ConvertPointFromScreen(root_window, &point_in_screen); 659 root_window->GetHost()->ConvertPointToNativeScreen(&point_in_screen); 660 cursor_location_in_native_coords_for_restore_ = point_in_screen; 661} 662 663void DisplayController::PostDisplayConfigurationChange() { 664 if (limiter_) 665 limiter_->SetThrottleTimeout(kAfterDisplayChangeThrottleTimeoutMs); 666 667 focus_activation_store_->Restore(); 668 669 internal::DisplayManager* display_manager = GetDisplayManager(); 670 internal::DisplayLayoutStore* layout_store = display_manager->layout_store(); 671 if (display_manager->num_connected_displays() > 1) { 672 DisplayIdPair pair = display_manager->GetCurrentDisplayIdPair(); 673 layout_store->UpdateMirrorStatus(pair, display_manager->IsMirrored()); 674 DisplayLayout layout = layout_store->GetRegisteredDisplayLayout(pair); 675 676 if (Shell::GetScreen()->GetNumDisplays() > 1 ) { 677 int64 primary_id = layout.primary_id; 678 SetPrimaryDisplayId( 679 primary_id == gfx::Display::kInvalidDisplayID ? 680 pair.first : primary_id); 681 // Update the primary_id in case the above call is 682 // ignored. Happens when a) default layout's primary id 683 // doesn't exist, or b) the primary_id has already been 684 // set to the same and didn't update it. 685 layout_store->UpdatePrimaryDisplayId( 686 pair, Shell::GetScreen()->GetPrimaryDisplay().id()); 687 } 688 } 689 FOR_EACH_OBSERVER(Observer, observers_, OnDisplayConfigurationChanged()); 690 UpdateHostWindowNames(); 691 EnsurePointerInDisplays(); 692} 693 694aura::WindowTreeHost* DisplayController::AddWindowTreeHostForDisplay( 695 const gfx::Display& display) { 696 static int host_count = 0; 697 const internal::DisplayInfo& display_info = 698 GetDisplayManager()->GetDisplayInfo(display.id()); 699 const gfx::Rect& bounds_in_native = display_info.bounds_in_native(); 700 aura::WindowTreeHost* host = 701 Shell::GetInstance()->window_tree_host_factory()->CreateWindowTreeHost( 702 bounds_in_native); 703 host->window()->SetName(base::StringPrintf("RootWindow-%d", host_count++)); 704 host->compositor()->SetBackgroundColor(SK_ColorBLACK); 705 // No need to remove our observer observer because the DisplayController 706 // outlives the host. 707 host->AddObserver(this); 708 internal::InitRootWindowSettings(host->window())->display_id = display.id(); 709 host->InitHost(); 710 711 root_windows_[display.id()] = host->window(); 712 SetDisplayPropertiesOnHost(host, display); 713 714#if defined(OS_CHROMEOS) 715 static bool force_constrain_pointer_to_root = 716 CommandLine::ForCurrentProcess()->HasSwitch( 717 switches::kAshConstrainPointerToRoot); 718 if (base::SysInfo::IsRunningOnChromeOS() || force_constrain_pointer_to_root) 719 host->ConfineCursorToRootWindow(); 720#endif 721 return host; 722} 723 724void DisplayController::OnFadeOutForSwapDisplayFinished() { 725#if defined(OS_CHROMEOS) 726 SetPrimaryDisplay(ScreenUtil::GetSecondaryDisplay()); 727 Shell::GetInstance()->output_configurator_animation()->StartFadeInAnimation(); 728#endif 729} 730 731void DisplayController::UpdateHostWindowNames() { 732#if defined(USE_X11) 733 // crbug.com/120229 - set the window title for the primary dislpay 734 // to "aura_root_0" so gtalk can find the primary root window to broadcast. 735 // TODO(jhorwich) Remove this once Chrome supports window-based broadcasting. 736 aura::Window* primary = Shell::GetPrimaryRootWindow(); 737 aura::Window::Windows root_windows = Shell::GetAllRootWindows(); 738 for (size_t i = 0; i < root_windows.size(); ++i) { 739 std::string name = 740 root_windows[i] == primary ? "aura_root_0" : "aura_root_x"; 741 gfx::AcceleratedWidget xwindow = 742 root_windows[i]->GetHost()->GetAcceleratedWidget(); 743 XStoreName(gfx::GetXDisplay(), xwindow, name.c_str()); 744 } 745#endif 746} 747 748} // namespace ash 749