hex_editor.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. #include <stdio.h>
  2. #include <furi.h>
  3. #include <gui/gui.h>
  4. #include <gui/elements.h>
  5. #include <dialogs/dialogs.h>
  6. #include <input/input.h>
  7. #include <notification/notification_messages.h>
  8. #include <storage/storage.h>
  9. #include <stream/stream.h>
  10. #include <stream/buffered_file_stream.h>
  11. #include <toolbox/stream/file_stream.h>
  12. #include <hex_editor_icons.h>
  13. // #include <assets_icons.h>
  14. #define TAG "HexEditor"
  15. typedef struct {
  16. // uint8_t file_bytes[HEX_editor_LINES_ON_SCREEN][HEX_editor_BYTES_PER_LINE];
  17. uint32_t file_offset;
  18. uint32_t file_read_bytes;
  19. uint32_t file_size;
  20. uint8_t string_offset;
  21. char editable_char;
  22. Stream* stream;
  23. bool mode; // Print address or content
  24. } HexEditorModel;
  25. typedef struct {
  26. HexEditorModel* model;
  27. FuriMutex** mutex;
  28. FuriMessageQueue* input_queue;
  29. ViewPort* view_port;
  30. Gui* gui;
  31. Storage* storage;
  32. FuriString* buffer;
  33. } HexEditor;
  34. static void draw_callback(Canvas* canvas, void* ctx) {
  35. // UNUSED(ctx);
  36. HexEditor* hex_editor = ctx;
  37. canvas_clear(canvas);
  38. canvas_set_font(canvas, FontPrimary);
  39. canvas_draw_str(canvas, 0, 10, "Line and mode:");
  40. // elements_button_right(canvas, "Info");
  41. // // elements_string_fit_width(canvas, buffer, 100);
  42. canvas_set_font(canvas, FontSecondary);
  43. canvas_draw_str_aligned(
  44. canvas,
  45. 0,
  46. 20,
  47. AlignLeft,
  48. AlignBottom,
  49. furi_string_get_cstr(hex_editor->buffer) + hex_editor->model->string_offset);
  50. // elements_scrollable_text_line(
  51. // canvas, 0, 20, 128, hex_editor->buffer, hex_editor->model->string_offset, false);
  52. // canvas_draw_line(canvas, 3, 20, 5, 30);
  53. canvas_draw_icon(canvas, 0, 20, &I_Pin_arrow_up_7x9);
  54. if(hex_editor->model->mode) {
  55. elements_button_left(canvas, "ASCII -");
  56. elements_button_right(canvas, "ASCII +");
  57. } else {
  58. elements_button_left(canvas, "");
  59. elements_button_right(canvas, "");
  60. }
  61. canvas_set_font(canvas, FontPrimary);
  62. canvas_draw_glyph(canvas, 0, 45, '0' + hex_editor->model->mode);
  63. canvas_draw_glyph(canvas, 30, 45, hex_editor->model->editable_char);
  64. }
  65. static void input_callback(InputEvent* input_event, void* ctx) {
  66. // Проверяем, что контекст не нулевой
  67. furi_assert(ctx);
  68. HexEditor* hex_editor = ctx;
  69. furi_message_queue_put(hex_editor->input_queue, input_event, 100);
  70. }
  71. static HexEditor* hex_editor_alloc() {
  72. HexEditor* instance = malloc(sizeof(HexEditor));
  73. instance->model = malloc(sizeof(HexEditorModel));
  74. memset(instance->model, 0x0, sizeof(HexEditorModel));
  75. instance->model->editable_char = ' ';
  76. instance->mutex = furi_mutex_alloc(FuriMutexTypeNormal);
  77. instance->input_queue = furi_message_queue_alloc(8, sizeof(InputEvent));
  78. instance->view_port = view_port_alloc();
  79. view_port_draw_callback_set(instance->view_port, draw_callback, instance);
  80. view_port_input_callback_set(instance->view_port, input_callback, instance);
  81. instance->gui = furi_record_open(RECORD_GUI);
  82. gui_add_view_port(instance->gui, instance->view_port, GuiLayerFullscreen);
  83. instance->storage = furi_record_open(RECORD_STORAGE);
  84. instance->buffer = furi_string_alloc();
  85. return instance;
  86. }
  87. static void hex_editor_free(HexEditor* instance) {
  88. furi_record_close(RECORD_STORAGE);
  89. gui_remove_view_port(instance->gui, instance->view_port);
  90. furi_record_close(RECORD_GUI);
  91. view_port_free(instance->view_port);
  92. furi_message_queue_free(instance->input_queue);
  93. furi_mutex_free(instance->mutex);
  94. if(instance->model->stream) buffered_file_stream_close(instance->model->stream);
  95. furi_string_free(instance->buffer);
  96. free(instance->model);
  97. free(instance);
  98. }
  99. static bool hex_editor_open_file(HexEditor* hex_editor, const char* file_path) {
  100. furi_assert(hex_editor);
  101. furi_assert(file_path);
  102. hex_editor->model->stream = buffered_file_stream_alloc(hex_editor->storage);
  103. bool isOk = true;
  104. do {
  105. if(!buffered_file_stream_open(
  106. hex_editor->model->stream, file_path, FSAM_READ_WRITE, FSOM_OPEN_EXISTING)) {
  107. FURI_LOG_E(TAG, "Unable to open stream: %s", file_path);
  108. isOk = false;
  109. break;
  110. };
  111. hex_editor->model->file_size = stream_size(hex_editor->model->stream);
  112. } while(false);
  113. return isOk;
  114. }
  115. // static bool hex_editor_read_file(HexEditor* hex_editor) {
  116. // furi_assert(hex_editor);
  117. // furi_assert(hex_editor->model->stream);
  118. // // furi_assert(hex_editor->model->file_offset % hex_editor_BYTES_PER_LINE == 0);
  119. // memset(hex_editor->model->file_bytes, 0x0, hex_editor_BUF_SIZE);
  120. // bool isOk = true;
  121. // do {
  122. // uint32_t offset = hex_editor->model->file_offset;
  123. // if(!stream_seek(hex_editor->model->stream, offset, true)) {
  124. // FURI_LOG_E(TAG, "Unable to seek stream");
  125. // isOk = false;
  126. // break;
  127. // }
  128. // hex_editor->model->file_read_bytes = stream_read(
  129. // hex_editor->model->stream,
  130. // (uint8_t*)hex_editor->model->file_bytes,
  131. // hex_editor_BUF_SIZE);
  132. // } while(false);
  133. // return isOk;
  134. // }
  135. int32_t hex_editor_app(void* p) {
  136. UNUSED(p);
  137. HexEditor* hex_editor = hex_editor_alloc();
  138. FuriString* file_path;
  139. file_path = furi_string_alloc();
  140. // furi_string_printf(
  141. // hex_editor->buffer,
  142. // "qqqqq1231231232343454565676urtfgsdfascesc\nasdqwe\new ra sssssssssssssssssssssssssqqqqqqqqqqq1231231232343454565676urtfgsdfascesc\nq2e");
  143. do {
  144. if(p && strlen(p)) {
  145. furi_string_set(file_path, (const char*)p);
  146. } else {
  147. furi_string_set(file_path, "/any");
  148. DialogsFileBrowserOptions browser_options;
  149. dialog_file_browser_set_basic_options(&browser_options, "*", &I_edit_10px);
  150. browser_options.hide_ext = false;
  151. DialogsApp* dialogs = furi_record_open(RECORD_DIALOGS);
  152. bool res = dialog_file_browser_show(dialogs, file_path, file_path, &browser_options);
  153. furi_record_close(RECORD_DIALOGS);
  154. if(!res) {
  155. FURI_LOG_I(TAG, "No file selected");
  156. break;
  157. }
  158. }
  159. FURI_LOG_I(TAG, "File selected: %s", furi_string_get_cstr(file_path));
  160. if(!hex_editor_open_file(hex_editor, furi_string_get_cstr(file_path))) break;
  161. if(!stream_read_line(hex_editor->model->stream, hex_editor->buffer)) {
  162. FURI_LOG_T(TAG, "No keys left in dict");
  163. break;
  164. }
  165. InputEvent event;
  166. int8_t off;
  167. while(1) {
  168. // Выбираем событие из очереди в переменную event (ждем бесконечно долго, если очередь пуста)
  169. // и проверяем, что у нас получилось это сделать
  170. furi_check(
  171. furi_message_queue_get(hex_editor->input_queue, &event, FuriWaitForever) ==
  172. FuriStatusOk);
  173. // Если нажата кнопка "назад", то выходим из цикла, а следовательно и из приложения
  174. if(event.type == InputTypeShort || event.type == InputTypeRepeat) {
  175. if(!hex_editor->model->mode) {
  176. off = 1;
  177. if(event.type == InputTypeRepeat) {
  178. off = 2;
  179. }
  180. if(event.key == InputKeyRight) {
  181. hex_editor->model->string_offset += off;
  182. if(hex_editor->model->string_offset >=
  183. furi_string_size(hex_editor->buffer)) {
  184. // dengeros
  185. hex_editor->model->string_offset -=
  186. furi_string_size(hex_editor->buffer);
  187. }
  188. }
  189. if(event.key == InputKeyLeft) {
  190. if(hex_editor->model->string_offset - off < 0) {
  191. // dengeros
  192. hex_editor->model->string_offset +=
  193. furi_string_size(hex_editor->buffer);
  194. }
  195. hex_editor->model->string_offset -= off;
  196. }
  197. if(event.key == InputKeyDown) {
  198. hex_editor->model->string_offset = 0;
  199. if(!stream_read_line(hex_editor->model->stream, hex_editor->buffer)) {
  200. FURI_LOG_T(TAG, "No keys left in dict");
  201. }
  202. }
  203. if(event.key == InputKeyUp) {
  204. hex_editor->model->string_offset = 0;
  205. // TODO asert
  206. if(!stream_seek(hex_editor->model->stream, -1, StreamOffsetFromCurrent)) {
  207. FURI_LOG_E(TAG, "Unable to seek stream");
  208. break;
  209. }
  210. // NOT work on first line
  211. stream_seek_to_char(
  212. hex_editor->model->stream, '\n', StreamDirectionBackward);
  213. // if(!stream_seek(hex_editor->model->stream, -1, StreamOffsetFromCurrent)) {
  214. // FURI_LOG_E(TAG, "Unable to seek stream");
  215. // break;
  216. // }
  217. if(!stream_seek_to_char(
  218. hex_editor->model->stream, '\n', StreamDirectionBackward)) {
  219. stream_rewind(hex_editor->model->stream);
  220. } else {
  221. if(!stream_seek(
  222. hex_editor->model->stream, 1, StreamOffsetFromCurrent)) {
  223. FURI_LOG_E(TAG, "Unable to seek stream");
  224. break;
  225. }
  226. }
  227. if(!stream_read_line(hex_editor->model->stream, hex_editor->buffer)) {
  228. FURI_LOG_T(TAG, "No keys left in dict");
  229. break;
  230. }
  231. }
  232. if(event.key == InputKeyOk) {
  233. hex_editor->model->editable_char = furi_string_get_char(
  234. hex_editor->buffer, hex_editor->model->string_offset);
  235. hex_editor->model->mode = 1;
  236. }
  237. } else {
  238. off = 1;
  239. if(event.type == InputTypeRepeat) {
  240. off = 4;
  241. }
  242. if(event.key == InputKeyRight) {
  243. hex_editor->model->editable_char += off;
  244. }
  245. if(event.key == InputKeyLeft) {
  246. hex_editor->model->editable_char -= off;
  247. }
  248. if(event.key == InputKeyOk) {
  249. if(!stream_seek(hex_editor->model->stream, -1, StreamOffsetFromCurrent)) {
  250. FURI_LOG_E(TAG, "Unable to seek stream");
  251. break;
  252. }
  253. stream_seek_to_char(
  254. hex_editor->model->stream, '\n', StreamDirectionBackward);
  255. stream_seek(
  256. hex_editor->model->stream,
  257. hex_editor->model->string_offset + 1,
  258. StreamOffsetFromCurrent);
  259. stream_write_char(
  260. hex_editor->model->stream, hex_editor->model->editable_char);
  261. hex_editor->model->editable_char = ' ';
  262. hex_editor->model->mode = 0;
  263. stream_seek_to_char(
  264. hex_editor->model->stream, '\n', StreamDirectionBackward);
  265. if(!stream_seek(hex_editor->model->stream, 1, StreamOffsetFromCurrent)) {
  266. FURI_LOG_E(TAG, "Unable to seek stream");
  267. break;
  268. }
  269. if(!stream_read_line(hex_editor->model->stream, hex_editor->buffer)) {
  270. FURI_LOG_T(TAG, "No keys left in dict");
  271. break;
  272. }
  273. }
  274. }
  275. }
  276. if(event.key == InputKeyBack) {
  277. break;
  278. }
  279. // ?
  280. view_port_update(hex_editor->view_port);
  281. }
  282. } while(false);
  283. furi_string_free(file_path);
  284. hex_editor_free(hex_editor);
  285. return 0;
  286. }