File: xdg_popup.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 (363 lines) | stat: -rw-r--r-- 12,957 bytes parent folder | download | duplicates (2)
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
// 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/xdg_popup.h"

#include <xdg-shell-client-protocol.h>

#include <memory>

#include "base/command_line.h"
#include "base/environment.h"
#include "base/logging.h"
#include "base/nix/xdg_util.h"
#include "ui/base/ui_base_types.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/ozone/platform/wayland/common/wayland_util.h"
#include "ui/ozone/platform/wayland/host/wayland_connection.h"
#include "ui/ozone/platform/wayland/host/wayland_popup.h"
#include "ui/ozone/platform/wayland/host/wayland_seat.h"
#include "ui/ozone/platform/wayland/host/wayland_serial_tracker.h"
#include "ui/ozone/platform/wayland/host/wayland_toplevel_window.h"
#include "ui/ozone/platform/wayland/host/wayland_window.h"
#include "ui/ozone/platform/wayland/host/xdg_toplevel.h"
#include "ui/ozone/public/ozone_switches.h"

namespace ui {

namespace {

uint32_t TranslateAnchor(OwnedWindowAnchorPosition anchor) {
  switch (anchor) {
    case OwnedWindowAnchorPosition::kNone:
      return XDG_POSITIONER_ANCHOR_NONE;
    case OwnedWindowAnchorPosition::kTop:
      return XDG_POSITIONER_ANCHOR_TOP;
    case OwnedWindowAnchorPosition::kBottom:
      return XDG_POSITIONER_ANCHOR_BOTTOM;
    case OwnedWindowAnchorPosition::kLeft:
      return XDG_POSITIONER_ANCHOR_LEFT;
    case OwnedWindowAnchorPosition::kRight:
      return XDG_POSITIONER_ANCHOR_RIGHT;
    case OwnedWindowAnchorPosition::kTopLeft:
      return XDG_POSITIONER_ANCHOR_TOP_LEFT;
    case OwnedWindowAnchorPosition::kBottomLeft:
      return XDG_POSITIONER_ANCHOR_BOTTOM_LEFT;
    case OwnedWindowAnchorPosition::kTopRight:
      return XDG_POSITIONER_ANCHOR_TOP_RIGHT;
    case OwnedWindowAnchorPosition::kBottomRight:
      return XDG_POSITIONER_ANCHOR_BOTTOM_RIGHT;
  }
}

uint32_t TranslateGravity(OwnedWindowAnchorGravity gravity) {
  switch (gravity) {
    case OwnedWindowAnchorGravity::kNone:
      return XDG_POSITIONER_GRAVITY_NONE;
    case OwnedWindowAnchorGravity::kTop:
      return XDG_POSITIONER_GRAVITY_TOP;
    case OwnedWindowAnchorGravity::kBottom:
      return XDG_POSITIONER_GRAVITY_BOTTOM;
    case OwnedWindowAnchorGravity::kLeft:
      return XDG_POSITIONER_GRAVITY_LEFT;
    case OwnedWindowAnchorGravity::kRight:
      return XDG_POSITIONER_GRAVITY_RIGHT;
    case OwnedWindowAnchorGravity::kTopLeft:
      return XDG_POSITIONER_GRAVITY_TOP_LEFT;
    case OwnedWindowAnchorGravity::kBottomLeft:
      return XDG_POSITIONER_GRAVITY_BOTTOM_LEFT;
    case OwnedWindowAnchorGravity::kTopRight:
      return XDG_POSITIONER_GRAVITY_TOP_RIGHT;
    case OwnedWindowAnchorGravity::kBottomRight:
      return XDG_POSITIONER_GRAVITY_BOTTOM_RIGHT;
  }
}

uint32_t TranslateConstraintAdjustment(
    OwnedWindowConstraintAdjustment constraint_adjustment) {
  uint32_t res = 0;
  if ((constraint_adjustment &
       OwnedWindowConstraintAdjustment::kAdjustmentSlideX) !=
      OwnedWindowConstraintAdjustment::kAdjustmentNone) {
    res |= XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_SLIDE_X;
  }
  if ((constraint_adjustment &
       OwnedWindowConstraintAdjustment::kAdjustmentSlideY) !=
      OwnedWindowConstraintAdjustment::kAdjustmentNone) {
    res |= XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_SLIDE_Y;
  }
  if ((constraint_adjustment &
       OwnedWindowConstraintAdjustment::kAdjustmentFlipX) !=
      OwnedWindowConstraintAdjustment::kAdjustmentNone) {
    res |= XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_FLIP_X;
  }
  if ((constraint_adjustment &
       OwnedWindowConstraintAdjustment::kAdjustmentFlipY) !=
      OwnedWindowConstraintAdjustment::kAdjustmentNone) {
    res |= XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_FLIP_Y;
  }
  if ((constraint_adjustment &
       OwnedWindowConstraintAdjustment::kAdjustmentResizeX) !=
      OwnedWindowConstraintAdjustment::kAdjustmentNone) {
    res |= XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_RESIZE_X;
  }
  if ((constraint_adjustment &
       OwnedWindowConstraintAdjustment::kAdjustmentRezizeY) !=
      OwnedWindowConstraintAdjustment::kAdjustmentNone) {
    res |= XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_RESIZE_Y;
  }
  return res;
}

bool IsGnomeShell() {
  auto env = base::Environment::Create();
  return base::nix::GetDesktopEnvironment(env.get()) ==
         base::nix::DESKTOP_ENVIRONMENT_GNOME;
}

}  // namespace

XdgPopup::XdgPopup(std::unique_ptr<XdgSurface> xdg_surface)
    : xdg_surface_(std::move(xdg_surface)) {
  CHECK(xdg_surface_);
  CHECK(window() && window()->parent_window());
}

XdgPopup::~XdgPopup() = default;

bool XdgPopup::Initialize(const InitParams& params) {
  auto* xdg_parent = window()->AsWaylandPopup()->GetXdgParentWindow();
  if (!xdg_parent) {
    NOTREACHED() << "xdg_popup does not have a valid parent xdg_surface";
  }

  struct xdg_surface* parent_xdg_surface = nullptr;
  // If the xdg_parent window is a popup, the surface of that popup must be used
  // as a parent to create this xdg_popup.
  if (auto* parent_popup = xdg_parent->AsWaylandPopup()) {
    parent_xdg_surface = parent_popup->xdg_popup()->xdg_surface();
  } else if (auto* parent_toplevel = xdg_parent->AsWaylandToplevelWindow()) {
    parent_xdg_surface = parent_toplevel->xdg_toplevel()->xdg_surface();
  }

  CHECK(xdg_surface_ && parent_xdg_surface);

  if (!xdg_surface_ || !parent_xdg_surface) {
    return false;
  }

  params_ = params;
  // Wayland doesn't allow empty bounds. If a zero or negative size is set, the
  // invalid_input error is raised. Thus, use the least possible one.
  // WaylandPopup will update its bounds upon the following configure event.
  if (params_.bounds.IsEmpty()) {
    params_.bounds.set_size({1, 1});
  }

  auto positioner = CreatePositioner();
  if (!positioner) {
    return false;
  }

  xdg_popup_.reset(xdg_surface_get_popup(xdg_surface(), parent_xdg_surface,
                                         positioner.get()));
  if (!xdg_popup_) {
    return false;
  }
  connection()->window_manager()->NotifyWindowRoleAssigned(window());

  std::optional<bool> parent_xdg_popup_has_grab;
  if (auto* parent_popup = xdg_parent->AsWaylandPopup()) {
    parent_xdg_popup_has_grab.emplace(parent_popup->xdg_popup()->has_grab());
  }
  GrabIfPossible(connection(), parent_xdg_popup_has_grab);

  static constexpr xdg_popup_listener kXdgPopupListener = {
      .configure = &OnConfigure,
      .popup_done = &OnPopupDone,
      .repositioned = &OnRepositioned,
  };
  xdg_popup_add_listener(xdg_popup_.get(), &kXdgPopupListener, this);

  window()->root_surface()->Commit();
  return true;
}

void XdgPopup::AckConfigure(uint32_t serial) {
  DCHECK(xdg_surface_);
  xdg_surface_->AckConfigure(serial);
}

bool XdgPopup::IsConfigured() {
  DCHECK(xdg_surface_);
  return xdg_surface_->IsConfigured();
}

bool XdgPopup::SetBounds(const gfx::Rect& new_bounds) {
  if (xdg_popup_get_version(xdg_popup_.get()) <
      XDG_POPUP_REPOSITIONED_SINCE_VERSION) {
    return false;
  }

  params_.bounds = new_bounds;

  // Create a new positioner with new bounds.
  auto positioner = CreatePositioner();
  if (!positioner) {
    return false;
  }

  // TODO(msisov): figure out how we can make use of the reposition token.
  // The protocol says the token itself is opaque, and has no other special
  // meaning.
  xdg_popup_reposition(xdg_popup_.get(), positioner.get(),
                       ++next_reposition_token_);

  connection()->Flush();
  return true;
}

void XdgPopup::SetWindowGeometry(const gfx::Rect& bounds) {
  xdg_surface_set_window_geometry(xdg_surface(), bounds.x(), bounds.y(),
                                  bounds.width(), bounds.height());
}

void XdgPopup::Grab(uint32_t serial) {
  xdg_popup_grab(xdg_popup_.get(), connection()->seat()->wl_object(), serial);
}

void XdgPopup::FillAnchorData(
    const InitParams& params,
    gfx::Rect* anchor_rect,
    OwnedWindowAnchorPosition* anchor_position,
    OwnedWindowAnchorGravity* anchor_gravity,
    OwnedWindowConstraintAdjustment* constraints) const {
  DCHECK(anchor_rect && anchor_position && anchor_gravity && constraints);
  if (params.anchor.has_value()) {
    *anchor_rect = params.anchor->anchor_rect;
    *anchor_position = params.anchor->anchor_position;
    *anchor_gravity = params.anchor->anchor_gravity;
    *constraints = params.anchor->constraint_adjustment;
    return;
  }

  // Use default parameters if params.anchor doesn't have any data.
  *anchor_rect = params.bounds;
  anchor_rect->set_size({1, 1});
  *anchor_position = OwnedWindowAnchorPosition::kTopLeft;
  *anchor_gravity = OwnedWindowAnchorGravity::kBottomRight;
  *constraints = OwnedWindowConstraintAdjustment::kAdjustmentFlipY;
}

wl::Object<xdg_positioner> XdgPopup::CreatePositioner() {
  wl::Object<xdg_positioner> positioner(
      xdg_wm_base_create_positioner(connection()->shell()));
  if (!positioner) {
    return {};
  }

  gfx::Rect anchor_rect;
  OwnedWindowAnchorPosition anchor_position;
  OwnedWindowAnchorGravity anchor_gravity;
  OwnedWindowConstraintAdjustment constraint_adjustment;
  FillAnchorData(params_, &anchor_rect, &anchor_position, &anchor_gravity,
                 &constraint_adjustment);

  // XDG protocol does not allow empty geometries, but Chrome does. Set a dummy
  // {1, 1} size to prevent protocol error.
  if (anchor_rect.IsEmpty()) {
    anchor_rect.set_size({1, 1});
  }
  xdg_positioner_set_anchor_rect(positioner.get(), anchor_rect.x(),
                                 anchor_rect.y(), anchor_rect.width(),
                                 anchor_rect.height());
  // XDG protocol does not allow empty geometries, but Chrome does. Set a dummy
  // {1, 1} size to prevent protocol error.
  if (params_.bounds.IsEmpty()) {
    params_.bounds.set_size({1, 1});
  }
  xdg_positioner_set_size(positioner.get(), params_.bounds.width(),
                          params_.bounds.height());
  xdg_positioner_set_anchor(positioner.get(), TranslateAnchor(anchor_position));
  xdg_positioner_set_gravity(positioner.get(),
                             TranslateGravity(anchor_gravity));
  xdg_positioner_set_constraint_adjustment(
      positioner.get(), TranslateConstraintAdjustment(constraint_adjustment));
  return positioner;
}

void XdgPopup::GrabIfPossible(WaylandConnection* connection,
                              std::optional<bool> parent_shell_popup_has_grab) {
  base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess();
  if (!cmd_line->HasSwitch(switches::kUseWaylandExplicitGrab)) {
    return;
  }

  // When drag process starts, as described the protocol -
  // https://goo.gl/1Mskq3, the client must have an active implicit grab. If
  // we try to create a popup and grab it, it will be immediately dismissed.
  // Thus, do not take explicit grab during drag process.
  if (connection->IsDragInProgress() || !connection->seat()) {
    return;
  }

  // According to the definition of the xdg protocol, the grab request must be
  // used in response to some sort of user action like a button press, key
  // press, or touch down event.
  auto serial = connection->serial_tracker().GetSerial(
      {wl::SerialType::kTouchPress, wl::SerialType::kMousePress,
       wl::SerialType::kKeyPress});
  if (!serial.has_value()) {
    return;
  }

  // The parent of a grabbing popup must either be an xdg_toplevel surface or
  // another xdg_popup with an explicit grab. If it is a popup that did not take
  // an explicit grab, an error will be raised, so early out if that's the case.
  if (!parent_shell_popup_has_grab.value_or(true)) {
    return;
  }

  if (serial->type == wl::SerialType::kTouchPress && IsGnomeShell()) {
    return;
  }

  Grab(serial->value);
  has_grab_ = true;
}

// static
void XdgPopup::OnConfigure(void* data,
                           xdg_popup* popup,
                           int32_t x,
                           int32_t y,
                           int32_t width,
                           int32_t height) {
  // As long as the Wayland compositor repositions/requires to position windows
  // relative to their parents, do not propagate final bounds information to
  // Chromium. The browser places windows in respect to screen origin, but
  // Wayland requires doing so in respect to parent window's origin. To properly
  // place windows, the bounds are translated and adjusted according to the
  // Wayland compositor needs during WaylandWindow::CreateXdgPopup call.
  auto* self = static_cast<XdgPopup*>(data);
  WaylandWindow* window = self->window();
  DCHECK(window);
  window->HandlePopupConfigure({x, y, width, height});
}

// static
void XdgPopup::OnPopupDone(void* data, xdg_popup* popup) {
  auto* self = static_cast<XdgPopup*>(data);
  WaylandWindow* window = self->window();
  DCHECK(window);
  window->Hide();
  window->OnCloseRequest();
}

// static
void XdgPopup::OnRepositioned(void* data, xdg_popup* popup, uint32_t token) {
  NOTIMPLEMENTED_LOG_ONCE();
}

}  // namespace ui