Enable maximize mode when keyboard is open past 180 degrees.
[chromium-blink-merge.git] / ash / display / display_info.cc
blob1aa0583ec7330ce9b81ce4fc9d31e84b98667a82
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.
5 #include <stdio.h>
6 #include <string>
7 #include <vector>
9 #include "ash/display/display_info.h"
10 #include "base/logging.h"
11 #include "base/strings/string_number_conversions.h"
12 #include "base/strings/string_util.h"
13 #include "base/strings/stringprintf.h"
14 #include "ui/gfx/display.h"
15 #include "ui/gfx/size_conversions.h"
16 #include "ui/gfx/size_f.h"
18 #if defined(OS_WIN)
19 #include "ui/aura/window_tree_host.h"
20 #include "ui/gfx/win/dpi.h"
21 #endif
23 namespace ash {
25 DisplayMode::DisplayMode()
26 : refresh_rate(0.0f), interlaced(false), native(false) {}
28 DisplayMode::DisplayMode(const gfx::Size& size,
29 float refresh_rate,
30 bool interlaced,
31 bool native)
32 : size(size),
33 refresh_rate(refresh_rate),
34 interlaced(interlaced),
35 native(native) {}
37 // satic
38 DisplayInfo DisplayInfo::CreateFromSpec(const std::string& spec) {
39 return CreateFromSpecWithID(spec, gfx::Display::kInvalidDisplayID);
42 // static
43 DisplayInfo DisplayInfo::CreateFromSpecWithID(const std::string& spec,
44 int64 id) {
45 // Default bounds for a display.
46 const int kDefaultHostWindowX = 200;
47 const int kDefaultHostWindowY = 200;
48 const int kDefaultHostWindowWidth = 1366;
49 const int kDefaultHostWindowHeight = 768;
51 // Use larger than max int to catch overflow early.
52 static int64 synthesized_display_id = 2200000000LL;
54 #if defined(OS_WIN)
55 gfx::Rect bounds_in_native(aura::WindowTreeHost::GetNativeScreenSize());
56 #else
57 gfx::Rect bounds_in_native(kDefaultHostWindowX, kDefaultHostWindowY,
58 kDefaultHostWindowWidth, kDefaultHostWindowHeight);
59 #endif
60 std::string main_spec = spec;
62 float ui_scale = 1.0f;
63 std::vector<std::string> parts;
64 if (Tokenize(main_spec, "@", &parts) == 2) {
65 double scale_in_double = 0;
66 if (base::StringToDouble(parts[1], &scale_in_double))
67 ui_scale = scale_in_double;
68 main_spec = parts[0];
71 size_t count = Tokenize(main_spec, "/", &parts);
72 gfx::Display::Rotation rotation(gfx::Display::ROTATE_0);
73 bool has_overscan = false;
74 if (count) {
75 main_spec = parts[0];
76 if (count >= 2) {
77 std::string options = parts[1];
78 for (size_t i = 0; i < options.size(); ++i) {
79 char c = options[i];
80 switch (c) {
81 case 'o':
82 has_overscan = true;
83 break;
84 case 'r': // rotate 90 degrees to 'right'.
85 rotation = gfx::Display::ROTATE_90;
86 break;
87 case 'u': // 180 degrees, 'u'pside-down.
88 rotation = gfx::Display::ROTATE_180;
89 break;
90 case 'l': // rotate 90 degrees to 'left'.
91 rotation = gfx::Display::ROTATE_270;
92 break;
98 int x = 0, y = 0, width, height;
99 float device_scale_factor = 1.0f;
100 if (sscanf(main_spec.c_str(), "%dx%d*%f",
101 &width, &height, &device_scale_factor) >= 2 ||
102 sscanf(main_spec.c_str(), "%d+%d-%dx%d*%f", &x, &y, &width, &height,
103 &device_scale_factor) >= 4) {
104 bounds_in_native.SetRect(x, y, width, height);
105 } else {
106 #if defined(OS_WIN)
107 if (gfx::IsHighDPIEnabled()) {
108 device_scale_factor = gfx::GetModernUIScale();
110 #endif
113 std::vector<DisplayMode> display_modes;
114 if (Tokenize(main_spec, "#", &parts) == 2) {
115 size_t native_mode = 0;
116 int largest_area = -1;
117 float highest_refresh_rate = -1.0f;
118 main_spec = parts[0];
119 std::string resolution_list = parts[1];
120 count = Tokenize(resolution_list, "|", &parts);
121 for (size_t i = 0; i < count; ++i) {
122 std::string resolution = parts[i];
123 int width, height;
124 float refresh_rate = 0.0f;
125 if (sscanf(resolution.c_str(),
126 "%dx%d%%%f",
127 &width,
128 &height,
129 &refresh_rate) >= 2) {
130 if (width * height >= largest_area &&
131 refresh_rate > highest_refresh_rate) {
132 // Use mode with largest area and highest refresh rate as native.
133 largest_area = width * height;
134 highest_refresh_rate = refresh_rate;
135 native_mode = i;
137 display_modes.push_back(
138 DisplayMode(gfx::Size(width, height), refresh_rate, false, false));
141 display_modes[native_mode].native = true;
144 if (id == gfx::Display::kInvalidDisplayID)
145 id = synthesized_display_id++;
146 DisplayInfo display_info(
147 id, base::StringPrintf("Display-%d", static_cast<int>(id)), has_overscan);
148 display_info.set_device_scale_factor(device_scale_factor);
149 display_info.set_rotation(rotation);
150 display_info.set_configured_ui_scale(ui_scale);
151 display_info.SetBounds(bounds_in_native);
152 display_info.set_display_modes(display_modes);
154 // To test the overscan, it creates the default 5% overscan.
155 if (has_overscan) {
156 int width = bounds_in_native.width() / device_scale_factor / 40;
157 int height = bounds_in_native.height() / device_scale_factor / 40;
158 display_info.SetOverscanInsets(gfx::Insets(height, width, height, width));
159 display_info.UpdateDisplaySize();
162 DVLOG(1) << "DisplayInfoFromSpec info=" << display_info.ToString()
163 << ", spec=" << spec;
164 return display_info;
167 DisplayInfo::DisplayInfo()
168 : id_(gfx::Display::kInvalidDisplayID),
169 has_overscan_(false),
170 rotation_(gfx::Display::ROTATE_0),
171 touch_support_(gfx::Display::TOUCH_SUPPORT_UNKNOWN),
172 device_scale_factor_(1.0f),
173 overscan_insets_in_dip_(0, 0, 0, 0),
174 configured_ui_scale_(1.0f),
175 native_(false),
176 color_profile_(ui::COLOR_PROFILE_STANDARD) {
179 DisplayInfo::DisplayInfo(int64 id,
180 const std::string& name,
181 bool has_overscan)
182 : id_(id),
183 name_(name),
184 has_overscan_(has_overscan),
185 rotation_(gfx::Display::ROTATE_0),
186 touch_support_(gfx::Display::TOUCH_SUPPORT_UNKNOWN),
187 device_scale_factor_(1.0f),
188 overscan_insets_in_dip_(0, 0, 0, 0),
189 configured_ui_scale_(1.0f),
190 native_(false),
191 color_profile_(ui::COLOR_PROFILE_STANDARD) {
194 DisplayInfo::~DisplayInfo() {
197 void DisplayInfo::Copy(const DisplayInfo& native_info) {
198 DCHECK(id_ == native_info.id_);
199 name_ = native_info.name_;
200 has_overscan_ = native_info.has_overscan_;
202 DCHECK(!native_info.bounds_in_native_.IsEmpty());
203 bounds_in_native_ = native_info.bounds_in_native_;
204 size_in_pixel_ = native_info.size_in_pixel_;
205 device_scale_factor_ = native_info.device_scale_factor_;
206 display_modes_ = native_info.display_modes_;
207 touch_support_ = native_info.touch_support_;
209 // Copy overscan_insets_in_dip_ if it's not empty. This is for test
210 // cases which use "/o" annotation which sets the overscan inset
211 // to native, and that overscan has to be propagated. This does not
212 // happen on the real environment.
213 if (!native_info.overscan_insets_in_dip_.empty())
214 overscan_insets_in_dip_ = native_info.overscan_insets_in_dip_;
216 // Rotation_ and ui_scale_ color_profile_ are given by preference,
217 // or unit tests. Don't copy if this native_info came from
218 // DisplayChangeObserver.
219 if (!native_info.native()) {
220 rotation_ = native_info.rotation_;
221 configured_ui_scale_ = native_info.configured_ui_scale_;
222 color_profile_ = native_info.color_profile();
225 available_color_profiles_ = native_info.available_color_profiles();
227 // Don't copy insets as it may be given by preference. |rotation_|
228 // is treated as a native so that it can be specified in
229 // |CreateFromSpec|.
232 void DisplayInfo::SetBounds(const gfx::Rect& new_bounds_in_native) {
233 bounds_in_native_ = new_bounds_in_native;
234 size_in_pixel_ = new_bounds_in_native.size();
235 UpdateDisplaySize();
238 float DisplayInfo::GetEffectiveUIScale() const {
239 if (device_scale_factor_ == 2.0f && configured_ui_scale_ == 2.0f)
240 return 1.0f;
241 return configured_ui_scale_;
244 void DisplayInfo::UpdateDisplaySize() {
245 size_in_pixel_ = bounds_in_native_.size();
246 if (!overscan_insets_in_dip_.empty()) {
247 gfx::Insets insets_in_pixel =
248 overscan_insets_in_dip_.Scale(device_scale_factor_);
249 size_in_pixel_.Enlarge(-insets_in_pixel.width(), -insets_in_pixel.height());
250 } else {
251 overscan_insets_in_dip_.Set(0, 0, 0, 0);
254 if (rotation_ == gfx::Display::ROTATE_90 ||
255 rotation_ == gfx::Display::ROTATE_270)
256 size_in_pixel_.SetSize(size_in_pixel_.height(), size_in_pixel_.width());
257 gfx::SizeF size_f(size_in_pixel_);
258 size_f.Scale(GetEffectiveUIScale());
259 size_in_pixel_ = gfx::ToFlooredSize(size_f);
262 void DisplayInfo::SetOverscanInsets(const gfx::Insets& insets_in_dip) {
263 overscan_insets_in_dip_ = insets_in_dip;
266 gfx::Insets DisplayInfo::GetOverscanInsetsInPixel() const {
267 return overscan_insets_in_dip_.Scale(device_scale_factor_);
270 std::string DisplayInfo::ToString() const {
271 int rotation_degree = static_cast<int>(rotation_) * 90;
272 return base::StringPrintf(
273 "DisplayInfo[%lld] native bounds=%s, size=%s, scale=%f, "
274 "overscan=%s, rotation=%d, ui-scale=%f, touchscreen=%s",
275 static_cast<long long int>(id_),
276 bounds_in_native_.ToString().c_str(),
277 size_in_pixel_.ToString().c_str(),
278 device_scale_factor_,
279 overscan_insets_in_dip_.ToString().c_str(),
280 rotation_degree,
281 configured_ui_scale_,
282 touch_support_ == gfx::Display::TOUCH_SUPPORT_AVAILABLE
283 ? "yes"
284 : touch_support_ == gfx::Display::TOUCH_SUPPORT_UNAVAILABLE
285 ? "no"
286 : "unknown");
289 std::string DisplayInfo::ToFullString() const {
290 std::string display_modes_str;
291 std::vector<DisplayMode>::const_iterator iter = display_modes_.begin();
292 for (; iter != display_modes_.end(); ++iter) {
293 if (!display_modes_str.empty())
294 display_modes_str += ",";
295 base::StringAppendF(&display_modes_str,
296 "(%dx%d@%f%c%s)",
297 iter->size.width(),
298 iter->size.height(),
299 iter->refresh_rate,
300 iter->interlaced ? 'I' : 'P',
301 iter->native ? "(N)" : "");
303 return ToString() + ", display_modes==" + display_modes_str;
306 void DisplayInfo::SetColorProfile(ui::ColorCalibrationProfile profile) {
307 if (IsColorProfileAvailable(profile))
308 color_profile_ = profile;
311 bool DisplayInfo::IsColorProfileAvailable(
312 ui::ColorCalibrationProfile profile) const {
313 return std::find(available_color_profiles_.begin(),
314 available_color_profiles_.end(),
315 profile) != available_color_profiles_.end();
318 } // namespace ash