File: active_display_monitor_x11.cc

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 6,122,156 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (275 lines) | stat: -rw-r--r-- 9,026 bytes parent folder | download | duplicates (6)
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
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "remoting/host/linux/active_display_monitor_x11.h"

#include <memory>

#include "base/task/bind_post_task.h"
#include "base/task/single_thread_task_runner.h"
#include "base/types/cxx23_to_underlying.h"
#include "remoting/base/logging.h"
#include "third_party/webrtc/modules/desktop_capture/desktop_geometry.h"
#include "ui/base/x/x11_display_util.h"
#include "ui/base/x/x11_util.h"
#include "ui/gfx/x/atom_cache.h"
#include "ui/gfx/x/connection.h"
#include "ui/gfx/x/event.h"
#include "ui/gfx/x/future.h"
#include "ui/gfx/x/randr.h"
#include "ui/gfx/x/window_event_manager.h"

namespace remoting {

namespace {

// For X11, webrtc::ScreenId is implemented as a RANDR Monitor ID, which
// requires XRANDR 1.5.
constexpr std::pair<uint32_t, uint32_t> kMinRandrVersion{1, 5};

}  // namespace

// This class uses Chromium X11 utilities which create global singletons, so
// this code needs to run on the UI thread.
class ActiveDisplayMonitorX11::Core : public x11::EventObserver {
 public:
  // `callback` will be run on the UI thread whenever the active display is
  // changed. The callback is responsible for posting a task back to the
  // caller's thread.
  explicit Core(ActiveDisplayMonitor::Callback callback);

  Core(const Core&) = delete;
  Core& operator=(const Core&) = delete;

  ~Core() override;

  void Init();

  // x11::EventObserver implementation.
  void OnEvent(const x11::Event& xevent) override;

 private:
  // Called when the active display might have changed. This gets the
  // currently-focused window and determines which display it is on. If the
  // display is changed, it is sent to `callback_`.
  void GetAndSendActiveDisplay();

  // Returns the window with input-focus, or x11::Window::None. The returned
  // window may be a descendent of a top-level window.
  x11::Window GetFocusedWindow() const;

  // Returns the ancestor of 'window' which is a child of the root window. The
  // returned window may be a window-manager frame. On error, this returns
  // x11::Window::None.
  x11::Window GetTopLevelWindow(x11::Window window) const;

  bool IsWindowVisible(x11::Window window) const;

  // Returns the display that the window is positioned on. On error, this
  // returns webrtc::kInvalidScreenId.
  webrtc::ScreenId GetDisplayForWindow(x11::Window window) const;

  ActiveDisplayMonitor::Callback callback_;

  raw_ptr<x11::Connection> connection_ = nullptr;

  x11::ScopedEventSelector root_window_event_selector_;

  x11::Atom net_active_window_atom_{};

  webrtc::ScreenId current_active_display_{webrtc::kInvalidScreenId};
};

////////////////////////////////////////////////////////////////////////////////
// ActiveDisplayMonitorX11::Core implementation.

ActiveDisplayMonitorX11::Core::Core(ActiveDisplayMonitor::Callback callback)
    : callback_(callback) {}

ActiveDisplayMonitorX11::Core::~Core() {
  // Does nothing if AddEventObserver() was not called.
  connection_->RemoveEventObserver(this);
}

void ActiveDisplayMonitorX11::Core::Init() {
  connection_ = x11::Connection::Get();
  auto xrandr_version = connection_->randr_version();
  if (xrandr_version < kMinRandrVersion) {
    LOG(ERROR) << "XRANDR version (" << xrandr_version.first << ", "
               << xrandr_version.second << ") is unsupported.";
    return;
  }

  root_window_event_selector_ = connection_->ScopedSelectEvent(
      ui::GetX11RootWindow(), x11::EventMask::PropertyChange);

  net_active_window_atom_ = x11::GetAtom("_NET_ACTIVE_WINDOW");

  connection_->AddEventObserver(this);
}

void ActiveDisplayMonitorX11::Core::OnEvent(const x11::Event& xevent) {
  const auto* property_notify = xevent.As<x11::PropertyNotifyEvent>();
  if (!property_notify) {
    return;
  }

  if (property_notify->window != ui::GetX11RootWindow()) {
    return;
  }

  if (property_notify->atom != net_active_window_atom_) {
    return;
  }

  // Check the property was not deleted.
  if (property_notify->state != x11::Property::NewValue) {
    return;
  }

  GetAndSendActiveDisplay();
}

void ActiveDisplayMonitorX11::Core::GetAndSendActiveDisplay() {
  x11::Window focused_window = GetFocusedWindow();
  if (focused_window == x11::Window::None) {
    HOST_LOG << "No window is focused.";
    return;
  }

  focused_window = GetTopLevelWindow(focused_window);
  if (focused_window == x11::Window::None) {
    return;
  }

  if (!IsWindowVisible(focused_window)) {
    // It's not clear if a window-manager would ever move input-focus to a
    // hidden window? It seems safer to not activate a different display if the
    // user can't see the window on it.
    HOST_LOG << "Focused window " << base::to_underlying(focused_window)
             << " is hidden, ignoring.";
    return;
  }

  webrtc::ScreenId active_display = GetDisplayForWindow(focused_window);
  if (active_display == webrtc::kInvalidScreenId) {
    LOG(ERROR) << "Failed to determine display for window "
               << base::to_underlying(focused_window);
    return;
  }

  if (active_display == current_active_display_) {
    return;
  }

  current_active_display_ = active_display;
  HOST_LOG << "Active display changed to " << active_display
           << " due to window " << base::to_underlying(focused_window);
  callback_.Run(active_display);
}

x11::Window ActiveDisplayMonitorX11::Core::GetFocusedWindow() const {
  x11::Window focused_window;
  if (!x11::Connection::Get()->GetPropertyAs(
          ui::GetX11RootWindow(), net_active_window_atom_, &focused_window)) {
    LOG(ERROR) << "Failed to read _NET_ACTIVE_WINDOW on root window.";
    return x11::Window::None;
  }

  return focused_window;
}

x11::Window ActiveDisplayMonitorX11::Core::GetTopLevelWindow(
    x11::Window window) const {
  // This will not loop infinitely, as long as the X11 server respects the
  // protocol and provides a correct tree of windows.
  while (true) {
    auto query_response = connection_->QueryTree({window}).Sync();
    if (!query_response) {
      LOG(ERROR) << "QueryTree failed for window "
                 << base::to_underlying(window);
      return x11::Window::None;
    }
    if (query_response->parent == x11::Window::None) {
      // The root window was provided, so just return it directly. This is not
      // expected to happen, but any program could set arbitrary values for the
      // root window's _NET_ACTIVE_WINDOW property, so this code should handle
      // all possibilities.
      break;
    }
    if (query_response->root == query_response->parent) {
      // Found top-level window.
      break;
    }

    window = query_response->parent;
  }
  return window;
}

bool ActiveDisplayMonitorX11::Core::IsWindowVisible(x11::Window window) const {
  auto attributes = connection_->GetWindowAttributes({window}).Sync();
  if (!attributes) {
    LOG(ERROR) << "Failed to get attributes for window "
               << base::to_underlying(window);
    return false;
  }
  return attributes->map_state == x11::MapState::Viewable;
}

webrtc::ScreenId ActiveDisplayMonitorX11::Core::GetDisplayForWindow(
    x11::Window window) const {
  webrtc::ScreenId result = webrtc::kInvalidScreenId;

  auto geometry = connection_->GetGeometry(window).Sync();
  if (!geometry) {
    LOG(ERROR) << "GetGeometry() failed for window "
               << base::to_underlying(window);
    return result;
  }

  auto monitors =
      connection_->randr().GetMonitors({ui::GetX11RootWindow()}).Sync();
  if (!monitors) {
    LOG(ERROR) << "RANDR GetMonitors() failed.";
    return result;
  }

  auto window_rect = webrtc::DesktopRect::MakeXYWH(
      geometry->x, geometry->y, geometry->width, geometry->height);
  int best_area = 0;
  for (const x11::RandR::MonitorInfo& info : monitors->monitors) {
    auto monitor_rect =
        webrtc::DesktopRect::MakeXYWH(info.x, info.y, info.width, info.height);
    monitor_rect.IntersectWith(window_rect);
    int area = monitor_rect.width() * monitor_rect.height();

    // Strict inequality ensures that kInvalidScreenId is returned in the case
    // that all overlaps have zero area.
    if (area > best_area) {
      // The X11 desktop-capturer uses the `name` atom as the screen ID.
      result = base::to_underlying(info.name);
      best_area = area;
    }
  }

  return result;
}

////////////////////////////////////////////////////////////////////////////////
// ActiveDisplayMonitorX11 implementation.

ActiveDisplayMonitorX11::ActiveDisplayMonitorX11(
    scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner,
    Callback active_display_callback)
    : active_display_callback_(active_display_callback) {
  Callback wrapped_callback =
      base::BindPostTaskToCurrentDefault(active_display_callback_.callback());
  core_.emplace(ui_task_runner, wrapped_callback);
  core_.AsyncCall(&ActiveDisplayMonitorX11::Core::Init);
}

ActiveDisplayMonitorX11::~ActiveDisplayMonitorX11() = default;

}  // namespace remoting