From cbc3951ab1d86f5fe054986d8541b179ab4f313f Mon Sep 17 00:00:00 2001 From: Romain Goyet Date: Thu, 3 Sep 2020 23:00:20 -0400 Subject: [PATCH] [ion] Add an event journal It's pretty much just two callbacks that one can hook into to get some events in or out of Ion. It adds a couple useless checks and pointers to any build and could be hidden behind a feature flag, but the extra weight is minimal. --- ion/include/ion/events.h | 9 +++++++++ ion/src/shared/events_keyboard.cpp | 28 +++++++++++++++++++++++++++- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/ion/include/ion/events.h b/ion/include/ion/events.h index 95ad80ff4..6c1c64d76 100644 --- a/ion/include/ion/events.h +++ b/ion/include/ion/events.h @@ -53,6 +53,15 @@ enum class ShiftAlphaStatus { // Timeout is decremented Event getEvent(int * timeout); +class Journal { +public: + virtual void pushEvent(Event e) = 0; + virtual Event popEvent() = 0; +}; + +void replayFrom(Journal * l); +void logTo(Journal * l); + ShiftAlphaStatus shiftAlphaStatus(); void setShiftAlphaStatus(ShiftAlphaStatus s); void removeShift(); diff --git a/ion/src/shared/events_keyboard.cpp b/ion/src/shared/events_keyboard.cpp index 2c7c91c56..c368b785e 100644 --- a/ion/src/shared/events_keyboard.cpp +++ b/ion/src/shared/events_keyboard.cpp @@ -50,7 +50,11 @@ void resetLongRepetition() { ComputeAndSetRepetionFactor(sEventRepetitionCount); } -Event getEvent(int * timeout) { +static Keyboard::Key keyFromState(Keyboard::State state) { + return static_cast(63 - __builtin_clzll(state)); +} + +static inline Event innerGetEvent(int * timeout) { assert(*timeout > delayBeforeRepeat); assert(*timeout > delayBetweenRepeat); int time = 0; @@ -114,5 +118,27 @@ Event getEvent(int * timeout) { } } +static Journal * sSourceJournal = nullptr; +static Journal * sDestinationJournal = nullptr; +void replayFrom(Journal * l) { sSourceJournal = l; } +void logTo(Journal * l) { sDestinationJournal = l; } + +Event getEvent(int * timeout) { + if (sSourceJournal != nullptr) { + Event e = sSourceJournal->popEvent(); + if (e == None) { + sSourceJournal = nullptr; + } else { + return e; + } + } + Event e = innerGetEvent(timeout); + if (sDestinationJournal != nullptr && e != None) { + sDestinationJournal->pushEvent(e); + } + return e; +} + + } }