OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ash/monitor/multi_monitor_manager.h" | 5 #include "ash/monitor/multi_monitor_manager.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
11 #include "base/stl_util.h" | 11 #include "base/stl_util.h" |
12 #include "base/string_split.h" | 12 #include "base/string_split.h" |
13 #include "ui/aura/aura_switches.h" | 13 #include "ui/aura/aura_switches.h" |
14 #include "ui/aura/env.h" | 14 #include "ui/aura/env.h" |
15 #include "ui/aura/monitor.h" | 15 #include "ui/aura/monitor_aura.h" |
16 #include "ui/aura/root_window.h" | 16 #include "ui/aura/root_window.h" |
17 #include "ui/aura/root_window_host.h" | 17 #include "ui/aura/root_window_host.h" |
| 18 #include "ui/aura/window_property.h" |
18 #include "ui/gfx/rect.h" | 19 #include "ui/gfx/rect.h" |
19 #include "ui/aura/window_property.h" | |
20 | 20 |
21 DECLARE_WINDOW_PROPERTY_TYPE(aura::Monitor*); | 21 DECLARE_WINDOW_PROPERTY_TYPE(aura::MonitorAura*); |
22 | 22 |
23 namespace ash { | 23 namespace ash { |
24 namespace internal { | 24 namespace internal { |
25 namespace { | 25 namespace { |
26 | 26 |
27 aura::Monitor* Copy(aura::Monitor* m) { | 27 aura::MonitorAura* Copy(aura::MonitorAura* m) { |
28 aura::Monitor* monitor = new aura::Monitor; | 28 aura::MonitorAura* monitor = new aura::MonitorAura; |
29 monitor->set_bounds(m->bounds()); | 29 monitor->set_bounds(m->bounds()); |
30 return monitor; | 30 return monitor; |
31 } | 31 } |
32 | 32 |
33 } // namespace | 33 } // namespace |
34 | 34 |
35 DEFINE_WINDOW_PROPERTY_KEY(aura::Monitor*, kMonitorKey, NULL); | 35 DEFINE_WINDOW_PROPERTY_KEY(aura::MonitorAura*, kMonitorKey, NULL); |
36 | 36 |
| 37 using aura::MonitorAura; |
| 38 using aura::RootWindow; |
| 39 using aura::Window; |
37 using std::string; | 40 using std::string; |
38 using std::vector; | 41 using std::vector; |
39 using aura::Monitor; | |
40 using aura::RootWindow; | |
41 using aura::Window; | |
42 | 42 |
43 MultiMonitorManager::MultiMonitorManager() { | 43 MultiMonitorManager::MultiMonitorManager() { |
44 Init(); | 44 Init(); |
45 } | 45 } |
46 | 46 |
47 MultiMonitorManager::~MultiMonitorManager() { | 47 MultiMonitorManager::~MultiMonitorManager() { |
48 STLDeleteContainerPointers(monitors_.begin(), monitors_.end()); | 48 STLDeleteContainerPointers(monitors_.begin(), monitors_.end()); |
49 } | 49 } |
50 | 50 |
51 // static | 51 // static |
52 void MultiMonitorManager::AddRemoveMonitor() { | 52 void MultiMonitorManager::AddRemoveMonitor() { |
53 MultiMonitorManager* manager = static_cast<MultiMonitorManager*>( | 53 MultiMonitorManager* manager = static_cast<MultiMonitorManager*>( |
54 aura::Env::GetInstance()->monitor_manager()); | 54 aura::Env::GetInstance()->monitor_manager()); |
55 manager->AddRemoveMonitorImpl(); | 55 manager->AddRemoveMonitorImpl(); |
56 } | 56 } |
57 | 57 |
58 void MultiMonitorManager::CycleMonitor() { | 58 void MultiMonitorManager::CycleMonitor() { |
59 MultiMonitorManager* manager = static_cast<MultiMonitorManager*>( | 59 MultiMonitorManager* manager = static_cast<MultiMonitorManager*>( |
60 aura::Env::GetInstance()->monitor_manager()); | 60 aura::Env::GetInstance()->monitor_manager()); |
61 manager->CycleMonitorImpl(); | 61 manager->CycleMonitorImpl(); |
62 } | 62 } |
63 | 63 |
64 void MultiMonitorManager::OnNativeMonitorsChanged( | 64 void MultiMonitorManager::OnNativeMonitorsChanged( |
65 const std::vector<const aura::Monitor*>& new_monitors) { | 65 const std::vector<const aura::MonitorAura*>& new_monitors) { |
66 size_t min = std::min(monitors_.size(), new_monitors.size()); | 66 size_t min = std::min(monitors_.size(), new_monitors.size()); |
67 | 67 |
68 // For m19, we only care about 1st monitor as primary, and | 68 // For m19, we only care about 1st monitor as primary, and |
69 // don't differentiate the rest of monitors as all secondary | 69 // don't differentiate the rest of monitors as all secondary |
70 // monitors have the same content. | 70 // monitors have the same content. |
71 // TODO(oshima): Fix this so that we can differentiate outputs | 71 // TODO(oshima): Fix this so that we can differentiate outputs |
72 // and keep a content on one monitor stays on the same monitor | 72 // and keep a content on one monitor stays on the same monitor |
73 // when a monitor is added or removed. | 73 // when a monitor is added or removed. |
74 for (size_t i = 0; i < min; ++i) { | 74 for (size_t i = 0; i < min; ++i) { |
75 Monitor* current_monitor = monitors_[i]; | 75 aura::MonitorAura* current_monitor = monitors_[i]; |
76 const Monitor* new_monitor = new_monitors[i]; | 76 const aura::MonitorAura* new_monitor = new_monitors[i]; |
77 if (current_monitor->bounds() != new_monitor->bounds()) { | 77 if (current_monitor->bounds() != new_monitor->bounds()) { |
78 current_monitor->set_bounds(new_monitor->bounds()); | 78 current_monitor->set_bounds(new_monitor->bounds()); |
79 NotifyBoundsChanged(current_monitor); | 79 NotifyBoundsChanged(current_monitor); |
80 } | 80 } |
81 } | 81 } |
82 | 82 |
83 if (monitors_.size() < new_monitors.size()) { | 83 if (monitors_.size() < new_monitors.size()) { |
84 // New monitors added | 84 // New monitors added |
85 for (size_t i = min; i < new_monitors.size(); ++i) { | 85 for (size_t i = min; i < new_monitors.size(); ++i) { |
86 Monitor* monitor = new Monitor(); | 86 aura::MonitorAura* monitor = new aura::MonitorAura(); |
87 monitor->set_bounds(new_monitors[i]->bounds()); | 87 monitor->set_bounds(new_monitors[i]->bounds()); |
88 monitors_.push_back(monitor); | 88 monitors_.push_back(monitor); |
89 NotifyMonitorAdded(monitor); | 89 NotifyMonitorAdded(monitor); |
90 } | 90 } |
91 } else { | 91 } else { |
92 // Monitors are removed. We keep the monitor for the primary | 92 // Monitors are removed. We keep the monitor for the primary |
93 // monitor (at index 0) because it needs the monitor information | 93 // monitor (at index 0) because it needs the monitor information |
94 // even if it doesn't exit. | 94 // even if it doesn't exit. |
95 while (monitors_.size() > new_monitors.size() && monitors_.size() > 1) { | 95 while (monitors_.size() > new_monitors.size() && monitors_.size() > 1) { |
96 Monitor* monitor = monitors_.back(); | 96 aura::MonitorAura* monitor = monitors_.back(); |
97 NotifyMonitorRemoved(monitor); | 97 NotifyMonitorRemoved(monitor); |
98 monitors_.erase(std::find(monitors_.begin(), monitors_.end(), monitor)); | 98 monitors_.erase(std::find(monitors_.begin(), monitors_.end(), monitor)); |
99 delete monitor; | 99 delete monitor; |
100 } | 100 } |
101 } | 101 } |
102 } | 102 } |
103 | 103 |
104 RootWindow* MultiMonitorManager::CreateRootWindowForMonitor( | 104 RootWindow* MultiMonitorManager::CreateRootWindowForMonitor( |
105 Monitor* monitor) { | 105 aura::MonitorAura* monitor) { |
106 RootWindow* root_window = new RootWindow(monitor->bounds()); | 106 RootWindow* root_window = new RootWindow(monitor->bounds()); |
107 // No need to remove RootWindowObserver because | 107 // No need to remove RootWindowObserver because |
108 // the MonitorManager object outlives RootWindow objects. | 108 // the MonitorManager object outlives RootWindow objects. |
109 root_window->AddRootWindowObserver(this); | 109 root_window->AddRootWindowObserver(this); |
110 root_window->SetProperty(kMonitorKey, monitor); | 110 root_window->SetProperty(kMonitorKey, monitor); |
111 return root_window; | 111 return root_window; |
112 } | 112 } |
113 | 113 |
114 const Monitor* MultiMonitorManager::GetMonitorNearestWindow( | 114 aura::MonitorAura* MultiMonitorManager::GetMonitorAt(size_t index) { |
| 115 return index < monitors_.size() ? monitors_[index] : NULL; |
| 116 } |
| 117 |
| 118 size_t MultiMonitorManager::GetNumMonitors() const { |
| 119 return monitors_.size(); |
| 120 } |
| 121 |
| 122 const aura::MonitorAura* MultiMonitorManager::GetMonitorNearestWindow( |
115 const Window* window) const { | 123 const Window* window) const { |
116 if (!window) { | 124 if (!window) { |
117 MultiMonitorManager* manager = const_cast<MultiMonitorManager*>(this); | 125 MultiMonitorManager* manager = const_cast<MultiMonitorManager*>(this); |
118 return manager->GetMonitorAt(0); | 126 return manager->GetMonitorAt(0); |
119 } | 127 } |
120 const RootWindow* root = window->GetRootWindow(); | 128 const RootWindow* root = window->GetRootWindow(); |
121 return root ? root->GetProperty(kMonitorKey) : NULL; | 129 return root ? root->GetProperty(kMonitorKey) : NULL; |
122 } | 130 } |
123 | 131 |
124 const Monitor* MultiMonitorManager::GetMonitorNearestPoint( | 132 aura::MonitorAura* MultiMonitorManager::GetMonitorNearestWindow( |
| 133 const Window* window) { |
| 134 const MonitorManager* manager = this; |
| 135 return const_cast<aura::MonitorAura*>( |
| 136 manager->GetMonitorNearestWindow(window)); |
| 137 } |
| 138 |
| 139 const aura::MonitorAura* MultiMonitorManager::GetMonitorNearestPoint( |
125 const gfx::Point& point) const { | 140 const gfx::Point& point) const { |
126 // TODO(oshima): For m19, mouse is constrained within | 141 // TODO(oshima): For m19, mouse is constrained within |
127 // the primary window. | 142 // the primary window. |
128 MultiMonitorManager* manager = const_cast<MultiMonitorManager*>(this); | 143 MultiMonitorManager* manager = const_cast<MultiMonitorManager*>(this); |
129 return manager->GetMonitorAt(0); | 144 return manager->GetMonitorAt(0); |
130 } | 145 } |
131 | 146 |
132 Monitor* MultiMonitorManager::GetMonitorAt(size_t index) { | |
133 return index < monitors_.size() ? monitors_[index] : NULL; | |
134 } | |
135 | |
136 size_t MultiMonitorManager::GetNumMonitors() const { | |
137 return monitors_.size(); | |
138 } | |
139 | |
140 Monitor* MultiMonitorManager::GetMonitorNearestWindow(const Window* window) { | |
141 const MonitorManager* manager = this; | |
142 return const_cast<Monitor*>(manager->GetMonitorNearestWindow(window)); | |
143 } | |
144 | |
145 void MultiMonitorManager::OnRootWindowResized(const aura::RootWindow* root, | 147 void MultiMonitorManager::OnRootWindowResized(const aura::RootWindow* root, |
146 const gfx::Size& old_size) { | 148 const gfx::Size& old_size) { |
147 if (!use_fullscreen_host_window()) { | 149 if (!use_fullscreen_host_window()) { |
148 Monitor* monitor = root->GetProperty(kMonitorKey); | 150 MonitorAura* monitor = root->GetProperty(kMonitorKey); |
149 monitor->set_size(root->GetHostSize()); | 151 monitor->set_size(root->GetHostSize()); |
150 NotifyBoundsChanged(monitor); | 152 NotifyBoundsChanged(monitor); |
151 } | 153 } |
152 } | 154 } |
153 | 155 |
154 void MultiMonitorManager::Init() { | 156 void MultiMonitorManager::Init() { |
155 // TODO(oshima): Move this logic to MonitorChangeObserver. | 157 // TODO(oshima): Move this logic to MonitorChangeObserver. |
156 const string size_str = CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | 158 const string size_str = CommandLine::ForCurrentProcess()->GetSwitchValueASCII( |
157 switches::kAuraHostWindowSize); | 159 switches::kAuraHostWindowSize); |
158 vector<string> parts; | 160 vector<string> parts; |
159 base::SplitString(size_str, ',', &parts); | 161 base::SplitString(size_str, ',', &parts); |
160 for (vector<string>::const_iterator iter = parts.begin(); | 162 for (vector<string>::const_iterator iter = parts.begin(); |
161 iter != parts.end(); ++iter) { | 163 iter != parts.end(); ++iter) { |
162 monitors_.push_back(CreateMonitorFromSpec(*iter)); | 164 monitors_.push_back(CreateMonitorFromSpec(*iter)); |
163 } | 165 } |
164 if (monitors_.empty()) | 166 if (monitors_.empty()) |
165 monitors_.push_back(CreateMonitorFromSpec("" /* default */)); | 167 monitors_.push_back(CreateMonitorFromSpec("" /* default */)); |
166 } | 168 } |
167 | 169 |
168 void MultiMonitorManager::AddRemoveMonitorImpl() { | 170 void MultiMonitorManager::AddRemoveMonitorImpl() { |
169 std::vector<const Monitor*> new_monitors; | 171 std::vector<const MonitorAura*> new_monitors; |
170 if (monitors_.size() > 1) { | 172 if (monitors_.size() > 1) { |
171 // Remove if there is more than one monitor. | 173 // Remove if there is more than one monitor. |
172 int count = monitors_.size() - 1; | 174 int count = monitors_.size() - 1; |
173 for (Monitors::const_iterator iter = monitors_.begin(); count-- > 0; ++iter) | 175 for (Monitors::const_iterator iter = monitors_.begin(); count-- > 0; ++iter) |
174 new_monitors.push_back(Copy(*iter)); | 176 new_monitors.push_back(Copy(*iter)); |
175 } else { | 177 } else { |
176 // Add if there is only one monitor. | 178 // Add if there is only one monitor. |
177 new_monitors.push_back(Copy(monitors_[0])); | 179 new_monitors.push_back(Copy(monitors_[0])); |
178 aura::Monitor* extra_monitor = new Monitor; | 180 MonitorAura* extra_monitor = new MonitorAura; |
179 extra_monitor->set_bounds(gfx::Rect(100, 100, 1440, 800)); | 181 extra_monitor->set_bounds(gfx::Rect(100, 100, 1440, 800)); |
180 new_monitors.push_back(extra_monitor); | 182 new_monitors.push_back(extra_monitor); |
181 } | 183 } |
182 if (new_monitors.size()) | 184 if (new_monitors.size()) |
183 OnNativeMonitorsChanged(new_monitors); | 185 OnNativeMonitorsChanged(new_monitors); |
184 STLDeleteContainerPointers(new_monitors.begin(), new_monitors.end()); | 186 STLDeleteContainerPointers(new_monitors.begin(), new_monitors.end()); |
185 } | 187 } |
186 | 188 |
187 void MultiMonitorManager::CycleMonitorImpl() { | 189 void MultiMonitorManager::CycleMonitorImpl() { |
188 if (monitors_.size() > 1) { | 190 if (monitors_.size() > 1) { |
189 std::vector<const Monitor*> new_monitors; | 191 std::vector<const MonitorAura*> new_monitors; |
190 for (Monitors::const_iterator iter = monitors_.begin() + 1; | 192 for (Monitors::const_iterator iter = monitors_.begin() + 1; |
191 iter != monitors_.end(); ++iter) | 193 iter != monitors_.end(); ++iter) |
192 new_monitors.push_back(Copy(*iter)); | 194 new_monitors.push_back(Copy(*iter)); |
193 new_monitors.push_back(Copy(monitors_.front())); | 195 new_monitors.push_back(Copy(monitors_.front())); |
194 OnNativeMonitorsChanged(new_monitors); | 196 OnNativeMonitorsChanged(new_monitors); |
195 STLDeleteContainerPointers(new_monitors.begin(), new_monitors.end()); | 197 STLDeleteContainerPointers(new_monitors.begin(), new_monitors.end()); |
196 } | 198 } |
197 } | 199 } |
198 | 200 |
199 } // namespace internal | 201 } // namespace internal |
200 } // namespace ash | 202 } // namespace ash |
OLD | NEW |