-
Notifications
You must be signed in to change notification settings - Fork 0
/
menu.c
389 lines (357 loc) · 8.57 KB
/
menu.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
/* *******************************************
* frabenu - Framebuffer menu
* Copyright (C) 2018 Frank Mueller
*
* SPDX-License-Identifier: GPL-2.0-or-later
* *******************************************/
#include "menu.h"
#include "fbida/fbi.h"
#include <string.h>
#include <stdlib.h>
#include "debug.h"
/**menu
* @brief buildFileName
* @param fileName
* @param[in,out] xPos
* @param[in,out] yPos
* @return
*/
char *buildFileName(const char *fileName, uint8_t *xPos, uint8_t *yPos)
{
int i;
int first, second;
int x, y;
int cnt, needed;
int len;
char *str;
if (fileName == NULL) { return NULL; }
y = x = -1;
needed = 0;
if (*xPos > 0) { ++needed; }
if (*yPos > 0) { ++needed; }
if (needed == 0) { return NULL; }
for (i = 0, cnt = needed; (fileName[i] != 0) && (cnt > 0); ++i)
{
if (fileName[i] == '%')
{
if ((*xPos > 0) && (x < 0) && (fileName[i+1] == 'x')) // need x and x not yet found and x now found
{
x = i;
++i;
--cnt;
}
else if ((*yPos > 0) && (y < 0) && (fileName[i+1] == 'y')) // need y and y not yet found and y now found
{
y = i;
++i;
--cnt;
}
}
}
if (cnt > 0) { return NULL; } // not all found
len = strlen(fileName) - needed + 1;
str = malloc(len);
if (str == NULL) { return NULL; }
if (needed == 2)
{
if (x < y)
{
first = x;
second = y;
*xPos = x;
*yPos = y-1;
}
else
{
first = y;
second = x;
*xPos = x-1;
*yPos = y;
}
}
else if (*xPos > 0)
{
first = x;
second = -1;
*xPos = x;
}
else
{
first = y;
second = -1;
*yPos = y;
}
strncpy(str, fileName, first+1);
if (second > 0)
{
strncpy(str+first+1, fileName+first+2, second-first-1);
strncpy(str+second, fileName+second+2, len-second);
}
else
{
strncpy(str+first+1, fileName+first+2, len-first-1);
}
return str;
}
menu *menu_creat(uint8_t xMax, uint8_t yMax, const char *fileName)
{
menu *m;
uint8_t xPos;
uint8_t yPos;
uint8_t x, y;
char *str;
struct ida_image *img;
debugOut(debug_level3, "menu_creat(%d, %d, %s)\n", xMax, yMax, fileName);
if ((xMax < 1) || (xMax > 9) || (yMax < 1) || (yMax > 9)) { return NULL; }
m = calloc(1, sizeof(menu));
if (m == NULL) { return NULL; }
m->curX = m->curY = 0;
m->xMax = xMax;
m->yMax = yMax;
m->imgArr = calloc(xMax * yMax, sizeof(m->imgArr));
xPos = (xMax == 1) ? 0 : 1;
yPos = (yMax == 1) ? 0 : 1;
str = buildFileName(fileName, &xPos, &yPos);
if (str == NULL) { return menu_destroy(m); }
for (y = 0; y < yMax; ++y)
{
for (x = 0; x < xMax; ++x)
{
if (xPos > 0) { str[xPos] = '0' + x + 1; }
if (yPos > 0) { str[yPos] = '0' + y + 1; }
debugOut(debug_level3, "try read %s\n", str);
img = read_image(str);
if (img == NULL)
{
free(str);
return menu_destroy(m);
}
m->imgArr[y*xMax + x] = img;
}
}
free(str);
return m;
}
menu *menu_destroy(menu * m)
{
if (m != NULL)
{
if (m->imgArr != NULL)
{
uint8_t x, y;
for (y = 0; y < m->yMax; ++y)
{
for (x = 0; x < m->xMax; ++x)
{
if (m->imgArr[y*m->xMax + x] != NULL)
{
free(m->imgArr[y*m->xMax + x]);
}
}
}
free(m->imgArr);
}
free(m);
}
return NULL;
}
int menu_get(menu *m)
{
if (m == NULL) { return -1; }
return m->curY * m->xMax + m->curX;
}
void menu_set(menu *m , int select)
{
if ((m == NULL) || (select <= 0)) { return; }
--select;
int y = select / m->xMax;
if (y < m->yMax)
{
m->curX = select % m->xMax;
m->curY = y;
}
}
struct ida_image * menu_img(menu * m)
{
if (m == NULL) { return NULL; }
if (m->imgArr == NULL) { return NULL; }
return m->imgArr[menu_get(m)];
}
int menu_task(menu * m, menu_scroll_mode mode, input_event e)
{
int select = -1;
if (m == NULL) { return 0; }
switch (e)
{
case input_select1:
case input_select2:
case input_select3:
case input_select4:
case input_select5:
case input_select6:
case input_select7:
case input_select8:
case input_select9:
case input_select10:
if (e <= (m->xMax * m->yMax))
{
m->curX = (e-1) % m->xMax;
m->curY = (e-1) / m->xMax;
select = e;
}
break;
case input_left:
if (m->curX == 0)
{
switch (mode)
{
case menu_scroll_mode_1:
break;
case menu_scroll_mode_2:
m->curX = m->xMax - 1;
break;
case menu_scroll_mode_3:
if (m->curY > 0)
{
m->curX = m->xMax - 1;
--m->curY;
}
break;
case menu_scroll_mode_4:
m->curX = m->xMax - 1;
if (m->curY > 0)
{
--m->curY;
}
else
{
m->curY = m->yMax - 1;
}
break;
}
}
else
{
--m->curX;
}
break;
case input_right:
++m->curX;
if (m->curX >= m->xMax)
{
switch (mode)
{
case menu_scroll_mode_1:
m->curX = m->xMax - 1;
break;
case menu_scroll_mode_2:
m->curX = 0;
break;
case menu_scroll_mode_3:
if (m->curY+1 < m->yMax)
{
m->curX = 0;
++m->curY;
}
else
{
m->curX = m->xMax - 1;
}
break;
case menu_scroll_mode_4:
m->curX = 0;
if (m->curY+1 < m->yMax)
{
++m->curY;
}
else
{
m->curY = 0;
}
break;
}
}
break;
case input_up:
if (m->curY == 0)
{
switch (mode)
{
case menu_scroll_mode_1:
break;
case menu_scroll_mode_2:
m->curY = m->yMax - 1;
break;
case menu_scroll_mode_3:
if (m->curX > 0)
{
m->curY = m->yMax - 1;
--m->curX;
}
break;
case menu_scroll_mode_4:
m->curY = m->yMax - 1;
if (m->curX > 0)
{
--m->curX;
}
else
{
m->curX = m->xMax - 1;
}
break;
}
}
else
{
--m->curY;
}
break;
case input_down:
++m->curY;
if (m->curY >= m->yMax)
{
switch (mode)
{
case menu_scroll_mode_1:
m->curY = m->yMax - 1;
break;
case menu_scroll_mode_2:
m->curY = 0;
break;
case menu_scroll_mode_3:
if (m->curX+1 < m->xMax)
{
m->curY = 0;
++m->curX;
}
else
{
m->curY = m->yMax - 1;
}
break;
case menu_scroll_mode_4:
m->curY = 0;
if (m->curX+1 < m->xMax)
{
++m->curX;
}
else
{
m->curX = 0;
}
break;
}
}
break;
case input_select:
select = menu_get(m) + 1;
break;
case input_abort:
select = 0;
break;
default:
break;
}
return select;
}