File: wayland_window_manager.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (378 lines) | stat: -rw-r--r-- 12,055 bytes parent folder | download | duplicates (4)
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/ozone/platform/wayland/host/wayland_window_manager.h"

#include <algorithm>

#include "base/check.h"
#include "base/observer_list.h"
#include "ui/base/dragdrop/mojom/drag_drop_types.mojom.h"
#include "ui/display/display.h"
#include "ui/ozone/platform/wayland/host/wayland_connection.h"
#include "ui/ozone/platform/wayland/host/wayland_window.h"
#include "ui/ozone/platform/wayland/host/wayland_window_drag_controller.h"
#include "ui/ozone/platform/wayland/host/wayland_window_observer.h"

namespace ui {

namespace {

void UpdateToplevelActivation(WaylandWindow* old_focused_window,
                              WaylandWindow* new_focused_window) {
  auto* old_focused_toplevel_window =
      old_focused_window
          ? old_focused_window->GetRootParentWindow()->AsWaylandToplevelWindow()
          : nullptr;
  auto* focused_toplevel_window =
      new_focused_window
          ? new_focused_window->GetRootParentWindow()->AsWaylandToplevelWindow()
          : nullptr;
  if (focused_toplevel_window != old_focused_toplevel_window) {
    if (old_focused_toplevel_window) {
      old_focused_toplevel_window->UpdateActivationState();
    }
    if (focused_toplevel_window) {
      focused_toplevel_window->UpdateActivationState();
    }
  }
}

void UpdateParentToplevelAcivationOnRemoval(WaylandWindow* window) {
  auto* toplevel_window =
      window->GetRootParentWindow()->AsWaylandToplevelWindow();
  if (toplevel_window && toplevel_window != window) {
    toplevel_window->UpdateActivationState();
  }
}

}  // namespace

WaylandWindowManager::WaylandWindowManager(WaylandConnection* connection)
    : connection_(connection) {}

WaylandWindowManager::~WaylandWindowManager() = default;

void WaylandWindowManager::AddObserver(WaylandWindowObserver* observer) {
  observers_.AddObserver(observer);
}

void WaylandWindowManager::RemoveObserver(WaylandWindowObserver* observer) {
  observers_.RemoveObserver(observer);
}

void WaylandWindowManager::NotifyWindowConfigured(WaylandWindow* window) {
  observers_.Notify(&WaylandWindowObserver::OnWindowConfigured, window);
}

void WaylandWindowManager::NotifyWindowRoleAssigned(WaylandWindow* window) {
  observers_.Notify(&WaylandWindowObserver::OnWindowRoleAssigned, window);
}

void WaylandWindowManager::NotifyWindowRemovedFromSession(
    WaylandWindow* window) {
  observers_.Notify(&WaylandWindowObserver::OnWindowRemovedFromSession, window);
}

void WaylandWindowManager::GrabLocatedEvents(WaylandWindow* window) {
  DCHECK_NE(located_events_grabber_, window);

  // Wayland doesn't allow to grab the mouse. However, we start forwarding all
  // mouse events received by WaylandWindow to the aura::WindowEventDispatcher
  // which has capture.
  auto* old_grabber = located_events_grabber_.get();
  located_events_grabber_ = window;
  if (old_grabber)
    old_grabber->OnWindowLostCapture();
}

void WaylandWindowManager::UngrabLocatedEvents(WaylandWindow* window) {
  DCHECK_EQ(located_events_grabber_, window);
  auto* old_grabber = located_events_grabber_.get();
  located_events_grabber_ = nullptr;
  old_grabber->OnWindowLostCapture();
}

WaylandWindow* WaylandWindowManager::GetWindow(
    gfx::AcceleratedWidget widget) const {
  auto it = window_map_.find(widget);
  return it == window_map_.end() ? nullptr : it->second;
}

WaylandWindow* WaylandWindowManager::GetWindowWithLargestBounds() const {
  WaylandWindow* window_with_largest_bounds = nullptr;
  for (auto entry : window_map_) {
    if (!window_with_largest_bounds) {
      window_with_largest_bounds = entry.second;
      continue;
    }
    WaylandWindow* window = entry.second;
    if (window_with_largest_bounds->GetBoundsInDIP() < window->GetBoundsInDIP())
      window_with_largest_bounds = window;
  }
  return window_with_largest_bounds;
}

WaylandWindow* WaylandWindowManager::GetCurrentActiveWindow() const {
  for (const auto& entry : window_map_) {
    WaylandWindow* window = entry.second;
    if (window->IsActive())
      return window;
  }
  return nullptr;
}

WaylandWindow* WaylandWindowManager::GetCurrentFocusedWindow() const {
  for (const auto& entry : window_map_) {
    WaylandWindow* window = entry.second;
    if (window == pointer_focused_window_ || window->has_touch_focus() ||
        window == keyboard_focused_window_) {
      return window;
    }
  }
  return nullptr;
}

WaylandWindow* WaylandWindowManager::GetCurrentPointerOrTouchFocusedWindow()
    const {
  // Might be nullptr if no input devices are available.
  if (connection_->window_drag_controller()) {
    // In case there is an ongoing window dragging session, favor the window
    // according to the active drag source.
    //
    // TODO(crbug.com/40222425): Apply the same logic to data drag
    // sessions too?
    if (auto drag_source =
            connection_->window_drag_controller()->drag_source()) {
      return *drag_source == mojom::DragEventSource::kMouse
                 ? GetCurrentPointerFocusedWindow()
                 : GetCurrentTouchFocusedWindow();
    }
  }

  for (const auto& entry : window_map_) {
    WaylandWindow* window = entry.second;
    if (window == pointer_focused_window_ || window->has_touch_focus())
      return window;
  }
  return nullptr;
}

WaylandWindow* WaylandWindowManager::GetCurrentPointerFocusedWindow() const {
#if DCHECK_IS_ON()
  bool found = !pointer_focused_window_;
  for (const auto& entry : window_map_) {
    WaylandWindow* window = entry.second;
    if (window == pointer_focused_window_) {
      found = true;
      break;
    }
  }
  DCHECK(found);
#endif
  return pointer_focused_window_;
}

WaylandWindow* WaylandWindowManager::GetCurrentTouchFocusedWindow() const {
  for (const auto& entry : window_map_) {
    WaylandWindow* window = entry.second;
    if (window->has_touch_focus())
      return window;
  }
  return nullptr;
}

WaylandWindow* WaylandWindowManager::GetCurrentKeyboardFocusedWindow() const {
#if DCHECK_IS_ON()
  bool found = !keyboard_focused_window_;
  for (const auto& entry : window_map_) {
    WaylandWindow* window = entry.second;
    if (window == keyboard_focused_window_) {
      found = true;
      break;
    }
  }
  DCHECK(found);
#endif
  return keyboard_focused_window_;
}

WaylandWindow* WaylandWindowManager::GetCurrentTextInputFocusedWindow() const {
  return text_input_focused_window_;
}

void WaylandWindowManager::SetPointerFocusedWindow(WaylandWindow* window) {
  auto* old_focused_window = GetCurrentPointerFocusedWindow();
  if (window == old_focused_window)
    return;
  if (old_focused_window)
    old_focused_window->OnPointerFocusChanged(false);
  pointer_focused_window_ = window;
  if (window)
    window->OnPointerFocusChanged(true);
}

void WaylandWindowManager::SetTouchFocusedWindow(WaylandWindow* window) {
  auto* old_focused_window = GetCurrentTouchFocusedWindow();
  if (window == old_focused_window)
    return;
  if (old_focused_window)
    old_focused_window->set_touch_focus(false);
  if (window)
    window->set_touch_focus(true);
}

void WaylandWindowManager::SetKeyboardFocusedWindow(WaylandWindow* window) {
  auto* old_focused_window = GetCurrentKeyboardFocusedWindow();
  if (window == old_focused_window)
    return;
  keyboard_focused_window_ = window;
  if (old_focused_window) {
    old_focused_window->OnKeyboardFocusChanged(false);
  }
  if (window) {
    window->OnKeyboardFocusChanged(true);
  }
  UpdateToplevelActivation(old_focused_window, window);
}

void WaylandWindowManager::SetTextInputFocusedWindow(WaylandWindow* window) {
  auto* old_focused_window = GetCurrentTextInputFocusedWindow();
  if (window == old_focused_window) {
    return;
  }
  text_input_focused_window_ = window;
  if (old_focused_window) {
    old_focused_window->OnTextInputFocusChanged(false);
  }
  if (window) {
    window->OnTextInputFocusChanged(true);
  }
  UpdateToplevelActivation(old_focused_window, window);
}

void WaylandWindowManager::AddWindow(gfx::AcceleratedWidget widget,
                                     WaylandWindow* window) {
  window_map_[widget] = window;

  observers_.Notify(&WaylandWindowObserver::OnWindowAdded, window);
}

void WaylandWindowManager::RemoveWindow(gfx::AcceleratedWidget widget) {
  auto* window = window_map_[widget].get();
  DCHECK(window);

  window_map_.erase(widget);

  // Reset `*_focused_window_` before notifying any observers to make sure
  // GetCurrent*FocusedWindow() behave correctly.
  // The pointer case in particular can be problematic if notifying
  // WaylandWindowDragController that a window has been removed before resetting
  // `pointer_focused_window_`, because that leads to
  // WaylandEventSource::OnPointerButtonEvent() being called, which then calls
  // GetCurrentPointerFocusedWindow().
  if (window == pointer_focused_window_) {
    pointer_focused_window_ = nullptr;
  }
  if (window == keyboard_focused_window_) {
    keyboard_focused_window_ = nullptr;
    UpdateParentToplevelAcivationOnRemoval(window);
  }
  if (window == text_input_focused_window_) {
    text_input_focused_window_ = nullptr;
    UpdateParentToplevelAcivationOnRemoval(window);
  }

  observers_.Notify(&WaylandWindowObserver::OnWindowRemoved, window);
}

void WaylandWindowManager::AddSubsurface(gfx::AcceleratedWidget widget,
                                         WaylandSubsurface* subsurface) {
  auto* window = window_map_[widget].get();
  DCHECK(window);

  observers_.Notify(&WaylandWindowObserver::OnSubsurfaceAdded, window,
                    subsurface);
}

void WaylandWindowManager::RemoveSubsurface(gfx::AcceleratedWidget widget,
                                            WaylandSubsurface* subsurface) {
  auto* window = window_map_[widget].get();
  DCHECK(window);

  observers_.Notify(&WaylandWindowObserver::OnSubsurfaceRemoved, window,
                    subsurface);
}

void WaylandWindowManager::RecycleSubsurface(
    std::unique_ptr<WaylandSubsurface> subsurface) {
  // Reset the root window when the corresponding subsurface is invalid,
  // preventing it from receiving events.
  subsurface->wayland_surface()->UnsetRootWindow();
  subsurface_recycle_cache_ = std::move(subsurface);
}

gfx::AcceleratedWidget WaylandWindowManager::AllocateAcceleratedWidget() {
  return ++last_accelerated_widget_;
}

void WaylandWindowManager::DumpState(std::ostream& out) const {
  int i = 0;
  out << "WaylandWindowManager:" << std::endl;
  for (const auto& window : window_map_) {
    out << "  wayland_window[" << i++ << "]:";
    window.second->DumpState(out);
    out << std::endl;
  }
}

std::vector<WaylandWindow*> WaylandWindowManager::GetAllWindows() const {
  std::vector<WaylandWindow*> result;
  for (auto& entry : window_map_) {
    result.push_back(entry.second);
  }
  return result;
}

bool WaylandWindowManager::IsWindowValid(const WaylandWindow* window) const {
  for (auto& pair : window_map_) {
    if (pair.second == window)
      return true;
  }
  return false;
}

void WaylandWindowManager::SetFontScale(float new_font_scale) {
  if (new_font_scale == font_scale_) {
    return;
  }
  font_scale_ = new_font_scale;
  for (WaylandWindow* window : GetAllWindows()) {
    window->OnFontScaleFactorChanged();
  }
}

float WaylandWindowManager::DetermineUiScale() const {
  using display::Display;
  constexpr float kMinUiScale = 0.5f;
  constexpr float kMaxUiScale = 3.0f;
  CHECK(connection_->IsUiScaleEnabled() || font_scale_ == 1.0f) << font_scale_;

  const float ui_scale =
      Display::HasForceDeviceScaleFactor() && connection_->IsUiScaleEnabled()
          ? Display::GetForcedDeviceScaleFactor()
          : font_scale_;
  return std::clamp(ui_scale, kMinUiScale, kMaxUiScale);
}

void WaylandWindowManager::UpdateActivationState() {
  for (auto& pair : window_map_) {
    auto* toplevel_window = pair.second->AsWaylandToplevelWindow();
    if (toplevel_window) {
      toplevel_window->UpdateActivationState();
    }
  }
}

}  // namespace ui