File: wayland_zwp_pointer_constraints.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 (95 lines) | stat: -rw-r--r-- 3,358 bytes parent folder | download | duplicates (8)
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
// 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_pointer_constraints.h"

#include <pointer-constraints-unstable-v1-client-protocol.h>

#include "base/logging.h"
#include "ui/ozone/platform/wayland/host/wayland_connection.h"
#include "ui/ozone/platform/wayland/host/wayland_pointer.h"
#include "ui/ozone/platform/wayland/host/wayland_seat.h"
#include "ui/ozone/platform/wayland/host/wayland_surface.h"
#include "ui/ozone/platform/wayland/host/wayland_zwp_relative_pointer_manager.h"

namespace ui {

namespace {
constexpr uint32_t kMinVersion = 1;
}

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

// static
void WaylandZwpPointerConstraints::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_pointer_constraints_ ||
      !wl::CanBind(interface, version, kMinVersion, kMinVersion)) {
    return;
  }

  auto zwp_pointer_constraints_v1 =
      wl::Bind<struct zwp_pointer_constraints_v1>(registry, name, kMinVersion);
  if (!zwp_pointer_constraints_v1) {
    LOG(ERROR) << "Failed to bind wp_pointer_constraints_v1";
    return;
  }
  connection->zwp_pointer_constraints_ =
      std::make_unique<WaylandZwpPointerConstraints>(
          zwp_pointer_constraints_v1.release(), connection);
}

WaylandZwpPointerConstraints::WaylandZwpPointerConstraints(
    zwp_pointer_constraints_v1* pointer_constraints,
    WaylandConnection* connection)
    : obj_(pointer_constraints), connection_(connection) {
  DCHECK(obj_);
  DCHECK(connection_);
}

WaylandZwpPointerConstraints::~WaylandZwpPointerConstraints() = default;

void WaylandZwpPointerConstraints::LockPointer(WaylandSurface* surface) {
  locked_pointer_.reset(zwp_pointer_constraints_v1_lock_pointer(
      obj_.get(), surface->surface(),
      connection_->seat()->pointer()->wl_object(), nullptr,
      ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_ONESHOT));

  static constexpr zwp_locked_pointer_v1_listener kLockedPointerListener = {
      .locked = &OnLocked,
      .unlocked = &OnUnlocked,
  };
  zwp_locked_pointer_v1_add_listener(locked_pointer_.get(),
                                     &kLockedPointerListener, this);
}

void WaylandZwpPointerConstraints::UnlockPointer() {
  locked_pointer_.reset();
  connection_->zwp_relative_pointer_manager()->DisableRelativePointer();
}

// static
void WaylandZwpPointerConstraints::OnLocked(
    void* data,
    struct zwp_locked_pointer_v1* locked_pointer) {
  auto* self = static_cast<WaylandZwpPointerConstraints*>(data);
  self->connection_->zwp_relative_pointer_manager()->EnableRelativePointer();
}

// static
void WaylandZwpPointerConstraints::OnUnlocked(
    void* data,
    struct zwp_locked_pointer_v1* locked_pointer) {
  auto* self = static_cast<WaylandZwpPointerConstraints*>(data);
  self->UnlockPointer();
}

}  // namespace ui