File: zcr_ui_controls.cc

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,122,156 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (339 lines) | stat: -rw-r--r-- 13,828 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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/exo/wayland/zcr_ui_controls.h"

#include <stdint.h>
#include <ui-controls-unstable-v1-server-protocol.h>
#include <wayland-server-core.h>

#include <limits>
#include <variant>

#include "ash/display/screen_orientation_controller_test_api.h"
#include "ash/shell.h"
#include "base/bit_cast.h"
#include "base/logging.h"
#include "base/memory/raw_ptr.h"
#include "base/notreached.h"
#include "base/strings/stringprintf.h"
#include "base/test/bind.h"
#include "components/exo/display.h"
#include "components/exo/wayland/server.h"
#include "components/exo/wayland/server_util.h"
#include "ui/aura/env.h"
#include "ui/aura/env_input_state_controller.h"
#include "ui/base/test/ui_controls.h"
#include "ui/base/wayland/wayland_display_util.h"
#include "ui/display/manager/managed_display_info.h"
#include "ui/display/test/display_manager_test_api.h"
#include "ui/display/types/display_constants.h"
#include "ui/events/event_constants.h"
#include "ui/events/gesture_detection/gesture_configuration.h"
#include "ui/events/keycodes/dom/dom_code.h"
#include "ui/events/keycodes/dom/keycode_converter.h"
#include "ui/events/keycodes/keyboard_code_conversion.h"

namespace exo::wayland {

struct UiControls::UiControlsState {
  explicit UiControlsState(Server* server, const Seat* seat)
      : server_(server), seat_(seat) {}
  UiControlsState(const UiControlsState&) = delete;
  UiControlsState& operator=(const UiControlsState&) = delete;

  raw_ptr<Server> server_;
  const raw_ptr<const Seat> seat_;

  // Keeps track of the IDs of pending requests for that we still need to emit
  // request_processed events. This is per wl_resource so that we can drop
  // pending requests for a resource when the resource is destroyed.
  std::map<wl_resource*, std::set<uint32_t>> pending_request_ids_;

  // Keeps track of the original display spec to be restored on destroy.
  std::vector<display::ManagedDisplayInfo> original_displays_;
  // Pending display info to be added with display_info_done.
  std::optional<display::ManagedDisplayInfo> pending_display_;
  // Pending display info lists to be committed with display_info_list_done.
  std::vector<display::ManagedDisplayInfo> pending_display_info_list_;
};

namespace {

using UiControlsState = UiControls::UiControlsState;

constexpr uint32_t kUiControlsVersion =
    ZCR_UI_CONTROLS_V1_DISPLAY_INFO_LIST_DONE_SINCE_VERSION;

base::OnceClosure UpdateStateAndBindEmitProcessed(struct wl_resource* resource,
                                                  uint32_t id) {
  auto* state = GetUserDataAs<UiControlsState>(resource);
  auto pending_request_ids =
      state->pending_request_ids_.try_emplace(resource).first->second;
  pending_request_ids.insert(id);

  return base::BindLambdaForTesting([=]() {
    // Ensure |resource| hasn't been destroyed in the meantime.
    if (base::Contains(state->pending_request_ids_, resource)) {
      zcr_ui_controls_v1_send_request_processed(resource, id);
      state->pending_request_ids_[resource].erase(id);

      // It can sometimes happen that the request_processed event gets stuck in
      // libwayland's queue without ever being sent, because the client is
      // waiting for the event and there is nothing else generating events. To
      // ensure the client actually receives the event, we need to flush
      // manually.
      state->server_->Flush();
    }
  });
}

// Ensures that a crashed test doesn't leave behind pressed keys, mouse buttons,
// or touch points.
void ResetInputs(UiControlsState* state) {
  auto* window = ash::Shell::GetPrimaryRootWindow();
  auto pressed_keys = state->seat_->pressed_keys();
  for (auto key : pressed_keys) {
    const ui::DomCode* physical_key = std::get_if<ui::DomCode>(&key.first);
    if (!physical_key) {
      continue;
    }
    auto key_code = ui::DomCodeToUsLayoutNonLocatedKeyboardCode(*physical_key);
    ui_controls::SendKeyEvents(window, key_code, ui_controls::kKeyRelease);
  }

  auto* env = aura::Env::GetInstance();
  int button_flags = env->mouse_button_flags();
  if (button_flags & ui::EF_LEFT_MOUSE_BUTTON) {
    ui_controls::SendMouseEvents(ui_controls::LEFT,
                                 ui_controls::MouseButtonState::UP);
  }
  if (button_flags & ui::EF_MIDDLE_MOUSE_BUTTON) {
    ui_controls::SendMouseEvents(ui_controls::MIDDLE,
                                 ui_controls::MouseButtonState::UP);
  }
  if (button_flags & ui::EF_RIGHT_MOUSE_BUTTON) {
    ui_controls::SendMouseEvents(ui_controls::RIGHT,
                                 ui_controls::MouseButtonState::UP);
  }

  uint32_t touch_ids_down = env->env_controller()->touch_ids_down();
  for (uint32_t touch_id = 0; touch_id < 32; ++touch_id) {
    if (touch_ids_down & (1 << touch_id)) {
      ui_controls::SendTouchEvents(ui_controls::TouchType::kTouchRelease,
                                   touch_id, 0, 0);
    }
  }

  // TODO(crbug.com/40263572): Fix this issue and the code below should not be
  // necessary.
  ui_controls::SendMouseMove(0, 0);
}

// Ensure that the display is returned to the default setting.
void ResetDisplay(UiControlsState* state) {
  display::test::DisplayManagerTestApi(ash::Shell::Get()->display_manager())
      .UpdateDisplayWithDisplayInfoList(state->original_displays_);
  ash::ScreenOrientationControllerTestApi(
      ash::Shell::Get()->screen_orientation_controller())
      .UpdateNaturalOrientation();
  state->pending_display_.reset();
  state->pending_display_info_list_.clear();
}

void ui_controls_send_key_events(struct wl_client* client,
                                 struct wl_resource* resource,
                                 uint32_t key,
                                 uint32_t key_state,
                                 uint32_t pressed_modifiers,
                                 uint32_t id) {
  auto emit_processed = UpdateStateAndBindEmitProcessed(resource, id);
  auto* window = ash::Shell::GetPrimaryRootWindow();
  auto dom_code = ui::KeycodeConverter::EvdevCodeToDomCode(key);
  auto key_code = ui::DomCodeToUsLayoutNonLocatedKeyboardCode(dom_code);
  ui_controls::SendKeyEventsNotifyWhenDone(window, key_code, key_state,
                                           std::move(emit_processed),
                                           pressed_modifiers);
}

void ui_controls_send_mouse_move(struct wl_client* client,
                                 struct wl_resource* resource,
                                 int32_t x,
                                 int32_t y,
                                 struct wl_resource* surface,
                                 uint32_t id) {
  if (surface) {
    LOG(WARNING)
        << "The `surface` parameter for ui_controls.send_mouse_move should be "
           "NULL on LaCrOS, but it isn't. Why aren't we using screen "
           "coordinates?";
  }

  auto emit_processed = UpdateStateAndBindEmitProcessed(resource, id);
  ui_controls::SendMouseMoveNotifyWhenDone(x, y, std::move(emit_processed));
}

void ui_controls_send_mouse_button(struct wl_client* client,
                                   struct wl_resource* resource,
                                   uint32_t button,
                                   uint32_t button_state,
                                   uint32_t pressed_modifiers,
                                   uint32_t id) {
  auto emit_processed = UpdateStateAndBindEmitProcessed(resource, id);
  ui_controls::SendMouseEventsNotifyWhenDone(
      static_cast<ui_controls::MouseButton>(button), button_state,
      std::move(emit_processed), pressed_modifiers);
}

void ui_controls_send_touch(struct wl_client* client,
                            struct wl_resource* resource,
                            uint32_t action,
                            uint32_t touch_id,
                            int32_t x,
                            int32_t y,
                            struct wl_resource* surface,
                            uint32_t id) {
  if (surface) {
    LOG(WARNING)
        << "The `surface` parameter for ui_controls.send_touch should be NULL "
           "on LaCrOS, but it isn't. Why aren't we using screen coordinates?";
  }

  auto emit_processed = UpdateStateAndBindEmitProcessed(resource, id);
  ui_controls::SendTouchEventsNotifyWhenDone(action, touch_id, x, y,
                                             std::move(emit_processed));
}

void ui_controls_set_display_info_id(struct wl_client* client,
                                     struct wl_resource* resource,
                                     uint32_t display_id_hi,
                                     uint32_t display_id_lo) {
  auto* state = GetUserDataAs<UiControlsState>(resource);

  int64_t display_id =
      ui::wayland::FromWaylandDisplayIdPair({display_id_hi, display_id_lo});
  if (!state->pending_display_) {
    state->pending_display_ =
        display::ManagedDisplayInfo::CreateFromSpecWithID({}, display_id);
  } else {
    state->pending_display_->set_display_id(display_id);
  }
}

void ui_controls_set_display_info_size(struct wl_client* client,
                                       struct wl_resource* resource,
                                       uint32_t width,
                                       uint32_t height) {
  auto* state = GetUserDataAs<UiControlsState>(resource);

  if (!state->pending_display_) {
    state->pending_display_ = display::ManagedDisplayInfo::CreateFromSpec(
        base::StringPrintf("%dx%d", width, height));
  } else {
    state->pending_display_->SetBounds(gfx::Rect(width, height));
  }
}

void ui_controls_set_display_info_device_scale_factor(
    struct wl_client* client,
    struct wl_resource* resource,
    uint32_t scale_factor) {
  auto* state = GetUserDataAs<UiControlsState>(resource);
  static_assert(sizeof(uint32_t) == sizeof(float),
                "Sizes much match for reinterpret cast to be meaningful");
  // bit_cast is needed here because wayland doesn't support
  // float as primitive type and we are using 32 bits as storage.
  // static_cast won't work because the original value is integer.
  float device_scale_factor = base::bit_cast<float>(scale_factor);

  if (!state->pending_display_) {
    state->pending_display_ = display::ManagedDisplayInfo::CreateFromSpec({});
  }
  state->pending_display_->set_device_scale_factor(device_scale_factor);
}

void ui_controls_display_info_done(struct wl_client* client,
                                   struct wl_resource* resource) {
  auto* state = GetUserDataAs<UiControlsState>(resource);

  // Push info to pending display info list.
  state->pending_display_info_list_.push_back(*state->pending_display_);

  // Reset the state to default values.
  state->pending_display_.reset();
}

void ui_controls_display_info_list_done(struct wl_client* client,
                                        struct wl_resource* resource,
                                        uint32_t id) {
  auto emit_processed = UpdateStateAndBindEmitProcessed(resource, id);
  auto* state = GetUserDataAs<UiControlsState>(resource);

  display::test::DisplayManagerTestApi(ash::Shell::Get()->display_manager())
      .UpdateDisplayWithDisplayInfoList(state->pending_display_info_list_);
  ash::ScreenOrientationControllerTestApi(
      ash::Shell::Get()->screen_orientation_controller())
      .UpdateNaturalOrientation();
  state->pending_display_info_list_.clear();
  state->pending_display_.reset();

  std::move(emit_processed).Run();
}

const struct zcr_ui_controls_v1_interface ui_controls_implementation = {
    ui_controls_send_key_events,
    ui_controls_send_mouse_move,
    ui_controls_send_mouse_button,
    ui_controls_send_touch,
    ui_controls_set_display_info_id,
    ui_controls_set_display_info_size,
    ui_controls_set_display_info_device_scale_factor,
    ui_controls_display_info_done,
    ui_controls_display_info_list_done};

void destroy_ui_controls_resource(struct wl_resource* resource) {
  auto* state = GetUserDataAs<UiControlsState>(resource);
  state->pending_request_ids_.erase(resource);
  ResetInputs(state);
  ResetDisplay(state);
}

void bind_ui_controls(wl_client* client,
                      void* data,
                      uint32_t version,
                      uint32_t id) {
  wl_resource* resource =
      wl_resource_create(client, &zcr_ui_controls_v1_interface,
                         std::min(version, kUiControlsVersion), id);

  wl_resource_set_implementation(resource, &ui_controls_implementation, data,
                                 destroy_ui_controls_resource);
}

}  // namespace

UiControls::UiControls(Server* server)
    : state_(std::make_unique<UiControlsState>(server,
                                               server->GetDisplay()->seat())) {
  wl_global_create(server->GetWaylandDisplay(), &zcr_ui_controls_v1_interface,
                   kUiControlsVersion, state_.get(), bind_ui_controls);

  auto* const display_manager = ash::Shell::Get()->display_manager();
  auto& display_list = display_manager->active_display_list();
  for (const display::Display& display : display_list) {
    state_->original_displays_.push_back(
        display_manager->GetDisplayInfo(display.id()));
  }
  // TODO(crbug.com/324562919) This hardcodes fling gesture detection to be
  // disabled when ui_controls is in use, so that it does not interfere with
  // tests that don't intend to trigger fling gestures. Some future tests will
  // intentionally trigger fling gestures, so this will need to become
  // configurable by clients at some point.
  ui::GestureConfiguration::GetInstance()->set_min_fling_velocity(
      std::numeric_limits<float>::max());
}

UiControls::~UiControls() = default;

}  // namespace exo::wayland