feat(wayland): SetTextInput takes text-input-v3's purposes and its version 1 hints, and enables anew for a field of another kind

Window::SetTextInput knew two purposes, Normal and Phone, and
Device::ApplyTextInput chose the content hints from them: none for
Phone, and completion, spellcheck and auto_capitalization for Normal.
Nothing could send another purpose, or hidden_text or sensitive_data:
a password field was Normal, with those three hints, or Phone.

Types: TextInputPurpose has the fourteen content purposes of the
bundled text-input-v3 protocol, in its order, so that the enumerators
have the protocol's values; Phone was 1 and is 4. kTextInputHintNone
and the ten kTextInputHint* bits are the content hints of version 1
of the protocol, the version Device binds the text input's manager
at, with the protocol's values, and kTextInputHintMask is the ten
together. TextInputDefaultHints(purpose) is what a field gets when
the caller names no hints: the three Normal had, hidden_text and
sensitive_data for Password and Pin, none for the other eleven. For
Normal and Phone without hints that is the content type they had
before.

Window: SetTextInput takes the hints as a last, defaulted
std::optional<std::uint32_t>, which tells kTextInputHintNone (no
hints) from no argument (the purpose's defaults). It keeps only the
bits of kTextInputHintMask: version 2's hints are not in version 1's
enum. On Win32 and in the browser the call still does nothing.

Device, ApplyTextInput: it sends the window's purpose and hints as
they are. For a window whose field is enabled it compares them with
the ones the enable in force went out with, which the window now
keeps, and when either differs it sends an enable before the content
type, where it sent the content type without one. The protocol, as
bundled in lib/text-input-unstable-v3-client-protocol.h, says of
set_content_type's values that "Subsequent attempts to update them
may have no effect" and that they "remain valid until the next
committed enable or disable request", and of enable that it "must be
issued every time the focused text input changes to a new one,
including within the current surface". The same text has an enable
reset "the state associated with preedit_string, commit_string, and
delete_surrounding_text events", so such an enable clears the pending
flags and the record of the composing text the window was last told,
as the disable path does (6f471ca), and fires no onPreedit. A call
with the purpose and hints of the enable in force sends no enable, as
before, also when it is for another field: the window does not know
one field from another of the same purpose and hints, and a caller
marks that move with SetTextInput(false) first.

Device, text_input_done: a listener of onTextInputDelete, onTextInput
or onPreedit may call SetTextInput, and the disable path's clearing
then ran in the middle of a done that was still reading the pending
flags and strings it clears: a disable from a listener of onPreedit
cleared the string the listeners' string_view points into, and one
from a listener of onTextInputDelete reset the flag the commit of the
same done waited on, so that commit never reached onTextInput. The
enable for a field of another kind clears the same values. A listener
may also dispatch the display, as Clipboard::GetText and Window's
constructor do, and the next done then ran inside the first, on
pending values the first had not yet reset. The handler now moves a
done out of the pending values as it arrives, onto a queue, and
delivers from there: the deletion, then the commit, then the
composing text. A done that arrives while another is being delivered
waits on the queue for it, so the window hears the dones in the order
they came. Only the dones wait: a key, pointer or touch event that
the same dispatch brings still fires where it arrives, ahead of a
done that waits, which the consumer notes now say. The commit goes
out whatever a listener of the deletion called SetTextInput with. The
composing text is reported only when nothing has overtaken it since
its done arrived: textInputEpoch, a counter in Device.cpp, moves at a
disable, at an enable for a field of another kind, at a leave and at
every done, and the handler compares the value a done arrived with
against the one after its commit's listeners. A listener may also
destroy the window: ~Window calls Device::ForgetTextInputDones, which
takes the window out of the queue's entries, the one being delivered
among them, and the handler fires nothing more of those. A look-up of
the address in Device::windows would take a window opened at that
address in the meantime for the one destroyed.

Tests: TextInputContent (new) makes the text input a proxy on a
wl_display over a socketpair, as TitleWayland makes its proxies,
reads the requests off the other end and calls the text input's
listener callbacks itself. It checks the purposes and the hint
constants against the bundled protocol header's values, the mask,
and the default hints of the fourteen purposes; then, on the wire:
nothing from a window the text input has not entered; an enable, the
content type, the caret and a commit at the enter; no enable for a
caret that moves; an enable with its own content type for the other
thirteen purposes in turn, after which a composing text equal to the
one before them reaches onPreedit again; named hints in place of the
defaults; an enable for other hints under the same purpose; a
version 2 hint (no_emoji) neither sent nor taken for a change;
kTextInputHintNone sent as 0; the caret in surface coordinates at a
scale of 2; one disable for a field losing the focus; no enable for
a call for another field of the same purpose and hints, and one when
a disable comes before it; and after a leave, nothing until the
enter, which enables the field the window has by then. For the done,
with two listeners on onTextInput and on onPreedit: a first listener
of onPreedit that changes the purpose, or disables the field, and the
listener after it are given the composing text as the input method
sent it; after a listener of onTextInputDelete that changes the
hints, or disables the field, the commit of the same done reaches
both listeners of onTextInput and that done fires no onPreedit; a
done fed from inside a listener of
onTextInputDelete is delivered after the one that listener was called
for, whose commit reaches the listeners of onTextInput before the fed
one's; a done fed from inside a listener of onTextInput is delivered
after that listener's done, whose own composing text is not reported;
a leave fed from inside a listener of onTextInput is followed by no
onPreedit for that done's composing text; and when a listener of
onTextInputDelete feeds a done, destroys the window and puts another
at its address, listed in Device::windows, neither onTextInput nor
onPreedit fires on the new one.

Docs: the consumer notes list the purposes and the hints, the default
hints, what a call that changes either does to a composition under
way, the SetTextInput(false) that marks a move to another field of
the same purpose and hints, and the order the input method's dones
are delivered in. AGENTS.template.md names TextInputContent among
the tests that need no GPU.
This commit is contained in:
Jorijn van der Graaf 2026-11-20 08:08:54 +01:00
commit a9ce1a02e8
Signed by: jorijnvdgraaf
GPG key ID: 2937E59CDCC1BCFB
9 changed files with 707 additions and 24 deletions

View file

@ -0,0 +1,505 @@
//SPDX-License-Identifier: LGPL-3.0-only
//SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
// Window::SetTextInput's purpose and hints on the Wayland backend: what a
// text field holds and how the input method treats it, as text-input-v3's
// content type. The window knew two purposes, Normal and Phone, and chose
// the hints itself, so a password field was completed, spell-checked and
// learnt by the on-screen keyboard like prose, and an amount got letters
// where a number pad belongs. And a call that changed the purpose of a
// field that had the focus sent the new content type alone, which the
// protocol lets an input method ignore: the content type holds from the
// enable it was committed with, and a field of another kind is another text
// input, to be enabled anew.
//
// The text input is a proxy on a wl_display connected to one end of a
// socketpair, as in TitleWayland, and the test reads the requests off the
// other end and calls the listener callbacks itself, so no compositor is
// needed. Asserts:
//
// - TextInputPurpose and the kTextInputHint* constants have the protocol's
// values, which is what lets the window send them as they are
// - a purpose without hints gets its usual ones: prose is completed,
// checked and capitalised, a password and a PIN are hidden and not
// learnt, every other purpose is taken as typed
// - hints the caller names replace those, and a hint of a later version of
// the protocol than the one bound is not sent
// - a field getting the focus is one enable, its content type, its caret
// and a commit; a caret moving within it sends no second enable
// - a field of another purpose, or of other hints, is enabled anew, and
// the composition under way ends with it: the same composing text
// afterwards is new to the window
// - a field losing the focus is a disable, and the next one an enable
// - a call for another field of the same purpose and hints sends no enable
// by itself, as for a caret that moved, and a disable before it makes it
// one
// - a window the compositor has not given the text input sends nothing,
// and its field is enabled when the text input enters it
//
// A listener of the window's text events may call SetTextInput, and may
// dispatch the display, while the input method's done is being delivered;
// a change of purpose or hints from there, like a disable, clears the pending
// values that done is made of. Asserts:
//
// - a listener of onPreedit that changes the purpose, or disables the
// field: it and the listener after it still read the composing text
// - a listener of onTextInputDelete that changes the hints, or disables
// the field: the commit of the same done still arrives, and the done's
// composing text, of a composition the listener ended, is not reported
// - a later done that a listener of onTextInputDelete or onTextInput
// brings: it is delivered after the done that listener was called for,
// whose commit comes first and whose composing text is not reported
// - a leave that a listener of onTextInput brings: the done's composing
// text is not reported after it
// - a listener of onTextInputDelete that destroys the window and opens
// another at its address: nothing more of the done, and nothing of a
// done waiting behind it, fires through the handler's pointer
#include <wayland-client.h>
#include <sys/socket.h>
#include <unistd.h>
#include <cerrno>
#include <cstdlib>
#include <cstring>
#include "../../lib/text-input-unstable-v3-client-protocol.h"
import Crafter.Graphics;
import Crafter.Event;
import std;
using namespace Crafter;
namespace {
int failures = 0;
void Check(bool ok, std::string_view what) {
std::println("{} {}", ok ? "PASS" : "FAIL", what);
if (!ok) ++failures;
}
// One request on the text input: its opcode and its arguments, which are
// all 32-bit words for the requests the window makes.
struct Request {
std::uint32_t opcode;
std::vector<std::uint32_t> arguments;
bool operator==(const Request&) const = default;
};
Request Enable() { return { ZWP_TEXT_INPUT_V3_ENABLE, {} }; }
Request Disable() { return { ZWP_TEXT_INPUT_V3_DISABLE, {} }; }
Request Commit() { return { ZWP_TEXT_INPUT_V3_COMMIT, {} }; }
Request Content(std::uint32_t hints, std::uint32_t purpose) { return { ZWP_TEXT_INPUT_V3_SET_CONTENT_TYPE, { hints, purpose } }; }
Request Caret(std::uint32_t x, std::uint32_t y, std::uint32_t width, std::uint32_t height) {
return { ZWP_TEXT_INPUT_V3_SET_CURSOR_RECTANGLE, { x, y, width, height } };
}
std::string Describe(const std::vector<Request>& requests) {
std::string out;
for (const Request& request : requests) {
out += std::format("{}(", request.opcode);
for (std::size_t i = 0; i < request.arguments.size(); ++i) {
out += std::format("{}{:#x}", i == 0 ? "" : ", ", request.arguments[i]);
}
out += ") ";
}
return out;
}
// What the display has written since the last call: the requests on
// `object`. A request is its object id, then its size and opcode in one
// word, then its arguments.
std::vector<Request> Sent(wl_display* display, int peer, std::uint32_t object) {
wl_display_flush(display);
std::vector<std::uint8_t> wire;
std::uint8_t chunk[4096];
for (ssize_t n; (n = recv(peer, chunk, sizeof(chunk), MSG_DONTWAIT)) > 0;) {
wire.insert(wire.end(), chunk, chunk + n);
}
std::vector<Request> requests;
for (std::size_t at = 0; at + 8 <= wire.size();) {
std::uint32_t header[2];
std::memcpy(header, wire.data() + at, sizeof(header));
const std::uint32_t size = header[1] >> 16;
if (size < 8 || at + size > wire.size()) break;
if (header[0] == object) {
Request request{ header[1] & 0xFFFF, {} };
for (std::size_t word = 8; word + 4 <= size; word += 4) {
std::uint32_t argument;
std::memcpy(&argument, wire.data() + at + word, sizeof(argument));
request.arguments.push_back(argument);
}
requests.push_back(std::move(request));
}
at += size;
}
return requests;
}
void Expect(const std::vector<Request>& sent, const std::vector<Request>& expected, std::string_view what) {
Check(sent == expected, what);
if (sent != expected) {
std::println(" sent {}", Describe(sent));
std::println(" expected {}", Describe(expected));
}
}
constexpr std::uint32_t kProse = kTextInputHintCompletion | kTextInputHintSpellcheck | kTextInputHintAutoCapitalization;
constexpr std::uint32_t kSecret = kTextInputHintHiddenText | kTextInputHintSensitiveData;
// The purposes, each with the protocol's value for it and the hints a field
// of it gets when the caller names none.
struct Purpose {
TextInputPurpose purpose;
std::uint32_t wire;
std::uint32_t hints;
std::string_view name;
};
constexpr Purpose kPurposes[] = {
{ TextInputPurpose::Normal, ZWP_TEXT_INPUT_V3_CONTENT_PURPOSE_NORMAL, kProse, "Normal" },
{ TextInputPurpose::Alpha, ZWP_TEXT_INPUT_V3_CONTENT_PURPOSE_ALPHA, 0, "Alpha" },
{ TextInputPurpose::Digits, ZWP_TEXT_INPUT_V3_CONTENT_PURPOSE_DIGITS, 0, "Digits" },
{ TextInputPurpose::Number, ZWP_TEXT_INPUT_V3_CONTENT_PURPOSE_NUMBER, 0, "Number" },
{ TextInputPurpose::Phone, ZWP_TEXT_INPUT_V3_CONTENT_PURPOSE_PHONE, 0, "Phone" },
{ TextInputPurpose::Url, ZWP_TEXT_INPUT_V3_CONTENT_PURPOSE_URL, 0, "Url" },
{ TextInputPurpose::Email, ZWP_TEXT_INPUT_V3_CONTENT_PURPOSE_EMAIL, 0, "Email" },
{ TextInputPurpose::Name, ZWP_TEXT_INPUT_V3_CONTENT_PURPOSE_NAME, 0, "Name" },
{ TextInputPurpose::Password, ZWP_TEXT_INPUT_V3_CONTENT_PURPOSE_PASSWORD, kSecret, "Password" },
{ TextInputPurpose::Pin, ZWP_TEXT_INPUT_V3_CONTENT_PURPOSE_PIN, kSecret, "Pin" },
{ TextInputPurpose::Date, ZWP_TEXT_INPUT_V3_CONTENT_PURPOSE_DATE, 0, "Date" },
{ TextInputPurpose::Time, ZWP_TEXT_INPUT_V3_CONTENT_PURPOSE_TIME, 0, "Time" },
{ TextInputPurpose::Datetime, ZWP_TEXT_INPUT_V3_CONTENT_PURPOSE_DATETIME, 0, "Datetime" },
{ TextInputPurpose::Terminal, ZWP_TEXT_INPUT_V3_CONTENT_PURPOSE_TERMINAL, 0, "Terminal" },
};
} // namespace
int main() {
Check(kTextInputHintNone == ZWP_TEXT_INPUT_V3_CONTENT_HINT_NONE
&& kTextInputHintCompletion == ZWP_TEXT_INPUT_V3_CONTENT_HINT_COMPLETION
&& kTextInputHintSpellcheck == ZWP_TEXT_INPUT_V3_CONTENT_HINT_SPELLCHECK
&& kTextInputHintAutoCapitalization == ZWP_TEXT_INPUT_V3_CONTENT_HINT_AUTO_CAPITALIZATION
&& kTextInputHintLowercase == ZWP_TEXT_INPUT_V3_CONTENT_HINT_LOWERCASE
&& kTextInputHintUppercase == ZWP_TEXT_INPUT_V3_CONTENT_HINT_UPPERCASE
&& kTextInputHintTitlecase == ZWP_TEXT_INPUT_V3_CONTENT_HINT_TITLECASE
&& kTextInputHintHiddenText == ZWP_TEXT_INPUT_V3_CONTENT_HINT_HIDDEN_TEXT
&& kTextInputHintSensitiveData == ZWP_TEXT_INPUT_V3_CONTENT_HINT_SENSITIVE_DATA
&& kTextInputHintLatin == ZWP_TEXT_INPUT_V3_CONTENT_HINT_LATIN
&& kTextInputHintMultiline == ZWP_TEXT_INPUT_V3_CONTENT_HINT_MULTILINE,
"the hint constants have the protocol's values");
Check(kTextInputHintMask == (kTextInputHintCompletion | kTextInputHintSpellcheck | kTextInputHintAutoCapitalization
| kTextInputHintLowercase | kTextInputHintUppercase | kTextInputHintTitlecase
| kTextInputHintHiddenText | kTextInputHintSensitiveData | kTextInputHintLatin | kTextInputHintMultiline),
"the mask is every hint of version 1, and no later one");
bool values = true;
bool defaults = true;
for (const Purpose& purpose : kPurposes) {
if (static_cast<std::uint32_t>(purpose.purpose) != purpose.wire) {
values = false;
std::println(" {} is {}, the protocol's is {}", purpose.name, static_cast<std::uint32_t>(purpose.purpose), purpose.wire);
}
if (TextInputDefaultHints(purpose.purpose) != purpose.hints) {
defaults = false;
std::println(" {} gets {:#x}, expected {:#x}", purpose.name, TextInputDefaultHints(purpose.purpose), purpose.hints);
}
}
Check(values, "the purposes have the protocol's values");
Check(defaults, "prose is completed, checked and capitalised; a password and a PIN hidden and not learnt; the rest taken as typed");
int fds[2];
if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, fds) != 0) {
std::println("FAIL socketpair: {}", std::strerror(errno));
return EXIT_FAILURE;
}
// The display owns fds[0] from here.
wl_display* display = wl_display_connect_to_fd(fds[0]);
if (display == nullptr) {
std::println("FAIL wl_display_connect_to_fd: {}", std::strerror(errno));
return EXIT_FAILURE;
}
// libwayland makes whatever proxy it is asked to bind; nothing answers.
// Version 1, as Device::handle_global binds the text input's manager.
wl_registry* registry = wl_display_get_registry(display);
auto* input = static_cast<zwp_text_input_v3*>(wl_registry_bind(registry, 1, &zwp_text_input_v3_interface, 1));
const std::uint32_t id = wl_proxy_get_id(reinterpret_cast<wl_proxy*>(input));
Device::display = display;
Device::textInput = input;
{
// Default-constructed windows, which ~Window can tear down without a
// device. The surface is what the text input's enter names.
Window window;
window.surface = static_cast<wl_surface*>(wl_registry_bind(registry, 2, &wl_surface_interface, 1));
Device::windows.push_back(&window);
Sent(display, fds[1], id);
std::vector<std::string> composed;
EventListener<const std::string_view> preedit(&window.onPreedit, [&](const std::string_view text) { composed.emplace_back(text); });
window.SetTextInput(true, 10, 20, 2, 30);
Expect(Sent(display, fds[1], id), {}, "a window the text input has not entered sends nothing");
Device::text_input_enter(nullptr, input, window.surface);
Expect(Sent(display, fds[1], id), { Enable(), Content(kProse, ZWP_TEXT_INPUT_V3_CONTENT_PURPOSE_NORMAL), Caret(10, 20, 2, 30), Commit() },
"the text input entering it enables the field it asked for: one enable, the content type, the caret, a commit");
window.SetTextInput(true, 14, 20, 2, 30);
Expect(Sent(display, fds[1], id), { Content(kProse, ZWP_TEXT_INPUT_V3_CONTENT_PURPOSE_NORMAL), Caret(14, 20, 2, 30), Commit() },
"a caret that moves within the field sends no second enable");
// The input method composes "ab"; the window shows it.
Device::text_input_preedit(nullptr, input, "ab", 2, 2);
Device::text_input_done(nullptr, input, 1);
Check(composed == std::vector<std::string>{ "ab" }, "a composing text reaches the window");
composed.clear();
// The focus moves to a field of another purpose, each in turn: an
// enable for every one, with its own content type.
bool each = true;
TextInputPurpose last = TextInputPurpose::Normal;
for (const Purpose& purpose : kPurposes) {
if (purpose.purpose == last) continue;
window.SetTextInput(true, 14, 20, 2, 30, purpose.purpose);
const std::vector<Request> sent = Sent(display, fds[1], id);
const std::vector<Request> expected = { Enable(), Content(purpose.hints, purpose.wire), Caret(14, 20, 2, 30), Commit() };
if (sent != expected) {
each = false;
std::println(" {}: sent {}", purpose.name, Describe(sent));
}
last = purpose.purpose;
}
Check(each, "a field of another purpose is enabled anew, with that purpose and its usual hints");
// The enable ended the composition: the same text again is a new
// composition, which the window must hear of.
Device::text_input_preedit(nullptr, input, "ab", 2, 2);
Device::text_input_done(nullptr, input, 2);
Check(composed == std::vector<std::string>{ "ab" }, "the composition ended with the enable: the same composing text afterwards is new");
composed.clear();
window.SetTextInput(true, 14, 20, 2, 30, TextInputPurpose::Email, kTextInputHintLowercase);
Expect(Sent(display, fds[1], id), { Enable(), Content(kTextInputHintLowercase, ZWP_TEXT_INPUT_V3_CONTENT_PURPOSE_EMAIL), Caret(14, 20, 2, 30), Commit() },
"hints the caller names replace the purpose's usual ones");
window.SetTextInput(true, 14, 20, 2, 30, TextInputPurpose::Email, kTextInputHintLowercase | kTextInputHintLatin);
Expect(Sent(display, fds[1], id), { Enable(), Content(kTextInputHintLowercase | kTextInputHintLatin, ZWP_TEXT_INPUT_V3_CONTENT_PURPOSE_EMAIL), Caret(14, 20, 2, 30), Commit() },
"other hints for the same purpose are another field too");
window.SetTextInput(true, 14, 20, 2, 30, TextInputPurpose::Email, kTextInputHintLowercase | kTextInputHintLatin | ZWP_TEXT_INPUT_V3_CONTENT_HINT_NO_EMOJI);
Expect(Sent(display, fds[1], id), { Content(kTextInputHintLowercase | kTextInputHintLatin, ZWP_TEXT_INPUT_V3_CONTENT_PURPOSE_EMAIL), Caret(14, 20, 2, 30), Commit() },
"a hint of a later version than the one bound is not sent, and makes no other field");
window.SetTextInput(true, 14, 20, 2, 30, TextInputPurpose::Password, kTextInputHintNone);
Expect(Sent(display, fds[1], id), { Enable(), Content(0, ZWP_TEXT_INPUT_V3_CONTENT_PURPOSE_PASSWORD), Caret(14, 20, 2, 30), Commit() },
"naming no hints at all is not asking for the usual ones");
// The caret is in physical pixels; the protocol wants the surface's.
window.scale = 2.0f;
window.SetTextInput(true, 20, 40, 4, 60, TextInputPurpose::Password, kTextInputHintNone);
Expect(Sent(display, fds[1], id), { Content(0, ZWP_TEXT_INPUT_V3_CONTENT_PURPOSE_PASSWORD), Caret(10, 20, 2, 30), Commit() },
"the caret is sent in surface coordinates");
window.scale = 1.0f;
window.SetTextInput(false);
Expect(Sent(display, fds[1], id), { Disable(), Commit() }, "a field losing the focus is a disable");
window.SetTextInput(false);
Expect(Sent(display, fds[1], id), {}, "which goes once");
window.SetTextInput(true, 14, 20, 2, 30, TextInputPurpose::Number);
Expect(Sent(display, fds[1], id), { Enable(), Content(0, ZWP_TEXT_INPUT_V3_CONTENT_PURPOSE_NUMBER), Caret(14, 20, 2, 30), Commit() },
"the next field is enabled");
// The focus moves to another field of the same purpose and hints.
// The window cannot tell that from a caret that moved, so the caller
// marks it with a disable.
window.SetTextInput(true, 40, 20, 2, 30, TextInputPurpose::Number);
Expect(Sent(display, fds[1], id), { Content(0, ZWP_TEXT_INPUT_V3_CONTENT_PURPOSE_NUMBER), Caret(40, 20, 2, 30), Commit() },
"a call for another field of the same purpose and hints sends no enable by itself");
window.SetTextInput(false);
window.SetTextInput(true, 40, 20, 2, 30, TextInputPurpose::Number);
Expect(Sent(display, fds[1], id), { Disable(), Commit(), Enable(), Content(0, ZWP_TEXT_INPUT_V3_CONTENT_PURPOSE_NUMBER), Caret(40, 20, 2, 30), Commit() },
"a disable before it makes it one: a disable, then an enable with the content type and the caret");
// The keyboard focus leaves the window and comes back: the field it
// still has is enabled again, as what it is now.
Device::text_input_leave(nullptr, input, window.surface);
window.SetTextInput(true, 14, 20, 2, 30, TextInputPurpose::Pin);
Expect(Sent(display, fds[1], id), {}, "a window the text input has left sends nothing");
Device::text_input_enter(nullptr, input, window.surface);
Expect(Sent(display, fds[1], id), { Enable(), Content(kSecret, ZWP_TEXT_INPUT_V3_CONTENT_PURPOSE_PIN), Caret(14, 20, 2, 30), Commit() },
"and is enabled once, as the field it has by then, when the text input comes back");
// ~Window destroys the surface and takes the window off the device.
}
// Listeners that call SetTextInput, or dispatch the display, while a done
// is being delivered. Each event has two listeners: the first does what
// the case asks and then tells what it was given, the second only tells.
{
Window window;
window.surface = static_cast<wl_surface*>(wl_registry_bind(registry, 3, &wl_surface_interface, 1));
Device::windows.push_back(&window);
window.SetTextInput(true, 14, 20, 2, 30);
Device::text_input_enter(nullptr, input, window.surface);
Sent(display, fds[1], id);
std::vector<std::string> heard;
std::function<void()> inDelete, inCommit, inPreedit;
EventListener<TextDelete> deleteActs(&window.onTextInputDelete, [&](TextDelete deletion) {
if (inDelete) inDelete();
heard.push_back(std::format("delete {} {}", deletion.beforeBytes, deletion.afterBytes));
});
EventListener<const std::string_view> commitActs(&window.onTextInput, [&](const std::string_view text) {
if (inCommit) inCommit();
heard.push_back(std::format("commit {}", text));
});
EventListener<const std::string_view> commitTells(&window.onTextInput, [&](const std::string_view text) { heard.push_back(std::format("commit {}", text)); });
EventListener<const std::string_view> preeditActs(&window.onPreedit, [&](const std::string_view text) {
if (inPreedit) inPreedit();
heard.push_back(std::format("preedit {}", text));
});
EventListener<const std::string_view> preeditTells(&window.onPreedit, [&](const std::string_view text) { heard.push_back(std::format("preedit {}", text)); });
// One done from the input method: a deletion before the cursor, a
// commit and a composing text, each when given.
std::uint32_t serial = 0;
auto done = [&](std::uint32_t before, const char* commit, const char* preedit) {
if (preedit != nullptr) Device::text_input_preedit(nullptr, input, preedit, 0, 0);
if (commit != nullptr) Device::text_input_commit(nullptr, input, commit);
if (before > 0) Device::text_input_delete(nullptr, input, before, 0);
Device::text_input_done(nullptr, input, ++serial);
};
auto expectHeard = [&](const std::vector<std::string>& expected, std::string_view what) {
Check(heard == expected, what);
if (heard != expected) {
std::println(" heard {}", heard);
std::println(" expected {}", expected);
}
heard.clear();
};
inPreedit = [&] { window.SetTextInput(true, 14, 20, 2, 30, TextInputPurpose::Email); };
done(0, nullptr, "a@");
expectHeard({ "preedit a@", "preedit a@" },
"a listener of onPreedit that changes the purpose: it and the listener after it still read the composing text");
Expect(Sent(display, fds[1], id), { Enable(), Content(0, ZWP_TEXT_INPUT_V3_CONTENT_PURPOSE_EMAIL), Caret(14, 20, 2, 30), Commit() },
"and the field is enabled anew from inside the listener");
done(0, nullptr, "a@");
expectHeard({ "preedit a@", "preedit a@" }, "that enable ended the composition: the same composing text again is new");
Sent(display, fds[1], id);
inPreedit = [&] { window.SetTextInput(false); };
done(0, nullptr, "bc");
expectHeard({ "preedit bc", "preedit bc" },
"a listener of onPreedit that disables the field: it and the listener after it still read the composing text");
Expect(Sent(display, fds[1], id), { Disable(), Commit() }, "and the field is disabled from inside the listener");
inPreedit = nullptr;
window.SetTextInput(true, 14, 20, 2, 30);
Sent(display, fds[1], id);
inDelete = [&] { window.SetTextInput(true, 14, 20, 2, 30, TextInputPurpose::Normal, kTextInputHintNone); };
done(3, "the ", "q");
expectHeard({ "delete 3 0", "commit the ", "commit the " },
"a listener of onTextInputDelete that changes the hints: the commit of the same done still arrives, its composing text does not");
Expect(Sent(display, fds[1], id), { Enable(), Content(0, ZWP_TEXT_INPUT_V3_CONTENT_PURPOSE_NORMAL), Caret(14, 20, 2, 30), Commit() },
"and the field is enabled anew from inside the listener");
inDelete = nullptr;
done(0, nullptr, "q");
expectHeard({ "preedit q", "preedit q" }, "the composing text that was not reported is new when the input method sends it again");
inDelete = [&] { window.SetTextInput(false); };
done(1, "k", nullptr);
expectHeard({ "delete 1 0", "commit k", "commit k" },
"a listener of onTextInputDelete that disables the field: the commit of the same done still arrives, and no onPreedit after it");
Expect(Sent(display, fds[1], id), { Disable(), Commit() }, "and the field is disabled from inside the listener");
inDelete = nullptr;
window.SetTextInput(true, 14, 20, 2, 30);
Sent(display, fds[1], id);
// What a listener that dispatches the display can bring: the next
// done, once, and the compositor's leave. The next done waits for
// the one being delivered, so the window hears the two in the order
// the input method sent them.
bool fed = false;
inDelete = [&] {
if (fed) return;
fed = true;
done(0, "x", nullptr);
};
done(1, "e", nullptr);
expectHeard({ "delete 1 0", "commit e", "commit e", "commit x", "commit x", "preedit ", "preedit " },
"a later done from inside a listener of onTextInputDelete: its commit arrives after the commit of the done being delivered");
inDelete = nullptr;
fed = false;
inCommit = [&] {
if (fed) return;
fed = true;
done(0, nullptr, "new");
};
done(0, "c", "old");
expectHeard({ "commit c", "commit c", "preedit new", "preedit new" },
"a later done from inside a listener of onTextInput: it is delivered after that listener's done, whose composing text is not reported");
inCommit = [&] { Device::text_input_leave(nullptr, input, window.surface); };
done(0, "d", "old");
expectHeard({ "preedit ", "preedit ", "commit d", "commit d" },
"a leave from inside a listener of onTextInput: the window hears its composing text is gone, and the done's is not reported after it");
inCommit = nullptr;
Expect(Sent(display, fds[1], id), {}, "and nothing is sent for a window the text input has left");
}
// A listener of onTextInputDelete destroys the window: the done fires
// nothing more through its pointer. Another window is made in its place,
// at the same address, and listed on the device as Window's constructor
// lists every window it opens, so an event fired through the old pointer
// would show, and a handler that only looked the address up in
// Device::windows would take the new window for the old one.
{
std::optional<Window> holder;
holder.emplace();
holder->surface = static_cast<wl_surface*>(wl_registry_bind(registry, 4, &wl_surface_interface, 1));
Device::windows.push_back(&*holder);
Device::text_input_enter(nullptr, input, holder->surface);
int firedOnReplacement = 0;
std::optional<EventListener<const std::string_view>> commitSub, preeditSub;
EventListener<TextDelete> deleteSub(&holder->onTextInputDelete, [&](TextDelete) {
// A later done for the window, which waits its turn, and then
// the window goes.
Device::text_input_commit(nullptr, input, "z");
Device::text_input_done(nullptr, input, 2);
holder.reset();
Window& replacement = holder.emplace();
Device::windows.push_back(&replacement);
commitSub.emplace(&replacement.onTextInput, [&](const std::string_view) { ++firedOnReplacement; });
preeditSub.emplace(&replacement.onPreedit, [&](const std::string_view) { ++firedOnReplacement; });
});
Device::text_input_preedit(nullptr, input, "p", 0, 0);
Device::text_input_commit(nullptr, input, "c");
Device::text_input_delete(nullptr, input, 1, 0);
Device::text_input_done(nullptr, input, 1);
Check(std::ranges::contains(Device::windows, &*holder), "the window made in the destroyed one's place is listed at its address");
Check(firedOnReplacement == 0, "a window destroyed from onTextInputDelete: neither the rest of that done nor the done waiting behind it fires through the handler's pointer");
commitSub.reset();
preeditSub.reset();
}
const int error = wl_display_get_error(display);
Check(error == 0, "the display took every request");
if (error != 0) std::println(" the display failed: {}", std::strerror(error));
Device::textInput = nullptr;
Device::display = nullptr;
zwp_text_input_v3_destroy(input);
wl_registry_destroy(registry);
wl_display_disconnect(display);
close(fds[1]);
if (failures != 0) {
std::println("{} check(s) failed", failures);
return EXIT_FAILURE;
}
std::println("all checks passed");
return EXIT_SUCCESS;
}