main_menu.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. #include "main_menu.h"
  2. #include "../fuzzer_i.h"
  3. #include <input/input.h>
  4. #include "../lib/worker/protocol.h"
  5. #define PROTOCOL_NAME_Y 12
  6. // #define PROTOCOL_CAROUSEL
  7. struct FuzzerViewMain {
  8. View* view;
  9. FuzzerViewMainCallback callback;
  10. void* context;
  11. };
  12. typedef struct {
  13. uint8_t proto_index;
  14. uint8_t menu_index;
  15. uint8_t proto_max;
  16. uint8_t menu_max;
  17. } FuzzerViewMainModel;
  18. void fuzzer_view_main_update_data(FuzzerViewMain* view, FuzzerState state) {
  19. furi_assert(view);
  20. with_view_model(
  21. view->view,
  22. FuzzerViewMainModel * model,
  23. {
  24. model->proto_index = state.proto_index;
  25. model->menu_index = state.menu_index;
  26. },
  27. true);
  28. }
  29. void fuzzer_view_main_get_state(FuzzerViewMain* view, FuzzerState* state) {
  30. furi_assert(view);
  31. with_view_model(
  32. view->view,
  33. FuzzerViewMainModel * model,
  34. {
  35. state->proto_index = model->proto_index;
  36. state->menu_index = model->menu_index;
  37. },
  38. true);
  39. }
  40. void fuzzer_view_main_set_callback(
  41. FuzzerViewMain* view,
  42. FuzzerViewMainCallback callback,
  43. void* context) {
  44. furi_assert(view);
  45. view->callback = callback;
  46. view->context = context;
  47. }
  48. void fuzzer_view_main_draw(Canvas* canvas, FuzzerViewMainModel* model) {
  49. canvas_clear(canvas);
  50. canvas_set_color(canvas, ColorBlack);
  51. if(model->menu_index > 0) {
  52. canvas_set_font(canvas, FontSecondary);
  53. canvas_draw_str_aligned(
  54. canvas,
  55. 64,
  56. 24,
  57. AlignCenter,
  58. AlignTop,
  59. fuzzer_proto_get_menu_label(model->menu_index - 1));
  60. }
  61. canvas_set_font(canvas, FontPrimary);
  62. canvas_draw_str_aligned(
  63. canvas, 64, 36, AlignCenter, AlignTop, fuzzer_proto_get_menu_label(model->menu_index));
  64. if(model->menu_index < (model->menu_max - 1)) {
  65. canvas_set_font(canvas, FontSecondary);
  66. canvas_draw_str_aligned(
  67. canvas,
  68. 64,
  69. 48,
  70. AlignCenter,
  71. AlignTop,
  72. fuzzer_proto_get_menu_label(model->menu_index + 1));
  73. }
  74. canvas_set_font(canvas, FontPrimary);
  75. canvas_draw_str_aligned(canvas, 27, PROTOCOL_NAME_Y, AlignCenter, AlignBottom, "<");
  76. canvas_draw_str_aligned(
  77. canvas,
  78. 64,
  79. PROTOCOL_NAME_Y,
  80. AlignCenter,
  81. AlignBottom,
  82. fuzzer_proto_get_name(model->proto_index));
  83. canvas_draw_str_aligned(canvas, 101, PROTOCOL_NAME_Y, AlignCenter, AlignBottom, ">");
  84. #ifdef PROTOCOL_CAROUSEL
  85. canvas_set_font(canvas, FontSecondary);
  86. canvas_draw_str_aligned(
  87. canvas,
  88. 20,
  89. PROTOCOL_NAME_Y,
  90. AlignRight,
  91. AlignBottom,
  92. (model->proto_index > 0) ? fuzzer_proto_get_name(model->proto_index - 1) :
  93. fuzzer_proto_get_name((model->proto_max - 1)));
  94. canvas_draw_str_aligned(
  95. canvas,
  96. 108,
  97. PROTOCOL_NAME_Y,
  98. AlignLeft,
  99. AlignBottom,
  100. (model->proto_index < (model->proto_max - 1)) ?
  101. fuzzer_proto_get_name(model->proto_index + 1) :
  102. fuzzer_proto_get_name(0));
  103. #endif
  104. }
  105. bool fuzzer_view_main_input(InputEvent* event, void* context) {
  106. furi_assert(context);
  107. FuzzerViewMain* view = context;
  108. if(event->key == InputKeyBack &&
  109. (event->type == InputTypeLong || event->type == InputTypeShort)) {
  110. view->callback(FuzzerCustomEventViewMainBack, view->context);
  111. return true;
  112. } else if(event->key == InputKeyOk && event->type == InputTypeShort) {
  113. view->callback(FuzzerCustomEventViewMainOk, view->context);
  114. return true;
  115. } else if(event->key == InputKeyDown && event->type == InputTypeShort) {
  116. with_view_model(
  117. view->view,
  118. FuzzerViewMainModel * model,
  119. {
  120. if(model->menu_index < (model->menu_max - 1)) {
  121. model->menu_index++;
  122. }
  123. },
  124. true);
  125. return true;
  126. } else if(event->key == InputKeyUp && event->type == InputTypeShort) {
  127. with_view_model(
  128. view->view,
  129. FuzzerViewMainModel * model,
  130. {
  131. if(model->menu_index != 0) {
  132. model->menu_index--;
  133. }
  134. },
  135. true);
  136. return true;
  137. } else if(event->key == InputKeyLeft && event->type == InputTypeShort) {
  138. with_view_model(
  139. view->view,
  140. FuzzerViewMainModel * model,
  141. {
  142. if(model->proto_index != 0) {
  143. model->proto_index--;
  144. } else {
  145. model->proto_index = (model->proto_max - 1);
  146. }
  147. },
  148. true);
  149. return true;
  150. } else if(event->key == InputKeyRight && event->type == InputTypeShort) {
  151. with_view_model(
  152. view->view,
  153. FuzzerViewMainModel * model,
  154. {
  155. if(model->proto_index == (model->proto_max - 1)) {
  156. model->proto_index = 0;
  157. } else {
  158. model->proto_index++;
  159. }
  160. },
  161. true);
  162. return true;
  163. }
  164. return true;
  165. }
  166. void fuzzer_view_main_enter(void* context) {
  167. furi_assert(context);
  168. }
  169. void fuzzer_view_main_exit(void* context) {
  170. furi_assert(context);
  171. }
  172. FuzzerViewMain* fuzzer_view_main_alloc() {
  173. FuzzerViewMain* view = malloc(sizeof(FuzzerViewMain));
  174. // View allocation and configuration
  175. view->view = view_alloc();
  176. view_allocate_model(view->view, ViewModelTypeLocking, sizeof(FuzzerViewMainModel));
  177. view_set_context(view->view, view);
  178. view_set_draw_callback(view->view, (ViewDrawCallback)fuzzer_view_main_draw);
  179. view_set_input_callback(view->view, fuzzer_view_main_input);
  180. view_set_enter_callback(view->view, fuzzer_view_main_enter);
  181. view_set_exit_callback(view->view, fuzzer_view_main_exit);
  182. with_view_model(
  183. view->view,
  184. FuzzerViewMainModel * model,
  185. {
  186. model->proto_index = 0;
  187. model->proto_max = fuzzer_proto_get_count_of_protocols();
  188. model->menu_index = 0;
  189. model->menu_max = fuzzer_proto_get_count_of_menu_items();
  190. },
  191. true);
  192. return view;
  193. }
  194. void fuzzer_view_main_free(FuzzerViewMain* view) {
  195. furi_assert(view);
  196. // with_view_model(
  197. // view->view,
  198. // FuzzerViewMainModel * model,
  199. // {
  200. // },
  201. // true);
  202. view_free(view->view);
  203. free(view);
  204. }
  205. View* fuzzer_view_main_get_view(FuzzerViewMain* view) {
  206. furi_assert(view);
  207. return view->view;
  208. }