/src/barebox/common/menu.c
Line | Count | Source |
1 | | // SPDX-License-Identifier: GPL-2.0-only |
2 | | /* |
3 | | * (C) Copyright 2009-2010 Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com> |
4 | | */ |
5 | | |
6 | | #include <common.h> |
7 | | #include <command.h> |
8 | | #include <environment.h> |
9 | | #include <init.h> |
10 | | #include <menu.h> |
11 | | #include <malloc.h> |
12 | | #include <slice.h> |
13 | | #include <xfuncs.h> |
14 | | #include <errno.h> |
15 | | #include <readkey.h> |
16 | | #include <clock.h> |
17 | | #include <linux/err.h> |
18 | | #include <libbb.h> |
19 | | |
20 | | static LIST_HEAD(menus); |
21 | | |
22 | | struct menu* menu_get_menus(void) |
23 | 0 | { |
24 | 0 | if (list_empty(&menus)) |
25 | 0 | return NULL; |
26 | | |
27 | 0 | return list_entry(&menus, struct menu, list); |
28 | 0 | } |
29 | | EXPORT_SYMBOL(menu_get_menus); |
30 | | |
31 | | void menu_free(struct menu *m) |
32 | 0 | { |
33 | 0 | struct menu_entry *me, *tmp; |
34 | 0 | int i; |
35 | |
|
36 | 0 | if (!m) |
37 | 0 | return; |
38 | 0 | free_const(m->name); |
39 | 0 | for (i = 0; i < m->display_lines; i++) |
40 | 0 | free_const(m->display[i]); |
41 | 0 | free(m->display); |
42 | 0 | free_const(m->auto_display); |
43 | |
|
44 | 0 | list_for_each_entry_safe(me, tmp, &m->entries, list) |
45 | 0 | menu_entry_free(me); |
46 | |
|
47 | 0 | free(m); |
48 | 0 | } |
49 | | EXPORT_SYMBOL(menu_free); |
50 | | |
51 | | int menu_add(struct menu *m) |
52 | 0 | { |
53 | 0 | if (!m || !m->name) |
54 | 0 | return -EINVAL; |
55 | | |
56 | 0 | if (menu_get_by_name(m->name)) |
57 | 0 | return -EEXIST; |
58 | | |
59 | 0 | list_add_tail(&m->list, &menus); |
60 | |
|
61 | 0 | return 0; |
62 | 0 | } |
63 | | EXPORT_SYMBOL(menu_add); |
64 | | |
65 | | void menu_remove(struct menu *m) |
66 | 0 | { |
67 | 0 | if (!m) |
68 | 0 | return; |
69 | | |
70 | 0 | list_del(&m->list); |
71 | 0 | } |
72 | | EXPORT_SYMBOL(menu_remove); |
73 | | |
74 | | int menu_add_entry(struct menu *m, struct menu_entry *me) |
75 | 0 | { |
76 | 0 | if (!m || !me || !me->display) |
77 | 0 | return -EINVAL; |
78 | | |
79 | 0 | m->nb_entries++; |
80 | 0 | me->num = m->nb_entries; |
81 | 0 | list_add_tail(&me->list, &m->entries); |
82 | |
|
83 | 0 | return 0; |
84 | 0 | } |
85 | | EXPORT_SYMBOL(menu_add_entry); |
86 | | |
87 | | void menu_remove_entry(struct menu *m, struct menu_entry *me) |
88 | 0 | { |
89 | 0 | int i = 1; |
90 | |
|
91 | 0 | if (!m || !me) |
92 | 0 | return; |
93 | | |
94 | 0 | m->nb_entries--; |
95 | 0 | list_del(&me->list); |
96 | |
|
97 | 0 | list_for_each_entry(me, &m->entries, list) |
98 | 0 | me->num = i++; |
99 | 0 | } |
100 | | EXPORT_SYMBOL(menu_remove_entry); |
101 | | |
102 | | struct menu* menu_get_by_name(const char *name) |
103 | 0 | { |
104 | 0 | struct menu* m; |
105 | |
|
106 | 0 | if (!name) |
107 | 0 | return NULL; |
108 | | |
109 | 0 | list_for_each_entry(m, &menus, list) { |
110 | 0 | if(strcmp(m->name, name) == 0) |
111 | 0 | return m; |
112 | 0 | } |
113 | | |
114 | 0 | return NULL; |
115 | 0 | } |
116 | | EXPORT_SYMBOL(menu_get_by_name); |
117 | | |
118 | | struct menu_entry* menu_entry_get_by_num(struct menu* m, int num) |
119 | 0 | { |
120 | 0 | struct menu_entry* me; |
121 | |
|
122 | 0 | if (!m || num < 1 || num > m->nb_entries) |
123 | 0 | return NULL; |
124 | | |
125 | 0 | list_for_each_entry(me, &m->entries, list) { |
126 | 0 | if(me->num == num) |
127 | 0 | return me; |
128 | 0 | } |
129 | | |
130 | 0 | return NULL; |
131 | 0 | } |
132 | | EXPORT_SYMBOL(menu_entry_get_by_num); |
133 | | |
134 | | void menu_entry_free(struct menu_entry *me) |
135 | 0 | { |
136 | 0 | if (!me) |
137 | 0 | return; |
138 | | |
139 | 0 | me->free(me); |
140 | 0 | } |
141 | | EXPORT_SYMBOL(menu_entry_free); |
142 | | |
143 | | static void __print_entry(const char *str) |
144 | 0 | { |
145 | 0 | static char outstr[256]; |
146 | |
|
147 | 0 | if (IS_ENABLED(CONFIG_SHELL_HUSH)) { |
148 | 0 | process_escape_sequence(str, outstr, 256); |
149 | 0 | puts(outstr); |
150 | 0 | } else { |
151 | 0 | puts(str); |
152 | 0 | } |
153 | 0 | } |
154 | | |
155 | | static void print_menu_entry(struct menu *m, struct menu_entry *me, |
156 | | int selected) |
157 | 0 | { |
158 | 0 | gotoXY(3, me->num + m->display_lines); |
159 | |
|
160 | 0 | if (me->type == MENU_ENTRY_BOX) { |
161 | 0 | if (me->box_state) |
162 | 0 | puts("[*]"); |
163 | 0 | else |
164 | 0 | puts("[ ]"); |
165 | 0 | } else { |
166 | 0 | puts(" "); |
167 | 0 | } |
168 | |
|
169 | 0 | printf(" %2d: ", me->num); |
170 | 0 | if (selected) |
171 | 0 | puts("\e[7m"); |
172 | |
|
173 | 0 | __print_entry(me->display); |
174 | |
|
175 | 0 | if (selected) |
176 | 0 | puts("\e[0m"); |
177 | 0 | } |
178 | | |
179 | | int menu_set_selected_entry(struct menu *m, struct menu_entry* me) |
180 | 0 | { |
181 | 0 | struct menu_entry* tmp; |
182 | |
|
183 | 0 | if (!m || !me) |
184 | 0 | return -EINVAL; |
185 | | |
186 | 0 | list_for_each_entry(tmp, &m->entries, list) { |
187 | 0 | if(me == tmp) { |
188 | 0 | m->selected = me; |
189 | 0 | return 0; |
190 | 0 | } |
191 | 0 | } |
192 | | |
193 | 0 | return -EINVAL; |
194 | 0 | } |
195 | | EXPORT_SYMBOL(menu_set_selected_entry); |
196 | | |
197 | | int menu_set_selected(struct menu *m, int num) |
198 | 0 | { |
199 | 0 | struct menu_entry *me; |
200 | |
|
201 | 0 | me = menu_entry_get_by_num(m, num); |
202 | |
|
203 | 0 | if (!me) |
204 | 0 | return -EINVAL; |
205 | | |
206 | 0 | m->selected = me; |
207 | |
|
208 | 0 | return 0; |
209 | 0 | } |
210 | | EXPORT_SYMBOL(menu_set_selected); |
211 | | |
212 | | int menu_set_auto_select(struct menu *m, int delay) |
213 | 0 | { |
214 | 0 | if (!m) |
215 | 0 | return -EINVAL; |
216 | | |
217 | 0 | m->auto_select = delay; |
218 | |
|
219 | 0 | return 0; |
220 | 0 | } |
221 | | EXPORT_SYMBOL(menu_set_auto_select); |
222 | | |
223 | | static void print_menu(struct menu *m) |
224 | 0 | { |
225 | 0 | struct menu_entry *me; |
226 | 0 | int i; |
227 | |
|
228 | 0 | clear(); |
229 | 0 | for (i = 0; i < m->display_lines; i++) { |
230 | 0 | gotoXY(2, 1 + i); |
231 | 0 | __print_entry(m->display[i]); |
232 | 0 | } |
233 | |
|
234 | 0 | list_for_each_entry(me, &m->entries, list) { |
235 | 0 | if(m->selected != me) |
236 | 0 | print_menu_entry(m, me, 0); |
237 | 0 | } |
238 | |
|
239 | 0 | if (!m->selected) { |
240 | 0 | m->selected = list_first_entry(&m->entries, |
241 | 0 | struct menu_entry, list); |
242 | 0 | } |
243 | |
|
244 | 0 | print_menu_entry(m, m->selected, 1); |
245 | 0 | } |
246 | | |
247 | | int menu_show(struct menu *m) |
248 | 0 | { |
249 | 0 | int ch, ch_previous = 0; |
250 | 0 | int countdown; |
251 | 0 | int auto_display_len = 16; |
252 | 0 | uint64_t start, second; |
253 | |
|
254 | 0 | if(!m || list_empty(&m->entries)) |
255 | 0 | return -EINVAL; |
256 | | |
257 | 0 | print_menu(m); |
258 | |
|
259 | 0 | countdown = m->auto_select; |
260 | 0 | if (m->auto_select >= 0) { |
261 | 0 | gotoXY(3, m->nb_entries + m->display_lines + 1); |
262 | 0 | if (!m->auto_display) { |
263 | 0 | printf("Auto Select in"); |
264 | 0 | } else { |
265 | 0 | auto_display_len = strlen(m->auto_display); |
266 | 0 | printf("%s", m->auto_display); |
267 | 0 | } |
268 | 0 | printf(" %2d", countdown--); |
269 | 0 | } |
270 | |
|
271 | 0 | start = get_time_ns(); |
272 | 0 | second = start; |
273 | 0 | while (m->auto_select > 0 && !is_timeout(start, m->auto_select * SECOND)) { |
274 | 0 | if (tstc()) { |
275 | 0 | m->auto_select = -1; |
276 | 0 | break; |
277 | 0 | } |
278 | | |
279 | 0 | if (is_timeout(second, SECOND)) { |
280 | 0 | printf("\b\b%2d", countdown--); |
281 | 0 | second += SECOND; |
282 | 0 | } |
283 | 0 | } |
284 | |
|
285 | 0 | gotoXY(3, m->nb_entries + m->display_lines + 1); |
286 | 0 | printf("%*c", auto_display_len + 4, ' '); |
287 | |
|
288 | 0 | gotoXY(3, m->selected->num + m->display_lines); |
289 | |
|
290 | 0 | do { |
291 | 0 | struct menu_entry *old_selected = m->selected; |
292 | 0 | int repaint = 0; |
293 | |
|
294 | 0 | if (m->auto_select >= 0) { |
295 | 0 | ch = BB_KEY_RETURN; |
296 | 0 | } else { |
297 | | /* Ensure workqueues can run during menu, e.g. for fastboot */ |
298 | 0 | command_slice_release(); |
299 | 0 | ch = read_key(); |
300 | 0 | command_slice_acquire(); |
301 | 0 | } |
302 | |
|
303 | 0 | m->auto_select = -1; |
304 | |
|
305 | 0 | switch (ch) { |
306 | 0 | case '0' ... '9': { |
307 | 0 | struct menu_entry *me; |
308 | 0 | int num = ch - '0'; |
309 | 0 | int next_num = m->selected->num + 10; |
310 | 0 | if (!num) |
311 | 0 | num = 10; |
312 | |
|
313 | 0 | if (ch_previous == ch && next_num <= m->nb_entries) |
314 | 0 | num = next_num; |
315 | |
|
316 | 0 | me = menu_entry_get_by_num(m, num); |
317 | 0 | if (me) { |
318 | 0 | m->selected = me; |
319 | 0 | repaint = 1; |
320 | 0 | } |
321 | 0 | break; |
322 | 0 | } |
323 | 0 | case 'k': |
324 | 0 | case BB_KEY_UP: |
325 | 0 | m->selected = list_entry(m->selected->list.prev, struct menu_entry, |
326 | 0 | list); |
327 | 0 | if (&(m->selected->list) == &(m->entries)) { |
328 | 0 | m->selected = list_entry(m->selected->list.prev, struct menu_entry, |
329 | 0 | list); |
330 | 0 | } |
331 | 0 | repaint = 1; |
332 | 0 | break; |
333 | 0 | case 'j': |
334 | 0 | case BB_KEY_DOWN: |
335 | 0 | m->selected = list_entry(m->selected->list.next, struct menu_entry, |
336 | 0 | list); |
337 | 0 | if (&(m->selected->list) == &(m->entries)) { |
338 | 0 | m->selected = list_entry(m->selected->list.next, struct menu_entry, |
339 | 0 | list); |
340 | 0 | } |
341 | 0 | repaint = 1; |
342 | 0 | break; |
343 | 0 | case ' ': |
344 | 0 | if (m->selected->type == MENU_ENTRY_BOX) { |
345 | 0 | m->selected->box_state = !m->selected->box_state; |
346 | 0 | if (m->selected->action) |
347 | 0 | m->selected->action(m, m->selected); |
348 | 0 | repaint = 1; |
349 | 0 | break; |
350 | 0 | } |
351 | | /* no break */ |
352 | 0 | case BB_KEY_ENTER: |
353 | 0 | if (ch_previous == BB_KEY_RETURN) |
354 | 0 | break; |
355 | 0 | case BB_KEY_RETURN: |
356 | 0 | if (ch_previous == BB_KEY_ENTER) |
357 | 0 | break; |
358 | 0 | clear(); |
359 | 0 | gotoXY(1,1); |
360 | 0 | if (m->selected->action) |
361 | 0 | m->selected->action(m, m->selected); |
362 | 0 | if (m->selected->non_re_ent) |
363 | 0 | return m->selected->num; |
364 | 0 | else |
365 | 0 | print_menu(m); |
366 | 0 | break; |
367 | 0 | case BB_KEY_HOME: |
368 | 0 | m->selected = list_first_entry(&m->entries, struct menu_entry, list); |
369 | 0 | repaint = 1; |
370 | 0 | break; |
371 | 0 | case BB_KEY_END: |
372 | 0 | m->selected = list_last_entry(&m->entries, struct menu_entry, list); |
373 | 0 | repaint = 1; |
374 | 0 | break; |
375 | 0 | default: |
376 | 0 | break; |
377 | 0 | } |
378 | | |
379 | 0 | if (repaint) { |
380 | 0 | print_menu_entry(m, old_selected, 0); |
381 | 0 | print_menu_entry(m, m->selected, 1); |
382 | 0 | } |
383 | |
|
384 | 0 | ch_previous = ch; |
385 | 0 | } while(1); |
386 | | |
387 | 0 | return 0; |
388 | 0 | } |
389 | | EXPORT_SYMBOL(menu_show); |
390 | | |
391 | 0 | void menu_action_exit(struct menu *m, struct menu_entry *me) {} |
392 | | EXPORT_SYMBOL(menu_action_exit); |
393 | | |
394 | | struct submenu { |
395 | | const char *submenu; |
396 | | struct menu_entry entry; |
397 | | }; |
398 | | |
399 | | static void menu_action_show(struct menu *m, struct menu_entry *me) |
400 | 0 | { |
401 | 0 | struct submenu *s = container_of(me, struct submenu, entry); |
402 | 0 | struct menu *sm; |
403 | |
|
404 | 0 | if (me->type == MENU_ENTRY_BOX && !me->box_state) |
405 | 0 | return; |
406 | | |
407 | 0 | sm = menu_get_by_name(s->submenu); |
408 | 0 | if (sm) |
409 | 0 | menu_show(sm); |
410 | 0 | else |
411 | 0 | eprintf("no such menu: %s\n", s->submenu); |
412 | 0 | } |
413 | | |
414 | | static void submenu_free(struct menu_entry *me) |
415 | 0 | { |
416 | 0 | struct submenu *s = container_of(me, struct submenu, entry); |
417 | |
|
418 | 0 | free_const(s->entry.display); |
419 | 0 | free_const(s->submenu); |
420 | 0 | free(s); |
421 | 0 | } |
422 | | |
423 | | struct menu_entry *menu_add_submenu(struct menu *parent, |
424 | | const char *submenu, |
425 | | const char *display) |
426 | 0 | { |
427 | 0 | struct submenu *s = calloc(1, sizeof(*s)); |
428 | 0 | int ret; |
429 | |
|
430 | 0 | if (!s) |
431 | 0 | return ERR_PTR(-ENOMEM); |
432 | | |
433 | 0 | s->submenu = strdup_const(submenu); |
434 | 0 | s->entry.action = menu_action_show; |
435 | 0 | s->entry.free = submenu_free; |
436 | 0 | s->entry.display = strdup_const(display); |
437 | 0 | if (!s->entry.display || !s->submenu) { |
438 | 0 | ret = -ENOMEM; |
439 | 0 | goto err_free; |
440 | 0 | } |
441 | | |
442 | 0 | ret = menu_add_entry(parent, &s->entry); |
443 | 0 | if (ret) |
444 | 0 | goto err_free; |
445 | | |
446 | 0 | return &s->entry; |
447 | | |
448 | 0 | err_free: |
449 | 0 | submenu_free(&s->entry); |
450 | 0 | return ERR_PTR(ret); |
451 | 0 | } |
452 | | EXPORT_SYMBOL(menu_add_submenu); |
453 | | |
454 | | struct action_entry { |
455 | | const char *command; |
456 | | struct menu_entry entry; |
457 | | }; |
458 | | |
459 | | static void menu_action_command(struct menu *m, struct menu_entry *me) |
460 | 0 | { |
461 | 0 | struct action_entry *e = container_of(me, struct action_entry, entry); |
462 | 0 | int ret; |
463 | 0 | const char *s = getenv(e->command); |
464 | | |
465 | | /* can be a command as boot */ |
466 | 0 | if (!s) |
467 | 0 | s = e->command; |
468 | |
|
469 | 0 | ret = run_command("%s", s); |
470 | |
|
471 | 0 | if (ret < 0) |
472 | 0 | udelay(1000000); |
473 | 0 | } |
474 | | |
475 | | static void menu_command_free(struct menu_entry *me) |
476 | 0 | { |
477 | 0 | struct action_entry *e = container_of(me, struct action_entry, entry); |
478 | |
|
479 | 0 | free_const(e->entry.display); |
480 | 0 | free_const(e->command); |
481 | |
|
482 | 0 | free(e); |
483 | 0 | } |
484 | | |
485 | | struct menu_entry *menu_add_command_entry(struct menu *m, const char *display, |
486 | | const char *command, menu_entry_type type) |
487 | 0 | { |
488 | 0 | struct action_entry *e = calloc(1, sizeof(*e)); |
489 | 0 | int ret; |
490 | |
|
491 | 0 | if (!e) |
492 | 0 | return ERR_PTR(-ENOMEM); |
493 | | |
494 | 0 | e->command = strdup_const(command); |
495 | 0 | e->entry.action = menu_action_command; |
496 | 0 | e->entry.free = menu_command_free; |
497 | 0 | e->entry.type = type; |
498 | 0 | e->entry.display = strdup_const(display); |
499 | |
|
500 | 0 | if (!e->entry.display || !e->command) { |
501 | 0 | ret = -ENOMEM; |
502 | 0 | goto err_free; |
503 | 0 | } |
504 | | |
505 | 0 | ret = menu_add_entry(m, &e->entry); |
506 | 0 | if (ret) |
507 | 0 | goto err_free; |
508 | | |
509 | 0 | return &e->entry; |
510 | 0 | err_free: |
511 | 0 | menu_command_free(&e->entry); |
512 | 0 | return ERR_PTR(ret); |
513 | 0 | } |
514 | | EXPORT_SYMBOL(menu_add_command_entry); |
515 | | |
516 | | /* |
517 | | * Add title to menu. |
518 | | * Lines are separated by explicit char '\n' or by string "\n". |
519 | | * |
520 | | * @display: NULL or pointer to the string which will be freed in this function. |
521 | | * If NULL or zero length string is provided, default title will be added. |
522 | | */ |
523 | | void menu_add_title(struct menu *m, const char *display) |
524 | 0 | { |
525 | 0 | char *tmp, *src, *dst; |
526 | 0 | int lines = 1; |
527 | 0 | int i; |
528 | |
|
529 | 0 | if (!display || !strlen(display)) |
530 | 0 | src = dst = tmp = xasprintf("Menu : %s", m->name ? m->name : ""); |
531 | 0 | else |
532 | 0 | src = dst = tmp = xstrdup(display); |
533 | | |
534 | | /* Count lines and separate single string into multiple strings */ |
535 | 0 | while (*src) { |
536 | 0 | if (*src == '\\') { |
537 | 0 | if (*(src + 1) == '\\') { |
538 | 0 | *dst++ = *src++; |
539 | 0 | *dst++ = *src++; |
540 | 0 | continue; |
541 | 0 | } |
542 | 0 | if (*(src + 1) == 'n') { |
543 | 0 | *dst = 0; |
544 | 0 | src += 2; |
545 | 0 | dst++; |
546 | 0 | lines++; |
547 | 0 | continue; |
548 | 0 | } |
549 | 0 | } |
550 | 0 | if (*src == '\n') { |
551 | 0 | *dst = 0; |
552 | 0 | src++; |
553 | 0 | dst++; |
554 | 0 | lines++; |
555 | 0 | continue; |
556 | 0 | } |
557 | 0 | *dst++ = *src++; |
558 | 0 | } |
559 | 0 | *dst = 0; |
560 | |
|
561 | 0 | m->display = xzalloc(sizeof(*m->display) * lines); |
562 | 0 | m->display_lines = lines; |
563 | |
|
564 | 0 | for (src = tmp, i = 0; i < lines; i++) { |
565 | 0 | m->display[i] = xstrdup_const(src); |
566 | | /* Go to the next line */ |
567 | 0 | src += strlen(src) + 1; |
568 | 0 | } |
569 | |
|
570 | 0 | free(tmp); |
571 | 0 | } |
572 | | EXPORT_SYMBOL(menu_add_title); |