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