1 // Copyright 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.
4
5 #include <stdio.h>
6
7 #include "ash/ash_switches.h"
8 #include "ash/display/display_layout_store.h"
9 #include "base/command_line.h"
10 #include "base/logging.h"
11 #include "ui/gfx/display.h"
12
13 namespace ash {
14
DisplayLayoutStore()15 DisplayLayoutStore::DisplayLayoutStore() {
16 CommandLine* command_line = CommandLine::ForCurrentProcess();
17 if (command_line->HasSwitch(switches::kAshSecondaryDisplayLayout)) {
18 std::string value = command_line->GetSwitchValueASCII(
19 switches::kAshSecondaryDisplayLayout);
20 char layout;
21 int offset = 0;
22 if (sscanf(value.c_str(), "%c,%d", &layout, &offset) == 2) {
23 if (layout == 't')
24 default_display_layout_.position = DisplayLayout::TOP;
25 else if (layout == 'b')
26 default_display_layout_.position = DisplayLayout::BOTTOM;
27 else if (layout == 'r')
28 default_display_layout_.position = DisplayLayout::RIGHT;
29 else if (layout == 'l')
30 default_display_layout_.position = DisplayLayout::LEFT;
31 default_display_layout_.offset = offset;
32 }
33 }
34 }
35
~DisplayLayoutStore()36 DisplayLayoutStore::~DisplayLayoutStore() {
37 }
38
SetDefaultDisplayLayout(const DisplayLayout & layout)39 void DisplayLayoutStore::SetDefaultDisplayLayout(const DisplayLayout& layout) {
40 CommandLine* command_line = CommandLine::ForCurrentProcess();
41 if (!command_line->HasSwitch(switches::kAshSecondaryDisplayLayout))
42 default_display_layout_ = layout;
43 }
44
RegisterLayoutForDisplayIdPair(int64 id1,int64 id2,const DisplayLayout & layout)45 void DisplayLayoutStore::RegisterLayoutForDisplayIdPair(
46 int64 id1,
47 int64 id2,
48 const DisplayLayout& layout) {
49 paired_layouts_[std::make_pair(id1, id2)] = layout;
50 }
51
GetRegisteredDisplayLayout(const DisplayIdPair & pair)52 DisplayLayout DisplayLayoutStore::GetRegisteredDisplayLayout(
53 const DisplayIdPair& pair) {
54 std::map<DisplayIdPair, DisplayLayout>::const_iterator iter =
55 paired_layouts_.find(pair);
56 return
57 iter != paired_layouts_.end() ? iter->second : CreateDisplayLayout(pair);
58 }
59
ComputeDisplayLayoutForDisplayIdPair(const DisplayIdPair & pair)60 DisplayLayout DisplayLayoutStore::ComputeDisplayLayoutForDisplayIdPair(
61 const DisplayIdPair& pair) {
62 DisplayLayout layout = GetRegisteredDisplayLayout(pair);
63 DCHECK_NE(layout.primary_id, gfx::Display::kInvalidDisplayID);
64 // Invert if the primary was swapped. If mirrored, first is always
65 // primary.
66 return (layout.primary_id == gfx::Display::kInvalidDisplayID ||
67 pair.first == layout.primary_id) ? layout : layout.Invert();
68 }
69
UpdateMirrorStatus(const DisplayIdPair & pair,bool mirrored)70 void DisplayLayoutStore::UpdateMirrorStatus(const DisplayIdPair& pair,
71 bool mirrored) {
72 if (paired_layouts_.find(pair) == paired_layouts_.end())
73 CreateDisplayLayout(pair);
74 paired_layouts_[pair].mirrored = mirrored;
75 }
76
UpdatePrimaryDisplayId(const DisplayIdPair & pair,int64 display_id)77 void DisplayLayoutStore::UpdatePrimaryDisplayId(const DisplayIdPair& pair,
78 int64 display_id) {
79 if (paired_layouts_.find(pair) == paired_layouts_.end())
80 CreateDisplayLayout(pair);
81 paired_layouts_[pair].primary_id = display_id;
82 }
83
CreateDisplayLayout(const DisplayIdPair & pair)84 DisplayLayout DisplayLayoutStore::CreateDisplayLayout(
85 const DisplayIdPair& pair) {
86 DisplayLayout layout = default_display_layout_;
87 layout.primary_id = pair.first;
88 paired_layouts_[pair] = layout;
89 return layout;
90 }
91
92 } // namespace ash
93