summaryrefslogtreecommitdiff
path: root/chromium/ui/ozone/platform/wayland/host/xdg_activation.cc
blob: ad964e91cf94740a4a7a5fefe29096edd11bacca (plain)
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
// Copyright 2022 The Chromium Authors. All rights reserved.
// 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_activation.h"

#include <xdg-activation-v1-client-protocol.h>

#include <memory>

#include "base/bind.h"
#include "base/callback.h"
#include "base/check_op.h"
#include "base/logging.h"
#include "ui/ozone/platform/wayland/host/wayland_connection.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_window.h"

namespace ui {

namespace {
constexpr uint32_t kMaxVersion = 1;
}

using ActivationDoneCallback = base::OnceCallback<void(std::string token)>;

// Wraps the actual activation token.
class XdgActivation::Token {
 public:
  Token(wl::Object<xdg_activation_token_v1> token,
        wl_surface* surface,
        wl_seat* seat,
        absl::optional<wl::Serial> serial,
        ActivationDoneCallback callback);
  Token(const Token&) = delete;
  Token& operator=(const Token&) = delete;
  ~Token();

 private:
  static void Done(void* data,
                   struct xdg_activation_token_v1* xdg_activation_token_v1,
                   const char* token);

  wl::Object<xdg_activation_token_v1> token_;

  ActivationDoneCallback callback_;
};

// static
constexpr char XdgActivation::kInterfaceName[];

// static
void XdgActivation::Instantiate(WaylandConnection* connection,
                                wl_registry* registry,
                                uint32_t name,
                                const std::string& interface,
                                uint32_t version) {
  DCHECK_EQ(interface, kInterfaceName);

  if (connection->xdg_activation_)
    return;

  auto instance = wl::Bind<::xdg_activation_v1>(registry, name,
                                                std::min(version, kMaxVersion));
  if (!instance) {
    LOG(ERROR) << "Failed to bind " << kInterfaceName;
    return;
  }
  connection->xdg_activation_ =
      std::make_unique<XdgActivation>(std::move(instance), connection);
}

XdgActivation::XdgActivation(wl::Object<xdg_activation_v1> xdg_activation_v1,
                             WaylandConnection* connection)
    : xdg_activation_v1_(std::move(xdg_activation_v1)),
      connection_(connection) {}

XdgActivation::~XdgActivation() = default;

void XdgActivation::Activate(wl_surface* surface) const {
  const WaylandWindow* const active_window =
      connection_->wayland_window_manager()->GetCurrentActiveWindow();
  if (!active_window) {
    LOG(WARNING) << "Cannot activate a window because no active windows found!";
    return;
  }

  if (token_.get() != nullptr) {
    // TODO(crbug.com/1175327): chain the incoming request and try to serve it
    // after the current one is done.
    LOG(WARNING) << "Another activation request is in progress!";
    return;
  }

  auto* const token =
      xdg_activation_v1_get_activation_token(xdg_activation_v1_.get());
  if (!token) {
    LOG(WARNING) << "Could not get an XDG activation token!";
    return;
  }

  token_ = std::make_unique<Token>(
      wl::Object<xdg_activation_token_v1>(token),
      active_window->root_surface()->surface(),
      connection_->seat()->wl_object(),
      connection_->serial_tracker().GetSerial(
          {wl::SerialType::kTouchPress, wl::SerialType::kMousePress,
           wl::SerialType::kMouseEnter, wl::SerialType::kKeyPress}),
      base::BindOnce(&XdgActivation::OnActivateDone, weak_factory_.GetWeakPtr(),
                     surface));
}

void XdgActivation::OnActivateDone(wl_surface* surface, std::string token) {
  xdg_activation_v1_activate(xdg_activation_v1_.get(), token.c_str(), surface);
  token_.reset();
}

XdgActivation::Token::Token(wl::Object<xdg_activation_token_v1> token,
                            wl_surface* surface,
                            wl_seat* seat,
                            absl::optional<wl::Serial> serial,
                            ActivationDoneCallback callback)
    : token_(std::move(token)), callback_(std::move(callback)) {
  static constexpr xdg_activation_token_v1_listener kListener = {&Done};
  xdg_activation_token_v1_add_listener(token_.get(), &kListener, this);
  xdg_activation_token_v1_set_surface(token_.get(), surface);
  if (serial)
    xdg_activation_token_v1_set_serial(token_.get(), serial->value, seat);
  xdg_activation_token_v1_commit(token_.get());
}

XdgActivation::Token::~Token() = default;

// static
void XdgActivation::Token::Done(
    void* data,
    struct xdg_activation_token_v1* xdg_activation_token_v1,
    const char* token) {
  auto* const self = static_cast<XdgActivation::Token*>(data);
  std::move(self->callback_).Run(token);
}

}  // namespace ui