File: x11_pointer_grab.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 (137 lines) | stat: -rw-r--r-- 4,715 bytes parent folder | download | duplicates (7)
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
// Copyright 2015 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/base/x/x11_pointer_grab.h"

#include "base/cancelable_callback.h"
#include "base/check.h"
#include "base/functional/bind.h"
#include "base/no_destructor.h"
#include "ui/base/x/x11_util.h"
#include "ui/events/devices/x11/device_data_manager_x11.h"
#include "ui/gfx/x/connection.h"
#include "ui/gfx/x/xinput.h"
#include "ui/gfx/x/xproto.h"

namespace ui {

namespace {

// The grab window. None if there are no active pointer grabs.
x11::Window g_grab_window = x11::Window::None;

// The "owner events" parameter used to grab the pointer.
bool g_owner_events = false;

base::CancelableOnceCallback<void(x11::Cursor)>& GetGrabCallback() {
  static base::NoDestructor<base::CancelableOnceCallback<void(x11::Cursor)>>
      callback;
  return *callback;
}

x11::GrabStatus GrabPointerImpl(x11::Window window,
                                bool owner_events,
                                x11::Cursor cursor) {
  GetGrabCallback().Cancel();
  auto result = x11::GrabStatus::InvalidTime;
  auto* connection = x11::Connection::Get();
  if (ui::IsXInput2Available()) {
    // Do an xinput pointer grab. If there is an active xinput pointer grab
    // as a result of normal button press, GrabPointer() will fail.
    auto mask = x11::Input::XIEventMask::ButtonPress |
                x11::Input::XIEventMask::ButtonRelease |
                x11::Input::XIEventMask::Motion |
                x11::Input::XIEventMask::TouchBegin |
                x11::Input::XIEventMask::TouchUpdate |
                x11::Input::XIEventMask::TouchEnd;
    static_assert(sizeof(mask) == 4);

    for (auto master_pointer :
         ui::DeviceDataManagerX11::GetInstance()->master_pointers()) {
      x11::Input::XIGrabDeviceRequest req{
          .window = window,
          .time = x11::Time::CurrentTime,
          .cursor = cursor,
          .deviceid = master_pointer,
          .mode = x11::GrabMode::Async,
          .paired_device_mode = x11::GrabMode::Async,
          .owner_events = owner_events ? x11::Input::GrabOwner::Owner
                                       : x11::Input::GrabOwner::NoOwner,
          .mask = {static_cast<uint32_t>(mask)},
      };
      if (auto reply = connection->xinput().XIGrabDevice(req).Sync())
        result = reply->status;

      // Assume that the grab will succeed on either all or none of the master
      // pointers.
      if (result != x11::GrabStatus::Success) {
        // Try core pointer grab.
        break;
      }
    }
  }

  if (result != x11::GrabStatus::Success) {
    auto mask = x11::EventMask::PointerMotion | x11::EventMask::ButtonRelease |
                x11::EventMask::ButtonPress;
    x11::GrabPointerRequest req{
        .owner_events = owner_events,
        .grab_window = window,
        .event_mask = mask,
        .pointer_mode = x11::GrabMode::Async,
        .keyboard_mode = x11::GrabMode::Async,
        .confine_to = x11::Window::None,
        .cursor = cursor,
        .time = x11::Time::CurrentTime,
    };
    if (auto reply = connection->GrabPointer(req).Sync())
      result = reply->status;
  }

  if (result == x11::GrabStatus::Success) {
    g_grab_window = window;
    g_owner_events = owner_events;
  }
  return result;
}

}  // namespace

x11::GrabStatus GrabPointer(x11::Window window,
                            bool owner_events,
                            scoped_refptr<ui::X11Cursor> cursor) {
  if (!cursor)
    return GrabPointerImpl(window, owner_events, x11::Cursor::None);
  if (cursor->loaded())
    return GrabPointerImpl(window, owner_events, cursor->xcursor());

  auto result = GrabPointerImpl(window, owner_events, x11::Cursor::None);
  GetGrabCallback().Reset(base::BindOnce(base::IgnoreResult(GrabPointerImpl),
                                         window, owner_events));
  cursor->OnCursorLoaded(GetGrabCallback().callback());
  return result;
}

void ChangeActivePointerGrabCursor(scoped_refptr<ui::X11Cursor> cursor) {
  if (g_grab_window != x11::Window::None)
    GrabPointer(g_grab_window, g_owner_events, cursor);
}

void UngrabPointer() {
  GetGrabCallback().Cancel();
  g_grab_window = x11::Window::None;
  auto* connection = x11::Connection::Get();
  if (ui::IsXInput2Available()) {
    for (auto master_pointer :
         ui::DeviceDataManagerX11::GetInstance()->master_pointers()) {
      connection->xinput()
          .XIUngrabDevice({x11::Time::CurrentTime, master_pointer})
          .IgnoreError();
    }
  }
  // Try core pointer ungrab in case the XInput2 pointer ungrab failed.
  connection->UngrabPointer().IgnoreError();
}

}  // namespace ui