| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2013 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_layout.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 #include <sstream> | |
| 9 | |
| 10 #include "ash/ash_switches.h" | |
| 11 #include "ash/display/display_pref_util.h" | |
| 12 #include "ash/shell.h" | |
| 13 #include "base/logging.h" | |
| 14 #include "base/strings/string_number_conversions.h" | |
| 15 #include "base/strings/stringprintf.h" | |
| 16 #include "base/values.h" | |
| 17 #include "ui/gfx/display.h" | |
| 18 | |
| 19 namespace ash { | |
| 20 namespace { | |
| 21 | |
| 22 // DisplayPlacement Positions | |
| 23 const char kTop[] = "top"; | |
| 24 const char kRight[] = "right"; | |
| 25 const char kBottom[] = "bottom"; | |
| 26 const char kLeft[] = "left"; | |
| 27 const char kUnknown[] = "unknown"; | |
| 28 | |
| 29 // The maximum value for 'offset' in DisplayLayout in case of outliers. Need | |
| 30 // to change this value in case to support even larger displays. | |
| 31 const int kMaxValidOffset = 10000; | |
| 32 | |
| 33 bool IsIdInList(int64_t id, const DisplayIdList& list) { | |
| 34 const auto iter = | |
| 35 std::find_if(list.begin(), list.end(), | |
| 36 [id](int64_t display_id) { return display_id == id; }); | |
| 37 return iter != list.end(); | |
| 38 } | |
| 39 | |
| 40 } // namespace | |
| 41 | |
| 42 //////////////////////////////////////////////////////////////////////////////// | |
| 43 // DisplayPlacement | |
| 44 | |
| 45 DisplayPlacement::DisplayPlacement() | |
| 46 : display_id(gfx::Display::kInvalidDisplayID), | |
| 47 parent_display_id(gfx::Display::kInvalidDisplayID), | |
| 48 position(DisplayPlacement::RIGHT), | |
| 49 offset(0) {} | |
| 50 | |
| 51 DisplayPlacement::DisplayPlacement(Position pos, int offset) | |
| 52 : display_id(gfx::Display::kInvalidDisplayID), | |
| 53 parent_display_id(gfx::Display::kInvalidDisplayID), | |
| 54 position(pos), | |
| 55 offset(offset) { | |
| 56 DCHECK_LE(TOP, position); | |
| 57 DCHECK_GE(LEFT, position); | |
| 58 // Set the default value to |position| in case position is invalid. DCHECKs | |
| 59 // above doesn't stop in Release builds. | |
| 60 if (TOP > position || LEFT < position) | |
| 61 this->position = RIGHT; | |
| 62 | |
| 63 DCHECK_GE(kMaxValidOffset, abs(offset)); | |
| 64 } | |
| 65 | |
| 66 DisplayPlacement::DisplayPlacement(const DisplayPlacement& placement) | |
| 67 : display_id(placement.display_id), | |
| 68 parent_display_id(placement.parent_display_id), | |
| 69 position(placement.position), | |
| 70 offset(placement.offset) {} | |
| 71 | |
| 72 DisplayPlacement& DisplayPlacement::Swap() { | |
| 73 switch (position) { | |
| 74 case TOP: | |
| 75 position = BOTTOM; | |
| 76 break; | |
| 77 case BOTTOM: | |
| 78 position = TOP; | |
| 79 break; | |
| 80 case RIGHT: | |
| 81 position = LEFT; | |
| 82 break; | |
| 83 case LEFT: | |
| 84 position = RIGHT; | |
| 85 break; | |
| 86 } | |
| 87 offset = -offset; | |
| 88 std::swap(display_id, parent_display_id); | |
| 89 return *this; | |
| 90 } | |
| 91 | |
| 92 std::string DisplayPlacement::ToString() const { | |
| 93 std::stringstream s; | |
| 94 if (display_id != gfx::Display::kInvalidDisplayID) | |
| 95 s << "id=" << display_id << ", "; | |
| 96 if (parent_display_id != gfx::Display::kInvalidDisplayID) | |
| 97 s << "parent=" << parent_display_id << ", "; | |
| 98 s << PositionToString(position) << ", "; | |
| 99 s << offset; | |
| 100 return s.str(); | |
| 101 } | |
| 102 | |
| 103 // static | |
| 104 std::string DisplayPlacement::PositionToString(Position position) { | |
| 105 switch (position) { | |
| 106 case TOP: | |
| 107 return kTop; | |
| 108 case RIGHT: | |
| 109 return kRight; | |
| 110 case BOTTOM: | |
| 111 return kBottom; | |
| 112 case LEFT: | |
| 113 return kLeft; | |
| 114 } | |
| 115 return kUnknown; | |
| 116 } | |
| 117 | |
| 118 // static | |
| 119 bool DisplayPlacement::StringToPosition(const base::StringPiece& string, | |
| 120 Position* position) { | |
| 121 if (string == kTop) { | |
| 122 *position = TOP; | |
| 123 return true; | |
| 124 } | |
| 125 | |
| 126 if (string == kRight) { | |
| 127 *position = RIGHT; | |
| 128 return true; | |
| 129 } | |
| 130 | |
| 131 if (string == kBottom) { | |
| 132 *position = BOTTOM; | |
| 133 return true; | |
| 134 } | |
| 135 | |
| 136 if (string == kLeft) { | |
| 137 *position = LEFT; | |
| 138 return true; | |
| 139 } | |
| 140 | |
| 141 LOG(ERROR) << "Invalid position value:" << string; | |
| 142 | |
| 143 return false; | |
| 144 } | |
| 145 | |
| 146 //////////////////////////////////////////////////////////////////////////////// | |
| 147 // DisplayLayout | |
| 148 | |
| 149 DisplayLayout::DisplayLayout() | |
| 150 : mirrored(false), | |
| 151 default_unified(true), | |
| 152 primary_id(gfx::Display::kInvalidDisplayID) {} | |
| 153 | |
| 154 DisplayLayout::~DisplayLayout() {} | |
| 155 | |
| 156 // static | |
| 157 bool DisplayLayout::Validate(const DisplayIdList& list, | |
| 158 const DisplayLayout& layout) { | |
| 159 // The primary display should be in the list. | |
| 160 DCHECK(IsIdInList(layout.primary_id, list)); | |
| 161 | |
| 162 // Unified mode, or mirror mode switched from unified mode, | |
| 163 // may not have the placement yet. | |
| 164 if (layout.placement_list.size() == 0u) | |
| 165 return true; | |
| 166 | |
| 167 bool has_primary_as_parent = false; | |
| 168 int64_t id = 0; | |
| 169 | |
| 170 for (const auto& placement : layout.placement_list) { | |
| 171 // Placements are sorted by display_id. | |
| 172 if (id >= placement.display_id) { | |
| 173 LOG(ERROR) << "PlacementList must be sorted by display_id"; | |
| 174 return false; | |
| 175 } | |
| 176 if (placement.display_id == gfx::Display::kInvalidDisplayID) { | |
| 177 LOG(ERROR) << "display_id is not initialized"; | |
| 178 return false; | |
| 179 } | |
| 180 if (placement.parent_display_id == gfx::Display::kInvalidDisplayID) { | |
| 181 LOG(ERROR) << "display_parent_id is not initialized"; | |
| 182 return false; | |
| 183 } | |
| 184 if (placement.display_id == placement.parent_display_id) { | |
| 185 LOG(ERROR) << "display_id must not be same as parent_display_id"; | |
| 186 return false; | |
| 187 } | |
| 188 if (!IsIdInList(placement.display_id, list)) { | |
| 189 LOG(ERROR) << "display_id is not in the id list:" << placement.ToString(); | |
| 190 return false; | |
| 191 } | |
| 192 | |
| 193 if (!IsIdInList(placement.parent_display_id, list)) { | |
| 194 LOG(ERROR) << "parent_display_id is not in the id list:" | |
| 195 << placement.ToString(); | |
| 196 return false; | |
| 197 } | |
| 198 has_primary_as_parent |= layout.primary_id == placement.parent_display_id; | |
| 199 } | |
| 200 if (!has_primary_as_parent) | |
| 201 LOG(ERROR) << "At least, one placement must have the primary as a parent."; | |
| 202 return has_primary_as_parent; | |
| 203 } | |
| 204 | |
| 205 scoped_ptr<DisplayLayout> DisplayLayout::Copy() const { | |
| 206 scoped_ptr<DisplayLayout> copy(new DisplayLayout); | |
| 207 for (const auto& placement : placement_list) | |
| 208 copy->placement_list.push_back(placement); | |
| 209 copy->mirrored = mirrored; | |
| 210 copy->default_unified = default_unified; | |
| 211 copy->primary_id = primary_id; | |
| 212 return copy; | |
| 213 } | |
| 214 | |
| 215 bool DisplayLayout::HasSamePlacementList(const DisplayLayout& layout) const { | |
| 216 if (placement_list.size() != layout.placement_list.size()) | |
| 217 return false; | |
| 218 for (size_t index = 0; index < placement_list.size(); index++) { | |
| 219 const DisplayPlacement& placement1 = placement_list[index]; | |
| 220 const DisplayPlacement& placement2 = layout.placement_list[index]; | |
| 221 if (placement1.position != placement2.position || | |
| 222 placement1.offset != placement2.offset || | |
| 223 placement1.display_id != placement2.display_id || | |
| 224 placement1.parent_display_id != placement2.parent_display_id) { | |
| 225 return false; | |
| 226 } | |
| 227 } | |
| 228 return true; | |
| 229 } | |
| 230 | |
| 231 std::string DisplayLayout::ToString() const { | |
| 232 std::stringstream s; | |
| 233 s << "primary=" << primary_id; | |
| 234 if (mirrored) | |
| 235 s << ", mirrored"; | |
| 236 if (default_unified) | |
| 237 s << ", default_unified"; | |
| 238 bool added = false; | |
| 239 for (const auto& placement : placement_list) { | |
| 240 s << (added ? "),(" : " [("); | |
| 241 s << placement.ToString(); | |
| 242 added = true; | |
| 243 } | |
| 244 if (added) | |
| 245 s << ")]"; | |
| 246 return s.str(); | |
| 247 } | |
| 248 | |
| 249 DisplayPlacement DisplayLayout::FindPlacementById(int64_t display_id) const { | |
| 250 const auto iter = | |
| 251 std::find_if(placement_list.begin(), placement_list.end(), | |
| 252 [display_id](const DisplayPlacement& placement) { | |
| 253 return placement.display_id == display_id; | |
| 254 }); | |
| 255 return (iter == placement_list.end()) ? DisplayPlacement() | |
| 256 : DisplayPlacement(*iter); | |
| 257 } | |
| 258 | |
| 259 } // namespace ash | |
| OLD | NEW |