evil_portal_scene_start.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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", {""}, 1, {"sethtml"}, NO_ARGS, FOCUS_CONSOLE_START, NO_TIP},
  29. // stop portal
  30. {"Stop portal", {""}, 1, {"reset"}, NO_ARGS, FOCUS_CONSOLE_START, NO_TIP},
  31. // console
  32. {"Save logs",
  33. {""},
  34. 1,
  35. {"savelogs"},
  36. NO_ARGS,
  37. FOCUS_CONSOLE_START,
  38. SHOW_STOPSCAN_TIP},
  39. // help
  40. {"Help",
  41. {""},
  42. 1,
  43. {"help"},
  44. NO_ARGS,
  45. FOCUS_CONSOLE_START,
  46. SHOW_STOPSCAN_TIP},
  47. };
  48. static void evil_portal_scene_start_var_list_enter_callback(void *context,
  49. uint32_t index) {
  50. furi_assert(context);
  51. Evil_PortalApp *app = context;
  52. furi_assert(index < NUM_MENU_ITEMS);
  53. const Evil_PortalItem *item = &items[index];
  54. const int selected_option_index = app->selected_option_index[index];
  55. furi_assert(selected_option_index < item->num_options_menu);
  56. app->selected_tx_string = item->actual_commands[selected_option_index];
  57. app->is_command = true;
  58. app->is_custom_tx_string = false;
  59. app->selected_menu_index = index;
  60. app->focus_console_start = (item->focus_console == FOCUS_CONSOLE_TOGGLE)
  61. ? (selected_option_index == 0)
  62. : item->focus_console;
  63. app->show_stopscan_tip = item->show_stopscan_tip;
  64. // bool needs_keyboard = (item->needs_keyboard == TOGGLE_ARGS)
  65. // ? (selected_option_index != 0)
  66. // : item->needs_keyboard;
  67. // if (needs_keyboard) {
  68. // view_dispatcher_send_custom_event(app->view_dispatcher,
  69. // Evil_PortalEventStartKeyboard);
  70. // } else {
  71. view_dispatcher_send_custom_event(app->view_dispatcher,
  72. Evil_PortalEventStartConsole);
  73. // }
  74. }
  75. static void
  76. evil_portal_scene_start_var_list_change_callback(VariableItem *item) {
  77. furi_assert(item);
  78. Evil_PortalApp *app = variable_item_get_context(item);
  79. furi_assert(app);
  80. const Evil_PortalItem *menu_item = &items[app->selected_menu_index];
  81. uint8_t item_index = variable_item_get_current_value_index(item);
  82. furi_assert(item_index < menu_item->num_options_menu);
  83. variable_item_set_current_value_text(item,
  84. menu_item->options_menu[item_index]);
  85. app->selected_option_index[app->selected_menu_index] = item_index;
  86. }
  87. void evil_portal_scene_start_on_enter(void *context) {
  88. Evil_PortalApp *app = context;
  89. VariableItemList *var_item_list = app->var_item_list;
  90. variable_item_list_set_enter_callback(
  91. var_item_list, evil_portal_scene_start_var_list_enter_callback, app);
  92. VariableItem *item;
  93. for (int i = 0; i < NUM_MENU_ITEMS; ++i) {
  94. item = variable_item_list_add(
  95. var_item_list, items[i].item_string, items[i].num_options_menu,
  96. evil_portal_scene_start_var_list_change_callback, app);
  97. variable_item_set_current_value_index(item, app->selected_option_index[i]);
  98. variable_item_set_current_value_text(
  99. item, items[i].options_menu[app->selected_option_index[i]]);
  100. }
  101. variable_item_list_set_selected_item(
  102. var_item_list,
  103. scene_manager_get_scene_state(app->scene_manager, Evil_PortalSceneStart));
  104. view_dispatcher_switch_to_view(app->view_dispatcher,
  105. Evil_PortalAppViewVarItemList);
  106. }
  107. bool evil_portal_scene_start_on_event(void *context, SceneManagerEvent event) {
  108. UNUSED(context);
  109. Evil_PortalApp *app = context;
  110. bool consumed = false;
  111. if (event.type == SceneManagerEventTypeCustom) {
  112. if (event.event == Evil_PortalEventStartPortal) {
  113. scene_manager_set_scene_state(app->scene_manager, Evil_PortalSceneStart,
  114. app->selected_menu_index);
  115. scene_manager_next_scene(app->scene_manager, Evil_PortalAppViewStartPortal);
  116. } else if (event.event == Evil_PortalEventStartKeyboard) {
  117. scene_manager_set_scene_state(app->scene_manager, Evil_PortalSceneStart,
  118. app->selected_menu_index);
  119. } else if (event.event == Evil_PortalEventStartConsole) {
  120. scene_manager_set_scene_state(app->scene_manager, Evil_PortalSceneStart,
  121. app->selected_menu_index);
  122. scene_manager_next_scene(app->scene_manager,
  123. Evil_PortalAppViewConsoleOutput);
  124. }
  125. consumed = true;
  126. } else if (event.type == SceneManagerEventTypeTick) {
  127. app->selected_menu_index =
  128. variable_item_list_get_selected_item_index(app->var_item_list);
  129. consumed = true;
  130. }
  131. return consumed;
  132. }
  133. void evil_portal_scene_start_on_exit(void *context) {
  134. Evil_PortalApp *app = context;
  135. variable_item_list_reset(app->var_item_list);
  136. }