File: x11_whole_screen_move_loop.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 (281 lines) | stat: -rw-r--r-- 9,285 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
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
// Copyright 2012 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_whole_screen_move_loop.h"

#include <stddef.h>

#include <memory>
#include <utility>

#include "base/functional/bind.h"
#include "base/logging.h"
#include "base/run_loop.h"
#include "base/task/current_thread.h"
#include "base/task/single_thread_task_runner.h"
#include "ui/base/x/x11_pointer_grab.h"
#include "ui/base/x/x11_util.h"
#include "ui/events/event.h"
#include "ui/events/event_utils.h"
#include "ui/events/keycodes/keyboard_code_conversion_x.h"
#include "ui/events/platform/platform_event_source.h"
#include "ui/events/platform/scoped_event_dispatcher.h"
#include "ui/events/x/events_x_utils.h"
#include "ui/events/x/x11_event_translation.h"
#include "ui/gfx/x/connection.h"
#include "ui/gfx/x/keysyms/keysyms.h"
#include "ui/gfx/x/window_event_manager.h"
#include "ui/gfx/x/xproto.h"

namespace ui {

namespace {

// XGrabKey requires the modifier mask to explicitly be specified.
constexpr x11::ModMask kModifiersMasks[] = {
    {},                  // No additional modifier.
    x11::ModMask::c_2,   // Num lock
    x11::ModMask::Lock,  // Caps lock
    x11::ModMask::c_5,   // Scroll lock
    x11::ModMask::c_2 | x11::ModMask::Lock,
    x11::ModMask::c_2 | x11::ModMask::c_5,
    x11::ModMask::Lock | x11::ModMask::c_5,
    x11::ModMask::c_2 | x11::ModMask::Lock | x11::ModMask::c_5,
};

const char* GrabStatusToString(x11::GrabStatus grab_status) {
  switch (grab_status) {
    case x11::GrabStatus::Success:
      return "Success";
    case x11::GrabStatus::AlreadyGrabbed:
      return "AlreadyGrabbed";
    case x11::GrabStatus::InvalidTime:
      return "InvalidTime";
    case x11::GrabStatus::NotViewable:
      return "NotViewable";
    case x11::GrabStatus::Frozen:
      return "Frozen";
  }
  NOTREACHED();
}

}  // namespace

X11WholeScreenMoveLoop::X11WholeScreenMoveLoop(X11MoveLoopDelegate* delegate)
    : delegate_(delegate),
      in_move_loop_(false),
      grab_input_window_(x11::Window::None),
      grabbed_pointer_(false),
      canceled_(false) {}

X11WholeScreenMoveLoop::~X11WholeScreenMoveLoop() {
  EndMoveLoop();
}

void X11WholeScreenMoveLoop::PostDispatchIfNeeded(const ui::MouseEvent& event) {
}

////////////////////////////////////////////////////////////////////////////////
// DesktopWindowTreeHostLinux, ui::PlatformEventDispatcher implementation:

bool X11WholeScreenMoveLoop::CanDispatchEvent(const ui::PlatformEvent& event) {
  return in_move_loop_;
}

uint32_t X11WholeScreenMoveLoop::DispatchEvent(const ui::PlatformEvent& event) {
  DCHECK(base::CurrentUIThread::IsSet());

  // This method processes all events while the move loop is active.
  if (!in_move_loop_) {
    return ui::POST_DISPATCH_PERFORM_DEFAULT;
  }

  switch (event->type()) {
    case ui::EventType::kMouseMoved:
    case ui::EventType::kMouseDragged: {
      auto& current_xevent = *x11::Connection::Get()->dispatching_event();
      x11::Event last_xevent;
      std::unique_ptr<ui::Event> last_motion;
      auto* mouse_event = event->AsMouseEvent();
      if ((current_xevent.As<x11::MotionNotifyEvent>() ||
           current_xevent.As<x11::Input::DeviceEvent>()) &&
          ui::CoalescePendingMotionEvents(current_xevent, &last_xevent)) {
        last_motion = ui::BuildEventFromXEvent(last_xevent);
        mouse_event = last_motion->AsMouseEvent();
      }
      delegate_->OnMouseMovement(mouse_event->root_location(),
                                 mouse_event->flags(),
                                 mouse_event->time_stamp());
      return ui::POST_DISPATCH_NONE;
    }
    case ui::EventType::kMouseReleased: {
      if (event->AsMouseEvent()->IsLeftMouseButton()) {
        // Assume that drags are being done with the left mouse button. Only
        // break the drag if the left mouse button was released.
        delegate_->OnMouseReleased();

        if (!grabbed_pointer_) {
          // If the source widget had capture prior to the move loop starting,
          // it may be relying on views::Widget getting the mouse release and
          // releasing capture in Widget::OnMouseEvent().
          return ui::POST_DISPATCH_PERFORM_DEFAULT;
        }
      }
      return ui::POST_DISPATCH_NONE;
    }
    case ui::EventType::kKeyPressed:
      if (event->AsKeyEvent()->key_code() == ui::VKEY_ESCAPE) {
        canceled_ = true;
        EndMoveLoop();
        return ui::POST_DISPATCH_NONE;
      }
      break;
    default:
      break;
  }
  return ui::POST_DISPATCH_PERFORM_DEFAULT;
}

bool X11WholeScreenMoveLoop::RunMoveLoop(
    bool can_grab_pointer,
    scoped_refptr<ui::X11Cursor> old_cursor,
    scoped_refptr<ui::X11Cursor> new_cursor,
    base::OnceClosure started_callback) {
  DCHECK(!in_move_loop_);  // Can only handle one nested loop at a time.

  // Query the mouse cursor prior to the move loop starting so that it can be
  // restored when the move loop finishes.
  initial_cursor_ = old_cursor;

  auto* connection = x11::Connection::Get();
  CreateDragInputWindow(connection);

  // Only grab mouse capture of |grab_input_window_| if |can_grab_pointer| is
  // true aka the source that initiated the move loop doesn't have explicit
  // grab.
  // - The caller may intend to transfer capture to a different X11Window
  //   when the move loop ends and not release capture.
  // - Releasing capture and X window destruction are both asynchronous. We drop
  //   events targeted at |grab_input_window_| in the time between the move
  //   loop ends and |grab_input_window_| loses capture.
  grabbed_pointer_ = false;
  if (can_grab_pointer) {
    grabbed_pointer_ = GrabPointer(new_cursor);
    if (!grabbed_pointer_) {
      x11::Connection::Get()->DestroyWindow({grab_input_window_});
      return false;
    }
  }

  GrabEscKey();

  std::unique_ptr<ui::ScopedEventDispatcher> old_dispatcher =
      std::move(nested_dispatcher_);
  nested_dispatcher_ =
      ui::PlatformEventSource::GetInstance()->OverrideDispatcher(this);

  std::move(started_callback).Run();

  base::WeakPtr<X11WholeScreenMoveLoop> alive(weak_factory_.GetWeakPtr());

  in_move_loop_ = true;
  canceled_ = false;
  base::RunLoop run_loop(base::RunLoop::Type::kNestableTasksAllowed);
  quit_closure_ = run_loop.QuitClosure();
  run_loop.Run();

  if (!alive) {
    return false;
  }

  nested_dispatcher_ = std::move(old_dispatcher);
  return !canceled_;
}

void X11WholeScreenMoveLoop::UpdateCursor(scoped_refptr<ui::X11Cursor> cursor) {
  if (in_move_loop_) {
    ui::ChangeActivePointerGrabCursor(cursor);
  }
}

void X11WholeScreenMoveLoop::EndMoveLoop() {
  if (!in_move_loop_) {
    return;
  }

  // TODO(erg): Is this ungrab the cause of having to click to give input focus
  // on drawn out windows? Not ungrabbing here screws the X server until I kill
  // the chrome process.

  // Ungrab before we let go of the window.
  if (grabbed_pointer_) {
    ui::UngrabPointer();
  } else {
    UpdateCursor(initial_cursor_);
  }

  auto* connection = x11::Connection::Get();
  auto esc_keycode = connection->KeysymToKeycode(XK_Escape);
  for (auto mask : kModifiersMasks) {
    connection->UngrabKey({esc_keycode, grab_input_window_, mask});
  }

  // Restore the previous dispatcher.
  nested_dispatcher_.reset();
  delegate_->OnMoveLoopEnded();
  grab_input_window_events_.Reset();
  connection->DestroyWindow({grab_input_window_});
  grab_input_window_ = x11::Window::None;

  in_move_loop_ = false;
  std::move(quit_closure_).Run();
}

bool X11WholeScreenMoveLoop::GrabPointer(scoped_refptr<X11Cursor> cursor) {
  auto* connection = x11::Connection::Get();

  // Pass "owner_events" as false so that X sends all mouse events to
  // |grab_input_window_|.
  auto ret = ui::GrabPointer(grab_input_window_, false, cursor);
  if (ret != x11::GrabStatus::Success) {
    DLOG(ERROR) << "Grabbing pointer for dragging failed: "
                << GrabStatusToString(ret);
  }
  connection->Flush();
  return ret == x11::GrabStatus::Success;
}

void X11WholeScreenMoveLoop::GrabEscKey() {
  auto* connection = x11::Connection::Get();
  auto esc_keycode = connection->KeysymToKeycode(XK_Escape);
  for (auto mask : kModifiersMasks) {
    connection->GrabKey({false, grab_input_window_, mask, esc_keycode,
                         x11::GrabMode::Async, x11::GrabMode::Async});
  }
}

void X11WholeScreenMoveLoop::CreateDragInputWindow(
    x11::Connection* connection) {
  grab_input_window_ = connection->GenerateId<x11::Window>();
  connection->CreateWindow(x11::CreateWindowRequest{
      .wid = grab_input_window_,
      .parent = connection->default_root(),
      .x = -100,
      .y = -100,
      .width = 10,
      .height = 10,
      .c_class = x11::WindowClass::InputOnly,
      .override_redirect = x11::Bool32(true),
  });
  auto event_mask =
      x11::EventMask::ButtonPress | x11::EventMask::ButtonRelease |
      x11::EventMask::PointerMotion | x11::EventMask::KeyPress |
      x11::EventMask::KeyRelease | x11::EventMask::StructureNotify;
  grab_input_window_events_ =
      connection->ScopedSelectEvent(grab_input_window_, event_mask);
  connection->MapWindow(grab_input_window_);
  connection->RaiseWindow(grab_input_window_);
}

}  // namespace ui