hex_viewer_startscreen.c 8.9 KB

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