evil_portal_scene_start.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #include "../evil_portal_app_i.h"
  2. #include "../helpers/evil_portal_speaker.h"
  3. // For each command, define whether additional arguments are needed
  4. // (enabling text input to fill them out), and whether the console
  5. // text box should focus at the start of the output or the end
  6. typedef enum { NO_ARGS = 0, INPUT_ARGS, TOGGLE_ARGS } InputArgs;
  7. typedef enum {
  8. FOCUS_CONSOLE_END = 0,
  9. FOCUS_CONSOLE_START,
  10. FOCUS_CONSOLE_TOGGLE
  11. } FocusConsole;
  12. #define SHOW_STOPSCAN_TIP (true)
  13. #define NO_TIP (false)
  14. #define MAX_OPTIONS (9)
  15. typedef struct {
  16. const char *item_string;
  17. const char *options_menu[MAX_OPTIONS];
  18. int num_options_menu;
  19. const char *actual_commands[MAX_OPTIONS];
  20. InputArgs needs_keyboard;
  21. FocusConsole focus_console;
  22. bool show_stopscan_tip;
  23. } Evil_PortalItem;
  24. // NUM_MENU_ITEMS defined in evil_portal_app_i.h - if you add an entry here,
  25. // increment it!
  26. const Evil_PortalItem items[NUM_MENU_ITEMS] = {
  27. // send command
  28. {"Start portal",
  29. {""},
  30. 1,
  31. {SET_HTML_CMD},
  32. NO_ARGS,
  33. FOCUS_CONSOLE_START,
  34. SHOW_STOPSCAN_TIP},
  35. // stop portal
  36. {"Stop portal", {""}, 1, {RESET_CMD}, NO_ARGS, FOCUS_CONSOLE_START, SHOW_STOPSCAN_TIP},
  37. // console
  38. {"Save logs",
  39. {""},
  40. 1,
  41. {"savelogs"},
  42. NO_ARGS,
  43. FOCUS_CONSOLE_START,
  44. SHOW_STOPSCAN_TIP},
  45. // help
  46. {"Help",
  47. {""},
  48. 1,
  49. {"help"},
  50. NO_ARGS,
  51. FOCUS_CONSOLE_START,
  52. SHOW_STOPSCAN_TIP},
  53. };
  54. static void evil_portal_scene_start_var_list_enter_callback(void *context,
  55. uint32_t index) {
  56. furi_assert(context);
  57. Evil_PortalApp *app = context;
  58. furi_assert(index < NUM_MENU_ITEMS);
  59. const Evil_PortalItem *item = &items[index];
  60. const int selected_option_index = app->selected_option_index[index];
  61. furi_assert(selected_option_index < item->num_options_menu);
  62. app->selected_tx_string = item->actual_commands[selected_option_index];
  63. app->is_command = true;
  64. app->is_custom_tx_string = false;
  65. app->selected_menu_index = index;
  66. app->focus_console_start = (item->focus_console == FOCUS_CONSOLE_TOGGLE)
  67. ? (selected_option_index == 0)
  68. : item->focus_console;
  69. app->show_stopscan_tip = item->show_stopscan_tip;
  70. view_dispatcher_send_custom_event(app->view_dispatcher,
  71. Evil_PortalEventStartConsole);
  72. }
  73. static void
  74. evil_portal_scene_start_var_list_change_callback(VariableItem *item) {
  75. furi_assert(item);
  76. Evil_PortalApp *app = variable_item_get_context(item);
  77. furi_assert(app);
  78. const Evil_PortalItem *menu_item = &items[app->selected_menu_index];
  79. uint8_t item_index = variable_item_get_current_value_index(item);
  80. furi_assert(item_index < menu_item->num_options_menu);
  81. variable_item_set_current_value_text(item,
  82. menu_item->options_menu[item_index]);
  83. app->selected_option_index[app->selected_menu_index] = item_index;
  84. }
  85. void evil_portal_scene_start_on_enter(void *context) {
  86. Evil_PortalApp *app = context;
  87. VariableItemList *var_item_list = app->var_item_list;
  88. variable_item_list_set_enter_callback(
  89. var_item_list, evil_portal_scene_start_var_list_enter_callback, app);
  90. VariableItem *item;
  91. for (int i = 0; i < NUM_MENU_ITEMS; ++i) {
  92. item = variable_item_list_add(
  93. var_item_list, items[i].item_string, items[i].num_options_menu,
  94. evil_portal_scene_start_var_list_change_callback, app);
  95. variable_item_set_current_value_index(item, app->selected_option_index[i]);
  96. variable_item_set_current_value_text(
  97. item, items[i].options_menu[app->selected_option_index[i]]);
  98. }
  99. variable_item_list_set_selected_item(
  100. var_item_list,
  101. scene_manager_get_scene_state(app->scene_manager, Evil_PortalSceneStart));
  102. view_dispatcher_switch_to_view(app->view_dispatcher,
  103. Evil_PortalAppViewVarItemList);
  104. }
  105. bool evil_portal_scene_start_on_event(void *context, SceneManagerEvent event) {
  106. UNUSED(context);
  107. Evil_PortalApp *app = context;
  108. bool consumed = false;
  109. if (event.type == SceneManagerEventTypeCustom) {
  110. if (event.event == Evil_PortalEventStartPortal) {
  111. scene_manager_set_scene_state(app->scene_manager, Evil_PortalSceneStart,
  112. app->selected_menu_index);
  113. scene_manager_next_scene(app->scene_manager,
  114. Evil_PortalAppViewStartPortal);
  115. } else if (event.event == Evil_PortalEventStartKeyboard) {
  116. scene_manager_set_scene_state(app->scene_manager, Evil_PortalSceneStart,
  117. app->selected_menu_index);
  118. } else if (event.event == Evil_PortalEventStartConsole) {
  119. scene_manager_set_scene_state(app->scene_manager, Evil_PortalSceneStart,
  120. app->selected_menu_index);
  121. scene_manager_next_scene(app->scene_manager,
  122. Evil_PortalAppViewConsoleOutput);
  123. }
  124. consumed = true;
  125. } else if (event.type == SceneManagerEventTypeTick) {
  126. app->selected_menu_index =
  127. variable_item_list_get_selected_item_index(app->var_item_list);
  128. consumed = true;
  129. }
  130. return consumed;
  131. }
  132. void evil_portal_scene_start_on_exit(void *context) {
  133. Evil_PortalApp *app = context;
  134. variable_item_list_reset(app->var_item_list);
  135. }