hf_repo4 / src /core /input.h
fava21's picture
Backup: patch pack applied + verified on Win11 22000 (d446cc1, ba8617e, dcf5cb8)
2edf277 verified
Raw History Blame Contribute Delete
8.33 kB
// FCS Glass - src/core/input.h
//
// Implements: C3 (input is snapshotted once per frame, before any widget runs),
// C4 (Tab/Shift+Tab focus ring; Enter/Space activate; scroll),
// E11 (SetCapture during drags, WM_CAPTURECHANGED and Esc cancel,
// WM_CHAR surrogate pairs, paste strips newlines).
//
// PURE LOGIC, no Windows headers. See src/core/layout.h for why that is a hard
// requirement - tools/extract_logic.py lifts these bodies verbatim into
// tools/generated_logic.inc.
//
// THREE WINDOWS FACTS THIS FILE ENCODES, each of which is easy to get wrong:
//
// 1. **WM_CHAR delivers UTF-16 CODE UNITS, not characters.** Anything outside
// the BMP - every emoji, most symbols - arrives as a high surrogate then a
// low surrogate in two SEPARATE messages. Emitting each as its own
// character is how a text field ends up full of U+FFFD, and the bug is
// invisible in testing because nothing an engineer types is non-BMP. So the
// pairs are combined here, and an unpaired surrogate is DROPPED rather than
// passed on: a lone surrogate is not a character and must not reach a
// widget.
// 2. **Losing the mouse is not a drag ending.** WM_CAPTURECHANGED arrives when
// anything takes the capture - another window, a UAC prompt, a remote
// session switch. A drag that keeps running after that reports a pointer
// position nobody can move, so the drag has to CANCEL. Esc cancels too, and
// both surface as a cancellation for exactly one frame so the widget can
// react, rather than silently reverting.
// 3. **Input must be frozen before any widget runs.** If one widget reads the
// mouse and a later one reads it again in the same frame, and a WM_MOUSEMOVE
// arrives in between, they disagree - and which one is right depends on
// where in the frame the message landed. The snapshot makes that impossible
// rather than merely unlikely.
#ifndef FCS_CORE_INPUT_H
#define FCS_CORE_INPUT_H
#include "core/id.h"
#include <vector>
namespace fcs {
namespace core {
// The keys the core layer reasons about. Deliberately small: a virtual-key
// table here would be a second, worse copy of the SDK's.
enum class Key : int { None = 0, Tab, Escape, Enter, Space, Left, Right, Up, Down, Count };
int KeyBit(Key key);
// One frame of input, frozen. Read-only to widgets: if a widget could write
// here, the freeze would be a suggestion.
struct InputSnapshot {
static constexpr int kMaxChars = 16;
int frame = 0;
int mouse_x = 0;
int mouse_y = 0;
int mouse_buttons = 0; // bitmask, bit 0 = left
int wheel = 0; // accumulated over the frame, not per message
int keys = 0; // bitmask over Key, LEVEL
// Bitmasks of things that WENT DOWN since the previous snapshot. Windows sends
// one WM_LBUTTONDOWN for a whole held click, so level alone cannot tell "just
// pressed" from "still held" - and Tab held down would otherwise walk the focus
// ring at the keyboard repeat rate (~30 focus moves a second) instead of once.
int keys_pressed = 0;
int mouse_pressed = 0;
// CODE POINTS, already surrogate-paired. Not UTF-16 units.
int char_count = 0;
int chars[kMaxChars] = {};
// E11: a drag that was cancelled this frame. One frame only.
bool drag_cancelled = false;
bool KeyDown(Key key) const { return (keys & KeyBit(key)) != 0; }
bool MouseDown(int button) const { return (mouse_buttons & (1 << button)) != 0; }
bool KeyPressed(Key key) const { return (keys_pressed & KeyBit(key)) != 0; }
bool MousePressed(int button) const { return (mouse_pressed & (1 << button)) != 0; }
};
// Accumulates messages between frames, then freezes one frame's worth.
class InputFrame {
public:
// --- accumulation: called from the message pump, in any order -------------
void OnMouseMove(int x, int y);
void OnMouseButton(int button, bool down);
void OnWheel(int delta);
void OnKey(Key key, bool down);
// E11: WM_KILLFOCUS and WM_CANCELMODE. Windows does NOT deliver the matching
// WM_KEYUP when focus is lost, so without this a key held during Alt+Tab stays
// down for the rest of the session - and so does a mouse button pressed while
// a menu was up.
void ReleaseAllKeys();
void ReleaseAllButtons();
// E11: one UTF-16 code unit, exactly as WM_CHAR delivers it.
void OnCharUnit(unsigned unit);
// E11: Esc, and losing the capture. Both cancel an active drag.
void OnEscape();
void OnCaptureChanged();
// E11: a drag starts by taking the capture. Returns false when one is already
// active, so a second press cannot silently orphan the first drag.
bool BeginDrag();
void EndDrag();
bool drag_active() const { return drag_active_; }
// The platform layer needs these to decide whether releasing a mouse button
// should also release the capture - and a WndProc cannot see inside the
// accumulator. Read-only, so the freeze still holds.
int buttons_down() const { return mouse_down_; }
static int ButtonBit(int button) { return 1 << button; }
// C3: freeze. Call ONCE per frame, before any widget runs. Everything
// accumulated since the previous call is this frame; the accumulator is
// cleared ready for the next. Calling it twice would silently discard a
// frame, which is why frame() only advances on a real freeze.
//
// LIFETIME, because this returns a reference rather than a copy: the object is
// overwritten IN PLACE on the next call. Two snapshots cannot be held at once -
// the second reference names the same memory, and comparing them silently
// compares the newer frame with itself. Copy out what you need to keep. This is
// deliberate (a per-frame copy of this struct for every frame at 60 fps is
// waste) but it is a sharp edge, and the probe's first input row walked
// straight into it.
const InputSnapshot& Snapshot();
// Unpaired surrogates and characters dropped for want of room in the frame.
// Surfaced so a text field can show "[character limit]" rather than eating
// keystrokes without explanation.
int dropped_chars() const { return dropped_; }
private:
void EmitChar(int code_point);
// Two stages, and that is the whole point of the freeze. `acc_` collects
// messages between frames; `out_` is what widgets read. Snapshot() copies
// one into the other, so a message that arrives after the freeze lands in
// the accumulator and cannot change a snapshot a widget is already reading.
InputSnapshot acc_;
InputSnapshot out_;
// LEVEL, persistent across frames. Events update these; Snapshot() only
// publishes them. See Snapshot() for why this cannot live in acc_.
int keys_down_ = 0;
int mouse_down_ = 0;
// EDGES since the last Snapshot(). Cleared by Snapshot(), so "pressed" is
// true for exactly one frame.
int keys_pressed_ = 0;
int mouse_pressed_ = 0;
int pending_high_ = 0; // 0 = none waiting
bool drag_active_ = false;
bool drag_cancelled_ = false;
int dropped_ = 0;
int frame_ = 0;
};
// C4: Tab / Shift+Tab focus ring. Indices into a caller-owned list of ids, so
// this never holds a widget either.
class FocusRing {
public:
// REQ C4 - the ring starts fully enabled. Leaving enabled_ empty made
// NextEnabled find nothing and every Move fail, because the "is this entry
// enabled" check tests enabled_[i] rather than "not disabled". T-19 caught it.
void SetIds(const std::vector<Id>& ids) {
ids_ = ids;
enabled_.assign(ids_.size(), true);
focused_ = -1;
}
int count() const { return static_cast<int>(ids_.size()); }
int focused() const { return focused_; }
Id focused_id() const;
// Disabled entries are skipped, and an all-disabled ring is a no-op rather
// than a loop between two ends.
void SetEnabled(int index, bool enabled);
bool MoveNext(); // Tab
bool MovePrev(); // Shift+Tab
void Clear() { focused_ = -1; }
private:
int NextEnabled(int from, int step) const;
std::vector<Id> ids_;
std::vector<bool> enabled_;
int focused_ = -1;
};
} // namespace core
} // namespace fcs
#endif // FCS_CORE_INPUT_H