hf_repo4 / src /core /input.cpp
fava21's picture
Backup: patch pack applied + verified on Win11 22000 (d446cc1, ba8617e, dcf5cb8)
2edf277 verified
Raw History Blame Contribute Delete
7.6 kB
// FCS Glass - src/core/input.cpp
//
// Implements: C3, C4, E11. See src/core/input.h for the three Windows facts
// this encodes.
#include "core/input.h"
namespace fcs {
namespace core {
namespace {
// UTF-16 surrogate ranges. A non-BMP character is delivered as a high
// surrogate followed by a low one, in two separate WM_CHAR messages.
constexpr unsigned kHighSurrogateFirst = 0xD800u;
constexpr unsigned kHighSurrogateLast = 0xDBFFu;
constexpr unsigned kLowSurrogateFirst = 0xDC00u;
constexpr unsigned kLowSurrogateLast = 0xDFFFu;
bool IsHighSurrogate(unsigned u) {
return u >= kHighSurrogateFirst && u <= kHighSurrogateLast;
}
bool IsLowSurrogate(unsigned u) {
return u >= kLowSurrogateFirst && u <= kLowSurrogateLast;
}
} // namespace
// REQ C4
int KeyBit(Key key) { return 1 << static_cast<int>(key); }
// --- accumulation ----------------------------------------------------------
void InputFrame::OnMouseMove(int x, int y) {
acc_.mouse_x = x;
acc_.mouse_y = y;
}
void InputFrame::OnMouseButton(int button, bool down) {
if (button < 0 || button > 30) return; // a stray index must not shift the mask
const int bit = 1 << button;
if (down) {
// The edge is recorded on the TRANSITION, not on "a down arrived". Windows
// repeats nothing for the mouse, but a second WM_LBUTTONDOWN while the
// button is already held would otherwise read as a fresh click.
if ((mouse_down_ & bit) == 0) mouse_pressed_ |= bit;
mouse_down_ |= bit;
} else {
mouse_down_ &= ~bit;
}
}
// E11: WM_KILLFOCUS / WM_CANCELMODE arrive with no matching button-up.
void InputFrame::ReleaseAllButtons() { mouse_down_ = 0; }
// REQ C4 - wheel is ACCUMULATED across the frame. Two notches arriving as two
// messages are one scroll of two, not two scrolls of one, and a text field
// scrolled per message overshoots.
void InputFrame::OnWheel(int delta) { acc_.wheel += delta; }
void InputFrame::OnKey(Key key, bool down) {
if (key == Key::None) return;
const int bit = KeyBit(key);
if (down) {
// KEY REPEAT is the reason this is an edge and not "a down arrived". Holding
// Tab sends WM_KEYDOWN ~30 times a second; recording an edge each time would
// walk the focus ring 30 entries a second instead of one.
if ((keys_down_ & bit) == 0) keys_pressed_ |= bit;
keys_down_ |= bit;
} else {
keys_down_ &= ~bit;
}
}
// E11: WM_KILLFOCUS / WM_CANCELMODE arrive with no matching key-up.
void InputFrame::ReleaseAllKeys() { keys_down_ = 0; }
// REQ E11 - surrogate pairing. A lone surrogate is not a character, so it is
// dropped and counted rather than handed to a widget as a replacement glyph.
void InputFrame::OnCharUnit(unsigned unit) {
if (IsHighSurrogate(unit)) {
// A second high surrogate while one is pending means the first was orphaned
// (an input method, a paste that truncated). Drop the stale one.
if (pending_high_ != 0) ++dropped_;
pending_high_ = static_cast<int>(unit);
return;
}
if (IsLowSurrogate(unit)) {
if (pending_high_ == 0) { ++dropped_; return; } // unpaired low
const unsigned cp = 0x10000u +
((static_cast<unsigned>(pending_high_) - kHighSurrogateFirst) << 10) +
(unit - kLowSurrogateFirst);
pending_high_ = 0;
EmitChar(static_cast<int>(cp));
return;
}
// A plain BMP character after a pending high surrogate: that high surrogate
// will never be completed.
if (pending_high_ != 0) { pending_high_ = 0; ++dropped_; }
EmitChar(static_cast<int>(unit));
}
void InputFrame::EmitChar(int code_point) {
if (acc_.char_count >= InputSnapshot::kMaxChars) { ++dropped_; return; }
acc_.chars[acc_.char_count++] = code_point;
}
// REQ E11 - Esc and a lost capture both cancel, and neither ends the drag
// quietly: the cancellation is reported for one frame so the widget can react.
void InputFrame::OnEscape() {
if (drag_active_) drag_cancelled_ = true;
// Goes through the LEVEL state, not acc_. Snapshot() overwrites out_.keys from
// keys_down_, so setting the bit on acc_ alone would publish a frame in which
// Escape was pressed, the drag was cancelled, and the key read as up.
OnKey(Key::Escape, true);
}
void InputFrame::OnCaptureChanged() {
if (drag_active_) drag_cancelled_ = true;
}
// REQ E11 - a second press must not orphan an in-flight drag.
bool InputFrame::BeginDrag() {
if (drag_active_) return false;
drag_active_ = true;
drag_cancelled_ = false;
return true;
}
void InputFrame::EndDrag() { drag_active_ = false; }
// REQ C3 - the freeze. acc_ becomes out_, and acc_ is emptied ready for the next
// frame, so anything that arrives afterwards belongs to the next frame and
// cannot disturb this one.
const InputSnapshot& InputFrame::Snapshot() {
// A high surrogate still waiting at the end of a frame was never completed -
// its partner arrives next frame, and emitting it here would put half a
// character in this frame's text.
if (pending_high_ != 0) { pending_high_ = 0; ++dropped_; }
out_ = acc_;
out_.frame = ++frame_;
// Levels are published from persistent state, NOT from acc_: Windows sends one
// WM_LBUTTONDOWN for a whole held click, so a bitmask assembled purely from
// this frame's messages would read "released" on every frame after the first -
// and every drag would end after exactly one frame.
out_.keys = keys_down_;
out_.mouse_buttons = mouse_down_;
// Edges, then cleared: "pressed" is true for exactly one frame by definition.
out_.keys_pressed = keys_pressed_;
out_.mouse_pressed = mouse_pressed_;
keys_pressed_ = 0;
mouse_pressed_ = 0;
out_.drag_cancelled = drag_cancelled_;
acc_ = InputSnapshot{};
drag_cancelled_ = false; // a cancellation is one frame, not sticky
return out_;
}
// --- focus ring (C4) --------------------------------------------------------
Id FocusRing::focused_id() const {
if (focused_ < 0 || focused_ >= static_cast<int>(ids_.size())) return kNullId;
return ids_[static_cast<std::size_t>(focused_)];
}
// REQ C4
void FocusRing::SetEnabled(int index, bool enabled) {
if (index < 0 || index >= static_cast<int>(ids_.size())) return;
// Assign ONE entry. This used to re-assign the whole vector to true first,
// which silently re-enabled the target of every EARLIER call - so disabling
// two entries in a row left the first one enabled again. T-19 caught it, and
// only because it disabled two.
enabled_[static_cast<std::size_t>(index)] = enabled;
// Losing focus to a disabled entry is worse than losing it, so step on.
if (focused_ == index && !enabled) focused_ = -1;
}
int FocusRing::NextEnabled(int from, int step) const {
const int n = static_cast<int>(ids_.size());
for (int k = 1; k <= n; ++k) {
int i = (from + step * k) % n;
if (i < 0) i += n; // C++ % keeps the sign of the dividend
const std::size_t u = static_cast<std::size_t>(i);
if (u < enabled_.size() && enabled_[u]) return i;
}
return -1; // every entry disabled
}
bool FocusRing::MoveNext() {
const int nxt = NextEnabled(focused_ < 0 ? -1 : focused_, 1);
if (nxt < 0) return false;
focused_ = nxt;
return true;
}
bool FocusRing::MovePrev() {
const int nxt = NextEnabled(focused_ < 0 ? 0 : focused_, -1);
if (nxt < 0) return false;
focused_ = nxt;
return true;
}
} // namespace core
} // namespace fcs