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_pref_util.h"
9 #include "base/json/json_value_converter.h"
10 #include "base/logging.h"
11 #include "base/strings/string_number_conversions.h"
12 #include "base/strings/string_piece.h"
13 #include "base/strings/stringprintf.h"
14 #include "base/values.h"
15 #include "ui/gfx/display.h"
20 // The maximum value for 'offset' in DisplayLayout in case of outliers. Need
21 // to change this value in case to support even larger displays.
22 const int kMaxValidOffset
= 10000;
24 // Persistent key names
25 const char kPositionKey
[] = "position";
26 const char kOffsetKey
[] = "offset";
27 const char kMirroredKey
[] = "mirrored";
28 const char kDefaultUnifiedKey
[] = "default_unified";
29 const char kPrimaryIdKey
[] = "primary-id";
31 typedef std::map
<DisplayLayout::Position
, std::string
> PositionToStringMap
;
33 const PositionToStringMap
* GetPositionToStringMap() {
34 static const PositionToStringMap
* map
= CreateToStringMap(
35 DisplayLayout::TOP
, "top",
36 DisplayLayout::BOTTOM
, "bottom",
37 DisplayLayout::RIGHT
, "right",
38 DisplayLayout::LEFT
, "left");
42 bool GetPositionFromString(const base::StringPiece
& position
,
43 DisplayLayout::Position
* field
) {
44 if (ReverseFind(GetPositionToStringMap(), position
, field
))
46 LOG(ERROR
) << "Invalid position value:" << position
;
50 std::string
GetStringFromPosition(DisplayLayout::Position position
) {
51 const PositionToStringMap
* map
= GetPositionToStringMap();
52 PositionToStringMap::const_iterator iter
= map
->find(position
);
53 return iter
!= map
->end() ? iter
->second
: std::string("unknown");
56 bool GetDisplayIdFromString(const base::StringPiece
& position
, int64
* field
) {
57 return base::StringToInt64(position
, field
);
62 ////////////////////////////////////////////////////////////////////////////////
66 DisplayLayout
DisplayLayout::FromInts(int position
, int offsets
) {
67 return DisplayLayout(static_cast<Position
>(position
), offsets
);
70 DisplayLayout::DisplayLayout()
74 #if defined(OS_CHROMEOS)
75 default_unified(switches::UnifiedDesktopEnabled()),
77 default_unified(false),
79 primary_id(gfx::Display::kInvalidDisplayID
) {
82 DisplayLayout::DisplayLayout(DisplayLayout::Position position
, int offset
)
86 #if defined(OS_CHROMEOS)
87 default_unified(switches::UnifiedDesktopEnabled()),
89 default_unified(false),
91 primary_id(gfx::Display::kInvalidDisplayID
) {
92 DCHECK_LE(TOP
, position
);
93 DCHECK_GE(LEFT
, position
);
95 // Set the default value to |position| in case position is invalid. DCHECKs
96 // above doesn't stop in Release builds.
97 if (TOP
> position
|| LEFT
< position
)
98 this->position
= RIGHT
;
100 DCHECK_GE(kMaxValidOffset
, abs(offset
));
103 DisplayLayout
DisplayLayout::Invert() const {
104 Position inverted_position
= RIGHT
;
107 inverted_position
= BOTTOM
;
110 inverted_position
= TOP
;
113 inverted_position
= LEFT
;
116 inverted_position
= RIGHT
;
119 DisplayLayout ret
= DisplayLayout(inverted_position
, -offset
);
120 ret
.primary_id
= primary_id
;
125 bool DisplayLayout::ConvertFromValue(const base::Value
& value
,
126 DisplayLayout
* layout
) {
127 base::JSONValueConverter
<DisplayLayout
> converter
;
128 return converter
.Convert(value
, layout
);
132 bool DisplayLayout::ConvertToValue(const DisplayLayout
& layout
,
133 base::Value
* value
) {
134 base::DictionaryValue
* dict_value
= NULL
;
135 if (!value
->GetAsDictionary(&dict_value
) || dict_value
== NULL
)
138 const std::string position_str
= GetStringFromPosition(layout
.position
);
139 dict_value
->SetString(kPositionKey
, position_str
);
140 dict_value
->SetInteger(kOffsetKey
, layout
.offset
);
141 dict_value
->SetBoolean(kMirroredKey
, layout
.mirrored
);
142 dict_value
->SetBoolean(kDefaultUnifiedKey
, layout
.default_unified
);
143 dict_value
->SetString(kPrimaryIdKey
, base::Int64ToString(layout
.primary_id
));
147 std::string
DisplayLayout::ToString() const {
148 const std::string position_str
= GetStringFromPosition(position
);
149 return base::StringPrintf("%s, %d%s%s", position_str
.c_str(), offset
,
150 mirrored
? ", mirrored" : "",
151 default_unified
? ", unified" : "");
155 void DisplayLayout::RegisterJSONConverter(
156 base::JSONValueConverter
<DisplayLayout
>* converter
) {
157 converter
->RegisterCustomField
<Position
>(
158 kPositionKey
, &DisplayLayout::position
, &GetPositionFromString
);
159 converter
->RegisterIntField(kOffsetKey
, &DisplayLayout::offset
);
160 converter
->RegisterBoolField(kMirroredKey
, &DisplayLayout::mirrored
);
161 converter
->RegisterBoolField(kDefaultUnifiedKey
,
162 &DisplayLayout::default_unified
);
163 converter
->RegisterCustomField
<int64
>(
164 kPrimaryIdKey
, &DisplayLayout::primary_id
, &GetDisplayIdFromString
);