summaryrefslogtreecommitdiffstats
path: root/ash/display/display_layout.cc
blob: 1897d76d94a6270e6b506fb45158dd50badf1416 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ash/display/display_layout.h"

#include <algorithm>
#include <sstream>

#include "ash/ash_switches.h"
#include "ash/display/display_pref_util.h"
#include "ash/shell.h"
#include "base/logging.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/stringprintf.h"
#include "base/values.h"
#include "ui/gfx/display.h"

namespace ash {
namespace  {

// DisplayPlacement Positions
const char kTop[] = "top";
const char kRight[] = "right";
const char kBottom[] = "bottom";
const char kLeft[] = "left";
const char kUnknown[] = "unknown";

// The maximum value for 'offset' in DisplayLayout in case of outliers.  Need
// to change this value in case to support even larger displays.
const int kMaxValidOffset = 10000;

bool IsIdInList(int64_t id, const DisplayIdList& list) {
  const auto iter =
      std::find_if(list.begin(), list.end(),
                   [id](int64_t display_id) { return display_id == id; });
  return iter != list.end();
}

}  // namespace

////////////////////////////////////////////////////////////////////////////////
// DisplayPlacement

DisplayPlacement::DisplayPlacement()
    : display_id(gfx::Display::kInvalidDisplayID),
      parent_display_id(gfx::Display::kInvalidDisplayID),
      position(DisplayPlacement::RIGHT),
      offset(0) {}

DisplayPlacement::DisplayPlacement(const DisplayPlacement& placement)
    : display_id(placement.display_id),
      parent_display_id(placement.parent_display_id),
      position(placement.position),
      offset(placement.offset) {}

DisplayPlacement::DisplayPlacement(Position pos, int offset)
    : display_id(gfx::Display::kInvalidDisplayID),
      parent_display_id(gfx::Display::kInvalidDisplayID),
      position(pos),
      offset(offset) {
  DCHECK_LE(TOP, position);
  DCHECK_GE(LEFT, position);
  // Set the default value to |position| in case position is invalid.  DCHECKs
  // above doesn't stop in Release builds.
  if (TOP > position || LEFT < position)
    this->position = RIGHT;

  DCHECK_GE(kMaxValidOffset, abs(offset));
}

DisplayPlacement& DisplayPlacement::Swap() {
  switch (position) {
    case TOP:
      position = BOTTOM;
      break;
    case BOTTOM:
      position = TOP;
      break;
    case RIGHT:
      position = LEFT;
      break;
    case LEFT:
      position = RIGHT;
      break;
  }
  offset = -offset;
  std::swap(display_id, parent_display_id);
  return *this;
}

std::string DisplayPlacement::ToString() const {
  std::stringstream s;
  if (display_id != gfx::Display::kInvalidDisplayID)
    s << "id=" << display_id << ", ";
  if (parent_display_id != gfx::Display::kInvalidDisplayID)
    s << "parent=" << parent_display_id << ", ";
  s << PositionToString(position) << ", ";
  s << offset;
  return s.str();
}

// static
std::string DisplayPlacement::PositionToString(Position position) {
  switch (position) {
    case TOP:
      return kTop;
    case RIGHT:
      return kRight;
    case BOTTOM:
      return kBottom;
    case LEFT:
      return kLeft;
  }
  return kUnknown;
}

// static
bool DisplayPlacement::StringToPosition(const base::StringPiece& string,
                                        Position* position) {
  if (string == kTop) {
    *position = TOP;
    return true;
  }

  if (string == kRight) {
    *position = RIGHT;
    return true;
  }

  if (string == kBottom) {
    *position = BOTTOM;
    return true;
  }

  if (string == kLeft) {
    *position = LEFT;
    return true;
  }

  LOG(ERROR) << "Invalid position value:" << string;

  return false;
}

////////////////////////////////////////////////////////////////////////////////
// DisplayLayout

DisplayLayout::DisplayLayout()
    : mirrored(false),
      default_unified(true),
      primary_id(gfx::Display::kInvalidDisplayID) {}

DisplayLayout::~DisplayLayout() {}

// static
bool DisplayLayout::Validate(const DisplayIdList& list,
                             const DisplayLayout& layout) {
  // The primary display should be in the list.
  DCHECK(IsIdInList(layout.primary_id, list));

  // Unified mode, or mirror mode switched from unified mode,
  // may not have the placement yet.
  if (layout.placement_list.size() == 0u)
    return true;

  bool has_primary_as_parent = false;
  int64_t id = 0;

  for (const auto* placement : layout.placement_list) {
    // Placements are sorted by display_id.
    if (id >= placement->display_id) {
      LOG(ERROR) << "PlacementList must be sorted by display_id";
      return false;
    }
    if (placement->display_id == gfx::Display::kInvalidDisplayID) {
      LOG(ERROR) << "display_id is not initialized";
      return false;
    }
    if (placement->parent_display_id == gfx::Display::kInvalidDisplayID) {
      LOG(ERROR) << "display_parent_id is not initialized";
      return false;
    }
    if (placement->display_id == placement->parent_display_id) {
      LOG(ERROR) << "display_id must not be same as parent_display_id";
      return false;
    }
    if (!IsIdInList(placement->display_id, list)) {
      LOG(ERROR) << "display_id is not in the id list:"
                 << placement->ToString();
      return false;
    }

    if (!IsIdInList(placement->parent_display_id, list)) {
      LOG(ERROR) << "parent_display_id is not in the id list:"
                 << placement->ToString();
      return false;
    }
    has_primary_as_parent |= layout.primary_id == placement->parent_display_id;
  }
  if (!has_primary_as_parent)
    LOG(ERROR) << "At least, one placement must have the primary as a parent.";
  return has_primary_as_parent;
}

scoped_ptr<DisplayLayout> DisplayLayout::Copy() const {
  scoped_ptr<DisplayLayout> copy(new DisplayLayout);
  for (auto placement : placement_list)
    copy->placement_list.push_back(new DisplayPlacement(*placement));
  copy->mirrored = mirrored;
  copy->default_unified = default_unified;
  copy->primary_id = primary_id;
  return copy;
}

bool DisplayLayout::HasSamePlacementList(const DisplayLayout& layout) const {
  if (placement_list.size() != layout.placement_list.size())
    return false;
  for (size_t index = 0; index < placement_list.size(); index++) {
    const DisplayPlacement& placement1 = *placement_list[index];
    const DisplayPlacement& placement2 = *layout.placement_list[index];
    if (placement1.position != placement2.position ||
        placement1.offset != placement2.offset ||
        placement1.display_id != placement2.display_id ||
        placement1.parent_display_id != placement2.parent_display_id) {
      return false;
    }
  }
  return true;
}

std::string DisplayLayout::ToString() const {
  std::stringstream s;
  s << "primary=" << primary_id;
  if (mirrored)
    s << ", mirrored";
  if (default_unified)
    s << ", default_unified";
  bool added = false;
  for (const auto* placement : placement_list) {
    s << (added ? "),(" : " [(");
    s << placement->ToString();
    added = true;
  }
  if (added)
    s << ")]";
  return s.str();
}

const DisplayPlacement* DisplayLayout::FindPlacementById(
    int64_t display_id) const {
  const auto iter =
      std::find_if(placement_list.begin(), placement_list.end(),
                   [display_id](const DisplayPlacement* placement) {
                     return placement->display_id == display_id;
                   });
  return (iter == placement_list.end()) ? nullptr : *iter;
}

}  // namespace ash