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 "ash/display/display_layout.h"
7 #include "ash/ash_switches.h"
8 #include "ash/display/display_manager.h"
9 #include "ash/display/display_pref_util.h"
10 #include "ash/shell.h"
11 #include "base/json/json_value_converter.h"
12 #include "base/logging.h"
13 #include "base/strings/string_number_conversions.h"
14 #include "base/strings/string_piece.h"
15 #include "base/strings/stringprintf.h"
16 #include "base/values.h"
17 #include "ui/gfx/display.h"
22 // The maximum value for 'offset' in DisplayLayout in case of outliers. Need
23 // to change this value in case to support even larger displays.
24 const int kMaxValidOffset
= 10000;
26 // Persistent key names
27 const char kPositionKey
[] = "position";
28 const char kOffsetKey
[] = "offset";
29 const char kMirroredKey
[] = "mirrored";
30 const char kDefaultUnifiedKey
[] = "default_unified";
31 const char kPrimaryIdKey
[] = "primary-id";
33 typedef std::map
<DisplayLayout::Position
, std::string
> PositionToStringMap
;
35 const PositionToStringMap
* GetPositionToStringMap() {
36 static const PositionToStringMap
* map
= CreateToStringMap(
37 DisplayLayout::TOP
, "top",
38 DisplayLayout::BOTTOM
, "bottom",
39 DisplayLayout::RIGHT
, "right",
40 DisplayLayout::LEFT
, "left");
44 bool GetPositionFromString(const base::StringPiece
& position
,
45 DisplayLayout::Position
* field
) {
46 if (ReverseFind(GetPositionToStringMap(), position
, field
))
48 LOG(ERROR
) << "Invalid position value:" << position
;
52 std::string
GetStringFromPosition(DisplayLayout::Position position
) {
53 const PositionToStringMap
* map
= GetPositionToStringMap();
54 PositionToStringMap::const_iterator iter
= map
->find(position
);
55 return iter
!= map
->end() ? iter
->second
: std::string("unknown");
58 bool GetDisplayIdFromString(const base::StringPiece
& position
, int64
* field
) {
59 return base::StringToInt64(position
, field
);
64 ////////////////////////////////////////////////////////////////////////////////
68 DisplayLayout
DisplayLayout::FromInts(int position
, int offsets
) {
69 return DisplayLayout(static_cast<Position
>(position
), offsets
);
72 DisplayLayout::DisplayLayout()
76 #if defined(OS_CHROMEOS)
77 default_unified(true),
79 default_unified(false),
81 primary_id(gfx::Display::kInvalidDisplayID
) {
84 DisplayLayout::DisplayLayout(DisplayLayout::Position position
, int offset
)
88 #if defined(OS_CHROMEOS)
89 default_unified(true),
91 default_unified(false),
93 primary_id(gfx::Display::kInvalidDisplayID
) {
94 DCHECK_LE(TOP
, position
);
95 DCHECK_GE(LEFT
, position
);
97 // Set the default value to |position| in case position is invalid. DCHECKs
98 // above doesn't stop in Release builds.
99 if (TOP
> position
|| LEFT
< position
)
100 this->position
= RIGHT
;
102 DCHECK_GE(kMaxValidOffset
, abs(offset
));
105 DisplayLayout
DisplayLayout::Invert() const {
106 Position inverted_position
= RIGHT
;
109 inverted_position
= BOTTOM
;
112 inverted_position
= TOP
;
115 inverted_position
= LEFT
;
118 inverted_position
= RIGHT
;
121 DisplayLayout ret
= DisplayLayout(inverted_position
, -offset
);
122 ret
.primary_id
= primary_id
;
127 bool DisplayLayout::ConvertFromValue(const base::Value
& value
,
128 DisplayLayout
* layout
) {
129 base::JSONValueConverter
<DisplayLayout
> converter
;
130 return converter
.Convert(value
, layout
);
134 bool DisplayLayout::ConvertToValue(const DisplayLayout
& layout
,
135 base::Value
* value
) {
136 base::DictionaryValue
* dict_value
= NULL
;
137 if (!value
->GetAsDictionary(&dict_value
) || dict_value
== NULL
)
140 const std::string position_str
= GetStringFromPosition(layout
.position
);
141 dict_value
->SetString(kPositionKey
, position_str
);
142 dict_value
->SetInteger(kOffsetKey
, layout
.offset
);
143 dict_value
->SetBoolean(kMirroredKey
, layout
.mirrored
);
144 dict_value
->SetBoolean(kDefaultUnifiedKey
, layout
.default_unified
);
145 dict_value
->SetString(kPrimaryIdKey
, base::Int64ToString(layout
.primary_id
));
149 std::string
DisplayLayout::ToString() const {
150 const std::string position_str
= GetStringFromPosition(position
);
153 Shell::GetInstance()->display_manager()->unified_desktop_enabled();
155 return base::StringPrintf("%s, %d%s%s", position_str
.c_str(), offset
,
156 mirrored
? ", mirrored" : "",
157 unified
? ", unified" : "");
161 void DisplayLayout::RegisterJSONConverter(
162 base::JSONValueConverter
<DisplayLayout
>* converter
) {
163 converter
->RegisterCustomField
<Position
>(
164 kPositionKey
, &DisplayLayout::position
, &GetPositionFromString
);
165 converter
->RegisterIntField(kOffsetKey
, &DisplayLayout::offset
);
166 converter
->RegisterBoolField(kMirroredKey
, &DisplayLayout::mirrored
);
167 converter
->RegisterBoolField(kDefaultUnifiedKey
,
168 &DisplayLayout::default_unified
);
169 converter
->RegisterCustomField
<int64
>(
170 kPrimaryIdKey
, &DisplayLayout::primary_id
, &GetDisplayIdFromString
);