evil_portal_scene_start.c 5.6 KB

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