505 lines
27 KiB
C++
505 lines
27 KiB
C++
|
|
//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;
|
||
|
|
}
|