subbrute_attack_view.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. #include "subbrute_attack_view.h"
  2. #include "../subbrute_i.h"
  3. #include "assets_icons.h"
  4. #include <input/input.h>
  5. #include <gui/elements.h>
  6. #include <gui/icon_i.h>
  7. #include <gui/icon_animation_i.h>
  8. #define TAG "SubBruteAttackView"
  9. struct SubBruteAttackView {
  10. View* view;
  11. SubBruteAttackViewCallback callback;
  12. void* context;
  13. };
  14. typedef struct {
  15. SubBruteAttacks index;
  16. uint64_t max_value;
  17. uint64_t current_step;
  18. bool is_attacking;
  19. bool is_continuous_worker;
  20. IconAnimation* icon;
  21. } SubBruteAttackViewModel;
  22. void subbrute_attack_view_set_callback(
  23. SubBruteAttackView* instance,
  24. SubBruteAttackViewCallback callback,
  25. void* context) {
  26. furi_assert(instance);
  27. furi_assert(callback);
  28. instance->callback = callback;
  29. instance->context = context;
  30. }
  31. bool subbrute_attack_view_input(InputEvent* event, void* context) {
  32. furi_assert(event);
  33. furi_assert(context);
  34. #ifdef FURI_DEBUG
  35. FURI_LOG_D(TAG, "InputKey: %d", event->key);
  36. #endif
  37. SubBruteAttackView* instance = context;
  38. if(event->key == InputKeyBack && event->type == InputTypeShort) {
  39. instance->callback(SubBruteCustomEventTypeBackPressed, instance->context);
  40. with_view_model(
  41. instance->view, (SubBruteAttackViewModel * model) {
  42. model->is_attacking = false;
  43. model->is_continuous_worker = false;
  44. return true;
  45. });
  46. return true;
  47. }
  48. bool is_attacking = false;
  49. with_view_model(
  50. instance->view, (SubBruteAttackViewModel * model) {
  51. is_attacking = model->is_attacking;
  52. return false;
  53. });
  54. // if(!is_attacking) {
  55. // instance->callback(SubBruteCustomEventTypeTransmitNotStarted, instance->context);
  56. // } else {
  57. // instance->callback(SubBruteCustomEventTypeTransmitStarted, instance->context);
  58. // }
  59. if(!is_attacking) {
  60. if(event->type == InputTypeShort && event->key == InputKeyOk) {
  61. #ifdef FURI_DEBUG
  62. FURI_LOG_D(TAG, "InputKey: %d OK", event->key);
  63. #endif
  64. with_view_model(
  65. instance->view, (SubBruteAttackViewModel * model) {
  66. model->is_attacking = true;
  67. model->is_continuous_worker = false;
  68. icon_animation_stop(model->icon);
  69. icon_animation_start(model->icon);
  70. return true;
  71. });
  72. instance->callback(SubBruteCustomEventTypeTransmitStarted, instance->context);
  73. /*if(event->type == InputTypeRepeat && event->key == InputKeyOk) {
  74. #ifdef FURI_DEBUG
  75. FURI_LOG_D(TAG, "InputKey: %d OK. SubBruteCustomEventTypeTransmitContinuousStarted", event->key);
  76. #endif
  77. with_view_model(
  78. instance->view, (SubBruteAttackViewModel * model) {
  79. model->is_attacking = true;
  80. model->is_continuous_worker = true;
  81. icon_animation_stop(model->icon);
  82. icon_animation_start(model->icon);
  83. return true;
  84. });
  85. instance->callback(SubBruteCustomEventTypeTransmitContinuousStarted, instance->context);
  86. } else if(event->type == InputTypeShort && event->key == InputKeyOk) {
  87. #ifdef FURI_DEBUG
  88. FURI_LOG_D(TAG, "InputKey: %d OK", event->key);
  89. #endif
  90. with_view_model(
  91. instance->view, (SubBruteAttackViewModel * model) {
  92. model->is_attacking = true;
  93. model->is_continuous_worker = false;
  94. icon_animation_stop(model->icon);
  95. icon_animation_start(model->icon);
  96. return true;
  97. });
  98. instance->callback(SubBruteCustomEventTypeTransmitStarted, instance->context);*/
  99. } else if(event->key == InputKeyUp) {
  100. instance->callback(SubBruteCustomEventTypeSaveFile, instance->context);
  101. } else if(event->key == InputKeyDown) {
  102. instance->callback(SubBruteCustomEventTypeTransmitCustom, instance->context);
  103. } else if(event->type == InputTypeShort) {
  104. if(event->key == InputKeyLeft) {
  105. instance->callback(SubBruteCustomEventTypeChangeStepDown, instance->context);
  106. } else if(event->key == InputKeyRight) {
  107. instance->callback(SubBruteCustomEventTypeChangeStepUp, instance->context);
  108. }
  109. // with_view_model(
  110. // instance->view, (SubBruteAttackViewModel * model) {
  111. // if(event->key == InputKeyLeft) {
  112. // model->current_step =
  113. // ((model->current_step - 1) + model->max_value) % model->max_value;
  114. // } else if(event->key == InputKeyRight) {
  115. // model->current_step = (model->current_step + 1) % model->max_value;
  116. // }
  117. // return true;
  118. // });
  119. // instance->callback(SubBruteCustomEventTypeChangeStep, instance->context);
  120. } else if(event->type == InputTypeRepeat) {
  121. if(event->key == InputKeyLeft) {
  122. instance->callback(SubBruteCustomEventTypeChangeStepDownMore, instance->context);
  123. } else if(event->key == InputKeyRight) {
  124. instance->callback(SubBruteCustomEventTypeChangeStepUpMore, instance->context);
  125. }
  126. /*with_view_model(
  127. instance->view, (SubBruteAttackViewModel * model) {
  128. if(event->key == InputKeyLeft) {
  129. model->current_step =
  130. ((model->current_step - 100) + model->max_value) % model->max_value;
  131. } else if(event->key == InputKeyRight) {
  132. model->current_step = (model->current_step + 100) % model->max_value;
  133. }
  134. return true;
  135. });
  136. instance->callback(SubBruteCustomEventTypeChangeStep, instance->context);*/
  137. }
  138. } else {
  139. if((event->type == InputTypeShort || event->type == InputTypeRepeat) &&
  140. (event->key == InputKeyOk || event->key == InputKeyBack)) {
  141. with_view_model(
  142. instance->view, (SubBruteAttackViewModel * model) {
  143. model->is_attacking = false;
  144. model->is_continuous_worker = false;
  145. icon_animation_stop(model->icon);
  146. icon_animation_start(model->icon);
  147. return true;
  148. });
  149. instance->callback(SubBruteCustomEventTypeTransmitNotStarted, instance->context);
  150. }
  151. }
  152. return true;
  153. }
  154. SubBruteAttackView* subbrute_attack_view_alloc() {
  155. SubBruteAttackView* instance = malloc(sizeof(SubBruteAttackView));
  156. instance->view = view_alloc();
  157. view_allocate_model(instance->view, ViewModelTypeLocking, sizeof(SubBruteAttackViewModel));
  158. view_set_context(instance->view, instance);
  159. with_view_model(
  160. instance->view, (SubBruteAttackViewModel * model) {
  161. model->icon = icon_animation_alloc(&A_Sub1ghz_14);
  162. view_tie_icon_animation(instance->view, model->icon);
  163. return false;
  164. });
  165. view_set_draw_callback(instance->view, (ViewDrawCallback)subbrute_attack_view_draw);
  166. view_set_input_callback(instance->view, subbrute_attack_view_input);
  167. view_set_enter_callback(instance->view, subbrute_attack_view_enter);
  168. view_set_exit_callback(instance->view, subbrute_attack_view_exit);
  169. return instance;
  170. }
  171. void subbrute_attack_view_enter(void* context) {
  172. furi_assert(context);
  173. #ifdef FURI_DEBUG
  174. FURI_LOG_D(TAG, "subbrute_attack_view_enter");
  175. #endif
  176. }
  177. void subbrute_attack_view_free(SubBruteAttackView* instance) {
  178. furi_assert(instance);
  179. #ifdef FURI_DEBUG
  180. FURI_LOG_D(TAG, "subbrute_attack_view_free");
  181. #endif
  182. with_view_model(
  183. instance->view, (SubBruteAttackViewModel * model) {
  184. icon_animation_free(model->icon);
  185. return false;
  186. });
  187. view_free(instance->view);
  188. free(instance);
  189. }
  190. View* subbrute_attack_view_get_view(SubBruteAttackView* instance) {
  191. furi_assert(instance);
  192. return instance->view;
  193. }
  194. void subbrute_attack_view_set_current_step(SubBruteAttackView* instance, uint64_t current_step) {
  195. furi_assert(instance);
  196. #ifdef FURI_DEBUG
  197. //FURI_LOG_D(TAG, "Set step: %d", current_step);
  198. #endif
  199. with_view_model(
  200. instance->view, (SubBruteAttackViewModel * model) {
  201. model->current_step = current_step;
  202. return true;
  203. });
  204. }
  205. void subbrute_attack_view_set_worker_type(SubBruteAttackView* instance, bool is_continuous_worker) {
  206. furi_assert(instance);
  207. with_view_model(
  208. instance->view, (SubBruteAttackViewModel * model) {
  209. model->is_continuous_worker = is_continuous_worker;
  210. return true;
  211. });
  212. }
  213. // We need to call init every time, because not every time we calls enter
  214. // normally, call enter only once
  215. void subbrute_attack_view_init_values(
  216. SubBruteAttackView* instance,
  217. uint8_t index,
  218. uint64_t max_value,
  219. uint64_t current_step,
  220. bool is_attacking) {
  221. #ifdef FURI_DEBUG
  222. FURI_LOG_D(
  223. TAG, "init, index: %d, max_value: %d, current_step: %d", index, max_value, current_step);
  224. #endif
  225. with_view_model(
  226. instance->view, (SubBruteAttackViewModel * model) {
  227. model->max_value = max_value;
  228. model->index = index;
  229. model->current_step = current_step;
  230. model->is_attacking = is_attacking;
  231. if(is_attacking) {
  232. icon_animation_start(model->icon);
  233. } else {
  234. icon_animation_stop(model->icon);
  235. }
  236. return true;
  237. });
  238. }
  239. void subbrute_attack_view_exit(void* context) {
  240. furi_assert(context);
  241. SubBruteAttackView* instance = context;
  242. #ifdef FURI_DEBUG
  243. FURI_LOG_D(TAG, "subbrute_attack_view_exit");
  244. #endif
  245. with_view_model(
  246. instance->view, (SubBruteAttackViewModel * model) {
  247. icon_animation_stop(model->icon);
  248. return false;
  249. });
  250. }
  251. void elements_button_top_left(Canvas* canvas, const char* str) {
  252. const Icon* icon = &I_ButtonUp_7x4;
  253. const uint8_t button_height = 12;
  254. const uint8_t vertical_offset = 9; //
  255. const uint8_t horizontal_offset = 3;
  256. const uint8_t string_width = canvas_string_width(canvas, str);
  257. const uint8_t icon_h_offset = 3;
  258. const uint8_t icon_width_with_offset = icon->width + icon_h_offset;
  259. const uint8_t icon_v_offset = icon->height; //
  260. const uint8_t button_width = string_width + horizontal_offset * 2 + icon_width_with_offset + 1;
  261. const uint8_t x = 0;
  262. const uint8_t y = 0;
  263. canvas_draw_box(canvas, x, y, button_width, button_height);
  264. #ifdef FURI_DEBUG
  265. FURI_LOG_D(
  266. TAG, "lbox, x: %d, y: %d, width: %d, height: %d", x, y, button_width, button_height);
  267. #endif
  268. // canvas_draw_line(canvas, x + button_width + 0, y, x + button_width + 0, y + button_height - 0); //
  269. // canvas_draw_line(canvas, x + button_width + 1, y, x + button_width + 1, y + button_height - 1);
  270. // canvas_draw_line(canvas, x + button_width + 2, y, x + button_width + 2, y + button_height - 2);
  271. canvas_invert_color(canvas);
  272. canvas_draw_icon(canvas, x + horizontal_offset, y + icon_v_offset, icon);
  273. canvas_draw_str(
  274. canvas, x + horizontal_offset + icon_width_with_offset, y + vertical_offset, str);
  275. canvas_invert_color(canvas);
  276. }
  277. void elements_button_top_right(Canvas* canvas, const char* str) {
  278. const Icon* icon = &I_ButtonDown_7x4;
  279. const uint8_t button_height = 12;
  280. const uint8_t vertical_offset = 9;
  281. const uint8_t horizontal_offset = 3;
  282. const uint8_t string_width = canvas_string_width(canvas, str);
  283. const uint8_t icon_h_offset = 3;
  284. const uint8_t icon_width_with_offset = icon->width + icon_h_offset;
  285. const uint8_t icon_v_offset = icon->height; // + vertical_offset;
  286. const uint8_t button_width = string_width + horizontal_offset * 2 + icon_width_with_offset + 1;
  287. const uint8_t x = canvas_width(canvas);
  288. const uint8_t y = 0;
  289. canvas_draw_box(canvas, x - button_width, y, button_width, button_height);
  290. #ifdef FURI_DEBUG
  291. FURI_LOG_D(
  292. TAG,
  293. "rbox, x: %d, y: %d, width: %d, height: %d",
  294. x - button_width,
  295. y,
  296. button_width,
  297. button_height);
  298. #endif
  299. // canvas_draw_line(canvas, x - button_width - 1, y, x + button_width - 1, y + button_height - 0);
  300. // canvas_draw_line(canvas, x - button_width - 2, y, x + button_width - 2, y + button_height - 1);
  301. // canvas_draw_line(canvas, x - button_width - 3, y, x + button_width - 3, y + button_height - 2);
  302. canvas_invert_color(canvas);
  303. canvas_draw_str(canvas, x - button_width + horizontal_offset, y + vertical_offset, str);
  304. canvas_draw_icon(canvas, x - horizontal_offset - icon->width, y + icon_v_offset, icon);
  305. canvas_invert_color(canvas);
  306. }
  307. void subbrute_attack_view_draw(Canvas* canvas, void* context) {
  308. furi_assert(context);
  309. SubBruteAttackViewModel* model = (SubBruteAttackViewModel*)context;
  310. char buffer[26];
  311. const char* attack_name = NULL;
  312. attack_name = subbrute_get_menu_name(model->index);
  313. // Title
  314. if(model->is_attacking) {
  315. canvas_set_color(canvas, ColorBlack);
  316. canvas_set_font(canvas, FontSecondary);
  317. canvas_draw_str_aligned(canvas, 64, 2, AlignCenter, AlignTop, attack_name);
  318. }
  319. // Value
  320. canvas_set_font(canvas, FontBigNumbers);
  321. snprintf(buffer, sizeof(buffer), "%04d/%04d", (int)model->current_step, (int)model->max_value);
  322. canvas_draw_str_aligned(canvas, 64, 17, AlignCenter, AlignTop, buffer);
  323. canvas_set_font(canvas, FontSecondary);
  324. if(!model->is_attacking) {
  325. canvas_set_color(canvas, ColorBlack);
  326. canvas_set_font(canvas, FontSecondary);
  327. canvas_draw_str_aligned(canvas, 64, 44, AlignCenter, AlignBottom, attack_name);
  328. elements_button_left(canvas, "-1");
  329. elements_button_right(canvas, "+1");
  330. elements_button_center(canvas, "Start");
  331. elements_button_top_left(canvas, "Save");
  332. elements_button_top_right(canvas, "Resend");
  333. } else {
  334. if (model->is_continuous_worker) {
  335. canvas_invert_color(canvas);
  336. }
  337. // canvas_draw_icon_animation
  338. const uint8_t icon_h_offset = 0;
  339. const uint8_t icon_width_with_offset = model->icon->icon->width + icon_h_offset;
  340. const uint8_t icon_v_offset = model->icon->icon->height; // + vertical_offset;
  341. const uint8_t x = canvas_width(canvas);
  342. const uint8_t y = canvas_height(canvas);
  343. canvas_draw_icon_animation(
  344. canvas, x - icon_width_with_offset, y - icon_v_offset, model->icon);
  345. // Progress bar
  346. // Resolution: 128x64 px
  347. float progress_value = (float)model->current_step / model->max_value;
  348. elements_progress_bar(canvas, 8, 37, 110, progress_value > 1 ? 1 : progress_value);
  349. elements_button_center(canvas, "Stop");
  350. if (model->is_continuous_worker) {
  351. canvas_invert_color(canvas);
  352. }
  353. }
  354. }