Crafter.Graphics/interfaces/Crafter.Graphics-Types.cppm

309 lines
13 KiB
Text
Raw Normal View History

2026-07-22 18:09:06 +02:00
//SPDX-License-Identifier: LGPL-3.0-only
//SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
2025-05-07 19:21:51 +02:00
2026-01-28 23:37:12 +01:00
module;
2026-05-18 02:07:48 +02:00
// DOM builds drop the Vulkan header — DescriptorBinding (the only
// Vk-typed entity here) compiles out under the same guard below.
#ifndef CRAFTER_GRAPHICS_WINDOW_DOM
2026-03-02 23:53:13 +01:00
#include "vulkan/vulkan.h"
2026-05-18 02:07:48 +02:00
#endif
2025-04-16 00:43:33 +02:00
export module Crafter.Graphics:Types;
2025-11-16 15:32:11 +01:00
import std;
2026-03-09 20:10:19 +01:00
import Crafter.Math;
2025-04-16 00:43:33 +02:00
2025-12-30 23:28:38 +01:00
export namespace Crafter {
struct MouseMoveEvent {
2026-03-09 20:10:19 +01:00
Vector<float, 2> lastMousePos;
Vector<float, 2> currentMousePos;
Vector<float, 2> mouseDelta;
2025-12-30 23:28:38 +01:00
};
// One finger on a touchscreen, in the window's physical pixels; the id is
// the compositor's and stays with the finger until it lifts.
struct TouchPoint {
std::int32_t id;
Vector<float, 2> position;
};
// The input method asks for bytes around the cursor to go (text-input-v3
// counts UTF-8 bytes).
struct TextDelete {
std::uint32_t beforeBytes;
std::uint32_t afterBytes;
};
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.
2026-11-20 08:08:54 +01:00
// What a text field holds, which the input method picks its layout by (a
// number pad for Digits, an @ key for Email): text-input-v3's content
// purposes, with the protocol's values.
enum class TextInputPurpose : std::uint8_t {
Normal, // any text
Alpha, // letters only
Digits, // digits only
Number, // a number, with its sign and decimal separator
Phone,
Url,
Email,
Name, // a person's name
Password,
Pin, // a password of digits
Date,
Time,
Datetime,
Terminal,
};
// How the input method treats what is typed there: text-input-v3's
// content hints, or-ed together, with the protocol's values. These are
// the hints of version 1 of the protocol, the version the library binds;
// kTextInputHintMask is all of them.
inline constexpr std::uint32_t kTextInputHintNone = 0x0;
inline constexpr std::uint32_t kTextInputHintCompletion = 0x1; // suggest completions
inline constexpr std::uint32_t kTextInputHintSpellcheck = 0x2; // suggest corrections
inline constexpr std::uint32_t kTextInputHintAutoCapitalization = 0x4; // a capital at the start of a sentence
inline constexpr std::uint32_t kTextInputHintLowercase = 0x8; // prefer lowercase letters
inline constexpr std::uint32_t kTextInputHintUppercase = 0x10; // prefer capitals
inline constexpr std::uint32_t kTextInputHintTitlecase = 0x20; // a capital at the start of each word
inline constexpr std::uint32_t kTextInputHintHiddenText = 0x40; // the field shows no characters: show none either
inline constexpr std::uint32_t kTextInputHintSensitiveData = 0x80; // do not store or learn what is typed
inline constexpr std::uint32_t kTextInputHintLatin = 0x100; // prefer Latin characters
inline constexpr std::uint32_t kTextInputHintMultiline = 0x200; // the text may have line breaks
inline constexpr std::uint32_t kTextInputHintMask = 0x3FF;
// The hints Window::SetTextInput gives a field whose caller names none:
// prose is completed, checked and capitalised; a password or a PIN is
// neither shown nor remembered; everything else is taken as typed, since
// a phone number, an address or an account number is no word to correct.
constexpr std::uint32_t TextInputDefaultHints(TextInputPurpose purpose) {
switch (purpose) {
case TextInputPurpose::Normal:
return kTextInputHintCompletion | kTextInputHintSpellcheck | kTextInputHintAutoCapitalization;
case TextInputPurpose::Password:
case TextInputPurpose::Pin:
return kTextInputHintHiddenText | kTextInputHintSensitiveData;
default:
return kTextInputHintNone;
}
}
2026-03-09 20:10:19 +01:00
struct ScaleData2D {
2026-04-09 00:15:09 +02:00
Vector<std::int16_t, 2, 2> position;
Vector<std::int16_t, 2, 2> size;
2025-11-22 20:58:42 +01:00
};
2025-11-26 20:15:25 +01:00
2025-12-30 23:28:38 +01:00
struct ClipRect {
2026-03-22 21:08:02 +01:00
std::uint16_t left;
std::uint16_t right;
std::uint16_t top;
std::uint16_t bottom;
2025-11-26 20:15:25 +01:00
};
2025-04-16 00:43:33 +02:00
2025-12-30 23:28:38 +01:00
struct FrameTime {
2025-11-24 03:38:20 +01:00
std::chrono::time_point<std::chrono::high_resolution_clock> now;
std::chrono::duration<double> delta;
};
2025-11-23 04:04:53 +01:00
2026-04-16 23:03:24 +02:00
enum class CrafterKeys : std::uint8_t {
2025-12-28 00:05:03 +01:00
// Alphabetic keys
A, B, C, D, E, F, G, H, I, J, K, L, M,
N, O, P, Q, R, S, T, U, V, W, X, Y, Z,
// Numeric keys
_0, _1, _2, _3, _4, _5, _6, _7, _8, _9,
// Function keys
F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12,
// Control keys
Escape, Tab, Enter, Space, Backspace, Delete, Insert,
Home, End, PageUp, PageDown, CapsLock, NumLock, ScrollLock,
// Modifier keys
LeftShift, RightShift, LeftCtrl, RightCtrl,
LeftAlt, RightAlt, LeftSuper, RightSuper,
// Arrow keys
Up, Down, Left, Right,
// Keypad keys
keypad_0, keypad_1, keypad_2, keypad_3, keypad_4,
keypad_5, keypad_6, keypad_7, keypad_8, keypad_9,
keypad_enter, keypad_plus, keypad_minus, keypad_multiply,
keypad_divide, keypad_decimal,
// Punctuation and special keys
grave, minus, equal, bracket_left, bracket_right,
backslash, semicolon, quote, comma, period, slash,
print_screen, pause, menu,
// Additional keys
volume_up, volume_down, volume_mute,
media_play, media_stop, media_prev, media_next,
browser_back, browser_forward, browser_refresh,
browser_stop, browser_search, browser_home,
launch_mail, launch_calculator, launch_media_player,
CrafterKeysMax
};
2026-01-28 23:37:12 +01:00
2026-04-16 23:03:24 +02:00
constexpr std::string CrafterKeyToString(CrafterKeys key) {
switch (key) {
// Alphabetic keys
case CrafterKeys::A: return "A";
case CrafterKeys::B: return "B";
case CrafterKeys::C: return "C";
case CrafterKeys::D: return "D";
case CrafterKeys::E: return "E";
case CrafterKeys::F: return "F";
case CrafterKeys::G: return "G";
case CrafterKeys::H: return "H";
case CrafterKeys::I: return "I";
case CrafterKeys::J: return "J";
case CrafterKeys::K: return "K";
case CrafterKeys::L: return "L";
case CrafterKeys::M: return "M";
case CrafterKeys::N: return "N";
case CrafterKeys::O: return "O";
case CrafterKeys::P: return "P";
case CrafterKeys::Q: return "Q";
case CrafterKeys::R: return "R";
case CrafterKeys::S: return "S";
case CrafterKeys::T: return "T";
case CrafterKeys::U: return "U";
case CrafterKeys::V: return "V";
case CrafterKeys::W: return "W";
case CrafterKeys::X: return "X";
case CrafterKeys::Y: return "Y";
case CrafterKeys::Z: return "Z";
// Numeric keys
case CrafterKeys::_0: return "0";
case CrafterKeys::_1: return "1";
case CrafterKeys::_2: return "2";
case CrafterKeys::_3: return "3";
case CrafterKeys::_4: return "4";
case CrafterKeys::_5: return "5";
case CrafterKeys::_6: return "6";
case CrafterKeys::_7: return "7";
case CrafterKeys::_8: return "8";
case CrafterKeys::_9: return "9";
// Function keys
case CrafterKeys::F1: return "F1";
case CrafterKeys::F2: return "F2";
case CrafterKeys::F3: return "F3";
case CrafterKeys::F4: return "F4";
case CrafterKeys::F5: return "F5";
case CrafterKeys::F6: return "F6";
case CrafterKeys::F7: return "F7";
case CrafterKeys::F8: return "F8";
case CrafterKeys::F9: return "F9";
case CrafterKeys::F10: return "F10";
case CrafterKeys::F11: return "F11";
case CrafterKeys::F12: return "F12";
// Control keys
case CrafterKeys::Escape: return "Escape";
case CrafterKeys::Tab: return "Tab";
case CrafterKeys::Enter: return "Enter";
case CrafterKeys::Space: return "Space";
case CrafterKeys::Backspace: return "Backspace";
case CrafterKeys::Delete: return "Delete";
case CrafterKeys::Insert: return "Insert";
case CrafterKeys::Home: return "Home";
case CrafterKeys::End: return "End";
case CrafterKeys::PageUp: return "PageUp";
case CrafterKeys::PageDown: return "PageDown";
case CrafterKeys::CapsLock: return "CapsLock";
case CrafterKeys::NumLock: return "NumLock";
case CrafterKeys::ScrollLock: return "ScrollLock";
// Modifier keys
case CrafterKeys::LeftShift: return "LeftShift";
case CrafterKeys::RightShift: return "RightShift";
case CrafterKeys::LeftCtrl: return "LeftCtrl";
case CrafterKeys::RightCtrl: return "RightCtrl";
case CrafterKeys::LeftAlt: return "LeftAlt";
case CrafterKeys::RightAlt: return "RightAlt";
case CrafterKeys::LeftSuper: return "LeftSuper";
case CrafterKeys::RightSuper: return "RightSuper";
// Arrow keys
case CrafterKeys::Up: return "Up";
case CrafterKeys::Down: return "Down";
case CrafterKeys::Left: return "Left";
case CrafterKeys::Right: return "Right";
// Keypad keys
case CrafterKeys::keypad_0: return "Keypad0";
case CrafterKeys::keypad_1: return "Keypad1";
case CrafterKeys::keypad_2: return "Keypad2";
case CrafterKeys::keypad_3: return "Keypad3";
case CrafterKeys::keypad_4: return "Keypad4";
case CrafterKeys::keypad_5: return "Keypad5";
case CrafterKeys::keypad_6: return "Keypad6";
case CrafterKeys::keypad_7: return "Keypad7";
case CrafterKeys::keypad_8: return "Keypad8";
case CrafterKeys::keypad_9: return "Keypad9";
case CrafterKeys::keypad_enter: return "KeypadEnter";
case CrafterKeys::keypad_plus: return "KeypadPlus";
case CrafterKeys::keypad_minus: return "KeypadMinus";
case CrafterKeys::keypad_multiply: return "KeypadMultiply";
case CrafterKeys::keypad_divide: return "KeypadDivide";
case CrafterKeys::keypad_decimal: return "KeypadDecimal";
// Punctuation and special keys
case CrafterKeys::grave: return "Grave";
case CrafterKeys::minus: return "Minus";
case CrafterKeys::equal: return "Equal";
case CrafterKeys::bracket_left: return "BracketLeft";
case CrafterKeys::bracket_right: return "BracketRight";
case CrafterKeys::backslash: return "Backslash";
case CrafterKeys::semicolon: return "Semicolon";
case CrafterKeys::quote: return "Quote";
case CrafterKeys::comma: return "Comma";
case CrafterKeys::period: return "Period";
case CrafterKeys::slash: return "Slash";
case CrafterKeys::print_screen: return "PrintScreen";
case CrafterKeys::pause: return "Pause";
case CrafterKeys::menu: return "Menu";
// Additional keys
case CrafterKeys::volume_up: return "VolumeUp";
case CrafterKeys::volume_down: return "VolumeDown";
case CrafterKeys::volume_mute: return "VolumeMute";
case CrafterKeys::media_play: return "MediaPlay";
case CrafterKeys::media_stop: return "MediaStop";
case CrafterKeys::media_prev: return "MediaPrev";
case CrafterKeys::media_next: return "MediaNext";
case CrafterKeys::browser_back: return "BrowserBack";
case CrafterKeys::browser_forward: return "BrowserForward";
case CrafterKeys::browser_refresh: return "BrowserRefresh";
case CrafterKeys::browser_stop: return "BrowserStop";
case CrafterKeys::browser_search: return "BrowserSearch";
case CrafterKeys::browser_home: return "BrowserHome";
case CrafterKeys::launch_mail: return "LaunchMail";
case CrafterKeys::launch_calculator: return "LaunchCalculator";
case CrafterKeys::launch_media_player:return "LaunchMediaPlayer";
case CrafterKeys::CrafterKeysMax: return "Unknown";
}
return "Unknown";
}
2026-01-29 01:31:17 +01:00
template <typename T, typename T2>
constexpr T AlignUp(T value, T2 alignment) {
return (value + alignment - 1) & ~(alignment - 1);
}
2026-02-22 00:46:38 +01:00
template <typename T>
constexpr T GetTanHalfFov(T fov) {
return std::tan(fov * std::numbers::pi / 360.0);
}
2026-05-18 02:07:48 +02:00
#ifndef CRAFTER_GRAPHICS_WINDOW_DOM
2026-01-28 23:37:12 +01:00
struct DescriptorBinding {
VkDescriptorType type;
std::uint32_t slot;
};
2026-05-18 02:07:48 +02:00
#endif
2025-04-16 00:43:33 +02:00
}