-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstates.c
456 lines (396 loc) · 11.4 KB
/
states.c
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
/*
* states.c
*
* qodem - Qodem Terminal Emulator
*
* Written 2003-2017 by Kevin Lamonte
*
* To the extent possible under law, the author(s) have dedicated all
* copyright and related and neighboring rights to this software to the
* public domain worldwide. This software is distributed without any
* warranty.
*
* You should have received a copy of the CC0 Public Domain Dedication along
* with this software. If not, see
* <http://creativecommons.org/publicdomain/zero/1.0/>.
*/
#include "qcurses.h"
#include "common.h"
#include <assert.h>
#include <sys/time.h>
#include <stdlib.h>
#include <string.h>
#include "qodem.h"
#include "console.h"
#include "script.h"
#include "protocols.h"
#include "keyboard.h"
#include "translate.h"
#include "screen.h"
#include "options.h"
#include "states.h"
/**
* Global state.
*/
Q_PROGRAM_STATE q_program_state;
/**
* Whether or not the keyboard is supposed to be blocking (last argument to
* nodelay()).
*/
Q_BOOL q_keyboard_blocks;
/**
* Look for input from the keyboard and mouse. If input came in, dispatch it
* to the appropriate keyboard handler for the current program state.
*/
void keyboard_handler() {
int flags = 0; /* passed to *_keyboard_handler() */
int keystroke; /* " */
/*
* Grab keystroke
*/
if (q_keyboard_blocks == Q_TRUE) {
qodem_getch(&keystroke, &flags, Q_KEYBOARD_DELAY);
} else {
qodem_getch(&keystroke, &flags, 0);
}
if (keystroke == Q_ERR) {
/*
* No data, return
*/
return;
}
switch (q_program_state) {
case Q_STATE_CONSOLE:
if (q_status.quicklearn == Q_FALSE) {
console_keyboard_handler(keystroke, flags);
} else {
console_quicklearn_keyboard_handler(keystroke, flags);
}
break;
case Q_STATE_CONSOLE_MENU:
console_menu_keyboard_handler(keystroke, flags);
break;
case Q_STATE_INFO:
console_info_keyboard_handler(keystroke, flags);
break;
case Q_STATE_SCROLLBACK:
scrollback_keyboard_handler(keystroke, flags);
break;
case Q_STATE_SCRIPT_EXECUTE:
script_keyboard_handler(keystroke, flags);
break;
case Q_STATE_HOST:
host_keyboard_handler(keystroke, flags);
break;
case Q_STATE_DOWNLOAD_MENU:
case Q_STATE_UPLOAD_MENU:
protocol_menu_keyboard_handler(keystroke, flags);
break;
case Q_STATE_DOWNLOAD_PATHDIALOG:
case Q_STATE_UPLOAD_PATHDIALOG:
case Q_STATE_UPLOAD_BATCH_DIALOG:
protocol_pathdialog_keyboard_handler(keystroke, flags);
break;
case Q_STATE_UPLOAD:
case Q_STATE_UPLOAD_BATCH:
case Q_STATE_DOWNLOAD:
protocol_transfer_keyboard_handler(keystroke, flags);
break;
case Q_STATE_EMULATION_MENU:
emulation_menu_keyboard_handler(keystroke, flags);
break;
case Q_STATE_TRANSLATE_MENU:
translate_table_menu_keyboard_handler(keystroke, flags);
break;
case Q_STATE_TRANSLATE_EDITOR_8BIT:
translate_table_editor_8bit_keyboard_handler(keystroke, flags);
break;
case Q_STATE_TRANSLATE_EDITOR_UNICODE:
translate_table_editor_unicode_keyboard_handler(keystroke, flags);
break;
case Q_STATE_PHONEBOOK:
phonebook_keyboard_handler(keystroke, flags);
break;
case Q_STATE_DIALER:
dialer_keyboard_handler(keystroke, flags);
break;
case Q_STATE_SCREENSAVER:
screensaver_keyboard_handler(keystroke, flags);
break;
case Q_STATE_FUNCTION_KEY_EDITOR:
function_key_editor_keyboard_handler(keystroke, flags);
break;
#ifndef Q_NO_SERIAL
case Q_STATE_MODEM_CONFIG:
modem_config_keyboard_handler(keystroke, flags);
break;
#endif
case Q_STATE_CODEPAGE:
codepage_keyboard_handler(keystroke, flags);
break;
case Q_STATE_INITIALIZATION:
case Q_STATE_EXIT:
/*
* Program BUG
*/
abort();
}
}
/**
* Dispatch to the appropriate draw function for the current program state.
*/
void refresh_handler() {
#ifdef Q_PDCURSES_WIN32
static long last_time = 1000000;
#else
static suseconds_t last_time = 1000000;
#endif
struct timeval tv;
switch (q_program_state) {
case Q_STATE_CONSOLE:
/*
* Only update the console 8 times a second
*/
gettimeofday(&tv, NULL);
if ((tv.tv_usec < last_time) || (tv.tv_usec - last_time > 125000)) {
console_refresh(Q_TRUE);
last_time = tv.tv_usec;
}
break;
case Q_STATE_SCRIPT_EXECUTE:
/*
* Only update the console 8 times a second
*/
gettimeofday(&tv, NULL);
if ((tv.tv_usec < last_time) || (tv.tv_usec - last_time > 125000)) {
script_refresh();
last_time = tv.tv_usec;
}
break;
case Q_STATE_HOST:
/*
* Only update the console 8 times a second
*/
gettimeofday(&tv, NULL);
if ((tv.tv_usec < last_time) || (tv.tv_usec - last_time > 125000)) {
host_refresh();
last_time = tv.tv_usec;
}
break;
case Q_STATE_CONSOLE_MENU:
console_menu_refresh();
break;
case Q_STATE_INFO:
console_info_refresh();
break;
case Q_STATE_SCROLLBACK:
scrollback_refresh();
break;
case Q_STATE_DOWNLOAD_MENU:
case Q_STATE_UPLOAD_MENU:
protocol_menu_refresh();
break;
case Q_STATE_DOWNLOAD_PATHDIALOG:
case Q_STATE_UPLOAD_PATHDIALOG:
case Q_STATE_UPLOAD_BATCH_DIALOG:
protocol_pathdialog_refresh();
break;
case Q_STATE_UPLOAD:
case Q_STATE_UPLOAD_BATCH:
case Q_STATE_DOWNLOAD:
protocol_transfer_refresh();
break;
case Q_STATE_EMULATION_MENU:
emulation_menu_refresh();
break;
case Q_STATE_TRANSLATE_MENU:
translate_table_menu_refresh();
break;
case Q_STATE_TRANSLATE_EDITOR_8BIT:
translate_table_editor_8bit_refresh();
break;
case Q_STATE_TRANSLATE_EDITOR_UNICODE:
translate_table_editor_unicode_refresh();
break;
case Q_STATE_PHONEBOOK:
case Q_STATE_DIALER:
phonebook_refresh();
break;
case Q_STATE_SCREENSAVER:
screensaver_refresh();
break;
case Q_STATE_FUNCTION_KEY_EDITOR:
function_key_editor_refresh();
break;
#ifndef Q_NO_SERIAL
case Q_STATE_MODEM_CONFIG:
modem_config_refresh();
break;
#endif
case Q_STATE_CODEPAGE:
codepage_refresh();
break;
case Q_STATE_INITIALIZATION:
case Q_STATE_EXIT:
/*
* Program BUG
*/
abort();
}
}
/**
* Switch to a new state, handling things like visible cursor, blocking
* keyboard, etc.
*
* @param new_state state to switch to
*/
void switch_state(const Q_PROGRAM_STATE new_state) {
if ((q_program_state == Q_STATE_CONSOLE) &&
(has_true_doublewidth() == Q_TRUE)) {
screen_clear();
}
switch (new_state) {
case Q_STATE_DOWNLOAD_MENU:
case Q_STATE_UPLOAD_MENU:
case Q_STATE_DOWNLOAD_PATHDIALOG:
case Q_STATE_UPLOAD_PATHDIALOG:
case Q_STATE_EMULATION_MENU:
case Q_STATE_TRANSLATE_MENU:
case Q_STATE_INITIALIZATION:
case Q_STATE_UPLOAD_BATCH_DIALOG:
case Q_STATE_CODEPAGE:
set_blocking_input(Q_TRUE);
q_keyboard_blocks = Q_TRUE;
q_screen_dirty = Q_TRUE;
q_cursor_on();
break;
case Q_STATE_SCROLLBACK:
if (q_scrollback_search_string != NULL) {
Xfree(q_scrollback_search_string, __FILE__, __LINE__);
q_scrollback_search_string = NULL;
}
q_scrollback_highlight_search_string = Q_FALSE;
/*
* Fall through...
*/
case Q_STATE_DIALER:
case Q_STATE_UPLOAD:
case Q_STATE_UPLOAD_BATCH:
case Q_STATE_DOWNLOAD:
case Q_STATE_CONSOLE_MENU:
case Q_STATE_INFO:
case Q_STATE_SCRIPT_EXECUTE:
set_blocking_input(Q_FALSE);
q_keyboard_blocks = Q_FALSE;
q_screen_dirty = Q_TRUE;
q_cursor_off();
original_state = q_program_state;
break;
case Q_STATE_HOST:
set_blocking_input(Q_FALSE);
q_keyboard_blocks = Q_FALSE;
q_screen_dirty = Q_TRUE;
q_cursor_on();
break;
case Q_STATE_CONSOLE:
set_blocking_input(Q_FALSE);
q_keyboard_blocks = Q_FALSE;
screen_clear();
q_screen_dirty = Q_TRUE;
if (q_status.split_screen == Q_TRUE) {
q_split_screen_dirty = Q_TRUE;
}
if (((q_status.emulation == Q_EMUL_LINUX)
|| (q_status.emulation == Q_EMUL_LINUX_UTF8)
|| (q_status.emulation == Q_EMUL_VT220))
&& (q_status.visible_cursor == Q_FALSE)) {
q_cursor_off();
} else {
q_cursor_on();
}
break;
case Q_STATE_FUNCTION_KEY_EDITOR:
/*
* Fall through ...
*/
original_state = q_program_state;
#ifndef Q_NO_SERIAL
case Q_STATE_MODEM_CONFIG:
#endif
case Q_STATE_PHONEBOOK:
case Q_STATE_TRANSLATE_EDITOR_8BIT:
case Q_STATE_TRANSLATE_EDITOR_UNICODE:
set_blocking_input(Q_TRUE);
q_keyboard_blocks = Q_TRUE;
q_screen_dirty = Q_TRUE;
q_cursor_off();
break;
case Q_STATE_EXIT:
q_cursor_on();
break;
case Q_STATE_SCREENSAVER:
if (q_program_state == Q_STATE_SCREENSAVER) {
break;
}
original_state = q_program_state;
set_blocking_input(Q_TRUE);
q_keyboard_blocks = Q_TRUE;
q_cursor_off();
q_screen_dirty = Q_TRUE;
break;
}
q_program_state = new_state;
}
/* Screensaver feature ------------------------------------------------------ */
/**
* Maximum password length is 64 chars.
*/
static char password_buffer[64];
static int password_buffer_n = 0;
/**
* State we were in before the screensaver was activated.
*/
Q_PROGRAM_STATE original_state;
/**
* Keyboard handler for the screensaver.
*
* @param keystroke the keystroke from the user.
* @param flags KEY_FLAG_ALT, KEY_FLAG_CTRL, etc. See input.h.
*/
void screensaver_keyboard_handler(const int keystroke, const int flags) {
if (keystroke == Q_KEY_ENTER) {
if (password_buffer_n > 0) {
if (strcmp(password_buffer,
get_option(Q_OPTION_SCREENSAVER_PASSWORD)) == 0) {
/*
* UNLOCK
*/
switch_state(original_state);
qlog(_("SCREENSAVER ending, returning to original state %u...\n"),
original_state);
} else {
qlog(_("SCREENSAVER invalid password entered.\n"));
}
}
memset(password_buffer, 0, sizeof(password_buffer));
password_buffer_n = 0;
} else {
if (q_key_code_yes(keystroke) == 0) {
password_buffer[password_buffer_n] = keystroke & 0xFF;
password_buffer_n++;
if (password_buffer_n == sizeof(password_buffer)) {
memset(password_buffer, 0, sizeof(password_buffer));
password_buffer_n = 0;
}
}
}
}
/**
* Draw screen for the screensaver.
*/
void screensaver_refresh() {
screen_clear();
screen_put_color_str_yx(HEIGHT - 1, 0, _("Enter password to unlock: "),
Q_COLOR_CONSOLE);
screen_flush();
}