File: x11_screen_ozone.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 (282 lines) | stat: -rw-r--r-- 9,391 bytes parent folder | download | duplicates (5)
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
// Copyright 2018 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/x11/x11_screen_ozone.h"

#include <memory>

#include "base/containers/flat_set.h"
#include "ui/base/linux/linux_desktop.h"
#include "ui/base/x/x11_display_util.h"
#include "ui/base/x/x11_idle_query.h"
#include "ui/base/x/x11_screensaver.h"
#include "ui/base/x/x11_util.h"
#include "ui/display/display.h"
#include "ui/display/display_finder.h"
#include "ui/events/platform/x11/x11_event_source.h"
#include "ui/gfx/font_render_params.h"
#include "ui/gfx/geometry/point_conversions.h"
#include "ui/gfx/native_widget_types.h"
#include "ui/gfx/x/window_cache.h"
#include "ui/ozone/platform/x11/x11_window.h"
#include "ui/ozone/platform/x11/x11_window_manager.h"

#if BUILDFLAG(IS_LINUX)
#include "ui/linux/linux_ui.h"
#endif

namespace ui {

namespace {

using DisplayList = std::vector<display::Display>;

gfx::Rect DisplayBoundsInPixels(const display::Display& display) {
  return {display.native_origin(), display.GetSizeInPixel()};
}

const display::Display& GetDisplayForRect(const DisplayList& displays,
                                          const gfx::Rect& rect,
                                          bool rect_in_px) {
  DCHECK(!displays.empty());
  constexpr auto kMaxDist = std::make_pair(INT_MAX, INT_MAX);
  auto min_dist_display = std::make_pair(kMaxDist, displays.data());
  for (const auto& display : displays) {
    auto bounds =
        rect_in_px ? DisplayBoundsInPixels(display) : display.bounds();
    min_dist_display = std::min(
        min_dist_display, std::make_pair(RectDistance(bounds, rect), &display));
  }
  return *min_dist_display.second;
}

gfx::PointF PointPxToDip(const DisplayList& displays, gfx::Point point_px) {
  const auto& display =
      GetDisplayForRect(displays, gfx::Rect(point_px, gfx::Size(1, 1)), true);
  gfx::Vector2d delta_px = point_px - display.native_origin();
  gfx::Vector2dF delta_dip =
      gfx::ScaleVector2d(delta_px, 1.0 / display.device_scale_factor());
  return gfx::PointF(display.bounds().origin()) + delta_dip;
}

gfx::PointF PointDipToPx(const DisplayList& displays, gfx::Point point_dip) {
  const auto& display =
      GetDisplayForRect(displays, gfx::Rect(point_dip, gfx::Size(1, 1)), false);
  gfx::Rect bounds_dip = display.bounds();
  gfx::Vector2d delta_dip = point_dip - bounds_dip.origin();
  gfx::Vector2dF delta_px =
      gfx::ScaleVector2d(delta_dip, display.device_scale_factor());
  return gfx::PointF(display.native_origin()) + delta_px;
}

gfx::Point GetCursorScreenPointPx(x11::Connection& connection) {
  if (ui::X11EventSource::HasInstance()) {
    auto point_in_pixels =
        ui::X11EventSource::GetInstance()->last_cursor_location();
    if (point_in_pixels.has_value()) {
      return point_in_pixels.value();
    }
  }
  // This call is expensive so we explicitly only call it when
  // X11EventSource doesn't have a last_cursor_location set.
  auto response = connection.QueryPointer({connection.default_root()}).Sync();
  auto point_in_pixels =
      response ? gfx::Point(response->root_x, response->root_y) : gfx::Point();
  if (ui::X11EventSource::HasInstance()) {
    ui::X11EventSource::GetInstance()->set_last_cursor_location(
        point_in_pixels);
  }
  return point_in_pixels;
}

}  // namespace

X11ScreenOzone::X11ScreenOzone()
    : connection_(x11::Connection::Get()),
      window_manager_(X11WindowManager::GetInstance()),
      x11_display_manager_(std::make_unique<XDisplayManager>(this)) {
  DCHECK(window_manager_);
#if BUILDFLAG(IS_LINUX)
  if (auto* linux_ui = ui::LinuxUi::instance()) {
    display_scale_factor_observer_.Observe(linux_ui);
  }
#endif
}

X11ScreenOzone::~X11ScreenOzone() {
  if (x11_display_manager_->IsXrandrAvailable()) {
    connection_->RemoveEventObserver(this);
  }
}

void X11ScreenOzone::Init() {
  initialized_ = true;
  if (x11_display_manager_->IsXrandrAvailable()) {
    connection_->AddEventObserver(this);
  }
  x11_display_manager_->Init();
}

const std::vector<display::Display>& X11ScreenOzone::GetAllDisplays() const {
  return x11_display_manager_->displays();
}

display::Display X11ScreenOzone::GetPrimaryDisplay() const {
  return x11_display_manager_->GetPrimaryDisplay();
}

display::Display X11ScreenOzone::GetDisplayForAcceleratedWidget(
    gfx::AcceleratedWidget widget) const {
  if (widget == gfx::kNullAcceleratedWidget) {
    return GetPrimaryDisplay();
  }

  X11Window* window = window_manager_->GetWindow(widget);
  if (window) {
    return GetDisplayForRect(GetAllDisplays(), window->GetBoundsInPixels(),
                             true);
  }
  return GetPrimaryDisplay();
}

gfx::Point X11ScreenOzone::GetCursorScreenPoint() const {
  // TODO(danakj): Should this be rounded? Or kept as a floating point?
  return gfx::ToFlooredPoint(
      PointPxToDip(GetAllDisplays(), GetCursorScreenPointPx(*connection_)));
}

bool X11ScreenOzone::IsAcceleratedWidgetUnderCursor(
    gfx::AcceleratedWidget widget) const {
  // Only ask the X11Window for its pointer state when some other window does
  // not have mouse capture because capture disrupts pointer event tracking.
  if (!window_manager_->located_events_grabber()) {
    if (X11Window* window = window_manager_->GetWindow(widget)) {
      return window->has_pointer();
    }
  }
  return GetAcceleratedWidgetAtScreenPoint(GetCursorScreenPoint()) == widget;
}

gfx::AcceleratedWidget X11ScreenOzone::GetAcceleratedWidgetAtScreenPoint(
    const gfx::Point& point) const {
  gfx::Point point_in_pixels =
      gfx::ToFlooredPoint(PointDipToPx(GetAllDisplays(), point));
  auto widget = static_cast<gfx::AcceleratedWidget>(
      x11::GetWindowAtPoint(point_in_pixels));
  auto* window = window_manager_->GetWindow(widget);
  if (window && !window->IsVisible()) {
    // The window cache may be out of sync with respect to recently changed
    // window state. This can happen if the window was recently hidden.
    return gfx::kNullAcceleratedWidget;
  }
  return widget;
}

gfx::AcceleratedWidget X11ScreenOzone::GetLocalProcessWidgetAtPoint(
    const gfx::Point& point,
    const std::set<gfx::AcceleratedWidget>& ignore) const {
  gfx::AcceleratedWidget widget{};
  if (ignore.empty()) {
    widget = GetAcceleratedWidgetAtScreenPoint(point);
  } else {
    gfx::Point point_in_pixels =
        gfx::ToFlooredPoint(PointDipToPx(GetAllDisplays(), point));
    base::flat_set<x11::Window> ignore_windows;
    for (auto ignore_widget : ignore) {
      ignore_windows.insert(static_cast<x11::Window>(ignore_widget));
    }
    widget = static_cast<gfx::AcceleratedWidget>(
        x11::GetWindowAtPoint(point_in_pixels, &ignore_windows));
  }
  return window_manager_->GetWindow(widget) ? widget : gfx::AcceleratedWidget{};
}

display::Display X11ScreenOzone::GetDisplayNearestPoint(
    const gfx::Point& point) const {
  auto displays = GetAllDisplays();
  if (displays.size() <= 1) {
    return GetPrimaryDisplay();
  }
  return *display::FindDisplayNearestPoint(displays, point);
}

display::Display X11ScreenOzone::GetDisplayMatching(
    const gfx::Rect& match_rect) const {
  const display::Display* matching_display =
      display::FindDisplayWithBiggestIntersection(
          x11_display_manager_->displays(), match_rect);
  return matching_display ? *matching_display : GetPrimaryDisplay();
}

X11ScreenOzone::X11ScreenSaverSuspender::X11ScreenSaverSuspender() {
  is_suspending_ = SuspendX11ScreenSaver(true);
}

std::unique_ptr<X11ScreenOzone::X11ScreenSaverSuspender>
X11ScreenOzone::X11ScreenSaverSuspender::Create() {
  auto suspender = base::WrapUnique(new X11ScreenSaverSuspender());
  if (suspender->is_suspending_) {
    return suspender;
  }

  return nullptr;
}

X11ScreenOzone::X11ScreenSaverSuspender::~X11ScreenSaverSuspender() {
  if (is_suspending_) {
    SuspendX11ScreenSaver(false);
  }
}

std::unique_ptr<PlatformScreen::PlatformScreenSaverSuspender>
X11ScreenOzone::SuspendScreenSaver() {
  return X11ScreenSaverSuspender::Create();
}

bool X11ScreenOzone::IsScreenSaverActive() const {
  // Usually the screensaver is used to lock the screen.
  return IsXScreensaverActive();
}

base::TimeDelta X11ScreenOzone::CalculateIdleTime() const {
  IdleQueryX11 idle_query;
  return base::Seconds(idle_query.IdleTime());
}

void X11ScreenOzone::AddObserver(display::DisplayObserver* observer) {
  x11_display_manager_->AddObserver(observer);
}

void X11ScreenOzone::RemoveObserver(display::DisplayObserver* observer) {
  x11_display_manager_->RemoveObserver(observer);
}

std::string X11ScreenOzone::GetCurrentWorkspace() {
  return x11_display_manager_->GetCurrentWorkspace();
}

base::Value::List X11ScreenOzone::GetGpuExtraInfo(
    const gfx::GpuExtraInfo& gpu_extra_info) {
  auto result = GetDesktopEnvironmentInfo();
  StorePlatformNameIntoListOfValues(result, "x11");
  return result;
}

void X11ScreenOzone::OnEvent(const x11::Event& xev) {
  x11_display_manager_->OnEvent(xev);
}

#if BUILDFLAG(IS_LINUX)
void X11ScreenOzone::OnDeviceScaleFactorChanged() {
  x11_display_manager_->DispatchDelayedDisplayListUpdate();
}
#endif

void X11ScreenOzone::OnXDisplayListUpdated() {
  float scale_factor =
      x11_display_manager_->GetPrimaryDisplay().device_scale_factor();
  gfx::SetFontRenderParamsDeviceScaleFactor(scale_factor);
}

}  // namespace ui