OLD | NEW |
| (Empty) |
1 // Copyright 2016 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 "components/mus/ws/display_manager.h" | |
6 | |
7 #include "base/memory/ptr_util.h" | |
8 #include "components/mus/ws/display.h" | |
9 #include "components/mus/ws/display_manager_delegate.h" | |
10 #include "components/mus/ws/event_dispatcher.h" | |
11 #include "components/mus/ws/server_window.h" | |
12 #include "components/mus/ws/user_display_manager.h" | |
13 #include "components/mus/ws/user_id_tracker.h" | |
14 #include "components/mus/ws/window_manager_state.h" | |
15 | |
16 namespace mus { | |
17 namespace ws { | |
18 | |
19 DisplayManager::DisplayManager(DisplayManagerDelegate* delegate, | |
20 UserIdTracker* user_id_tracker) | |
21 // |next_root_id_| is used as the lower bits, so that starting at 0 is | |
22 // fine. |next_display_id_| is used by itself, so we start at 1 to reserve | |
23 // 0 as invalid. | |
24 : delegate_(delegate), | |
25 user_id_tracker_(user_id_tracker), | |
26 next_root_id_(0), | |
27 next_display_id_(1) { | |
28 user_id_tracker_->AddObserver(this); | |
29 } | |
30 | |
31 DisplayManager::~DisplayManager() { | |
32 user_id_tracker_->RemoveObserver(this); | |
33 DestroyAllDisplays(); | |
34 } | |
35 | |
36 UserDisplayManager* DisplayManager::GetUserDisplayManager( | |
37 const UserId& user_id) { | |
38 if (!user_display_managers_.count(user_id)) { | |
39 user_display_managers_[user_id] = | |
40 base::WrapUnique(new UserDisplayManager(this, delegate_, user_id)); | |
41 } | |
42 return user_display_managers_[user_id].get(); | |
43 } | |
44 | |
45 void DisplayManager::AddDisplay(Display* display) { | |
46 DCHECK_EQ(0u, pending_displays_.count(display)); | |
47 pending_displays_.insert(display); | |
48 } | |
49 | |
50 void DisplayManager::DestroyDisplay(Display* display) { | |
51 if (pending_displays_.count(display)) { | |
52 pending_displays_.erase(display); | |
53 } else { | |
54 for (const auto& pair : user_display_managers_) | |
55 pair.second->OnWillDestroyDisplay(display); | |
56 | |
57 DCHECK(displays_.count(display)); | |
58 displays_.erase(display); | |
59 } | |
60 delete display; | |
61 | |
62 // If we have no more roots left, let the app know so it can terminate. | |
63 // TODO(sky): move to delegate/observer. | |
64 if (!displays_.size() && !pending_displays_.size()) | |
65 delegate_->OnNoMoreDisplays(); | |
66 } | |
67 | |
68 void DisplayManager::DestroyAllDisplays() { | |
69 while (!pending_displays_.empty()) | |
70 DestroyDisplay(*pending_displays_.begin()); | |
71 DCHECK(pending_displays_.empty()); | |
72 | |
73 while (!displays_.empty()) | |
74 DestroyDisplay(*displays_.begin()); | |
75 DCHECK(displays_.empty()); | |
76 } | |
77 | |
78 std::set<const Display*> DisplayManager::displays() const { | |
79 std::set<const Display*> ret_value(displays_.begin(), displays_.end()); | |
80 return ret_value; | |
81 } | |
82 | |
83 void DisplayManager::OnDisplayUpdate(Display* display) { | |
84 for (const auto& pair : user_display_managers_) | |
85 pair.second->OnDisplayUpdate(display); | |
86 } | |
87 | |
88 Display* DisplayManager::GetDisplayContaining(ServerWindow* window) { | |
89 return const_cast<Display*>( | |
90 static_cast<const DisplayManager*>(this)->GetDisplayContaining(window)); | |
91 } | |
92 | |
93 const Display* DisplayManager::GetDisplayContaining( | |
94 const ServerWindow* window) const { | |
95 while (window && window->parent()) | |
96 window = window->parent(); | |
97 for (Display* display : displays_) { | |
98 if (window == display->root_window()) | |
99 return display; | |
100 } | |
101 return nullptr; | |
102 } | |
103 | |
104 const WindowManagerDisplayRoot* DisplayManager::GetWindowManagerDisplayRoot( | |
105 const ServerWindow* window) const { | |
106 const ServerWindow* last = window; | |
107 while (window && window->parent()) { | |
108 last = window; | |
109 window = window->parent(); | |
110 } | |
111 for (Display* display : displays_) { | |
112 if (window == display->root_window()) | |
113 return display->GetWindowManagerDisplayRootWithRoot(last); | |
114 } | |
115 return nullptr; | |
116 } | |
117 | |
118 WindowManagerDisplayRoot* DisplayManager::GetWindowManagerDisplayRoot( | |
119 const ServerWindow* window) { | |
120 return const_cast<WindowManagerDisplayRoot*>( | |
121 const_cast<const DisplayManager*>(this)->GetWindowManagerDisplayRoot( | |
122 window)); | |
123 } | |
124 | |
125 WindowId DisplayManager::GetAndAdvanceNextRootId() { | |
126 // TODO(sky): handle wrapping! | |
127 const uint16_t id = next_root_id_++; | |
128 DCHECK_LT(id, next_root_id_); | |
129 return RootWindowId(id); | |
130 } | |
131 | |
132 uint32_t DisplayManager::GetAndAdvanceNextDisplayId() { | |
133 // TODO(sky): handle wrapping! | |
134 const uint32_t id = next_display_id_++; | |
135 DCHECK_LT(id, next_display_id_); | |
136 return id; | |
137 } | |
138 | |
139 void DisplayManager::OnDisplayAcceleratedWidgetAvailable(Display* display) { | |
140 DCHECK_NE(0u, pending_displays_.count(display)); | |
141 DCHECK_EQ(0u, displays_.count(display)); | |
142 const bool is_first_display = displays_.empty(); | |
143 displays_.insert(display); | |
144 pending_displays_.erase(display); | |
145 if (is_first_display) | |
146 delegate_->OnFirstDisplayReady(); | |
147 } | |
148 | |
149 void DisplayManager::OnActiveUserIdChanged(const UserId& previously_active_id, | |
150 const UserId& active_id) { | |
151 WindowManagerState* previous_window_manager_state = | |
152 delegate_->GetWindowManagerStateForUser(previously_active_id); | |
153 gfx::Point mouse_location_on_screen; | |
154 if (previous_window_manager_state) { | |
155 mouse_location_on_screen = previous_window_manager_state->event_dispatcher() | |
156 ->mouse_pointer_last_location(); | |
157 previous_window_manager_state->Deactivate(); | |
158 } | |
159 | |
160 WindowManagerState* current_window_manager_state = | |
161 delegate_->GetWindowManagerStateForUser(active_id); | |
162 if (current_window_manager_state) | |
163 current_window_manager_state->Activate(mouse_location_on_screen); | |
164 } | |
165 | |
166 } // namespace ws | |
167 } // namespace mus | |
OLD | NEW |