File: zwp_text_input_v1.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 (353 lines) | stat: -rw-r--r-- 13,137 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
// Copyright 2018 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_v1.h"

#include <sys/mman.h>

#include <string>
#include <string_view>
#include <utility>

#include "base/base64.h"
#include "base/check.h"
#include "base/files/file_util.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/strings/strcat.h"
#include "base/strings/string_split.h"
#include "base/time/time.h"
#include "ui/base/wayland/wayland_client_input_types.h"
#include "ui/gfx/range/range.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_V1_CONTENT_PURPOSE_NORMAL;
    case TEXT_INPUT_TYPE_TEXT:
      return ZWP_TEXT_INPUT_V1_CONTENT_PURPOSE_NORMAL;
    case TEXT_INPUT_TYPE_PASSWORD:
      return ZWP_TEXT_INPUT_V1_CONTENT_PURPOSE_PASSWORD;
    case TEXT_INPUT_TYPE_SEARCH:
      return ZWP_TEXT_INPUT_V1_CONTENT_PURPOSE_NORMAL;
    case TEXT_INPUT_TYPE_EMAIL:
      return ZWP_TEXT_INPUT_V1_CONTENT_PURPOSE_EMAIL;
    case TEXT_INPUT_TYPE_NUMBER:
      return ZWP_TEXT_INPUT_V1_CONTENT_PURPOSE_NUMBER;
    case TEXT_INPUT_TYPE_TELEPHONE:
      return ZWP_TEXT_INPUT_V1_CONTENT_PURPOSE_PHONE;
    case TEXT_INPUT_TYPE_URL:
      return ZWP_TEXT_INPUT_V1_CONTENT_PURPOSE_URL;
    case TEXT_INPUT_TYPE_DATE:
      return ZWP_TEXT_INPUT_V1_CONTENT_PURPOSE_DATE;
    case TEXT_INPUT_TYPE_DATE_TIME:
      return ZWP_TEXT_INPUT_V1_CONTENT_PURPOSE_DATETIME;
    case TEXT_INPUT_TYPE_DATE_TIME_LOCAL:
      return ZWP_TEXT_INPUT_V1_CONTENT_PURPOSE_DATETIME;
    case TEXT_INPUT_TYPE_MONTH:
      return ZWP_TEXT_INPUT_V1_CONTENT_PURPOSE_DATE;
    case TEXT_INPUT_TYPE_TIME:
      return ZWP_TEXT_INPUT_V1_CONTENT_PURPOSE_TIME;
    case TEXT_INPUT_TYPE_WEEK:
      return ZWP_TEXT_INPUT_V1_CONTENT_PURPOSE_DATE;
    case TEXT_INPUT_TYPE_TEXT_AREA:
      return ZWP_TEXT_INPUT_V1_CONTENT_PURPOSE_NORMAL;
    case TEXT_INPUT_TYPE_CONTENT_EDITABLE:
      return ZWP_TEXT_INPUT_V1_CONTENT_PURPOSE_NORMAL;
    case TEXT_INPUT_TYPE_DATE_TIME_FIELD:
      return ZWP_TEXT_INPUT_V1_CONTENT_PURPOSE_DATETIME;
    case TEXT_INPUT_TYPE_NULL:
      return ZWP_TEXT_INPUT_V1_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_V1_CONTENT_HINT_AUTO_COMPLETION;
  }
  if (input_flags & TEXT_INPUT_FLAG_AUTOCORRECT_ON) {
    hint |= ZWP_TEXT_INPUT_V1_CONTENT_HINT_AUTO_CORRECTION;
  }
  // No good match. Fallback to AUTO_CORRECTION.
  if (input_flags & TEXT_INPUT_FLAG_SPELLCHECK_ON) {
    hint |= ZWP_TEXT_INPUT_V1_CONTENT_HINT_AUTO_CORRECTION;
  }
  if (input_flags & TEXT_INPUT_FLAG_AUTOCAPITALIZE_CHARACTERS) {
    hint |= ZWP_TEXT_INPUT_V1_CONTENT_HINT_UPPERCASE;
  }
  if (input_flags & TEXT_INPUT_FLAG_AUTOCAPITALIZE_WORDS) {
    hint |= ZWP_TEXT_INPUT_V1_CONTENT_HINT_TITLECASE;
  }
  if (input_flags & TEXT_INPUT_FLAG_AUTOCAPITALIZE_SENTENCES) {
    hint |= ZWP_TEXT_INPUT_V1_CONTENT_HINT_AUTO_CAPITALIZATION;
  }
  if (input_flags & TEXT_INPUT_FLAG_HAS_BEEN_PASSWORD) {
    hint |= ZWP_TEXT_INPUT_V1_CONTENT_HINT_PASSWORD;
  }
  return hint;
}

// Parses the content of |array|, and creates a map of modifiers.
// The content of array is just a concat of modifier names in c-style string
// (i.e., '\0' terminated string), thus this splits the whole byte array by
// '\0' character.
std::vector<std::string> ParseModifiersMap(wl_array* array) {
  return base::SplitString(
      std::string_view(static_cast<char*>(array->data),
                       array->size - 1),  // exclude trailing '\0'.
      std::string_view("\0", 1),          // '\0' as a delimiter.
      base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
}

// Returns ImeTextSpan style to be assigned. Maybe nullopt if it is not
// supported.
std::optional<SpanStyle::Style> ConvertStyle(uint32_t style) {
  switch (style) {
    case ZWP_TEXT_INPUT_V1_PREEDIT_STYLE_DEFAULT:
      return {{ImeTextSpan::Type::kComposition, ImeTextSpan::Thickness::kNone}};
    case ZWP_TEXT_INPUT_V1_PREEDIT_STYLE_HIGHLIGHT:
      return {
          {ImeTextSpan::Type::kComposition, ImeTextSpan::Thickness::kThick}};
    case ZWP_TEXT_INPUT_V1_PREEDIT_STYLE_UNDERLINE:
      return {{ImeTextSpan::Type::kComposition, ImeTextSpan::Thickness::kThin}};
    case ZWP_TEXT_INPUT_V1_PREEDIT_STYLE_SELECTION:
      return {{ImeTextSpan::Type::kSuggestion, ImeTextSpan::Thickness::kNone}};
    case ZWP_TEXT_INPUT_V1_PREEDIT_STYLE_INCORRECT:
      return {{ImeTextSpan::Type::kMisspellingSuggestion,
               ImeTextSpan::Thickness::kNone}};
    case ZWP_TEXT_INPUT_V1_PREEDIT_STYLE_NONE:
    case ZWP_TEXT_INPUT_V1_PREEDIT_STYLE_ACTIVE:
    case ZWP_TEXT_INPUT_V1_PREEDIT_STYLE_INACTIVE:
    default:
      VLOG(1) << "Unsupported style. Skipped: " << style;
  }
  return std::nullopt;
}

}  // namespace

ZwpTextInputV1Impl::ZwpTextInputV1Impl(
    WaylandConnection* connection,
    zwp_text_input_manager_v1* text_input_manager)
    : connection_(connection) {
  static constexpr zwp_text_input_v1_listener kTextInputListener = {
      .enter = &OnEnter,
      .leave = &OnLeave,
      .modifiers_map = &OnModifiersMap,
      .input_panel_state = &OnInputPanelState,
      .preedit_string = &OnPreeditString,
      .preedit_styling = &OnPreeditStyling,
      .preedit_cursor = &OnPreeditCursor,
      .commit_string = &OnCommitString,
      .cursor_position = &OnCursorPosition,
      .delete_surrounding_text = &OnDeleteSurroundingText,
      .keysym = &OnKeysym,
      .language = &OnLanguage,
      .text_direction = &OnTextDirection,
  };

  obj_ = wl::Object<zwp_text_input_v1>(
      zwp_text_input_manager_v1_create_text_input(text_input_manager));
  DCHECK(obj_.get());
  zwp_text_input_v1_add_listener(obj_.get(), &kTextInputListener, this);
}

ZwpTextInputV1Impl::~ZwpTextInputV1Impl() = default;

void ZwpTextInputV1Impl::Reset() {
  ResetInputEventState();
  zwp_text_input_v1_reset(obj_.get());
}

void ZwpTextInputV1Impl::Activate(WaylandWindow* window) {
  DCHECK(connection_->seat());
  zwp_text_input_v1_activate(obj_.get(), connection_->seat()->wl_object(),
                             window->root_surface()->surface());
}

void ZwpTextInputV1Impl::SetClient(ZwpTextInputV1Client* context) {
  client_ = context;
}

void ZwpTextInputV1Impl::OnClientDestroyed(ZwpTextInputV1Client* context) {
  if (client_ == context) {
    client_ = nullptr;
    Deactivate();
  }
}

void ZwpTextInputV1Impl::Deactivate() {
  DCHECK(connection_->seat());

  zwp_text_input_v1_deactivate(obj_.get(), connection_->seat()->wl_object());
}

void ZwpTextInputV1Impl::ShowInputPanel() {
  zwp_text_input_v1_show_input_panel(obj_.get());
}

void ZwpTextInputV1Impl::HideInputPanel() {
  zwp_text_input_v1_hide_input_panel(obj_.get());
}

void ZwpTextInputV1Impl::SetCursorRect(const gfx::Rect& rect) {
  zwp_text_input_v1_set_cursor_rectangle(obj_.get(), rect.x(), rect.y(),
                                         rect.width(), rect.height());
}

void ZwpTextInputV1Impl::SetSurroundingText(const std::string& text,
                                            const gfx::Range& preedit_range,
                                            const gfx::Range& selection_range) {
  zwp_text_input_v1_set_surrounding_text(
      obj_.get(), text.c_str(), selection_range.start(), selection_range.end());
}

void ZwpTextInputV1Impl::SetContentType(ui::TextInputType type,
                                        uint32_t flags,
                                        bool should_do_learning) {
  uint32_t content_purpose = InputTypeToContentPurpose(type);
  uint32_t content_hint = InputFlagsToContentHint(flags);
  if (!should_do_learning) {
    content_hint |= ZWP_TEXT_INPUT_V1_CONTENT_HINT_SENSITIVE_DATA;
  }
  zwp_text_input_v1_set_content_type(obj_.get(), content_hint, content_purpose);
}

void ZwpTextInputV1Impl::ResetInputEventState() {
  spans_.clear();
  preedit_cursor_ = -1;
}

// static
void ZwpTextInputV1Impl::OnEnter(void* data,
                                 struct zwp_text_input_v1* text_input,
                                 struct wl_surface* surface) {
  NOTIMPLEMENTED_LOG_ONCE();
}

// static
void ZwpTextInputV1Impl::OnLeave(void* data,
                                 struct zwp_text_input_v1* text_input) {
  NOTIMPLEMENTED_LOG_ONCE();
}

// static
void ZwpTextInputV1Impl::OnModifiersMap(void* data,
                                        struct zwp_text_input_v1* text_input,
                                        struct wl_array* map) {
  auto* self = static_cast<ZwpTextInputV1Impl*>(data);
  self->client_->OnModifiersMap(ParseModifiersMap(map));
}

// static
void ZwpTextInputV1Impl::OnInputPanelState(void* data,
                                           struct zwp_text_input_v1* text_input,
                                           uint32_t state) {
  auto* self = static_cast<ZwpTextInputV1Impl*>(data);
  self->client_->OnInputPanelState(state);
}

// static
void ZwpTextInputV1Impl::OnPreeditString(void* data,
                                         struct zwp_text_input_v1* text_input,
                                         uint32_t serial,
                                         const char* text,
                                         const char* commit) {
  auto* self = static_cast<ZwpTextInputV1Impl*>(data);
  auto spans = std::move(self->spans_);
  int32_t preedit_cursor = self->preedit_cursor_;
  self->ResetInputEventState();
  self->client_->OnPreeditString(text, spans,
                                 preedit_cursor < 0
                                     ? gfx::Range::InvalidRange()
                                     : gfx::Range(preedit_cursor));
}

// static
void ZwpTextInputV1Impl::OnPreeditStyling(void* data,
                                          struct zwp_text_input_v1* text_input,
                                          uint32_t index,
                                          uint32_t length,
                                          uint32_t style) {
  auto* self = static_cast<ZwpTextInputV1Impl*>(data);
  self->spans_.push_back(SpanStyle{index, length, ConvertStyle(style)});
}

// static
void ZwpTextInputV1Impl::OnPreeditCursor(void* data,
                                         struct zwp_text_input_v1* text_input,
                                         int32_t index) {
  auto* self = static_cast<ZwpTextInputV1Impl*>(data);
  self->preedit_cursor_ = index;
}

// static
void ZwpTextInputV1Impl::OnCommitString(void* data,
                                        struct zwp_text_input_v1* text_input,
                                        uint32_t serial,
                                        const char* text) {
  auto* self = static_cast<ZwpTextInputV1Impl*>(data);
  self->ResetInputEventState();
  self->client_->OnCommitString(text);
}

// static
void ZwpTextInputV1Impl::OnCursorPosition(void* data,
                                          struct zwp_text_input_v1* text_input,
                                          int32_t index,
                                          int32_t anchor) {
  auto* self = static_cast<ZwpTextInputV1Impl*>(data);
  self->client_->OnCursorPosition(index, anchor);
}

// static
void ZwpTextInputV1Impl::OnDeleteSurroundingText(
    void* data,
    struct zwp_text_input_v1* text_input,
    int32_t index,
    uint32_t length) {
  auto* self = static_cast<ZwpTextInputV1Impl*>(data);
  self->client_->OnDeleteSurroundingText(index, length);
}

// static
void ZwpTextInputV1Impl::OnKeysym(void* data,
                                  struct zwp_text_input_v1* text_input,
                                  uint32_t serial,
                                  uint32_t time,
                                  uint32_t key,
                                  uint32_t state,
                                  uint32_t modifiers) {
  auto* self = static_cast<ZwpTextInputV1Impl*>(data);
  self->client_->OnKeysym(key, state, modifiers, time);
}

// static
void ZwpTextInputV1Impl::OnLanguage(void* data,
                                    struct zwp_text_input_v1* text_input,
                                    uint32_t serial,
                                    const char* language) {
  NOTIMPLEMENTED_LOG_ONCE();
}

// static
void ZwpTextInputV1Impl::OnTextDirection(void* data,
                                         struct zwp_text_input_v1* text_input,
                                         uint32_t serial,
                                         uint32_t direction) {
  NOTIMPLEMENTED_LOG_ONCE();
}

}  // namespace ui