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
|
// Copyright 2021 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/wayland_zwp_relative_pointer_manager.h"
#include <relative-pointer-unstable-v1-client-protocol.h>
#include "base/logging.h"
#include "build/buildflag.h"
#include "ui/ozone/platform/wayland/host/wayland_connection.h"
#include "ui/ozone/platform/wayland/host/wayland_event_source.h"
#include "ui/ozone/platform/wayland/host/wayland_pointer.h"
#include "ui/ozone/platform/wayland/host/wayland_seat.h"
namespace ui {
namespace {
constexpr uint32_t kMinVersion = 1;
}
// static
constexpr char WaylandZwpRelativePointerManager::kInterfaceName[];
// static
void WaylandZwpRelativePointerManager::Instantiate(
WaylandConnection* connection,
wl_registry* registry,
uint32_t name,
const std::string& interface,
uint32_t version) {
CHECK_EQ(interface, kInterfaceName) << "Expected \"" << kInterfaceName
<< "\" but got \"" << interface << "\"";
if (connection->zwp_relative_pointer_manager_ ||
!wl::CanBind(interface, version, kMinVersion, kMinVersion)) {
return;
}
auto new_pointer_manager =
wl::Bind<zwp_relative_pointer_manager_v1>(registry, name, kMinVersion);
if (!new_pointer_manager) {
LOG(ERROR) << "Failed to bind zwp_relative_pointer_manager_v1";
return;
}
connection->zwp_relative_pointer_manager_ =
std::make_unique<WaylandZwpRelativePointerManager>(
new_pointer_manager.release(), connection);
}
WaylandZwpRelativePointerManager::WaylandZwpRelativePointerManager(
zwp_relative_pointer_manager_v1* relative_pointer_manager,
WaylandConnection* connection)
: obj_(relative_pointer_manager),
connection_(connection),
delegate_(connection_->event_source()) {
DCHECK(obj_);
DCHECK(connection_);
DCHECK(delegate_);
}
WaylandZwpRelativePointerManager::~WaylandZwpRelativePointerManager() = default;
void WaylandZwpRelativePointerManager::EnableRelativePointer() {
relative_pointer_.reset(zwp_relative_pointer_manager_v1_get_relative_pointer(
obj_.get(), connection_->seat()->pointer()->wl_object()));
static constexpr zwp_relative_pointer_v1_listener kRelativePointerListener = {
.relative_motion = &OnRelativeMotion,
};
zwp_relative_pointer_v1_add_listener(relative_pointer_.get(),
&kRelativePointerListener, this);
delegate_->SetRelativePointerMotionEnabled(true);
}
void WaylandZwpRelativePointerManager::DisableRelativePointer() {
relative_pointer_.reset();
delegate_->SetRelativePointerMotionEnabled(false);
}
// static
void WaylandZwpRelativePointerManager::OnRelativeMotion(
void* data,
zwp_relative_pointer_v1* pointer,
uint32_t utime_hi,
uint32_t utime_lo,
wl_fixed_t dx,
wl_fixed_t dy,
wl_fixed_t dx_unaccel,
wl_fixed_t dy_unaccel) {
auto* self = static_cast<WaylandZwpRelativePointerManager*>(data);
gfx::Vector2dF delta = {static_cast<float>(wl_fixed_to_double(dx)),
static_cast<float>(wl_fixed_to_double(dy))};
gfx::Vector2dF delta_unaccel = {
static_cast<float>(wl_fixed_to_double(dx_unaccel)),
static_cast<float>(wl_fixed_to_double(dy_unaccel))};
self->delegate_->OnRelativePointerMotion(
delta, wl::EventMillisecondsToTimeTicks(utime_lo));
}
} // namespace ui
|