File: randr_output_manager.h

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 (156 lines) | stat: -rw-r--r-- 5,568 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
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef UI_GFX_X_RANDR_OUTPUT_MANAGER_H_
#define UI_GFX_X_RANDR_OUTPUT_MANAGER_H_

#include <memory>
#include <optional>
#include <string>
#include <utility>
#include <vector>

#include "base/component_export.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/vector2d.h"
#include "ui/gfx/x/randr.h"

namespace x11 {

// Wrapper class for the XRRScreenResources struct.
class COMPONENT_EXPORT(X11) ScreenResources {
 public:
  ScreenResources();
  ~ScreenResources();

  bool Refresh(x11::RandR* randr, x11::Window window);

  x11::RandR::Mode GetIdForMode(const std::string& name);

  x11::RandR::GetScreenResourcesCurrentReply* get();

 private:
  std::unique_ptr<x11::RandR::GetScreenResourcesCurrentReply> resources_;
};

// Encapsulates basic configuration properties for a single RandR monitor.
class COMPONENT_EXPORT(X11) RandRMonitorConfig {
 public:
  using ScreenId = int64_t;
  RandRMonitorConfig(std::optional<ScreenId> id,
                     gfx::Rect rect,
                     gfx::Vector2d dpi);
  RandRMonitorConfig(const RandRMonitorConfig& other);
  RandRMonitorConfig& operator=(const RandRMonitorConfig& other);
  bool operator==(const RandRMonitorConfig& rhs) const;

  std::optional<ScreenId> id() const { return id_; }

  const gfx::Rect& rect() const { return rect_; }
  const gfx::Vector2d& dpi() const { return dpi_; }

 private:
  // An opaque ID used to identify this monitor. Unset when the ID is unknown,
  // for example, when a caller is setting a new layout and the screen ID
  // does not exist yet.
  std::optional<ScreenId> id_;
  gfx::Rect rect_;
  gfx::Vector2d dpi_;
};

// Encapsulates a set of monitors to represent an entire screen layout.
struct COMPONENT_EXPORT(X11) RandRMonitorLayout {
  RandRMonitorLayout();
  RandRMonitorLayout(const RandRMonitorLayout&);
  explicit RandRMonitorLayout(std::vector<RandRMonitorConfig> configs);
  RandRMonitorLayout& operator=(const RandRMonitorLayout&);
  ~RandRMonitorLayout();
  bool operator==(const RandRMonitorLayout& rhs) const;

  std::vector<RandRMonitorConfig> configs;
  std::optional<int64_t> primary_screen_id;
};

// Attaches an arbitrary pointer to a monitor config for internal tracking
// purposes.
struct COMPONENT_EXPORT(X11) RandRMonitorConfigWithContext {
  x11::RandRMonitorConfig config;
  raw_ptr<void> context;
};

// Structure to hold data which describes changes between two layouts.
struct COMPONENT_EXPORT(X11) DisplayLayoutDiff {
  DisplayLayoutDiff();
  DisplayLayoutDiff(
      x11::RandRMonitorLayout new_displays,
      std::vector<RandRMonitorConfigWithContext> updated_displays,
      std::vector<RandRMonitorConfigWithContext> removed_displays);
  DisplayLayoutDiff(const DisplayLayoutDiff&);
  ~DisplayLayoutDiff();
  x11::RandRMonitorLayout new_displays;
  std::vector<RandRMonitorConfigWithContext> updated_displays;
  std::vector<RandRMonitorConfigWithContext> removed_displays;
};

// Calculates the difference between the current display layout and the new
// display layout. Displays are matched using the screen ID.
COMPONENT_EXPORT(X11)
DisplayLayoutDiff CalculateDisplayLayoutDiff(
    const std::vector<RandRMonitorConfigWithContext>& current_displays,
    const x11::RandRMonitorLayout& new_layout);

// Manages modes and layout of RandR outputs.
class COMPONENT_EXPORT(X11) RandROutputManager final {
 public:
  // `output_name_prefix` is used when creating new RandR outputs. Optionally
  // override the default mode dot clock with `default_mode_dot_clock`.
  explicit RandROutputManager(
      std::string output_name_prefix,
      uint32_t default_mode_dot_clock = 60 * 1e6 /* Realistic default */);
  ~RandROutputManager();

  // Attempts to get the current list of XRandR monitors from the current
  // connection. Returns true on success in which case `list` is populated with
  // the monitors. Returns false otherwise.
  bool TryGetCurrentMonitors(std::vector<x11::RandR::MonitorInfo>& list);

  // Obtains the current RandR layout.
  RandRMonitorLayout GetLayout();
  // Adjusts outputs to match the specified layout.
  void SetLayout(const RandRMonitorLayout& layout);

  // Removes the existing mode from the output and replaces it with the new
  // size. Returns the new mode ID, or None (0) on failure.
  x11::RandR::Mode UpdateMode(x11::RandR::Output output, int width, int height);
  // Remove the specified mode from the output, and delete it. If the mode is in
  // use, it is not deleted.
  // |name| should be set to GetModeNameForOutput(output). The parameter is to
  // avoid creating the mode name twice.
  void DeleteMode(x11::RandR::Output output, const std::string& name);

  // Add a mode matching the specified resolution and switch to it.
  void SetResolutionForOutput(x11::RandR::Output output,
                              const gfx::Size& dimensions,
                              const gfx::Vector2d& dpi);

 private:
  using OutputInfoList = std::vector<
      std::pair<x11::RandR::Output, x11::RandR::GetOutputInfoReply>>;

  OutputInfoList GetDisabledOutputs();

  std::string GetModeNameForOutput(x11::RandR::Output output);

  raw_ptr<x11::Connection> connection_ = nullptr;
  const raw_ptr<x11::RandR> randr_ = nullptr;
  x11::Window root_;
  ScreenResources resources_;
  bool has_randr_;
  std::string output_name_prefix_;
  const uint32_t default_mode_dot_clock_;
};

}  // namespace x11

#endif  // UI_GFX_X_RANDR_OUTPUT_MANAGER_H_