-
Notifications
You must be signed in to change notification settings - Fork 10
/
engine.h
226 lines (179 loc) · 8.95 KB
/
engine.h
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
//---------------------------------------------------------------------------
#pragma once
#include "orthography.h"
#include "processor/processor.h"
#include "segment_builder.h"
#include "static_allocate.h"
#include "steno_key_code_buffer.h"
#include "steno_key_code_emitter.h"
#include "stroke_history.h"
//---------------------------------------------------------------------------
class Console;
class Pattern;
class StenoDictionary;
class StenoReverseDictionaryLookup;
class StenoSegmentList;
class StenoUserDictionary;
//---------------------------------------------------------------------------
enum StenoEngineMode { NORMAL, ADD_TRANSLATION, CONSOLE };
//---------------------------------------------------------------------------
class StenoEngine final : public StenoProcessorElement {
public:
StenoEngine(StenoDictionary &dictionary,
const StenoCompiledOrthography &orthography,
StenoUserDictionary *userDictionary = nullptr);
size_t GetStrokeCount() const { return strokeCount; }
void Process(const StenoKeyState &value, StenoAction action);
void ProcessUndo();
void ProcessStroke(StenoStroke stroke);
bool ProcessScanCode(uint32_t scanCodeAndModifiers, ScanCodeAction action);
void SendText(const uint8_t *p);
void PrintInfo() const;
void PrintDictionary(const char *name) const;
void ListDictionaries() const;
bool EnableDictionary(const char *name);
bool DisableDictionary(const char *name);
bool ToggleDictionary(const char *name);
void ReverseLookup(StenoReverseDictionaryLookup &lookup) const;
StenoDictionary &GetDictionary() const { return dictionary; }
const StenoCompiledOrthography &GetOrthography() const { return orthography; }
bool IsPaperTapeEnabled() const { return paperTapeEnabled; }
void EnablePaperTape() { paperTapeEnabled = true; }
void DisablePaperTape() { paperTapeEnabled = false; }
bool IsSuggestionsEnabled() const { return suggestionsEnabled; }
void EnableSuggestions() { suggestionsEnabled = true; }
void DisableSuggestions() { suggestionsEnabled = false; }
bool IsTextLogEnabled() const { return textLogEnabled; }
void EnableTextLog() { textLogEnabled = true; }
void DisableTextLog() { textLogEnabled = false; }
bool IsSpaceAfter() const { return placeSpaceAfter; }
void SetSpaceAfter(bool spaceAfter) { placeSpaceAfter = spaceAfter; }
bool IsJoinNext() const { return state.joinNext; }
void AddConsoleCommands(Console &console);
static JavelinStaticAllocate<StenoEngine> container;
static StenoEngine &GetInstance() { return container.value; }
const char *GetTemplateValue(size_t index) const {
if (index >= TEMPLATE_VALUE_COUNT)
return "";
return templateValues[index].GetValue();
}
// data needs to be dynamically allocated, and StenoEngine will assume
// responsibility for its lifecycle/
void SetTemplateValue(size_t index, char *data);
char *ConvertText(StenoSegmentList &segments, size_t startingOffset);
private:
static const StenoStroke UNDO_STROKE;
static const size_t PAPER_TAPE_SUGGESTION_SEGMENT_LIMIT = 8;
bool paperTapeEnabled = false;
bool suggestionsEnabled = false;
bool templateValueUpdateEnabled = false;
bool textLogEnabled = false;
bool placeSpaceAfter = false;
StenoEngineMode mode = StenoEngineMode::NORMAL;
size_t strokeCount = 0;
StenoDictionary &dictionary;
const StenoCompiledOrthography orthography;
StenoUserDictionary *userDictionary;
StenoState state;
StenoState altTranslationState;
char *addTranslationText = nullptr;
StenoKeyCodeEmitter emitter;
StenoStrokeHistory history;
StenoStrokeHistory altTranslationHistory;
struct ConversionBuffer {
StenoSegmentBuilder segmentBuilder;
StenoKeyCodeBuffer keyCodeBuffer;
void Prepare(const StenoCompiledOrthography *orthography,
StenoDictionary *dictionary) {
keyCodeBuffer.Prepare(orthography, dictionary);
}
};
ConversionBuffer previousConversionBuffer;
ConversionBuffer nextConversionBuffer;
static const size_t TEMPLATE_VALUE_COUNT = 64;
struct TemplateValue {
char *value;
const char *GetValue() const { return value ? value : ""; }
void Set(char *newValue);
bool Set(size_t updateId, char *newValue);
};
TemplateValue templateValues[TEMPLATE_VALUE_COUNT] = {};
struct UpdateNormalModeTextBufferThreadData;
void ProcessNormalModeUndo();
void ProcessNormalModeStroke(StenoStroke stroke);
void InitiateAddTranslationMode(const char *text);
void ProcessAddTranslationModeUndo();
void ProcessAddTranslationModeStroke(StenoStroke stroke);
bool HandleAddTranslationModeScanCode(uint32_t scanCodeAndModifiers,
ScanCodeAction action);
void UpdateAddTranslationModeTextBuffer(ConversionBuffer &buffer);
void EndAddTranslationMode();
void InitiateConsoleMode();
void ProcessConsoleModeUndo();
void ProcessConsoleModeStroke(StenoStroke stroke);
bool HandleConsoleModeScanCode(uint32_t scanCodeAndModifiers,
ScanCodeAction action);
void UpdateConsoleModeTextBuffer(ConversionBuffer &buffer);
void ConsoleModeExecute();
void EndConsoleMode();
bool IsNewline(StenoStroke stroke) const;
void AddTranslation(size_t newlineIndex);
void DeleteTranslation(size_t newlineIndex);
void ResetState();
static void Pump();
void CreateSegments(StenoSegmentList &segments, StenoSegmentBuilder &builder,
const StenoStroke *strokes, size_t length);
void CreateSegments(size_t sourceStrokeCount, ConversionBuffer &buffer,
size_t conversionLimit, StenoSegmentList &segments,
bool allowSetValue);
void CreateSegmentsUsingLongerResult(size_t sourceStrokeCount,
ConversionBuffer &buffer,
size_t conversionLimit,
StenoSegmentList &segments,
const ConversionBuffer &longerBuffer,
const StenoSegmentList &longerSegments);
void ConvertText(StenoKeyCodeBuffer &keyCodeBuffer,
StenoSegmentList &segments, size_t startingOffset);
void PrintPaperTape(StenoStroke stroke,
const StenoSegmentList &previousSegments,
const StenoSegmentList &nextSegments) const;
void PrintPaperTapeUndo(size_t undoCount) const;
void PrintSuggestions(const StenoSegmentList &previousSegments,
const StenoSegmentList &nextSegments);
void PrintFingerSpellingSuggestions(const StenoSegmentList &previousSegments,
const StenoSegmentList &nextSegments);
void PrintSuggestion(const char *p, size_t arrowPrefixCount,
size_t strokeThreshold) const;
char *PrintSegmentSuggestion(size_t wordCount,
const StenoSegmentList &segments,
char *lastLookup);
void PrintTextLog(const StenoKeyCodeBuffer &previousKeyCodeBus,
const StenoKeyCodeBuffer &nextKeyCodeBuffer) const;
size_t GetStartingStrokeForNormalModeProcessing() const;
static void SetSpacePosition_Binding(void *context, const char *commandLine);
static void ListDictionaries_Binding(void *context, const char *commandLine);
static void EnableDictionary_Binding(void *context, const char *commandLine);
static void DisableDictionary_Binding(void *context, const char *commandLine);
static void ToggleDictionary_Binding(void *context, const char *commandLine);
static void PrintDictionary_Binding(void *context, const char *commandLine);
static void EnablePaperTape_Binding(void *context, const char *commandLine);
static void DisablePaperTape_Binding(void *context, const char *commandLine);
static void EnableSuggestions_Binding(void *context, const char *commandLine);
static void DisableSuggestions_Binding(void *context,
const char *commandLine);
static void EnableTextLog_Binding(void *context, const char *commandLine);
static void DisableTextLog_Binding(void *context, const char *commandLine);
static void Lookup_Binding(void *context, const char *commandLine);
static void LookupStroke_Binding(void *context, const char *commandLine);
static void RemoveStroke_Binding(void *context, const char *commandLine);
static void ProcessStrokes_Binding(void *context, const char *commandLine);
static void ListTemplateValues_Binding(void *context,
const char *commandLine);
static void SetTemplateValue_Binding(void *context, const char *commandLine);
static void EnableTemplateValueUpdate_Binding(void *context,
const char *commandLine);
static void DisableTemplateValueUpdate_Binding(void *context,
const char *commandLine);
friend class StenoEngineTester;
};
//---------------------------------------------------------------------------