File size: 7,596 Bytes
2edf277 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 | // 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
|