| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/display_controller.h" | 5 #include "ash/display/display_controller.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "ash/ash_switches.h" | 9 #include "ash/ash_switches.h" |
| 10 #include "ash/display/multi_display_manager.h" | 10 #include "ash/display/multi_display_manager.h" |
| 11 #include "ash/root_window_controller.h" | 11 #include "ash/root_window_controller.h" |
| 12 #include "ash/screen_ash.h" | 12 #include "ash/screen_ash.h" |
| 13 #include "ash/shell.h" | 13 #include "ash/shell.h" |
| 14 #include "ash/wm/coordinate_conversion.h" | 14 #include "ash/wm/coordinate_conversion.h" |
| 15 #include "ash/wm/property_util.h" | 15 #include "ash/wm/property_util.h" |
| 16 #include "ash/wm/window_util.h" | 16 #include "ash/wm/window_util.h" |
| 17 #include "base/command_line.h" | 17 #include "base/command_line.h" |
| 18 #include "base/json/json_value_converter.h" |
| 19 #include "base/string_piece.h" |
| 20 #include "base/values.h" |
| 18 #include "ui/aura/client/screen_position_client.h" | 21 #include "ui/aura/client/screen_position_client.h" |
| 19 #include "ui/aura/env.h" | 22 #include "ui/aura/env.h" |
| 20 #include "ui/aura/root_window.h" | 23 #include "ui/aura/root_window.h" |
| 21 #include "ui/aura/window.h" | 24 #include "ui/aura/window.h" |
| 22 #include "ui/compositor/dip_util.h" | 25 #include "ui/compositor/dip_util.h" |
| 23 #include "ui/gfx/display.h" | 26 #include "ui/gfx/display.h" |
| 24 #include "ui/gfx/screen.h" | 27 #include "ui/gfx/screen.h" |
| 25 | 28 |
| 26 namespace ash { | 29 namespace ash { |
| 27 namespace internal { | 30 namespace internal { |
| 28 namespace { | 31 namespace { |
| 29 | 32 |
| 33 // The maximum value for 'offset' in DisplayLayout in case of outliers. Need |
| 34 // to change this value in case to support even larger displays. |
| 35 const int kMaxValidOffset = 10000; |
| 36 |
| 30 // The number of pixels to overlap between the primary and secondary displays, | 37 // The number of pixels to overlap between the primary and secondary displays, |
| 31 // in case that the offset value is too large. | 38 // in case that the offset value is too large. |
| 32 const int kMinimumOverlapForInvalidOffset = 50; | 39 const int kMinimumOverlapForInvalidOffset = 50; |
| 33 | 40 |
| 41 bool GetPositionFromString(const base::StringPiece& position, |
| 42 DisplayLayout::Position* field) { |
| 43 if (position == "top") { |
| 44 *field = DisplayLayout::TOP; |
| 45 return true; |
| 46 } else if (position == "bottom") { |
| 47 *field = DisplayLayout::BOTTOM; |
| 48 return true; |
| 49 } else if (position == "right") { |
| 50 *field = DisplayLayout::RIGHT; |
| 51 return true; |
| 52 } else if (position == "left") { |
| 53 *field = DisplayLayout::LEFT; |
| 54 return true; |
| 55 } |
| 56 LOG(ERROR) << "Invalid position value: " << position; |
| 57 |
| 58 return false; |
| 59 } |
| 60 |
| 61 } // namespace |
| 62 |
| 63 DisplayLayout::DisplayLayout() |
| 64 : position(RIGHT), offset(0) {} |
| 65 |
| 66 DisplayLayout::DisplayLayout(DisplayLayout::Position position, int offset) |
| 67 : position(position), offset(offset) { |
| 68 DCHECK_LE(TOP, position); |
| 69 DCHECK_GE(LEFT, position); |
| 70 |
| 71 // Set the default value to |position| in case position is invalid. DCHECKs |
| 72 // above doesn't stop in Release builds. |
| 73 if (TOP > position || LEFT < position) |
| 74 this->position = RIGHT; |
| 75 |
| 76 DCHECK_GE(kMaxValidOffset, abs(offset)); |
| 77 } |
| 78 |
| 79 void DisplayLayout::RegisterJSONConverter( |
| 80 base::JSONValueConverter<DisplayLayout>* converter) { |
| 81 converter->RegisterCustomField<Position>( |
| 82 "position", &DisplayLayout::position, &GetPositionFromString); |
| 83 converter->RegisterIntField("offset", &DisplayLayout::offset); |
| 84 } |
| 85 |
| 86 bool DisplayLayout::ConvertToValue(base::DictionaryValue* value) { |
| 87 std::string position_value; |
| 88 switch (position) { |
| 89 case TOP: |
| 90 position_value = "top"; |
| 91 break; |
| 92 case BOTTOM: |
| 93 position_value = "bottom"; |
| 94 break; |
| 95 case RIGHT: |
| 96 position_value = "right"; |
| 97 break; |
| 98 case LEFT: |
| 99 position_value = "left"; |
| 100 break; |
| 101 default: |
| 102 return false; |
| 103 } |
| 104 |
| 105 value->SetString("position", position_value); |
| 106 value->SetInteger("offset", offset); |
| 107 return true; |
| 34 } | 108 } |
| 35 | 109 |
| 36 DisplayController::DisplayController() | 110 DisplayController::DisplayController() |
| 37 : secondary_display_layout_(RIGHT), | 111 : dont_warp_mouse_(false) { |
| 38 secondary_display_offset_(0), | |
| 39 dont_warp_mouse_(false) { | |
| 40 aura::Env::GetInstance()->display_manager()->AddObserver(this); | 112 aura::Env::GetInstance()->display_manager()->AddObserver(this); |
| 41 } | 113 } |
| 42 | 114 |
| 43 DisplayController::~DisplayController() { | 115 DisplayController::~DisplayController() { |
| 44 aura::Env::GetInstance()->display_manager()->RemoveObserver(this); | 116 aura::Env::GetInstance()->display_manager()->RemoveObserver(this); |
| 45 // Delete all root window controllers, which deletes root window | 117 // Delete all root window controllers, which deletes root window |
| 46 // from the last so that the primary root window gets deleted last. | 118 // from the last so that the primary root window gets deleted last. |
| 47 for (std::map<int64, aura::RootWindow*>::const_reverse_iterator it = | 119 for (std::map<int64, aura::RootWindow*>::const_reverse_iterator it = |
| 48 root_windows_.rbegin(); it != root_windows_.rend(); ++it) { | 120 root_windows_.rbegin(); it != root_windows_.rend(); ++it) { |
| 49 internal::RootWindowController* controller = | 121 internal::RootWindowController* controller = |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 121 for (std::map<int64, aura::RootWindow*>::const_iterator it = | 193 for (std::map<int64, aura::RootWindow*>::const_iterator it = |
| 122 root_windows_.begin(); it != root_windows_.end(); ++it) { | 194 root_windows_.begin(); it != root_windows_.end(); ++it) { |
| 123 internal::RootWindowController* controller = | 195 internal::RootWindowController* controller = |
| 124 GetRootWindowController(it->second); | 196 GetRootWindowController(it->second); |
| 125 if (controller) | 197 if (controller) |
| 126 controllers.push_back(controller); | 198 controllers.push_back(controller); |
| 127 } | 199 } |
| 128 return controllers; | 200 return controllers; |
| 129 } | 201 } |
| 130 | 202 |
| 131 void DisplayController::SetSecondaryDisplayLayout( | 203 void DisplayController::SetDefaultDisplayLayout(const DisplayLayout& layout) { |
| 132 SecondaryDisplayLayout layout) { | 204 default_display_layout_ = layout; |
| 133 secondary_display_layout_ = layout; | |
| 134 UpdateDisplayBoundsForLayout(); | 205 UpdateDisplayBoundsForLayout(); |
| 135 } | 206 } |
| 136 | 207 |
| 137 void DisplayController::SetSecondaryDisplayOffset(int offset) { | 208 void DisplayController::SetLayoutForDisplayName(const std::string& name, |
| 138 secondary_display_offset_ = offset; | 209 const DisplayLayout& layout) { |
| 210 secondary_layouts_[name] = layout; |
| 139 UpdateDisplayBoundsForLayout(); | 211 UpdateDisplayBoundsForLayout(); |
| 140 } | 212 } |
| 141 | 213 |
| 214 const DisplayLayout& DisplayController::GetLayoutForDisplayName( |
| 215 const std::string& name) { |
| 216 std::map<std::string, DisplayLayout>::const_iterator it = |
| 217 secondary_layouts_.find(name); |
| 218 |
| 219 if (it != secondary_layouts_.end()) |
| 220 return it->second; |
| 221 return default_display_layout_; |
| 222 } |
| 223 |
| 142 bool DisplayController::WarpMouseCursorIfNecessary( | 224 bool DisplayController::WarpMouseCursorIfNecessary( |
| 143 aura::RootWindow* current_root, | 225 aura::RootWindow* current_root, |
| 144 const gfx::Point& point_in_root) { | 226 const gfx::Point& point_in_root) { |
| 145 if (root_windows_.size() < 2 || dont_warp_mouse_) | 227 if (root_windows_.size() < 2 || dont_warp_mouse_) |
| 146 return false; | 228 return false; |
| 147 const float scale = ui::GetDeviceScaleFactor(current_root->layer()); | 229 const float scale = ui::GetDeviceScaleFactor(current_root->layer()); |
| 148 | 230 |
| 149 // The pointer might be outside the |current_root|. Get the root window where | 231 // The pointer might be outside the |current_root|. Get the root window where |
| 150 // the pointer is currently on. | 232 // the pointer is currently on. |
| 151 std::pair<aura::RootWindow*, gfx::Point> actual_location = | 233 std::pair<aura::RootWindow*, gfx::Point> actual_location = |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 248 | 330 |
| 249 void DisplayController::UpdateDisplayBoundsForLayout() { | 331 void DisplayController::UpdateDisplayBoundsForLayout() { |
| 250 if (!IsExtendedDesktopEnabled() || gfx::Screen::GetNumDisplays() <= 1) { | 332 if (!IsExtendedDesktopEnabled() || gfx::Screen::GetNumDisplays() <= 1) { |
| 251 return; | 333 return; |
| 252 } | 334 } |
| 253 DCHECK_EQ(2, gfx::Screen::GetNumDisplays()); | 335 DCHECK_EQ(2, gfx::Screen::GetNumDisplays()); |
| 254 aura::DisplayManager* display_manager = | 336 aura::DisplayManager* display_manager = |
| 255 aura::Env::GetInstance()->display_manager(); | 337 aura::Env::GetInstance()->display_manager(); |
| 256 const gfx::Rect& primary_bounds = display_manager->GetDisplayAt(0)->bounds(); | 338 const gfx::Rect& primary_bounds = display_manager->GetDisplayAt(0)->bounds(); |
| 257 gfx::Display* secondary_display = display_manager->GetDisplayAt(1); | 339 gfx::Display* secondary_display = display_manager->GetDisplayAt(1); |
| 340 const std::string& secondary_name = display_manager->GetDisplayNameAt(1); |
| 258 const gfx::Rect& secondary_bounds = secondary_display->bounds(); | 341 const gfx::Rect& secondary_bounds = secondary_display->bounds(); |
| 259 gfx::Point new_secondary_origin = primary_bounds.origin(); | 342 gfx::Point new_secondary_origin = primary_bounds.origin(); |
| 260 | 343 |
| 261 // TODO(oshima|mukai): Implement more flexible layout. | 344 const DisplayLayout* layout = &default_display_layout_; |
| 345 std::map<std::string, DisplayLayout>::const_iterator iter = |
| 346 secondary_layouts_.find(secondary_name); |
| 347 if (iter != secondary_layouts_.end()) |
| 348 layout = &iter->second; |
| 349 |
| 350 DisplayLayout::Position position = layout->position; |
| 262 | 351 |
| 263 // Ignore the offset in case the secondary display doesn't share edges with | 352 // Ignore the offset in case the secondary display doesn't share edges with |
| 264 // the primary display. | 353 // the primary display. |
| 265 int offset = secondary_display_offset_; | 354 int offset = layout->offset; |
| 266 if (secondary_display_layout_ == TOP || secondary_display_layout_ == BOTTOM) { | 355 if (position == DisplayLayout::TOP || position == DisplayLayout::BOTTOM) { |
| 267 offset = std::min( | 356 offset = std::min( |
| 268 offset, primary_bounds.width() - kMinimumOverlapForInvalidOffset); | 357 offset, primary_bounds.width() - kMinimumOverlapForInvalidOffset); |
| 269 offset = std::max( | 358 offset = std::max( |
| 270 offset, -secondary_bounds.width() + kMinimumOverlapForInvalidOffset); | 359 offset, -secondary_bounds.width() + kMinimumOverlapForInvalidOffset); |
| 271 } else { | 360 } else { |
| 272 offset = std::min( | 361 offset = std::min( |
| 273 offset, primary_bounds.height() - kMinimumOverlapForInvalidOffset); | 362 offset, primary_bounds.height() - kMinimumOverlapForInvalidOffset); |
| 274 offset = std::max( | 363 offset = std::max( |
| 275 offset, -secondary_bounds.height() + kMinimumOverlapForInvalidOffset); | 364 offset, -secondary_bounds.height() + kMinimumOverlapForInvalidOffset); |
| 276 } | 365 } |
| 277 switch (secondary_display_layout_) { | 366 switch (position) { |
| 278 case TOP: | 367 case DisplayLayout::TOP: |
| 279 new_secondary_origin.Offset(offset, -secondary_bounds.height()); | 368 new_secondary_origin.Offset(offset, -secondary_bounds.height()); |
| 280 break; | 369 break; |
| 281 case RIGHT: | 370 case DisplayLayout::RIGHT: |
| 282 new_secondary_origin.Offset(primary_bounds.width(), offset); | 371 new_secondary_origin.Offset(primary_bounds.width(), offset); |
| 283 break; | 372 break; |
| 284 case BOTTOM: | 373 case DisplayLayout::BOTTOM: |
| 285 new_secondary_origin.Offset(offset, primary_bounds.height()); | 374 new_secondary_origin.Offset(offset, primary_bounds.height()); |
| 286 break; | 375 break; |
| 287 case LEFT: | 376 case DisplayLayout::LEFT: |
| 288 new_secondary_origin.Offset(-secondary_bounds.width(), offset); | 377 new_secondary_origin.Offset(-secondary_bounds.width(), offset); |
| 289 break; | 378 break; |
| 290 } | 379 } |
| 291 gfx::Insets insets = secondary_display->GetWorkAreaInsets(); | 380 gfx::Insets insets = secondary_display->GetWorkAreaInsets(); |
| 292 secondary_display->set_bounds( | 381 secondary_display->set_bounds( |
| 293 gfx::Rect(new_secondary_origin, secondary_bounds.size())); | 382 gfx::Rect(new_secondary_origin, secondary_bounds.size())); |
| 294 secondary_display->UpdateWorkAreaFromInsets(insets); | 383 secondary_display->UpdateWorkAreaFromInsets(insets); |
| 295 } | 384 } |
| 296 | 385 |
| 297 } // namespace internal | 386 } // namespace internal |
| 298 } // namespace ash | 387 } // namespace ash |
| OLD | NEW |