OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "ui/display/fake_display_delegate.h" | 5 #include "ui/display/fake_display_delegate.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
| 8 #include <utility> |
8 | 9 |
9 #include "base/command_line.h" | 10 #include "base/command_line.h" |
10 #include "base/hash.h" | 11 #include "base/hash.h" |
11 #include "base/logging.h" | 12 #include "base/logging.h" |
12 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
13 #include "base/strings/string_split.h" | 14 #include "base/strings/string_split.h" |
14 #include "base/strings/stringprintf.h" | 15 #include "base/strings/stringprintf.h" |
15 #include "ui/display/display.h" | 16 #include "ui/display/display.h" |
16 #include "ui/display/display_switches.h" | 17 #include "ui/display/display_switches.h" |
17 #include "ui/display/types/native_display_observer.h" | 18 #include "ui/display/types/native_display_observer.h" |
18 #include "ui/display/util/display_util.h" | 19 #include "ui/display/util/display_util.h" |
19 | 20 |
20 namespace display { | 21 namespace display { |
21 | 22 |
22 namespace { | 23 namespace { |
23 | 24 |
24 // The EDID specification marks the top bit of the manufacturer id as reserved. | 25 // The EDID specification marks the top bit of the manufacturer id as reserved. |
25 const uint16_t kReservedManufacturerID = 1 << 15; | 26 const uint16_t kReservedManufacturerID = 1 << 15; |
26 | 27 |
27 // A random product name hash. | 28 // A random product name hash. |
28 const uint32_t kProductCodeHash = base::Hash("Very Generic Display"); | 29 const uint32_t kProductCodeHash = base::Hash("Very Generic Display"); |
29 | 30 |
30 } // namespace | 31 } // namespace |
31 | 32 |
32 FakeDisplayDelegate::FakeDisplayDelegate() {} | 33 FakeDisplayDelegate::FakeDisplayDelegate() {} |
33 | 34 |
34 FakeDisplayDelegate::~FakeDisplayDelegate() {} | 35 FakeDisplayDelegate::~FakeDisplayDelegate() {} |
35 | 36 |
36 int64_t FakeDisplayDelegate::AddDisplay(const gfx::Size& display_size) { | 37 int64_t FakeDisplayDelegate::AddDisplay(const gfx::Size& display_size) { |
| 38 DCHECK(!display_size.IsEmpty()); |
| 39 |
37 if (next_display_id_ == 0xFF) { | 40 if (next_display_id_ == 0xFF) { |
38 LOG(ERROR) << "Exceeded display id limit"; | 41 LOG(ERROR) << "Exceeded display id limit"; |
39 return display::Display::kInvalidDisplayID; | 42 return Display::kInvalidDisplayID; |
40 } | 43 } |
41 | 44 |
42 int64_t id = GenerateDisplayID(kReservedManufacturerID, kProductCodeHash, | 45 int64_t id = GenerateDisplayID(kReservedManufacturerID, kProductCodeHash, |
43 ++next_display_id_); | 46 ++next_display_id_); |
| 47 |
| 48 FakeDisplaySnapshot::Builder builder; |
| 49 builder.SetId(id).SetNativeMode(display_size); |
| 50 builder.SetName(base::StringPrintf("Fake Display %d", next_display_id_)); |
| 51 |
44 // Add the first display as internal. | 52 // Add the first display as internal. |
45 ui::DisplayConnectionType type = displays_.empty() | 53 if (displays_.empty()) |
46 ? ui::DISPLAY_CONNECTION_TYPE_INTERNAL | 54 builder.SetType(ui::DISPLAY_CONNECTION_TYPE_INTERNAL); |
47 : ui::DISPLAY_CONNECTION_TYPE_UNKNOWN; | |
48 std::string name = base::StringPrintf("Fake Display %d", next_display_id_); | |
49 | 55 |
50 displays_.push_back( | 56 return AddDisplay(builder.Build()) ? id : Display::kInvalidDisplayID; |
51 base::MakeUnique<FakeDisplaySnapshot>(id, display_size, type, name)); | 57 } |
| 58 |
| 59 bool FakeDisplayDelegate::AddDisplay( |
| 60 std::unique_ptr<ui::DisplaySnapshot> display) { |
| 61 DCHECK(display); |
| 62 |
| 63 int64_t display_id = display->display_id(); |
| 64 // Check there is no existing display with the same id. |
| 65 for (auto& existing_display : displays_) { |
| 66 if (existing_display->display_id() == display_id) { |
| 67 LOG(ERROR) << "Display with id " << display_id << " already exists"; |
| 68 return false; |
| 69 } |
| 70 } |
| 71 |
| 72 DVLOG(1) << "Added display " << display->ToString(); |
| 73 displays_.push_back(std::move(display)); |
52 OnConfigurationChanged(); | 74 OnConfigurationChanged(); |
53 | 75 |
54 return id; | 76 return true; |
55 } | 77 } |
56 | 78 |
57 bool FakeDisplayDelegate::RemoveDisplay(int64_t display_id) { | 79 bool FakeDisplayDelegate::RemoveDisplay(int64_t display_id) { |
58 // Find display snapshot with matching id and remove it. | 80 // Find display snapshot with matching id and remove it. |
59 for (auto iter = displays_.begin(); iter != displays_.end(); ++iter) { | 81 for (auto iter = displays_.begin(); iter != displays_.end(); ++iter) { |
60 if ((*iter)->display_id() == display_id) { | 82 if ((*iter)->display_id() == display_id) { |
| 83 DVLOG(1) << "Removed display " << (*iter)->ToString(); |
61 displays_.erase(iter); | 84 displays_.erase(iter); |
62 OnConfigurationChanged(); | 85 OnConfigurationChanged(); |
63 return true; | 86 return true; |
64 } | 87 } |
65 } | 88 } |
66 | 89 |
67 return false; | 90 return false; |
68 } | 91 } |
69 | 92 |
70 void FakeDisplayDelegate::Initialize() { | 93 void FakeDisplayDelegate::Initialize() { |
71 DCHECK(!initialized_); | 94 DCHECK(!initialized_); |
72 InitFromCommandLine(); | |
73 | 95 |
74 // If no command line flags are provided then initialize a default display. | 96 // If no command line flags are provided then initialize a default display. |
75 if (displays_.empty()) | 97 if (!InitFromCommandLine()) |
76 AddDisplay(gfx::Size(1024, 768)); | 98 AddDisplay(gfx::Size(1024, 768)); |
77 | 99 |
78 initialized_ = true; | 100 initialized_ = true; |
79 } | 101 } |
80 | 102 |
81 void FakeDisplayDelegate::GrabServer() {} | 103 void FakeDisplayDelegate::GrabServer() {} |
82 | 104 |
83 void FakeDisplayDelegate::UngrabServer() {} | 105 void FakeDisplayDelegate::UngrabServer() {} |
84 | 106 |
85 void FakeDisplayDelegate::TakeDisplayControl( | 107 void FakeDisplayDelegate::TakeDisplayControl( |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
155 } | 177 } |
156 | 178 |
157 void FakeDisplayDelegate::RemoveObserver(ui::NativeDisplayObserver* observer) { | 179 void FakeDisplayDelegate::RemoveObserver(ui::NativeDisplayObserver* observer) { |
158 observers_.RemoveObserver(observer); | 180 observers_.RemoveObserver(observer); |
159 } | 181 } |
160 | 182 |
161 FakeDisplayController* FakeDisplayDelegate::GetFakeDisplayController() { | 183 FakeDisplayController* FakeDisplayDelegate::GetFakeDisplayController() { |
162 return static_cast<FakeDisplayController*>(this); | 184 return static_cast<FakeDisplayController*>(this); |
163 } | 185 } |
164 | 186 |
165 void FakeDisplayDelegate::InitFromCommandLine() { | 187 bool FakeDisplayDelegate::InitFromCommandLine() { |
166 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); | 188 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
167 if (!command_line->HasSwitch(switches::kScreenConfig)) | 189 if (!command_line->HasSwitch(switches::kScreenConfig)) |
168 return; | 190 return false; |
169 | 191 |
170 const std::string command_string = | 192 const std::string command_string = |
171 command_line->GetSwitchValueASCII(switches::kScreenConfig); | 193 command_line->GetSwitchValueASCII(switches::kScreenConfig); |
172 | 194 |
| 195 // Start without any displays. |
| 196 if (command_string == "none") |
| 197 return true; |
| 198 |
173 // Split on commas and parse each display string. | 199 // Split on commas and parse each display string. |
174 for (std::string part : base::SplitString( | 200 for (std::string part : base::SplitString( |
175 command_string, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL)) { | 201 command_string, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL)) { |
176 int width = 0; | 202 std::unique_ptr<ui::DisplaySnapshot> snapshot = |
177 int height = 0; | 203 CreateSnapshotFromSpec(part); |
178 if (sscanf(part.c_str(), "%dx%d", &width, &height) >= 2) { | 204 if (snapshot) { |
179 gfx::Size display_size(width, height); | 205 AddDisplay(std::move(snapshot)); |
180 AddDisplay(display_size); | |
181 } else { | 206 } else { |
182 LOG(ERROR) << "Failed to parse display \"" << part << "\""; | 207 LOG(ERROR) << "Failed to parse display \"" << part << "\""; |
183 } | 208 } |
184 } | 209 } |
| 210 |
| 211 return true; |
| 212 } |
| 213 |
| 214 std::unique_ptr<ui::DisplaySnapshot> |
| 215 FakeDisplayDelegate::CreateSnapshotFromSpec(const std::string& spec) { |
| 216 int width = 0; |
| 217 int height = 0; |
| 218 int dpi = 0; |
| 219 |
| 220 // Width and height are required but DPI is optional. |
| 221 int found = sscanf(spec.c_str(), "%dx%d^%d", &width, &height, &dpi); |
| 222 if (found < 2) |
| 223 return nullptr; |
| 224 |
| 225 int64_t id = GenerateDisplayID(kReservedManufacturerID, kProductCodeHash, |
| 226 ++next_display_id_); |
| 227 |
| 228 FakeDisplaySnapshot::Builder builder; |
| 229 builder.SetId(id).SetNativeMode(gfx::Size(width, height)); |
| 230 builder.SetName(base::StringPrintf("Fake Display %d", next_display_id_)); |
| 231 |
| 232 if (found >= 3) |
| 233 builder.SetDPI(dpi); |
| 234 |
| 235 // TODO(kylechar): Add type to the spec string. |
| 236 if (displays_.empty()) |
| 237 builder.SetType(ui::DISPLAY_CONNECTION_TYPE_INTERNAL); |
| 238 |
| 239 return builder.Build(); |
185 } | 240 } |
186 | 241 |
187 void FakeDisplayDelegate::OnConfigurationChanged() { | 242 void FakeDisplayDelegate::OnConfigurationChanged() { |
188 if (!initialized_) | 243 if (!initialized_) |
189 return; | 244 return; |
190 | 245 |
191 FOR_EACH_OBSERVER(ui::NativeDisplayObserver, observers_, | 246 FOR_EACH_OBSERVER(ui::NativeDisplayObserver, observers_, |
192 OnConfigurationChanged()); | 247 OnConfigurationChanged()); |
193 } | 248 } |
194 | 249 |
195 } // namespace display | 250 } // namespace display |
OLD | NEW |