hex_viewer_startscreen.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. #include "../hex_viewer.h"
  2. #include <furi.h>
  3. #include <furi_hal.h>
  4. #include <input/input.h>
  5. #include <gui/elements.h>
  6. struct HexViewerStartscreen {
  7. View* view;
  8. HexViewerStartscreenCallback callback;
  9. void* context;
  10. };
  11. typedef struct {
  12. uint8_t file_bytes[HEX_VIEWER_LINES_ON_SCREEN][HEX_VIEWER_BYTES_PER_LINE];
  13. uint32_t file_offset;
  14. uint32_t file_read_bytes;
  15. uint32_t file_size;
  16. bool mode; // Print address or content
  17. } HexViewerStartscreenModel;
  18. void hex_viewer_startscreen_set_callback(
  19. HexViewerStartscreen* instance,
  20. HexViewerStartscreenCallback callback,
  21. void* context) {
  22. furi_assert(instance);
  23. furi_assert(callback);
  24. instance->callback = callback;
  25. instance->context = context;
  26. }
  27. void hex_viewer_startscreen_draw(Canvas* canvas, HexViewerStartscreenModel* model) {
  28. UNUSED(model);
  29. canvas_clear(canvas);
  30. if (!model->file_size) {
  31. canvas_set_color(canvas, ColorBlack);
  32. canvas_set_font(canvas, FontPrimary);
  33. canvas_draw_str_aligned(canvas, 64, 10, AlignCenter, AlignTop, "HexViewer v2.0");
  34. canvas_set_font(canvas, FontSecondary);
  35. canvas_draw_str_aligned(canvas, 64, 22, AlignCenter, AlignTop, "Basic hex viewer");
  36. canvas_draw_str_aligned(canvas, 64, 32, AlignCenter, AlignTop, "for your Flipper");
  37. elements_button_center(canvas, "Open");
  38. } else {
  39. canvas_set_color(canvas, ColorBlack);
  40. elements_button_left(canvas, model->mode ? "Addr" : "Text");
  41. elements_button_right(canvas, "Info");
  42. elements_button_center(canvas, "Menu");
  43. int ROW_HEIGHT = 12;
  44. int TOP_OFFSET = 10;
  45. int LEFT_OFFSET = 3;
  46. uint32_t line_count = model->file_size / HEX_VIEWER_BYTES_PER_LINE;
  47. if(model->file_size % HEX_VIEWER_BYTES_PER_LINE != 0) line_count += 1;
  48. uint32_t first_line_on_screen = model->file_offset / HEX_VIEWER_BYTES_PER_LINE;
  49. if(line_count > HEX_VIEWER_LINES_ON_SCREEN) {
  50. uint8_t width = canvas_width(canvas);
  51. elements_scrollbar_pos(
  52. canvas,
  53. width,
  54. 0,
  55. ROW_HEIGHT * HEX_VIEWER_LINES_ON_SCREEN,
  56. first_line_on_screen, // TODO
  57. line_count - (HEX_VIEWER_LINES_ON_SCREEN - 1));
  58. }
  59. char temp_buf[32];
  60. uint32_t row_iters = model->file_read_bytes / HEX_VIEWER_BYTES_PER_LINE;
  61. if(model->file_read_bytes % HEX_VIEWER_BYTES_PER_LINE != 0) row_iters += 1;
  62. for(uint32_t i = 0; i < row_iters; ++i) {
  63. uint32_t bytes_left_per_row =
  64. model->file_read_bytes - i * HEX_VIEWER_BYTES_PER_LINE;
  65. bytes_left_per_row = MIN(bytes_left_per_row, HEX_VIEWER_BYTES_PER_LINE);
  66. if(model->mode) {
  67. memcpy(temp_buf, model->file_bytes[i], bytes_left_per_row);
  68. temp_buf[bytes_left_per_row] = '\0';
  69. for(uint32_t j = 0; j < bytes_left_per_row; ++j)
  70. if(!isprint((int)temp_buf[j])) temp_buf[j] = '.';
  71. canvas_set_font(canvas, FontKeyboard);
  72. canvas_draw_str(canvas, LEFT_OFFSET, TOP_OFFSET + i * ROW_HEIGHT, temp_buf);
  73. } else {
  74. uint32_t addr = model->file_offset + i * HEX_VIEWER_BYTES_PER_LINE;
  75. snprintf(temp_buf, 32, "%04lX", addr);
  76. canvas_set_font(canvas, FontKeyboard);
  77. canvas_draw_str(canvas, LEFT_OFFSET, TOP_OFFSET + i * ROW_HEIGHT, temp_buf);
  78. }
  79. char* p = temp_buf;
  80. for(uint32_t j = 0; j < bytes_left_per_row; ++j)
  81. p += snprintf(p, 32, "%02X ", model->file_bytes[i][j]);
  82. canvas_set_font(canvas, FontKeyboard);
  83. canvas_draw_str(canvas, LEFT_OFFSET + 41, TOP_OFFSET + i * ROW_HEIGHT, temp_buf);
  84. }
  85. }
  86. }
  87. static void hex_viewer_startscreen_model_init(HexViewerStartscreenModel* const model) {
  88. memset(model->file_bytes, 0, sizeof(model->file_bytes));
  89. model->file_offset = 0;
  90. model->file_read_bytes = 0;
  91. model->file_size = 0;
  92. model->mode = false;
  93. }
  94. static void update_local_model_from_app(HexViewer* const app, HexViewerStartscreenModel* const model)
  95. {
  96. memcpy(model->file_bytes, app->model->file_bytes, sizeof(model->file_bytes));
  97. model->file_offset = app->model->file_offset;
  98. model->file_read_bytes = app->model->file_read_bytes;
  99. model->file_size = app->model->file_size;
  100. model->mode = app->model->mode;
  101. }
  102. bool hex_viewer_startscreen_input(InputEvent* event, void* context) {
  103. furi_assert(context);
  104. HexViewerStartscreen* instance = context;
  105. if (event->type == InputTypeRelease) {
  106. switch(event->key) {
  107. case InputKeyBack:
  108. with_view_model(
  109. instance->view,
  110. HexViewerStartscreenModel * model,
  111. {
  112. instance->callback(HexViewerCustomEventStartscreenBack, instance->context);
  113. update_local_model_from_app(instance->context, model);
  114. },
  115. true);
  116. break;
  117. case InputKeyLeft:
  118. with_view_model(
  119. instance->view,
  120. HexViewerStartscreenModel * model,
  121. {
  122. UNUSED(model);
  123. instance->callback(HexViewerCustomEventStartscreenLeft, instance->context);
  124. update_local_model_from_app(instance->context, model);
  125. },
  126. true);
  127. break;
  128. case InputKeyRight:
  129. with_view_model(
  130. instance->view,
  131. HexViewerStartscreenModel * model,
  132. {
  133. UNUSED(model);
  134. instance->callback(HexViewerCustomEventStartscreenRight, instance->context);
  135. update_local_model_from_app(instance->context, model);
  136. },
  137. true);
  138. break;
  139. case InputKeyUp:
  140. with_view_model(
  141. instance->view,
  142. HexViewerStartscreenModel * model,
  143. {
  144. UNUSED(model);
  145. instance->callback(HexViewerCustomEventStartscreenUp, instance->context);
  146. update_local_model_from_app(instance->context, model);
  147. },
  148. true);
  149. break;
  150. case InputKeyDown:
  151. with_view_model(
  152. instance->view,
  153. HexViewerStartscreenModel * model,
  154. {
  155. UNUSED(model);
  156. instance->callback(HexViewerCustomEventStartscreenDown, instance->context);
  157. update_local_model_from_app(instance->context, model);
  158. },
  159. true);
  160. break;
  161. case InputKeyOk:
  162. with_view_model(
  163. instance->view,
  164. HexViewerStartscreenModel* model,
  165. {
  166. UNUSED(model);
  167. instance->callback(HexViewerCustomEventStartscreenOk, instance->context);
  168. update_local_model_from_app(instance->context, model);
  169. },
  170. true);
  171. break;
  172. case InputKeyMAX:
  173. break;
  174. }
  175. }
  176. return true;
  177. }
  178. void hex_viewer_startscreen_exit(void* context) {
  179. furi_assert(context);
  180. }
  181. void hex_viewer_startscreen_enter(void* context) {
  182. furi_assert(context);
  183. HexViewerStartscreen* instance = (HexViewerStartscreen*)context;
  184. with_view_model(
  185. instance->view,
  186. HexViewerStartscreenModel * model,
  187. {
  188. hex_viewer_startscreen_model_init(model);
  189. // TODO update_local_model_from_app(instance->context, model);
  190. },
  191. true
  192. );
  193. }
  194. HexViewerStartscreen* hex_viewer_startscreen_alloc() {
  195. HexViewerStartscreen* instance = malloc(sizeof(HexViewerStartscreen));
  196. instance->view = view_alloc();
  197. view_allocate_model(instance->view, ViewModelTypeLocking, sizeof(HexViewerStartscreenModel));
  198. view_set_context(instance->view, instance); // furi_assert crashes in events without this
  199. view_set_draw_callback(instance->view, (ViewDrawCallback)hex_viewer_startscreen_draw);
  200. view_set_input_callback(instance->view, hex_viewer_startscreen_input);
  201. //view_set_enter_callback(instance->view, hex_viewer_startscreen_enter);
  202. //view_set_exit_callback(instance->view, hex_viewer_startscreen_exit);
  203. with_view_model(
  204. instance->view,
  205. HexViewerStartscreenModel * model,
  206. {
  207. hex_viewer_startscreen_model_init(model);
  208. },
  209. true
  210. );
  211. return instance;
  212. }
  213. void hex_viewer_startscreen_free(HexViewerStartscreen* instance) {
  214. furi_assert(instance);
  215. with_view_model(
  216. instance->view,
  217. HexViewerStartscreenModel * model,
  218. {
  219. UNUSED(model);
  220. },
  221. true);
  222. view_free(instance->view);
  223. free(instance);
  224. }
  225. View* hex_viewer_startscreen_get_view(HexViewerStartscreen* instance) {
  226. furi_assert(instance);
  227. return instance->view;
  228. }