forked from K5WH-Walter/packcom
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput.cpp
More file actions
341 lines (298 loc) · 11.1 KB
/
Copy pathinput.cpp
File metadata and controls
341 lines (298 loc) · 11.1 KB
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
// ============================================================================
// input.cpp
// ============================================================================
#include "input.h"
#include "keys.h"
#include <cstdio>
#include <cstring>
#include <algorithm>
InputManager::InputManager(PackComConfig& cfg, SendFn on_send)
: cfg_(cfg), on_send_(on_send) {}
// ---------------------------------------------------------------------------
// Macro file (binary format: each entry = 1-byte length + text)
// We use simple text format for portability
// ---------------------------------------------------------------------------
bool InputManager::load_macros(const std::string& path) {
FILE* f = ::fopen(path.c_str(), "r");
if (!f) return false;
char line[512];
int idx = 0;
while (::fgets(line, sizeof(line), f) && idx < NUM_MACRO_KEYS) {
size_t len = ::strlen(line);
while (len > 0 && (line[len-1]=='\r'||line[len-1]=='\n')) line[--len]=0;
// Format: LABEL\tTEXT
char* tab = ::strchr(line, '\t');
if (tab) {
*tab = 0;
macros_[idx].label = line;
macros_[idx].text = tab+1;
} else {
macros_[idx].label = line;
macros_[idx].text = "";
}
++idx;
}
::fclose(f);
return true;
}
bool InputManager::save_macros(const std::string& path) const {
FILE* f = ::fopen(path.c_str(), "w");
if (!f) return false;
for (auto& m : macros_)
::fprintf(f, "%s\t%s\n", m.label.c_str(), m.text.c_str());
::fclose(f);
return true;
}
// ---------------------------------------------------------------------------
// Macro expansion
// ] = CR, @x = command char (sends text as TNC cmd), ~ = 500ms delay, ; = comment
// ---------------------------------------------------------------------------
void InputManager::expand_and_send(const std::string& macro_text) {
std::string out;
bool in_comment = false;
for (size_t i = 0; i < macro_text.size() && !in_comment; ++i) {
char c = macro_text[i];
if (c == ']') {
out += '\r';
} else if (c == '~') {
if (on_send_ && !out.empty()) { on_send_(out); out.clear(); }
Platform::sleep_ms(500);
} else if (c == ';') {
in_comment = true; // rest of string is a comment
} else if (c == '@' && i+1 < macro_text.size()) {
// @x means send as TNC command; for now just include the char
out += macro_text[++i];
} else {
out += c;
}
}
if (on_send_ && !out.empty()) on_send_(out);
}
// ---------------------------------------------------------------------------
// Key processing
// ---------------------------------------------------------------------------
int InputManager::macro_index(int key) {
// Map F-keys and their shifted/ctrl/alt variants to macro slots
if (key >= KEY_F1 && key >= KEY_F1 - 10) return -(key - KEY_F1);
return -1;
}
void InputManager::insert_char(char c) {
if (insert_mode_) {
line_.insert(line_.begin() + cursor_, c);
} else {
if (cursor_ < (int)line_.size())
line_[cursor_] = c;
else
line_ += c;
}
++cursor_;
}
void InputManager::commit_line() {
if (on_send_) on_send_(line_);
if (!line_.empty()) {
history_.push_front(line_);
if ((int)history_.size() > 50) history_.pop_back();
}
line_.clear();
cursor_ = 0;
history_pos_= -1;
}
bool InputManager::process_key(int key) {
// --- Character mode: send immediately, no editing ---
if (mode_ == InputMode::Char) {
if (key > 0 && key < 256) {
std::string s(1, (char)key);
if (on_send_) on_send_(s);
}
return true;
}
// --- Line mode ---
// Navigation / editing (recovered from binary help screen)
switch (key) {
// (Left Arrow) = Move Cursor to Left
case KEY_LEFT:
if (cursor_ > 0) --cursor_;
return true;
// (Right Arrow) = Move Cursor to Right
case KEY_RIGHT:
if (cursor_ < (int)line_.size()) ++cursor_;
return true;
// (Cntl Home) = Beginning of Line
case KEY_CTRL_HOME:
cursor_ = 0;
return true;
// (End) = End of Line
case KEY_END:
cursor_ = (int)line_.size();
return true;
// (Cntl End) = Erase from Cursor to End of Line
case KEY_CTRL_END:
line_.erase(cursor_);
return true;
// (Ins) = Toggle Insert Mode
case KEY_INS:
insert_mode_ = !insert_mode_;
return true;
// (Del) = Delete Current Character
case KEY_DEL:
if (cursor_ < (int)line_.size())
line_.erase(cursor_, 1);
return true;
// (Back Space) = Delete Previous Character
case 127:
if (cursor_ > 0) {
line_.erase(--cursor_, 1);
}
return true;
// (Cntl Left Arrow) = Tab Cursor Left
case KEY_CTRL_LEFT: {
if (cursor_ <= 0) return true;
--cursor_;
while (cursor_ > 0 && line_[cursor_-1] != ' ') --cursor_;
return true;
}
// (Cntl Right Arrow) = Tab Cursor Right
case KEY_CTRL_RIGHT: {
int sz = (int)line_.size();
while (cursor_ < sz && line_[cursor_] != ' ') ++cursor_;
while (cursor_ < sz && line_[cursor_] == ' ') ++cursor_;
return true;
}
// (Cntl Page Up) = Scroll Keyboard Buffer Backward
case KEY_CTRL_PGUP:
if (!history_.empty()) {
if (history_pos_ < (int)history_.size() - 1) {
++history_pos_;
line_ = history_[history_pos_];
cursor_ = (int)line_.size();
}
}
return true;
// (Cntl Page Down) = Scroll Keyboard Buffer Forward
case KEY_CTRL_PGDN:
if (history_pos_ > 0) {
--history_pos_;
line_ = history_[history_pos_];
cursor_ = (int)line_.size();
} else if (history_pos_ == 0) {
history_pos_ = -1;
line_.clear();
cursor_ = 0;
}
return true;
// Enter: commit (KEY_ENTER == 13 == '\r', use only one)
case KEY_ENTER:
commit_line();
return true;
// ESC: clear line
case KEY_ESC:
line_.clear();
cursor_ = 0;
return true;
default:
break;
}
// Printable character
if (key >= 32 && key < 127) {
insert_char((char)key);
return true;
}
// Ctrl characters (pass-through for TNC commands)
if (key >= 1 && key < 32) {
insert_char((char)key);
return true;
}
return true;
}
// ---------------------------------------------------------------------------
// Draw the input line at the bottom of the screen
// ---------------------------------------------------------------------------
void InputManager::draw_input_line(int screen_row, int screen_cols) const {
Platform::goto_xy(screen_row, 0);
Platform::set_color(cfg_.colors.foreground, cfg_.colors.background);
// Mode indicator
std::string mode_label;
if (mode_ == InputMode::Line)
mode_label = " Key Board Input (LINE MODE) ";
else
mode_label = " Key Board Input (CHAR MODE) ";
Platform::set_color(cfg_.colors.rev_fg, cfg_.colors.rev_bg);
Platform::puts_raw(mode_label);
Platform::reset_color();
Platform::goto_xy(screen_row + 1, 0);
Platform::set_color(cfg_.colors.foreground, cfg_.colors.background);
// Clip line to screen width minus a small margin
std::string display = line_;
if ((int)display.size() >= screen_cols) display = display.substr(0, screen_cols - 1);
Platform::puts_raw(display);
// Pad rest with spaces
if ((int)display.size() < screen_cols)
Platform::puts_raw(std::string(screen_cols - display.size(), ' '));
// Position cursor
Platform::goto_xy(screen_row + 1, std::min(cursor_, screen_cols - 1));
Platform::reset_color();
}
// ---------------------------------------------------------------------------
// Macro editor menu
// ---------------------------------------------------------------------------
void InputManager::run_macro_menu() {
Platform::clear_screen();
Platform::goto_xy(1, 2);
Platform::puts_raw("Function Keys, Use: ] for CR, @x for CMDS, ~ for delay, ; for comment.");
Platform::goto_xy(2, 2);
Platform::puts_raw("Enter: C=Chg, F=PgFwd, B=PgBak, Q=Quit.");
// Labels: Unshifted, Shifted, Ctrl, Alt (recovered from .001)
const char* section_names[] = {"< Unshifted >", "< Shifted >", "< Ctrl >", "< Alt >"};
int page = 0; // 0-3 for the four sections
bool done = false;
while (!done) {
Platform::goto_xy(3, 2);
Platform::puts_raw(std::string(76, ' '));
Platform::goto_xy(3, 2);
Platform::puts_raw(section_names[page % 4]);
// Show 10 macros per page section
int base = (page % 4) * 10;
for (int i = 0; i < 10 && base+i < NUM_MACRO_KEYS; ++i) {
Platform::goto_xy(5 + i, 2);
char buf[80];
::snprintf(buf, sizeof(buf), "F%-2d %-40s",
i+1, macros_[base+i].text.c_str());
Platform::puts_raw(buf);
}
Platform::goto_xy(17, 2);
Platform::puts_raw("Enter: C=Chg, F=PgFwd, B=PgBak, Q=Quit. ");
int k = Platform::getch_raw();
switch (k) {
case 'c': case 'C': {
Platform::goto_xy(18, 2);
Platform::puts_raw("Which One? ");
std::string num_str;
int c2;
while ((c2 = Platform::getch_raw()) != KEY_ENTER && c2 != '\r') {
if (c2 >= '0' && c2 <= '9') { num_str += (char)c2; Platform::putch((char)c2); }
}
int fn = num_str.empty() ? 0 : ::atoi(num_str.c_str()) - 1;
if (fn >= 0 && fn < 10) {
int slot = base + fn;
Platform::goto_xy(19, 2);
Platform::puts_raw("New text: ");
std::string text;
char c3;
while ((c3 = (char)Platform::getch_raw()) != '\r' && c3 != KEY_ENTER) {
if ((unsigned char)c3 >= 32) { text += c3; Platform::putch(c3); }
else if (c3 == KEY_BACKSPACE && !text.empty()) {
text.pop_back(); Platform::putch('\b'); Platform::putch(' '); Platform::putch('\b');
}
}
macros_[slot].text = text;
}
break;
}
case 'f': case 'F': page = (page + 1) % 4; break;
case 'b': case 'B': page = (page + 3) % 4; break;
case 'q': case 'Q': done = true; break;
default: break;
}
}
save_macros();
}