-
Notifications
You must be signed in to change notification settings - Fork 0
/
pumiceConsoleMenu.c
304 lines (266 loc) · 7.95 KB
/
pumiceConsoleMenu.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
/*
* Copyright (c) 2017 Thor Mortensen
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <stdbool.h>
#include "pumiceConsoleMenu.h"
bool run = true;
enum evalInputModes {
NAV,
GET_ENTER,
INPUT_MODE_COUNT
};
menuItem_t* currentMenu;
menuItem_t* backStack;
const char* switchInteraction[] = {
"|U |N*|D |",
"|U*|N |D |",
"|U |N |D*|"
};
const char* buttonInteraction[] = {
"|R*|A |",
"|R |A*|",
};
void printUsage();
void clearScreen() {
printf("\033[2J\r");
printf("\033[0;0H");
}
void exitPoint(int signo) {
system("/bin/stty cooked");
printf("uit\n");
run = false;
}
void printHelp() {
clearScreen();
printUsage();
}
bool navInput(int input, menuItem_t** menu) {
switch (input) {
case KEY_UP:
(*(menu))->i--;
if ((*(menu))->i < 0) {
(*(menu))->i = 0;
}
return true;
case KEY_DOWN:
(*(menu))->i++;
if ((*(menu))->i > (*(menu))->itemCount - 1) {
(*(menu))->i = (*(menu))->itemCount - 1;
}
return true;
case KEY_SELECT:
if ((*(menu))->child[(*(menu))->i] != NULL) {
*menu = (*(menu))->child[(*(menu))->i];
}
return true;
case KEY_BACK:
if ((*(menu))->parent != NULL) {
*menu = (*(menu))->parent;
}
return true;
case KEY_HELP:
printHelp();
return true;
case KEY_QUIT:
exitPoint(0);
return true;
}
return false;
}
void editInput(int input, menuItem_t** menu) {
int inputnum = 0;
int inCount = 0;
switch ((*(menu))->interactionType) {
case SWITCH:
if (input == KEY_EDIT_DOWN) {
(*(menu))->itemVal[(*(menu))->i] = POS_DPOWN;
} else if (input == KEY_EDIT_UP) {
(*(menu))->itemVal[(*(menu))->i] = POS_UP;
} else if (input == KEY_EDIT_NEU) {
(*(menu))->itemVal[(*(menu))->i] = NEUTRAL;
}
break;
case BUTTON:
if (input == KEY_EDDIT) {
(*(menu))->itemVal[(*(menu))->i] ^= (1 << 0);
} else if (input == ' ') {
(*(menu))->itemVal[(*(menu))->i] ^= (1 << 0);
}
break;
case NUM:
if (input == KEY_EDDIT) {
system("/bin/stty cooked");
printf("nter number: ");
inCount = scanf("%d", &inputnum);
if (inCount >= 0) {
(*(menu))->itemVal[(*(menu))->i] = inputnum;
}
system("/bin/stty raw");
}
break;
}
if ((*(menu))->interAction != NULL) {
(*(menu))->interAction((*(menu))->i, input, (*(menu))->itemVal[(*(menu))->i]);
}
}
void evalInput(uint8_t mode, menuItem_t** menu) {
int input = 0;
int inCount = 0;
bool validIn = false;
switch (mode) {
case NAV:
inCount = scanf("%c", (char*) &input);
if (inCount > 0) {
if (!navInput(input, menu)) {
editInput(input, menu);
}
}
break;
case GET_ENTER:
inCount = scanf("%c", (char*) &input);
if (inCount > 0) {
if (input == KEY_QUIT) {
exitPoint(0);
} else {
validIn = true;
}
}
break;
}
}
void pauseMenuScreen() {
system("/bin/stty cooked");
clearScreen();
}
void resumeMenuScreen() {
system("/bin/stty raw");
}
void exitMenu() {
clearScreen();
exitPoint(0);
}
void printUsage() {
system("/bin/stty cooked");
printf("\n"
" Up \n"
" \n"
" '%c' \n"
" ^ \n"
" | \n"
" +----------+ \n"
"Back '%c'<-- |NAVIGATION| -->'%c' Select \n"
" +----------+ \n"
" | \n"
" v \n"
" '%c' \n"
" \n"
" Down \n"
"\n"
" +--------------------------+ \n"
" | INTERACTION | \n"
" +--------------------------+ \n"
" | This Overview ~> '%c' | \n"
" | Eddit ~> '%c' | \n"
" | Switch Up ~> '%c' | \n"
" | Switch Neutral ~> '%c' | \n"
" | Switch Down ~> '%c' | \n"
" | Do Stuff ~> ENTER | \n"
" | Quit ~> '%c' | \n"
" +--------------------------+ \n"
"\n Press any key to continue... \n"
, KEY_UP, KEY_BACK, KEY_SELECT, KEY_DOWN,
KEY_HELP, KEY_EDDIT, KEY_EDIT_UP, KEY_EDIT_NEU, KEY_EDIT_DOWN, KEY_QUIT);
system("/bin/stty raw");
evalInput(GET_ENTER, NULL);
}
void updateMenuScreen() {
int i;
int noIn = 0;
clearScreen();
printf("You are here: Main Menu");
backStack = currentMenu->parent;
if (backStack != NULL) {
while (backStack->parent != NULL) {
backStack = backStack->parent;
}
while (backStack != currentMenu) {
printf(" / %s", backStack->menuStr[backStack->i]);
backStack = backStack->child[backStack->i];
}
}
printf("*");
printf("\n\n\r");
switch (currentMenu->interactionType) {
case SWITCH:
printf(" Use Switch keys [%c,%c,%c] to change { Up | Neutral | Down }. Press Enter to set\n\n\r", KEY_EDIT_UP, KEY_EDIT_NEU, KEY_EDIT_DOWN);
break;
case BUTTON:
printf(" Press SPACE to activate button or \n\r");
printf(" Edit [%c] to toggle { Release | Active }. Press Enter to set\n\n\r", KEY_EDDIT);
break;
case NUM:
printf(" Edit [%c] to change { value }. Press Enter to set\n\n\r", KEY_EDDIT);
break;
default:
printf("\n\n\r");
break;
}
for (i = 0; i < currentMenu->itemCount; i++) {
if (currentMenu->i == i) {
printf("-->[%s] ", currentMenu->menuStr[i]);
} else {
printf(" [%s] ", currentMenu->menuStr[i]);
}
switch (currentMenu->interactionType) {
case SWITCH:
printf("\t%s\n\r", switchInteraction[currentMenu->itemVal[i]]);
break;
case NUM:
printf("\t%d\n\r", currentMenu->itemVal[i]);
break;
case BUTTON:
printf("\t%s\n\r", buttonInteraction[currentMenu->itemVal[i]]);
break;
default:
printf("\n\r");
break;
}
}
printf("\n\r");
fflush(stdout);
}
void runMenu(menuItem_t* mainMenu, char* wellcomeMsg) {
if (wellcomeMsg != NULL) {
printf("%s\n", wellcomeMsg);
}
if (signal(SIGINT, exitPoint) == SIG_ERR) {
printf("\ncan't catch SIGINT\n");
}
currentMenu = mainMenu;
backStack = NULL;
printUsage();
while (run) {
updateMenuScreen();
evalInput(NAV, ¤tMenu);
printf("\n\r");
}
run = true;
system("/bin/stty cooked");
printf("\n");
}