File: zwp_text_input_v3.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 (416 lines) | stat: -rw-r--r-- 14,864 bytes parent folder | download | duplicates (2)
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
// Copyright 2024 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/zwp_text_input_v3.h"

#include <string>
#include <utility>

#include "base/logging.h"
#include "base/memory/ptr_util.h"
#include "base/notimplemented.h"
#include "base/numerics/safe_conversions.h"
#include "ui/base/wayland/wayland_client_input_types.h"
#include "ui/gfx/range/range.h"
#include "ui/ozone/platform/wayland/common/wayland_util.h"
#include "ui/ozone/platform/wayland/host/span_style.h"
#include "ui/ozone/platform/wayland/host/wayland_connection.h"
#include "ui/ozone/platform/wayland/host/wayland_seat.h"
#include "ui/ozone/platform/wayland/host/wayland_window.h"

namespace ui {

namespace {

// Converts Chrome's TextInputType into wayland's content_purpose.
// Some of TextInputType values do not have clearly corresponding wayland value,
// and they fallback to closer type.
uint32_t InputTypeToContentPurpose(TextInputType input_type) {
  switch (input_type) {
    case TEXT_INPUT_TYPE_NONE:
      return ZWP_TEXT_INPUT_V3_CONTENT_PURPOSE_NORMAL;
    case TEXT_INPUT_TYPE_TEXT:
      return ZWP_TEXT_INPUT_V3_CONTENT_PURPOSE_NORMAL;
    case TEXT_INPUT_TYPE_PASSWORD:
      return ZWP_TEXT_INPUT_V3_CONTENT_PURPOSE_PASSWORD;
    case TEXT_INPUT_TYPE_SEARCH:
      return ZWP_TEXT_INPUT_V3_CONTENT_PURPOSE_NORMAL;
    case TEXT_INPUT_TYPE_EMAIL:
      return ZWP_TEXT_INPUT_V3_CONTENT_PURPOSE_EMAIL;
    case TEXT_INPUT_TYPE_NUMBER:
      return ZWP_TEXT_INPUT_V3_CONTENT_PURPOSE_NUMBER;
    case TEXT_INPUT_TYPE_TELEPHONE:
      return ZWP_TEXT_INPUT_V3_CONTENT_PURPOSE_PHONE;
    case TEXT_INPUT_TYPE_URL:
      return ZWP_TEXT_INPUT_V3_CONTENT_PURPOSE_URL;
    case TEXT_INPUT_TYPE_DATE:
      return ZWP_TEXT_INPUT_V3_CONTENT_PURPOSE_DATE;
    case TEXT_INPUT_TYPE_DATE_TIME:
      return ZWP_TEXT_INPUT_V3_CONTENT_PURPOSE_DATETIME;
    case TEXT_INPUT_TYPE_DATE_TIME_LOCAL:
      return ZWP_TEXT_INPUT_V3_CONTENT_PURPOSE_DATETIME;
    case TEXT_INPUT_TYPE_MONTH:
      return ZWP_TEXT_INPUT_V3_CONTENT_PURPOSE_DATE;
    case TEXT_INPUT_TYPE_TIME:
      return ZWP_TEXT_INPUT_V3_CONTENT_PURPOSE_TIME;
    case TEXT_INPUT_TYPE_WEEK:
      return ZWP_TEXT_INPUT_V3_CONTENT_PURPOSE_DATE;
    case TEXT_INPUT_TYPE_TEXT_AREA:
      return ZWP_TEXT_INPUT_V3_CONTENT_PURPOSE_NORMAL;
    case TEXT_INPUT_TYPE_CONTENT_EDITABLE:
      return ZWP_TEXT_INPUT_V3_CONTENT_PURPOSE_NORMAL;
    case TEXT_INPUT_TYPE_DATE_TIME_FIELD:
      return ZWP_TEXT_INPUT_V3_CONTENT_PURPOSE_DATETIME;
    case TEXT_INPUT_TYPE_NULL:
      return ZWP_TEXT_INPUT_V3_CONTENT_PURPOSE_NORMAL;
  }
}

// Converts Chrome's TextInputType into wayland's content_hint.
uint32_t InputFlagsToContentHint(int input_flags) {
  uint32_t hint = 0;
  if (input_flags & TEXT_INPUT_FLAG_AUTOCOMPLETE_ON) {
    hint |= ZWP_TEXT_INPUT_V3_CONTENT_HINT_COMPLETION;
  }
  if (input_flags & TEXT_INPUT_FLAG_SPELLCHECK_ON) {
    hint |= ZWP_TEXT_INPUT_V3_CONTENT_HINT_SPELLCHECK;
  }
  // No good match. Fallback to SPELLCHECK.
  if (input_flags & TEXT_INPUT_FLAG_AUTOCORRECT_ON) {
    hint |= ZWP_TEXT_INPUT_V3_CONTENT_HINT_SPELLCHECK;
  }
  if (input_flags & TEXT_INPUT_FLAG_AUTOCAPITALIZE_CHARACTERS) {
    hint |= ZWP_TEXT_INPUT_V3_CONTENT_HINT_AUTO_CAPITALIZATION;
  }
  if (input_flags & TEXT_INPUT_FLAG_AUTOCAPITALIZE_WORDS) {
    hint |= ZWP_TEXT_INPUT_V3_CONTENT_HINT_AUTO_CAPITALIZATION;
  }
  if (input_flags & TEXT_INPUT_FLAG_AUTOCAPITALIZE_SENTENCES) {
    hint |= ZWP_TEXT_INPUT_V3_CONTENT_HINT_AUTO_CAPITALIZATION;
  }
  if (input_flags & TEXT_INPUT_FLAG_HAS_BEEN_PASSWORD) {
    hint |= ZWP_TEXT_INPUT_V3_CONTENT_HINT_HIDDEN_TEXT;
    hint |= ZWP_TEXT_INPUT_V3_CONTENT_HINT_SENSITIVE_DATA;
  }
  return hint;
}

}  // namespace

ZwpTextInputV3Impl::ImeData::ImeData() = default;
ZwpTextInputV3Impl::ImeData::~ImeData() = default;
void ZwpTextInputV3Impl::ImeData::Reset() {
  surrounding_text.reset();
  cursor_rect.reset();
  content_type.reset();
}

ZwpTextInputV3Impl::InputEvents::InputEvents() = default;
ZwpTextInputV3Impl::InputEvents::~InputEvents() = default;

ZwpTextInputV3Impl::ZwpTextInputV3Impl(
    WaylandConnection* connection,
    zwp_text_input_manager_v3* text_input_manager)
    : connection_(connection) {
  static constexpr zwp_text_input_v3_listener kTextInputListener = {
      &OnEnter,
      &OnLeave,
      &OnPreeditString,
      &OnCommitString,
      &OnDeleteSurroundingText,
      &OnDone,
  };

  CHECK(text_input_manager);
  auto* text_input = zwp_text_input_manager_v3_get_text_input(
      text_input_manager, connection_->seat()->wl_object());
  obj_ = wl::Object<zwp_text_input_v3>(text_input);

  zwp_text_input_v3_add_listener(text_input, &kTextInputListener, this);
}

ZwpTextInputV3Impl::~ZwpTextInputV3Impl() = default;

void ZwpTextInputV3Impl::Reset() {
  // Clear last committed values.
  ResetCommittedImeData();
  // There is no explicit reset API in v3. See [1].
  // Disable+enable to force a reset has been discussed as a possible solution.
  // But this is not implemented yet in compositors. In fact, it was seen in
  // both mutter and kwin that it can cause the IME to enter a grab state
  // unexpectedly. So at this point, leave it unimplemented.
  //
  // If no reset is implemented at all, it can lead to bad user experience,
  // e.g. preedit being duplicated if composition is aborted on the chromium
  // side by clicking in the input field. So the logic below is still needed
  // until a proper fix is in place.
  //
  // Even though chromium expects only preedit to be reset, the surrounding text
  // in fact could change along with reset being called if composition was
  // canceled internally. So we shouldn't keep old surrounding text anyway. See
  // related crbug.com/353915732 where surrounding text update is not sent after
  // reset when composition is canceled.
  //
  // [1]
  // https://gitlab.freedesktop.org/wayland/wayland-protocols/-/merge_requests/34
  ResetPendingImeData();
  ResetInputEventsState();
}

void ZwpTextInputV3Impl::SetClient(ZwpTextInputV3Client* context) {
  client_ = context;
}

void ZwpTextInputV3Impl::OnClientDestroyed(ZwpTextInputV3Client* context) {
  if (client_ == context) {
    client_ = nullptr;
    Disable();
  }
}

void ZwpTextInputV3Impl::Enable() {
  // Pending state is reset on enable.
  ResetPendingImeData();
  ResetInputEventsState();
  zwp_text_input_v3_enable(obj_.get());
  Commit();
}

void ZwpTextInputV3Impl::Disable() {
  // Avoid sending pending requests if done is received after disabling.
  ResetPendingImeData();
  // Do not process pending input events after deactivating.
  ResetInputEventsState();
  zwp_text_input_v3_disable(obj_.get());
  Commit();
}

bool ZwpTextInputV3Impl::DoneSerialEqualsCommitCount() {
  return committed_ime_data_.commit_count ==
         pending_input_events_.last_done_serial;
}

void ZwpTextInputV3Impl::SetCursorRect(const gfx::Rect& rect) {
  if (committed_ime_data_.cursor_rect &&
      *committed_ime_data_.cursor_rect == rect) {
    // This is to avoid a loop in sending cursor rect and receiving pre-edit
    // string.
    return;
  }
  pending_ime_data_.cursor_rect = std::make_unique<gfx::Rect>(rect);
  SendPendingImeData();
}

bool ZwpTextInputV3Impl::SendCursorRect() {
  CHECK(DoneSerialEqualsCommitCount());
  if (const auto& rect = pending_ime_data_.cursor_rect; rect) {
    zwp_text_input_v3_set_cursor_rectangle(obj_.get(), rect->x(), rect->y(),
                                           rect->width(), rect->height());
    committed_ime_data_.cursor_rect = std::move(pending_ime_data_.cursor_rect);
    return true;
  }
  return false;
}

void ZwpTextInputV3Impl::SetSurroundingText(
    const std::string& text_with_preedit,
    const gfx::Range& preedit_range,
    const gfx::Range& selection_range) {
  auto text = text_with_preedit;
  int32_t anchor, cursor;
  if (!preedit_range.is_empty()) {
    CHECK(preedit_range.IsBoundedBy({0, text_with_preedit.length()}));
    const size_t preedit_min = preedit_range.GetMin();
    const size_t preedit_max = preedit_range.GetMax();
    // Remove preedit portion from surrounding text
    text.erase(preedit_min, preedit_range.length());
    // Now re-calculate selection range
    if (selection_range.IsValid()) {
      auto selection_start = selection_range.start();
      auto selection_end = selection_range.end();
      anchor = selection_start -
               (selection_start <= preedit_min
                    ? 0
                    : std::min(selection_start, preedit_max) - preedit_min);
      cursor = selection_end -
               (selection_end <= preedit_min
                    ? 0
                    : std::min(selection_end, preedit_max) - preedit_min);

    } else {
      // Invalid selection range. Put cursor at preedit position.
      anchor = preedit_min;
      cursor = preedit_min;
    }
  } else {
    anchor = base::checked_cast<int32_t>(
        selection_range.IsValid() ? selection_range.start() : text.length());
    cursor = base::checked_cast<int32_t>(
        selection_range.IsValid() ? selection_range.end() : text.length());
  }
  auto data =
      std::make_unique<SetSurroundingTextData>(std::move(text), cursor, anchor);
  if (committed_ime_data_.surrounding_text &&
      *committed_ime_data_.surrounding_text == *data) {
    return;
  }
  pending_ime_data_.surrounding_text = std::move(data);
  SendPendingImeData();
}

bool ZwpTextInputV3Impl::SendSurroundingText() {
  CHECK(DoneSerialEqualsCommitCount());
  if (const auto& data = pending_ime_data_.surrounding_text) {
    zwp_text_input_v3_set_surrounding_text(obj_.get(), data->text.c_str(),
                                           data->cursor, data->anchor);
    committed_ime_data_.surrounding_text =
        std::move(pending_ime_data_.surrounding_text);
    return true;
  }
  return false;
}

void ZwpTextInputV3Impl::SetContentType(ui::TextInputType type,
                                        uint32_t flags,
                                        bool should_do_learning) {
  uint32_t content_hint = InputFlagsToContentHint(flags);
  if (!should_do_learning) {
    content_hint |= ZWP_TEXT_INPUT_V3_CONTENT_HINT_SENSITIVE_DATA;
  }
  uint32_t content_purpose = InputTypeToContentPurpose(type);
  if (!should_do_learning) {
    content_hint |= ZWP_TEXT_INPUT_V3_CONTENT_HINT_SENSITIVE_DATA;
  }
  auto content_type =
      std::make_unique<ContentType>(content_hint, content_purpose);
  if (committed_ime_data_.content_type &&
      *committed_ime_data_.content_type == *content_type) {
    return;
  }
  pending_ime_data_.content_type = std::move(content_type);
  SendPendingImeData();
}

bool ZwpTextInputV3Impl::SendContentType() {
  CHECK(DoneSerialEqualsCommitCount());
  if (const auto& content_type = pending_ime_data_.content_type) {
    zwp_text_input_v3_set_content_type(obj_.get(), content_type->content_hint,
                                       content_type->content_purpose);
    committed_ime_data_.content_type =
        std::move(pending_ime_data_.content_type);
    return true;
  }
  return false;
}

void ZwpTextInputV3Impl::SendPendingImeData() {
  if (!DoneSerialEqualsCommitCount()) {
    return;
  }
  bool commit = false;
  if (SendContentType()) {
    commit = true;
  }
  if (SendCursorRect()) {
    commit = true;
  }
  if (SendSurroundingText()) {
    commit = true;
  }
  if (commit) {
    Commit();
  }
}

void ZwpTextInputV3Impl::ResetPendingImeData() {
  pending_ime_data_.Reset();
}

void ZwpTextInputV3Impl::ResetCommittedImeData() {
  committed_ime_data_.Reset();
}

void ZwpTextInputV3Impl::ResetInputEventsState() {
  pending_input_events_.preedit = std::nullopt;
  pending_input_events_.commit = std::nullopt;
}

void ZwpTextInputV3Impl::Commit() {
  zwp_text_input_v3_commit(obj_.get());
  // It will wrap around to 0 once it reaches uint32_t max value. It is
  // expected that this will occur on the compositor side as well.
  ++committed_ime_data_.commit_count;
}

void ZwpTextInputV3Impl::OnEnter(void* data,
                                 struct zwp_text_input_v3* text_input,
                                 struct wl_surface* surface) {
  auto* self = static_cast<ZwpTextInputV3Impl*>(data);
  if (auto* window = wl::RootWindowFromWlSurface(surface)) {
    self->connection_->window_manager()->SetTextInputFocusedWindow(window);
  }
}

void ZwpTextInputV3Impl::OnLeave(void* data,
                                 struct zwp_text_input_v3* text_input,
                                 struct wl_surface* surface) {
  auto* self = static_cast<ZwpTextInputV3Impl*>(data);
  self->connection_->window_manager()->SetTextInputFocusedWindow(nullptr);
}

void ZwpTextInputV3Impl::OnPreeditString(void* data,
                                         struct zwp_text_input_v3* text_input,
                                         const char* text,
                                         int32_t cursor_begin,
                                         int32_t cursor_end) {
  auto* self = static_cast<ZwpTextInputV3Impl*>(data);
  self->pending_input_events_.preedit = {text ? text : std::string(),
                                         cursor_begin, cursor_end};
}

void ZwpTextInputV3Impl::OnCommitString(void* data,
                                        struct zwp_text_input_v3* text_input,
                                        const char* text) {
  auto* self = static_cast<ZwpTextInputV3Impl*>(data);
  self->pending_input_events_.commit = text ? text : std::string();
}

void ZwpTextInputV3Impl::OnDeleteSurroundingText(
    void* data,
    struct zwp_text_input_v3* text_input,
    uint32_t before_length,
    uint32_t after_length) {
  NOTIMPLEMENTED_LOG_ONCE();
}

void ZwpTextInputV3Impl::OnDone(void* data,
                                struct zwp_text_input_v3* text_input,
                                uint32_t serial) {
  // TODO(crbug.com/40113488) apply delete surrounding
  auto* self = static_cast<ZwpTextInputV3Impl*>(data);
  self->pending_input_events_.last_done_serial = serial;

  if (!self->client_) {
    return;
  }

  if (const auto& commit_string = self->pending_input_events_.commit) {
    // Replace the existing preedit with the commit string.
    self->client_->OnCommitString(commit_string->c_str());
  }
  if (const auto preedit_data = self->pending_input_events_.preedit) {
    // Finally process any new preedit string.
    gfx::Range preedit_cursor =
        (preedit_data->cursor_begin < 0 || preedit_data->cursor_end < 0)
            ? gfx::Range::InvalidRange()
            : gfx::Range(preedit_data->cursor_begin, preedit_data->cursor_end);
    self->client_->OnPreeditString(preedit_data->text.c_str(), {},
                                   preedit_cursor);
  }

  self->ResetInputEventsState();
  self->SendPendingImeData();
}

}  // namespace ui